blob: 6d2c4e4ca1f5b4eed87643c7df763c9ea85fc902 [file] [log] [blame]
Arjan van de Venf71d20e2006-06-28 04:26:45 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 Copyright (C) 2002 Richard Henderson
Rusty Russell51f3d0f2010-08-05 12:59:13 -06003 Copyright (C) 2001 Rusty Russell, 2002, 2010 Rusty Russell IBM.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18*/
Paul Gortmaker9984de12011-05-23 14:51:41 -040019#include <linux/export.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <linux/moduleloader.h>
Steven Rostedt6d723732009-04-10 14:53:50 -040021#include <linux/ftrace_event.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/init.h>
Alexey Dobriyanae84e322007-05-08 00:28:38 -070023#include <linux/kallsyms.h>
Kees Cook34e11692012-10-16 07:31:07 +103024#include <linux/file.h>
Alexey Dobriyan3b5d5c62008-10-06 13:19:27 +040025#include <linux/fs.h>
Roland McGrath6d760132007-10-16 23:26:40 -070026#include <linux/sysfs.h>
Randy Dunlap9f158332005-09-13 01:25:16 -070027#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <linux/slab.h>
29#include <linux/vmalloc.h>
30#include <linux/elf.h>
Alexey Dobriyan3b5d5c62008-10-06 13:19:27 +040031#include <linux/proc_fs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include <linux/seq_file.h>
33#include <linux/syscalls.h>
34#include <linux/fcntl.h>
35#include <linux/rcupdate.h>
Randy.Dunlapc59ede72006-01-11 12:17:46 -080036#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#include <linux/cpu.h>
38#include <linux/moduleparam.h>
39#include <linux/errno.h>
40#include <linux/err.h>
41#include <linux/vermagic.h>
42#include <linux/notifier.h>
Al Virof6a57032006-10-18 01:47:25 -040043#include <linux/sched.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070044#include <linux/stop_machine.h>
45#include <linux/device.h>
Matt Domschc988d2b2005-06-23 22:05:15 -070046#include <linux/string.h>
Arjan van de Ven97d1f152006-03-23 03:00:24 -080047#include <linux/mutex.h>
Andi Kleend72b3752008-08-30 10:09:00 +020048#include <linux/rculist.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070049#include <asm/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070050#include <asm/cacheflush.h>
Bernd Schmidteb8cdec2009-09-21 17:03:57 -070051#include <asm/mmu_context.h>
Sam Ravnborgb817f6f2006-06-09 21:53:55 +020052#include <linux/license.h>
Christoph Lameter6d762392008-02-08 04:18:42 -080053#include <asm/sections.h>
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -040054#include <linux/tracepoint.h>
Steven Rostedt90d595f2008-08-14 15:45:09 -040055#include <linux/ftrace.h>
Arjan van de Ven22a9d642009-01-07 08:45:46 -080056#include <linux/async.h>
Tejun Heofbf59bc2009-02-20 16:29:08 +090057#include <linux/percpu.h>
Catalin Marinas4f2294b2009-06-11 13:23:20 +010058#include <linux/kmemleak.h>
Jason Baronbf5438fc2010-09-17 11:09:00 -040059#include <linux/jump_label.h>
matthieu castet84e1c6b2010-11-16 22:35:16 +010060#include <linux/pfn.h>
Alessio Igor Bogani403ed272011-04-20 11:10:52 +020061#include <linux/bsearch.h>
David Howells1d0059f2012-09-26 10:09:50 +010062#include <linux/fips.h>
Rusty Russell106a4ee2012-09-26 10:09:40 +010063#include "module-internal.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
Li Zefan7ead8b82009-08-17 16:56:28 +080065#define CREATE_TRACE_POINTS
66#include <trace/events/module.h>
67
Linus Torvalds1da177e2005-04-16 15:20:36 -070068#ifndef ARCH_SHF_SMALL
69#define ARCH_SHF_SMALL 0
70#endif
71
matthieu castet84e1c6b2010-11-16 22:35:16 +010072/*
73 * Modules' sections will be aligned on page boundaries
74 * to ensure complete separation of code and data, but
75 * only when CONFIG_DEBUG_SET_MODULE_RONX=y
76 */
77#ifdef CONFIG_DEBUG_SET_MODULE_RONX
78# define debug_align(X) ALIGN(X, PAGE_SIZE)
79#else
80# define debug_align(X) (X)
81#endif
82
83/*
84 * Given BASE and SIZE this macro calculates the number of pages the
85 * memory regions occupies
86 */
87#define MOD_NUMBER_OF_PAGES(BASE, SIZE) (((SIZE) > 0) ? \
88 (PFN_DOWN((unsigned long)(BASE) + (SIZE) - 1) - \
89 PFN_DOWN((unsigned long)BASE) + 1) \
90 : (0UL))
91
Linus Torvalds1da177e2005-04-16 15:20:36 -070092/* If this is set, the section belongs in the init part of the module */
93#define INIT_OFFSET_MASK (1UL << (BITS_PER_LONG-1))
94
Rusty Russell75676502010-06-05 11:17:36 -060095/*
96 * Mutex protects:
97 * 1) List of modules (also safely readable with preempt_disable),
98 * 2) module_use links,
99 * 3) module_addr_min/module_addr_max.
Andi Kleend72b3752008-08-30 10:09:00 +0200100 * (delete uses stop_machine/add uses RCU list operations). */
Tim Abbottc6b37802008-12-05 19:03:59 -0500101DEFINE_MUTEX(module_mutex);
102EXPORT_SYMBOL_GPL(module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103static LIST_HEAD(modules);
Jason Wessel67fc4e02010-05-20 21:04:21 -0500104#ifdef CONFIG_KGDB_KDB
105struct list_head *kdb_modules = &modules; /* kdb needs the list of modules */
106#endif /* CONFIG_KGDB_KDB */
107
Rusty Russell106a4ee2012-09-26 10:09:40 +0100108#ifdef CONFIG_MODULE_SIG
109#ifdef CONFIG_MODULE_SIG_FORCE
110static bool sig_enforce = true;
111#else
112static bool sig_enforce = false;
113
114static int param_set_bool_enable_only(const char *val,
115 const struct kernel_param *kp)
116{
117 int err;
118 bool test;
119 struct kernel_param dummy_kp = *kp;
120
121 dummy_kp.arg = &test;
122
123 err = param_set_bool(val, &dummy_kp);
124 if (err)
125 return err;
126
127 /* Don't let them unset it once it's set! */
128 if (!test && sig_enforce)
129 return -EROFS;
130
131 if (test)
132 sig_enforce = true;
133 return 0;
134}
135
136static const struct kernel_param_ops param_ops_bool_enable_only = {
137 .set = param_set_bool_enable_only,
138 .get = param_get_bool,
139};
140#define param_check_bool_enable_only param_check_bool
141
142module_param(sig_enforce, bool_enable_only, 0644);
143#endif /* !CONFIG_MODULE_SIG_FORCE */
144#endif /* CONFIG_MODULE_SIG */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145
Stephen Rothwell19e45292009-04-14 17:27:18 +1000146/* Block module loading/unloading? */
147int modules_disabled = 0;
Dave Young02608be2012-02-01 10:33:14 +0800148core_param(nomodule, modules_disabled, bint, 0);
Stephen Rothwell19e45292009-04-14 17:27:18 +1000149
Rusty Russellc9a3ba52008-01-29 17:13:18 -0500150/* Waiting for a module to finish initializing? */
151static DECLARE_WAIT_QUEUE_HEAD(module_wq);
152
Alan Sterne041c682006-03-27 01:16:30 -0800153static BLOCKING_NOTIFIER_HEAD(module_notify_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154
Rusty Russell75676502010-06-05 11:17:36 -0600155/* Bounds of module allocation, for speeding __module_address.
156 * Protected by module_mutex. */
Rusty Russell3a642e92008-07-22 19:24:28 -0500157static unsigned long module_addr_min = -1UL, module_addr_max = 0;
158
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159int register_module_notifier(struct notifier_block * nb)
160{
Alan Sterne041c682006-03-27 01:16:30 -0800161 return blocking_notifier_chain_register(&module_notify_list, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162}
163EXPORT_SYMBOL(register_module_notifier);
164
165int unregister_module_notifier(struct notifier_block * nb)
166{
Alan Sterne041c682006-03-27 01:16:30 -0800167 return blocking_notifier_chain_unregister(&module_notify_list, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168}
169EXPORT_SYMBOL(unregister_module_notifier);
170
Rusty Russelleded41c2010-08-05 12:59:07 -0600171struct load_info {
172 Elf_Ehdr *hdr;
173 unsigned long len;
174 Elf_Shdr *sechdrs;
Rusty Russell6526c532010-08-05 12:59:10 -0600175 char *secstrings, *strtab;
Rusty Russelld9131882010-08-05 12:59:08 -0600176 unsigned long symoffs, stroffs;
Rusty Russell811d66a2010-08-05 12:59:12 -0600177 struct _ddebug *debug;
178 unsigned int num_debug;
Rusty Russell106a4ee2012-09-26 10:09:40 +0100179 bool sig_ok;
Rusty Russelleded41c2010-08-05 12:59:07 -0600180 struct {
181 unsigned int sym, str, mod, vers, info, pcpu;
182 } index;
183};
184
Matti Linnanvuori9a4b9702007-11-08 08:37:38 -0800185/* We require a truly strong try_module_get(): 0 means failure due to
186 ongoing or failed initialization etc. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187static inline int strong_try_module_get(struct module *mod)
188{
189 if (mod && mod->state == MODULE_STATE_COMING)
Rusty Russellc9a3ba52008-01-29 17:13:18 -0500190 return -EBUSY;
191 if (try_module_get(mod))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 return 0;
Rusty Russellc9a3ba52008-01-29 17:13:18 -0500193 else
194 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195}
196
Florin Malitafa3ba2e82006-10-11 01:21:48 -0700197static inline void add_taint_module(struct module *mod, unsigned flag)
198{
199 add_taint(flag);
Andi Kleen25ddbb12008-10-15 22:01:41 -0700200 mod->taints |= (1U << flag);
Florin Malitafa3ba2e82006-10-11 01:21:48 -0700201}
202
Robert P. J. Day02a3e592007-05-09 07:26:28 +0200203/*
204 * A thread that wants to hold a reference to a module only while it
205 * is running can call this to safely exit. nfsd and lockd use this.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 */
207void __module_put_and_exit(struct module *mod, long code)
208{
209 module_put(mod);
210 do_exit(code);
211}
212EXPORT_SYMBOL(__module_put_and_exit);
Daniel Walker22a8bde2007-10-18 03:06:07 -0700213
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214/* Find a module section: 0 means not found. */
Rusty Russell49668682010-08-05 12:59:10 -0600215static unsigned int find_sec(const struct load_info *info, const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216{
217 unsigned int i;
218
Rusty Russell49668682010-08-05 12:59:10 -0600219 for (i = 1; i < info->hdr->e_shnum; i++) {
220 Elf_Shdr *shdr = &info->sechdrs[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 /* Alloc bit cleared means "ignore it." */
Rusty Russell49668682010-08-05 12:59:10 -0600222 if ((shdr->sh_flags & SHF_ALLOC)
223 && strcmp(info->secstrings + shdr->sh_name, name) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 return i;
Rusty Russell49668682010-08-05 12:59:10 -0600225 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 return 0;
227}
228
Rusty Russell5e458cc2008-10-22 10:00:13 -0500229/* Find a module section, or NULL. */
Rusty Russell49668682010-08-05 12:59:10 -0600230static void *section_addr(const struct load_info *info, const char *name)
Rusty Russell5e458cc2008-10-22 10:00:13 -0500231{
232 /* Section 0 has sh_addr 0. */
Rusty Russell49668682010-08-05 12:59:10 -0600233 return (void *)info->sechdrs[find_sec(info, name)].sh_addr;
Rusty Russell5e458cc2008-10-22 10:00:13 -0500234}
235
236/* Find a module section, or NULL. Fill in number of "objects" in section. */
Rusty Russell49668682010-08-05 12:59:10 -0600237static void *section_objs(const struct load_info *info,
Rusty Russell5e458cc2008-10-22 10:00:13 -0500238 const char *name,
239 size_t object_size,
240 unsigned int *num)
241{
Rusty Russell49668682010-08-05 12:59:10 -0600242 unsigned int sec = find_sec(info, name);
Rusty Russell5e458cc2008-10-22 10:00:13 -0500243
244 /* Section 0 has sh_addr 0 and sh_size 0. */
Rusty Russell49668682010-08-05 12:59:10 -0600245 *num = info->sechdrs[sec].sh_size / object_size;
246 return (void *)info->sechdrs[sec].sh_addr;
Rusty Russell5e458cc2008-10-22 10:00:13 -0500247}
248
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249/* Provided by the linker */
250extern const struct kernel_symbol __start___ksymtab[];
251extern const struct kernel_symbol __stop___ksymtab[];
252extern const struct kernel_symbol __start___ksymtab_gpl[];
253extern const struct kernel_symbol __stop___ksymtab_gpl[];
Greg Kroah-Hartman9f28bb72006-03-20 13:17:13 -0800254extern const struct kernel_symbol __start___ksymtab_gpl_future[];
255extern const struct kernel_symbol __stop___ksymtab_gpl_future[];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256extern const unsigned long __start___kcrctab[];
257extern const unsigned long __start___kcrctab_gpl[];
Greg Kroah-Hartman9f28bb72006-03-20 13:17:13 -0800258extern const unsigned long __start___kcrctab_gpl_future[];
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500259#ifdef CONFIG_UNUSED_SYMBOLS
260extern const struct kernel_symbol __start___ksymtab_unused[];
261extern const struct kernel_symbol __stop___ksymtab_unused[];
262extern const struct kernel_symbol __start___ksymtab_unused_gpl[];
263extern const struct kernel_symbol __stop___ksymtab_unused_gpl[];
Arjan van de Venf71d20e2006-06-28 04:26:45 -0700264extern const unsigned long __start___kcrctab_unused[];
265extern const unsigned long __start___kcrctab_unused_gpl[];
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500266#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267
268#ifndef CONFIG_MODVERSIONS
269#define symversion(base, idx) NULL
270#else
Andrew Mortonf83ca9f2006-03-28 01:56:20 -0800271#define symversion(base, idx) ((base != NULL) ? ((base) + (idx)) : NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272#endif
273
Rusty Russelldafd0942008-07-22 19:24:25 -0500274static bool each_symbol_in_section(const struct symsearch *arr,
275 unsigned int arrsize,
276 struct module *owner,
277 bool (*fn)(const struct symsearch *syms,
278 struct module *owner,
Rusty Russellde4d8d52011-04-19 21:49:58 +0200279 void *data),
Rusty Russelldafd0942008-07-22 19:24:25 -0500280 void *data)
Sam Ravnborg3fd68052006-02-08 21:16:45 +0100281{
Rusty Russellde4d8d52011-04-19 21:49:58 +0200282 unsigned int j;
Rusty Russelldafd0942008-07-22 19:24:25 -0500283
284 for (j = 0; j < arrsize; j++) {
Rusty Russellde4d8d52011-04-19 21:49:58 +0200285 if (fn(&arr[j], owner, data))
286 return true;
Rusty Russelldafd0942008-07-22 19:24:25 -0500287 }
288
289 return false;
Sam Ravnborg3fd68052006-02-08 21:16:45 +0100290}
291
Rusty Russelldafd0942008-07-22 19:24:25 -0500292/* Returns true as soon as fn returns true, otherwise false. */
Rusty Russellde4d8d52011-04-19 21:49:58 +0200293bool each_symbol_section(bool (*fn)(const struct symsearch *arr,
294 struct module *owner,
295 void *data),
296 void *data)
Arjan van de Venf71d20e2006-06-28 04:26:45 -0700297{
Rusty Russelldafd0942008-07-22 19:24:25 -0500298 struct module *mod;
Linus Torvalds44032e62010-08-05 12:59:05 -0600299 static const struct symsearch arr[] = {
Rusty Russelldafd0942008-07-22 19:24:25 -0500300 { __start___ksymtab, __stop___ksymtab, __start___kcrctab,
301 NOT_GPL_ONLY, false },
302 { __start___ksymtab_gpl, __stop___ksymtab_gpl,
303 __start___kcrctab_gpl,
304 GPL_ONLY, false },
305 { __start___ksymtab_gpl_future, __stop___ksymtab_gpl_future,
306 __start___kcrctab_gpl_future,
307 WILL_BE_GPL_ONLY, false },
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500308#ifdef CONFIG_UNUSED_SYMBOLS
Rusty Russelldafd0942008-07-22 19:24:25 -0500309 { __start___ksymtab_unused, __stop___ksymtab_unused,
310 __start___kcrctab_unused,
311 NOT_GPL_ONLY, true },
312 { __start___ksymtab_unused_gpl, __stop___ksymtab_unused_gpl,
313 __start___kcrctab_unused_gpl,
314 GPL_ONLY, true },
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500315#endif
Rusty Russelldafd0942008-07-22 19:24:25 -0500316 };
317
318 if (each_symbol_in_section(arr, ARRAY_SIZE(arr), NULL, fn, data))
319 return true;
320
Andi Kleend72b3752008-08-30 10:09:00 +0200321 list_for_each_entry_rcu(mod, &modules, list) {
Rusty Russelldafd0942008-07-22 19:24:25 -0500322 struct symsearch arr[] = {
323 { mod->syms, mod->syms + mod->num_syms, mod->crcs,
324 NOT_GPL_ONLY, false },
325 { mod->gpl_syms, mod->gpl_syms + mod->num_gpl_syms,
326 mod->gpl_crcs,
327 GPL_ONLY, false },
328 { mod->gpl_future_syms,
329 mod->gpl_future_syms + mod->num_gpl_future_syms,
330 mod->gpl_future_crcs,
331 WILL_BE_GPL_ONLY, false },
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500332#ifdef CONFIG_UNUSED_SYMBOLS
Rusty Russelldafd0942008-07-22 19:24:25 -0500333 { mod->unused_syms,
334 mod->unused_syms + mod->num_unused_syms,
335 mod->unused_crcs,
336 NOT_GPL_ONLY, true },
337 { mod->unused_gpl_syms,
338 mod->unused_gpl_syms + mod->num_unused_gpl_syms,
339 mod->unused_gpl_crcs,
340 GPL_ONLY, true },
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500341#endif
Rusty Russelldafd0942008-07-22 19:24:25 -0500342 };
343
344 if (each_symbol_in_section(arr, ARRAY_SIZE(arr), mod, fn, data))
345 return true;
346 }
347 return false;
Arjan van de Venf71d20e2006-06-28 04:26:45 -0700348}
Rusty Russellde4d8d52011-04-19 21:49:58 +0200349EXPORT_SYMBOL_GPL(each_symbol_section);
Arjan van de Venf71d20e2006-06-28 04:26:45 -0700350
Rusty Russelldafd0942008-07-22 19:24:25 -0500351struct find_symbol_arg {
352 /* Input */
353 const char *name;
354 bool gplok;
355 bool warn;
356
357 /* Output */
358 struct module *owner;
359 const unsigned long *crc;
Tim Abbott414fd312008-12-05 19:03:56 -0500360 const struct kernel_symbol *sym;
Rusty Russelldafd0942008-07-22 19:24:25 -0500361};
362
Rusty Russellde4d8d52011-04-19 21:49:58 +0200363static bool check_symbol(const struct symsearch *syms,
364 struct module *owner,
365 unsigned int symnum, void *data)
Rusty Russellad9546c2008-05-01 21:14:59 -0500366{
Rusty Russelldafd0942008-07-22 19:24:25 -0500367 struct find_symbol_arg *fsa = data;
368
Rusty Russelldafd0942008-07-22 19:24:25 -0500369 if (!fsa->gplok) {
370 if (syms->licence == GPL_ONLY)
371 return false;
372 if (syms->licence == WILL_BE_GPL_ONLY && fsa->warn) {
373 printk(KERN_WARNING "Symbol %s is being used "
374 "by a non-GPL module, which will not "
375 "be allowed in the future\n", fsa->name);
376 printk(KERN_WARNING "Please see the file "
377 "Documentation/feature-removal-schedule.txt "
378 "in the kernel source tree for more details.\n");
379 }
380 }
381
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500382#ifdef CONFIG_UNUSED_SYMBOLS
Rusty Russelldafd0942008-07-22 19:24:25 -0500383 if (syms->unused && fsa->warn) {
Rusty Russellad9546c2008-05-01 21:14:59 -0500384 printk(KERN_WARNING "Symbol %s is marked as UNUSED, "
Rusty Russelldafd0942008-07-22 19:24:25 -0500385 "however this module is using it.\n", fsa->name);
Rusty Russellad9546c2008-05-01 21:14:59 -0500386 printk(KERN_WARNING
387 "This symbol will go away in the future.\n");
388 printk(KERN_WARNING
389 "Please evalute if this is the right api to use and if "
390 "it really is, submit a report the linux kernel "
391 "mailinglist together with submitting your code for "
392 "inclusion.\n");
393 }
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500394#endif
Rusty Russelldafd0942008-07-22 19:24:25 -0500395
396 fsa->owner = owner;
397 fsa->crc = symversion(syms->crcs, symnum);
Tim Abbott414fd312008-12-05 19:03:56 -0500398 fsa->sym = &syms->start[symnum];
Rusty Russellad9546c2008-05-01 21:14:59 -0500399 return true;
400}
401
Alessio Igor Bogani403ed272011-04-20 11:10:52 +0200402static int cmp_name(const void *va, const void *vb)
403{
404 const char *a;
405 const struct kernel_symbol *b;
406 a = va; b = vb;
407 return strcmp(a, b->name);
408}
409
Rusty Russellde4d8d52011-04-19 21:49:58 +0200410static bool find_symbol_in_section(const struct symsearch *syms,
411 struct module *owner,
412 void *data)
413{
414 struct find_symbol_arg *fsa = data;
Alessio Igor Bogani403ed272011-04-20 11:10:52 +0200415 struct kernel_symbol *sym;
Rusty Russellde4d8d52011-04-19 21:49:58 +0200416
Alessio Igor Bogani403ed272011-04-20 11:10:52 +0200417 sym = bsearch(fsa->name, syms->start, syms->stop - syms->start,
418 sizeof(struct kernel_symbol), cmp_name);
419
420 if (sym != NULL && check_symbol(syms, owner, sym - syms->start, data))
421 return true;
422
Rusty Russellde4d8d52011-04-19 21:49:58 +0200423 return false;
424}
425
Tim Abbott414fd312008-12-05 19:03:56 -0500426/* Find a symbol and return it, along with, (optional) crc and
Rusty Russell75676502010-06-05 11:17:36 -0600427 * (optional) module which owns it. Needs preempt disabled or module_mutex. */
Tim Abbottc6b37802008-12-05 19:03:59 -0500428const struct kernel_symbol *find_symbol(const char *name,
429 struct module **owner,
430 const unsigned long **crc,
431 bool gplok,
432 bool warn)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433{
Rusty Russelldafd0942008-07-22 19:24:25 -0500434 struct find_symbol_arg fsa;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435
Rusty Russelldafd0942008-07-22 19:24:25 -0500436 fsa.name = name;
437 fsa.gplok = gplok;
438 fsa.warn = warn;
439
Rusty Russellde4d8d52011-04-19 21:49:58 +0200440 if (each_symbol_section(find_symbol_in_section, &fsa)) {
Rusty Russellad9546c2008-05-01 21:14:59 -0500441 if (owner)
Rusty Russelldafd0942008-07-22 19:24:25 -0500442 *owner = fsa.owner;
443 if (crc)
444 *crc = fsa.crc;
Tim Abbott414fd312008-12-05 19:03:56 -0500445 return fsa.sym;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 }
Rusty Russellad9546c2008-05-01 21:14:59 -0500447
Jim Cromie5e124162011-12-06 12:11:31 -0700448 pr_debug("Failed to find symbol %s\n", name);
Tim Abbott414fd312008-12-05 19:03:56 -0500449 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450}
Tim Abbottc6b37802008-12-05 19:03:59 -0500451EXPORT_SYMBOL_GPL(find_symbol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453/* Search for module by name: must hold module_mutex. */
Tim Abbottc6b37802008-12-05 19:03:59 -0500454struct module *find_module(const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455{
456 struct module *mod;
457
458 list_for_each_entry(mod, &modules, list) {
459 if (strcmp(mod->name, name) == 0)
460 return mod;
461 }
462 return NULL;
463}
Tim Abbottc6b37802008-12-05 19:03:59 -0500464EXPORT_SYMBOL_GPL(find_module);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465
466#ifdef CONFIG_SMP
Tejun Heofbf59bc2009-02-20 16:29:08 +0900467
Tejun Heo259354d2010-03-10 18:56:10 +0900468static inline void __percpu *mod_percpu(struct module *mod)
Tejun Heofbf59bc2009-02-20 16:29:08 +0900469{
Tejun Heo259354d2010-03-10 18:56:10 +0900470 return mod->percpu;
471}
Tejun Heofbf59bc2009-02-20 16:29:08 +0900472
Tejun Heo259354d2010-03-10 18:56:10 +0900473static int percpu_modalloc(struct module *mod,
474 unsigned long size, unsigned long align)
475{
Tejun Heofbf59bc2009-02-20 16:29:08 +0900476 if (align > PAGE_SIZE) {
477 printk(KERN_WARNING "%s: per-cpu alignment %li > %li\n",
Tejun Heo259354d2010-03-10 18:56:10 +0900478 mod->name, align, PAGE_SIZE);
Tejun Heofbf59bc2009-02-20 16:29:08 +0900479 align = PAGE_SIZE;
480 }
481
Tejun Heo259354d2010-03-10 18:56:10 +0900482 mod->percpu = __alloc_reserved_percpu(size, align);
483 if (!mod->percpu) {
Tejun Heofbf59bc2009-02-20 16:29:08 +0900484 printk(KERN_WARNING
Rusty Russelld9131882010-08-05 12:59:08 -0600485 "%s: Could not allocate %lu bytes percpu data\n",
486 mod->name, size);
Tejun Heo259354d2010-03-10 18:56:10 +0900487 return -ENOMEM;
488 }
489 mod->percpu_size = size;
490 return 0;
Tejun Heofbf59bc2009-02-20 16:29:08 +0900491}
492
Tejun Heo259354d2010-03-10 18:56:10 +0900493static void percpu_modfree(struct module *mod)
Tejun Heofbf59bc2009-02-20 16:29:08 +0900494{
Tejun Heo259354d2010-03-10 18:56:10 +0900495 free_percpu(mod->percpu);
Tejun Heofbf59bc2009-02-20 16:29:08 +0900496}
497
Rusty Russell49668682010-08-05 12:59:10 -0600498static unsigned int find_pcpusec(struct load_info *info)
Tejun Heo6b588c12009-02-20 16:29:07 +0900499{
Rusty Russell49668682010-08-05 12:59:10 -0600500 return find_sec(info, ".data..percpu");
Tejun Heo6b588c12009-02-20 16:29:07 +0900501}
502
Tejun Heo259354d2010-03-10 18:56:10 +0900503static void percpu_modcopy(struct module *mod,
504 const void *from, unsigned long size)
Tejun Heo6b588c12009-02-20 16:29:07 +0900505{
506 int cpu;
507
508 for_each_possible_cpu(cpu)
Tejun Heo259354d2010-03-10 18:56:10 +0900509 memcpy(per_cpu_ptr(mod->percpu, cpu), from, size);
Tejun Heo6b588c12009-02-20 16:29:07 +0900510}
511
Tejun Heo10fad5e2010-03-10 18:57:54 +0900512/**
513 * is_module_percpu_address - test whether address is from module static percpu
514 * @addr: address to test
515 *
516 * Test whether @addr belongs to module static percpu area.
517 *
518 * RETURNS:
519 * %true if @addr is from module static percpu area
520 */
521bool is_module_percpu_address(unsigned long addr)
522{
523 struct module *mod;
524 unsigned int cpu;
525
526 preempt_disable();
527
528 list_for_each_entry_rcu(mod, &modules, list) {
529 if (!mod->percpu_size)
530 continue;
531 for_each_possible_cpu(cpu) {
532 void *start = per_cpu_ptr(mod->percpu, cpu);
533
534 if ((void *)addr >= start &&
535 (void *)addr < start + mod->percpu_size) {
536 preempt_enable();
537 return true;
538 }
539 }
540 }
541
542 preempt_enable();
543 return false;
Daniel Walker22a8bde2007-10-18 03:06:07 -0700544}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545
546#else /* ... !CONFIG_SMP */
Tejun Heo6b588c12009-02-20 16:29:07 +0900547
Tejun Heo259354d2010-03-10 18:56:10 +0900548static inline void __percpu *mod_percpu(struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549{
550 return NULL;
551}
Tejun Heo259354d2010-03-10 18:56:10 +0900552static inline int percpu_modalloc(struct module *mod,
553 unsigned long size, unsigned long align)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554{
Tejun Heo259354d2010-03-10 18:56:10 +0900555 return -ENOMEM;
556}
557static inline void percpu_modfree(struct module *mod)
558{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559}
Rusty Russell49668682010-08-05 12:59:10 -0600560static unsigned int find_pcpusec(struct load_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561{
562 return 0;
563}
Tejun Heo259354d2010-03-10 18:56:10 +0900564static inline void percpu_modcopy(struct module *mod,
565 const void *from, unsigned long size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566{
567 /* pcpusec should be 0, and size of that section should be 0. */
568 BUG_ON(size != 0);
569}
Tejun Heo10fad5e2010-03-10 18:57:54 +0900570bool is_module_percpu_address(unsigned long addr)
571{
572 return false;
573}
Tejun Heo6b588c12009-02-20 16:29:07 +0900574
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575#endif /* CONFIG_SMP */
576
Matt Domschc988d2b2005-06-23 22:05:15 -0700577#define MODINFO_ATTR(field) \
578static void setup_modinfo_##field(struct module *mod, const char *s) \
579{ \
580 mod->field = kstrdup(s, GFP_KERNEL); \
581} \
582static ssize_t show_modinfo_##field(struct module_attribute *mattr, \
Kay Sievers4befb022011-07-24 22:06:04 +0930583 struct module_kobject *mk, char *buffer) \
Matt Domschc988d2b2005-06-23 22:05:15 -0700584{ \
Kay Sievers4befb022011-07-24 22:06:04 +0930585 return sprintf(buffer, "%s\n", mk->mod->field); \
Matt Domschc988d2b2005-06-23 22:05:15 -0700586} \
587static int modinfo_##field##_exists(struct module *mod) \
588{ \
589 return mod->field != NULL; \
590} \
591static void free_modinfo_##field(struct module *mod) \
592{ \
Daniel Walker22a8bde2007-10-18 03:06:07 -0700593 kfree(mod->field); \
594 mod->field = NULL; \
Matt Domschc988d2b2005-06-23 22:05:15 -0700595} \
596static struct module_attribute modinfo_##field = { \
Tejun Heo7b595752007-06-14 03:45:17 +0900597 .attr = { .name = __stringify(field), .mode = 0444 }, \
Matt Domschc988d2b2005-06-23 22:05:15 -0700598 .show = show_modinfo_##field, \
599 .setup = setup_modinfo_##field, \
600 .test = modinfo_##field##_exists, \
601 .free = free_modinfo_##field, \
602};
603
604MODINFO_ATTR(version);
605MODINFO_ATTR(srcversion);
606
Arjan van de Vene14af7e2008-01-25 21:08:33 +0100607static char last_unloaded_module[MODULE_NAME_LEN+1];
608
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -0800609#ifdef CONFIG_MODULE_UNLOAD
Steven Rostedteb0c5372010-03-29 14:25:18 -0400610
611EXPORT_TRACEPOINT_SYMBOL(module_get);
612
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613/* Init the unload section of the module. */
Rusty Russell9f85a4b2010-08-05 12:59:04 -0600614static int module_unload_init(struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615{
Rusty Russell9f85a4b2010-08-05 12:59:04 -0600616 mod->refptr = alloc_percpu(struct module_ref);
617 if (!mod->refptr)
618 return -ENOMEM;
619
Linus Torvalds2c02dfe2010-05-31 12:19:37 -0700620 INIT_LIST_HEAD(&mod->source_list);
621 INIT_LIST_HEAD(&mod->target_list);
Christoph Lametere1783a22010-01-05 15:34:50 +0900622
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 /* Hold reference count during initialization. */
Nick Piggin5fbfb182010-04-01 19:09:40 +1100624 __this_cpu_write(mod->refptr->incs, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 /* Backwards compatibility macros put refcount during init. */
626 mod->waiter = current;
Rusty Russell9f85a4b2010-08-05 12:59:04 -0600627
628 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629}
630
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631/* Does a already use b? */
632static int already_uses(struct module *a, struct module *b)
633{
634 struct module_use *use;
635
Linus Torvalds2c02dfe2010-05-31 12:19:37 -0700636 list_for_each_entry(use, &b->source_list, source_list) {
637 if (use->source == a) {
Jim Cromie5e124162011-12-06 12:11:31 -0700638 pr_debug("%s uses %s!\n", a->name, b->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 return 1;
640 }
641 }
Jim Cromie5e124162011-12-06 12:11:31 -0700642 pr_debug("%s does not use %s!\n", a->name, b->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 return 0;
644}
645
Linus Torvalds2c02dfe2010-05-31 12:19:37 -0700646/*
647 * Module a uses b
648 * - we add 'a' as a "source", 'b' as a "target" of module use
649 * - the module_use is added to the list of 'b' sources (so
650 * 'b' can walk the list to see who sourced them), and of 'a'
651 * targets (so 'a' can see what modules it targets).
652 */
653static int add_module_usage(struct module *a, struct module *b)
654{
Linus Torvalds2c02dfe2010-05-31 12:19:37 -0700655 struct module_use *use;
656
Jim Cromie5e124162011-12-06 12:11:31 -0700657 pr_debug("Allocating new usage for %s.\n", a->name);
Linus Torvalds2c02dfe2010-05-31 12:19:37 -0700658 use = kmalloc(sizeof(*use), GFP_ATOMIC);
659 if (!use) {
660 printk(KERN_WARNING "%s: out of memory loading\n", a->name);
661 return -ENOMEM;
662 }
663
664 use->source = a;
665 use->target = b;
666 list_add(&use->source_list, &b->source_list);
667 list_add(&use->target_list, &a->target_list);
Linus Torvalds2c02dfe2010-05-31 12:19:37 -0700668 return 0;
669}
670
Rusty Russell75676502010-06-05 11:17:36 -0600671/* Module a uses b: caller needs module_mutex() */
Rusty Russell9bea7f22010-06-05 11:17:37 -0600672int ref_module(struct module *a, struct module *b)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673{
Rusty Russellc8e21ce2010-06-05 11:17:35 -0600674 int err;
Kay Sievers270a6c42007-01-18 13:26:15 +0100675
Rusty Russell9bea7f22010-06-05 11:17:37 -0600676 if (b == NULL || already_uses(a, b))
Linus Torvalds218ce732010-05-25 16:48:30 -0700677 return 0;
Linus Torvalds218ce732010-05-25 16:48:30 -0700678
Rusty Russell9bea7f22010-06-05 11:17:37 -0600679 /* If module isn't available, we fail. */
680 err = strong_try_module_get(b);
Rusty Russellc9a3ba52008-01-29 17:13:18 -0500681 if (err)
Rusty Russell9bea7f22010-06-05 11:17:37 -0600682 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683
Linus Torvalds2c02dfe2010-05-31 12:19:37 -0700684 err = add_module_usage(a, b);
685 if (err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 module_put(b);
Rusty Russell9bea7f22010-06-05 11:17:37 -0600687 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 }
Rusty Russell9bea7f22010-06-05 11:17:37 -0600689 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690}
Rusty Russell9bea7f22010-06-05 11:17:37 -0600691EXPORT_SYMBOL_GPL(ref_module);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692
693/* Clear the unload stuff of the module. */
694static void module_unload_free(struct module *mod)
695{
Linus Torvalds2c02dfe2010-05-31 12:19:37 -0700696 struct module_use *use, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697
Rusty Russell75676502010-06-05 11:17:36 -0600698 mutex_lock(&module_mutex);
Linus Torvalds2c02dfe2010-05-31 12:19:37 -0700699 list_for_each_entry_safe(use, tmp, &mod->target_list, target_list) {
700 struct module *i = use->target;
Jim Cromie5e124162011-12-06 12:11:31 -0700701 pr_debug("%s unusing %s\n", mod->name, i->name);
Linus Torvalds2c02dfe2010-05-31 12:19:37 -0700702 module_put(i);
703 list_del(&use->source_list);
704 list_del(&use->target_list);
705 kfree(use);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 }
Rusty Russell75676502010-06-05 11:17:36 -0600707 mutex_unlock(&module_mutex);
Rusty Russell9f85a4b2010-08-05 12:59:04 -0600708
709 free_percpu(mod->refptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710}
711
712#ifdef CONFIG_MODULE_FORCE_UNLOAD
Akinobu Mitafb169792006-01-08 01:04:29 -0800713static inline int try_force_unload(unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714{
715 int ret = (flags & O_TRUNC);
716 if (ret)
Akinobu Mitafb169792006-01-08 01:04:29 -0800717 add_taint(TAINT_FORCED_RMMOD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 return ret;
719}
720#else
Akinobu Mitafb169792006-01-08 01:04:29 -0800721static inline int try_force_unload(unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722{
723 return 0;
724}
725#endif /* CONFIG_MODULE_FORCE_UNLOAD */
726
727struct stopref
728{
729 struct module *mod;
730 int flags;
731 int *forced;
732};
733
734/* Whole machine is stopped with interrupts off when this runs. */
735static int __try_stop_module(void *_sref)
736{
737 struct stopref *sref = _sref;
738
Rusty Russellda39ba52008-07-22 19:24:25 -0500739 /* If it's not unused, quit unless we're forcing. */
740 if (module_refcount(sref->mod) != 0) {
Akinobu Mitafb169792006-01-08 01:04:29 -0800741 if (!(*sref->forced = try_force_unload(sref->flags)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 return -EWOULDBLOCK;
743 }
744
745 /* Mark it as dying. */
746 sref->mod->state = MODULE_STATE_GOING;
747 return 0;
748}
749
750static int try_stop_module(struct module *mod, int flags, int *forced)
751{
Rusty Russellda39ba52008-07-22 19:24:25 -0500752 if (flags & O_NONBLOCK) {
753 struct stopref sref = { mod, flags, forced };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754
Rusty Russell9b1a4d32008-07-28 12:16:30 -0500755 return stop_machine(__try_stop_module, &sref, NULL);
Rusty Russellda39ba52008-07-22 19:24:25 -0500756 } else {
757 /* We don't need to stop the machine for this. */
758 mod->state = MODULE_STATE_GOING;
759 synchronize_sched();
760 return 0;
761 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762}
763
Eric Dumazetbd77c042012-01-13 09:32:14 +1030764unsigned long module_refcount(struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765{
Eric Dumazetbd77c042012-01-13 09:32:14 +1030766 unsigned long incs = 0, decs = 0;
Eric Dumazet720eba32009-02-03 13:31:36 +1030767 int cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768
Eric Dumazet720eba32009-02-03 13:31:36 +1030769 for_each_possible_cpu(cpu)
Nick Piggin5fbfb182010-04-01 19:09:40 +1100770 decs += per_cpu_ptr(mod->refptr, cpu)->decs;
771 /*
772 * ensure the incs are added up after the decs.
773 * module_put ensures incs are visible before decs with smp_wmb.
774 *
775 * This 2-count scheme avoids the situation where the refcount
776 * for CPU0 is read, then CPU0 increments the module refcount,
777 * then CPU1 drops that refcount, then the refcount for CPU1 is
778 * read. We would record a decrement but not its corresponding
779 * increment so we would see a low count (disaster).
780 *
781 * Rare situation? But module_refcount can be preempted, and we
782 * might be tallying up 4096+ CPUs. So it is not impossible.
783 */
784 smp_rmb();
785 for_each_possible_cpu(cpu)
786 incs += per_cpu_ptr(mod->refptr, cpu)->incs;
787 return incs - decs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788}
789EXPORT_SYMBOL(module_refcount);
790
791/* This exists whether we can unload or not */
792static void free_module(struct module *mod);
793
794static void wait_for_zero_refcount(struct module *mod)
795{
Matthew Wilcoxa6550202008-02-26 10:47:18 -0500796 /* Since we might sleep for some time, release the mutex first */
Ashutosh Naik6389a382006-03-23 03:00:46 -0800797 mutex_unlock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 for (;;) {
Jim Cromie5e124162011-12-06 12:11:31 -0700799 pr_debug("Looking at refcount...\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 set_current_state(TASK_UNINTERRUPTIBLE);
801 if (module_refcount(mod) == 0)
802 break;
803 schedule();
804 }
805 current->state = TASK_RUNNING;
Ashutosh Naik6389a382006-03-23 03:00:46 -0800806 mutex_lock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807}
808
Heiko Carstens17da2bd2009-01-14 14:14:10 +0100809SYSCALL_DEFINE2(delete_module, const char __user *, name_user,
810 unsigned int, flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811{
812 struct module *mod;
Greg Kroah-Hartmandfff0a02007-02-23 14:54:57 -0800813 char name[MODULE_NAME_LEN];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 int ret, forced = 0;
815
Kees Cook3d433212009-04-02 15:49:29 -0700816 if (!capable(CAP_SYS_MODULE) || modules_disabled)
Greg Kroah-Hartmandfff0a02007-02-23 14:54:57 -0800817 return -EPERM;
818
819 if (strncpy_from_user(name, name_user, MODULE_NAME_LEN-1) < 0)
820 return -EFAULT;
821 name[MODULE_NAME_LEN-1] = '\0';
822
Tejun Heo3fc1f1e2010-05-06 18:49:20 +0200823 if (mutex_lock_interruptible(&module_mutex) != 0)
824 return -EINTR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825
826 mod = find_module(name);
827 if (!mod) {
828 ret = -ENOENT;
829 goto out;
830 }
831
Linus Torvalds2c02dfe2010-05-31 12:19:37 -0700832 if (!list_empty(&mod->source_list)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 /* Other modules depend on us: get rid of them first. */
834 ret = -EWOULDBLOCK;
835 goto out;
836 }
837
838 /* Doing init or already dying? */
839 if (mod->state != MODULE_STATE_LIVE) {
840 /* FIXME: if (force), slam module count and wake up
841 waiter --RR */
Jim Cromie5e124162011-12-06 12:11:31 -0700842 pr_debug("%s already dying\n", mod->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 ret = -EBUSY;
844 goto out;
845 }
846
847 /* If it has an init func, it must have an exit func to unload */
Rusty Russellaf49d922007-10-16 23:26:27 -0700848 if (mod->init && !mod->exit) {
Akinobu Mitafb169792006-01-08 01:04:29 -0800849 forced = try_force_unload(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 if (!forced) {
851 /* This module can't be removed */
852 ret = -EBUSY;
853 goto out;
854 }
855 }
856
857 /* Set this up before setting mod->state */
858 mod->waiter = current;
859
860 /* Stop the machine so refcounts can't move and disable module. */
861 ret = try_stop_module(mod, flags, &forced);
862 if (ret != 0)
863 goto out;
864
865 /* Never wait if forced. */
866 if (!forced && module_refcount(mod) != 0)
867 wait_for_zero_refcount(mod);
868
Peter Oberparleiterdf4b5652008-04-21 14:34:31 +0200869 mutex_unlock(&module_mutex);
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300870 /* Final destruction now no one is using it. */
Peter Oberparleiterdf4b5652008-04-21 14:34:31 +0200871 if (mod->exit != NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 mod->exit();
Peter Oberparleiterdf4b5652008-04-21 14:34:31 +0200873 blocking_notifier_call_chain(&module_notify_list,
874 MODULE_STATE_GOING, mod);
Arjan van de Ven22a9d642009-01-07 08:45:46 -0800875 async_synchronize_full();
Rusty Russell75676502010-06-05 11:17:36 -0600876
Arjan van de Vene14af7e2008-01-25 21:08:33 +0100877 /* Store the name of the last unloaded module for diagnostic purposes */
Rusty Russellefa53452008-01-29 17:13:20 -0500878 strlcpy(last_unloaded_module, mod->name, sizeof(last_unloaded_module));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879
Rusty Russell75676502010-06-05 11:17:36 -0600880 free_module(mod);
881 return 0;
882out:
Ashutosh Naik6389a382006-03-23 03:00:46 -0800883 mutex_unlock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 return ret;
885}
886
Jianjun Kongd1e99d72008-12-08 14:26:29 +0800887static inline void print_unload_info(struct seq_file *m, struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888{
889 struct module_use *use;
890 int printed_something = 0;
891
Eric Dumazetbd77c042012-01-13 09:32:14 +1030892 seq_printf(m, " %lu ", module_refcount(mod));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893
894 /* Always include a trailing , so userspace can differentiate
895 between this and the old multi-field proc format. */
Linus Torvalds2c02dfe2010-05-31 12:19:37 -0700896 list_for_each_entry(use, &mod->source_list, source_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 printed_something = 1;
Linus Torvalds2c02dfe2010-05-31 12:19:37 -0700898 seq_printf(m, "%s,", use->source->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 }
900
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 if (mod->init != NULL && mod->exit == NULL) {
902 printed_something = 1;
903 seq_printf(m, "[permanent],");
904 }
905
906 if (!printed_something)
907 seq_printf(m, "-");
908}
909
910void __symbol_put(const char *symbol)
911{
912 struct module *owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913
Rusty Russell24da1cb2007-07-15 23:41:46 -0700914 preempt_disable();
Tim Abbott414fd312008-12-05 19:03:56 -0500915 if (!find_symbol(symbol, &owner, NULL, true, false))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 BUG();
917 module_put(owner);
Rusty Russell24da1cb2007-07-15 23:41:46 -0700918 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919}
920EXPORT_SYMBOL(__symbol_put);
921
Rusty Russell7d1d16e2009-08-26 22:02:54 +0930922/* Note this assumes addr is a function, which it currently always is. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923void symbol_put_addr(void *addr)
924{
Trent Piepho5e376612006-05-15 09:44:06 -0700925 struct module *modaddr;
Rusty Russell7d1d16e2009-08-26 22:02:54 +0930926 unsigned long a = (unsigned long)dereference_function_descriptor(addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927
Rusty Russell7d1d16e2009-08-26 22:02:54 +0930928 if (core_kernel_text(a))
Trent Piepho5e376612006-05-15 09:44:06 -0700929 return;
930
Rusty Russella6e6abd2009-03-31 13:05:31 -0600931 /* module_text_address is safe here: we're supposed to have reference
932 * to module from symbol_get, so it can't go away. */
Rusty Russell7d1d16e2009-08-26 22:02:54 +0930933 modaddr = __module_text_address(a);
Rusty Russella6e6abd2009-03-31 13:05:31 -0600934 BUG_ON(!modaddr);
Trent Piepho5e376612006-05-15 09:44:06 -0700935 module_put(modaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936}
937EXPORT_SYMBOL_GPL(symbol_put_addr);
938
939static ssize_t show_refcnt(struct module_attribute *mattr,
Kay Sievers4befb022011-07-24 22:06:04 +0930940 struct module_kobject *mk, char *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941{
Eric Dumazetbd77c042012-01-13 09:32:14 +1030942 return sprintf(buffer, "%lu\n", module_refcount(mk->mod));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943}
944
Kay Sieverscca3e702012-01-13 09:32:15 +1030945static struct module_attribute modinfo_refcnt =
946 __ATTR(refcnt, 0444, show_refcnt, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947
Steven Rostedtd53799b2012-03-26 12:50:52 +1030948void __module_get(struct module *module)
949{
950 if (module) {
951 preempt_disable();
952 __this_cpu_inc(module->refptr->incs);
953 trace_module_get(module, _RET_IP_);
954 preempt_enable();
955 }
956}
957EXPORT_SYMBOL(__module_get);
958
959bool try_module_get(struct module *module)
960{
961 bool ret = true;
962
963 if (module) {
964 preempt_disable();
965
966 if (likely(module_is_live(module))) {
967 __this_cpu_inc(module->refptr->incs);
968 trace_module_get(module, _RET_IP_);
969 } else
970 ret = false;
971
972 preempt_enable();
973 }
974 return ret;
975}
976EXPORT_SYMBOL(try_module_get);
977
Al Virof6a57032006-10-18 01:47:25 -0400978void module_put(struct module *module)
979{
980 if (module) {
Christoph Lametere1783a22010-01-05 15:34:50 +0900981 preempt_disable();
Nick Piggin5fbfb182010-04-01 19:09:40 +1100982 smp_wmb(); /* see comment in module_refcount */
983 __this_cpu_inc(module->refptr->decs);
Christoph Lametere1783a22010-01-05 15:34:50 +0900984
Li Zefanae832d12010-03-24 10:57:43 +0800985 trace_module_put(module, _RET_IP_);
Al Virof6a57032006-10-18 01:47:25 -0400986 /* Maybe they're waiting for us to drop reference? */
987 if (unlikely(!module_is_live(module)))
988 wake_up_process(module->waiter);
Christoph Lametere1783a22010-01-05 15:34:50 +0900989 preempt_enable();
Al Virof6a57032006-10-18 01:47:25 -0400990 }
991}
992EXPORT_SYMBOL(module_put);
993
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994#else /* !CONFIG_MODULE_UNLOAD */
Jianjun Kongd1e99d72008-12-08 14:26:29 +0800995static inline void print_unload_info(struct seq_file *m, struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996{
997 /* We don't know the usage count, or what modules are using. */
998 seq_printf(m, " - -");
999}
1000
1001static inline void module_unload_free(struct module *mod)
1002{
1003}
1004
Rusty Russell9bea7f22010-06-05 11:17:37 -06001005int ref_module(struct module *a, struct module *b)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006{
Rusty Russell9bea7f22010-06-05 11:17:37 -06001007 return strong_try_module_get(b);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008}
Rusty Russell9bea7f22010-06-05 11:17:37 -06001009EXPORT_SYMBOL_GPL(ref_module);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010
Rusty Russell9f85a4b2010-08-05 12:59:04 -06001011static inline int module_unload_init(struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012{
Rusty Russell9f85a4b2010-08-05 12:59:04 -06001013 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014}
1015#endif /* CONFIG_MODULE_UNLOAD */
1016
Kevin Winchester53999bf2012-01-15 19:32:55 -04001017static size_t module_flags_taint(struct module *mod, char *buf)
1018{
1019 size_t l = 0;
1020
1021 if (mod->taints & (1 << TAINT_PROPRIETARY_MODULE))
1022 buf[l++] = 'P';
1023 if (mod->taints & (1 << TAINT_OOT_MODULE))
1024 buf[l++] = 'O';
1025 if (mod->taints & (1 << TAINT_FORCED_MODULE))
1026 buf[l++] = 'F';
1027 if (mod->taints & (1 << TAINT_CRAP))
1028 buf[l++] = 'C';
1029 /*
1030 * TAINT_FORCED_RMMOD: could be added.
1031 * TAINT_UNSAFE_SMP, TAINT_MACHINE_CHECK, TAINT_BAD_PAGE don't
1032 * apply to modules.
1033 */
1034 return l;
1035}
1036
Kay Sievers1f717402006-11-24 12:15:25 +01001037static ssize_t show_initstate(struct module_attribute *mattr,
Kay Sievers4befb022011-07-24 22:06:04 +09301038 struct module_kobject *mk, char *buffer)
Kay Sievers1f717402006-11-24 12:15:25 +01001039{
1040 const char *state = "unknown";
1041
Kay Sievers4befb022011-07-24 22:06:04 +09301042 switch (mk->mod->state) {
Kay Sievers1f717402006-11-24 12:15:25 +01001043 case MODULE_STATE_LIVE:
1044 state = "live";
1045 break;
1046 case MODULE_STATE_COMING:
1047 state = "coming";
1048 break;
1049 case MODULE_STATE_GOING:
1050 state = "going";
1051 break;
1052 }
1053 return sprintf(buffer, "%s\n", state);
1054}
1055
Kay Sieverscca3e702012-01-13 09:32:15 +10301056static struct module_attribute modinfo_initstate =
1057 __ATTR(initstate, 0444, show_initstate, NULL);
Kay Sievers1f717402006-11-24 12:15:25 +01001058
Kay Sievers88bfa322011-07-24 22:06:04 +09301059static ssize_t store_uevent(struct module_attribute *mattr,
1060 struct module_kobject *mk,
1061 const char *buffer, size_t count)
1062{
1063 enum kobject_action action;
1064
1065 if (kobject_action_type(buffer, count, &action) == 0)
1066 kobject_uevent(&mk->kobj, action);
1067 return count;
1068}
1069
Kay Sieverscca3e702012-01-13 09:32:15 +10301070struct module_attribute module_uevent =
1071 __ATTR(uevent, 0200, NULL, store_uevent);
1072
1073static ssize_t show_coresize(struct module_attribute *mattr,
1074 struct module_kobject *mk, char *buffer)
1075{
1076 return sprintf(buffer, "%u\n", mk->mod->core_size);
1077}
1078
1079static struct module_attribute modinfo_coresize =
1080 __ATTR(coresize, 0444, show_coresize, NULL);
1081
1082static ssize_t show_initsize(struct module_attribute *mattr,
1083 struct module_kobject *mk, char *buffer)
1084{
1085 return sprintf(buffer, "%u\n", mk->mod->init_size);
1086}
1087
1088static struct module_attribute modinfo_initsize =
1089 __ATTR(initsize, 0444, show_initsize, NULL);
1090
1091static ssize_t show_taint(struct module_attribute *mattr,
1092 struct module_kobject *mk, char *buffer)
1093{
1094 size_t l;
1095
1096 l = module_flags_taint(mk->mod, buffer);
1097 buffer[l++] = '\n';
1098 return l;
1099}
1100
1101static struct module_attribute modinfo_taint =
1102 __ATTR(taint, 0444, show_taint, NULL);
Kay Sievers88bfa322011-07-24 22:06:04 +09301103
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001104static struct module_attribute *modinfo_attrs[] = {
Kay Sieverscca3e702012-01-13 09:32:15 +10301105 &module_uevent,
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001106 &modinfo_version,
1107 &modinfo_srcversion,
Kay Sieverscca3e702012-01-13 09:32:15 +10301108 &modinfo_initstate,
1109 &modinfo_coresize,
1110 &modinfo_initsize,
1111 &modinfo_taint,
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001112#ifdef CONFIG_MODULE_UNLOAD
Kay Sieverscca3e702012-01-13 09:32:15 +10301113 &modinfo_refcnt,
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001114#endif
1115 NULL,
1116};
1117
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118static const char vermagic[] = VERMAGIC_STRING;
1119
Rusty Russellc6e665c2009-03-31 13:05:33 -06001120static int try_to_force_load(struct module *mod, const char *reason)
Linus Torvalds826e4502008-05-04 17:04:16 -07001121{
1122#ifdef CONFIG_MODULE_FORCE_LOAD
Andi Kleen25ddbb12008-10-15 22:01:41 -07001123 if (!test_taint(TAINT_FORCED_MODULE))
Rusty Russellc6e665c2009-03-31 13:05:33 -06001124 printk(KERN_WARNING "%s: %s: kernel tainted.\n",
1125 mod->name, reason);
Linus Torvalds826e4502008-05-04 17:04:16 -07001126 add_taint_module(mod, TAINT_FORCED_MODULE);
1127 return 0;
1128#else
1129 return -ENOEXEC;
1130#endif
1131}
1132
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133#ifdef CONFIG_MODVERSIONS
Rusty Russelld4703ae2009-12-15 16:28:32 -06001134/* If the arch applies (non-zero) relocations to kernel kcrctab, unapply it. */
1135static unsigned long maybe_relocated(unsigned long crc,
1136 const struct module *crc_owner)
1137{
1138#ifdef ARCH_RELOCATES_KCRCTAB
1139 if (crc_owner == NULL)
1140 return crc - (unsigned long)reloc_start;
1141#endif
1142 return crc;
1143}
1144
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145static int check_version(Elf_Shdr *sechdrs,
1146 unsigned int versindex,
1147 const char *symname,
1148 struct module *mod,
Rusty Russelld4703ae2009-12-15 16:28:32 -06001149 const unsigned long *crc,
1150 const struct module *crc_owner)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151{
1152 unsigned int i, num_versions;
1153 struct modversion_info *versions;
1154
1155 /* Exporting module didn't supply crcs? OK, we're already tainted. */
1156 if (!crc)
1157 return 1;
1158
Rusty Russella5dd6972008-05-09 16:24:21 +10001159 /* No versions at all? modprobe --force does this. */
1160 if (versindex == 0)
1161 return try_to_force_load(mod, symname) == 0;
1162
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163 versions = (void *) sechdrs[versindex].sh_addr;
1164 num_versions = sechdrs[versindex].sh_size
1165 / sizeof(struct modversion_info);
1166
1167 for (i = 0; i < num_versions; i++) {
1168 if (strcmp(versions[i].name, symname) != 0)
1169 continue;
1170
Rusty Russelld4703ae2009-12-15 16:28:32 -06001171 if (versions[i].crc == maybe_relocated(*crc, crc_owner))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 return 1;
Jim Cromie5e124162011-12-06 12:11:31 -07001173 pr_debug("Found checksum %lX vs module %lX\n",
Rusty Russelld4703ae2009-12-15 16:28:32 -06001174 maybe_relocated(*crc, crc_owner), versions[i].crc);
Linus Torvalds826e4502008-05-04 17:04:16 -07001175 goto bad_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 }
Linus Torvalds826e4502008-05-04 17:04:16 -07001177
Rusty Russella5dd6972008-05-09 16:24:21 +10001178 printk(KERN_WARNING "%s: no symbol version for %s\n",
1179 mod->name, symname);
1180 return 0;
Linus Torvalds826e4502008-05-04 17:04:16 -07001181
1182bad_version:
1183 printk("%s: disagrees about version of symbol %s\n",
1184 mod->name, symname);
1185 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186}
1187
1188static inline int check_modstruct_version(Elf_Shdr *sechdrs,
1189 unsigned int versindex,
1190 struct module *mod)
1191{
1192 const unsigned long *crc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193
Rusty Russell75676502010-06-05 11:17:36 -06001194 /* Since this should be found in kernel (which can't be removed),
1195 * no locking is necessary. */
Mike Frysinger6560dc12009-07-23 23:42:08 +09301196 if (!find_symbol(MODULE_SYMBOL_PREFIX "module_layout", NULL,
1197 &crc, true, false))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198 BUG();
Rusty Russelld4703ae2009-12-15 16:28:32 -06001199 return check_version(sechdrs, versindex, "module_layout", mod, crc,
1200 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201}
1202
Rusty Russell91e37a72008-05-09 16:25:28 +10001203/* First part is kernel version, which we ignore if module has crcs. */
1204static inline int same_magic(const char *amagic, const char *bmagic,
1205 bool has_crcs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206{
Rusty Russell91e37a72008-05-09 16:25:28 +10001207 if (has_crcs) {
1208 amagic += strcspn(amagic, " ");
1209 bmagic += strcspn(bmagic, " ");
1210 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 return strcmp(amagic, bmagic) == 0;
1212}
1213#else
1214static inline int check_version(Elf_Shdr *sechdrs,
1215 unsigned int versindex,
1216 const char *symname,
1217 struct module *mod,
Rusty Russelld4703ae2009-12-15 16:28:32 -06001218 const unsigned long *crc,
1219 const struct module *crc_owner)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220{
1221 return 1;
1222}
1223
1224static inline int check_modstruct_version(Elf_Shdr *sechdrs,
1225 unsigned int versindex,
1226 struct module *mod)
1227{
1228 return 1;
1229}
1230
Rusty Russell91e37a72008-05-09 16:25:28 +10001231static inline int same_magic(const char *amagic, const char *bmagic,
1232 bool has_crcs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233{
1234 return strcmp(amagic, bmagic) == 0;
1235}
1236#endif /* CONFIG_MODVERSIONS */
1237
Rusty Russell75676502010-06-05 11:17:36 -06001238/* Resolve a symbol for this module. I.e. if we find one, record usage. */
Rusty Russell49668682010-08-05 12:59:10 -06001239static const struct kernel_symbol *resolve_symbol(struct module *mod,
1240 const struct load_info *info,
Tim Abbott414fd312008-12-05 19:03:56 -05001241 const char *name,
Rusty Russell9bea7f22010-06-05 11:17:37 -06001242 char ownername[])
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243{
1244 struct module *owner;
Tim Abbott414fd312008-12-05 19:03:56 -05001245 const struct kernel_symbol *sym;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246 const unsigned long *crc;
Rusty Russell9bea7f22010-06-05 11:17:37 -06001247 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248
Rusty Russell75676502010-06-05 11:17:36 -06001249 mutex_lock(&module_mutex);
Tim Abbott414fd312008-12-05 19:03:56 -05001250 sym = find_symbol(name, &owner, &crc,
Andi Kleen25ddbb12008-10-15 22:01:41 -07001251 !(mod->taints & (1 << TAINT_PROPRIETARY_MODULE)), true);
Rusty Russell9bea7f22010-06-05 11:17:37 -06001252 if (!sym)
1253 goto unlock;
1254
Rusty Russell49668682010-08-05 12:59:10 -06001255 if (!check_version(info->sechdrs, info->index.vers, name, mod, crc,
1256 owner)) {
Rusty Russell9bea7f22010-06-05 11:17:37 -06001257 sym = ERR_PTR(-EINVAL);
1258 goto getname;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 }
Rusty Russell9bea7f22010-06-05 11:17:37 -06001260
1261 err = ref_module(mod, owner);
1262 if (err) {
1263 sym = ERR_PTR(err);
1264 goto getname;
1265 }
1266
1267getname:
1268 /* We must make copy under the lock if we failed to get ref. */
1269 strncpy(ownername, module_name(owner), MODULE_NAME_LEN);
1270unlock:
Rusty Russell75676502010-06-05 11:17:36 -06001271 mutex_unlock(&module_mutex);
Linus Torvalds218ce732010-05-25 16:48:30 -07001272 return sym;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273}
1274
Rusty Russell49668682010-08-05 12:59:10 -06001275static const struct kernel_symbol *
1276resolve_symbol_wait(struct module *mod,
1277 const struct load_info *info,
1278 const char *name)
Rusty Russell9bea7f22010-06-05 11:17:37 -06001279{
1280 const struct kernel_symbol *ksym;
Rusty Russell49668682010-08-05 12:59:10 -06001281 char owner[MODULE_NAME_LEN];
Rusty Russell9bea7f22010-06-05 11:17:37 -06001282
1283 if (wait_event_interruptible_timeout(module_wq,
Rusty Russell49668682010-08-05 12:59:10 -06001284 !IS_ERR(ksym = resolve_symbol(mod, info, name, owner))
1285 || PTR_ERR(ksym) != -EBUSY,
Rusty Russell9bea7f22010-06-05 11:17:37 -06001286 30 * HZ) <= 0) {
1287 printk(KERN_WARNING "%s: gave up waiting for init of module %s.\n",
Rusty Russell49668682010-08-05 12:59:10 -06001288 mod->name, owner);
Rusty Russell9bea7f22010-06-05 11:17:37 -06001289 }
1290 return ksym;
1291}
1292
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293/*
1294 * /sys/module/foo/sections stuff
1295 * J. Corbet <corbet@lwn.net>
1296 */
Rusty Russell8f6d0372010-08-05 12:59:09 -06001297#ifdef CONFIG_SYSFS
Ben Hutchings10b465a2009-12-19 14:43:01 +00001298
Rusty Russell8f6d0372010-08-05 12:59:09 -06001299#ifdef CONFIG_KALLSYMS
Ben Hutchings10b465a2009-12-19 14:43:01 +00001300static inline bool sect_empty(const Elf_Shdr *sect)
1301{
1302 return !(sect->sh_flags & SHF_ALLOC) || sect->sh_size == 0;
1303}
1304
Rusty Russella58730c2008-03-13 09:03:44 +00001305struct module_sect_attr
1306{
1307 struct module_attribute mattr;
1308 char *name;
1309 unsigned long address;
1310};
1311
1312struct module_sect_attrs
1313{
1314 struct attribute_group grp;
1315 unsigned int nsections;
1316 struct module_sect_attr attrs[0];
1317};
1318
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319static ssize_t module_sect_show(struct module_attribute *mattr,
Kay Sievers4befb022011-07-24 22:06:04 +09301320 struct module_kobject *mk, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321{
1322 struct module_sect_attr *sattr =
1323 container_of(mattr, struct module_sect_attr, mattr);
Kees Cook9f36e2c2011-03-22 16:34:22 -07001324 return sprintf(buf, "0x%pK\n", (void *)sattr->address);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325}
1326
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001327static void free_sect_attrs(struct module_sect_attrs *sect_attrs)
1328{
Rusty Russella58730c2008-03-13 09:03:44 +00001329 unsigned int section;
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001330
1331 for (section = 0; section < sect_attrs->nsections; section++)
1332 kfree(sect_attrs->attrs[section].name);
1333 kfree(sect_attrs);
1334}
1335
Rusty Russell8f6d0372010-08-05 12:59:09 -06001336static void add_sect_attrs(struct module *mod, const struct load_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337{
1338 unsigned int nloaded = 0, i, size[2];
1339 struct module_sect_attrs *sect_attrs;
1340 struct module_sect_attr *sattr;
1341 struct attribute **gattr;
Daniel Walker22a8bde2007-10-18 03:06:07 -07001342
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343 /* Count loaded sections and allocate structures */
Rusty Russell8f6d0372010-08-05 12:59:09 -06001344 for (i = 0; i < info->hdr->e_shnum; i++)
1345 if (!sect_empty(&info->sechdrs[i]))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346 nloaded++;
1347 size[0] = ALIGN(sizeof(*sect_attrs)
1348 + nloaded * sizeof(sect_attrs->attrs[0]),
1349 sizeof(sect_attrs->grp.attrs[0]));
1350 size[1] = (nloaded + 1) * sizeof(sect_attrs->grp.attrs[0]);
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001351 sect_attrs = kzalloc(size[0] + size[1], GFP_KERNEL);
1352 if (sect_attrs == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353 return;
1354
1355 /* Setup section attributes. */
1356 sect_attrs->grp.name = "sections";
1357 sect_attrs->grp.attrs = (void *)sect_attrs + size[0];
1358
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001359 sect_attrs->nsections = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360 sattr = &sect_attrs->attrs[0];
1361 gattr = &sect_attrs->grp.attrs[0];
Rusty Russell8f6d0372010-08-05 12:59:09 -06001362 for (i = 0; i < info->hdr->e_shnum; i++) {
1363 Elf_Shdr *sec = &info->sechdrs[i];
1364 if (sect_empty(sec))
Helge Deller35dead42009-12-03 00:29:15 +01001365 continue;
Rusty Russell8f6d0372010-08-05 12:59:09 -06001366 sattr->address = sec->sh_addr;
1367 sattr->name = kstrdup(info->secstrings + sec->sh_name,
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001368 GFP_KERNEL);
1369 if (sattr->name == NULL)
1370 goto out;
1371 sect_attrs->nsections++;
Eric W. Biederman361795b2010-02-12 13:41:56 -08001372 sysfs_attr_init(&sattr->mattr.attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373 sattr->mattr.show = module_sect_show;
1374 sattr->mattr.store = NULL;
1375 sattr->mattr.attr.name = sattr->name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376 sattr->mattr.attr.mode = S_IRUGO;
1377 *(gattr++) = &(sattr++)->mattr.attr;
1378 }
1379 *gattr = NULL;
1380
1381 if (sysfs_create_group(&mod->mkobj.kobj, &sect_attrs->grp))
1382 goto out;
1383
1384 mod->sect_attrs = sect_attrs;
1385 return;
1386 out:
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001387 free_sect_attrs(sect_attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388}
1389
1390static void remove_sect_attrs(struct module *mod)
1391{
1392 if (mod->sect_attrs) {
1393 sysfs_remove_group(&mod->mkobj.kobj,
1394 &mod->sect_attrs->grp);
1395 /* We are positive that no one is using any sect attrs
1396 * at this point. Deallocate immediately. */
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001397 free_sect_attrs(mod->sect_attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398 mod->sect_attrs = NULL;
1399 }
1400}
1401
Roland McGrath6d760132007-10-16 23:26:40 -07001402/*
1403 * /sys/module/foo/notes/.section.name gives contents of SHT_NOTE sections.
1404 */
1405
1406struct module_notes_attrs {
1407 struct kobject *dir;
1408 unsigned int notes;
1409 struct bin_attribute attrs[0];
1410};
1411
Chris Wright2c3c8be2010-05-12 18:28:57 -07001412static ssize_t module_notes_read(struct file *filp, struct kobject *kobj,
Roland McGrath6d760132007-10-16 23:26:40 -07001413 struct bin_attribute *bin_attr,
1414 char *buf, loff_t pos, size_t count)
1415{
1416 /*
1417 * The caller checked the pos and count against our size.
1418 */
1419 memcpy(buf, bin_attr->private + pos, count);
1420 return count;
1421}
1422
1423static void free_notes_attrs(struct module_notes_attrs *notes_attrs,
1424 unsigned int i)
1425{
1426 if (notes_attrs->dir) {
1427 while (i-- > 0)
1428 sysfs_remove_bin_file(notes_attrs->dir,
1429 &notes_attrs->attrs[i]);
Alexey Dobriyane9432092008-09-23 23:51:11 +04001430 kobject_put(notes_attrs->dir);
Roland McGrath6d760132007-10-16 23:26:40 -07001431 }
1432 kfree(notes_attrs);
1433}
1434
Rusty Russell8f6d0372010-08-05 12:59:09 -06001435static void add_notes_attrs(struct module *mod, const struct load_info *info)
Roland McGrath6d760132007-10-16 23:26:40 -07001436{
1437 unsigned int notes, loaded, i;
1438 struct module_notes_attrs *notes_attrs;
1439 struct bin_attribute *nattr;
1440
Ingo Molnarea6bff32009-08-28 10:44:56 +02001441 /* failed to create section attributes, so can't create notes */
1442 if (!mod->sect_attrs)
1443 return;
1444
Roland McGrath6d760132007-10-16 23:26:40 -07001445 /* Count notes sections and allocate structures. */
1446 notes = 0;
Rusty Russell8f6d0372010-08-05 12:59:09 -06001447 for (i = 0; i < info->hdr->e_shnum; i++)
1448 if (!sect_empty(&info->sechdrs[i]) &&
1449 (info->sechdrs[i].sh_type == SHT_NOTE))
Roland McGrath6d760132007-10-16 23:26:40 -07001450 ++notes;
1451
1452 if (notes == 0)
1453 return;
1454
1455 notes_attrs = kzalloc(sizeof(*notes_attrs)
1456 + notes * sizeof(notes_attrs->attrs[0]),
1457 GFP_KERNEL);
1458 if (notes_attrs == NULL)
1459 return;
1460
1461 notes_attrs->notes = notes;
1462 nattr = &notes_attrs->attrs[0];
Rusty Russell8f6d0372010-08-05 12:59:09 -06001463 for (loaded = i = 0; i < info->hdr->e_shnum; ++i) {
1464 if (sect_empty(&info->sechdrs[i]))
Roland McGrath6d760132007-10-16 23:26:40 -07001465 continue;
Rusty Russell8f6d0372010-08-05 12:59:09 -06001466 if (info->sechdrs[i].sh_type == SHT_NOTE) {
Eric W. Biederman361795b2010-02-12 13:41:56 -08001467 sysfs_bin_attr_init(nattr);
Roland McGrath6d760132007-10-16 23:26:40 -07001468 nattr->attr.name = mod->sect_attrs->attrs[loaded].name;
1469 nattr->attr.mode = S_IRUGO;
Rusty Russell8f6d0372010-08-05 12:59:09 -06001470 nattr->size = info->sechdrs[i].sh_size;
1471 nattr->private = (void *) info->sechdrs[i].sh_addr;
Roland McGrath6d760132007-10-16 23:26:40 -07001472 nattr->read = module_notes_read;
1473 ++nattr;
1474 }
1475 ++loaded;
1476 }
1477
Greg Kroah-Hartman4ff6abf2007-11-05 22:24:43 -08001478 notes_attrs->dir = kobject_create_and_add("notes", &mod->mkobj.kobj);
Roland McGrath6d760132007-10-16 23:26:40 -07001479 if (!notes_attrs->dir)
1480 goto out;
1481
1482 for (i = 0; i < notes; ++i)
1483 if (sysfs_create_bin_file(notes_attrs->dir,
1484 &notes_attrs->attrs[i]))
1485 goto out;
1486
1487 mod->notes_attrs = notes_attrs;
1488 return;
1489
1490 out:
1491 free_notes_attrs(notes_attrs, i);
1492}
1493
1494static void remove_notes_attrs(struct module *mod)
1495{
1496 if (mod->notes_attrs)
1497 free_notes_attrs(mod->notes_attrs, mod->notes_attrs->notes);
1498}
1499
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500#else
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001501
Rusty Russell8f6d0372010-08-05 12:59:09 -06001502static inline void add_sect_attrs(struct module *mod,
1503 const struct load_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504{
1505}
1506
1507static inline void remove_sect_attrs(struct module *mod)
1508{
1509}
Roland McGrath6d760132007-10-16 23:26:40 -07001510
Rusty Russell8f6d0372010-08-05 12:59:09 -06001511static inline void add_notes_attrs(struct module *mod,
1512 const struct load_info *info)
Roland McGrath6d760132007-10-16 23:26:40 -07001513{
1514}
1515
1516static inline void remove_notes_attrs(struct module *mod)
1517{
1518}
Rusty Russell8f6d0372010-08-05 12:59:09 -06001519#endif /* CONFIG_KALLSYMS */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520
Rusty Russell80a3d1b2010-06-05 11:17:36 -06001521static void add_usage_links(struct module *mod)
1522{
1523#ifdef CONFIG_MODULE_UNLOAD
1524 struct module_use *use;
1525 int nowarn;
1526
Rusty Russell75676502010-06-05 11:17:36 -06001527 mutex_lock(&module_mutex);
Rusty Russell80a3d1b2010-06-05 11:17:36 -06001528 list_for_each_entry(use, &mod->target_list, target_list) {
1529 nowarn = sysfs_create_link(use->target->holders_dir,
1530 &mod->mkobj.kobj, mod->name);
1531 }
Rusty Russell75676502010-06-05 11:17:36 -06001532 mutex_unlock(&module_mutex);
Rusty Russell80a3d1b2010-06-05 11:17:36 -06001533#endif
1534}
1535
1536static void del_usage_links(struct module *mod)
1537{
1538#ifdef CONFIG_MODULE_UNLOAD
1539 struct module_use *use;
1540
Rusty Russell75676502010-06-05 11:17:36 -06001541 mutex_lock(&module_mutex);
Rusty Russell80a3d1b2010-06-05 11:17:36 -06001542 list_for_each_entry(use, &mod->target_list, target_list)
1543 sysfs_remove_link(use->target->holders_dir, mod->name);
Rusty Russell75676502010-06-05 11:17:36 -06001544 mutex_unlock(&module_mutex);
Rusty Russell80a3d1b2010-06-05 11:17:36 -06001545#endif
1546}
1547
Rusty Russell6407ebb22010-06-05 11:17:36 -06001548static int module_add_modinfo_attrs(struct module *mod)
Matt Domschc988d2b2005-06-23 22:05:15 -07001549{
1550 struct module_attribute *attr;
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001551 struct module_attribute *temp_attr;
Matt Domschc988d2b2005-06-23 22:05:15 -07001552 int error = 0;
1553 int i;
1554
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001555 mod->modinfo_attrs = kzalloc((sizeof(struct module_attribute) *
1556 (ARRAY_SIZE(modinfo_attrs) + 1)),
1557 GFP_KERNEL);
1558 if (!mod->modinfo_attrs)
1559 return -ENOMEM;
1560
1561 temp_attr = mod->modinfo_attrs;
Matt Domschc988d2b2005-06-23 22:05:15 -07001562 for (i = 0; (attr = modinfo_attrs[i]) && !error; i++) {
1563 if (!attr->test ||
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001564 (attr->test && attr->test(mod))) {
1565 memcpy(temp_attr, attr, sizeof(*temp_attr));
Eric W. Biederman361795b2010-02-12 13:41:56 -08001566 sysfs_attr_init(&temp_attr->attr);
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001567 error = sysfs_create_file(&mod->mkobj.kobj,&temp_attr->attr);
1568 ++temp_attr;
1569 }
Matt Domschc988d2b2005-06-23 22:05:15 -07001570 }
1571 return error;
1572}
1573
Rusty Russell6407ebb22010-06-05 11:17:36 -06001574static void module_remove_modinfo_attrs(struct module *mod)
Matt Domschc988d2b2005-06-23 22:05:15 -07001575{
1576 struct module_attribute *attr;
1577 int i;
1578
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001579 for (i = 0; (attr = &mod->modinfo_attrs[i]); i++) {
1580 /* pick a field to test for end of list */
1581 if (!attr->attr.name)
1582 break;
Matt Domschc988d2b2005-06-23 22:05:15 -07001583 sysfs_remove_file(&mod->mkobj.kobj,&attr->attr);
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001584 if (attr->free)
1585 attr->free(mod);
Matt Domschc988d2b2005-06-23 22:05:15 -07001586 }
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001587 kfree(mod->modinfo_attrs);
Matt Domschc988d2b2005-06-23 22:05:15 -07001588}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589
Rusty Russell6407ebb22010-06-05 11:17:36 -06001590static int mod_sysfs_init(struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591{
1592 int err;
Greg Kroah-Hartman6494a932008-01-27 15:38:40 -08001593 struct kobject *kobj;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594
Greg Kroah-Hartman823bccf2007-04-13 13:15:19 -07001595 if (!module_sysfs_initialized) {
1596 printk(KERN_ERR "%s: module sysfs not initialized\n",
Ed Swierk1cc5f712006-09-25 16:25:36 -07001597 mod->name);
1598 err = -EINVAL;
1599 goto out;
1600 }
Greg Kroah-Hartman6494a932008-01-27 15:38:40 -08001601
1602 kobj = kset_find_obj(module_kset, mod->name);
1603 if (kobj) {
1604 printk(KERN_ERR "%s: module is already loaded\n", mod->name);
1605 kobject_put(kobj);
1606 err = -EINVAL;
1607 goto out;
1608 }
1609
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610 mod->mkobj.mod = mod;
Kay Sieverse17e0f52006-11-24 12:15:25 +01001611
Greg Kroah-Hartmanac3c8142007-12-17 23:05:35 -07001612 memset(&mod->mkobj.kobj, 0, sizeof(mod->mkobj.kobj));
1613 mod->mkobj.kobj.kset = module_kset;
1614 err = kobject_init_and_add(&mod->mkobj.kobj, &module_ktype, NULL,
1615 "%s", mod->name);
1616 if (err)
1617 kobject_put(&mod->mkobj.kobj);
Kay Sievers270a6c42007-01-18 13:26:15 +01001618
Kay Sievers97c146ef2007-11-29 23:46:11 +01001619 /* delay uevent until full sysfs population */
Kay Sievers270a6c42007-01-18 13:26:15 +01001620out:
1621 return err;
1622}
1623
Rusty Russell6407ebb22010-06-05 11:17:36 -06001624static int mod_sysfs_setup(struct module *mod,
Rusty Russell8f6d0372010-08-05 12:59:09 -06001625 const struct load_info *info,
Kay Sievers270a6c42007-01-18 13:26:15 +01001626 struct kernel_param *kparam,
1627 unsigned int num_params)
1628{
1629 int err;
1630
Rusty Russell80a3d1b2010-06-05 11:17:36 -06001631 err = mod_sysfs_init(mod);
1632 if (err)
1633 goto out;
1634
Greg Kroah-Hartman4ff6abf2007-11-05 22:24:43 -08001635 mod->holders_dir = kobject_create_and_add("holders", &mod->mkobj.kobj);
Akinobu Mita240936e2007-04-26 00:12:09 -07001636 if (!mod->holders_dir) {
1637 err = -ENOMEM;
Kay Sievers270a6c42007-01-18 13:26:15 +01001638 goto out_unreg;
Akinobu Mita240936e2007-04-26 00:12:09 -07001639 }
Kay Sievers270a6c42007-01-18 13:26:15 +01001640
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641 err = module_param_sysfs_setup(mod, kparam, num_params);
1642 if (err)
Kay Sievers270a6c42007-01-18 13:26:15 +01001643 goto out_unreg_holders;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644
Matt Domschc988d2b2005-06-23 22:05:15 -07001645 err = module_add_modinfo_attrs(mod);
1646 if (err)
Kay Sieverse17e0f52006-11-24 12:15:25 +01001647 goto out_unreg_param;
Matt Domschc988d2b2005-06-23 22:05:15 -07001648
Rusty Russell80a3d1b2010-06-05 11:17:36 -06001649 add_usage_links(mod);
Rusty Russell8f6d0372010-08-05 12:59:09 -06001650 add_sect_attrs(mod, info);
1651 add_notes_attrs(mod, info);
Rusty Russell80a3d1b2010-06-05 11:17:36 -06001652
Kay Sieverse17e0f52006-11-24 12:15:25 +01001653 kobject_uevent(&mod->mkobj.kobj, KOBJ_ADD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654 return 0;
1655
Kay Sieverse17e0f52006-11-24 12:15:25 +01001656out_unreg_param:
1657 module_param_sysfs_remove(mod);
Kay Sievers270a6c42007-01-18 13:26:15 +01001658out_unreg_holders:
Greg Kroah-Hartman78a2d902007-12-20 08:13:05 -08001659 kobject_put(mod->holders_dir);
Kay Sievers270a6c42007-01-18 13:26:15 +01001660out_unreg:
Kay Sieverse17e0f52006-11-24 12:15:25 +01001661 kobject_put(&mod->mkobj.kobj);
Rusty Russell80a3d1b2010-06-05 11:17:36 -06001662out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663 return err;
1664}
Denis V. Lunev34e4e2f2008-05-20 13:59:48 +04001665
1666static void mod_sysfs_fini(struct module *mod)
1667{
Rusty Russell8f6d0372010-08-05 12:59:09 -06001668 remove_notes_attrs(mod);
1669 remove_sect_attrs(mod);
Denis V. Lunev34e4e2f2008-05-20 13:59:48 +04001670 kobject_put(&mod->mkobj.kobj);
1671}
1672
Rusty Russell8f6d0372010-08-05 12:59:09 -06001673#else /* !CONFIG_SYSFS */
Denis V. Lunev34e4e2f2008-05-20 13:59:48 +04001674
Rusty Russell8f6d0372010-08-05 12:59:09 -06001675static int mod_sysfs_setup(struct module *mod,
1676 const struct load_info *info,
Rusty Russell6407ebb22010-06-05 11:17:36 -06001677 struct kernel_param *kparam,
1678 unsigned int num_params)
1679{
1680 return 0;
1681}
1682
Denis V. Lunev34e4e2f2008-05-20 13:59:48 +04001683static void mod_sysfs_fini(struct module *mod)
1684{
1685}
1686
Rusty Russell36b03602010-08-05 12:59:09 -06001687static void module_remove_modinfo_attrs(struct module *mod)
1688{
1689}
1690
Rusty Russell80a3d1b2010-06-05 11:17:36 -06001691static void del_usage_links(struct module *mod)
1692{
1693}
1694
Denis V. Lunev34e4e2f2008-05-20 13:59:48 +04001695#endif /* CONFIG_SYSFS */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696
Rusty Russell36b03602010-08-05 12:59:09 -06001697static void mod_sysfs_teardown(struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698{
Rusty Russell80a3d1b2010-06-05 11:17:36 -06001699 del_usage_links(mod);
Matt Domschc988d2b2005-06-23 22:05:15 -07001700 module_remove_modinfo_attrs(mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701 module_param_sysfs_remove(mod);
Greg Kroah-Hartman78a2d902007-12-20 08:13:05 -08001702 kobject_put(mod->mkobj.drivers_dir);
1703 kobject_put(mod->holders_dir);
Denis V. Lunev34e4e2f2008-05-20 13:59:48 +04001704 mod_sysfs_fini(mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705}
1706
1707/*
1708 * unlink the module with the whole machine is stopped with interrupts off
1709 * - this defends against kallsyms not taking locks
1710 */
1711static int __unlink_module(void *_mod)
1712{
1713 struct module *mod = _mod;
1714 list_del(&mod->list);
Linus Torvalds53363772010-10-05 11:29:27 -07001715 module_bug_cleanup(mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716 return 0;
1717}
1718
matthieu castet84e1c6b2010-11-16 22:35:16 +01001719#ifdef CONFIG_DEBUG_SET_MODULE_RONX
1720/*
1721 * LKM RO/NX protection: protect module's text/ro-data
1722 * from modification and any data from execution.
1723 */
1724void set_page_attributes(void *start, void *end, int (*set)(unsigned long start, int num_pages))
1725{
1726 unsigned long begin_pfn = PFN_DOWN((unsigned long)start);
1727 unsigned long end_pfn = PFN_DOWN((unsigned long)end);
1728
1729 if (end_pfn > begin_pfn)
1730 set(begin_pfn << PAGE_SHIFT, end_pfn - begin_pfn);
1731}
1732
1733static void set_section_ro_nx(void *base,
1734 unsigned long text_size,
1735 unsigned long ro_size,
1736 unsigned long total_size)
1737{
1738 /* begin and end PFNs of the current subsection */
1739 unsigned long begin_pfn;
1740 unsigned long end_pfn;
1741
1742 /*
1743 * Set RO for module text and RO-data:
1744 * - Always protect first page.
1745 * - Do not protect last partial page.
1746 */
1747 if (ro_size > 0)
1748 set_page_attributes(base, base + ro_size, set_memory_ro);
1749
1750 /*
1751 * Set NX permissions for module data:
1752 * - Do not protect first partial page.
1753 * - Always protect last page.
1754 */
1755 if (total_size > text_size) {
1756 begin_pfn = PFN_UP((unsigned long)base + text_size);
1757 end_pfn = PFN_UP((unsigned long)base + total_size);
1758 if (end_pfn > begin_pfn)
1759 set_memory_nx(begin_pfn << PAGE_SHIFT, end_pfn - begin_pfn);
1760 }
1761}
1762
Jan Glauber01526ed2011-05-19 16:55:26 -06001763static void unset_module_core_ro_nx(struct module *mod)
matthieu castet84e1c6b2010-11-16 22:35:16 +01001764{
Jan Glauber01526ed2011-05-19 16:55:26 -06001765 set_page_attributes(mod->module_core + mod->core_text_size,
1766 mod->module_core + mod->core_size,
1767 set_memory_x);
1768 set_page_attributes(mod->module_core,
1769 mod->module_core + mod->core_ro_size,
1770 set_memory_rw);
1771}
1772
1773static void unset_module_init_ro_nx(struct module *mod)
1774{
1775 set_page_attributes(mod->module_init + mod->init_text_size,
1776 mod->module_init + mod->init_size,
1777 set_memory_x);
1778 set_page_attributes(mod->module_init,
1779 mod->module_init + mod->init_ro_size,
1780 set_memory_rw);
matthieu castet84e1c6b2010-11-16 22:35:16 +01001781}
1782
1783/* Iterate through all modules and set each module's text as RW */
Daniel J Blueman5d05c702011-03-08 22:01:47 +08001784void set_all_modules_text_rw(void)
matthieu castet84e1c6b2010-11-16 22:35:16 +01001785{
1786 struct module *mod;
1787
1788 mutex_lock(&module_mutex);
1789 list_for_each_entry_rcu(mod, &modules, list) {
1790 if ((mod->module_core) && (mod->core_text_size)) {
1791 set_page_attributes(mod->module_core,
1792 mod->module_core + mod->core_text_size,
1793 set_memory_rw);
1794 }
1795 if ((mod->module_init) && (mod->init_text_size)) {
1796 set_page_attributes(mod->module_init,
1797 mod->module_init + mod->init_text_size,
1798 set_memory_rw);
1799 }
1800 }
1801 mutex_unlock(&module_mutex);
1802}
1803
1804/* Iterate through all modules and set each module's text as RO */
Daniel J Blueman5d05c702011-03-08 22:01:47 +08001805void set_all_modules_text_ro(void)
matthieu castet84e1c6b2010-11-16 22:35:16 +01001806{
1807 struct module *mod;
1808
1809 mutex_lock(&module_mutex);
1810 list_for_each_entry_rcu(mod, &modules, list) {
1811 if ((mod->module_core) && (mod->core_text_size)) {
1812 set_page_attributes(mod->module_core,
1813 mod->module_core + mod->core_text_size,
1814 set_memory_ro);
1815 }
1816 if ((mod->module_init) && (mod->init_text_size)) {
1817 set_page_attributes(mod->module_init,
1818 mod->module_init + mod->init_text_size,
1819 set_memory_ro);
1820 }
1821 }
1822 mutex_unlock(&module_mutex);
1823}
1824#else
1825static 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 -06001826static void unset_module_core_ro_nx(struct module *mod) { }
1827static void unset_module_init_ro_nx(struct module *mod) { }
matthieu castet84e1c6b2010-11-16 22:35:16 +01001828#endif
1829
Jonas Bonn74e08fc2011-06-30 21:22:11 +02001830void __weak module_free(struct module *mod, void *module_region)
1831{
1832 vfree(module_region);
1833}
1834
1835void __weak module_arch_cleanup(struct module *mod)
1836{
1837}
1838
Rusty Russell75676502010-06-05 11:17:36 -06001839/* Free a module, remove from lists, etc. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840static void free_module(struct module *mod)
1841{
Li Zefan7ead8b82009-08-17 16:56:28 +08001842 trace_module_free(mod);
1843
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844 /* Delete from various lists */
Rusty Russell75676502010-06-05 11:17:36 -06001845 mutex_lock(&module_mutex);
Rusty Russell9b1a4d32008-07-28 12:16:30 -05001846 stop_machine(__unlink_module, mod, NULL);
Rusty Russell75676502010-06-05 11:17:36 -06001847 mutex_unlock(&module_mutex);
Rusty Russell36b03602010-08-05 12:59:09 -06001848 mod_sysfs_teardown(mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849
Jason Baronb82bab4b2010-07-27 13:18:01 -07001850 /* Remove dynamic debug info */
1851 ddebug_remove_module(mod->name);
1852
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853 /* Arch-specific cleanup. */
1854 module_arch_cleanup(mod);
1855
1856 /* Module unload stuff */
1857 module_unload_free(mod);
1858
Rusty Russelle180a6b2009-03-31 13:05:29 -06001859 /* Free any allocated parameters. */
1860 destroy_params(mod->kp, mod->num_kp);
1861
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862 /* This may be NULL, but that's OK */
Jan Glauber01526ed2011-05-19 16:55:26 -06001863 unset_module_init_ro_nx(mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864 module_free(mod, mod->module_init);
1865 kfree(mod->args);
Tejun Heo259354d2010-03-10 18:56:10 +09001866 percpu_modfree(mod);
Rusty Russell9f85a4b2010-08-05 12:59:04 -06001867
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001868 /* Free lock-classes: */
1869 lockdep_free_key_range(mod->module_core, mod->core_size);
1870
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871 /* Finally, free the core (containing the module structure) */
Jan Glauber01526ed2011-05-19 16:55:26 -06001872 unset_module_core_ro_nx(mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873 module_free(mod, mod->module_core);
Bernd Schmidteb8cdec2009-09-21 17:03:57 -07001874
1875#ifdef CONFIG_MPU
1876 update_protections(current->mm);
1877#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878}
1879
1880void *__symbol_get(const char *symbol)
1881{
1882 struct module *owner;
Tim Abbott414fd312008-12-05 19:03:56 -05001883 const struct kernel_symbol *sym;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884
Rusty Russell24da1cb2007-07-15 23:41:46 -07001885 preempt_disable();
Tim Abbott414fd312008-12-05 19:03:56 -05001886 sym = find_symbol(symbol, &owner, NULL, true, true);
1887 if (sym && strong_try_module_get(owner))
1888 sym = NULL;
Rusty Russell24da1cb2007-07-15 23:41:46 -07001889 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890
Tim Abbott414fd312008-12-05 19:03:56 -05001891 return sym ? (void *)sym->value : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892}
1893EXPORT_SYMBOL_GPL(__symbol_get);
1894
Ashutosh Naikeea8b542006-01-08 01:04:25 -08001895/*
1896 * Ensure that an exported symbol [global namespace] does not already exist
Robert P. J. Day02a3e592007-05-09 07:26:28 +02001897 * in the kernel or in some other module's exported symbol table.
Rusty Russellbe593f42010-06-05 11:17:37 -06001898 *
1899 * You must hold the module_mutex.
Ashutosh Naikeea8b542006-01-08 01:04:25 -08001900 */
1901static int verify_export_symbols(struct module *mod)
1902{
Rusty Russellb2111042008-05-01 21:15:00 -05001903 unsigned int i;
Ashutosh Naikeea8b542006-01-08 01:04:25 -08001904 struct module *owner;
Rusty Russellb2111042008-05-01 21:15:00 -05001905 const struct kernel_symbol *s;
1906 struct {
1907 const struct kernel_symbol *sym;
1908 unsigned int num;
1909 } arr[] = {
1910 { mod->syms, mod->num_syms },
1911 { mod->gpl_syms, mod->num_gpl_syms },
1912 { mod->gpl_future_syms, mod->num_gpl_future_syms },
Denys Vlasenkof7f5b672008-07-22 19:24:26 -05001913#ifdef CONFIG_UNUSED_SYMBOLS
Rusty Russellb2111042008-05-01 21:15:00 -05001914 { mod->unused_syms, mod->num_unused_syms },
1915 { mod->unused_gpl_syms, mod->num_unused_gpl_syms },
Denys Vlasenkof7f5b672008-07-22 19:24:26 -05001916#endif
Rusty Russellb2111042008-05-01 21:15:00 -05001917 };
Ashutosh Naikeea8b542006-01-08 01:04:25 -08001918
Rusty Russellb2111042008-05-01 21:15:00 -05001919 for (i = 0; i < ARRAY_SIZE(arr); i++) {
1920 for (s = arr[i].sym; s < arr[i].sym + arr[i].num; s++) {
Rusty Russellbe593f42010-06-05 11:17:37 -06001921 if (find_symbol(s->name, &owner, NULL, true, false)) {
Rusty Russellb2111042008-05-01 21:15:00 -05001922 printk(KERN_ERR
1923 "%s: exports duplicate symbol %s"
1924 " (owned by %s)\n",
1925 mod->name, s->name, module_name(owner));
1926 return -ENOEXEC;
1927 }
Ashutosh Naikeea8b542006-01-08 01:04:25 -08001928 }
Rusty Russellb2111042008-05-01 21:15:00 -05001929 }
1930 return 0;
Ashutosh Naikeea8b542006-01-08 01:04:25 -08001931}
1932
Matti Linnanvuori9a4b9702007-11-08 08:37:38 -08001933/* Change all symbols so that st_value encodes the pointer directly. */
Rusty Russell49668682010-08-05 12:59:10 -06001934static int simplify_symbols(struct module *mod, const struct load_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935{
Rusty Russell49668682010-08-05 12:59:10 -06001936 Elf_Shdr *symsec = &info->sechdrs[info->index.sym];
1937 Elf_Sym *sym = (void *)symsec->sh_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938 unsigned long secbase;
Rusty Russell49668682010-08-05 12:59:10 -06001939 unsigned int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940 int ret = 0;
Tim Abbott414fd312008-12-05 19:03:56 -05001941 const struct kernel_symbol *ksym;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942
Rusty Russell49668682010-08-05 12:59:10 -06001943 for (i = 1; i < symsec->sh_size / sizeof(Elf_Sym); i++) {
1944 const char *name = info->strtab + sym[i].st_name;
1945
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946 switch (sym[i].st_shndx) {
1947 case SHN_COMMON:
1948 /* We compiled with -fno-common. These are not
1949 supposed to happen. */
Jim Cromie5e124162011-12-06 12:11:31 -07001950 pr_debug("Common symbol: %s\n", name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951 printk("%s: please compile with -fno-common\n",
1952 mod->name);
1953 ret = -ENOEXEC;
1954 break;
1955
1956 case SHN_ABS:
1957 /* Don't need to do anything */
Jim Cromie5e124162011-12-06 12:11:31 -07001958 pr_debug("Absolute symbol: 0x%08lx\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959 (long)sym[i].st_value);
1960 break;
1961
1962 case SHN_UNDEF:
Rusty Russell49668682010-08-05 12:59:10 -06001963 ksym = resolve_symbol_wait(mod, info, name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964 /* Ok if resolved. */
Rusty Russell9bea7f22010-06-05 11:17:37 -06001965 if (ksym && !IS_ERR(ksym)) {
Tim Abbott414fd312008-12-05 19:03:56 -05001966 sym[i].st_value = ksym->value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967 break;
Tim Abbott414fd312008-12-05 19:03:56 -05001968 }
1969
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970 /* Ok if weak. */
Rusty Russell9bea7f22010-06-05 11:17:37 -06001971 if (!ksym && ELF_ST_BIND(sym[i].st_info) == STB_WEAK)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972 break;
1973
Rusty Russell9bea7f22010-06-05 11:17:37 -06001974 printk(KERN_WARNING "%s: Unknown symbol %s (err %li)\n",
Rusty Russell49668682010-08-05 12:59:10 -06001975 mod->name, name, PTR_ERR(ksym));
Rusty Russell9bea7f22010-06-05 11:17:37 -06001976 ret = PTR_ERR(ksym) ?: -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977 break;
1978
1979 default:
1980 /* Divert to percpu allocation if a percpu var. */
Rusty Russell49668682010-08-05 12:59:10 -06001981 if (sym[i].st_shndx == info->index.pcpu)
Tejun Heo259354d2010-03-10 18:56:10 +09001982 secbase = (unsigned long)mod_percpu(mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983 else
Rusty Russell49668682010-08-05 12:59:10 -06001984 secbase = info->sechdrs[sym[i].st_shndx].sh_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985 sym[i].st_value += secbase;
1986 break;
1987 }
1988 }
1989
1990 return ret;
1991}
1992
Rusty Russell49668682010-08-05 12:59:10 -06001993static int apply_relocations(struct module *mod, const struct load_info *info)
Rusty Russell22e268e2010-08-05 12:59:05 -06001994{
1995 unsigned int i;
1996 int err = 0;
1997
1998 /* Now do relocations. */
Rusty Russell49668682010-08-05 12:59:10 -06001999 for (i = 1; i < info->hdr->e_shnum; i++) {
2000 unsigned int infosec = info->sechdrs[i].sh_info;
Rusty Russell22e268e2010-08-05 12:59:05 -06002001
2002 /* Not a valid relocation section? */
Rusty Russell49668682010-08-05 12:59:10 -06002003 if (infosec >= info->hdr->e_shnum)
Rusty Russell22e268e2010-08-05 12:59:05 -06002004 continue;
2005
2006 /* Don't bother with non-allocated sections */
Rusty Russell49668682010-08-05 12:59:10 -06002007 if (!(info->sechdrs[infosec].sh_flags & SHF_ALLOC))
Rusty Russell22e268e2010-08-05 12:59:05 -06002008 continue;
2009
Rusty Russell49668682010-08-05 12:59:10 -06002010 if (info->sechdrs[i].sh_type == SHT_REL)
2011 err = apply_relocate(info->sechdrs, info->strtab,
2012 info->index.sym, i, mod);
2013 else if (info->sechdrs[i].sh_type == SHT_RELA)
2014 err = apply_relocate_add(info->sechdrs, info->strtab,
2015 info->index.sym, i, mod);
Rusty Russell22e268e2010-08-05 12:59:05 -06002016 if (err < 0)
2017 break;
2018 }
2019 return err;
2020}
2021
Helge Deller088af9a2008-12-31 12:31:18 +01002022/* Additional bytes needed by arch in front of individual sections */
2023unsigned int __weak arch_mod_section_prepend(struct module *mod,
2024 unsigned int section)
2025{
2026 /* default implementation just returns zero */
2027 return 0;
2028}
2029
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030/* Update size with this section: return offset. */
Helge Deller088af9a2008-12-31 12:31:18 +01002031static long get_offset(struct module *mod, unsigned int *size,
2032 Elf_Shdr *sechdr, unsigned int section)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033{
2034 long ret;
2035
Helge Deller088af9a2008-12-31 12:31:18 +01002036 *size += arch_mod_section_prepend(mod, section);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037 ret = ALIGN(*size, sechdr->sh_addralign ?: 1);
2038 *size = ret + sechdr->sh_size;
2039 return ret;
2040}
2041
2042/* Lay out the SHF_ALLOC sections in a way not dissimilar to how ld
2043 might -- code, read-only data, read-write data, small data. Tally
2044 sizes, and place the offsets into sh_entsize fields: high bit means it
2045 belongs in init. */
Rusty Russell49668682010-08-05 12:59:10 -06002046static void layout_sections(struct module *mod, struct load_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047{
2048 static unsigned long const masks[][2] = {
2049 /* NOTE: all executable code must be the first section
2050 * in this array; otherwise modify the text_size
2051 * finder in the two loops below */
2052 { SHF_EXECINSTR | SHF_ALLOC, ARCH_SHF_SMALL },
2053 { SHF_ALLOC, SHF_WRITE | ARCH_SHF_SMALL },
2054 { SHF_WRITE | SHF_ALLOC, ARCH_SHF_SMALL },
2055 { ARCH_SHF_SMALL | SHF_ALLOC, 0 }
2056 };
2057 unsigned int m, i;
2058
Rusty Russell49668682010-08-05 12:59:10 -06002059 for (i = 0; i < info->hdr->e_shnum; i++)
2060 info->sechdrs[i].sh_entsize = ~0UL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061
Jim Cromie5e124162011-12-06 12:11:31 -07002062 pr_debug("Core section allocation order:\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063 for (m = 0; m < ARRAY_SIZE(masks); ++m) {
Rusty Russell49668682010-08-05 12:59:10 -06002064 for (i = 0; i < info->hdr->e_shnum; ++i) {
2065 Elf_Shdr *s = &info->sechdrs[i];
2066 const char *sname = info->secstrings + s->sh_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067
2068 if ((s->sh_flags & masks[m][0]) != masks[m][0]
2069 || (s->sh_flags & masks[m][1])
2070 || s->sh_entsize != ~0UL
Rusty Russell49668682010-08-05 12:59:10 -06002071 || strstarts(sname, ".init"))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072 continue;
Helge Deller088af9a2008-12-31 12:31:18 +01002073 s->sh_entsize = get_offset(mod, &mod->core_size, s, i);
Jim Cromie5e124162011-12-06 12:11:31 -07002074 pr_debug("\t%s\n", sname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075 }
matthieu castet84e1c6b2010-11-16 22:35:16 +01002076 switch (m) {
2077 case 0: /* executable */
2078 mod->core_size = debug_align(mod->core_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079 mod->core_text_size = mod->core_size;
matthieu castet84e1c6b2010-11-16 22:35:16 +01002080 break;
2081 case 1: /* RO: text and ro-data */
2082 mod->core_size = debug_align(mod->core_size);
2083 mod->core_ro_size = mod->core_size;
2084 break;
2085 case 3: /* whole core */
2086 mod->core_size = debug_align(mod->core_size);
2087 break;
2088 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089 }
2090
Jim Cromie5e124162011-12-06 12:11:31 -07002091 pr_debug("Init section allocation order:\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092 for (m = 0; m < ARRAY_SIZE(masks); ++m) {
Rusty Russell49668682010-08-05 12:59:10 -06002093 for (i = 0; i < info->hdr->e_shnum; ++i) {
2094 Elf_Shdr *s = &info->sechdrs[i];
2095 const char *sname = info->secstrings + s->sh_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096
2097 if ((s->sh_flags & masks[m][0]) != masks[m][0]
2098 || (s->sh_flags & masks[m][1])
2099 || s->sh_entsize != ~0UL
Rusty Russell49668682010-08-05 12:59:10 -06002100 || !strstarts(sname, ".init"))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101 continue;
Helge Deller088af9a2008-12-31 12:31:18 +01002102 s->sh_entsize = (get_offset(mod, &mod->init_size, s, i)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103 | INIT_OFFSET_MASK);
Jim Cromie5e124162011-12-06 12:11:31 -07002104 pr_debug("\t%s\n", sname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105 }
matthieu castet84e1c6b2010-11-16 22:35:16 +01002106 switch (m) {
2107 case 0: /* executable */
2108 mod->init_size = debug_align(mod->init_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109 mod->init_text_size = mod->init_size;
matthieu castet84e1c6b2010-11-16 22:35:16 +01002110 break;
2111 case 1: /* RO: text and ro-data */
2112 mod->init_size = debug_align(mod->init_size);
2113 mod->init_ro_size = mod->init_size;
2114 break;
2115 case 3: /* whole init */
2116 mod->init_size = debug_align(mod->init_size);
2117 break;
2118 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119 }
2120}
2121
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122static void set_license(struct module *mod, const char *license)
2123{
2124 if (!license)
2125 license = "unspecified";
2126
Florin Malitafa3ba2e82006-10-11 01:21:48 -07002127 if (!license_is_gpl_compatible(license)) {
Andi Kleen25ddbb12008-10-15 22:01:41 -07002128 if (!test_taint(TAINT_PROPRIETARY_MODULE))
Jan Dittmer1d4d2622006-10-28 10:38:38 -07002129 printk(KERN_WARNING "%s: module license '%s' taints "
Florin Malitafa3ba2e82006-10-11 01:21:48 -07002130 "kernel.\n", mod->name, license);
2131 add_taint_module(mod, TAINT_PROPRIETARY_MODULE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132 }
2133}
2134
2135/* Parse tag=value strings from .modinfo section */
2136static char *next_string(char *string, unsigned long *secsize)
2137{
2138 /* Skip non-zero chars */
2139 while (string[0]) {
2140 string++;
2141 if ((*secsize)-- <= 1)
2142 return NULL;
2143 }
2144
2145 /* Skip any zero padding. */
2146 while (!string[0]) {
2147 string++;
2148 if ((*secsize)-- <= 1)
2149 return NULL;
2150 }
2151 return string;
2152}
2153
Rusty Russell49668682010-08-05 12:59:10 -06002154static char *get_modinfo(struct load_info *info, const char *tag)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155{
2156 char *p;
2157 unsigned int taglen = strlen(tag);
Rusty Russell49668682010-08-05 12:59:10 -06002158 Elf_Shdr *infosec = &info->sechdrs[info->index.info];
2159 unsigned long size = infosec->sh_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160
Rusty Russell49668682010-08-05 12:59:10 -06002161 for (p = (char *)infosec->sh_addr; p; p = next_string(p, &size)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162 if (strncmp(p, tag, taglen) == 0 && p[taglen] == '=')
2163 return p + taglen + 1;
2164 }
2165 return NULL;
2166}
2167
Rusty Russell49668682010-08-05 12:59:10 -06002168static void setup_modinfo(struct module *mod, struct load_info *info)
Matt Domschc988d2b2005-06-23 22:05:15 -07002169{
2170 struct module_attribute *attr;
2171 int i;
2172
2173 for (i = 0; (attr = modinfo_attrs[i]); i++) {
2174 if (attr->setup)
Rusty Russell49668682010-08-05 12:59:10 -06002175 attr->setup(mod, get_modinfo(info, attr->attr.name));
Matt Domschc988d2b2005-06-23 22:05:15 -07002176 }
2177}
Matt Domschc988d2b2005-06-23 22:05:15 -07002178
Rusty Russella263f7762009-09-25 00:32:58 -06002179static void free_modinfo(struct module *mod)
2180{
2181 struct module_attribute *attr;
2182 int i;
2183
2184 for (i = 0; (attr = modinfo_attrs[i]); i++) {
2185 if (attr->free)
2186 attr->free(mod);
2187 }
2188}
2189
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190#ifdef CONFIG_KALLSYMS
WANG Cong15bba372008-07-24 15:41:48 +01002191
2192/* lookup symbol in given range of kernel_symbols */
2193static const struct kernel_symbol *lookup_symbol(const char *name,
2194 const struct kernel_symbol *start,
2195 const struct kernel_symbol *stop)
2196{
Alessio Igor Bogani9d634872011-05-18 22:35:59 +02002197 return bsearch(name, start, stop - start,
2198 sizeof(struct kernel_symbol), cmp_name);
WANG Cong15bba372008-07-24 15:41:48 +01002199}
2200
Tim Abbottca4787b2009-01-05 08:40:10 -06002201static int is_exported(const char *name, unsigned long value,
2202 const struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203{
Tim Abbottca4787b2009-01-05 08:40:10 -06002204 const struct kernel_symbol *ks;
2205 if (!mod)
2206 ks = lookup_symbol(name, __start___ksymtab, __stop___ksymtab);
Sam Ravnborg3fd68052006-02-08 21:16:45 +01002207 else
Tim Abbottca4787b2009-01-05 08:40:10 -06002208 ks = lookup_symbol(name, mod->syms, mod->syms + mod->num_syms);
2209 return ks != NULL && ks->value == value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002210}
2211
2212/* As per nm */
Rusty Russelleded41c2010-08-05 12:59:07 -06002213static char elf_type(const Elf_Sym *sym, const struct load_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002214{
Rusty Russelleded41c2010-08-05 12:59:07 -06002215 const Elf_Shdr *sechdrs = info->sechdrs;
2216
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217 if (ELF_ST_BIND(sym->st_info) == STB_WEAK) {
2218 if (ELF_ST_TYPE(sym->st_info) == STT_OBJECT)
2219 return 'v';
2220 else
2221 return 'w';
2222 }
2223 if (sym->st_shndx == SHN_UNDEF)
2224 return 'U';
2225 if (sym->st_shndx == SHN_ABS)
2226 return 'a';
2227 if (sym->st_shndx >= SHN_LORESERVE)
2228 return '?';
2229 if (sechdrs[sym->st_shndx].sh_flags & SHF_EXECINSTR)
2230 return 't';
2231 if (sechdrs[sym->st_shndx].sh_flags & SHF_ALLOC
2232 && sechdrs[sym->st_shndx].sh_type != SHT_NOBITS) {
2233 if (!(sechdrs[sym->st_shndx].sh_flags & SHF_WRITE))
2234 return 'r';
2235 else if (sechdrs[sym->st_shndx].sh_flags & ARCH_SHF_SMALL)
2236 return 'g';
2237 else
2238 return 'd';
2239 }
2240 if (sechdrs[sym->st_shndx].sh_type == SHT_NOBITS) {
2241 if (sechdrs[sym->st_shndx].sh_flags & ARCH_SHF_SMALL)
2242 return 's';
2243 else
2244 return 'b';
2245 }
Rusty Russelleded41c2010-08-05 12:59:07 -06002246 if (strstarts(info->secstrings + sechdrs[sym->st_shndx].sh_name,
2247 ".debug")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002248 return 'n';
Rusty Russelleded41c2010-08-05 12:59:07 -06002249 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002250 return '?';
2251}
2252
Jan Beulich4a496222009-07-06 14:50:42 +01002253static bool is_core_symbol(const Elf_Sym *src, const Elf_Shdr *sechdrs,
2254 unsigned int shnum)
2255{
2256 const Elf_Shdr *sec;
2257
2258 if (src->st_shndx == SHN_UNDEF
2259 || src->st_shndx >= shnum
2260 || !src->st_name)
2261 return false;
2262
2263 sec = sechdrs + src->st_shndx;
2264 if (!(sec->sh_flags & SHF_ALLOC)
2265#ifndef CONFIG_KALLSYMS_ALL
2266 || !(sec->sh_flags & SHF_EXECINSTR)
2267#endif
2268 || (sec->sh_entsize & INIT_OFFSET_MASK))
2269 return false;
2270
2271 return true;
2272}
2273
Kevin Cernekee48fd1182012-01-13 09:32:14 +10302274/*
2275 * We only allocate and copy the strings needed by the parts of symtab
2276 * we keep. This is simple, but has the effect of making multiple
2277 * copies of duplicates. We could be more sophisticated, see
2278 * linux-kernel thread starting with
2279 * <73defb5e4bca04a6431392cc341112b1@localhost>.
2280 */
Rusty Russell49668682010-08-05 12:59:10 -06002281static void layout_symtab(struct module *mod, struct load_info *info)
Jan Beulich4a496222009-07-06 14:50:42 +01002282{
Rusty Russell49668682010-08-05 12:59:10 -06002283 Elf_Shdr *symsect = info->sechdrs + info->index.sym;
2284 Elf_Shdr *strsect = info->sechdrs + info->index.str;
Jan Beulich4a496222009-07-06 14:50:42 +01002285 const Elf_Sym *src;
Kevin Cernekee48fd1182012-01-13 09:32:14 +10302286 unsigned int i, nsrc, ndst, strtab_size;
Jan Beulich4a496222009-07-06 14:50:42 +01002287
2288 /* Put symbol section at end of init part of module. */
2289 symsect->sh_flags |= SHF_ALLOC;
2290 symsect->sh_entsize = get_offset(mod, &mod->init_size, symsect,
Rusty Russell49668682010-08-05 12:59:10 -06002291 info->index.sym) | INIT_OFFSET_MASK;
Jim Cromie5e124162011-12-06 12:11:31 -07002292 pr_debug("\t%s\n", info->secstrings + symsect->sh_name);
Jan Beulich4a496222009-07-06 14:50:42 +01002293
Rusty Russell49668682010-08-05 12:59:10 -06002294 src = (void *)info->hdr + symsect->sh_offset;
Jan Beulich4a496222009-07-06 14:50:42 +01002295 nsrc = symsect->sh_size / sizeof(*src);
Kevin Cernekee70b1e9162011-11-12 19:08:55 -08002296
Rusty Russell59ef28b2012-10-25 10:49:25 +10302297 /* strtab always starts with a nul, so offset 0 is the empty string. */
2298 strtab_size = 1;
2299
Kevin Cernekee48fd1182012-01-13 09:32:14 +10302300 /* Compute total space required for the core symbols' strtab. */
Rusty Russell59ef28b2012-10-25 10:49:25 +10302301 for (ndst = i = 0; i < nsrc; i++) {
2302 if (i == 0 ||
2303 is_core_symbol(src+i, info->sechdrs, info->hdr->e_shnum)) {
2304 strtab_size += strlen(&info->strtab[src[i].st_name])+1;
Kevin Cernekee48fd1182012-01-13 09:32:14 +10302305 ndst++;
Jan Beulich554bdfe2009-07-06 14:51:44 +01002306 }
Rusty Russell59ef28b2012-10-25 10:49:25 +10302307 }
Jan Beulich4a496222009-07-06 14:50:42 +01002308
2309 /* Append room for core symbols at end of core part. */
Rusty Russell49668682010-08-05 12:59:10 -06002310 info->symoffs = ALIGN(mod->core_size, symsect->sh_addralign ?: 1);
Kevin Cernekee48fd1182012-01-13 09:32:14 +10302311 info->stroffs = mod->core_size = info->symoffs + ndst * sizeof(Elf_Sym);
2312 mod->core_size += strtab_size;
Jan Beulich4a496222009-07-06 14:50:42 +01002313
Jan Beulich554bdfe2009-07-06 14:51:44 +01002314 /* Put string table section at end of init part of module. */
2315 strsect->sh_flags |= SHF_ALLOC;
2316 strsect->sh_entsize = get_offset(mod, &mod->init_size, strsect,
Rusty Russell49668682010-08-05 12:59:10 -06002317 info->index.str) | INIT_OFFSET_MASK;
Jim Cromie5e124162011-12-06 12:11:31 -07002318 pr_debug("\t%s\n", info->secstrings + strsect->sh_name);
Jan Beulich4a496222009-07-06 14:50:42 +01002319}
2320
Rusty Russell811d66a2010-08-05 12:59:12 -06002321static void add_kallsyms(struct module *mod, const struct load_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002322{
Jan Beulich4a496222009-07-06 14:50:42 +01002323 unsigned int i, ndst;
2324 const Elf_Sym *src;
2325 Elf_Sym *dst;
Jan Beulich554bdfe2009-07-06 14:51:44 +01002326 char *s;
Rusty Russelleded41c2010-08-05 12:59:07 -06002327 Elf_Shdr *symsec = &info->sechdrs[info->index.sym];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002328
Rusty Russelleded41c2010-08-05 12:59:07 -06002329 mod->symtab = (void *)symsec->sh_addr;
2330 mod->num_symtab = symsec->sh_size / sizeof(Elf_Sym);
Rusty Russell511ca6a2010-08-05 12:59:08 -06002331 /* Make sure we get permanent strtab: don't use info->strtab. */
2332 mod->strtab = (void *)info->sechdrs[info->index.str].sh_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002333
2334 /* Set types up while we still have access to sections. */
2335 for (i = 0; i < mod->num_symtab; i++)
Rusty Russelleded41c2010-08-05 12:59:07 -06002336 mod->symtab[i].st_info = elf_type(&mod->symtab[i], info);
Jan Beulich4a496222009-07-06 14:50:42 +01002337
Rusty Russelld9131882010-08-05 12:59:08 -06002338 mod->core_symtab = dst = mod->module_core + info->symoffs;
Kevin Cernekee48fd1182012-01-13 09:32:14 +10302339 mod->core_strtab = s = mod->module_core + info->stroffs;
Jan Beulich4a496222009-07-06 14:50:42 +01002340 src = mod->symtab;
Kevin Cernekee48fd1182012-01-13 09:32:14 +10302341 *s++ = 0;
Rusty Russell59ef28b2012-10-25 10:49:25 +10302342 for (ndst = i = 0; i < mod->num_symtab; i++) {
2343 if (i == 0 ||
2344 is_core_symbol(src+i, info->sechdrs, info->hdr->e_shnum)) {
2345 dst[ndst] = src[i];
2346 dst[ndst++].st_name = s - mod->core_strtab;
2347 s += strlcpy(s, &mod->strtab[src[i].st_name],
2348 KSYM_NAME_LEN) + 1;
2349 }
Jan Beulich4a496222009-07-06 14:50:42 +01002350 }
2351 mod->core_num_syms = ndst;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002352}
2353#else
Rusty Russell49668682010-08-05 12:59:10 -06002354static inline void layout_symtab(struct module *mod, struct load_info *info)
Jan Beulich4a496222009-07-06 14:50:42 +01002355{
2356}
Paul Mundt3ae91c22009-10-01 15:43:54 -07002357
Michał Mirosławabbce902010-09-20 01:58:08 +02002358static void add_kallsyms(struct module *mod, const struct load_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002359{
2360}
2361#endif /* CONFIG_KALLSYMS */
2362
Jason Barone9d376f2009-02-05 11:51:38 -05002363static void dynamic_debug_setup(struct _ddebug *debug, unsigned int num)
Rusty Russell5e458cc2008-10-22 10:00:13 -05002364{
Rusty Russell811d66a2010-08-05 12:59:12 -06002365 if (!debug)
2366 return;
Jason Barone9d376f2009-02-05 11:51:38 -05002367#ifdef CONFIG_DYNAMIC_DEBUG
2368 if (ddebug_add_module(debug, num, debug->modname))
2369 printk(KERN_ERR "dynamic debug error adding module: %s\n",
2370 debug->modname);
2371#endif
Rusty Russell5e458cc2008-10-22 10:00:13 -05002372}
Jason Baron346e15b2008-08-12 16:46:19 -04002373
Yehuda Sadehff49d742010-07-03 13:07:35 +10002374static void dynamic_debug_remove(struct _ddebug *debug)
2375{
2376 if (debug)
2377 ddebug_remove_module(debug->modname);
2378}
2379
Jonas Bonn74e08fc2011-06-30 21:22:11 +02002380void * __weak module_alloc(unsigned long size)
2381{
2382 return size == 0 ? NULL : vmalloc_exec(size);
2383}
2384
Rusty Russell3a642e92008-07-22 19:24:28 -05002385static void *module_alloc_update_bounds(unsigned long size)
2386{
2387 void *ret = module_alloc(size);
2388
2389 if (ret) {
Rusty Russell75676502010-06-05 11:17:36 -06002390 mutex_lock(&module_mutex);
Rusty Russell3a642e92008-07-22 19:24:28 -05002391 /* Update module bounds. */
2392 if ((unsigned long)ret < module_addr_min)
2393 module_addr_min = (unsigned long)ret;
2394 if ((unsigned long)ret + size > module_addr_max)
2395 module_addr_max = (unsigned long)ret + size;
Rusty Russell75676502010-06-05 11:17:36 -06002396 mutex_unlock(&module_mutex);
Rusty Russell3a642e92008-07-22 19:24:28 -05002397 }
2398 return ret;
2399}
2400
Catalin Marinas4f2294b2009-06-11 13:23:20 +01002401#ifdef CONFIG_DEBUG_KMEMLEAK
Rusty Russell49668682010-08-05 12:59:10 -06002402static void kmemleak_load_module(const struct module *mod,
2403 const struct load_info *info)
Catalin Marinas4f2294b2009-06-11 13:23:20 +01002404{
2405 unsigned int i;
2406
2407 /* only scan the sections containing data */
Catalin Marinasc017b4b2009-10-28 13:33:09 +00002408 kmemleak_scan_area(mod, sizeof(struct module), GFP_KERNEL);
Catalin Marinas4f2294b2009-06-11 13:23:20 +01002409
Rusty Russell49668682010-08-05 12:59:10 -06002410 for (i = 1; i < info->hdr->e_shnum; i++) {
2411 const char *name = info->secstrings + info->sechdrs[i].sh_name;
2412 if (!(info->sechdrs[i].sh_flags & SHF_ALLOC))
Catalin Marinas4f2294b2009-06-11 13:23:20 +01002413 continue;
Rusty Russell49668682010-08-05 12:59:10 -06002414 if (!strstarts(name, ".data") && !strstarts(name, ".bss"))
Catalin Marinas4f2294b2009-06-11 13:23:20 +01002415 continue;
2416
Rusty Russell49668682010-08-05 12:59:10 -06002417 kmemleak_scan_area((void *)info->sechdrs[i].sh_addr,
2418 info->sechdrs[i].sh_size, GFP_KERNEL);
Catalin Marinas4f2294b2009-06-11 13:23:20 +01002419 }
2420}
2421#else
Rusty Russell49668682010-08-05 12:59:10 -06002422static inline void kmemleak_load_module(const struct module *mod,
2423 const struct load_info *info)
Catalin Marinas4f2294b2009-06-11 13:23:20 +01002424{
2425}
2426#endif
2427
Rusty Russell106a4ee2012-09-26 10:09:40 +01002428#ifdef CONFIG_MODULE_SIG
Kees Cook34e11692012-10-16 07:31:07 +10302429static int module_sig_check(struct load_info *info)
Rusty Russell106a4ee2012-09-26 10:09:40 +01002430{
2431 int err = -ENOKEY;
Kees Cook34e11692012-10-16 07:31:07 +10302432 const unsigned long markerlen = sizeof(MODULE_SIG_STRING) - 1;
2433 const void *mod = info->hdr;
Rusty Russell106a4ee2012-09-26 10:09:40 +01002434
Kees Cook34e11692012-10-16 07:31:07 +10302435 if (info->len > markerlen &&
2436 memcmp(mod + info->len - markerlen, MODULE_SIG_STRING, markerlen) == 0) {
David Howellscaabe242012-10-20 01:19:29 +01002437 /* We truncate the module to discard the signature */
Kees Cook34e11692012-10-16 07:31:07 +10302438 info->len -= markerlen;
2439 err = mod_verify_sig(mod, &info->len);
Rusty Russell106a4ee2012-09-26 10:09:40 +01002440 }
2441
2442 if (!err) {
2443 info->sig_ok = true;
2444 return 0;
2445 }
2446
2447 /* Not having a signature is only an error if we're strict. */
David Howells1d0059f2012-09-26 10:09:50 +01002448 if (err < 0 && fips_enabled)
2449 panic("Module verification failed with error %d in FIPS mode\n",
2450 err);
Rusty Russell106a4ee2012-09-26 10:09:40 +01002451 if (err == -ENOKEY && !sig_enforce)
2452 err = 0;
2453
2454 return err;
2455}
2456#else /* !CONFIG_MODULE_SIG */
Kees Cook34e11692012-10-16 07:31:07 +10302457static int module_sig_check(struct load_info *info)
Rusty Russell106a4ee2012-09-26 10:09:40 +01002458{
2459 return 0;
2460}
2461#endif /* !CONFIG_MODULE_SIG */
2462
Kees Cook34e11692012-10-16 07:31:07 +10302463/* Sanity checks against invalid binaries, wrong arch, weird elf version. */
2464static int elf_header_check(struct load_info *info)
Rusty Russell40dd2562010-08-05 12:59:03 -06002465{
Kees Cook34e11692012-10-16 07:31:07 +10302466 if (info->len < sizeof(*(info->hdr)))
2467 return -ENOEXEC;
Rusty Russell40dd2562010-08-05 12:59:03 -06002468
Kees Cook34e11692012-10-16 07:31:07 +10302469 if (memcmp(info->hdr->e_ident, ELFMAG, SELFMAG) != 0
2470 || info->hdr->e_type != ET_REL
2471 || !elf_check_arch(info->hdr)
2472 || info->hdr->e_shentsize != sizeof(Elf_Shdr))
2473 return -ENOEXEC;
2474
2475 if (info->hdr->e_shoff >= info->len
2476 || (info->hdr->e_shnum * sizeof(Elf_Shdr) >
2477 info->len - info->hdr->e_shoff))
2478 return -ENOEXEC;
2479
2480 return 0;
2481}
2482
2483/* Sets info->hdr and info->len. */
2484static int copy_module_from_user(const void __user *umod, unsigned long len,
2485 struct load_info *info)
2486{
2487 info->len = len;
2488 if (info->len < sizeof(*(info->hdr)))
Rusty Russell40dd2562010-08-05 12:59:03 -06002489 return -ENOEXEC;
2490
2491 /* Suck in entire file: we'll want most of it. */
Kees Cook34e11692012-10-16 07:31:07 +10302492 info->hdr = vmalloc(info->len);
2493 if (!info->hdr)
Rusty Russell40dd2562010-08-05 12:59:03 -06002494 return -ENOMEM;
2495
Kees Cook34e11692012-10-16 07:31:07 +10302496 if (copy_from_user(info->hdr, umod, info->len) != 0) {
2497 vfree(info->hdr);
2498 return -EFAULT;
Rusty Russell40dd2562010-08-05 12:59:03 -06002499 }
2500
Rusty Russell40dd2562010-08-05 12:59:03 -06002501 return 0;
Kees Cook34e11692012-10-16 07:31:07 +10302502}
Rusty Russell40dd2562010-08-05 12:59:03 -06002503
Kees Cook34e11692012-10-16 07:31:07 +10302504/* Sets info->hdr and info->len. */
2505static int copy_module_from_fd(int fd, struct load_info *info)
2506{
2507 struct file *file;
2508 int err;
2509 struct kstat stat;
2510 loff_t pos;
2511 ssize_t bytes = 0;
2512
2513 file = fget(fd);
2514 if (!file)
2515 return -ENOEXEC;
2516
2517 err = vfs_getattr(file->f_vfsmnt, file->f_dentry, &stat);
2518 if (err)
2519 goto out;
2520
2521 if (stat.size > INT_MAX) {
2522 err = -EFBIG;
2523 goto out;
2524 }
2525 info->hdr = vmalloc(stat.size);
2526 if (!info->hdr) {
2527 err = -ENOMEM;
2528 goto out;
2529 }
2530
2531 pos = 0;
2532 while (pos < stat.size) {
2533 bytes = kernel_read(file, pos, (char *)(info->hdr) + pos,
2534 stat.size - pos);
2535 if (bytes < 0) {
2536 vfree(info->hdr);
2537 err = bytes;
2538 goto out;
2539 }
2540 if (bytes == 0)
2541 break;
2542 pos += bytes;
2543 }
2544 info->len = pos;
2545
2546out:
2547 fput(file);
Rusty Russell40dd2562010-08-05 12:59:03 -06002548 return err;
2549}
2550
Rusty Russelld9131882010-08-05 12:59:08 -06002551static void free_copy(struct load_info *info)
2552{
Rusty Russelld9131882010-08-05 12:59:08 -06002553 vfree(info->hdr);
2554}
2555
Rusty Russell8b5f61a2010-08-05 12:59:06 -06002556static int rewrite_section_headers(struct load_info *info)
2557{
2558 unsigned int i;
2559
2560 /* This should always be true, but let's be sure. */
2561 info->sechdrs[0].sh_addr = 0;
2562
2563 for (i = 1; i < info->hdr->e_shnum; i++) {
2564 Elf_Shdr *shdr = &info->sechdrs[i];
2565 if (shdr->sh_type != SHT_NOBITS
2566 && info->len < shdr->sh_offset + shdr->sh_size) {
2567 printk(KERN_ERR "Module len %lu truncated\n",
2568 info->len);
2569 return -ENOEXEC;
2570 }
2571
2572 /* Mark all sections sh_addr with their address in the
2573 temporary image. */
2574 shdr->sh_addr = (size_t)info->hdr + shdr->sh_offset;
2575
2576#ifndef CONFIG_MODULE_UNLOAD
2577 /* Don't load .exit sections */
2578 if (strstarts(info->secstrings+shdr->sh_name, ".exit"))
2579 shdr->sh_flags &= ~(unsigned long)SHF_ALLOC;
2580#endif
Rusty Russell8b5f61a2010-08-05 12:59:06 -06002581 }
Rusty Russelld6df72a2010-08-05 12:59:07 -06002582
2583 /* Track but don't keep modinfo and version sections. */
Rusty Russell49668682010-08-05 12:59:10 -06002584 info->index.vers = find_sec(info, "__versions");
2585 info->index.info = find_sec(info, ".modinfo");
Rusty Russelld6df72a2010-08-05 12:59:07 -06002586 info->sechdrs[info->index.info].sh_flags &= ~(unsigned long)SHF_ALLOC;
2587 info->sechdrs[info->index.vers].sh_flags &= ~(unsigned long)SHF_ALLOC;
Rusty Russell8b5f61a2010-08-05 12:59:06 -06002588 return 0;
2589}
2590
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002591/*
2592 * Set up our basic convenience variables (pointers to section headers,
2593 * search for module section index etc), and do some basic section
2594 * verification.
2595 *
2596 * Return the temporary module pointer (we'll replace it with the final
2597 * one when we move the module sections around).
2598 */
2599static struct module *setup_load_info(struct load_info *info)
2600{
2601 unsigned int i;
Rusty Russell8b5f61a2010-08-05 12:59:06 -06002602 int err;
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002603 struct module *mod;
2604
2605 /* Set up the convenience variables */
2606 info->sechdrs = (void *)info->hdr + info->hdr->e_shoff;
Rusty Russell8b5f61a2010-08-05 12:59:06 -06002607 info->secstrings = (void *)info->hdr
2608 + info->sechdrs[info->hdr->e_shstrndx].sh_offset;
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002609
Rusty Russell8b5f61a2010-08-05 12:59:06 -06002610 err = rewrite_section_headers(info);
2611 if (err)
2612 return ERR_PTR(err);
2613
2614 /* Find internal symbols and strings. */
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002615 for (i = 1; i < info->hdr->e_shnum; i++) {
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002616 if (info->sechdrs[i].sh_type == SHT_SYMTAB) {
2617 info->index.sym = i;
2618 info->index.str = info->sechdrs[i].sh_link;
Rusty Russell8b5f61a2010-08-05 12:59:06 -06002619 info->strtab = (char *)info->hdr
2620 + info->sechdrs[info->index.str].sh_offset;
2621 break;
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002622 }
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002623 }
2624
Rusty Russell49668682010-08-05 12:59:10 -06002625 info->index.mod = find_sec(info, ".gnu.linkonce.this_module");
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002626 if (!info->index.mod) {
2627 printk(KERN_WARNING "No module found in object\n");
2628 return ERR_PTR(-ENOEXEC);
2629 }
2630 /* This is temporary: point mod into copy of data. */
2631 mod = (void *)info->sechdrs[info->index.mod].sh_addr;
2632
2633 if (info->index.sym == 0) {
2634 printk(KERN_WARNING "%s: module has no symbols (stripped?)\n",
2635 mod->name);
2636 return ERR_PTR(-ENOEXEC);
2637 }
2638
Rusty Russell49668682010-08-05 12:59:10 -06002639 info->index.pcpu = find_pcpusec(info);
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002640
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002641 /* Check module struct version now, before we try to use module. */
2642 if (!check_modstruct_version(info->sechdrs, info->index.vers, mod))
2643 return ERR_PTR(-ENOEXEC);
2644
2645 return mod;
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002646}
2647
Rusty Russell49668682010-08-05 12:59:10 -06002648static int check_modinfo(struct module *mod, struct load_info *info)
Rusty Russell40dd2562010-08-05 12:59:03 -06002649{
Rusty Russell49668682010-08-05 12:59:10 -06002650 const char *modmagic = get_modinfo(info, "vermagic");
Rusty Russell40dd2562010-08-05 12:59:03 -06002651 int err;
2652
2653 /* This is allowed: modprobe --force will invalidate it. */
2654 if (!modmagic) {
2655 err = try_to_force_load(mod, "bad vermagic");
2656 if (err)
2657 return err;
Rusty Russell49668682010-08-05 12:59:10 -06002658 } else if (!same_magic(modmagic, vermagic, info->index.vers)) {
Rusty Russell40dd2562010-08-05 12:59:03 -06002659 printk(KERN_ERR "%s: version magic '%s' should be '%s'\n",
2660 mod->name, modmagic, vermagic);
2661 return -ENOEXEC;
2662 }
2663
Ben Hutchings2449b8b2011-10-24 15:12:28 +02002664 if (!get_modinfo(info, "intree"))
2665 add_taint_module(mod, TAINT_OOT_MODULE);
2666
Rusty Russell49668682010-08-05 12:59:10 -06002667 if (get_modinfo(info, "staging")) {
Rusty Russell40dd2562010-08-05 12:59:03 -06002668 add_taint_module(mod, TAINT_CRAP);
2669 printk(KERN_WARNING "%s: module is from the staging directory,"
2670 " the quality is unknown, you have been warned.\n",
2671 mod->name);
2672 }
Rusty Russell22e268e2010-08-05 12:59:05 -06002673
2674 /* Set up license info based on the info section */
Rusty Russell49668682010-08-05 12:59:10 -06002675 set_license(mod, get_modinfo(info, "license"));
Rusty Russell22e268e2010-08-05 12:59:05 -06002676
Rusty Russell40dd2562010-08-05 12:59:03 -06002677 return 0;
2678}
2679
Rusty Russell811d66a2010-08-05 12:59:12 -06002680static void find_module_sections(struct module *mod, struct load_info *info)
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06002681{
Rusty Russell49668682010-08-05 12:59:10 -06002682 mod->kp = section_objs(info, "__param",
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06002683 sizeof(*mod->kp), &mod->num_kp);
Rusty Russell49668682010-08-05 12:59:10 -06002684 mod->syms = section_objs(info, "__ksymtab",
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06002685 sizeof(*mod->syms), &mod->num_syms);
Rusty Russell49668682010-08-05 12:59:10 -06002686 mod->crcs = section_addr(info, "__kcrctab");
2687 mod->gpl_syms = section_objs(info, "__ksymtab_gpl",
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06002688 sizeof(*mod->gpl_syms),
2689 &mod->num_gpl_syms);
Rusty Russell49668682010-08-05 12:59:10 -06002690 mod->gpl_crcs = section_addr(info, "__kcrctab_gpl");
2691 mod->gpl_future_syms = section_objs(info,
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06002692 "__ksymtab_gpl_future",
2693 sizeof(*mod->gpl_future_syms),
2694 &mod->num_gpl_future_syms);
Rusty Russell49668682010-08-05 12:59:10 -06002695 mod->gpl_future_crcs = section_addr(info, "__kcrctab_gpl_future");
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06002696
2697#ifdef CONFIG_UNUSED_SYMBOLS
Rusty Russell49668682010-08-05 12:59:10 -06002698 mod->unused_syms = section_objs(info, "__ksymtab_unused",
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06002699 sizeof(*mod->unused_syms),
2700 &mod->num_unused_syms);
Rusty Russell49668682010-08-05 12:59:10 -06002701 mod->unused_crcs = section_addr(info, "__kcrctab_unused");
2702 mod->unused_gpl_syms = section_objs(info, "__ksymtab_unused_gpl",
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06002703 sizeof(*mod->unused_gpl_syms),
2704 &mod->num_unused_gpl_syms);
Rusty Russell49668682010-08-05 12:59:10 -06002705 mod->unused_gpl_crcs = section_addr(info, "__kcrctab_unused_gpl");
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06002706#endif
2707#ifdef CONFIG_CONSTRUCTORS
Rusty Russell49668682010-08-05 12:59:10 -06002708 mod->ctors = section_objs(info, ".ctors",
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06002709 sizeof(*mod->ctors), &mod->num_ctors);
2710#endif
2711
2712#ifdef CONFIG_TRACEPOINTS
Mathieu Desnoyers65498642011-01-26 17:26:22 -05002713 mod->tracepoints_ptrs = section_objs(info, "__tracepoints_ptrs",
2714 sizeof(*mod->tracepoints_ptrs),
2715 &mod->num_tracepoints);
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06002716#endif
Jason Baronbf5438fc2010-09-17 11:09:00 -04002717#ifdef HAVE_JUMP_LABEL
2718 mod->jump_entries = section_objs(info, "__jump_table",
2719 sizeof(*mod->jump_entries),
2720 &mod->num_jump_entries);
2721#endif
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06002722#ifdef CONFIG_EVENT_TRACING
Rusty Russell49668682010-08-05 12:59:10 -06002723 mod->trace_events = section_objs(info, "_ftrace_events",
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06002724 sizeof(*mod->trace_events),
2725 &mod->num_trace_events);
2726 /*
2727 * This section contains pointers to allocated objects in the trace
2728 * code and not scanning it leads to false positives.
2729 */
2730 kmemleak_scan_area(mod->trace_events, sizeof(*mod->trace_events) *
2731 mod->num_trace_events, GFP_KERNEL);
2732#endif
Steven Rostedt13b9b6e2010-11-10 22:19:24 -05002733#ifdef CONFIG_TRACING
2734 mod->trace_bprintk_fmt_start = section_objs(info, "__trace_printk_fmt",
2735 sizeof(*mod->trace_bprintk_fmt_start),
2736 &mod->num_trace_bprintk_fmt);
2737 /*
2738 * This section contains pointers to allocated objects in the trace
2739 * code and not scanning it leads to false positives.
2740 */
2741 kmemleak_scan_area(mod->trace_bprintk_fmt_start,
2742 sizeof(*mod->trace_bprintk_fmt_start) *
2743 mod->num_trace_bprintk_fmt, GFP_KERNEL);
2744#endif
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06002745#ifdef CONFIG_FTRACE_MCOUNT_RECORD
2746 /* sechdrs[0].sh_size is always zero */
Rusty Russell49668682010-08-05 12:59:10 -06002747 mod->ftrace_callsites = section_objs(info, "__mcount_loc",
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06002748 sizeof(*mod->ftrace_callsites),
2749 &mod->num_ftrace_callsites);
2750#endif
Rusty Russell22e268e2010-08-05 12:59:05 -06002751
Rusty Russell811d66a2010-08-05 12:59:12 -06002752 mod->extable = section_objs(info, "__ex_table",
2753 sizeof(*mod->extable), &mod->num_exentries);
2754
Rusty Russell49668682010-08-05 12:59:10 -06002755 if (section_addr(info, "__obsparm"))
Rusty Russell22e268e2010-08-05 12:59:05 -06002756 printk(KERN_WARNING "%s: Ignoring obsolete parameters\n",
2757 mod->name);
Rusty Russell811d66a2010-08-05 12:59:12 -06002758
2759 info->debug = section_objs(info, "__verbose",
2760 sizeof(*info->debug), &info->num_debug);
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06002761}
2762
Rusty Russell49668682010-08-05 12:59:10 -06002763static int move_module(struct module *mod, struct load_info *info)
Linus Torvalds65b8a9b2010-08-05 12:59:02 -06002764{
2765 int i;
2766 void *ptr;
2767
2768 /* Do the allocs. */
2769 ptr = module_alloc_update_bounds(mod->core_size);
2770 /*
2771 * The pointer to this block is stored in the module structure
2772 * which is inside the block. Just mark it as not being a
2773 * leak.
2774 */
2775 kmemleak_not_leak(ptr);
2776 if (!ptr)
Rusty Russelld9131882010-08-05 12:59:08 -06002777 return -ENOMEM;
Linus Torvalds65b8a9b2010-08-05 12:59:02 -06002778
2779 memset(ptr, 0, mod->core_size);
2780 mod->module_core = ptr;
2781
2782 ptr = module_alloc_update_bounds(mod->init_size);
2783 /*
2784 * The pointer to this block is stored in the module structure
2785 * which is inside the block. This block doesn't need to be
2786 * scanned as it contains data and code that will be freed
2787 * after the module is initialized.
2788 */
2789 kmemleak_ignore(ptr);
2790 if (!ptr && mod->init_size) {
2791 module_free(mod, mod->module_core);
Rusty Russelld9131882010-08-05 12:59:08 -06002792 return -ENOMEM;
Linus Torvalds65b8a9b2010-08-05 12:59:02 -06002793 }
2794 memset(ptr, 0, mod->init_size);
2795 mod->module_init = ptr;
2796
2797 /* Transfer each section which specifies SHF_ALLOC */
Jim Cromie5e124162011-12-06 12:11:31 -07002798 pr_debug("final section addresses:\n");
Rusty Russell49668682010-08-05 12:59:10 -06002799 for (i = 0; i < info->hdr->e_shnum; i++) {
Linus Torvalds65b8a9b2010-08-05 12:59:02 -06002800 void *dest;
Rusty Russell49668682010-08-05 12:59:10 -06002801 Elf_Shdr *shdr = &info->sechdrs[i];
Linus Torvalds65b8a9b2010-08-05 12:59:02 -06002802
Rusty Russell49668682010-08-05 12:59:10 -06002803 if (!(shdr->sh_flags & SHF_ALLOC))
Linus Torvalds65b8a9b2010-08-05 12:59:02 -06002804 continue;
2805
Rusty Russell49668682010-08-05 12:59:10 -06002806 if (shdr->sh_entsize & INIT_OFFSET_MASK)
Linus Torvalds65b8a9b2010-08-05 12:59:02 -06002807 dest = mod->module_init
Rusty Russell49668682010-08-05 12:59:10 -06002808 + (shdr->sh_entsize & ~INIT_OFFSET_MASK);
Linus Torvalds65b8a9b2010-08-05 12:59:02 -06002809 else
Rusty Russell49668682010-08-05 12:59:10 -06002810 dest = mod->module_core + shdr->sh_entsize;
Linus Torvalds65b8a9b2010-08-05 12:59:02 -06002811
Rusty Russell49668682010-08-05 12:59:10 -06002812 if (shdr->sh_type != SHT_NOBITS)
2813 memcpy(dest, (void *)shdr->sh_addr, shdr->sh_size);
Linus Torvalds65b8a9b2010-08-05 12:59:02 -06002814 /* Update sh_addr to point to copy in image. */
Rusty Russell49668682010-08-05 12:59:10 -06002815 shdr->sh_addr = (unsigned long)dest;
Jim Cromie5e124162011-12-06 12:11:31 -07002816 pr_debug("\t0x%lx %s\n",
2817 (long)shdr->sh_addr, info->secstrings + shdr->sh_name);
Linus Torvalds65b8a9b2010-08-05 12:59:02 -06002818 }
Rusty Russelld9131882010-08-05 12:59:08 -06002819
2820 return 0;
Linus Torvalds65b8a9b2010-08-05 12:59:02 -06002821}
2822
Rusty Russell49668682010-08-05 12:59:10 -06002823static int check_module_license_and_versions(struct module *mod)
Rusty Russell22e268e2010-08-05 12:59:05 -06002824{
2825 /*
2826 * ndiswrapper is under GPL by itself, but loads proprietary modules.
2827 * Don't use add_taint_module(), as it would prevent ndiswrapper from
2828 * using GPL-only symbols it needs.
2829 */
2830 if (strcmp(mod->name, "ndiswrapper") == 0)
2831 add_taint(TAINT_PROPRIETARY_MODULE);
2832
2833 /* driverloader was caught wrongly pretending to be under GPL */
2834 if (strcmp(mod->name, "driverloader") == 0)
2835 add_taint_module(mod, TAINT_PROPRIETARY_MODULE);
2836
Matthew Garrettc99af372012-06-22 13:49:31 -04002837 /* lve claims to be GPL but upstream won't provide source */
2838 if (strcmp(mod->name, "lve") == 0)
2839 add_taint_module(mod, TAINT_PROPRIETARY_MODULE);
2840
Rusty Russell22e268e2010-08-05 12:59:05 -06002841#ifdef CONFIG_MODVERSIONS
2842 if ((mod->num_syms && !mod->crcs)
2843 || (mod->num_gpl_syms && !mod->gpl_crcs)
2844 || (mod->num_gpl_future_syms && !mod->gpl_future_crcs)
2845#ifdef CONFIG_UNUSED_SYMBOLS
2846 || (mod->num_unused_syms && !mod->unused_crcs)
2847 || (mod->num_unused_gpl_syms && !mod->unused_gpl_crcs)
2848#endif
2849 ) {
2850 return try_to_force_load(mod,
2851 "no versions for exported symbols");
2852 }
2853#endif
2854 return 0;
2855}
2856
2857static void flush_module_icache(const struct module *mod)
2858{
2859 mm_segment_t old_fs;
2860
2861 /* flush the icache in correct context */
2862 old_fs = get_fs();
2863 set_fs(KERNEL_DS);
2864
2865 /*
2866 * Flush the instruction cache, since we've played with text.
2867 * Do it before processing of module parameters, so the module
2868 * can provide parameter accessor functions of its own.
2869 */
2870 if (mod->module_init)
2871 flush_icache_range((unsigned long)mod->module_init,
2872 (unsigned long)mod->module_init
2873 + mod->init_size);
2874 flush_icache_range((unsigned long)mod->module_core,
2875 (unsigned long)mod->module_core + mod->core_size);
2876
2877 set_fs(old_fs);
2878}
2879
Jonas Bonn74e08fc2011-06-30 21:22:11 +02002880int __weak module_frob_arch_sections(Elf_Ehdr *hdr,
2881 Elf_Shdr *sechdrs,
2882 char *secstrings,
2883 struct module *mod)
2884{
2885 return 0;
2886}
2887
Rusty Russelld9131882010-08-05 12:59:08 -06002888static struct module *layout_and_allocate(struct load_info *info)
2889{
2890 /* Module within temporary copy. */
2891 struct module *mod;
Rusty Russell49668682010-08-05 12:59:10 -06002892 Elf_Shdr *pcpusec;
Rusty Russelld9131882010-08-05 12:59:08 -06002893 int err;
2894
2895 mod = setup_load_info(info);
2896 if (IS_ERR(mod))
2897 return mod;
2898
Rusty Russell49668682010-08-05 12:59:10 -06002899 err = check_modinfo(mod, info);
Rusty Russelld9131882010-08-05 12:59:08 -06002900 if (err)
2901 return ERR_PTR(err);
2902
2903 /* Allow arches to frob section contents and sizes. */
Rusty Russell49668682010-08-05 12:59:10 -06002904 err = module_frob_arch_sections(info->hdr, info->sechdrs,
2905 info->secstrings, mod);
Rusty Russelld9131882010-08-05 12:59:08 -06002906 if (err < 0)
Rusty Russell6526c532010-08-05 12:59:10 -06002907 goto out;
Rusty Russelld9131882010-08-05 12:59:08 -06002908
Rusty Russell49668682010-08-05 12:59:10 -06002909 pcpusec = &info->sechdrs[info->index.pcpu];
2910 if (pcpusec->sh_size) {
Rusty Russelld9131882010-08-05 12:59:08 -06002911 /* We have a special allocation for this section. */
Rusty Russell49668682010-08-05 12:59:10 -06002912 err = percpu_modalloc(mod,
2913 pcpusec->sh_size, pcpusec->sh_addralign);
Rusty Russelld9131882010-08-05 12:59:08 -06002914 if (err)
Rusty Russell6526c532010-08-05 12:59:10 -06002915 goto out;
Rusty Russell49668682010-08-05 12:59:10 -06002916 pcpusec->sh_flags &= ~(unsigned long)SHF_ALLOC;
Rusty Russelld9131882010-08-05 12:59:08 -06002917 }
2918
2919 /* Determine total sizes, and put offsets in sh_entsize. For now
2920 this is done generically; there doesn't appear to be any
2921 special cases for the architectures. */
Rusty Russell49668682010-08-05 12:59:10 -06002922 layout_sections(mod, info);
Rusty Russell49668682010-08-05 12:59:10 -06002923 layout_symtab(mod, info);
Rusty Russelld9131882010-08-05 12:59:08 -06002924
2925 /* Allocate and move to the final place */
Rusty Russell49668682010-08-05 12:59:10 -06002926 err = move_module(mod, info);
Rusty Russelld9131882010-08-05 12:59:08 -06002927 if (err)
Kevin Cernekee48fd1182012-01-13 09:32:14 +10302928 goto free_percpu;
Rusty Russelld9131882010-08-05 12:59:08 -06002929
2930 /* Module has been copied to its final place now: return it. */
2931 mod = (void *)info->sechdrs[info->index.mod].sh_addr;
Rusty Russell49668682010-08-05 12:59:10 -06002932 kmemleak_load_module(mod, info);
Rusty Russelld9131882010-08-05 12:59:08 -06002933 return mod;
2934
Rusty Russelld9131882010-08-05 12:59:08 -06002935free_percpu:
2936 percpu_modfree(mod);
Rusty Russell6526c532010-08-05 12:59:10 -06002937out:
Rusty Russelld9131882010-08-05 12:59:08 -06002938 return ERR_PTR(err);
2939}
2940
2941/* mod is no longer valid after this! */
2942static void module_deallocate(struct module *mod, struct load_info *info)
2943{
Rusty Russelld9131882010-08-05 12:59:08 -06002944 percpu_modfree(mod);
2945 module_free(mod, mod->module_init);
2946 module_free(mod, mod->module_core);
2947}
2948
Jonas Bonn74e08fc2011-06-30 21:22:11 +02002949int __weak module_finalize(const Elf_Ehdr *hdr,
2950 const Elf_Shdr *sechdrs,
2951 struct module *me)
2952{
2953 return 0;
2954}
2955
Rusty Russell811d66a2010-08-05 12:59:12 -06002956static int post_relocation(struct module *mod, const struct load_info *info)
2957{
Rusty Russell51f3d0f2010-08-05 12:59:13 -06002958 /* Sort exception table now relocations are done. */
Rusty Russell811d66a2010-08-05 12:59:12 -06002959 sort_extable(mod->extable, mod->extable + mod->num_exentries);
2960
2961 /* Copy relocated percpu area over. */
2962 percpu_modcopy(mod, (void *)info->sechdrs[info->index.pcpu].sh_addr,
2963 info->sechdrs[info->index.pcpu].sh_size);
2964
Rusty Russell51f3d0f2010-08-05 12:59:13 -06002965 /* Setup kallsyms-specific fields. */
Rusty Russell811d66a2010-08-05 12:59:12 -06002966 add_kallsyms(mod, info);
2967
2968 /* Arch-specific module finalizing. */
2969 return module_finalize(info->hdr, info->sechdrs, mod);
2970}
2971
Rusty Russell9bb9c3b2012-09-28 14:31:03 +09302972/* Is this module of this name done loading? No locks held. */
2973static bool finished_loading(const char *name)
2974{
2975 struct module *mod;
2976 bool ret;
2977
2978 mutex_lock(&module_mutex);
2979 mod = find_module(name);
2980 ret = !mod || mod->state != MODULE_STATE_COMING;
2981 mutex_unlock(&module_mutex);
2982
2983 return ret;
2984}
2985
Peter Oberparleiterb99b87f2009-06-17 16:28:03 -07002986/* Call module constructors. */
2987static void do_mod_ctors(struct module *mod)
2988{
2989#ifdef CONFIG_CONSTRUCTORS
2990 unsigned long i;
2991
2992 for (i = 0; i < mod->num_ctors; i++)
2993 mod->ctors[i]();
2994#endif
2995}
2996
Linus Torvalds1da177e2005-04-16 15:20:36 -07002997/* This is where the real work happens */
Kees Cook34e11692012-10-16 07:31:07 +10302998static int do_init_module(struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002999{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003000 int ret = 0;
3001
Alan Sterne041c682006-03-27 01:16:30 -08003002 blocking_notifier_call_chain(&module_notify_list,
3003 MODULE_STATE_COMING, mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003004
Steven Rostedt94462ad2010-11-29 13:15:42 -05003005 /* Set RO and NX regions for core */
3006 set_section_ro_nx(mod->module_core,
3007 mod->core_text_size,
3008 mod->core_ro_size,
3009 mod->core_size);
3010
3011 /* Set RO and NX regions for init */
3012 set_section_ro_nx(mod->module_init,
3013 mod->init_text_size,
3014 mod->init_ro_size,
3015 mod->init_size);
3016
Peter Oberparleiterb99b87f2009-06-17 16:28:03 -07003017 do_mod_ctors(mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003018 /* Start the module */
3019 if (mod->init != NULL)
Arjan van de Ven59f94152008-07-30 12:49:02 -07003020 ret = do_one_initcall(mod->init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003021 if (ret < 0) {
3022 /* Init routine failed: abort. Try to protect us from
3023 buggy refcounters. */
3024 mod->state = MODULE_STATE_GOING;
Paul E. McKenneyfbd568a3e2005-05-01 08:59:04 -07003025 synchronize_sched();
Rusty Russellaf49d922007-10-16 23:26:27 -07003026 module_put(mod);
Peter Oberparleiterdf4b5652008-04-21 14:34:31 +02003027 blocking_notifier_call_chain(&module_notify_list,
3028 MODULE_STATE_GOING, mod);
Rusty Russellaf49d922007-10-16 23:26:27 -07003029 free_module(mod);
Rusty Russell6f139092012-09-28 14:31:03 +09303030 wake_up_all(&module_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003031 return ret;
3032 }
Alexey Dobriyane24e2e62008-03-10 11:43:53 -07003033 if (ret > 0) {
Joe Perchesad361c92009-07-06 13:05:40 -07003034 printk(KERN_WARNING
3035"%s: '%s'->init suspiciously returned %d, it should follow 0/-E convention\n"
3036"%s: loading module anyway...\n",
Alexey Dobriyane24e2e62008-03-10 11:43:53 -07003037 __func__, mod->name, ret,
3038 __func__);
3039 dump_stack();
3040 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003041
Rusty Russell6f139092012-09-28 14:31:03 +09303042 /* Now it's a first class citizen! */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003043 mod->state = MODULE_STATE_LIVE;
Masami Hiramatsu0deddf42009-01-06 14:41:54 -08003044 blocking_notifier_call_chain(&module_notify_list,
3045 MODULE_STATE_LIVE, mod);
Rusty Russell6c5db222008-03-10 11:43:52 -07003046
Linus Torvaldsd6de2c82009-04-10 12:17:41 -07003047 /* We need to finish all async code before the module init sequence is done */
3048 async_synchronize_full();
3049
Rusty Russell6c5db222008-03-10 11:43:52 -07003050 mutex_lock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003051 /* Drop initial reference. */
3052 module_put(mod);
Rusty Russellad6561d2009-06-12 21:47:03 -06003053 trim_init_extable(mod);
Jan Beulich4a496222009-07-06 14:50:42 +01003054#ifdef CONFIG_KALLSYMS
3055 mod->num_symtab = mod->core_num_syms;
3056 mod->symtab = mod->core_symtab;
Jan Beulich554bdfe2009-07-06 14:51:44 +01003057 mod->strtab = mod->core_strtab;
Jan Beulich4a496222009-07-06 14:50:42 +01003058#endif
Jan Glauber01526ed2011-05-19 16:55:26 -06003059 unset_module_init_ro_nx(mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003060 module_free(mod, mod->module_init);
3061 mod->module_init = NULL;
3062 mod->init_size = 0;
Jan Glauber4d103802011-05-19 16:55:25 -06003063 mod->init_ro_size = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003064 mod->init_text_size = 0;
Ashutosh Naik6389a382006-03-23 03:00:46 -08003065 mutex_unlock(&module_mutex);
Rusty Russell6f139092012-09-28 14:31:03 +09303066 wake_up_all(&module_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003067
3068 return 0;
3069}
3070
Kees Cook34e11692012-10-16 07:31:07 +10303071static int may_init_module(void)
3072{
3073 if (!capable(CAP_SYS_MODULE) || modules_disabled)
3074 return -EPERM;
3075
3076 return 0;
3077}
3078
3079/* Allocate and load the module: note that size of section 0 is always
3080 zero, and we rely on this for optional sections. */
3081static int load_module(struct load_info *info, const char __user *uargs)
3082{
3083 struct module *mod, *old;
3084 long err;
3085
3086 err = module_sig_check(info);
3087 if (err)
3088 goto free_copy;
3089
3090 err = elf_header_check(info);
3091 if (err)
3092 goto free_copy;
3093
3094 /* Figure out module layout, and allocate all the memory. */
3095 mod = layout_and_allocate(info);
3096 if (IS_ERR(mod)) {
3097 err = PTR_ERR(mod);
3098 goto free_copy;
3099 }
3100
3101#ifdef CONFIG_MODULE_SIG
3102 mod->sig_ok = info->sig_ok;
3103 if (!mod->sig_ok)
3104 add_taint_module(mod, TAINT_FORCED_MODULE);
3105#endif
3106
3107 /* Now module is in final location, initialize linked lists, etc. */
3108 err = module_unload_init(mod);
3109 if (err)
3110 goto free_module;
3111
3112 /* Now we've got everything in the final locations, we can
3113 * find optional sections. */
3114 find_module_sections(mod, info);
3115
3116 err = check_module_license_and_versions(mod);
3117 if (err)
3118 goto free_unload;
3119
3120 /* Set up MODINFO_ATTR fields */
3121 setup_modinfo(mod, info);
3122
3123 /* Fix up syms, so that st_value is a pointer to location. */
3124 err = simplify_symbols(mod, info);
3125 if (err < 0)
3126 goto free_modinfo;
3127
3128 err = apply_relocations(mod, info);
3129 if (err < 0)
3130 goto free_modinfo;
3131
3132 err = post_relocation(mod, info);
3133 if (err < 0)
3134 goto free_modinfo;
3135
3136 flush_module_icache(mod);
3137
3138 /* Now copy in args */
3139 mod->args = strndup_user(uargs, ~0UL >> 1);
3140 if (IS_ERR(mod->args)) {
3141 err = PTR_ERR(mod->args);
3142 goto free_arch_cleanup;
3143 }
3144
3145 /* Mark state as coming so strong_try_module_get() ignores us. */
3146 mod->state = MODULE_STATE_COMING;
3147
3148 /* Now sew it into the lists so we can get lockdep and oops
3149 * info during argument parsing. No one should access us, since
3150 * strong_try_module_get() will fail.
3151 * lockdep/oops can run asynchronous, so use the RCU list insertion
3152 * function to insert in a way safe to concurrent readers.
3153 * The mutex protects against concurrent writers.
3154 */
3155again:
3156 mutex_lock(&module_mutex);
3157 if ((old = find_module(mod->name)) != NULL) {
3158 if (old->state == MODULE_STATE_COMING) {
3159 /* Wait in case it fails to load. */
3160 mutex_unlock(&module_mutex);
3161 err = wait_event_interruptible(module_wq,
3162 finished_loading(mod->name));
3163 if (err)
3164 goto free_arch_cleanup;
3165 goto again;
3166 }
3167 err = -EEXIST;
3168 goto unlock;
3169 }
3170
3171 /* This has to be done once we're sure module name is unique. */
3172 dynamic_debug_setup(info->debug, info->num_debug);
3173
3174 /* Find duplicate symbols */
3175 err = verify_export_symbols(mod);
3176 if (err < 0)
3177 goto ddebug;
3178
3179 module_bug_finalize(info->hdr, info->sechdrs, mod);
3180 list_add_rcu(&mod->list, &modules);
3181 mutex_unlock(&module_mutex);
3182
3183 /* Module is ready to execute: parsing args may do that. */
3184 err = parse_args(mod->name, mod->args, mod->kp, mod->num_kp,
3185 -32768, 32767, &ddebug_dyndbg_module_param_cb);
3186 if (err < 0)
3187 goto unlink;
3188
3189 /* Link in to syfs. */
3190 err = mod_sysfs_setup(mod, info, mod->kp, mod->num_kp);
3191 if (err < 0)
3192 goto unlink;
3193
3194 /* Get rid of temporary copy. */
3195 free_copy(info);
3196
3197 /* Done! */
3198 trace_module_load(mod);
3199
3200 return do_init_module(mod);
3201
3202 unlink:
3203 mutex_lock(&module_mutex);
3204 /* Unlink carefully: kallsyms could be walking list. */
3205 list_del_rcu(&mod->list);
3206 module_bug_cleanup(mod);
3207 wake_up_all(&module_wq);
3208 ddebug:
3209 dynamic_debug_remove(info->debug);
3210 unlock:
3211 mutex_unlock(&module_mutex);
3212 synchronize_sched();
3213 kfree(mod->args);
3214 free_arch_cleanup:
3215 module_arch_cleanup(mod);
3216 free_modinfo:
3217 free_modinfo(mod);
3218 free_unload:
3219 module_unload_free(mod);
3220 free_module:
3221 module_deallocate(mod, info);
3222 free_copy:
3223 free_copy(info);
3224 return err;
3225}
3226
3227SYSCALL_DEFINE3(init_module, void __user *, umod,
3228 unsigned long, len, const char __user *, uargs)
3229{
3230 int err;
3231 struct load_info info = { };
3232
3233 err = may_init_module();
3234 if (err)
3235 return err;
3236
3237 pr_debug("init_module: umod=%p, len=%lu, uargs=%p\n",
3238 umod, len, uargs);
3239
3240 err = copy_module_from_user(umod, len, &info);
3241 if (err)
3242 return err;
3243
3244 return load_module(&info, uargs);
3245}
3246
3247SYSCALL_DEFINE2(finit_module, int, fd, const char __user *, uargs)
3248{
3249 int err;
3250 struct load_info info = { };
3251
3252 err = may_init_module();
3253 if (err)
3254 return err;
3255
3256 pr_debug("finit_module: fd=%d, uargs=%p\n", fd, uargs);
3257
3258 err = copy_module_from_fd(fd, &info);
3259 if (err)
3260 return err;
3261
3262 return load_module(&info, uargs);
3263}
3264
Linus Torvalds1da177e2005-04-16 15:20:36 -07003265static inline int within(unsigned long addr, void *start, unsigned long size)
3266{
3267 return ((void *)addr >= start && (void *)addr < start + size);
3268}
3269
3270#ifdef CONFIG_KALLSYMS
3271/*
3272 * This ignores the intensely annoying "mapping symbols" found
3273 * in ARM ELF files: $a, $t and $d.
3274 */
3275static inline int is_arm_mapping_symbol(const char *str)
3276{
Daniel Walker22a8bde2007-10-18 03:06:07 -07003277 return str[0] == '$' && strchr("atd", str[1])
Linus Torvalds1da177e2005-04-16 15:20:36 -07003278 && (str[2] == '\0' || str[2] == '.');
3279}
3280
3281static const char *get_ksymbol(struct module *mod,
3282 unsigned long addr,
3283 unsigned long *size,
3284 unsigned long *offset)
3285{
3286 unsigned int i, best = 0;
3287 unsigned long nextval;
3288
3289 /* At worse, next value is at end of module */
Masami Hiramatsua06f6212009-01-06 14:41:49 -08003290 if (within_module_init(addr, mod))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003291 nextval = (unsigned long)mod->module_init+mod->init_text_size;
Daniel Walker22a8bde2007-10-18 03:06:07 -07003292 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003293 nextval = (unsigned long)mod->module_core+mod->core_text_size;
3294
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003295 /* Scan for closest preceding symbol, and next symbol. (ELF
Daniel Walker22a8bde2007-10-18 03:06:07 -07003296 starts real symbols at 1). */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003297 for (i = 1; i < mod->num_symtab; i++) {
3298 if (mod->symtab[i].st_shndx == SHN_UNDEF)
3299 continue;
3300
3301 /* We ignore unnamed symbols: they're uninformative
3302 * and inserted at a whim. */
3303 if (mod->symtab[i].st_value <= addr
3304 && mod->symtab[i].st_value > mod->symtab[best].st_value
3305 && *(mod->strtab + mod->symtab[i].st_name) != '\0'
3306 && !is_arm_mapping_symbol(mod->strtab + mod->symtab[i].st_name))
3307 best = i;
3308 if (mod->symtab[i].st_value > addr
3309 && mod->symtab[i].st_value < nextval
3310 && *(mod->strtab + mod->symtab[i].st_name) != '\0'
3311 && !is_arm_mapping_symbol(mod->strtab + mod->symtab[i].st_name))
3312 nextval = mod->symtab[i].st_value;
3313 }
3314
3315 if (!best)
3316 return NULL;
3317
Alexey Dobriyanffb45122007-05-08 00:28:41 -07003318 if (size)
3319 *size = nextval - mod->symtab[best].st_value;
3320 if (offset)
3321 *offset = addr - mod->symtab[best].st_value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003322 return mod->strtab + mod->symtab[best].st_name;
3323}
3324
Rusty Russell6dd06c92008-01-29 17:13:22 -05003325/* For kallsyms to ask for address resolution. NULL means not found. Careful
3326 * not to lock to avoid deadlock on oopses, simply disable preemption. */
Andrew Morton92dfc9d2008-02-08 04:18:43 -08003327const char *module_address_lookup(unsigned long addr,
Rusty Russell6dd06c92008-01-29 17:13:22 -05003328 unsigned long *size,
3329 unsigned long *offset,
3330 char **modname,
3331 char *namebuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003332{
3333 struct module *mod;
Rusty Russellcb2a5202008-01-14 00:55:03 -08003334 const char *ret = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003335
Rusty Russellcb2a5202008-01-14 00:55:03 -08003336 preempt_disable();
Andi Kleend72b3752008-08-30 10:09:00 +02003337 list_for_each_entry_rcu(mod, &modules, list) {
Masami Hiramatsua06f6212009-01-06 14:41:49 -08003338 if (within_module_init(addr, mod) ||
3339 within_module_core(addr, mod)) {
Franck Bui-Huuffc50892006-10-03 01:13:48 -07003340 if (modname)
3341 *modname = mod->name;
Rusty Russellcb2a5202008-01-14 00:55:03 -08003342 ret = get_ksymbol(mod, addr, size, offset);
3343 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003344 }
3345 }
Rusty Russell6dd06c92008-01-29 17:13:22 -05003346 /* Make a copy in here where it's safe */
3347 if (ret) {
3348 strncpy(namebuf, ret, KSYM_NAME_LEN - 1);
3349 ret = namebuf;
3350 }
Rusty Russellcb2a5202008-01-14 00:55:03 -08003351 preempt_enable();
Andrew Morton92dfc9d2008-02-08 04:18:43 -08003352 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003353}
3354
Alexey Dobriyan9d65cb42007-05-08 00:28:43 -07003355int lookup_module_symbol_name(unsigned long addr, char *symname)
3356{
3357 struct module *mod;
3358
Rusty Russellcb2a5202008-01-14 00:55:03 -08003359 preempt_disable();
Andi Kleend72b3752008-08-30 10:09:00 +02003360 list_for_each_entry_rcu(mod, &modules, list) {
Masami Hiramatsua06f6212009-01-06 14:41:49 -08003361 if (within_module_init(addr, mod) ||
3362 within_module_core(addr, mod)) {
Alexey Dobriyan9d65cb42007-05-08 00:28:43 -07003363 const char *sym;
3364
3365 sym = get_ksymbol(mod, addr, NULL, NULL);
3366 if (!sym)
3367 goto out;
Tejun Heo9281ace2007-07-17 04:03:51 -07003368 strlcpy(symname, sym, KSYM_NAME_LEN);
Rusty Russellcb2a5202008-01-14 00:55:03 -08003369 preempt_enable();
Alexey Dobriyan9d65cb42007-05-08 00:28:43 -07003370 return 0;
3371 }
3372 }
3373out:
Rusty Russellcb2a5202008-01-14 00:55:03 -08003374 preempt_enable();
Alexey Dobriyan9d65cb42007-05-08 00:28:43 -07003375 return -ERANGE;
3376}
3377
Alexey Dobriyana5c43da2007-05-08 00:28:47 -07003378int lookup_module_symbol_attrs(unsigned long addr, unsigned long *size,
3379 unsigned long *offset, char *modname, char *name)
3380{
3381 struct module *mod;
3382
Rusty Russellcb2a5202008-01-14 00:55:03 -08003383 preempt_disable();
Andi Kleend72b3752008-08-30 10:09:00 +02003384 list_for_each_entry_rcu(mod, &modules, list) {
Masami Hiramatsua06f6212009-01-06 14:41:49 -08003385 if (within_module_init(addr, mod) ||
3386 within_module_core(addr, mod)) {
Alexey Dobriyana5c43da2007-05-08 00:28:47 -07003387 const char *sym;
3388
3389 sym = get_ksymbol(mod, addr, size, offset);
3390 if (!sym)
3391 goto out;
3392 if (modname)
Tejun Heo9281ace2007-07-17 04:03:51 -07003393 strlcpy(modname, mod->name, MODULE_NAME_LEN);
Alexey Dobriyana5c43da2007-05-08 00:28:47 -07003394 if (name)
Tejun Heo9281ace2007-07-17 04:03:51 -07003395 strlcpy(name, sym, KSYM_NAME_LEN);
Rusty Russellcb2a5202008-01-14 00:55:03 -08003396 preempt_enable();
Alexey Dobriyana5c43da2007-05-08 00:28:47 -07003397 return 0;
3398 }
3399 }
3400out:
Rusty Russellcb2a5202008-01-14 00:55:03 -08003401 preempt_enable();
Alexey Dobriyana5c43da2007-05-08 00:28:47 -07003402 return -ERANGE;
3403}
3404
Alexey Dobriyanea078902007-05-08 00:28:39 -07003405int module_get_kallsym(unsigned int symnum, unsigned long *value, char *type,
3406 char *name, char *module_name, int *exported)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003407{
3408 struct module *mod;
3409
Rusty Russellcb2a5202008-01-14 00:55:03 -08003410 preempt_disable();
Andi Kleend72b3752008-08-30 10:09:00 +02003411 list_for_each_entry_rcu(mod, &modules, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003412 if (symnum < mod->num_symtab) {
3413 *value = mod->symtab[symnum].st_value;
3414 *type = mod->symtab[symnum].st_info;
Andreas Gruenbacher098c5ee2006-07-14 00:24:04 -07003415 strlcpy(name, mod->strtab + mod->symtab[symnum].st_name,
Tejun Heo9281ace2007-07-17 04:03:51 -07003416 KSYM_NAME_LEN);
3417 strlcpy(module_name, mod->name, MODULE_NAME_LEN);
Tim Abbottca4787b2009-01-05 08:40:10 -06003418 *exported = is_exported(name, *value, mod);
Rusty Russellcb2a5202008-01-14 00:55:03 -08003419 preempt_enable();
Alexey Dobriyanea078902007-05-08 00:28:39 -07003420 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003421 }
3422 symnum -= mod->num_symtab;
3423 }
Rusty Russellcb2a5202008-01-14 00:55:03 -08003424 preempt_enable();
Alexey Dobriyanea078902007-05-08 00:28:39 -07003425 return -ERANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003426}
3427
3428static unsigned long mod_find_symname(struct module *mod, const char *name)
3429{
3430 unsigned int i;
3431
3432 for (i = 0; i < mod->num_symtab; i++)
Keith Owens54e8ce42006-02-03 03:03:53 -08003433 if (strcmp(name, mod->strtab+mod->symtab[i].st_name) == 0 &&
3434 mod->symtab[i].st_info != 'U')
Linus Torvalds1da177e2005-04-16 15:20:36 -07003435 return mod->symtab[i].st_value;
3436 return 0;
3437}
3438
3439/* Look for this name: can be of form module:name. */
3440unsigned long module_kallsyms_lookup_name(const char *name)
3441{
3442 struct module *mod;
3443 char *colon;
3444 unsigned long ret = 0;
3445
3446 /* Don't lock: we're in enough trouble already. */
Rusty Russellcb2a5202008-01-14 00:55:03 -08003447 preempt_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003448 if ((colon = strchr(name, ':')) != NULL) {
3449 *colon = '\0';
3450 if ((mod = find_module(name)) != NULL)
3451 ret = mod_find_symname(mod, colon+1);
3452 *colon = ':';
3453 } else {
Andi Kleend72b3752008-08-30 10:09:00 +02003454 list_for_each_entry_rcu(mod, &modules, list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003455 if ((ret = mod_find_symname(mod, name)) != 0)
3456 break;
3457 }
Rusty Russellcb2a5202008-01-14 00:55:03 -08003458 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003459 return ret;
3460}
Anders Kaseorg75a66612008-12-05 19:03:58 -05003461
3462int module_kallsyms_on_each_symbol(int (*fn)(void *, const char *,
3463 struct module *, unsigned long),
3464 void *data)
3465{
3466 struct module *mod;
3467 unsigned int i;
3468 int ret;
3469
3470 list_for_each_entry(mod, &modules, list) {
3471 for (i = 0; i < mod->num_symtab; i++) {
3472 ret = fn(data, mod->strtab + mod->symtab[i].st_name,
3473 mod, mod->symtab[i].st_value);
3474 if (ret != 0)
3475 return ret;
3476 }
3477 }
3478 return 0;
3479}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003480#endif /* CONFIG_KALLSYMS */
3481
Arjan van de Ven21aa9282008-01-25 21:08:33 +01003482static char *module_flags(struct module *mod, char *buf)
Florin Malitafa3ba2e82006-10-11 01:21:48 -07003483{
3484 int bx = 0;
3485
Arjan van de Ven21aa9282008-01-25 21:08:33 +01003486 if (mod->taints ||
3487 mod->state == MODULE_STATE_GOING ||
3488 mod->state == MODULE_STATE_COMING) {
Florin Malitafa3ba2e82006-10-11 01:21:48 -07003489 buf[bx++] = '(';
Kay Sieverscca3e702012-01-13 09:32:15 +10303490 bx += module_flags_taint(mod, buf + bx);
Arjan van de Ven21aa9282008-01-25 21:08:33 +01003491 /* Show a - for module-is-being-unloaded */
3492 if (mod->state == MODULE_STATE_GOING)
3493 buf[bx++] = '-';
3494 /* Show a + for module-is-being-loaded */
3495 if (mod->state == MODULE_STATE_COMING)
3496 buf[bx++] = '+';
Florin Malitafa3ba2e82006-10-11 01:21:48 -07003497 buf[bx++] = ')';
3498 }
3499 buf[bx] = '\0';
3500
3501 return buf;
3502}
3503
Alexey Dobriyan3b5d5c62008-10-06 13:19:27 +04003504#ifdef CONFIG_PROC_FS
3505/* Called by the /proc file system to return a list of modules. */
3506static void *m_start(struct seq_file *m, loff_t *pos)
3507{
3508 mutex_lock(&module_mutex);
3509 return seq_list_start(&modules, *pos);
3510}
3511
3512static void *m_next(struct seq_file *m, void *p, loff_t *pos)
3513{
3514 return seq_list_next(p, &modules, pos);
3515}
3516
3517static void m_stop(struct seq_file *m, void *p)
3518{
3519 mutex_unlock(&module_mutex);
3520}
3521
Linus Torvalds1da177e2005-04-16 15:20:36 -07003522static int m_show(struct seq_file *m, void *p)
3523{
3524 struct module *mod = list_entry(p, struct module, list);
Florin Malitafa3ba2e82006-10-11 01:21:48 -07003525 char buf[8];
3526
Denys Vlasenko2f0f2a32008-07-22 19:24:27 -05003527 seq_printf(m, "%s %u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003528 mod->name, mod->init_size + mod->core_size);
3529 print_unload_info(m, mod);
3530
3531 /* Informative for users. */
3532 seq_printf(m, " %s",
3533 mod->state == MODULE_STATE_GOING ? "Unloading":
3534 mod->state == MODULE_STATE_COMING ? "Loading":
3535 "Live");
3536 /* Used by oprofile and other similar tools. */
Kees Cook9f36e2c2011-03-22 16:34:22 -07003537 seq_printf(m, " 0x%pK", mod->module_core);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003538
Florin Malitafa3ba2e82006-10-11 01:21:48 -07003539 /* Taints info */
3540 if (mod->taints)
Arjan van de Ven21aa9282008-01-25 21:08:33 +01003541 seq_printf(m, " %s", module_flags(mod, buf));
Florin Malitafa3ba2e82006-10-11 01:21:48 -07003542
Linus Torvalds1da177e2005-04-16 15:20:36 -07003543 seq_printf(m, "\n");
3544 return 0;
3545}
3546
3547/* Format: modulename size refcount deps address
3548
3549 Where refcount is a number or -, and deps is a comma-separated list
3550 of depends or -.
3551*/
Alexey Dobriyan3b5d5c62008-10-06 13:19:27 +04003552static const struct seq_operations modules_op = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003553 .start = m_start,
3554 .next = m_next,
3555 .stop = m_stop,
3556 .show = m_show
3557};
3558
Alexey Dobriyan3b5d5c62008-10-06 13:19:27 +04003559static int modules_open(struct inode *inode, struct file *file)
3560{
3561 return seq_open(file, &modules_op);
3562}
3563
3564static const struct file_operations proc_modules_operations = {
3565 .open = modules_open,
3566 .read = seq_read,
3567 .llseek = seq_lseek,
3568 .release = seq_release,
3569};
3570
3571static int __init proc_modules_init(void)
3572{
3573 proc_create("modules", 0, NULL, &proc_modules_operations);
3574 return 0;
3575}
3576module_init(proc_modules_init);
3577#endif
3578
Linus Torvalds1da177e2005-04-16 15:20:36 -07003579/* Given an address, look for it in the module exception tables. */
3580const struct exception_table_entry *search_module_extables(unsigned long addr)
3581{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003582 const struct exception_table_entry *e = NULL;
3583 struct module *mod;
3584
Rusty Russell24da1cb2007-07-15 23:41:46 -07003585 preempt_disable();
Andi Kleend72b3752008-08-30 10:09:00 +02003586 list_for_each_entry_rcu(mod, &modules, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003587 if (mod->num_exentries == 0)
3588 continue;
Daniel Walker22a8bde2007-10-18 03:06:07 -07003589
Linus Torvalds1da177e2005-04-16 15:20:36 -07003590 e = search_extable(mod->extable,
3591 mod->extable + mod->num_exentries - 1,
3592 addr);
3593 if (e)
3594 break;
3595 }
Rusty Russell24da1cb2007-07-15 23:41:46 -07003596 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003597
3598 /* Now, if we found one, we are running inside it now, hence
Daniel Walker22a8bde2007-10-18 03:06:07 -07003599 we cannot unload the module, hence no refcnt needed. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003600 return e;
3601}
3602
Ingo Molnar4d435f92006-07-03 00:24:24 -07003603/*
Rusty Russelle6104992009-03-31 13:05:31 -06003604 * is_module_address - is this address inside a module?
3605 * @addr: the address to check.
3606 *
3607 * See is_module_text_address() if you simply want to see if the address
3608 * is code (not data).
Ingo Molnar4d435f92006-07-03 00:24:24 -07003609 */
Rusty Russelle6104992009-03-31 13:05:31 -06003610bool is_module_address(unsigned long addr)
Ingo Molnar4d435f92006-07-03 00:24:24 -07003611{
Rusty Russelle6104992009-03-31 13:05:31 -06003612 bool ret;
Ingo Molnar4d435f92006-07-03 00:24:24 -07003613
Rusty Russell24da1cb2007-07-15 23:41:46 -07003614 preempt_disable();
Rusty Russelle6104992009-03-31 13:05:31 -06003615 ret = __module_address(addr) != NULL;
Rusty Russell24da1cb2007-07-15 23:41:46 -07003616 preempt_enable();
Ingo Molnar4d435f92006-07-03 00:24:24 -07003617
Rusty Russelle6104992009-03-31 13:05:31 -06003618 return ret;
Ingo Molnar4d435f92006-07-03 00:24:24 -07003619}
3620
Rusty Russelle6104992009-03-31 13:05:31 -06003621/*
3622 * __module_address - get the module which contains an address.
3623 * @addr: the address.
3624 *
3625 * Must be called with preempt disabled or module mutex held so that
3626 * module doesn't get freed during this.
3627 */
Linus Torvalds714f83d2009-04-05 11:04:19 -07003628struct module *__module_address(unsigned long addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003629{
3630 struct module *mod;
3631
Rusty Russell3a642e92008-07-22 19:24:28 -05003632 if (addr < module_addr_min || addr > module_addr_max)
3633 return NULL;
3634
Andi Kleend72b3752008-08-30 10:09:00 +02003635 list_for_each_entry_rcu(mod, &modules, list)
Rusty Russelle6104992009-03-31 13:05:31 -06003636 if (within_module_core(addr, mod)
3637 || within_module_init(addr, mod))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003638 return mod;
3639 return NULL;
3640}
Tim Abbottc6b37802008-12-05 19:03:59 -05003641EXPORT_SYMBOL_GPL(__module_address);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003642
Rusty Russelle6104992009-03-31 13:05:31 -06003643/*
3644 * is_module_text_address - is this address inside module code?
3645 * @addr: the address to check.
3646 *
3647 * See is_module_address() if you simply want to see if the address is
3648 * anywhere in a module. See kernel_text_address() for testing if an
3649 * address corresponds to kernel or module code.
3650 */
3651bool is_module_text_address(unsigned long addr)
3652{
3653 bool ret;
3654
3655 preempt_disable();
3656 ret = __module_text_address(addr) != NULL;
3657 preempt_enable();
3658
3659 return ret;
3660}
3661
3662/*
3663 * __module_text_address - get the module whose code contains an address.
3664 * @addr: the address.
3665 *
3666 * Must be called with preempt disabled or module mutex held so that
3667 * module doesn't get freed during this.
3668 */
3669struct module *__module_text_address(unsigned long addr)
3670{
3671 struct module *mod = __module_address(addr);
3672 if (mod) {
3673 /* Make sure it's within the text section. */
3674 if (!within(addr, mod->module_init, mod->init_text_size)
3675 && !within(addr, mod->module_core, mod->core_text_size))
3676 mod = NULL;
3677 }
3678 return mod;
3679}
Tim Abbottc6b37802008-12-05 19:03:59 -05003680EXPORT_SYMBOL_GPL(__module_text_address);
Rusty Russelle6104992009-03-31 13:05:31 -06003681
Linus Torvalds1da177e2005-04-16 15:20:36 -07003682/* Don't grab lock, we're oopsing. */
3683void print_modules(void)
3684{
3685 struct module *mod;
Randy Dunlap2bc2d612006-10-02 02:17:02 -07003686 char buf[8];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003687
Linus Torvaldsb2311252009-06-16 11:07:14 -07003688 printk(KERN_DEFAULT "Modules linked in:");
Andi Kleend72b3752008-08-30 10:09:00 +02003689 /* Most callers should already have preempt disabled, but make sure */
3690 preempt_disable();
3691 list_for_each_entry_rcu(mod, &modules, list)
Arjan van de Ven21aa9282008-01-25 21:08:33 +01003692 printk(" %s%s", mod->name, module_flags(mod, buf));
Andi Kleend72b3752008-08-30 10:09:00 +02003693 preempt_enable();
Arjan van de Vene14af7e2008-01-25 21:08:33 +01003694 if (last_unloaded_module[0])
3695 printk(" [last unloaded: %s]", last_unloaded_module);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003696 printk("\n");
3697}
3698
Linus Torvalds1da177e2005-04-16 15:20:36 -07003699#ifdef CONFIG_MODVERSIONS
Rusty Russell8c8ef422009-03-31 13:05:34 -06003700/* Generate the signature for all relevant module structures here.
3701 * If these change, we don't want to try to parse the module. */
3702void module_layout(struct module *mod,
3703 struct modversion_info *ver,
3704 struct kernel_param *kp,
3705 struct kernel_symbol *ks,
Mathieu Desnoyers65498642011-01-26 17:26:22 -05003706 struct tracepoint * const *tp)
Rusty Russell8c8ef422009-03-31 13:05:34 -06003707{
3708}
3709EXPORT_SYMBOL(module_layout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003710#endif