blob: 68c564edb2c18c65829c954469ad5c708d05e559 [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>
Rusty Russell106a4ee2012-09-26 10:09:40 +010061#include "module-internal.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070062
Li Zefan7ead8b82009-08-17 16:56:28 +080063#define CREATE_TRACE_POINTS
64#include <trace/events/module.h>
65
Linus Torvalds1da177e2005-04-16 15:20:36 -070066#ifndef ARCH_SHF_SMALL
67#define ARCH_SHF_SMALL 0
68#endif
69
matthieu castet84e1c6b2010-11-16 22:35:16 +010070/*
71 * Modules' sections will be aligned on page boundaries
72 * to ensure complete separation of code and data, but
73 * only when CONFIG_DEBUG_SET_MODULE_RONX=y
74 */
75#ifdef CONFIG_DEBUG_SET_MODULE_RONX
76# define debug_align(X) ALIGN(X, PAGE_SIZE)
77#else
78# define debug_align(X) (X)
79#endif
80
81/*
82 * Given BASE and SIZE this macro calculates the number of pages the
83 * memory regions occupies
84 */
85#define MOD_NUMBER_OF_PAGES(BASE, SIZE) (((SIZE) > 0) ? \
86 (PFN_DOWN((unsigned long)(BASE) + (SIZE) - 1) - \
87 PFN_DOWN((unsigned long)BASE) + 1) \
88 : (0UL))
89
Linus Torvalds1da177e2005-04-16 15:20:36 -070090/* If this is set, the section belongs in the init part of the module */
91#define INIT_OFFSET_MASK (1UL << (BITS_PER_LONG-1))
92
Rusty Russell75676502010-06-05 11:17:36 -060093/*
94 * Mutex protects:
95 * 1) List of modules (also safely readable with preempt_disable),
96 * 2) module_use links,
97 * 3) module_addr_min/module_addr_max.
Andi Kleend72b3752008-08-30 10:09:00 +020098 * (delete uses stop_machine/add uses RCU list operations). */
Tim Abbottc6b37802008-12-05 19:03:59 -050099DEFINE_MUTEX(module_mutex);
100EXPORT_SYMBOL_GPL(module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101static LIST_HEAD(modules);
Jason Wessel67fc4e02010-05-20 21:04:21 -0500102#ifdef CONFIG_KGDB_KDB
103struct list_head *kdb_modules = &modules; /* kdb needs the list of modules */
104#endif /* CONFIG_KGDB_KDB */
105
Rusty Russell106a4ee2012-09-26 10:09:40 +0100106#ifdef CONFIG_MODULE_SIG
107#ifdef CONFIG_MODULE_SIG_FORCE
108static bool sig_enforce = true;
109#else
110static bool sig_enforce = false;
111
112static int param_set_bool_enable_only(const char *val,
113 const struct kernel_param *kp)
114{
115 int err;
116 bool test;
117 struct kernel_param dummy_kp = *kp;
118
119 dummy_kp.arg = &test;
120
121 err = param_set_bool(val, &dummy_kp);
122 if (err)
123 return err;
124
125 /* Don't let them unset it once it's set! */
126 if (!test && sig_enforce)
127 return -EROFS;
128
129 if (test)
130 sig_enforce = true;
131 return 0;
132}
133
134static const struct kernel_param_ops param_ops_bool_enable_only = {
135 .set = param_set_bool_enable_only,
136 .get = param_get_bool,
137};
138#define param_check_bool_enable_only param_check_bool
139
140module_param(sig_enforce, bool_enable_only, 0644);
141#endif /* !CONFIG_MODULE_SIG_FORCE */
142#endif /* CONFIG_MODULE_SIG */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143
Stephen Rothwell19e45292009-04-14 17:27:18 +1000144/* Block module loading/unloading? */
145int modules_disabled = 0;
Dave Young02608be2012-02-01 10:33:14 +0800146core_param(nomodule, modules_disabled, bint, 0);
Stephen Rothwell19e45292009-04-14 17:27:18 +1000147
Rusty Russellc9a3ba52008-01-29 17:13:18 -0500148/* Waiting for a module to finish initializing? */
149static DECLARE_WAIT_QUEUE_HEAD(module_wq);
150
Alan Sterne041c682006-03-27 01:16:30 -0800151static BLOCKING_NOTIFIER_HEAD(module_notify_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152
Rusty Russell75676502010-06-05 11:17:36 -0600153/* Bounds of module allocation, for speeding __module_address.
154 * Protected by module_mutex. */
Rusty Russell3a642e92008-07-22 19:24:28 -0500155static unsigned long module_addr_min = -1UL, module_addr_max = 0;
156
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157int register_module_notifier(struct notifier_block * nb)
158{
Alan Sterne041c682006-03-27 01:16:30 -0800159 return blocking_notifier_chain_register(&module_notify_list, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160}
161EXPORT_SYMBOL(register_module_notifier);
162
163int unregister_module_notifier(struct notifier_block * nb)
164{
Alan Sterne041c682006-03-27 01:16:30 -0800165 return blocking_notifier_chain_unregister(&module_notify_list, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166}
167EXPORT_SYMBOL(unregister_module_notifier);
168
Rusty Russelleded41c2010-08-05 12:59:07 -0600169struct load_info {
170 Elf_Ehdr *hdr;
171 unsigned long len;
172 Elf_Shdr *sechdrs;
Rusty Russell6526c532010-08-05 12:59:10 -0600173 char *secstrings, *strtab;
Rusty Russelld9131882010-08-05 12:59:08 -0600174 unsigned long symoffs, stroffs;
Rusty Russell811d66a2010-08-05 12:59:12 -0600175 struct _ddebug *debug;
176 unsigned int num_debug;
Rusty Russell106a4ee2012-09-26 10:09:40 +0100177 bool sig_ok;
Rusty Russelleded41c2010-08-05 12:59:07 -0600178 struct {
179 unsigned int sym, str, mod, vers, info, pcpu;
180 } index;
181};
182
Matti Linnanvuori9a4b9702007-11-08 08:37:38 -0800183/* We require a truly strong try_module_get(): 0 means failure due to
184 ongoing or failed initialization etc. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185static inline int strong_try_module_get(struct module *mod)
186{
187 if (mod && mod->state == MODULE_STATE_COMING)
Rusty Russellc9a3ba52008-01-29 17:13:18 -0500188 return -EBUSY;
189 if (try_module_get(mod))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 return 0;
Rusty Russellc9a3ba52008-01-29 17:13:18 -0500191 else
192 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193}
194
Florin Malitafa3ba2e82006-10-11 01:21:48 -0700195static inline void add_taint_module(struct module *mod, unsigned flag)
196{
197 add_taint(flag);
Andi Kleen25ddbb12008-10-15 22:01:41 -0700198 mod->taints |= (1U << flag);
Florin Malitafa3ba2e82006-10-11 01:21:48 -0700199}
200
Robert P. J. Day02a3e592007-05-09 07:26:28 +0200201/*
202 * A thread that wants to hold a reference to a module only while it
203 * is running can call this to safely exit. nfsd and lockd use this.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 */
205void __module_put_and_exit(struct module *mod, long code)
206{
207 module_put(mod);
208 do_exit(code);
209}
210EXPORT_SYMBOL(__module_put_and_exit);
Daniel Walker22a8bde2007-10-18 03:06:07 -0700211
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212/* Find a module section: 0 means not found. */
Rusty Russell49668682010-08-05 12:59:10 -0600213static unsigned int find_sec(const struct load_info *info, const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214{
215 unsigned int i;
216
Rusty Russell49668682010-08-05 12:59:10 -0600217 for (i = 1; i < info->hdr->e_shnum; i++) {
218 Elf_Shdr *shdr = &info->sechdrs[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 /* Alloc bit cleared means "ignore it." */
Rusty Russell49668682010-08-05 12:59:10 -0600220 if ((shdr->sh_flags & SHF_ALLOC)
221 && strcmp(info->secstrings + shdr->sh_name, name) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 return i;
Rusty Russell49668682010-08-05 12:59:10 -0600223 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 return 0;
225}
226
Rusty Russell5e458cc2008-10-22 10:00:13 -0500227/* Find a module section, or NULL. */
Rusty Russell49668682010-08-05 12:59:10 -0600228static void *section_addr(const struct load_info *info, const char *name)
Rusty Russell5e458cc2008-10-22 10:00:13 -0500229{
230 /* Section 0 has sh_addr 0. */
Rusty Russell49668682010-08-05 12:59:10 -0600231 return (void *)info->sechdrs[find_sec(info, name)].sh_addr;
Rusty Russell5e458cc2008-10-22 10:00:13 -0500232}
233
234/* Find a module section, or NULL. Fill in number of "objects" in section. */
Rusty Russell49668682010-08-05 12:59:10 -0600235static void *section_objs(const struct load_info *info,
Rusty Russell5e458cc2008-10-22 10:00:13 -0500236 const char *name,
237 size_t object_size,
238 unsigned int *num)
239{
Rusty Russell49668682010-08-05 12:59:10 -0600240 unsigned int sec = find_sec(info, name);
Rusty Russell5e458cc2008-10-22 10:00:13 -0500241
242 /* Section 0 has sh_addr 0 and sh_size 0. */
Rusty Russell49668682010-08-05 12:59:10 -0600243 *num = info->sechdrs[sec].sh_size / object_size;
244 return (void *)info->sechdrs[sec].sh_addr;
Rusty Russell5e458cc2008-10-22 10:00:13 -0500245}
246
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247/* Provided by the linker */
248extern const struct kernel_symbol __start___ksymtab[];
249extern const struct kernel_symbol __stop___ksymtab[];
250extern const struct kernel_symbol __start___ksymtab_gpl[];
251extern const struct kernel_symbol __stop___ksymtab_gpl[];
Greg Kroah-Hartman9f28bb72006-03-20 13:17:13 -0800252extern const struct kernel_symbol __start___ksymtab_gpl_future[];
253extern const struct kernel_symbol __stop___ksymtab_gpl_future[];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254extern const unsigned long __start___kcrctab[];
255extern const unsigned long __start___kcrctab_gpl[];
Greg Kroah-Hartman9f28bb72006-03-20 13:17:13 -0800256extern const unsigned long __start___kcrctab_gpl_future[];
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500257#ifdef CONFIG_UNUSED_SYMBOLS
258extern const struct kernel_symbol __start___ksymtab_unused[];
259extern const struct kernel_symbol __stop___ksymtab_unused[];
260extern const struct kernel_symbol __start___ksymtab_unused_gpl[];
261extern const struct kernel_symbol __stop___ksymtab_unused_gpl[];
Arjan van de Venf71d20e2006-06-28 04:26:45 -0700262extern const unsigned long __start___kcrctab_unused[];
263extern const unsigned long __start___kcrctab_unused_gpl[];
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500264#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265
266#ifndef CONFIG_MODVERSIONS
267#define symversion(base, idx) NULL
268#else
Andrew Mortonf83ca9f2006-03-28 01:56:20 -0800269#define symversion(base, idx) ((base != NULL) ? ((base) + (idx)) : NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270#endif
271
Rusty Russelldafd0942008-07-22 19:24:25 -0500272static bool each_symbol_in_section(const struct symsearch *arr,
273 unsigned int arrsize,
274 struct module *owner,
275 bool (*fn)(const struct symsearch *syms,
276 struct module *owner,
Rusty Russellde4d8d52011-04-19 21:49:58 +0200277 void *data),
Rusty Russelldafd0942008-07-22 19:24:25 -0500278 void *data)
Sam Ravnborg3fd68052006-02-08 21:16:45 +0100279{
Rusty Russellde4d8d52011-04-19 21:49:58 +0200280 unsigned int j;
Rusty Russelldafd0942008-07-22 19:24:25 -0500281
282 for (j = 0; j < arrsize; j++) {
Rusty Russellde4d8d52011-04-19 21:49:58 +0200283 if (fn(&arr[j], owner, data))
284 return true;
Rusty Russelldafd0942008-07-22 19:24:25 -0500285 }
286
287 return false;
Sam Ravnborg3fd68052006-02-08 21:16:45 +0100288}
289
Rusty Russelldafd0942008-07-22 19:24:25 -0500290/* Returns true as soon as fn returns true, otherwise false. */
Rusty Russellde4d8d52011-04-19 21:49:58 +0200291bool each_symbol_section(bool (*fn)(const struct symsearch *arr,
292 struct module *owner,
293 void *data),
294 void *data)
Arjan van de Venf71d20e2006-06-28 04:26:45 -0700295{
Rusty Russelldafd0942008-07-22 19:24:25 -0500296 struct module *mod;
Linus Torvalds44032e62010-08-05 12:59:05 -0600297 static const struct symsearch arr[] = {
Rusty Russelldafd0942008-07-22 19:24:25 -0500298 { __start___ksymtab, __stop___ksymtab, __start___kcrctab,
299 NOT_GPL_ONLY, false },
300 { __start___ksymtab_gpl, __stop___ksymtab_gpl,
301 __start___kcrctab_gpl,
302 GPL_ONLY, false },
303 { __start___ksymtab_gpl_future, __stop___ksymtab_gpl_future,
304 __start___kcrctab_gpl_future,
305 WILL_BE_GPL_ONLY, false },
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500306#ifdef CONFIG_UNUSED_SYMBOLS
Rusty Russelldafd0942008-07-22 19:24:25 -0500307 { __start___ksymtab_unused, __stop___ksymtab_unused,
308 __start___kcrctab_unused,
309 NOT_GPL_ONLY, true },
310 { __start___ksymtab_unused_gpl, __stop___ksymtab_unused_gpl,
311 __start___kcrctab_unused_gpl,
312 GPL_ONLY, true },
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500313#endif
Rusty Russelldafd0942008-07-22 19:24:25 -0500314 };
315
316 if (each_symbol_in_section(arr, ARRAY_SIZE(arr), NULL, fn, data))
317 return true;
318
Andi Kleend72b3752008-08-30 10:09:00 +0200319 list_for_each_entry_rcu(mod, &modules, list) {
Rusty Russelldafd0942008-07-22 19:24:25 -0500320 struct symsearch arr[] = {
321 { mod->syms, mod->syms + mod->num_syms, mod->crcs,
322 NOT_GPL_ONLY, false },
323 { mod->gpl_syms, mod->gpl_syms + mod->num_gpl_syms,
324 mod->gpl_crcs,
325 GPL_ONLY, false },
326 { mod->gpl_future_syms,
327 mod->gpl_future_syms + mod->num_gpl_future_syms,
328 mod->gpl_future_crcs,
329 WILL_BE_GPL_ONLY, false },
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500330#ifdef CONFIG_UNUSED_SYMBOLS
Rusty Russelldafd0942008-07-22 19:24:25 -0500331 { mod->unused_syms,
332 mod->unused_syms + mod->num_unused_syms,
333 mod->unused_crcs,
334 NOT_GPL_ONLY, true },
335 { mod->unused_gpl_syms,
336 mod->unused_gpl_syms + mod->num_unused_gpl_syms,
337 mod->unused_gpl_crcs,
338 GPL_ONLY, true },
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500339#endif
Rusty Russelldafd0942008-07-22 19:24:25 -0500340 };
341
342 if (each_symbol_in_section(arr, ARRAY_SIZE(arr), mod, fn, data))
343 return true;
344 }
345 return false;
Arjan van de Venf71d20e2006-06-28 04:26:45 -0700346}
Rusty Russellde4d8d52011-04-19 21:49:58 +0200347EXPORT_SYMBOL_GPL(each_symbol_section);
Arjan van de Venf71d20e2006-06-28 04:26:45 -0700348
Rusty Russelldafd0942008-07-22 19:24:25 -0500349struct find_symbol_arg {
350 /* Input */
351 const char *name;
352 bool gplok;
353 bool warn;
354
355 /* Output */
356 struct module *owner;
357 const unsigned long *crc;
Tim Abbott414fd312008-12-05 19:03:56 -0500358 const struct kernel_symbol *sym;
Rusty Russelldafd0942008-07-22 19:24:25 -0500359};
360
Rusty Russellde4d8d52011-04-19 21:49:58 +0200361static bool check_symbol(const struct symsearch *syms,
362 struct module *owner,
363 unsigned int symnum, void *data)
Rusty Russellad9546c2008-05-01 21:14:59 -0500364{
Rusty Russelldafd0942008-07-22 19:24:25 -0500365 struct find_symbol_arg *fsa = data;
366
Rusty Russelldafd0942008-07-22 19:24:25 -0500367 if (!fsa->gplok) {
368 if (syms->licence == GPL_ONLY)
369 return false;
370 if (syms->licence == WILL_BE_GPL_ONLY && fsa->warn) {
371 printk(KERN_WARNING "Symbol %s is being used "
372 "by a non-GPL module, which will not "
373 "be allowed in the future\n", fsa->name);
374 printk(KERN_WARNING "Please see the file "
375 "Documentation/feature-removal-schedule.txt "
376 "in the kernel source tree for more details.\n");
377 }
378 }
379
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500380#ifdef CONFIG_UNUSED_SYMBOLS
Rusty Russelldafd0942008-07-22 19:24:25 -0500381 if (syms->unused && fsa->warn) {
Rusty Russellad9546c2008-05-01 21:14:59 -0500382 printk(KERN_WARNING "Symbol %s is marked as UNUSED, "
Rusty Russelldafd0942008-07-22 19:24:25 -0500383 "however this module is using it.\n", fsa->name);
Rusty Russellad9546c2008-05-01 21:14:59 -0500384 printk(KERN_WARNING
385 "This symbol will go away in the future.\n");
386 printk(KERN_WARNING
387 "Please evalute if this is the right api to use and if "
388 "it really is, submit a report the linux kernel "
389 "mailinglist together with submitting your code for "
390 "inclusion.\n");
391 }
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500392#endif
Rusty Russelldafd0942008-07-22 19:24:25 -0500393
394 fsa->owner = owner;
395 fsa->crc = symversion(syms->crcs, symnum);
Tim Abbott414fd312008-12-05 19:03:56 -0500396 fsa->sym = &syms->start[symnum];
Rusty Russellad9546c2008-05-01 21:14:59 -0500397 return true;
398}
399
Alessio Igor Bogani403ed272011-04-20 11:10:52 +0200400static int cmp_name(const void *va, const void *vb)
401{
402 const char *a;
403 const struct kernel_symbol *b;
404 a = va; b = vb;
405 return strcmp(a, b->name);
406}
407
Rusty Russellde4d8d52011-04-19 21:49:58 +0200408static bool find_symbol_in_section(const struct symsearch *syms,
409 struct module *owner,
410 void *data)
411{
412 struct find_symbol_arg *fsa = data;
Alessio Igor Bogani403ed272011-04-20 11:10:52 +0200413 struct kernel_symbol *sym;
Rusty Russellde4d8d52011-04-19 21:49:58 +0200414
Alessio Igor Bogani403ed272011-04-20 11:10:52 +0200415 sym = bsearch(fsa->name, syms->start, syms->stop - syms->start,
416 sizeof(struct kernel_symbol), cmp_name);
417
418 if (sym != NULL && check_symbol(syms, owner, sym - syms->start, data))
419 return true;
420
Rusty Russellde4d8d52011-04-19 21:49:58 +0200421 return false;
422}
423
Tim Abbott414fd312008-12-05 19:03:56 -0500424/* Find a symbol and return it, along with, (optional) crc and
Rusty Russell75676502010-06-05 11:17:36 -0600425 * (optional) module which owns it. Needs preempt disabled or module_mutex. */
Tim Abbottc6b37802008-12-05 19:03:59 -0500426const struct kernel_symbol *find_symbol(const char *name,
427 struct module **owner,
428 const unsigned long **crc,
429 bool gplok,
430 bool warn)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431{
Rusty Russelldafd0942008-07-22 19:24:25 -0500432 struct find_symbol_arg fsa;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433
Rusty Russelldafd0942008-07-22 19:24:25 -0500434 fsa.name = name;
435 fsa.gplok = gplok;
436 fsa.warn = warn;
437
Rusty Russellde4d8d52011-04-19 21:49:58 +0200438 if (each_symbol_section(find_symbol_in_section, &fsa)) {
Rusty Russellad9546c2008-05-01 21:14:59 -0500439 if (owner)
Rusty Russelldafd0942008-07-22 19:24:25 -0500440 *owner = fsa.owner;
441 if (crc)
442 *crc = fsa.crc;
Tim Abbott414fd312008-12-05 19:03:56 -0500443 return fsa.sym;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 }
Rusty Russellad9546c2008-05-01 21:14:59 -0500445
Jim Cromie5e124162011-12-06 12:11:31 -0700446 pr_debug("Failed to find symbol %s\n", name);
Tim Abbott414fd312008-12-05 19:03:56 -0500447 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448}
Tim Abbottc6b37802008-12-05 19:03:59 -0500449EXPORT_SYMBOL_GPL(find_symbol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451/* Search for module by name: must hold module_mutex. */
Tim Abbottc6b37802008-12-05 19:03:59 -0500452struct module *find_module(const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453{
454 struct module *mod;
455
456 list_for_each_entry(mod, &modules, list) {
457 if (strcmp(mod->name, name) == 0)
458 return mod;
459 }
460 return NULL;
461}
Tim Abbottc6b37802008-12-05 19:03:59 -0500462EXPORT_SYMBOL_GPL(find_module);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463
464#ifdef CONFIG_SMP
Tejun Heofbf59bc2009-02-20 16:29:08 +0900465
Tejun Heo259354d2010-03-10 18:56:10 +0900466static inline void __percpu *mod_percpu(struct module *mod)
Tejun Heofbf59bc2009-02-20 16:29:08 +0900467{
Tejun Heo259354d2010-03-10 18:56:10 +0900468 return mod->percpu;
469}
Tejun Heofbf59bc2009-02-20 16:29:08 +0900470
Tejun Heo259354d2010-03-10 18:56:10 +0900471static int percpu_modalloc(struct module *mod,
472 unsigned long size, unsigned long align)
473{
Tejun Heofbf59bc2009-02-20 16:29:08 +0900474 if (align > PAGE_SIZE) {
475 printk(KERN_WARNING "%s: per-cpu alignment %li > %li\n",
Tejun Heo259354d2010-03-10 18:56:10 +0900476 mod->name, align, PAGE_SIZE);
Tejun Heofbf59bc2009-02-20 16:29:08 +0900477 align = PAGE_SIZE;
478 }
479
Tejun Heo259354d2010-03-10 18:56:10 +0900480 mod->percpu = __alloc_reserved_percpu(size, align);
481 if (!mod->percpu) {
Tejun Heofbf59bc2009-02-20 16:29:08 +0900482 printk(KERN_WARNING
Rusty Russelld9131882010-08-05 12:59:08 -0600483 "%s: Could not allocate %lu bytes percpu data\n",
484 mod->name, size);
Tejun Heo259354d2010-03-10 18:56:10 +0900485 return -ENOMEM;
486 }
487 mod->percpu_size = size;
488 return 0;
Tejun Heofbf59bc2009-02-20 16:29:08 +0900489}
490
Tejun Heo259354d2010-03-10 18:56:10 +0900491static void percpu_modfree(struct module *mod)
Tejun Heofbf59bc2009-02-20 16:29:08 +0900492{
Tejun Heo259354d2010-03-10 18:56:10 +0900493 free_percpu(mod->percpu);
Tejun Heofbf59bc2009-02-20 16:29:08 +0900494}
495
Rusty Russell49668682010-08-05 12:59:10 -0600496static unsigned int find_pcpusec(struct load_info *info)
Tejun Heo6b588c12009-02-20 16:29:07 +0900497{
Rusty Russell49668682010-08-05 12:59:10 -0600498 return find_sec(info, ".data..percpu");
Tejun Heo6b588c12009-02-20 16:29:07 +0900499}
500
Tejun Heo259354d2010-03-10 18:56:10 +0900501static void percpu_modcopy(struct module *mod,
502 const void *from, unsigned long size)
Tejun Heo6b588c12009-02-20 16:29:07 +0900503{
504 int cpu;
505
506 for_each_possible_cpu(cpu)
Tejun Heo259354d2010-03-10 18:56:10 +0900507 memcpy(per_cpu_ptr(mod->percpu, cpu), from, size);
Tejun Heo6b588c12009-02-20 16:29:07 +0900508}
509
Tejun Heo10fad5e2010-03-10 18:57:54 +0900510/**
511 * is_module_percpu_address - test whether address is from module static percpu
512 * @addr: address to test
513 *
514 * Test whether @addr belongs to module static percpu area.
515 *
516 * RETURNS:
517 * %true if @addr is from module static percpu area
518 */
519bool is_module_percpu_address(unsigned long addr)
520{
521 struct module *mod;
522 unsigned int cpu;
523
524 preempt_disable();
525
526 list_for_each_entry_rcu(mod, &modules, list) {
527 if (!mod->percpu_size)
528 continue;
529 for_each_possible_cpu(cpu) {
530 void *start = per_cpu_ptr(mod->percpu, cpu);
531
532 if ((void *)addr >= start &&
533 (void *)addr < start + mod->percpu_size) {
534 preempt_enable();
535 return true;
536 }
537 }
538 }
539
540 preempt_enable();
541 return false;
Daniel Walker22a8bde2007-10-18 03:06:07 -0700542}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543
544#else /* ... !CONFIG_SMP */
Tejun Heo6b588c12009-02-20 16:29:07 +0900545
Tejun Heo259354d2010-03-10 18:56:10 +0900546static inline void __percpu *mod_percpu(struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547{
548 return NULL;
549}
Tejun Heo259354d2010-03-10 18:56:10 +0900550static inline int percpu_modalloc(struct module *mod,
551 unsigned long size, unsigned long align)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552{
Tejun Heo259354d2010-03-10 18:56:10 +0900553 return -ENOMEM;
554}
555static inline void percpu_modfree(struct module *mod)
556{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557}
Rusty Russell49668682010-08-05 12:59:10 -0600558static unsigned int find_pcpusec(struct load_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559{
560 return 0;
561}
Tejun Heo259354d2010-03-10 18:56:10 +0900562static inline void percpu_modcopy(struct module *mod,
563 const void *from, unsigned long size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564{
565 /* pcpusec should be 0, and size of that section should be 0. */
566 BUG_ON(size != 0);
567}
Tejun Heo10fad5e2010-03-10 18:57:54 +0900568bool is_module_percpu_address(unsigned long addr)
569{
570 return false;
571}
Tejun Heo6b588c12009-02-20 16:29:07 +0900572
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573#endif /* CONFIG_SMP */
574
Matt Domschc988d2b2005-06-23 22:05:15 -0700575#define MODINFO_ATTR(field) \
576static void setup_modinfo_##field(struct module *mod, const char *s) \
577{ \
578 mod->field = kstrdup(s, GFP_KERNEL); \
579} \
580static ssize_t show_modinfo_##field(struct module_attribute *mattr, \
Kay Sievers4befb022011-07-24 22:06:04 +0930581 struct module_kobject *mk, char *buffer) \
Matt Domschc988d2b2005-06-23 22:05:15 -0700582{ \
Kay Sievers4befb022011-07-24 22:06:04 +0930583 return sprintf(buffer, "%s\n", mk->mod->field); \
Matt Domschc988d2b2005-06-23 22:05:15 -0700584} \
585static int modinfo_##field##_exists(struct module *mod) \
586{ \
587 return mod->field != NULL; \
588} \
589static void free_modinfo_##field(struct module *mod) \
590{ \
Daniel Walker22a8bde2007-10-18 03:06:07 -0700591 kfree(mod->field); \
592 mod->field = NULL; \
Matt Domschc988d2b2005-06-23 22:05:15 -0700593} \
594static struct module_attribute modinfo_##field = { \
Tejun Heo7b595752007-06-14 03:45:17 +0900595 .attr = { .name = __stringify(field), .mode = 0444 }, \
Matt Domschc988d2b2005-06-23 22:05:15 -0700596 .show = show_modinfo_##field, \
597 .setup = setup_modinfo_##field, \
598 .test = modinfo_##field##_exists, \
599 .free = free_modinfo_##field, \
600};
601
602MODINFO_ATTR(version);
603MODINFO_ATTR(srcversion);
604
Arjan van de Vene14af7e2008-01-25 21:08:33 +0100605static char last_unloaded_module[MODULE_NAME_LEN+1];
606
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -0800607#ifdef CONFIG_MODULE_UNLOAD
Steven Rostedteb0c5372010-03-29 14:25:18 -0400608
609EXPORT_TRACEPOINT_SYMBOL(module_get);
610
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611/* Init the unload section of the module. */
Rusty Russell9f85a4b2010-08-05 12:59:04 -0600612static int module_unload_init(struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613{
Rusty Russell9f85a4b2010-08-05 12:59:04 -0600614 mod->refptr = alloc_percpu(struct module_ref);
615 if (!mod->refptr)
616 return -ENOMEM;
617
Linus Torvalds2c02dfe2010-05-31 12:19:37 -0700618 INIT_LIST_HEAD(&mod->source_list);
619 INIT_LIST_HEAD(&mod->target_list);
Christoph Lametere1783a22010-01-05 15:34:50 +0900620
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 /* Hold reference count during initialization. */
Nick Piggin5fbfb182010-04-01 19:09:40 +1100622 __this_cpu_write(mod->refptr->incs, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 /* Backwards compatibility macros put refcount during init. */
624 mod->waiter = current;
Rusty Russell9f85a4b2010-08-05 12:59:04 -0600625
626 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627}
628
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629/* Does a already use b? */
630static int already_uses(struct module *a, struct module *b)
631{
632 struct module_use *use;
633
Linus Torvalds2c02dfe2010-05-31 12:19:37 -0700634 list_for_each_entry(use, &b->source_list, source_list) {
635 if (use->source == a) {
Jim Cromie5e124162011-12-06 12:11:31 -0700636 pr_debug("%s uses %s!\n", a->name, b->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 return 1;
638 }
639 }
Jim Cromie5e124162011-12-06 12:11:31 -0700640 pr_debug("%s does not use %s!\n", a->name, b->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 return 0;
642}
643
Linus Torvalds2c02dfe2010-05-31 12:19:37 -0700644/*
645 * Module a uses b
646 * - we add 'a' as a "source", 'b' as a "target" of module use
647 * - the module_use is added to the list of 'b' sources (so
648 * 'b' can walk the list to see who sourced them), and of 'a'
649 * targets (so 'a' can see what modules it targets).
650 */
651static int add_module_usage(struct module *a, struct module *b)
652{
Linus Torvalds2c02dfe2010-05-31 12:19:37 -0700653 struct module_use *use;
654
Jim Cromie5e124162011-12-06 12:11:31 -0700655 pr_debug("Allocating new usage for %s.\n", a->name);
Linus Torvalds2c02dfe2010-05-31 12:19:37 -0700656 use = kmalloc(sizeof(*use), GFP_ATOMIC);
657 if (!use) {
658 printk(KERN_WARNING "%s: out of memory loading\n", a->name);
659 return -ENOMEM;
660 }
661
662 use->source = a;
663 use->target = b;
664 list_add(&use->source_list, &b->source_list);
665 list_add(&use->target_list, &a->target_list);
Linus Torvalds2c02dfe2010-05-31 12:19:37 -0700666 return 0;
667}
668
Rusty Russell75676502010-06-05 11:17:36 -0600669/* Module a uses b: caller needs module_mutex() */
Rusty Russell9bea7f22010-06-05 11:17:37 -0600670int ref_module(struct module *a, struct module *b)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671{
Rusty Russellc8e21ce2010-06-05 11:17:35 -0600672 int err;
Kay Sievers270a6c42007-01-18 13:26:15 +0100673
Rusty Russell9bea7f22010-06-05 11:17:37 -0600674 if (b == NULL || already_uses(a, b))
Linus Torvalds218ce732010-05-25 16:48:30 -0700675 return 0;
Linus Torvalds218ce732010-05-25 16:48:30 -0700676
Rusty Russell9bea7f22010-06-05 11:17:37 -0600677 /* If module isn't available, we fail. */
678 err = strong_try_module_get(b);
Rusty Russellc9a3ba52008-01-29 17:13:18 -0500679 if (err)
Rusty Russell9bea7f22010-06-05 11:17:37 -0600680 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681
Linus Torvalds2c02dfe2010-05-31 12:19:37 -0700682 err = add_module_usage(a, b);
683 if (err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 module_put(b);
Rusty Russell9bea7f22010-06-05 11:17:37 -0600685 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 }
Rusty Russell9bea7f22010-06-05 11:17:37 -0600687 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688}
Rusty Russell9bea7f22010-06-05 11:17:37 -0600689EXPORT_SYMBOL_GPL(ref_module);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690
691/* Clear the unload stuff of the module. */
692static void module_unload_free(struct module *mod)
693{
Linus Torvalds2c02dfe2010-05-31 12:19:37 -0700694 struct module_use *use, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695
Rusty Russell75676502010-06-05 11:17:36 -0600696 mutex_lock(&module_mutex);
Linus Torvalds2c02dfe2010-05-31 12:19:37 -0700697 list_for_each_entry_safe(use, tmp, &mod->target_list, target_list) {
698 struct module *i = use->target;
Jim Cromie5e124162011-12-06 12:11:31 -0700699 pr_debug("%s unusing %s\n", mod->name, i->name);
Linus Torvalds2c02dfe2010-05-31 12:19:37 -0700700 module_put(i);
701 list_del(&use->source_list);
702 list_del(&use->target_list);
703 kfree(use);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 }
Rusty Russell75676502010-06-05 11:17:36 -0600705 mutex_unlock(&module_mutex);
Rusty Russell9f85a4b2010-08-05 12:59:04 -0600706
707 free_percpu(mod->refptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708}
709
710#ifdef CONFIG_MODULE_FORCE_UNLOAD
Akinobu Mitafb169792006-01-08 01:04:29 -0800711static inline int try_force_unload(unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712{
713 int ret = (flags & O_TRUNC);
714 if (ret)
Akinobu Mitafb169792006-01-08 01:04:29 -0800715 add_taint(TAINT_FORCED_RMMOD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 return ret;
717}
718#else
Akinobu Mitafb169792006-01-08 01:04:29 -0800719static inline int try_force_unload(unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720{
721 return 0;
722}
723#endif /* CONFIG_MODULE_FORCE_UNLOAD */
724
725struct stopref
726{
727 struct module *mod;
728 int flags;
729 int *forced;
730};
731
732/* Whole machine is stopped with interrupts off when this runs. */
733static int __try_stop_module(void *_sref)
734{
735 struct stopref *sref = _sref;
736
Rusty Russellda39ba52008-07-22 19:24:25 -0500737 /* If it's not unused, quit unless we're forcing. */
738 if (module_refcount(sref->mod) != 0) {
Akinobu Mitafb169792006-01-08 01:04:29 -0800739 if (!(*sref->forced = try_force_unload(sref->flags)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 return -EWOULDBLOCK;
741 }
742
743 /* Mark it as dying. */
744 sref->mod->state = MODULE_STATE_GOING;
745 return 0;
746}
747
748static int try_stop_module(struct module *mod, int flags, int *forced)
749{
Rusty Russellda39ba52008-07-22 19:24:25 -0500750 if (flags & O_NONBLOCK) {
751 struct stopref sref = { mod, flags, forced };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752
Rusty Russell9b1a4d32008-07-28 12:16:30 -0500753 return stop_machine(__try_stop_module, &sref, NULL);
Rusty Russellda39ba52008-07-22 19:24:25 -0500754 } else {
755 /* We don't need to stop the machine for this. */
756 mod->state = MODULE_STATE_GOING;
757 synchronize_sched();
758 return 0;
759 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760}
761
Eric Dumazetbd77c042012-01-13 09:32:14 +1030762unsigned long module_refcount(struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763{
Eric Dumazetbd77c042012-01-13 09:32:14 +1030764 unsigned long incs = 0, decs = 0;
Eric Dumazet720eba32009-02-03 13:31:36 +1030765 int cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766
Eric Dumazet720eba32009-02-03 13:31:36 +1030767 for_each_possible_cpu(cpu)
Nick Piggin5fbfb182010-04-01 19:09:40 +1100768 decs += per_cpu_ptr(mod->refptr, cpu)->decs;
769 /*
770 * ensure the incs are added up after the decs.
771 * module_put ensures incs are visible before decs with smp_wmb.
772 *
773 * This 2-count scheme avoids the situation where the refcount
774 * for CPU0 is read, then CPU0 increments the module refcount,
775 * then CPU1 drops that refcount, then the refcount for CPU1 is
776 * read. We would record a decrement but not its corresponding
777 * increment so we would see a low count (disaster).
778 *
779 * Rare situation? But module_refcount can be preempted, and we
780 * might be tallying up 4096+ CPUs. So it is not impossible.
781 */
782 smp_rmb();
783 for_each_possible_cpu(cpu)
784 incs += per_cpu_ptr(mod->refptr, cpu)->incs;
785 return incs - decs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786}
787EXPORT_SYMBOL(module_refcount);
788
789/* This exists whether we can unload or not */
790static void free_module(struct module *mod);
791
792static void wait_for_zero_refcount(struct module *mod)
793{
Matthew Wilcoxa6550202008-02-26 10:47:18 -0500794 /* Since we might sleep for some time, release the mutex first */
Ashutosh Naik6389a382006-03-23 03:00:46 -0800795 mutex_unlock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 for (;;) {
Jim Cromie5e124162011-12-06 12:11:31 -0700797 pr_debug("Looking at refcount...\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 set_current_state(TASK_UNINTERRUPTIBLE);
799 if (module_refcount(mod) == 0)
800 break;
801 schedule();
802 }
803 current->state = TASK_RUNNING;
Ashutosh Naik6389a382006-03-23 03:00:46 -0800804 mutex_lock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805}
806
Heiko Carstens17da2bd2009-01-14 14:14:10 +0100807SYSCALL_DEFINE2(delete_module, const char __user *, name_user,
808 unsigned int, flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809{
810 struct module *mod;
Greg Kroah-Hartmandfff0a02007-02-23 14:54:57 -0800811 char name[MODULE_NAME_LEN];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 int ret, forced = 0;
813
Kees Cook3d433212009-04-02 15:49:29 -0700814 if (!capable(CAP_SYS_MODULE) || modules_disabled)
Greg Kroah-Hartmandfff0a02007-02-23 14:54:57 -0800815 return -EPERM;
816
817 if (strncpy_from_user(name, name_user, MODULE_NAME_LEN-1) < 0)
818 return -EFAULT;
819 name[MODULE_NAME_LEN-1] = '\0';
820
Tejun Heo3fc1f1e2010-05-06 18:49:20 +0200821 if (mutex_lock_interruptible(&module_mutex) != 0)
822 return -EINTR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823
824 mod = find_module(name);
825 if (!mod) {
826 ret = -ENOENT;
827 goto out;
828 }
829
Linus Torvalds2c02dfe2010-05-31 12:19:37 -0700830 if (!list_empty(&mod->source_list)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 /* Other modules depend on us: get rid of them first. */
832 ret = -EWOULDBLOCK;
833 goto out;
834 }
835
836 /* Doing init or already dying? */
837 if (mod->state != MODULE_STATE_LIVE) {
838 /* FIXME: if (force), slam module count and wake up
839 waiter --RR */
Jim Cromie5e124162011-12-06 12:11:31 -0700840 pr_debug("%s already dying\n", mod->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 ret = -EBUSY;
842 goto out;
843 }
844
845 /* If it has an init func, it must have an exit func to unload */
Rusty Russellaf49d922007-10-16 23:26:27 -0700846 if (mod->init && !mod->exit) {
Akinobu Mitafb169792006-01-08 01:04:29 -0800847 forced = try_force_unload(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 if (!forced) {
849 /* This module can't be removed */
850 ret = -EBUSY;
851 goto out;
852 }
853 }
854
855 /* Set this up before setting mod->state */
856 mod->waiter = current;
857
858 /* Stop the machine so refcounts can't move and disable module. */
859 ret = try_stop_module(mod, flags, &forced);
860 if (ret != 0)
861 goto out;
862
863 /* Never wait if forced. */
864 if (!forced && module_refcount(mod) != 0)
865 wait_for_zero_refcount(mod);
866
Peter Oberparleiterdf4b5652008-04-21 14:34:31 +0200867 mutex_unlock(&module_mutex);
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300868 /* Final destruction now no one is using it. */
Peter Oberparleiterdf4b5652008-04-21 14:34:31 +0200869 if (mod->exit != NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 mod->exit();
Peter Oberparleiterdf4b5652008-04-21 14:34:31 +0200871 blocking_notifier_call_chain(&module_notify_list,
872 MODULE_STATE_GOING, mod);
Arjan van de Ven22a9d642009-01-07 08:45:46 -0800873 async_synchronize_full();
Rusty Russell75676502010-06-05 11:17:36 -0600874
Arjan van de Vene14af7e2008-01-25 21:08:33 +0100875 /* Store the name of the last unloaded module for diagnostic purposes */
Rusty Russellefa53452008-01-29 17:13:20 -0500876 strlcpy(last_unloaded_module, mod->name, sizeof(last_unloaded_module));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877
Rusty Russell75676502010-06-05 11:17:36 -0600878 free_module(mod);
879 return 0;
880out:
Ashutosh Naik6389a382006-03-23 03:00:46 -0800881 mutex_unlock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 return ret;
883}
884
Jianjun Kongd1e99d72008-12-08 14:26:29 +0800885static inline void print_unload_info(struct seq_file *m, struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886{
887 struct module_use *use;
888 int printed_something = 0;
889
Eric Dumazetbd77c042012-01-13 09:32:14 +1030890 seq_printf(m, " %lu ", module_refcount(mod));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891
892 /* Always include a trailing , so userspace can differentiate
893 between this and the old multi-field proc format. */
Linus Torvalds2c02dfe2010-05-31 12:19:37 -0700894 list_for_each_entry(use, &mod->source_list, source_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 printed_something = 1;
Linus Torvalds2c02dfe2010-05-31 12:19:37 -0700896 seq_printf(m, "%s,", use->source->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 }
898
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 if (mod->init != NULL && mod->exit == NULL) {
900 printed_something = 1;
901 seq_printf(m, "[permanent],");
902 }
903
904 if (!printed_something)
905 seq_printf(m, "-");
906}
907
908void __symbol_put(const char *symbol)
909{
910 struct module *owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911
Rusty Russell24da1cb2007-07-15 23:41:46 -0700912 preempt_disable();
Tim Abbott414fd312008-12-05 19:03:56 -0500913 if (!find_symbol(symbol, &owner, NULL, true, false))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 BUG();
915 module_put(owner);
Rusty Russell24da1cb2007-07-15 23:41:46 -0700916 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917}
918EXPORT_SYMBOL(__symbol_put);
919
Rusty Russell7d1d16e2009-08-26 22:02:54 +0930920/* Note this assumes addr is a function, which it currently always is. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921void symbol_put_addr(void *addr)
922{
Trent Piepho5e376612006-05-15 09:44:06 -0700923 struct module *modaddr;
Rusty Russell7d1d16e2009-08-26 22:02:54 +0930924 unsigned long a = (unsigned long)dereference_function_descriptor(addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925
Rusty Russell7d1d16e2009-08-26 22:02:54 +0930926 if (core_kernel_text(a))
Trent Piepho5e376612006-05-15 09:44:06 -0700927 return;
928
Rusty Russella6e6abd2009-03-31 13:05:31 -0600929 /* module_text_address is safe here: we're supposed to have reference
930 * to module from symbol_get, so it can't go away. */
Rusty Russell7d1d16e2009-08-26 22:02:54 +0930931 modaddr = __module_text_address(a);
Rusty Russella6e6abd2009-03-31 13:05:31 -0600932 BUG_ON(!modaddr);
Trent Piepho5e376612006-05-15 09:44:06 -0700933 module_put(modaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934}
935EXPORT_SYMBOL_GPL(symbol_put_addr);
936
937static ssize_t show_refcnt(struct module_attribute *mattr,
Kay Sievers4befb022011-07-24 22:06:04 +0930938 struct module_kobject *mk, char *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939{
Eric Dumazetbd77c042012-01-13 09:32:14 +1030940 return sprintf(buffer, "%lu\n", module_refcount(mk->mod));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941}
942
Kay Sieverscca3e702012-01-13 09:32:15 +1030943static struct module_attribute modinfo_refcnt =
944 __ATTR(refcnt, 0444, show_refcnt, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945
Steven Rostedtd53799b2012-03-26 12:50:52 +1030946void __module_get(struct module *module)
947{
948 if (module) {
949 preempt_disable();
950 __this_cpu_inc(module->refptr->incs);
951 trace_module_get(module, _RET_IP_);
952 preempt_enable();
953 }
954}
955EXPORT_SYMBOL(__module_get);
956
957bool try_module_get(struct module *module)
958{
959 bool ret = true;
960
961 if (module) {
962 preempt_disable();
963
964 if (likely(module_is_live(module))) {
965 __this_cpu_inc(module->refptr->incs);
966 trace_module_get(module, _RET_IP_);
967 } else
968 ret = false;
969
970 preempt_enable();
971 }
972 return ret;
973}
974EXPORT_SYMBOL(try_module_get);
975
Al Virof6a57032006-10-18 01:47:25 -0400976void module_put(struct module *module)
977{
978 if (module) {
Christoph Lametere1783a22010-01-05 15:34:50 +0900979 preempt_disable();
Nick Piggin5fbfb182010-04-01 19:09:40 +1100980 smp_wmb(); /* see comment in module_refcount */
981 __this_cpu_inc(module->refptr->decs);
Christoph Lametere1783a22010-01-05 15:34:50 +0900982
Li Zefanae832d12010-03-24 10:57:43 +0800983 trace_module_put(module, _RET_IP_);
Al Virof6a57032006-10-18 01:47:25 -0400984 /* Maybe they're waiting for us to drop reference? */
985 if (unlikely(!module_is_live(module)))
986 wake_up_process(module->waiter);
Christoph Lametere1783a22010-01-05 15:34:50 +0900987 preempt_enable();
Al Virof6a57032006-10-18 01:47:25 -0400988 }
989}
990EXPORT_SYMBOL(module_put);
991
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992#else /* !CONFIG_MODULE_UNLOAD */
Jianjun Kongd1e99d72008-12-08 14:26:29 +0800993static inline void print_unload_info(struct seq_file *m, struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994{
995 /* We don't know the usage count, or what modules are using. */
996 seq_printf(m, " - -");
997}
998
999static inline void module_unload_free(struct module *mod)
1000{
1001}
1002
Rusty Russell9bea7f22010-06-05 11:17:37 -06001003int ref_module(struct module *a, struct module *b)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004{
Rusty Russell9bea7f22010-06-05 11:17:37 -06001005 return strong_try_module_get(b);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006}
Rusty Russell9bea7f22010-06-05 11:17:37 -06001007EXPORT_SYMBOL_GPL(ref_module);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008
Rusty Russell9f85a4b2010-08-05 12:59:04 -06001009static inline int module_unload_init(struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010{
Rusty Russell9f85a4b2010-08-05 12:59:04 -06001011 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012}
1013#endif /* CONFIG_MODULE_UNLOAD */
1014
Kevin Winchester53999bf2012-01-15 19:32:55 -04001015static size_t module_flags_taint(struct module *mod, char *buf)
1016{
1017 size_t l = 0;
1018
1019 if (mod->taints & (1 << TAINT_PROPRIETARY_MODULE))
1020 buf[l++] = 'P';
1021 if (mod->taints & (1 << TAINT_OOT_MODULE))
1022 buf[l++] = 'O';
1023 if (mod->taints & (1 << TAINT_FORCED_MODULE))
1024 buf[l++] = 'F';
1025 if (mod->taints & (1 << TAINT_CRAP))
1026 buf[l++] = 'C';
1027 /*
1028 * TAINT_FORCED_RMMOD: could be added.
1029 * TAINT_UNSAFE_SMP, TAINT_MACHINE_CHECK, TAINT_BAD_PAGE don't
1030 * apply to modules.
1031 */
1032 return l;
1033}
1034
Kay Sievers1f717402006-11-24 12:15:25 +01001035static ssize_t show_initstate(struct module_attribute *mattr,
Kay Sievers4befb022011-07-24 22:06:04 +09301036 struct module_kobject *mk, char *buffer)
Kay Sievers1f717402006-11-24 12:15:25 +01001037{
1038 const char *state = "unknown";
1039
Kay Sievers4befb022011-07-24 22:06:04 +09301040 switch (mk->mod->state) {
Kay Sievers1f717402006-11-24 12:15:25 +01001041 case MODULE_STATE_LIVE:
1042 state = "live";
1043 break;
1044 case MODULE_STATE_COMING:
1045 state = "coming";
1046 break;
1047 case MODULE_STATE_GOING:
1048 state = "going";
1049 break;
1050 }
1051 return sprintf(buffer, "%s\n", state);
1052}
1053
Kay Sieverscca3e702012-01-13 09:32:15 +10301054static struct module_attribute modinfo_initstate =
1055 __ATTR(initstate, 0444, show_initstate, NULL);
Kay Sievers1f717402006-11-24 12:15:25 +01001056
Kay Sievers88bfa322011-07-24 22:06:04 +09301057static ssize_t store_uevent(struct module_attribute *mattr,
1058 struct module_kobject *mk,
1059 const char *buffer, size_t count)
1060{
1061 enum kobject_action action;
1062
1063 if (kobject_action_type(buffer, count, &action) == 0)
1064 kobject_uevent(&mk->kobj, action);
1065 return count;
1066}
1067
Kay Sieverscca3e702012-01-13 09:32:15 +10301068struct module_attribute module_uevent =
1069 __ATTR(uevent, 0200, NULL, store_uevent);
1070
1071static ssize_t show_coresize(struct module_attribute *mattr,
1072 struct module_kobject *mk, char *buffer)
1073{
1074 return sprintf(buffer, "%u\n", mk->mod->core_size);
1075}
1076
1077static struct module_attribute modinfo_coresize =
1078 __ATTR(coresize, 0444, show_coresize, NULL);
1079
1080static ssize_t show_initsize(struct module_attribute *mattr,
1081 struct module_kobject *mk, char *buffer)
1082{
1083 return sprintf(buffer, "%u\n", mk->mod->init_size);
1084}
1085
1086static struct module_attribute modinfo_initsize =
1087 __ATTR(initsize, 0444, show_initsize, NULL);
1088
1089static ssize_t show_taint(struct module_attribute *mattr,
1090 struct module_kobject *mk, char *buffer)
1091{
1092 size_t l;
1093
1094 l = module_flags_taint(mk->mod, buffer);
1095 buffer[l++] = '\n';
1096 return l;
1097}
1098
1099static struct module_attribute modinfo_taint =
1100 __ATTR(taint, 0444, show_taint, NULL);
Kay Sievers88bfa322011-07-24 22:06:04 +09301101
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001102static struct module_attribute *modinfo_attrs[] = {
Kay Sieverscca3e702012-01-13 09:32:15 +10301103 &module_uevent,
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001104 &modinfo_version,
1105 &modinfo_srcversion,
Kay Sieverscca3e702012-01-13 09:32:15 +10301106 &modinfo_initstate,
1107 &modinfo_coresize,
1108 &modinfo_initsize,
1109 &modinfo_taint,
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001110#ifdef CONFIG_MODULE_UNLOAD
Kay Sieverscca3e702012-01-13 09:32:15 +10301111 &modinfo_refcnt,
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001112#endif
1113 NULL,
1114};
1115
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116static const char vermagic[] = VERMAGIC_STRING;
1117
Rusty Russellc6e665c2009-03-31 13:05:33 -06001118static int try_to_force_load(struct module *mod, const char *reason)
Linus Torvalds826e4502008-05-04 17:04:16 -07001119{
1120#ifdef CONFIG_MODULE_FORCE_LOAD
Andi Kleen25ddbb12008-10-15 22:01:41 -07001121 if (!test_taint(TAINT_FORCED_MODULE))
Rusty Russellc6e665c2009-03-31 13:05:33 -06001122 printk(KERN_WARNING "%s: %s: kernel tainted.\n",
1123 mod->name, reason);
Linus Torvalds826e4502008-05-04 17:04:16 -07001124 add_taint_module(mod, TAINT_FORCED_MODULE);
1125 return 0;
1126#else
1127 return -ENOEXEC;
1128#endif
1129}
1130
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131#ifdef CONFIG_MODVERSIONS
Rusty Russelld4703ae2009-12-15 16:28:32 -06001132/* If the arch applies (non-zero) relocations to kernel kcrctab, unapply it. */
1133static unsigned long maybe_relocated(unsigned long crc,
1134 const struct module *crc_owner)
1135{
1136#ifdef ARCH_RELOCATES_KCRCTAB
1137 if (crc_owner == NULL)
1138 return crc - (unsigned long)reloc_start;
1139#endif
1140 return crc;
1141}
1142
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143static int check_version(Elf_Shdr *sechdrs,
1144 unsigned int versindex,
1145 const char *symname,
1146 struct module *mod,
Rusty Russelld4703ae2009-12-15 16:28:32 -06001147 const unsigned long *crc,
1148 const struct module *crc_owner)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149{
1150 unsigned int i, num_versions;
1151 struct modversion_info *versions;
1152
1153 /* Exporting module didn't supply crcs? OK, we're already tainted. */
1154 if (!crc)
1155 return 1;
1156
Rusty Russella5dd6972008-05-09 16:24:21 +10001157 /* No versions at all? modprobe --force does this. */
1158 if (versindex == 0)
1159 return try_to_force_load(mod, symname) == 0;
1160
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 versions = (void *) sechdrs[versindex].sh_addr;
1162 num_versions = sechdrs[versindex].sh_size
1163 / sizeof(struct modversion_info);
1164
1165 for (i = 0; i < num_versions; i++) {
1166 if (strcmp(versions[i].name, symname) != 0)
1167 continue;
1168
Rusty Russelld4703ae2009-12-15 16:28:32 -06001169 if (versions[i].crc == maybe_relocated(*crc, crc_owner))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170 return 1;
Jim Cromie5e124162011-12-06 12:11:31 -07001171 pr_debug("Found checksum %lX vs module %lX\n",
Rusty Russelld4703ae2009-12-15 16:28:32 -06001172 maybe_relocated(*crc, crc_owner), versions[i].crc);
Linus Torvalds826e4502008-05-04 17:04:16 -07001173 goto bad_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174 }
Linus Torvalds826e4502008-05-04 17:04:16 -07001175
Rusty Russella5dd6972008-05-09 16:24:21 +10001176 printk(KERN_WARNING "%s: no symbol version for %s\n",
1177 mod->name, symname);
1178 return 0;
Linus Torvalds826e4502008-05-04 17:04:16 -07001179
1180bad_version:
1181 printk("%s: disagrees about version of symbol %s\n",
1182 mod->name, symname);
1183 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184}
1185
1186static inline int check_modstruct_version(Elf_Shdr *sechdrs,
1187 unsigned int versindex,
1188 struct module *mod)
1189{
1190 const unsigned long *crc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191
Rusty Russell75676502010-06-05 11:17:36 -06001192 /* Since this should be found in kernel (which can't be removed),
1193 * no locking is necessary. */
Mike Frysinger6560dc12009-07-23 23:42:08 +09301194 if (!find_symbol(MODULE_SYMBOL_PREFIX "module_layout", NULL,
1195 &crc, true, false))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 BUG();
Rusty Russelld4703ae2009-12-15 16:28:32 -06001197 return check_version(sechdrs, versindex, "module_layout", mod, crc,
1198 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199}
1200
Rusty Russell91e37a72008-05-09 16:25:28 +10001201/* First part is kernel version, which we ignore if module has crcs. */
1202static inline int same_magic(const char *amagic, const char *bmagic,
1203 bool has_crcs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204{
Rusty Russell91e37a72008-05-09 16:25:28 +10001205 if (has_crcs) {
1206 amagic += strcspn(amagic, " ");
1207 bmagic += strcspn(bmagic, " ");
1208 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209 return strcmp(amagic, bmagic) == 0;
1210}
1211#else
1212static inline int check_version(Elf_Shdr *sechdrs,
1213 unsigned int versindex,
1214 const char *symname,
1215 struct module *mod,
Rusty Russelld4703ae2009-12-15 16:28:32 -06001216 const unsigned long *crc,
1217 const struct module *crc_owner)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218{
1219 return 1;
1220}
1221
1222static inline int check_modstruct_version(Elf_Shdr *sechdrs,
1223 unsigned int versindex,
1224 struct module *mod)
1225{
1226 return 1;
1227}
1228
Rusty Russell91e37a72008-05-09 16:25:28 +10001229static inline int same_magic(const char *amagic, const char *bmagic,
1230 bool has_crcs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231{
1232 return strcmp(amagic, bmagic) == 0;
1233}
1234#endif /* CONFIG_MODVERSIONS */
1235
Rusty Russell75676502010-06-05 11:17:36 -06001236/* Resolve a symbol for this module. I.e. if we find one, record usage. */
Rusty Russell49668682010-08-05 12:59:10 -06001237static const struct kernel_symbol *resolve_symbol(struct module *mod,
1238 const struct load_info *info,
Tim Abbott414fd312008-12-05 19:03:56 -05001239 const char *name,
Rusty Russell9bea7f22010-06-05 11:17:37 -06001240 char ownername[])
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241{
1242 struct module *owner;
Tim Abbott414fd312008-12-05 19:03:56 -05001243 const struct kernel_symbol *sym;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 const unsigned long *crc;
Rusty Russell9bea7f22010-06-05 11:17:37 -06001245 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246
Rusty Russell75676502010-06-05 11:17:36 -06001247 mutex_lock(&module_mutex);
Tim Abbott414fd312008-12-05 19:03:56 -05001248 sym = find_symbol(name, &owner, &crc,
Andi Kleen25ddbb12008-10-15 22:01:41 -07001249 !(mod->taints & (1 << TAINT_PROPRIETARY_MODULE)), true);
Rusty Russell9bea7f22010-06-05 11:17:37 -06001250 if (!sym)
1251 goto unlock;
1252
Rusty Russell49668682010-08-05 12:59:10 -06001253 if (!check_version(info->sechdrs, info->index.vers, name, mod, crc,
1254 owner)) {
Rusty Russell9bea7f22010-06-05 11:17:37 -06001255 sym = ERR_PTR(-EINVAL);
1256 goto getname;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257 }
Rusty Russell9bea7f22010-06-05 11:17:37 -06001258
1259 err = ref_module(mod, owner);
1260 if (err) {
1261 sym = ERR_PTR(err);
1262 goto getname;
1263 }
1264
1265getname:
1266 /* We must make copy under the lock if we failed to get ref. */
1267 strncpy(ownername, module_name(owner), MODULE_NAME_LEN);
1268unlock:
Rusty Russell75676502010-06-05 11:17:36 -06001269 mutex_unlock(&module_mutex);
Linus Torvalds218ce732010-05-25 16:48:30 -07001270 return sym;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271}
1272
Rusty Russell49668682010-08-05 12:59:10 -06001273static const struct kernel_symbol *
1274resolve_symbol_wait(struct module *mod,
1275 const struct load_info *info,
1276 const char *name)
Rusty Russell9bea7f22010-06-05 11:17:37 -06001277{
1278 const struct kernel_symbol *ksym;
Rusty Russell49668682010-08-05 12:59:10 -06001279 char owner[MODULE_NAME_LEN];
Rusty Russell9bea7f22010-06-05 11:17:37 -06001280
1281 if (wait_event_interruptible_timeout(module_wq,
Rusty Russell49668682010-08-05 12:59:10 -06001282 !IS_ERR(ksym = resolve_symbol(mod, info, name, owner))
1283 || PTR_ERR(ksym) != -EBUSY,
Rusty Russell9bea7f22010-06-05 11:17:37 -06001284 30 * HZ) <= 0) {
1285 printk(KERN_WARNING "%s: gave up waiting for init of module %s.\n",
Rusty Russell49668682010-08-05 12:59:10 -06001286 mod->name, owner);
Rusty Russell9bea7f22010-06-05 11:17:37 -06001287 }
1288 return ksym;
1289}
1290
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291/*
1292 * /sys/module/foo/sections stuff
1293 * J. Corbet <corbet@lwn.net>
1294 */
Rusty Russell8f6d0372010-08-05 12:59:09 -06001295#ifdef CONFIG_SYSFS
Ben Hutchings10b465a2009-12-19 14:43:01 +00001296
Rusty Russell8f6d0372010-08-05 12:59:09 -06001297#ifdef CONFIG_KALLSYMS
Ben Hutchings10b465a2009-12-19 14:43:01 +00001298static inline bool sect_empty(const Elf_Shdr *sect)
1299{
1300 return !(sect->sh_flags & SHF_ALLOC) || sect->sh_size == 0;
1301}
1302
Rusty Russella58730c2008-03-13 09:03:44 +00001303struct module_sect_attr
1304{
1305 struct module_attribute mattr;
1306 char *name;
1307 unsigned long address;
1308};
1309
1310struct module_sect_attrs
1311{
1312 struct attribute_group grp;
1313 unsigned int nsections;
1314 struct module_sect_attr attrs[0];
1315};
1316
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317static ssize_t module_sect_show(struct module_attribute *mattr,
Kay Sievers4befb022011-07-24 22:06:04 +09301318 struct module_kobject *mk, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319{
1320 struct module_sect_attr *sattr =
1321 container_of(mattr, struct module_sect_attr, mattr);
Kees Cook9f36e2c2011-03-22 16:34:22 -07001322 return sprintf(buf, "0x%pK\n", (void *)sattr->address);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323}
1324
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001325static void free_sect_attrs(struct module_sect_attrs *sect_attrs)
1326{
Rusty Russella58730c2008-03-13 09:03:44 +00001327 unsigned int section;
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001328
1329 for (section = 0; section < sect_attrs->nsections; section++)
1330 kfree(sect_attrs->attrs[section].name);
1331 kfree(sect_attrs);
1332}
1333
Rusty Russell8f6d0372010-08-05 12:59:09 -06001334static void add_sect_attrs(struct module *mod, const struct load_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335{
1336 unsigned int nloaded = 0, i, size[2];
1337 struct module_sect_attrs *sect_attrs;
1338 struct module_sect_attr *sattr;
1339 struct attribute **gattr;
Daniel Walker22a8bde2007-10-18 03:06:07 -07001340
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341 /* Count loaded sections and allocate structures */
Rusty Russell8f6d0372010-08-05 12:59:09 -06001342 for (i = 0; i < info->hdr->e_shnum; i++)
1343 if (!sect_empty(&info->sechdrs[i]))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344 nloaded++;
1345 size[0] = ALIGN(sizeof(*sect_attrs)
1346 + nloaded * sizeof(sect_attrs->attrs[0]),
1347 sizeof(sect_attrs->grp.attrs[0]));
1348 size[1] = (nloaded + 1) * sizeof(sect_attrs->grp.attrs[0]);
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001349 sect_attrs = kzalloc(size[0] + size[1], GFP_KERNEL);
1350 if (sect_attrs == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351 return;
1352
1353 /* Setup section attributes. */
1354 sect_attrs->grp.name = "sections";
1355 sect_attrs->grp.attrs = (void *)sect_attrs + size[0];
1356
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001357 sect_attrs->nsections = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358 sattr = &sect_attrs->attrs[0];
1359 gattr = &sect_attrs->grp.attrs[0];
Rusty Russell8f6d0372010-08-05 12:59:09 -06001360 for (i = 0; i < info->hdr->e_shnum; i++) {
1361 Elf_Shdr *sec = &info->sechdrs[i];
1362 if (sect_empty(sec))
Helge Deller35dead42009-12-03 00:29:15 +01001363 continue;
Rusty Russell8f6d0372010-08-05 12:59:09 -06001364 sattr->address = sec->sh_addr;
1365 sattr->name = kstrdup(info->secstrings + sec->sh_name,
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001366 GFP_KERNEL);
1367 if (sattr->name == NULL)
1368 goto out;
1369 sect_attrs->nsections++;
Eric W. Biederman361795b2010-02-12 13:41:56 -08001370 sysfs_attr_init(&sattr->mattr.attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371 sattr->mattr.show = module_sect_show;
1372 sattr->mattr.store = NULL;
1373 sattr->mattr.attr.name = sattr->name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374 sattr->mattr.attr.mode = S_IRUGO;
1375 *(gattr++) = &(sattr++)->mattr.attr;
1376 }
1377 *gattr = NULL;
1378
1379 if (sysfs_create_group(&mod->mkobj.kobj, &sect_attrs->grp))
1380 goto out;
1381
1382 mod->sect_attrs = sect_attrs;
1383 return;
1384 out:
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001385 free_sect_attrs(sect_attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386}
1387
1388static void remove_sect_attrs(struct module *mod)
1389{
1390 if (mod->sect_attrs) {
1391 sysfs_remove_group(&mod->mkobj.kobj,
1392 &mod->sect_attrs->grp);
1393 /* We are positive that no one is using any sect attrs
1394 * at this point. Deallocate immediately. */
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001395 free_sect_attrs(mod->sect_attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396 mod->sect_attrs = NULL;
1397 }
1398}
1399
Roland McGrath6d760132007-10-16 23:26:40 -07001400/*
1401 * /sys/module/foo/notes/.section.name gives contents of SHT_NOTE sections.
1402 */
1403
1404struct module_notes_attrs {
1405 struct kobject *dir;
1406 unsigned int notes;
1407 struct bin_attribute attrs[0];
1408};
1409
Chris Wright2c3c8be2010-05-12 18:28:57 -07001410static ssize_t module_notes_read(struct file *filp, struct kobject *kobj,
Roland McGrath6d760132007-10-16 23:26:40 -07001411 struct bin_attribute *bin_attr,
1412 char *buf, loff_t pos, size_t count)
1413{
1414 /*
1415 * The caller checked the pos and count against our size.
1416 */
1417 memcpy(buf, bin_attr->private + pos, count);
1418 return count;
1419}
1420
1421static void free_notes_attrs(struct module_notes_attrs *notes_attrs,
1422 unsigned int i)
1423{
1424 if (notes_attrs->dir) {
1425 while (i-- > 0)
1426 sysfs_remove_bin_file(notes_attrs->dir,
1427 &notes_attrs->attrs[i]);
Alexey Dobriyane9432092008-09-23 23:51:11 +04001428 kobject_put(notes_attrs->dir);
Roland McGrath6d760132007-10-16 23:26:40 -07001429 }
1430 kfree(notes_attrs);
1431}
1432
Rusty Russell8f6d0372010-08-05 12:59:09 -06001433static void add_notes_attrs(struct module *mod, const struct load_info *info)
Roland McGrath6d760132007-10-16 23:26:40 -07001434{
1435 unsigned int notes, loaded, i;
1436 struct module_notes_attrs *notes_attrs;
1437 struct bin_attribute *nattr;
1438
Ingo Molnarea6bff32009-08-28 10:44:56 +02001439 /* failed to create section attributes, so can't create notes */
1440 if (!mod->sect_attrs)
1441 return;
1442
Roland McGrath6d760132007-10-16 23:26:40 -07001443 /* Count notes sections and allocate structures. */
1444 notes = 0;
Rusty Russell8f6d0372010-08-05 12:59:09 -06001445 for (i = 0; i < info->hdr->e_shnum; i++)
1446 if (!sect_empty(&info->sechdrs[i]) &&
1447 (info->sechdrs[i].sh_type == SHT_NOTE))
Roland McGrath6d760132007-10-16 23:26:40 -07001448 ++notes;
1449
1450 if (notes == 0)
1451 return;
1452
1453 notes_attrs = kzalloc(sizeof(*notes_attrs)
1454 + notes * sizeof(notes_attrs->attrs[0]),
1455 GFP_KERNEL);
1456 if (notes_attrs == NULL)
1457 return;
1458
1459 notes_attrs->notes = notes;
1460 nattr = &notes_attrs->attrs[0];
Rusty Russell8f6d0372010-08-05 12:59:09 -06001461 for (loaded = i = 0; i < info->hdr->e_shnum; ++i) {
1462 if (sect_empty(&info->sechdrs[i]))
Roland McGrath6d760132007-10-16 23:26:40 -07001463 continue;
Rusty Russell8f6d0372010-08-05 12:59:09 -06001464 if (info->sechdrs[i].sh_type == SHT_NOTE) {
Eric W. Biederman361795b2010-02-12 13:41:56 -08001465 sysfs_bin_attr_init(nattr);
Roland McGrath6d760132007-10-16 23:26:40 -07001466 nattr->attr.name = mod->sect_attrs->attrs[loaded].name;
1467 nattr->attr.mode = S_IRUGO;
Rusty Russell8f6d0372010-08-05 12:59:09 -06001468 nattr->size = info->sechdrs[i].sh_size;
1469 nattr->private = (void *) info->sechdrs[i].sh_addr;
Roland McGrath6d760132007-10-16 23:26:40 -07001470 nattr->read = module_notes_read;
1471 ++nattr;
1472 }
1473 ++loaded;
1474 }
1475
Greg Kroah-Hartman4ff6abf2007-11-05 22:24:43 -08001476 notes_attrs->dir = kobject_create_and_add("notes", &mod->mkobj.kobj);
Roland McGrath6d760132007-10-16 23:26:40 -07001477 if (!notes_attrs->dir)
1478 goto out;
1479
1480 for (i = 0; i < notes; ++i)
1481 if (sysfs_create_bin_file(notes_attrs->dir,
1482 &notes_attrs->attrs[i]))
1483 goto out;
1484
1485 mod->notes_attrs = notes_attrs;
1486 return;
1487
1488 out:
1489 free_notes_attrs(notes_attrs, i);
1490}
1491
1492static void remove_notes_attrs(struct module *mod)
1493{
1494 if (mod->notes_attrs)
1495 free_notes_attrs(mod->notes_attrs, mod->notes_attrs->notes);
1496}
1497
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498#else
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001499
Rusty Russell8f6d0372010-08-05 12:59:09 -06001500static inline void add_sect_attrs(struct module *mod,
1501 const struct load_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502{
1503}
1504
1505static inline void remove_sect_attrs(struct module *mod)
1506{
1507}
Roland McGrath6d760132007-10-16 23:26:40 -07001508
Rusty Russell8f6d0372010-08-05 12:59:09 -06001509static inline void add_notes_attrs(struct module *mod,
1510 const struct load_info *info)
Roland McGrath6d760132007-10-16 23:26:40 -07001511{
1512}
1513
1514static inline void remove_notes_attrs(struct module *mod)
1515{
1516}
Rusty Russell8f6d0372010-08-05 12:59:09 -06001517#endif /* CONFIG_KALLSYMS */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518
Rusty Russell80a3d1b2010-06-05 11:17:36 -06001519static void add_usage_links(struct module *mod)
1520{
1521#ifdef CONFIG_MODULE_UNLOAD
1522 struct module_use *use;
1523 int nowarn;
1524
Rusty Russell75676502010-06-05 11:17:36 -06001525 mutex_lock(&module_mutex);
Rusty Russell80a3d1b2010-06-05 11:17:36 -06001526 list_for_each_entry(use, &mod->target_list, target_list) {
1527 nowarn = sysfs_create_link(use->target->holders_dir,
1528 &mod->mkobj.kobj, mod->name);
1529 }
Rusty Russell75676502010-06-05 11:17:36 -06001530 mutex_unlock(&module_mutex);
Rusty Russell80a3d1b2010-06-05 11:17:36 -06001531#endif
1532}
1533
1534static void del_usage_links(struct module *mod)
1535{
1536#ifdef CONFIG_MODULE_UNLOAD
1537 struct module_use *use;
1538
Rusty Russell75676502010-06-05 11:17:36 -06001539 mutex_lock(&module_mutex);
Rusty Russell80a3d1b2010-06-05 11:17:36 -06001540 list_for_each_entry(use, &mod->target_list, target_list)
1541 sysfs_remove_link(use->target->holders_dir, mod->name);
Rusty Russell75676502010-06-05 11:17:36 -06001542 mutex_unlock(&module_mutex);
Rusty Russell80a3d1b2010-06-05 11:17:36 -06001543#endif
1544}
1545
Rusty Russell6407ebb22010-06-05 11:17:36 -06001546static int module_add_modinfo_attrs(struct module *mod)
Matt Domschc988d2b2005-06-23 22:05:15 -07001547{
1548 struct module_attribute *attr;
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001549 struct module_attribute *temp_attr;
Matt Domschc988d2b2005-06-23 22:05:15 -07001550 int error = 0;
1551 int i;
1552
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001553 mod->modinfo_attrs = kzalloc((sizeof(struct module_attribute) *
1554 (ARRAY_SIZE(modinfo_attrs) + 1)),
1555 GFP_KERNEL);
1556 if (!mod->modinfo_attrs)
1557 return -ENOMEM;
1558
1559 temp_attr = mod->modinfo_attrs;
Matt Domschc988d2b2005-06-23 22:05:15 -07001560 for (i = 0; (attr = modinfo_attrs[i]) && !error; i++) {
1561 if (!attr->test ||
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001562 (attr->test && attr->test(mod))) {
1563 memcpy(temp_attr, attr, sizeof(*temp_attr));
Eric W. Biederman361795b2010-02-12 13:41:56 -08001564 sysfs_attr_init(&temp_attr->attr);
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001565 error = sysfs_create_file(&mod->mkobj.kobj,&temp_attr->attr);
1566 ++temp_attr;
1567 }
Matt Domschc988d2b2005-06-23 22:05:15 -07001568 }
1569 return error;
1570}
1571
Rusty Russell6407ebb22010-06-05 11:17:36 -06001572static void module_remove_modinfo_attrs(struct module *mod)
Matt Domschc988d2b2005-06-23 22:05:15 -07001573{
1574 struct module_attribute *attr;
1575 int i;
1576
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001577 for (i = 0; (attr = &mod->modinfo_attrs[i]); i++) {
1578 /* pick a field to test for end of list */
1579 if (!attr->attr.name)
1580 break;
Matt Domschc988d2b2005-06-23 22:05:15 -07001581 sysfs_remove_file(&mod->mkobj.kobj,&attr->attr);
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001582 if (attr->free)
1583 attr->free(mod);
Matt Domschc988d2b2005-06-23 22:05:15 -07001584 }
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001585 kfree(mod->modinfo_attrs);
Matt Domschc988d2b2005-06-23 22:05:15 -07001586}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587
Rusty Russell6407ebb22010-06-05 11:17:36 -06001588static int mod_sysfs_init(struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589{
1590 int err;
Greg Kroah-Hartman6494a932008-01-27 15:38:40 -08001591 struct kobject *kobj;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592
Greg Kroah-Hartman823bccf2007-04-13 13:15:19 -07001593 if (!module_sysfs_initialized) {
1594 printk(KERN_ERR "%s: module sysfs not initialized\n",
Ed Swierk1cc5f712006-09-25 16:25:36 -07001595 mod->name);
1596 err = -EINVAL;
1597 goto out;
1598 }
Greg Kroah-Hartman6494a932008-01-27 15:38:40 -08001599
1600 kobj = kset_find_obj(module_kset, mod->name);
1601 if (kobj) {
1602 printk(KERN_ERR "%s: module is already loaded\n", mod->name);
1603 kobject_put(kobj);
1604 err = -EINVAL;
1605 goto out;
1606 }
1607
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608 mod->mkobj.mod = mod;
Kay Sieverse17e0f52006-11-24 12:15:25 +01001609
Greg Kroah-Hartmanac3c8142007-12-17 23:05:35 -07001610 memset(&mod->mkobj.kobj, 0, sizeof(mod->mkobj.kobj));
1611 mod->mkobj.kobj.kset = module_kset;
1612 err = kobject_init_and_add(&mod->mkobj.kobj, &module_ktype, NULL,
1613 "%s", mod->name);
1614 if (err)
1615 kobject_put(&mod->mkobj.kobj);
Kay Sievers270a6c42007-01-18 13:26:15 +01001616
Kay Sievers97c146e2007-11-29 23:46:11 +01001617 /* delay uevent until full sysfs population */
Kay Sievers270a6c42007-01-18 13:26:15 +01001618out:
1619 return err;
1620}
1621
Rusty Russell6407ebb22010-06-05 11:17:36 -06001622static int mod_sysfs_setup(struct module *mod,
Rusty Russell8f6d0372010-08-05 12:59:09 -06001623 const struct load_info *info,
Kay Sievers270a6c42007-01-18 13:26:15 +01001624 struct kernel_param *kparam,
1625 unsigned int num_params)
1626{
1627 int err;
1628
Rusty Russell80a3d1b2010-06-05 11:17:36 -06001629 err = mod_sysfs_init(mod);
1630 if (err)
1631 goto out;
1632
Greg Kroah-Hartman4ff6abf2007-11-05 22:24:43 -08001633 mod->holders_dir = kobject_create_and_add("holders", &mod->mkobj.kobj);
Akinobu Mita240936e2007-04-26 00:12:09 -07001634 if (!mod->holders_dir) {
1635 err = -ENOMEM;
Kay Sievers270a6c42007-01-18 13:26:15 +01001636 goto out_unreg;
Akinobu Mita240936e2007-04-26 00:12:09 -07001637 }
Kay Sievers270a6c42007-01-18 13:26:15 +01001638
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639 err = module_param_sysfs_setup(mod, kparam, num_params);
1640 if (err)
Kay Sievers270a6c42007-01-18 13:26:15 +01001641 goto out_unreg_holders;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642
Matt Domschc988d2b2005-06-23 22:05:15 -07001643 err = module_add_modinfo_attrs(mod);
1644 if (err)
Kay Sieverse17e0f52006-11-24 12:15:25 +01001645 goto out_unreg_param;
Matt Domschc988d2b2005-06-23 22:05:15 -07001646
Rusty Russell80a3d1b2010-06-05 11:17:36 -06001647 add_usage_links(mod);
Rusty Russell8f6d0372010-08-05 12:59:09 -06001648 add_sect_attrs(mod, info);
1649 add_notes_attrs(mod, info);
Rusty Russell80a3d1b2010-06-05 11:17:36 -06001650
Kay Sieverse17e0f52006-11-24 12:15:25 +01001651 kobject_uevent(&mod->mkobj.kobj, KOBJ_ADD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652 return 0;
1653
Kay Sieverse17e0f52006-11-24 12:15:25 +01001654out_unreg_param:
1655 module_param_sysfs_remove(mod);
Kay Sievers270a6c42007-01-18 13:26:15 +01001656out_unreg_holders:
Greg Kroah-Hartman78a2d902007-12-20 08:13:05 -08001657 kobject_put(mod->holders_dir);
Kay Sievers270a6c42007-01-18 13:26:15 +01001658out_unreg:
Kay Sieverse17e0f52006-11-24 12:15:25 +01001659 kobject_put(&mod->mkobj.kobj);
Rusty Russell80a3d1b2010-06-05 11:17:36 -06001660out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661 return err;
1662}
Denis V. Lunev34e4e2f2008-05-20 13:59:48 +04001663
1664static void mod_sysfs_fini(struct module *mod)
1665{
Rusty Russell8f6d0372010-08-05 12:59:09 -06001666 remove_notes_attrs(mod);
1667 remove_sect_attrs(mod);
Denis V. Lunev34e4e2f2008-05-20 13:59:48 +04001668 kobject_put(&mod->mkobj.kobj);
1669}
1670
Rusty Russell8f6d0372010-08-05 12:59:09 -06001671#else /* !CONFIG_SYSFS */
Denis V. Lunev34e4e2f2008-05-20 13:59:48 +04001672
Rusty Russell8f6d0372010-08-05 12:59:09 -06001673static int mod_sysfs_setup(struct module *mod,
1674 const struct load_info *info,
Rusty Russell6407ebb22010-06-05 11:17:36 -06001675 struct kernel_param *kparam,
1676 unsigned int num_params)
1677{
1678 return 0;
1679}
1680
Denis V. Lunev34e4e2f2008-05-20 13:59:48 +04001681static void mod_sysfs_fini(struct module *mod)
1682{
1683}
1684
Rusty Russell36b03602010-08-05 12:59:09 -06001685static void module_remove_modinfo_attrs(struct module *mod)
1686{
1687}
1688
Rusty Russell80a3d1b2010-06-05 11:17:36 -06001689static void del_usage_links(struct module *mod)
1690{
1691}
1692
Denis V. Lunev34e4e2f2008-05-20 13:59:48 +04001693#endif /* CONFIG_SYSFS */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694
Rusty Russell36b03602010-08-05 12:59:09 -06001695static void mod_sysfs_teardown(struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696{
Rusty Russell80a3d1b2010-06-05 11:17:36 -06001697 del_usage_links(mod);
Matt Domschc988d2b2005-06-23 22:05:15 -07001698 module_remove_modinfo_attrs(mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699 module_param_sysfs_remove(mod);
Greg Kroah-Hartman78a2d902007-12-20 08:13:05 -08001700 kobject_put(mod->mkobj.drivers_dir);
1701 kobject_put(mod->holders_dir);
Denis V. Lunev34e4e2f2008-05-20 13:59:48 +04001702 mod_sysfs_fini(mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703}
1704
1705/*
1706 * unlink the module with the whole machine is stopped with interrupts off
1707 * - this defends against kallsyms not taking locks
1708 */
1709static int __unlink_module(void *_mod)
1710{
1711 struct module *mod = _mod;
1712 list_del(&mod->list);
Linus Torvalds53363772010-10-05 11:29:27 -07001713 module_bug_cleanup(mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714 return 0;
1715}
1716
matthieu castet84e1c6b2010-11-16 22:35:16 +01001717#ifdef CONFIG_DEBUG_SET_MODULE_RONX
1718/*
1719 * LKM RO/NX protection: protect module's text/ro-data
1720 * from modification and any data from execution.
1721 */
1722void set_page_attributes(void *start, void *end, int (*set)(unsigned long start, int num_pages))
1723{
1724 unsigned long begin_pfn = PFN_DOWN((unsigned long)start);
1725 unsigned long end_pfn = PFN_DOWN((unsigned long)end);
1726
1727 if (end_pfn > begin_pfn)
1728 set(begin_pfn << PAGE_SHIFT, end_pfn - begin_pfn);
1729}
1730
1731static void set_section_ro_nx(void *base,
1732 unsigned long text_size,
1733 unsigned long ro_size,
1734 unsigned long total_size)
1735{
1736 /* begin and end PFNs of the current subsection */
1737 unsigned long begin_pfn;
1738 unsigned long end_pfn;
1739
1740 /*
1741 * Set RO for module text and RO-data:
1742 * - Always protect first page.
1743 * - Do not protect last partial page.
1744 */
1745 if (ro_size > 0)
1746 set_page_attributes(base, base + ro_size, set_memory_ro);
1747
1748 /*
1749 * Set NX permissions for module data:
1750 * - Do not protect first partial page.
1751 * - Always protect last page.
1752 */
1753 if (total_size > text_size) {
1754 begin_pfn = PFN_UP((unsigned long)base + text_size);
1755 end_pfn = PFN_UP((unsigned long)base + total_size);
1756 if (end_pfn > begin_pfn)
1757 set_memory_nx(begin_pfn << PAGE_SHIFT, end_pfn - begin_pfn);
1758 }
1759}
1760
Jan Glauber01526ed2011-05-19 16:55:26 -06001761static void unset_module_core_ro_nx(struct module *mod)
matthieu castet84e1c6b2010-11-16 22:35:16 +01001762{
Jan Glauber01526ed2011-05-19 16:55:26 -06001763 set_page_attributes(mod->module_core + mod->core_text_size,
1764 mod->module_core + mod->core_size,
1765 set_memory_x);
1766 set_page_attributes(mod->module_core,
1767 mod->module_core + mod->core_ro_size,
1768 set_memory_rw);
1769}
1770
1771static void unset_module_init_ro_nx(struct module *mod)
1772{
1773 set_page_attributes(mod->module_init + mod->init_text_size,
1774 mod->module_init + mod->init_size,
1775 set_memory_x);
1776 set_page_attributes(mod->module_init,
1777 mod->module_init + mod->init_ro_size,
1778 set_memory_rw);
matthieu castet84e1c6b2010-11-16 22:35:16 +01001779}
1780
1781/* Iterate through all modules and set each module's text as RW */
Daniel J Blueman5d05c702011-03-08 22:01:47 +08001782void set_all_modules_text_rw(void)
matthieu castet84e1c6b2010-11-16 22:35:16 +01001783{
1784 struct module *mod;
1785
1786 mutex_lock(&module_mutex);
1787 list_for_each_entry_rcu(mod, &modules, list) {
1788 if ((mod->module_core) && (mod->core_text_size)) {
1789 set_page_attributes(mod->module_core,
1790 mod->module_core + mod->core_text_size,
1791 set_memory_rw);
1792 }
1793 if ((mod->module_init) && (mod->init_text_size)) {
1794 set_page_attributes(mod->module_init,
1795 mod->module_init + mod->init_text_size,
1796 set_memory_rw);
1797 }
1798 }
1799 mutex_unlock(&module_mutex);
1800}
1801
1802/* Iterate through all modules and set each module's text as RO */
Daniel J Blueman5d05c702011-03-08 22:01:47 +08001803void set_all_modules_text_ro(void)
matthieu castet84e1c6b2010-11-16 22:35:16 +01001804{
1805 struct module *mod;
1806
1807 mutex_lock(&module_mutex);
1808 list_for_each_entry_rcu(mod, &modules, list) {
1809 if ((mod->module_core) && (mod->core_text_size)) {
1810 set_page_attributes(mod->module_core,
1811 mod->module_core + mod->core_text_size,
1812 set_memory_ro);
1813 }
1814 if ((mod->module_init) && (mod->init_text_size)) {
1815 set_page_attributes(mod->module_init,
1816 mod->module_init + mod->init_text_size,
1817 set_memory_ro);
1818 }
1819 }
1820 mutex_unlock(&module_mutex);
1821}
1822#else
1823static 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 -06001824static void unset_module_core_ro_nx(struct module *mod) { }
1825static void unset_module_init_ro_nx(struct module *mod) { }
matthieu castet84e1c6b2010-11-16 22:35:16 +01001826#endif
1827
Jonas Bonn74e08fc2011-06-30 21:22:11 +02001828void __weak module_free(struct module *mod, void *module_region)
1829{
1830 vfree(module_region);
1831}
1832
1833void __weak module_arch_cleanup(struct module *mod)
1834{
1835}
1836
Rusty Russell75676502010-06-05 11:17:36 -06001837/* Free a module, remove from lists, etc. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838static void free_module(struct module *mod)
1839{
Li Zefan7ead8b82009-08-17 16:56:28 +08001840 trace_module_free(mod);
1841
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842 /* Delete from various lists */
Rusty Russell75676502010-06-05 11:17:36 -06001843 mutex_lock(&module_mutex);
Rusty Russell9b1a4d32008-07-28 12:16:30 -05001844 stop_machine(__unlink_module, mod, NULL);
Rusty Russell75676502010-06-05 11:17:36 -06001845 mutex_unlock(&module_mutex);
Rusty Russell36b03602010-08-05 12:59:09 -06001846 mod_sysfs_teardown(mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847
Jason Baronb82bab4b2010-07-27 13:18:01 -07001848 /* Remove dynamic debug info */
1849 ddebug_remove_module(mod->name);
1850
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851 /* Arch-specific cleanup. */
1852 module_arch_cleanup(mod);
1853
1854 /* Module unload stuff */
1855 module_unload_free(mod);
1856
Rusty Russelle180a6b2009-03-31 13:05:29 -06001857 /* Free any allocated parameters. */
1858 destroy_params(mod->kp, mod->num_kp);
1859
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860 /* This may be NULL, but that's OK */
Jan Glauber01526ed2011-05-19 16:55:26 -06001861 unset_module_init_ro_nx(mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862 module_free(mod, mod->module_init);
1863 kfree(mod->args);
Tejun Heo259354d2010-03-10 18:56:10 +09001864 percpu_modfree(mod);
Rusty Russell9f85a4b2010-08-05 12:59:04 -06001865
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001866 /* Free lock-classes: */
1867 lockdep_free_key_range(mod->module_core, mod->core_size);
1868
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869 /* Finally, free the core (containing the module structure) */
Jan Glauber01526ed2011-05-19 16:55:26 -06001870 unset_module_core_ro_nx(mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871 module_free(mod, mod->module_core);
Bernd Schmidteb8cdec2009-09-21 17:03:57 -07001872
1873#ifdef CONFIG_MPU
1874 update_protections(current->mm);
1875#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876}
1877
1878void *__symbol_get(const char *symbol)
1879{
1880 struct module *owner;
Tim Abbott414fd312008-12-05 19:03:56 -05001881 const struct kernel_symbol *sym;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882
Rusty Russell24da1cb2007-07-15 23:41:46 -07001883 preempt_disable();
Tim Abbott414fd312008-12-05 19:03:56 -05001884 sym = find_symbol(symbol, &owner, NULL, true, true);
1885 if (sym && strong_try_module_get(owner))
1886 sym = NULL;
Rusty Russell24da1cb2007-07-15 23:41:46 -07001887 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888
Tim Abbott414fd312008-12-05 19:03:56 -05001889 return sym ? (void *)sym->value : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890}
1891EXPORT_SYMBOL_GPL(__symbol_get);
1892
Ashutosh Naikeea8b542006-01-08 01:04:25 -08001893/*
1894 * Ensure that an exported symbol [global namespace] does not already exist
Robert P. J. Day02a3e592007-05-09 07:26:28 +02001895 * in the kernel or in some other module's exported symbol table.
Rusty Russellbe593f42010-06-05 11:17:37 -06001896 *
1897 * You must hold the module_mutex.
Ashutosh Naikeea8b542006-01-08 01:04:25 -08001898 */
1899static int verify_export_symbols(struct module *mod)
1900{
Rusty Russellb2111042008-05-01 21:15:00 -05001901 unsigned int i;
Ashutosh Naikeea8b542006-01-08 01:04:25 -08001902 struct module *owner;
Rusty Russellb2111042008-05-01 21:15:00 -05001903 const struct kernel_symbol *s;
1904 struct {
1905 const struct kernel_symbol *sym;
1906 unsigned int num;
1907 } arr[] = {
1908 { mod->syms, mod->num_syms },
1909 { mod->gpl_syms, mod->num_gpl_syms },
1910 { mod->gpl_future_syms, mod->num_gpl_future_syms },
Denys Vlasenkof7f5b672008-07-22 19:24:26 -05001911#ifdef CONFIG_UNUSED_SYMBOLS
Rusty Russellb2111042008-05-01 21:15:00 -05001912 { mod->unused_syms, mod->num_unused_syms },
1913 { mod->unused_gpl_syms, mod->num_unused_gpl_syms },
Denys Vlasenkof7f5b672008-07-22 19:24:26 -05001914#endif
Rusty Russellb2111042008-05-01 21:15:00 -05001915 };
Ashutosh Naikeea8b542006-01-08 01:04:25 -08001916
Rusty Russellb2111042008-05-01 21:15:00 -05001917 for (i = 0; i < ARRAY_SIZE(arr); i++) {
1918 for (s = arr[i].sym; s < arr[i].sym + arr[i].num; s++) {
Rusty Russellbe593f42010-06-05 11:17:37 -06001919 if (find_symbol(s->name, &owner, NULL, true, false)) {
Rusty Russellb2111042008-05-01 21:15:00 -05001920 printk(KERN_ERR
1921 "%s: exports duplicate symbol %s"
1922 " (owned by %s)\n",
1923 mod->name, s->name, module_name(owner));
1924 return -ENOEXEC;
1925 }
Ashutosh Naikeea8b542006-01-08 01:04:25 -08001926 }
Rusty Russellb2111042008-05-01 21:15:00 -05001927 }
1928 return 0;
Ashutosh Naikeea8b542006-01-08 01:04:25 -08001929}
1930
Matti Linnanvuori9a4b9702007-11-08 08:37:38 -08001931/* Change all symbols so that st_value encodes the pointer directly. */
Rusty Russell49668682010-08-05 12:59:10 -06001932static int simplify_symbols(struct module *mod, const struct load_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933{
Rusty Russell49668682010-08-05 12:59:10 -06001934 Elf_Shdr *symsec = &info->sechdrs[info->index.sym];
1935 Elf_Sym *sym = (void *)symsec->sh_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936 unsigned long secbase;
Rusty Russell49668682010-08-05 12:59:10 -06001937 unsigned int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938 int ret = 0;
Tim Abbott414fd312008-12-05 19:03:56 -05001939 const struct kernel_symbol *ksym;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940
Rusty Russell49668682010-08-05 12:59:10 -06001941 for (i = 1; i < symsec->sh_size / sizeof(Elf_Sym); i++) {
1942 const char *name = info->strtab + sym[i].st_name;
1943
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944 switch (sym[i].st_shndx) {
1945 case SHN_COMMON:
1946 /* We compiled with -fno-common. These are not
1947 supposed to happen. */
Jim Cromie5e124162011-12-06 12:11:31 -07001948 pr_debug("Common symbol: %s\n", name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949 printk("%s: please compile with -fno-common\n",
1950 mod->name);
1951 ret = -ENOEXEC;
1952 break;
1953
1954 case SHN_ABS:
1955 /* Don't need to do anything */
Jim Cromie5e124162011-12-06 12:11:31 -07001956 pr_debug("Absolute symbol: 0x%08lx\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957 (long)sym[i].st_value);
1958 break;
1959
1960 case SHN_UNDEF:
Rusty Russell49668682010-08-05 12:59:10 -06001961 ksym = resolve_symbol_wait(mod, info, name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962 /* Ok if resolved. */
Rusty Russell9bea7f22010-06-05 11:17:37 -06001963 if (ksym && !IS_ERR(ksym)) {
Tim Abbott414fd312008-12-05 19:03:56 -05001964 sym[i].st_value = ksym->value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965 break;
Tim Abbott414fd312008-12-05 19:03:56 -05001966 }
1967
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968 /* Ok if weak. */
Rusty Russell9bea7f22010-06-05 11:17:37 -06001969 if (!ksym && ELF_ST_BIND(sym[i].st_info) == STB_WEAK)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970 break;
1971
Rusty Russell9bea7f22010-06-05 11:17:37 -06001972 printk(KERN_WARNING "%s: Unknown symbol %s (err %li)\n",
Rusty Russell49668682010-08-05 12:59:10 -06001973 mod->name, name, PTR_ERR(ksym));
Rusty Russell9bea7f22010-06-05 11:17:37 -06001974 ret = PTR_ERR(ksym) ?: -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975 break;
1976
1977 default:
1978 /* Divert to percpu allocation if a percpu var. */
Rusty Russell49668682010-08-05 12:59:10 -06001979 if (sym[i].st_shndx == info->index.pcpu)
Tejun Heo259354d2010-03-10 18:56:10 +09001980 secbase = (unsigned long)mod_percpu(mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981 else
Rusty Russell49668682010-08-05 12:59:10 -06001982 secbase = info->sechdrs[sym[i].st_shndx].sh_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983 sym[i].st_value += secbase;
1984 break;
1985 }
1986 }
1987
1988 return ret;
1989}
1990
Rusty Russell49668682010-08-05 12:59:10 -06001991static int apply_relocations(struct module *mod, const struct load_info *info)
Rusty Russell22e268e2010-08-05 12:59:05 -06001992{
1993 unsigned int i;
1994 int err = 0;
1995
1996 /* Now do relocations. */
Rusty Russell49668682010-08-05 12:59:10 -06001997 for (i = 1; i < info->hdr->e_shnum; i++) {
1998 unsigned int infosec = info->sechdrs[i].sh_info;
Rusty Russell22e268e2010-08-05 12:59:05 -06001999
2000 /* Not a valid relocation section? */
Rusty Russell49668682010-08-05 12:59:10 -06002001 if (infosec >= info->hdr->e_shnum)
Rusty Russell22e268e2010-08-05 12:59:05 -06002002 continue;
2003
2004 /* Don't bother with non-allocated sections */
Rusty Russell49668682010-08-05 12:59:10 -06002005 if (!(info->sechdrs[infosec].sh_flags & SHF_ALLOC))
Rusty Russell22e268e2010-08-05 12:59:05 -06002006 continue;
2007
Rusty Russell49668682010-08-05 12:59:10 -06002008 if (info->sechdrs[i].sh_type == SHT_REL)
2009 err = apply_relocate(info->sechdrs, info->strtab,
2010 info->index.sym, i, mod);
2011 else if (info->sechdrs[i].sh_type == SHT_RELA)
2012 err = apply_relocate_add(info->sechdrs, info->strtab,
2013 info->index.sym, i, mod);
Rusty Russell22e268e2010-08-05 12:59:05 -06002014 if (err < 0)
2015 break;
2016 }
2017 return err;
2018}
2019
Helge Deller088af9a2008-12-31 12:31:18 +01002020/* Additional bytes needed by arch in front of individual sections */
2021unsigned int __weak arch_mod_section_prepend(struct module *mod,
2022 unsigned int section)
2023{
2024 /* default implementation just returns zero */
2025 return 0;
2026}
2027
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028/* Update size with this section: return offset. */
Helge Deller088af9a2008-12-31 12:31:18 +01002029static long get_offset(struct module *mod, unsigned int *size,
2030 Elf_Shdr *sechdr, unsigned int section)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031{
2032 long ret;
2033
Helge Deller088af9a2008-12-31 12:31:18 +01002034 *size += arch_mod_section_prepend(mod, section);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035 ret = ALIGN(*size, sechdr->sh_addralign ?: 1);
2036 *size = ret + sechdr->sh_size;
2037 return ret;
2038}
2039
2040/* Lay out the SHF_ALLOC sections in a way not dissimilar to how ld
2041 might -- code, read-only data, read-write data, small data. Tally
2042 sizes, and place the offsets into sh_entsize fields: high bit means it
2043 belongs in init. */
Rusty Russell49668682010-08-05 12:59:10 -06002044static void layout_sections(struct module *mod, struct load_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045{
2046 static unsigned long const masks[][2] = {
2047 /* NOTE: all executable code must be the first section
2048 * in this array; otherwise modify the text_size
2049 * finder in the two loops below */
2050 { SHF_EXECINSTR | SHF_ALLOC, ARCH_SHF_SMALL },
2051 { SHF_ALLOC, SHF_WRITE | ARCH_SHF_SMALL },
2052 { SHF_WRITE | SHF_ALLOC, ARCH_SHF_SMALL },
2053 { ARCH_SHF_SMALL | SHF_ALLOC, 0 }
2054 };
2055 unsigned int m, i;
2056
Rusty Russell49668682010-08-05 12:59:10 -06002057 for (i = 0; i < info->hdr->e_shnum; i++)
2058 info->sechdrs[i].sh_entsize = ~0UL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059
Jim Cromie5e124162011-12-06 12:11:31 -07002060 pr_debug("Core section allocation order:\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061 for (m = 0; m < ARRAY_SIZE(masks); ++m) {
Rusty Russell49668682010-08-05 12:59:10 -06002062 for (i = 0; i < info->hdr->e_shnum; ++i) {
2063 Elf_Shdr *s = &info->sechdrs[i];
2064 const char *sname = info->secstrings + s->sh_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065
2066 if ((s->sh_flags & masks[m][0]) != masks[m][0]
2067 || (s->sh_flags & masks[m][1])
2068 || s->sh_entsize != ~0UL
Rusty Russell49668682010-08-05 12:59:10 -06002069 || strstarts(sname, ".init"))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070 continue;
Helge Deller088af9a2008-12-31 12:31:18 +01002071 s->sh_entsize = get_offset(mod, &mod->core_size, s, i);
Jim Cromie5e124162011-12-06 12:11:31 -07002072 pr_debug("\t%s\n", sname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073 }
matthieu castet84e1c6b2010-11-16 22:35:16 +01002074 switch (m) {
2075 case 0: /* executable */
2076 mod->core_size = debug_align(mod->core_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077 mod->core_text_size = mod->core_size;
matthieu castet84e1c6b2010-11-16 22:35:16 +01002078 break;
2079 case 1: /* RO: text and ro-data */
2080 mod->core_size = debug_align(mod->core_size);
2081 mod->core_ro_size = mod->core_size;
2082 break;
2083 case 3: /* whole core */
2084 mod->core_size = debug_align(mod->core_size);
2085 break;
2086 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087 }
2088
Jim Cromie5e124162011-12-06 12:11:31 -07002089 pr_debug("Init section allocation order:\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090 for (m = 0; m < ARRAY_SIZE(masks); ++m) {
Rusty Russell49668682010-08-05 12:59:10 -06002091 for (i = 0; i < info->hdr->e_shnum; ++i) {
2092 Elf_Shdr *s = &info->sechdrs[i];
2093 const char *sname = info->secstrings + s->sh_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094
2095 if ((s->sh_flags & masks[m][0]) != masks[m][0]
2096 || (s->sh_flags & masks[m][1])
2097 || s->sh_entsize != ~0UL
Rusty Russell49668682010-08-05 12:59:10 -06002098 || !strstarts(sname, ".init"))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099 continue;
Helge Deller088af9a2008-12-31 12:31:18 +01002100 s->sh_entsize = (get_offset(mod, &mod->init_size, s, i)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101 | INIT_OFFSET_MASK);
Jim Cromie5e124162011-12-06 12:11:31 -07002102 pr_debug("\t%s\n", sname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103 }
matthieu castet84e1c6b2010-11-16 22:35:16 +01002104 switch (m) {
2105 case 0: /* executable */
2106 mod->init_size = debug_align(mod->init_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107 mod->init_text_size = mod->init_size;
matthieu castet84e1c6b2010-11-16 22:35:16 +01002108 break;
2109 case 1: /* RO: text and ro-data */
2110 mod->init_size = debug_align(mod->init_size);
2111 mod->init_ro_size = mod->init_size;
2112 break;
2113 case 3: /* whole init */
2114 mod->init_size = debug_align(mod->init_size);
2115 break;
2116 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117 }
2118}
2119
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120static void set_license(struct module *mod, const char *license)
2121{
2122 if (!license)
2123 license = "unspecified";
2124
Florin Malitafa3ba2e82006-10-11 01:21:48 -07002125 if (!license_is_gpl_compatible(license)) {
Andi Kleen25ddbb12008-10-15 22:01:41 -07002126 if (!test_taint(TAINT_PROPRIETARY_MODULE))
Jan Dittmer1d4d2622006-10-28 10:38:38 -07002127 printk(KERN_WARNING "%s: module license '%s' taints "
Florin Malitafa3ba2e82006-10-11 01:21:48 -07002128 "kernel.\n", mod->name, license);
2129 add_taint_module(mod, TAINT_PROPRIETARY_MODULE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130 }
2131}
2132
2133/* Parse tag=value strings from .modinfo section */
2134static char *next_string(char *string, unsigned long *secsize)
2135{
2136 /* Skip non-zero chars */
2137 while (string[0]) {
2138 string++;
2139 if ((*secsize)-- <= 1)
2140 return NULL;
2141 }
2142
2143 /* Skip any zero padding. */
2144 while (!string[0]) {
2145 string++;
2146 if ((*secsize)-- <= 1)
2147 return NULL;
2148 }
2149 return string;
2150}
2151
Rusty Russell49668682010-08-05 12:59:10 -06002152static char *get_modinfo(struct load_info *info, const char *tag)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153{
2154 char *p;
2155 unsigned int taglen = strlen(tag);
Rusty Russell49668682010-08-05 12:59:10 -06002156 Elf_Shdr *infosec = &info->sechdrs[info->index.info];
2157 unsigned long size = infosec->sh_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002158
Rusty Russell49668682010-08-05 12:59:10 -06002159 for (p = (char *)infosec->sh_addr; p; p = next_string(p, &size)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160 if (strncmp(p, tag, taglen) == 0 && p[taglen] == '=')
2161 return p + taglen + 1;
2162 }
2163 return NULL;
2164}
2165
Rusty Russell49668682010-08-05 12:59:10 -06002166static void setup_modinfo(struct module *mod, struct load_info *info)
Matt Domschc988d2b2005-06-23 22:05:15 -07002167{
2168 struct module_attribute *attr;
2169 int i;
2170
2171 for (i = 0; (attr = modinfo_attrs[i]); i++) {
2172 if (attr->setup)
Rusty Russell49668682010-08-05 12:59:10 -06002173 attr->setup(mod, get_modinfo(info, attr->attr.name));
Matt Domschc988d2b2005-06-23 22:05:15 -07002174 }
2175}
Matt Domschc988d2b2005-06-23 22:05:15 -07002176
Rusty Russella263f772009-09-25 00:32:58 -06002177static void free_modinfo(struct module *mod)
2178{
2179 struct module_attribute *attr;
2180 int i;
2181
2182 for (i = 0; (attr = modinfo_attrs[i]); i++) {
2183 if (attr->free)
2184 attr->free(mod);
2185 }
2186}
2187
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188#ifdef CONFIG_KALLSYMS
WANG Cong15bba372008-07-24 15:41:48 +01002189
2190/* lookup symbol in given range of kernel_symbols */
2191static const struct kernel_symbol *lookup_symbol(const char *name,
2192 const struct kernel_symbol *start,
2193 const struct kernel_symbol *stop)
2194{
Alessio Igor Bogani9d634872011-05-18 22:35:59 +02002195 return bsearch(name, start, stop - start,
2196 sizeof(struct kernel_symbol), cmp_name);
WANG Cong15bba372008-07-24 15:41:48 +01002197}
2198
Tim Abbottca4787b2009-01-05 08:40:10 -06002199static int is_exported(const char *name, unsigned long value,
2200 const struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201{
Tim Abbottca4787b2009-01-05 08:40:10 -06002202 const struct kernel_symbol *ks;
2203 if (!mod)
2204 ks = lookup_symbol(name, __start___ksymtab, __stop___ksymtab);
Sam Ravnborg3fd68052006-02-08 21:16:45 +01002205 else
Tim Abbottca4787b2009-01-05 08:40:10 -06002206 ks = lookup_symbol(name, mod->syms, mod->syms + mod->num_syms);
2207 return ks != NULL && ks->value == value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208}
2209
2210/* As per nm */
Rusty Russelleded41c2010-08-05 12:59:07 -06002211static char elf_type(const Elf_Sym *sym, const struct load_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212{
Rusty Russelleded41c2010-08-05 12:59:07 -06002213 const Elf_Shdr *sechdrs = info->sechdrs;
2214
Linus Torvalds1da177e2005-04-16 15:20:36 -07002215 if (ELF_ST_BIND(sym->st_info) == STB_WEAK) {
2216 if (ELF_ST_TYPE(sym->st_info) == STT_OBJECT)
2217 return 'v';
2218 else
2219 return 'w';
2220 }
2221 if (sym->st_shndx == SHN_UNDEF)
2222 return 'U';
2223 if (sym->st_shndx == SHN_ABS)
2224 return 'a';
2225 if (sym->st_shndx >= SHN_LORESERVE)
2226 return '?';
2227 if (sechdrs[sym->st_shndx].sh_flags & SHF_EXECINSTR)
2228 return 't';
2229 if (sechdrs[sym->st_shndx].sh_flags & SHF_ALLOC
2230 && sechdrs[sym->st_shndx].sh_type != SHT_NOBITS) {
2231 if (!(sechdrs[sym->st_shndx].sh_flags & SHF_WRITE))
2232 return 'r';
2233 else if (sechdrs[sym->st_shndx].sh_flags & ARCH_SHF_SMALL)
2234 return 'g';
2235 else
2236 return 'd';
2237 }
2238 if (sechdrs[sym->st_shndx].sh_type == SHT_NOBITS) {
2239 if (sechdrs[sym->st_shndx].sh_flags & ARCH_SHF_SMALL)
2240 return 's';
2241 else
2242 return 'b';
2243 }
Rusty Russelleded41c2010-08-05 12:59:07 -06002244 if (strstarts(info->secstrings + sechdrs[sym->st_shndx].sh_name,
2245 ".debug")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002246 return 'n';
Rusty Russelleded41c2010-08-05 12:59:07 -06002247 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002248 return '?';
2249}
2250
Jan Beulich4a496222009-07-06 14:50:42 +01002251static bool is_core_symbol(const Elf_Sym *src, const Elf_Shdr *sechdrs,
2252 unsigned int shnum)
2253{
2254 const Elf_Shdr *sec;
2255
2256 if (src->st_shndx == SHN_UNDEF
2257 || src->st_shndx >= shnum
2258 || !src->st_name)
2259 return false;
2260
2261 sec = sechdrs + src->st_shndx;
2262 if (!(sec->sh_flags & SHF_ALLOC)
2263#ifndef CONFIG_KALLSYMS_ALL
2264 || !(sec->sh_flags & SHF_EXECINSTR)
2265#endif
2266 || (sec->sh_entsize & INIT_OFFSET_MASK))
2267 return false;
2268
2269 return true;
2270}
2271
Kevin Cernekee48fd1182012-01-13 09:32:14 +10302272/*
2273 * We only allocate and copy the strings needed by the parts of symtab
2274 * we keep. This is simple, but has the effect of making multiple
2275 * copies of duplicates. We could be more sophisticated, see
2276 * linux-kernel thread starting with
2277 * <73defb5e4bca04a6431392cc341112b1@localhost>.
2278 */
Rusty Russell49668682010-08-05 12:59:10 -06002279static void layout_symtab(struct module *mod, struct load_info *info)
Jan Beulich4a496222009-07-06 14:50:42 +01002280{
Rusty Russell49668682010-08-05 12:59:10 -06002281 Elf_Shdr *symsect = info->sechdrs + info->index.sym;
2282 Elf_Shdr *strsect = info->sechdrs + info->index.str;
Jan Beulich4a496222009-07-06 14:50:42 +01002283 const Elf_Sym *src;
Kevin Cernekee48fd1182012-01-13 09:32:14 +10302284 unsigned int i, nsrc, ndst, strtab_size;
Jan Beulich4a496222009-07-06 14:50:42 +01002285
2286 /* Put symbol section at end of init part of module. */
2287 symsect->sh_flags |= SHF_ALLOC;
2288 symsect->sh_entsize = get_offset(mod, &mod->init_size, symsect,
Rusty Russell49668682010-08-05 12:59:10 -06002289 info->index.sym) | INIT_OFFSET_MASK;
Jim Cromie5e124162011-12-06 12:11:31 -07002290 pr_debug("\t%s\n", info->secstrings + symsect->sh_name);
Jan Beulich4a496222009-07-06 14:50:42 +01002291
Rusty Russell49668682010-08-05 12:59:10 -06002292 src = (void *)info->hdr + symsect->sh_offset;
Jan Beulich4a496222009-07-06 14:50:42 +01002293 nsrc = symsect->sh_size / sizeof(*src);
Kevin Cernekee70b1e9162011-11-12 19:08:55 -08002294
Kevin Cernekee48fd1182012-01-13 09:32:14 +10302295 /* Compute total space required for the core symbols' strtab. */
2296 for (ndst = i = strtab_size = 1; i < nsrc; ++i, ++src)
Rusty Russell49668682010-08-05 12:59:10 -06002297 if (is_core_symbol(src, info->sechdrs, info->hdr->e_shnum)) {
Kevin Cernekee48fd1182012-01-13 09:32:14 +10302298 strtab_size += strlen(&info->strtab[src->st_name]) + 1;
2299 ndst++;
Jan Beulich554bdfe2009-07-06 14:51:44 +01002300 }
Jan Beulich4a496222009-07-06 14:50:42 +01002301
2302 /* Append room for core symbols at end of core part. */
Rusty Russell49668682010-08-05 12:59:10 -06002303 info->symoffs = ALIGN(mod->core_size, symsect->sh_addralign ?: 1);
Kevin Cernekee48fd1182012-01-13 09:32:14 +10302304 info->stroffs = mod->core_size = info->symoffs + ndst * sizeof(Elf_Sym);
2305 mod->core_size += strtab_size;
Jan Beulich4a496222009-07-06 14:50:42 +01002306
Jan Beulich554bdfe2009-07-06 14:51:44 +01002307 /* Put string table section at end of init part of module. */
2308 strsect->sh_flags |= SHF_ALLOC;
2309 strsect->sh_entsize = get_offset(mod, &mod->init_size, strsect,
Rusty Russell49668682010-08-05 12:59:10 -06002310 info->index.str) | INIT_OFFSET_MASK;
Jim Cromie5e124162011-12-06 12:11:31 -07002311 pr_debug("\t%s\n", info->secstrings + strsect->sh_name);
Jan Beulich4a496222009-07-06 14:50:42 +01002312}
2313
Rusty Russell811d66a2010-08-05 12:59:12 -06002314static void add_kallsyms(struct module *mod, const struct load_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002315{
Jan Beulich4a496222009-07-06 14:50:42 +01002316 unsigned int i, ndst;
2317 const Elf_Sym *src;
2318 Elf_Sym *dst;
Jan Beulich554bdfe2009-07-06 14:51:44 +01002319 char *s;
Rusty Russelleded41c2010-08-05 12:59:07 -06002320 Elf_Shdr *symsec = &info->sechdrs[info->index.sym];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002321
Rusty Russelleded41c2010-08-05 12:59:07 -06002322 mod->symtab = (void *)symsec->sh_addr;
2323 mod->num_symtab = symsec->sh_size / sizeof(Elf_Sym);
Rusty Russell511ca6a2010-08-05 12:59:08 -06002324 /* Make sure we get permanent strtab: don't use info->strtab. */
2325 mod->strtab = (void *)info->sechdrs[info->index.str].sh_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002326
2327 /* Set types up while we still have access to sections. */
2328 for (i = 0; i < mod->num_symtab; i++)
Rusty Russelleded41c2010-08-05 12:59:07 -06002329 mod->symtab[i].st_info = elf_type(&mod->symtab[i], info);
Jan Beulich4a496222009-07-06 14:50:42 +01002330
Rusty Russelld9131882010-08-05 12:59:08 -06002331 mod->core_symtab = dst = mod->module_core + info->symoffs;
Kevin Cernekee48fd1182012-01-13 09:32:14 +10302332 mod->core_strtab = s = mod->module_core + info->stroffs;
Jan Beulich4a496222009-07-06 14:50:42 +01002333 src = mod->symtab;
2334 *dst = *src;
Kevin Cernekee48fd1182012-01-13 09:32:14 +10302335 *s++ = 0;
Jan Beulich4a496222009-07-06 14:50:42 +01002336 for (ndst = i = 1; i < mod->num_symtab; ++i, ++src) {
Rusty Russelleded41c2010-08-05 12:59:07 -06002337 if (!is_core_symbol(src, info->sechdrs, info->hdr->e_shnum))
Jan Beulich4a496222009-07-06 14:50:42 +01002338 continue;
Kevin Cernekee48fd1182012-01-13 09:32:14 +10302339
Jan Beulich4a496222009-07-06 14:50:42 +01002340 dst[ndst] = *src;
Kevin Cernekee48fd1182012-01-13 09:32:14 +10302341 dst[ndst++].st_name = s - mod->core_strtab;
2342 s += strlcpy(s, &mod->strtab[src->st_name], KSYM_NAME_LEN) + 1;
Jan Beulich4a496222009-07-06 14:50:42 +01002343 }
2344 mod->core_num_syms = ndst;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002345}
2346#else
Rusty Russell49668682010-08-05 12:59:10 -06002347static inline void layout_symtab(struct module *mod, struct load_info *info)
Jan Beulich4a496222009-07-06 14:50:42 +01002348{
2349}
Paul Mundt3ae91c22009-10-01 15:43:54 -07002350
Michał Mirosławabbce902010-09-20 01:58:08 +02002351static void add_kallsyms(struct module *mod, const struct load_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002352{
2353}
2354#endif /* CONFIG_KALLSYMS */
2355
Jason Barone9d376f2009-02-05 11:51:38 -05002356static void dynamic_debug_setup(struct _ddebug *debug, unsigned int num)
Rusty Russell5e458cc2008-10-22 10:00:13 -05002357{
Rusty Russell811d66a2010-08-05 12:59:12 -06002358 if (!debug)
2359 return;
Jason Barone9d376f2009-02-05 11:51:38 -05002360#ifdef CONFIG_DYNAMIC_DEBUG
2361 if (ddebug_add_module(debug, num, debug->modname))
2362 printk(KERN_ERR "dynamic debug error adding module: %s\n",
2363 debug->modname);
2364#endif
Rusty Russell5e458cc2008-10-22 10:00:13 -05002365}
Jason Baron346e15b2008-08-12 16:46:19 -04002366
Yehuda Sadehff49d742010-07-03 13:07:35 +10002367static void dynamic_debug_remove(struct _ddebug *debug)
2368{
2369 if (debug)
2370 ddebug_remove_module(debug->modname);
2371}
2372
Jonas Bonn74e08fc2011-06-30 21:22:11 +02002373void * __weak module_alloc(unsigned long size)
2374{
2375 return size == 0 ? NULL : vmalloc_exec(size);
2376}
2377
Rusty Russell3a642e92008-07-22 19:24:28 -05002378static void *module_alloc_update_bounds(unsigned long size)
2379{
2380 void *ret = module_alloc(size);
2381
2382 if (ret) {
Rusty Russell75676502010-06-05 11:17:36 -06002383 mutex_lock(&module_mutex);
Rusty Russell3a642e92008-07-22 19:24:28 -05002384 /* Update module bounds. */
2385 if ((unsigned long)ret < module_addr_min)
2386 module_addr_min = (unsigned long)ret;
2387 if ((unsigned long)ret + size > module_addr_max)
2388 module_addr_max = (unsigned long)ret + size;
Rusty Russell75676502010-06-05 11:17:36 -06002389 mutex_unlock(&module_mutex);
Rusty Russell3a642e92008-07-22 19:24:28 -05002390 }
2391 return ret;
2392}
2393
Catalin Marinas4f2294b2009-06-11 13:23:20 +01002394#ifdef CONFIG_DEBUG_KMEMLEAK
Rusty Russell49668682010-08-05 12:59:10 -06002395static void kmemleak_load_module(const struct module *mod,
2396 const struct load_info *info)
Catalin Marinas4f2294b2009-06-11 13:23:20 +01002397{
2398 unsigned int i;
2399
2400 /* only scan the sections containing data */
Catalin Marinasc017b4b2009-10-28 13:33:09 +00002401 kmemleak_scan_area(mod, sizeof(struct module), GFP_KERNEL);
Catalin Marinas4f2294b2009-06-11 13:23:20 +01002402
Rusty Russell49668682010-08-05 12:59:10 -06002403 for (i = 1; i < info->hdr->e_shnum; i++) {
2404 const char *name = info->secstrings + info->sechdrs[i].sh_name;
2405 if (!(info->sechdrs[i].sh_flags & SHF_ALLOC))
Catalin Marinas4f2294b2009-06-11 13:23:20 +01002406 continue;
Rusty Russell49668682010-08-05 12:59:10 -06002407 if (!strstarts(name, ".data") && !strstarts(name, ".bss"))
Catalin Marinas4f2294b2009-06-11 13:23:20 +01002408 continue;
2409
Rusty Russell49668682010-08-05 12:59:10 -06002410 kmemleak_scan_area((void *)info->sechdrs[i].sh_addr,
2411 info->sechdrs[i].sh_size, GFP_KERNEL);
Catalin Marinas4f2294b2009-06-11 13:23:20 +01002412 }
2413}
2414#else
Rusty Russell49668682010-08-05 12:59:10 -06002415static inline void kmemleak_load_module(const struct module *mod,
2416 const struct load_info *info)
Catalin Marinas4f2294b2009-06-11 13:23:20 +01002417{
2418}
2419#endif
2420
Rusty Russell106a4ee2012-09-26 10:09:40 +01002421#ifdef CONFIG_MODULE_SIG
2422static int module_sig_check(struct load_info *info,
2423 const void *mod, unsigned long *len)
2424{
2425 int err = -ENOKEY;
2426 const unsigned long markerlen = sizeof(MODULE_SIG_STRING) - 1;
2427 const void *p = mod, *end = mod + *len;
2428
2429 /* Poor man's memmem. */
2430 while ((p = memchr(p, MODULE_SIG_STRING[0], end - p))) {
2431 if (p + markerlen > end)
2432 break;
2433
2434 if (memcmp(p, MODULE_SIG_STRING, markerlen) == 0) {
2435 const void *sig = p + markerlen;
2436 /* Truncate module up to signature. */
2437 *len = p - mod;
2438 err = mod_verify_sig(mod, *len, sig, end - sig);
2439 break;
2440 }
2441 p++;
2442 }
2443
2444 if (!err) {
2445 info->sig_ok = true;
2446 return 0;
2447 }
2448
2449 /* Not having a signature is only an error if we're strict. */
2450 if (err == -ENOKEY && !sig_enforce)
2451 err = 0;
2452
2453 return err;
2454}
2455#else /* !CONFIG_MODULE_SIG */
2456static int module_sig_check(struct load_info *info,
2457 void *mod, unsigned long *len)
2458{
2459 return 0;
2460}
2461#endif /* !CONFIG_MODULE_SIG */
2462
2463/* Sets info->hdr, info->len and info->sig_ok. */
Rusty Russelld9131882010-08-05 12:59:08 -06002464static int copy_and_check(struct load_info *info,
2465 const void __user *umod, unsigned long len,
2466 const char __user *uargs)
Rusty Russell40dd2562010-08-05 12:59:03 -06002467{
2468 int err;
2469 Elf_Ehdr *hdr;
2470
2471 if (len < sizeof(*hdr))
2472 return -ENOEXEC;
2473
2474 /* Suck in entire file: we'll want most of it. */
Sasha Levinf946eeb2012-01-30 23:07:22 -05002475 if ((hdr = vmalloc(len)) == NULL)
Rusty Russell40dd2562010-08-05 12:59:03 -06002476 return -ENOMEM;
2477
2478 if (copy_from_user(hdr, umod, len) != 0) {
2479 err = -EFAULT;
2480 goto free_hdr;
2481 }
2482
Rusty Russell106a4ee2012-09-26 10:09:40 +01002483 err = module_sig_check(info, hdr, &len);
2484 if (err)
2485 goto free_hdr;
2486
Rusty Russell40dd2562010-08-05 12:59:03 -06002487 /* Sanity checks against insmoding binaries or wrong arch,
2488 weird elf version */
2489 if (memcmp(hdr->e_ident, ELFMAG, SELFMAG) != 0
2490 || hdr->e_type != ET_REL
2491 || !elf_check_arch(hdr)
2492 || hdr->e_shentsize != sizeof(Elf_Shdr)) {
2493 err = -ENOEXEC;
2494 goto free_hdr;
2495 }
2496
David Howellsef26a5a2012-05-22 15:56:13 +01002497 if (hdr->e_shoff >= len ||
2498 hdr->e_shnum * sizeof(Elf_Shdr) > len - hdr->e_shoff) {
Rusty Russell40dd2562010-08-05 12:59:03 -06002499 err = -ENOEXEC;
2500 goto free_hdr;
2501 }
Rusty Russelld9131882010-08-05 12:59:08 -06002502
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002503 info->hdr = hdr;
2504 info->len = len;
Rusty Russell40dd2562010-08-05 12:59:03 -06002505 return 0;
2506
2507free_hdr:
2508 vfree(hdr);
2509 return err;
2510}
2511
Rusty Russelld9131882010-08-05 12:59:08 -06002512static void free_copy(struct load_info *info)
2513{
Rusty Russelld9131882010-08-05 12:59:08 -06002514 vfree(info->hdr);
2515}
2516
Rusty Russell8b5f61a2010-08-05 12:59:06 -06002517static int rewrite_section_headers(struct load_info *info)
2518{
2519 unsigned int i;
2520
2521 /* This should always be true, but let's be sure. */
2522 info->sechdrs[0].sh_addr = 0;
2523
2524 for (i = 1; i < info->hdr->e_shnum; i++) {
2525 Elf_Shdr *shdr = &info->sechdrs[i];
2526 if (shdr->sh_type != SHT_NOBITS
2527 && info->len < shdr->sh_offset + shdr->sh_size) {
2528 printk(KERN_ERR "Module len %lu truncated\n",
2529 info->len);
2530 return -ENOEXEC;
2531 }
2532
2533 /* Mark all sections sh_addr with their address in the
2534 temporary image. */
2535 shdr->sh_addr = (size_t)info->hdr + shdr->sh_offset;
2536
2537#ifndef CONFIG_MODULE_UNLOAD
2538 /* Don't load .exit sections */
2539 if (strstarts(info->secstrings+shdr->sh_name, ".exit"))
2540 shdr->sh_flags &= ~(unsigned long)SHF_ALLOC;
2541#endif
Rusty Russell8b5f61a2010-08-05 12:59:06 -06002542 }
Rusty Russelld6df72a2010-08-05 12:59:07 -06002543
2544 /* Track but don't keep modinfo and version sections. */
Rusty Russell49668682010-08-05 12:59:10 -06002545 info->index.vers = find_sec(info, "__versions");
2546 info->index.info = find_sec(info, ".modinfo");
Rusty Russelld6df72a2010-08-05 12:59:07 -06002547 info->sechdrs[info->index.info].sh_flags &= ~(unsigned long)SHF_ALLOC;
2548 info->sechdrs[info->index.vers].sh_flags &= ~(unsigned long)SHF_ALLOC;
Rusty Russell8b5f61a2010-08-05 12:59:06 -06002549 return 0;
2550}
2551
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002552/*
2553 * Set up our basic convenience variables (pointers to section headers,
2554 * search for module section index etc), and do some basic section
2555 * verification.
2556 *
2557 * Return the temporary module pointer (we'll replace it with the final
2558 * one when we move the module sections around).
2559 */
2560static struct module *setup_load_info(struct load_info *info)
2561{
2562 unsigned int i;
Rusty Russell8b5f61a2010-08-05 12:59:06 -06002563 int err;
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002564 struct module *mod;
2565
2566 /* Set up the convenience variables */
2567 info->sechdrs = (void *)info->hdr + info->hdr->e_shoff;
Rusty Russell8b5f61a2010-08-05 12:59:06 -06002568 info->secstrings = (void *)info->hdr
2569 + info->sechdrs[info->hdr->e_shstrndx].sh_offset;
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002570
Rusty Russell8b5f61a2010-08-05 12:59:06 -06002571 err = rewrite_section_headers(info);
2572 if (err)
2573 return ERR_PTR(err);
2574
2575 /* Find internal symbols and strings. */
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002576 for (i = 1; i < info->hdr->e_shnum; i++) {
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002577 if (info->sechdrs[i].sh_type == SHT_SYMTAB) {
2578 info->index.sym = i;
2579 info->index.str = info->sechdrs[i].sh_link;
Rusty Russell8b5f61a2010-08-05 12:59:06 -06002580 info->strtab = (char *)info->hdr
2581 + info->sechdrs[info->index.str].sh_offset;
2582 break;
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002583 }
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002584 }
2585
Rusty Russell49668682010-08-05 12:59:10 -06002586 info->index.mod = find_sec(info, ".gnu.linkonce.this_module");
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002587 if (!info->index.mod) {
2588 printk(KERN_WARNING "No module found in object\n");
2589 return ERR_PTR(-ENOEXEC);
2590 }
2591 /* This is temporary: point mod into copy of data. */
2592 mod = (void *)info->sechdrs[info->index.mod].sh_addr;
2593
2594 if (info->index.sym == 0) {
2595 printk(KERN_WARNING "%s: module has no symbols (stripped?)\n",
2596 mod->name);
2597 return ERR_PTR(-ENOEXEC);
2598 }
2599
Rusty Russell49668682010-08-05 12:59:10 -06002600 info->index.pcpu = find_pcpusec(info);
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002601
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002602 /* Check module struct version now, before we try to use module. */
2603 if (!check_modstruct_version(info->sechdrs, info->index.vers, mod))
2604 return ERR_PTR(-ENOEXEC);
2605
2606 return mod;
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002607}
2608
Rusty Russell49668682010-08-05 12:59:10 -06002609static int check_modinfo(struct module *mod, struct load_info *info)
Rusty Russell40dd2562010-08-05 12:59:03 -06002610{
Rusty Russell49668682010-08-05 12:59:10 -06002611 const char *modmagic = get_modinfo(info, "vermagic");
Rusty Russell40dd2562010-08-05 12:59:03 -06002612 int err;
2613
2614 /* This is allowed: modprobe --force will invalidate it. */
2615 if (!modmagic) {
2616 err = try_to_force_load(mod, "bad vermagic");
2617 if (err)
2618 return err;
Rusty Russell49668682010-08-05 12:59:10 -06002619 } else if (!same_magic(modmagic, vermagic, info->index.vers)) {
Rusty Russell40dd2562010-08-05 12:59:03 -06002620 printk(KERN_ERR "%s: version magic '%s' should be '%s'\n",
2621 mod->name, modmagic, vermagic);
2622 return -ENOEXEC;
2623 }
2624
Ben Hutchings2449b8b2011-10-24 15:12:28 +02002625 if (!get_modinfo(info, "intree"))
2626 add_taint_module(mod, TAINT_OOT_MODULE);
2627
Rusty Russell49668682010-08-05 12:59:10 -06002628 if (get_modinfo(info, "staging")) {
Rusty Russell40dd2562010-08-05 12:59:03 -06002629 add_taint_module(mod, TAINT_CRAP);
2630 printk(KERN_WARNING "%s: module is from the staging directory,"
2631 " the quality is unknown, you have been warned.\n",
2632 mod->name);
2633 }
Rusty Russell22e268e2010-08-05 12:59:05 -06002634
2635 /* Set up license info based on the info section */
Rusty Russell49668682010-08-05 12:59:10 -06002636 set_license(mod, get_modinfo(info, "license"));
Rusty Russell22e268e2010-08-05 12:59:05 -06002637
Rusty Russell40dd2562010-08-05 12:59:03 -06002638 return 0;
2639}
2640
Rusty Russell811d66a2010-08-05 12:59:12 -06002641static void find_module_sections(struct module *mod, struct load_info *info)
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06002642{
Rusty Russell49668682010-08-05 12:59:10 -06002643 mod->kp = section_objs(info, "__param",
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06002644 sizeof(*mod->kp), &mod->num_kp);
Rusty Russell49668682010-08-05 12:59:10 -06002645 mod->syms = section_objs(info, "__ksymtab",
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06002646 sizeof(*mod->syms), &mod->num_syms);
Rusty Russell49668682010-08-05 12:59:10 -06002647 mod->crcs = section_addr(info, "__kcrctab");
2648 mod->gpl_syms = section_objs(info, "__ksymtab_gpl",
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06002649 sizeof(*mod->gpl_syms),
2650 &mod->num_gpl_syms);
Rusty Russell49668682010-08-05 12:59:10 -06002651 mod->gpl_crcs = section_addr(info, "__kcrctab_gpl");
2652 mod->gpl_future_syms = section_objs(info,
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06002653 "__ksymtab_gpl_future",
2654 sizeof(*mod->gpl_future_syms),
2655 &mod->num_gpl_future_syms);
Rusty Russell49668682010-08-05 12:59:10 -06002656 mod->gpl_future_crcs = section_addr(info, "__kcrctab_gpl_future");
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06002657
2658#ifdef CONFIG_UNUSED_SYMBOLS
Rusty Russell49668682010-08-05 12:59:10 -06002659 mod->unused_syms = section_objs(info, "__ksymtab_unused",
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06002660 sizeof(*mod->unused_syms),
2661 &mod->num_unused_syms);
Rusty Russell49668682010-08-05 12:59:10 -06002662 mod->unused_crcs = section_addr(info, "__kcrctab_unused");
2663 mod->unused_gpl_syms = section_objs(info, "__ksymtab_unused_gpl",
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06002664 sizeof(*mod->unused_gpl_syms),
2665 &mod->num_unused_gpl_syms);
Rusty Russell49668682010-08-05 12:59:10 -06002666 mod->unused_gpl_crcs = section_addr(info, "__kcrctab_unused_gpl");
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06002667#endif
2668#ifdef CONFIG_CONSTRUCTORS
Rusty Russell49668682010-08-05 12:59:10 -06002669 mod->ctors = section_objs(info, ".ctors",
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06002670 sizeof(*mod->ctors), &mod->num_ctors);
2671#endif
2672
2673#ifdef CONFIG_TRACEPOINTS
Mathieu Desnoyers65498642011-01-26 17:26:22 -05002674 mod->tracepoints_ptrs = section_objs(info, "__tracepoints_ptrs",
2675 sizeof(*mod->tracepoints_ptrs),
2676 &mod->num_tracepoints);
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06002677#endif
Jason Baronbf5438fc2010-09-17 11:09:00 -04002678#ifdef HAVE_JUMP_LABEL
2679 mod->jump_entries = section_objs(info, "__jump_table",
2680 sizeof(*mod->jump_entries),
2681 &mod->num_jump_entries);
2682#endif
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06002683#ifdef CONFIG_EVENT_TRACING
Rusty Russell49668682010-08-05 12:59:10 -06002684 mod->trace_events = section_objs(info, "_ftrace_events",
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06002685 sizeof(*mod->trace_events),
2686 &mod->num_trace_events);
2687 /*
2688 * This section contains pointers to allocated objects in the trace
2689 * code and not scanning it leads to false positives.
2690 */
2691 kmemleak_scan_area(mod->trace_events, sizeof(*mod->trace_events) *
2692 mod->num_trace_events, GFP_KERNEL);
2693#endif
Steven Rostedt13b9b6e2010-11-10 22:19:24 -05002694#ifdef CONFIG_TRACING
2695 mod->trace_bprintk_fmt_start = section_objs(info, "__trace_printk_fmt",
2696 sizeof(*mod->trace_bprintk_fmt_start),
2697 &mod->num_trace_bprintk_fmt);
2698 /*
2699 * This section contains pointers to allocated objects in the trace
2700 * code and not scanning it leads to false positives.
2701 */
2702 kmemleak_scan_area(mod->trace_bprintk_fmt_start,
2703 sizeof(*mod->trace_bprintk_fmt_start) *
2704 mod->num_trace_bprintk_fmt, GFP_KERNEL);
2705#endif
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06002706#ifdef CONFIG_FTRACE_MCOUNT_RECORD
2707 /* sechdrs[0].sh_size is always zero */
Rusty Russell49668682010-08-05 12:59:10 -06002708 mod->ftrace_callsites = section_objs(info, "__mcount_loc",
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06002709 sizeof(*mod->ftrace_callsites),
2710 &mod->num_ftrace_callsites);
2711#endif
Rusty Russell22e268e2010-08-05 12:59:05 -06002712
Rusty Russell811d66a2010-08-05 12:59:12 -06002713 mod->extable = section_objs(info, "__ex_table",
2714 sizeof(*mod->extable), &mod->num_exentries);
2715
Rusty Russell49668682010-08-05 12:59:10 -06002716 if (section_addr(info, "__obsparm"))
Rusty Russell22e268e2010-08-05 12:59:05 -06002717 printk(KERN_WARNING "%s: Ignoring obsolete parameters\n",
2718 mod->name);
Rusty Russell811d66a2010-08-05 12:59:12 -06002719
2720 info->debug = section_objs(info, "__verbose",
2721 sizeof(*info->debug), &info->num_debug);
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06002722}
2723
Rusty Russell49668682010-08-05 12:59:10 -06002724static int move_module(struct module *mod, struct load_info *info)
Linus Torvalds65b8a9b2010-08-05 12:59:02 -06002725{
2726 int i;
2727 void *ptr;
2728
2729 /* Do the allocs. */
2730 ptr = module_alloc_update_bounds(mod->core_size);
2731 /*
2732 * The pointer to this block is stored in the module structure
2733 * which is inside the block. Just mark it as not being a
2734 * leak.
2735 */
2736 kmemleak_not_leak(ptr);
2737 if (!ptr)
Rusty Russelld9131882010-08-05 12:59:08 -06002738 return -ENOMEM;
Linus Torvalds65b8a9b2010-08-05 12:59:02 -06002739
2740 memset(ptr, 0, mod->core_size);
2741 mod->module_core = ptr;
2742
2743 ptr = module_alloc_update_bounds(mod->init_size);
2744 /*
2745 * The pointer to this block is stored in the module structure
2746 * which is inside the block. This block doesn't need to be
2747 * scanned as it contains data and code that will be freed
2748 * after the module is initialized.
2749 */
2750 kmemleak_ignore(ptr);
2751 if (!ptr && mod->init_size) {
2752 module_free(mod, mod->module_core);
Rusty Russelld9131882010-08-05 12:59:08 -06002753 return -ENOMEM;
Linus Torvalds65b8a9b2010-08-05 12:59:02 -06002754 }
2755 memset(ptr, 0, mod->init_size);
2756 mod->module_init = ptr;
2757
2758 /* Transfer each section which specifies SHF_ALLOC */
Jim Cromie5e124162011-12-06 12:11:31 -07002759 pr_debug("final section addresses:\n");
Rusty Russell49668682010-08-05 12:59:10 -06002760 for (i = 0; i < info->hdr->e_shnum; i++) {
Linus Torvalds65b8a9b2010-08-05 12:59:02 -06002761 void *dest;
Rusty Russell49668682010-08-05 12:59:10 -06002762 Elf_Shdr *shdr = &info->sechdrs[i];
Linus Torvalds65b8a9b2010-08-05 12:59:02 -06002763
Rusty Russell49668682010-08-05 12:59:10 -06002764 if (!(shdr->sh_flags & SHF_ALLOC))
Linus Torvalds65b8a9b2010-08-05 12:59:02 -06002765 continue;
2766
Rusty Russell49668682010-08-05 12:59:10 -06002767 if (shdr->sh_entsize & INIT_OFFSET_MASK)
Linus Torvalds65b8a9b2010-08-05 12:59:02 -06002768 dest = mod->module_init
Rusty Russell49668682010-08-05 12:59:10 -06002769 + (shdr->sh_entsize & ~INIT_OFFSET_MASK);
Linus Torvalds65b8a9b2010-08-05 12:59:02 -06002770 else
Rusty Russell49668682010-08-05 12:59:10 -06002771 dest = mod->module_core + shdr->sh_entsize;
Linus Torvalds65b8a9b2010-08-05 12:59:02 -06002772
Rusty Russell49668682010-08-05 12:59:10 -06002773 if (shdr->sh_type != SHT_NOBITS)
2774 memcpy(dest, (void *)shdr->sh_addr, shdr->sh_size);
Linus Torvalds65b8a9b2010-08-05 12:59:02 -06002775 /* Update sh_addr to point to copy in image. */
Rusty Russell49668682010-08-05 12:59:10 -06002776 shdr->sh_addr = (unsigned long)dest;
Jim Cromie5e124162011-12-06 12:11:31 -07002777 pr_debug("\t0x%lx %s\n",
2778 (long)shdr->sh_addr, info->secstrings + shdr->sh_name);
Linus Torvalds65b8a9b2010-08-05 12:59:02 -06002779 }
Rusty Russelld9131882010-08-05 12:59:08 -06002780
2781 return 0;
Linus Torvalds65b8a9b2010-08-05 12:59:02 -06002782}
2783
Rusty Russell49668682010-08-05 12:59:10 -06002784static int check_module_license_and_versions(struct module *mod)
Rusty Russell22e268e2010-08-05 12:59:05 -06002785{
2786 /*
2787 * ndiswrapper is under GPL by itself, but loads proprietary modules.
2788 * Don't use add_taint_module(), as it would prevent ndiswrapper from
2789 * using GPL-only symbols it needs.
2790 */
2791 if (strcmp(mod->name, "ndiswrapper") == 0)
2792 add_taint(TAINT_PROPRIETARY_MODULE);
2793
2794 /* driverloader was caught wrongly pretending to be under GPL */
2795 if (strcmp(mod->name, "driverloader") == 0)
2796 add_taint_module(mod, TAINT_PROPRIETARY_MODULE);
2797
Matthew Garrettc99af372012-06-22 13:49:31 -04002798 /* lve claims to be GPL but upstream won't provide source */
2799 if (strcmp(mod->name, "lve") == 0)
2800 add_taint_module(mod, TAINT_PROPRIETARY_MODULE);
2801
Rusty Russell22e268e2010-08-05 12:59:05 -06002802#ifdef CONFIG_MODVERSIONS
2803 if ((mod->num_syms && !mod->crcs)
2804 || (mod->num_gpl_syms && !mod->gpl_crcs)
2805 || (mod->num_gpl_future_syms && !mod->gpl_future_crcs)
2806#ifdef CONFIG_UNUSED_SYMBOLS
2807 || (mod->num_unused_syms && !mod->unused_crcs)
2808 || (mod->num_unused_gpl_syms && !mod->unused_gpl_crcs)
2809#endif
2810 ) {
2811 return try_to_force_load(mod,
2812 "no versions for exported symbols");
2813 }
2814#endif
2815 return 0;
2816}
2817
2818static void flush_module_icache(const struct module *mod)
2819{
2820 mm_segment_t old_fs;
2821
2822 /* flush the icache in correct context */
2823 old_fs = get_fs();
2824 set_fs(KERNEL_DS);
2825
2826 /*
2827 * Flush the instruction cache, since we've played with text.
2828 * Do it before processing of module parameters, so the module
2829 * can provide parameter accessor functions of its own.
2830 */
2831 if (mod->module_init)
2832 flush_icache_range((unsigned long)mod->module_init,
2833 (unsigned long)mod->module_init
2834 + mod->init_size);
2835 flush_icache_range((unsigned long)mod->module_core,
2836 (unsigned long)mod->module_core + mod->core_size);
2837
2838 set_fs(old_fs);
2839}
2840
Jonas Bonn74e08fc2011-06-30 21:22:11 +02002841int __weak module_frob_arch_sections(Elf_Ehdr *hdr,
2842 Elf_Shdr *sechdrs,
2843 char *secstrings,
2844 struct module *mod)
2845{
2846 return 0;
2847}
2848
Rusty Russelld9131882010-08-05 12:59:08 -06002849static struct module *layout_and_allocate(struct load_info *info)
2850{
2851 /* Module within temporary copy. */
2852 struct module *mod;
Rusty Russell49668682010-08-05 12:59:10 -06002853 Elf_Shdr *pcpusec;
Rusty Russelld9131882010-08-05 12:59:08 -06002854 int err;
2855
2856 mod = setup_load_info(info);
2857 if (IS_ERR(mod))
2858 return mod;
2859
Rusty Russell49668682010-08-05 12:59:10 -06002860 err = check_modinfo(mod, info);
Rusty Russelld9131882010-08-05 12:59:08 -06002861 if (err)
2862 return ERR_PTR(err);
2863
2864 /* Allow arches to frob section contents and sizes. */
Rusty Russell49668682010-08-05 12:59:10 -06002865 err = module_frob_arch_sections(info->hdr, info->sechdrs,
2866 info->secstrings, mod);
Rusty Russelld9131882010-08-05 12:59:08 -06002867 if (err < 0)
Rusty Russell6526c532010-08-05 12:59:10 -06002868 goto out;
Rusty Russelld9131882010-08-05 12:59:08 -06002869
Rusty Russell49668682010-08-05 12:59:10 -06002870 pcpusec = &info->sechdrs[info->index.pcpu];
2871 if (pcpusec->sh_size) {
Rusty Russelld9131882010-08-05 12:59:08 -06002872 /* We have a special allocation for this section. */
Rusty Russell49668682010-08-05 12:59:10 -06002873 err = percpu_modalloc(mod,
2874 pcpusec->sh_size, pcpusec->sh_addralign);
Rusty Russelld9131882010-08-05 12:59:08 -06002875 if (err)
Rusty Russell6526c532010-08-05 12:59:10 -06002876 goto out;
Rusty Russell49668682010-08-05 12:59:10 -06002877 pcpusec->sh_flags &= ~(unsigned long)SHF_ALLOC;
Rusty Russelld9131882010-08-05 12:59:08 -06002878 }
2879
2880 /* Determine total sizes, and put offsets in sh_entsize. For now
2881 this is done generically; there doesn't appear to be any
2882 special cases for the architectures. */
Rusty Russell49668682010-08-05 12:59:10 -06002883 layout_sections(mod, info);
Rusty Russell49668682010-08-05 12:59:10 -06002884 layout_symtab(mod, info);
Rusty Russelld9131882010-08-05 12:59:08 -06002885
2886 /* Allocate and move to the final place */
Rusty Russell49668682010-08-05 12:59:10 -06002887 err = move_module(mod, info);
Rusty Russelld9131882010-08-05 12:59:08 -06002888 if (err)
Kevin Cernekee48fd1182012-01-13 09:32:14 +10302889 goto free_percpu;
Rusty Russelld9131882010-08-05 12:59:08 -06002890
2891 /* Module has been copied to its final place now: return it. */
2892 mod = (void *)info->sechdrs[info->index.mod].sh_addr;
Rusty Russell49668682010-08-05 12:59:10 -06002893 kmemleak_load_module(mod, info);
Rusty Russelld9131882010-08-05 12:59:08 -06002894 return mod;
2895
Rusty Russelld9131882010-08-05 12:59:08 -06002896free_percpu:
2897 percpu_modfree(mod);
Rusty Russell6526c532010-08-05 12:59:10 -06002898out:
Rusty Russelld9131882010-08-05 12:59:08 -06002899 return ERR_PTR(err);
2900}
2901
2902/* mod is no longer valid after this! */
2903static void module_deallocate(struct module *mod, struct load_info *info)
2904{
Rusty Russelld9131882010-08-05 12:59:08 -06002905 percpu_modfree(mod);
2906 module_free(mod, mod->module_init);
2907 module_free(mod, mod->module_core);
2908}
2909
Jonas Bonn74e08fc2011-06-30 21:22:11 +02002910int __weak module_finalize(const Elf_Ehdr *hdr,
2911 const Elf_Shdr *sechdrs,
2912 struct module *me)
2913{
2914 return 0;
2915}
2916
Rusty Russell811d66a2010-08-05 12:59:12 -06002917static int post_relocation(struct module *mod, const struct load_info *info)
2918{
Rusty Russell51f3d0f2010-08-05 12:59:13 -06002919 /* Sort exception table now relocations are done. */
Rusty Russell811d66a2010-08-05 12:59:12 -06002920 sort_extable(mod->extable, mod->extable + mod->num_exentries);
2921
2922 /* Copy relocated percpu area over. */
2923 percpu_modcopy(mod, (void *)info->sechdrs[info->index.pcpu].sh_addr,
2924 info->sechdrs[info->index.pcpu].sh_size);
2925
Rusty Russell51f3d0f2010-08-05 12:59:13 -06002926 /* Setup kallsyms-specific fields. */
Rusty Russell811d66a2010-08-05 12:59:12 -06002927 add_kallsyms(mod, info);
2928
2929 /* Arch-specific module finalizing. */
2930 return module_finalize(info->hdr, info->sechdrs, mod);
2931}
2932
Rusty Russell9bb9c3b2012-09-28 14:31:03 +09302933/* Is this module of this name done loading? No locks held. */
2934static bool finished_loading(const char *name)
2935{
2936 struct module *mod;
2937 bool ret;
2938
2939 mutex_lock(&module_mutex);
2940 mod = find_module(name);
2941 ret = !mod || mod->state != MODULE_STATE_COMING;
2942 mutex_unlock(&module_mutex);
2943
2944 return ret;
2945}
2946
Linus Torvalds1da177e2005-04-16 15:20:36 -07002947/* Allocate and load the module: note that size of section 0 is always
2948 zero, and we rely on this for optional sections. */
Rusty Russell51f3d0f2010-08-05 12:59:13 -06002949static struct module *load_module(void __user *umod,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002950 unsigned long len,
2951 const char __user *uargs)
2952{
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002953 struct load_info info = { NULL, };
Rusty Russell9bb9c3b2012-09-28 14:31:03 +09302954 struct module *mod, *old;
Rusty Russell40dd2562010-08-05 12:59:03 -06002955 long err;
Paul Mundt3ae91c22009-10-01 15:43:54 -07002956
Jim Cromie5e124162011-12-06 12:11:31 -07002957 pr_debug("load_module: umod=%p, len=%lu, uargs=%p\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002958 umod, len, uargs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002959
Rusty Russelld9131882010-08-05 12:59:08 -06002960 /* Copy in the blobs from userspace, check they are vaguely sane. */
2961 err = copy_and_check(&info, umod, len, uargs);
Rusty Russell40dd2562010-08-05 12:59:03 -06002962 if (err)
2963 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002964
Rusty Russelld9131882010-08-05 12:59:08 -06002965 /* Figure out module layout, and allocate all the memory. */
2966 mod = layout_and_allocate(&info);
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002967 if (IS_ERR(mod)) {
2968 err = PTR_ERR(mod);
Rusty Russelld9131882010-08-05 12:59:08 -06002969 goto free_copy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002970 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002971
Rusty Russell106a4ee2012-09-26 10:09:40 +01002972#ifdef CONFIG_MODULE_SIG
2973 mod->sig_ok = info.sig_ok;
2974 if (!mod->sig_ok)
2975 add_taint_module(mod, TAINT_FORCED_MODULE);
2976#endif
2977
Rusty Russell49668682010-08-05 12:59:10 -06002978 /* Now module is in final location, initialize linked lists, etc. */
Rusty Russell9f85a4b2010-08-05 12:59:04 -06002979 err = module_unload_init(mod);
2980 if (err)
Rusty Russelld9131882010-08-05 12:59:08 -06002981 goto free_module;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002982
Rusty Russell22e268e2010-08-05 12:59:05 -06002983 /* Now we've got everything in the final locations, we can
2984 * find optional sections. */
Rusty Russell49668682010-08-05 12:59:10 -06002985 find_module_sections(mod, &info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002986
Rusty Russell49668682010-08-05 12:59:10 -06002987 err = check_module_license_and_versions(mod);
Rusty Russell22e268e2010-08-05 12:59:05 -06002988 if (err)
2989 goto free_unload;
Dave Jones9841d61d2006-01-08 01:03:41 -08002990
Matt Domschc988d2b2005-06-23 22:05:15 -07002991 /* Set up MODINFO_ATTR fields */
Rusty Russell49668682010-08-05 12:59:10 -06002992 setup_modinfo(mod, &info);
Matt Domschc988d2b2005-06-23 22:05:15 -07002993
Linus Torvalds1da177e2005-04-16 15:20:36 -07002994 /* Fix up syms, so that st_value is a pointer to location. */
Rusty Russell49668682010-08-05 12:59:10 -06002995 err = simplify_symbols(mod, &info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002996 if (err < 0)
Rusty Russelld9131882010-08-05 12:59:08 -06002997 goto free_modinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002998
Rusty Russell49668682010-08-05 12:59:10 -06002999 err = apply_relocations(mod, &info);
Rusty Russell22e268e2010-08-05 12:59:05 -06003000 if (err < 0)
Rusty Russelld9131882010-08-05 12:59:08 -06003001 goto free_modinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003002
Rusty Russell811d66a2010-08-05 12:59:12 -06003003 err = post_relocation(mod, &info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003004 if (err < 0)
Rusty Russelld9131882010-08-05 12:59:08 -06003005 goto free_modinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003006
Rusty Russell22e268e2010-08-05 12:59:05 -06003007 flush_module_icache(mod);
Thomas Koeller378bac82005-09-06 15:17:11 -07003008
Rusty Russell6526c532010-08-05 12:59:10 -06003009 /* Now copy in args */
3010 mod->args = strndup_user(uargs, ~0UL >> 1);
3011 if (IS_ERR(mod->args)) {
3012 err = PTR_ERR(mod->args);
3013 goto free_arch_cleanup;
3014 }
Rusty Russell8d3b33f2006-03-25 03:07:05 -08003015
Rusty Russell51f3d0f2010-08-05 12:59:13 -06003016 /* Mark state as coming so strong_try_module_get() ignores us. */
Rusty Russelld9131882010-08-05 12:59:08 -06003017 mod->state = MODULE_STATE_COMING;
3018
Rusty Russellbb9d3d52008-01-29 17:13:21 -05003019 /* Now sew it into the lists so we can get lockdep and oops
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003020 * info during argument parsing. No one should access us, since
Andi Kleend72b3752008-08-30 10:09:00 +02003021 * strong_try_module_get() will fail.
3022 * lockdep/oops can run asynchronous, so use the RCU list insertion
3023 * function to insert in a way safe to concurrent readers.
3024 * The mutex protects against concurrent writers.
3025 */
Rusty Russell9bb9c3b2012-09-28 14:31:03 +09303026again:
Rusty Russell75676502010-06-05 11:17:36 -06003027 mutex_lock(&module_mutex);
Rusty Russell9bb9c3b2012-09-28 14:31:03 +09303028 if ((old = find_module(mod->name)) != NULL) {
3029 if (old->state == MODULE_STATE_COMING) {
3030 /* Wait in case it fails to load. */
3031 mutex_unlock(&module_mutex);
3032 err = wait_event_interruptible(module_wq,
3033 finished_loading(mod->name));
3034 if (err)
3035 goto free_arch_cleanup;
3036 goto again;
3037 }
Linus Torvalds3bafeb62010-06-05 11:17:36 -06003038 err = -EEXIST;
Rusty Russellbe593f42010-06-05 11:17:37 -06003039 goto unlock;
Linus Torvalds3bafeb62010-06-05 11:17:36 -06003040 }
3041
Rusty Russell811d66a2010-08-05 12:59:12 -06003042 /* This has to be done once we're sure module name is unique. */
Ben Hutchings1cd0d6c2011-11-01 03:59:33 +00003043 dynamic_debug_setup(info.debug, info.num_debug);
Yehuda Sadehff49d742010-07-03 13:07:35 +10003044
Rusty Russellbe593f42010-06-05 11:17:37 -06003045 /* Find duplicate symbols */
3046 err = verify_export_symbols(mod);
3047 if (err < 0)
Yehuda Sadehff49d742010-07-03 13:07:35 +10003048 goto ddebug;
Rusty Russellbe593f42010-06-05 11:17:37 -06003049
Linus Torvalds53363772010-10-05 11:29:27 -07003050 module_bug_finalize(info.hdr, info.sechdrs, mod);
Andi Kleend72b3752008-08-30 10:09:00 +02003051 list_add_rcu(&mod->list, &modules);
Rusty Russell75676502010-06-05 11:17:36 -06003052 mutex_unlock(&module_mutex);
Rusty Russellbb9d3d52008-01-29 17:13:21 -05003053
Rusty Russell51f3d0f2010-08-05 12:59:13 -06003054 /* Module is ready to execute: parsing args may do that. */
Pawel Moll026cee02012-03-26 12:50:51 +10303055 err = parse_args(mod->name, mod->args, mod->kp, mod->num_kp,
Jim Cromieb48420c2012-04-27 14:30:35 -06003056 -32768, 32767, &ddebug_dyndbg_module_param_cb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003057 if (err < 0)
Rusty Russellbb9d3d52008-01-29 17:13:21 -05003058 goto unlink;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003059
Rusty Russell51f3d0f2010-08-05 12:59:13 -06003060 /* Link in to syfs. */
Rusty Russell8f6d0372010-08-05 12:59:09 -06003061 err = mod_sysfs_setup(mod, &info, mod->kp, mod->num_kp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003062 if (err < 0)
Rusty Russellbb9d3d52008-01-29 17:13:21 -05003063 goto unlink;
Rusty Russell80a3d1b2010-06-05 11:17:36 -06003064
Kevin Cernekee48fd1182012-01-13 09:32:14 +10303065 /* Get rid of temporary copy. */
Rusty Russelld9131882010-08-05 12:59:08 -06003066 free_copy(&info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003067
3068 /* Done! */
Rusty Russell51f3d0f2010-08-05 12:59:13 -06003069 trace_module_load(mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003070 return mod;
3071
Rusty Russellbb9d3d52008-01-29 17:13:21 -05003072 unlink:
Rusty Russell75676502010-06-05 11:17:36 -06003073 mutex_lock(&module_mutex);
Rusty Russelle91defa2009-03-31 13:05:35 -06003074 /* Unlink carefully: kallsyms could be walking list. */
3075 list_del_rcu(&mod->list);
Linus Torvalds53363772010-10-05 11:29:27 -07003076 module_bug_cleanup(mod);
Rusty Russell6f139092012-09-28 14:31:03 +09303077 wake_up_all(&module_wq);
Yehuda Sadehff49d742010-07-03 13:07:35 +10003078 ddebug:
Ben Hutchings1cd0d6c2011-11-01 03:59:33 +00003079 dynamic_debug_remove(info.debug);
Rusty Russellbe593f42010-06-05 11:17:37 -06003080 unlock:
Rusty Russell75676502010-06-05 11:17:36 -06003081 mutex_unlock(&module_mutex);
Rusty Russelle91defa2009-03-31 13:05:35 -06003082 synchronize_sched();
Rusty Russell6526c532010-08-05 12:59:10 -06003083 kfree(mod->args);
3084 free_arch_cleanup:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003085 module_arch_cleanup(mod);
Rusty Russelld9131882010-08-05 12:59:08 -06003086 free_modinfo:
Rusty Russella263f772009-09-25 00:32:58 -06003087 free_modinfo(mod);
Rusty Russell22e268e2010-08-05 12:59:05 -06003088 free_unload:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003089 module_unload_free(mod);
Rusty Russelld9131882010-08-05 12:59:08 -06003090 free_module:
3091 module_deallocate(mod, &info);
3092 free_copy:
3093 free_copy(&info);
Jayachandran C6fe2e702006-01-06 00:19:54 -08003094 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003095}
3096
Peter Oberparleiterb99b87f2009-06-17 16:28:03 -07003097/* Call module constructors. */
3098static void do_mod_ctors(struct module *mod)
3099{
3100#ifdef CONFIG_CONSTRUCTORS
3101 unsigned long i;
3102
3103 for (i = 0; i < mod->num_ctors; i++)
3104 mod->ctors[i]();
3105#endif
3106}
3107
Linus Torvalds1da177e2005-04-16 15:20:36 -07003108/* This is where the real work happens */
Heiko Carstens17da2bd2009-01-14 14:14:10 +01003109SYSCALL_DEFINE3(init_module, void __user *, umod,
3110 unsigned long, len, const char __user *, uargs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003111{
3112 struct module *mod;
3113 int ret = 0;
3114
3115 /* Must have permission */
Kees Cook3d433212009-04-02 15:49:29 -07003116 if (!capable(CAP_SYS_MODULE) || modules_disabled)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003117 return -EPERM;
3118
Linus Torvalds1da177e2005-04-16 15:20:36 -07003119 /* Do all the hard work */
3120 mod = load_module(umod, len, uargs);
Rusty Russell75676502010-06-05 11:17:36 -06003121 if (IS_ERR(mod))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003122 return PTR_ERR(mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003123
Alan Sterne041c682006-03-27 01:16:30 -08003124 blocking_notifier_call_chain(&module_notify_list,
3125 MODULE_STATE_COMING, mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003126
Steven Rostedt94462ad2010-11-29 13:15:42 -05003127 /* Set RO and NX regions for core */
3128 set_section_ro_nx(mod->module_core,
3129 mod->core_text_size,
3130 mod->core_ro_size,
3131 mod->core_size);
3132
3133 /* Set RO and NX regions for init */
3134 set_section_ro_nx(mod->module_init,
3135 mod->init_text_size,
3136 mod->init_ro_size,
3137 mod->init_size);
3138
Peter Oberparleiterb99b87f2009-06-17 16:28:03 -07003139 do_mod_ctors(mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003140 /* Start the module */
3141 if (mod->init != NULL)
Arjan van de Ven59f94152008-07-30 12:49:02 -07003142 ret = do_one_initcall(mod->init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003143 if (ret < 0) {
3144 /* Init routine failed: abort. Try to protect us from
3145 buggy refcounters. */
3146 mod->state = MODULE_STATE_GOING;
Paul E. McKenneyfbd568a3e2005-05-01 08:59:04 -07003147 synchronize_sched();
Rusty Russellaf49d922007-10-16 23:26:27 -07003148 module_put(mod);
Peter Oberparleiterdf4b5652008-04-21 14:34:31 +02003149 blocking_notifier_call_chain(&module_notify_list,
3150 MODULE_STATE_GOING, mod);
Rusty Russellaf49d922007-10-16 23:26:27 -07003151 free_module(mod);
Rusty Russell6f139092012-09-28 14:31:03 +09303152 wake_up_all(&module_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003153 return ret;
3154 }
Alexey Dobriyane24e2e62008-03-10 11:43:53 -07003155 if (ret > 0) {
Joe Perchesad361c92009-07-06 13:05:40 -07003156 printk(KERN_WARNING
3157"%s: '%s'->init suspiciously returned %d, it should follow 0/-E convention\n"
3158"%s: loading module anyway...\n",
Alexey Dobriyane24e2e62008-03-10 11:43:53 -07003159 __func__, mod->name, ret,
3160 __func__);
3161 dump_stack();
3162 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003163
Rusty Russell6f139092012-09-28 14:31:03 +09303164 /* Now it's a first class citizen! */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003165 mod->state = MODULE_STATE_LIVE;
Masami Hiramatsu0deddf432009-01-06 14:41:54 -08003166 blocking_notifier_call_chain(&module_notify_list,
3167 MODULE_STATE_LIVE, mod);
Rusty Russell6c5db222008-03-10 11:43:52 -07003168
Linus Torvaldsd6de2c82009-04-10 12:17:41 -07003169 /* We need to finish all async code before the module init sequence is done */
3170 async_synchronize_full();
3171
Rusty Russell6c5db222008-03-10 11:43:52 -07003172 mutex_lock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003173 /* Drop initial reference. */
3174 module_put(mod);
Rusty Russellad6561d2009-06-12 21:47:03 -06003175 trim_init_extable(mod);
Jan Beulich4a496222009-07-06 14:50:42 +01003176#ifdef CONFIG_KALLSYMS
3177 mod->num_symtab = mod->core_num_syms;
3178 mod->symtab = mod->core_symtab;
Jan Beulich554bdfe2009-07-06 14:51:44 +01003179 mod->strtab = mod->core_strtab;
Jan Beulich4a496222009-07-06 14:50:42 +01003180#endif
Jan Glauber01526ed2011-05-19 16:55:26 -06003181 unset_module_init_ro_nx(mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003182 module_free(mod, mod->module_init);
3183 mod->module_init = NULL;
3184 mod->init_size = 0;
Jan Glauber4d103802011-05-19 16:55:25 -06003185 mod->init_ro_size = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003186 mod->init_text_size = 0;
Ashutosh Naik6389a382006-03-23 03:00:46 -08003187 mutex_unlock(&module_mutex);
Rusty Russell6f139092012-09-28 14:31:03 +09303188 wake_up_all(&module_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003189
3190 return 0;
3191}
3192
3193static inline int within(unsigned long addr, void *start, unsigned long size)
3194{
3195 return ((void *)addr >= start && (void *)addr < start + size);
3196}
3197
3198#ifdef CONFIG_KALLSYMS
3199/*
3200 * This ignores the intensely annoying "mapping symbols" found
3201 * in ARM ELF files: $a, $t and $d.
3202 */
3203static inline int is_arm_mapping_symbol(const char *str)
3204{
Daniel Walker22a8bde2007-10-18 03:06:07 -07003205 return str[0] == '$' && strchr("atd", str[1])
Linus Torvalds1da177e2005-04-16 15:20:36 -07003206 && (str[2] == '\0' || str[2] == '.');
3207}
3208
3209static const char *get_ksymbol(struct module *mod,
3210 unsigned long addr,
3211 unsigned long *size,
3212 unsigned long *offset)
3213{
3214 unsigned int i, best = 0;
3215 unsigned long nextval;
3216
3217 /* At worse, next value is at end of module */
Masami Hiramatsua06f6212009-01-06 14:41:49 -08003218 if (within_module_init(addr, mod))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003219 nextval = (unsigned long)mod->module_init+mod->init_text_size;
Daniel Walker22a8bde2007-10-18 03:06:07 -07003220 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003221 nextval = (unsigned long)mod->module_core+mod->core_text_size;
3222
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003223 /* Scan for closest preceding symbol, and next symbol. (ELF
Daniel Walker22a8bde2007-10-18 03:06:07 -07003224 starts real symbols at 1). */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003225 for (i = 1; i < mod->num_symtab; i++) {
3226 if (mod->symtab[i].st_shndx == SHN_UNDEF)
3227 continue;
3228
3229 /* We ignore unnamed symbols: they're uninformative
3230 * and inserted at a whim. */
3231 if (mod->symtab[i].st_value <= addr
3232 && mod->symtab[i].st_value > mod->symtab[best].st_value
3233 && *(mod->strtab + mod->symtab[i].st_name) != '\0'
3234 && !is_arm_mapping_symbol(mod->strtab + mod->symtab[i].st_name))
3235 best = i;
3236 if (mod->symtab[i].st_value > addr
3237 && mod->symtab[i].st_value < nextval
3238 && *(mod->strtab + mod->symtab[i].st_name) != '\0'
3239 && !is_arm_mapping_symbol(mod->strtab + mod->symtab[i].st_name))
3240 nextval = mod->symtab[i].st_value;
3241 }
3242
3243 if (!best)
3244 return NULL;
3245
Alexey Dobriyanffb45122007-05-08 00:28:41 -07003246 if (size)
3247 *size = nextval - mod->symtab[best].st_value;
3248 if (offset)
3249 *offset = addr - mod->symtab[best].st_value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003250 return mod->strtab + mod->symtab[best].st_name;
3251}
3252
Rusty Russell6dd06c92008-01-29 17:13:22 -05003253/* For kallsyms to ask for address resolution. NULL means not found. Careful
3254 * not to lock to avoid deadlock on oopses, simply disable preemption. */
Andrew Morton92dfc9d2008-02-08 04:18:43 -08003255const char *module_address_lookup(unsigned long addr,
Rusty Russell6dd06c92008-01-29 17:13:22 -05003256 unsigned long *size,
3257 unsigned long *offset,
3258 char **modname,
3259 char *namebuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003260{
3261 struct module *mod;
Rusty Russellcb2a5202008-01-14 00:55:03 -08003262 const char *ret = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003263
Rusty Russellcb2a5202008-01-14 00:55:03 -08003264 preempt_disable();
Andi Kleend72b3752008-08-30 10:09:00 +02003265 list_for_each_entry_rcu(mod, &modules, list) {
Masami Hiramatsua06f6212009-01-06 14:41:49 -08003266 if (within_module_init(addr, mod) ||
3267 within_module_core(addr, mod)) {
Franck Bui-Huuffc50892006-10-03 01:13:48 -07003268 if (modname)
3269 *modname = mod->name;
Rusty Russellcb2a5202008-01-14 00:55:03 -08003270 ret = get_ksymbol(mod, addr, size, offset);
3271 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003272 }
3273 }
Rusty Russell6dd06c92008-01-29 17:13:22 -05003274 /* Make a copy in here where it's safe */
3275 if (ret) {
3276 strncpy(namebuf, ret, KSYM_NAME_LEN - 1);
3277 ret = namebuf;
3278 }
Rusty Russellcb2a5202008-01-14 00:55:03 -08003279 preempt_enable();
Andrew Morton92dfc9d2008-02-08 04:18:43 -08003280 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003281}
3282
Alexey Dobriyan9d65cb42007-05-08 00:28:43 -07003283int lookup_module_symbol_name(unsigned long addr, char *symname)
3284{
3285 struct module *mod;
3286
Rusty Russellcb2a5202008-01-14 00:55:03 -08003287 preempt_disable();
Andi Kleend72b3752008-08-30 10:09:00 +02003288 list_for_each_entry_rcu(mod, &modules, list) {
Masami Hiramatsua06f6212009-01-06 14:41:49 -08003289 if (within_module_init(addr, mod) ||
3290 within_module_core(addr, mod)) {
Alexey Dobriyan9d65cb42007-05-08 00:28:43 -07003291 const char *sym;
3292
3293 sym = get_ksymbol(mod, addr, NULL, NULL);
3294 if (!sym)
3295 goto out;
Tejun Heo9281ace2007-07-17 04:03:51 -07003296 strlcpy(symname, sym, KSYM_NAME_LEN);
Rusty Russellcb2a5202008-01-14 00:55:03 -08003297 preempt_enable();
Alexey Dobriyan9d65cb42007-05-08 00:28:43 -07003298 return 0;
3299 }
3300 }
3301out:
Rusty Russellcb2a5202008-01-14 00:55:03 -08003302 preempt_enable();
Alexey Dobriyan9d65cb42007-05-08 00:28:43 -07003303 return -ERANGE;
3304}
3305
Alexey Dobriyana5c43da2007-05-08 00:28:47 -07003306int lookup_module_symbol_attrs(unsigned long addr, unsigned long *size,
3307 unsigned long *offset, char *modname, char *name)
3308{
3309 struct module *mod;
3310
Rusty Russellcb2a5202008-01-14 00:55:03 -08003311 preempt_disable();
Andi Kleend72b3752008-08-30 10:09:00 +02003312 list_for_each_entry_rcu(mod, &modules, list) {
Masami Hiramatsua06f6212009-01-06 14:41:49 -08003313 if (within_module_init(addr, mod) ||
3314 within_module_core(addr, mod)) {
Alexey Dobriyana5c43da2007-05-08 00:28:47 -07003315 const char *sym;
3316
3317 sym = get_ksymbol(mod, addr, size, offset);
3318 if (!sym)
3319 goto out;
3320 if (modname)
Tejun Heo9281ace2007-07-17 04:03:51 -07003321 strlcpy(modname, mod->name, MODULE_NAME_LEN);
Alexey Dobriyana5c43da2007-05-08 00:28:47 -07003322 if (name)
Tejun Heo9281ace2007-07-17 04:03:51 -07003323 strlcpy(name, sym, KSYM_NAME_LEN);
Rusty Russellcb2a5202008-01-14 00:55:03 -08003324 preempt_enable();
Alexey Dobriyana5c43da2007-05-08 00:28:47 -07003325 return 0;
3326 }
3327 }
3328out:
Rusty Russellcb2a5202008-01-14 00:55:03 -08003329 preempt_enable();
Alexey Dobriyana5c43da2007-05-08 00:28:47 -07003330 return -ERANGE;
3331}
3332
Alexey Dobriyanea078902007-05-08 00:28:39 -07003333int module_get_kallsym(unsigned int symnum, unsigned long *value, char *type,
3334 char *name, char *module_name, int *exported)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003335{
3336 struct module *mod;
3337
Rusty Russellcb2a5202008-01-14 00:55:03 -08003338 preempt_disable();
Andi Kleend72b3752008-08-30 10:09:00 +02003339 list_for_each_entry_rcu(mod, &modules, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003340 if (symnum < mod->num_symtab) {
3341 *value = mod->symtab[symnum].st_value;
3342 *type = mod->symtab[symnum].st_info;
Andreas Gruenbacher098c5ee2006-07-14 00:24:04 -07003343 strlcpy(name, mod->strtab + mod->symtab[symnum].st_name,
Tejun Heo9281ace2007-07-17 04:03:51 -07003344 KSYM_NAME_LEN);
3345 strlcpy(module_name, mod->name, MODULE_NAME_LEN);
Tim Abbottca4787b2009-01-05 08:40:10 -06003346 *exported = is_exported(name, *value, mod);
Rusty Russellcb2a5202008-01-14 00:55:03 -08003347 preempt_enable();
Alexey Dobriyanea078902007-05-08 00:28:39 -07003348 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003349 }
3350 symnum -= mod->num_symtab;
3351 }
Rusty Russellcb2a5202008-01-14 00:55:03 -08003352 preempt_enable();
Alexey Dobriyanea078902007-05-08 00:28:39 -07003353 return -ERANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003354}
3355
3356static unsigned long mod_find_symname(struct module *mod, const char *name)
3357{
3358 unsigned int i;
3359
3360 for (i = 0; i < mod->num_symtab; i++)
Keith Owens54e8ce42006-02-03 03:03:53 -08003361 if (strcmp(name, mod->strtab+mod->symtab[i].st_name) == 0 &&
3362 mod->symtab[i].st_info != 'U')
Linus Torvalds1da177e2005-04-16 15:20:36 -07003363 return mod->symtab[i].st_value;
3364 return 0;
3365}
3366
3367/* Look for this name: can be of form module:name. */
3368unsigned long module_kallsyms_lookup_name(const char *name)
3369{
3370 struct module *mod;
3371 char *colon;
3372 unsigned long ret = 0;
3373
3374 /* Don't lock: we're in enough trouble already. */
Rusty Russellcb2a5202008-01-14 00:55:03 -08003375 preempt_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003376 if ((colon = strchr(name, ':')) != NULL) {
3377 *colon = '\0';
3378 if ((mod = find_module(name)) != NULL)
3379 ret = mod_find_symname(mod, colon+1);
3380 *colon = ':';
3381 } else {
Andi Kleend72b3752008-08-30 10:09:00 +02003382 list_for_each_entry_rcu(mod, &modules, list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003383 if ((ret = mod_find_symname(mod, name)) != 0)
3384 break;
3385 }
Rusty Russellcb2a5202008-01-14 00:55:03 -08003386 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003387 return ret;
3388}
Anders Kaseorg75a66612008-12-05 19:03:58 -05003389
3390int module_kallsyms_on_each_symbol(int (*fn)(void *, const char *,
3391 struct module *, unsigned long),
3392 void *data)
3393{
3394 struct module *mod;
3395 unsigned int i;
3396 int ret;
3397
3398 list_for_each_entry(mod, &modules, list) {
3399 for (i = 0; i < mod->num_symtab; i++) {
3400 ret = fn(data, mod->strtab + mod->symtab[i].st_name,
3401 mod, mod->symtab[i].st_value);
3402 if (ret != 0)
3403 return ret;
3404 }
3405 }
3406 return 0;
3407}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003408#endif /* CONFIG_KALLSYMS */
3409
Arjan van de Ven21aa9282008-01-25 21:08:33 +01003410static char *module_flags(struct module *mod, char *buf)
Florin Malitafa3ba2e82006-10-11 01:21:48 -07003411{
3412 int bx = 0;
3413
Arjan van de Ven21aa9282008-01-25 21:08:33 +01003414 if (mod->taints ||
3415 mod->state == MODULE_STATE_GOING ||
3416 mod->state == MODULE_STATE_COMING) {
Florin Malitafa3ba2e82006-10-11 01:21:48 -07003417 buf[bx++] = '(';
Kay Sieverscca3e702012-01-13 09:32:15 +10303418 bx += module_flags_taint(mod, buf + bx);
Arjan van de Ven21aa9282008-01-25 21:08:33 +01003419 /* Show a - for module-is-being-unloaded */
3420 if (mod->state == MODULE_STATE_GOING)
3421 buf[bx++] = '-';
3422 /* Show a + for module-is-being-loaded */
3423 if (mod->state == MODULE_STATE_COMING)
3424 buf[bx++] = '+';
Florin Malitafa3ba2e82006-10-11 01:21:48 -07003425 buf[bx++] = ')';
3426 }
3427 buf[bx] = '\0';
3428
3429 return buf;
3430}
3431
Alexey Dobriyan3b5d5c62008-10-06 13:19:27 +04003432#ifdef CONFIG_PROC_FS
3433/* Called by the /proc file system to return a list of modules. */
3434static void *m_start(struct seq_file *m, loff_t *pos)
3435{
3436 mutex_lock(&module_mutex);
3437 return seq_list_start(&modules, *pos);
3438}
3439
3440static void *m_next(struct seq_file *m, void *p, loff_t *pos)
3441{
3442 return seq_list_next(p, &modules, pos);
3443}
3444
3445static void m_stop(struct seq_file *m, void *p)
3446{
3447 mutex_unlock(&module_mutex);
3448}
3449
Linus Torvalds1da177e2005-04-16 15:20:36 -07003450static int m_show(struct seq_file *m, void *p)
3451{
3452 struct module *mod = list_entry(p, struct module, list);
Florin Malitafa3ba2e82006-10-11 01:21:48 -07003453 char buf[8];
3454
Denys Vlasenko2f0f2a32008-07-22 19:24:27 -05003455 seq_printf(m, "%s %u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003456 mod->name, mod->init_size + mod->core_size);
3457 print_unload_info(m, mod);
3458
3459 /* Informative for users. */
3460 seq_printf(m, " %s",
3461 mod->state == MODULE_STATE_GOING ? "Unloading":
3462 mod->state == MODULE_STATE_COMING ? "Loading":
3463 "Live");
3464 /* Used by oprofile and other similar tools. */
Kees Cook9f36e2c2011-03-22 16:34:22 -07003465 seq_printf(m, " 0x%pK", mod->module_core);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003466
Florin Malitafa3ba2e82006-10-11 01:21:48 -07003467 /* Taints info */
3468 if (mod->taints)
Arjan van de Ven21aa9282008-01-25 21:08:33 +01003469 seq_printf(m, " %s", module_flags(mod, buf));
Florin Malitafa3ba2e82006-10-11 01:21:48 -07003470
Linus Torvalds1da177e2005-04-16 15:20:36 -07003471 seq_printf(m, "\n");
3472 return 0;
3473}
3474
3475/* Format: modulename size refcount deps address
3476
3477 Where refcount is a number or -, and deps is a comma-separated list
3478 of depends or -.
3479*/
Alexey Dobriyan3b5d5c62008-10-06 13:19:27 +04003480static const struct seq_operations modules_op = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003481 .start = m_start,
3482 .next = m_next,
3483 .stop = m_stop,
3484 .show = m_show
3485};
3486
Alexey Dobriyan3b5d5c62008-10-06 13:19:27 +04003487static int modules_open(struct inode *inode, struct file *file)
3488{
3489 return seq_open(file, &modules_op);
3490}
3491
3492static const struct file_operations proc_modules_operations = {
3493 .open = modules_open,
3494 .read = seq_read,
3495 .llseek = seq_lseek,
3496 .release = seq_release,
3497};
3498
3499static int __init proc_modules_init(void)
3500{
3501 proc_create("modules", 0, NULL, &proc_modules_operations);
3502 return 0;
3503}
3504module_init(proc_modules_init);
3505#endif
3506
Linus Torvalds1da177e2005-04-16 15:20:36 -07003507/* Given an address, look for it in the module exception tables. */
3508const struct exception_table_entry *search_module_extables(unsigned long addr)
3509{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003510 const struct exception_table_entry *e = NULL;
3511 struct module *mod;
3512
Rusty Russell24da1cb2007-07-15 23:41:46 -07003513 preempt_disable();
Andi Kleend72b3752008-08-30 10:09:00 +02003514 list_for_each_entry_rcu(mod, &modules, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003515 if (mod->num_exentries == 0)
3516 continue;
Daniel Walker22a8bde2007-10-18 03:06:07 -07003517
Linus Torvalds1da177e2005-04-16 15:20:36 -07003518 e = search_extable(mod->extable,
3519 mod->extable + mod->num_exentries - 1,
3520 addr);
3521 if (e)
3522 break;
3523 }
Rusty Russell24da1cb2007-07-15 23:41:46 -07003524 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003525
3526 /* Now, if we found one, we are running inside it now, hence
Daniel Walker22a8bde2007-10-18 03:06:07 -07003527 we cannot unload the module, hence no refcnt needed. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003528 return e;
3529}
3530
Ingo Molnar4d435f92006-07-03 00:24:24 -07003531/*
Rusty Russelle6104992009-03-31 13:05:31 -06003532 * is_module_address - is this address inside a module?
3533 * @addr: the address to check.
3534 *
3535 * See is_module_text_address() if you simply want to see if the address
3536 * is code (not data).
Ingo Molnar4d435f92006-07-03 00:24:24 -07003537 */
Rusty Russelle6104992009-03-31 13:05:31 -06003538bool is_module_address(unsigned long addr)
Ingo Molnar4d435f92006-07-03 00:24:24 -07003539{
Rusty Russelle6104992009-03-31 13:05:31 -06003540 bool ret;
Ingo Molnar4d435f92006-07-03 00:24:24 -07003541
Rusty Russell24da1cb2007-07-15 23:41:46 -07003542 preempt_disable();
Rusty Russelle6104992009-03-31 13:05:31 -06003543 ret = __module_address(addr) != NULL;
Rusty Russell24da1cb2007-07-15 23:41:46 -07003544 preempt_enable();
Ingo Molnar4d435f92006-07-03 00:24:24 -07003545
Rusty Russelle6104992009-03-31 13:05:31 -06003546 return ret;
Ingo Molnar4d435f92006-07-03 00:24:24 -07003547}
3548
Rusty Russelle6104992009-03-31 13:05:31 -06003549/*
3550 * __module_address - get the module which contains an address.
3551 * @addr: the address.
3552 *
3553 * Must be called with preempt disabled or module mutex held so that
3554 * module doesn't get freed during this.
3555 */
Linus Torvalds714f83d2009-04-05 11:04:19 -07003556struct module *__module_address(unsigned long addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003557{
3558 struct module *mod;
3559
Rusty Russell3a642e92008-07-22 19:24:28 -05003560 if (addr < module_addr_min || addr > module_addr_max)
3561 return NULL;
3562
Andi Kleend72b3752008-08-30 10:09:00 +02003563 list_for_each_entry_rcu(mod, &modules, list)
Rusty Russelle6104992009-03-31 13:05:31 -06003564 if (within_module_core(addr, mod)
3565 || within_module_init(addr, mod))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003566 return mod;
3567 return NULL;
3568}
Tim Abbottc6b37802008-12-05 19:03:59 -05003569EXPORT_SYMBOL_GPL(__module_address);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003570
Rusty Russelle6104992009-03-31 13:05:31 -06003571/*
3572 * is_module_text_address - is this address inside module code?
3573 * @addr: the address to check.
3574 *
3575 * See is_module_address() if you simply want to see if the address is
3576 * anywhere in a module. See kernel_text_address() for testing if an
3577 * address corresponds to kernel or module code.
3578 */
3579bool is_module_text_address(unsigned long addr)
3580{
3581 bool ret;
3582
3583 preempt_disable();
3584 ret = __module_text_address(addr) != NULL;
3585 preempt_enable();
3586
3587 return ret;
3588}
3589
3590/*
3591 * __module_text_address - get the module whose code contains an address.
3592 * @addr: the address.
3593 *
3594 * Must be called with preempt disabled or module mutex held so that
3595 * module doesn't get freed during this.
3596 */
3597struct module *__module_text_address(unsigned long addr)
3598{
3599 struct module *mod = __module_address(addr);
3600 if (mod) {
3601 /* Make sure it's within the text section. */
3602 if (!within(addr, mod->module_init, mod->init_text_size)
3603 && !within(addr, mod->module_core, mod->core_text_size))
3604 mod = NULL;
3605 }
3606 return mod;
3607}
Tim Abbottc6b37802008-12-05 19:03:59 -05003608EXPORT_SYMBOL_GPL(__module_text_address);
Rusty Russelle6104992009-03-31 13:05:31 -06003609
Linus Torvalds1da177e2005-04-16 15:20:36 -07003610/* Don't grab lock, we're oopsing. */
3611void print_modules(void)
3612{
3613 struct module *mod;
Randy Dunlap2bc2d612006-10-02 02:17:02 -07003614 char buf[8];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003615
Linus Torvaldsb2311252009-06-16 11:07:14 -07003616 printk(KERN_DEFAULT "Modules linked in:");
Andi Kleend72b3752008-08-30 10:09:00 +02003617 /* Most callers should already have preempt disabled, but make sure */
3618 preempt_disable();
3619 list_for_each_entry_rcu(mod, &modules, list)
Arjan van de Ven21aa9282008-01-25 21:08:33 +01003620 printk(" %s%s", mod->name, module_flags(mod, buf));
Andi Kleend72b3752008-08-30 10:09:00 +02003621 preempt_enable();
Arjan van de Vene14af7e2008-01-25 21:08:33 +01003622 if (last_unloaded_module[0])
3623 printk(" [last unloaded: %s]", last_unloaded_module);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003624 printk("\n");
3625}
3626
Linus Torvalds1da177e2005-04-16 15:20:36 -07003627#ifdef CONFIG_MODVERSIONS
Rusty Russell8c8ef422009-03-31 13:05:34 -06003628/* Generate the signature for all relevant module structures here.
3629 * If these change, we don't want to try to parse the module. */
3630void module_layout(struct module *mod,
3631 struct modversion_info *ver,
3632 struct kernel_param *kp,
3633 struct kernel_symbol *ks,
Mathieu Desnoyers65498642011-01-26 17:26:22 -05003634 struct tracepoint * const *tp)
Rusty Russell8c8ef422009-03-31 13:05:34 -06003635{
3636}
3637EXPORT_SYMBOL(module_layout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003638#endif