blob: c3a2ee8e367903b6334b405a73e64bb381aa0de6 [file] [log] [blame]
Arjan van de Venf71d20e2006-06-28 04:26:45 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 Copyright (C) 2002 Richard Henderson
Rusty Russell51f3d0f2010-08-05 12:59:13 -06003 Copyright (C) 2001 Rusty Russell, 2002, 2010 Rusty Russell IBM.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18*/
Paul Gortmaker9984de12011-05-23 14:51:41 -040019#include <linux/export.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <linux/moduleloader.h>
Steven Rostedt6d723732009-04-10 14:53:50 -040021#include <linux/ftrace_event.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/init.h>
Alexey Dobriyanae84e322007-05-08 00:28:38 -070023#include <linux/kallsyms.h>
Kees Cook34e11692012-10-16 07:31:07 +103024#include <linux/file.h>
Alexey Dobriyan3b5d5c62008-10-06 13:19:27 +040025#include <linux/fs.h>
Roland McGrath6d760132007-10-16 23:26:40 -070026#include <linux/sysfs.h>
Randy Dunlap9f158332005-09-13 01:25:16 -070027#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <linux/slab.h>
29#include <linux/vmalloc.h>
30#include <linux/elf.h>
Alexey Dobriyan3b5d5c62008-10-06 13:19:27 +040031#include <linux/proc_fs.h>
Kees Cook2e72d512012-10-16 07:32:07 +103032#include <linux/security.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include <linux/seq_file.h>
34#include <linux/syscalls.h>
35#include <linux/fcntl.h>
36#include <linux/rcupdate.h>
Randy.Dunlapc59ede72006-01-11 12:17:46 -080037#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#include <linux/cpu.h>
39#include <linux/moduleparam.h>
40#include <linux/errno.h>
41#include <linux/err.h>
42#include <linux/vermagic.h>
43#include <linux/notifier.h>
Al Virof6a57032006-10-18 01:47:25 -040044#include <linux/sched.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#include <linux/stop_machine.h>
46#include <linux/device.h>
Matt Domschc988d2b2005-06-23 22:05:15 -070047#include <linux/string.h>
Arjan van de Ven97d1f152006-03-23 03:00:24 -080048#include <linux/mutex.h>
Andi Kleend72b3752008-08-30 10:09:00 +020049#include <linux/rculist.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070050#include <asm/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070051#include <asm/cacheflush.h>
Bernd Schmidteb8cdec2009-09-21 17:03:57 -070052#include <asm/mmu_context.h>
Sam Ravnborgb817f6f2006-06-09 21:53:55 +020053#include <linux/license.h>
Christoph Lameter6d762392008-02-08 04:18:42 -080054#include <asm/sections.h>
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -040055#include <linux/tracepoint.h>
Steven Rostedt90d595f2008-08-14 15:45:09 -040056#include <linux/ftrace.h>
Arjan van de Ven22a9d642009-01-07 08:45:46 -080057#include <linux/async.h>
Tejun Heofbf59bc2009-02-20 16:29:08 +090058#include <linux/percpu.h>
Catalin Marinas4f2294b2009-06-11 13:23:20 +010059#include <linux/kmemleak.h>
Jason Baronbf5438fc2010-09-17 11:09:00 -040060#include <linux/jump_label.h>
matthieu castet84e1c6b2010-11-16 22:35:16 +010061#include <linux/pfn.h>
Alessio Igor Bogani403ed272011-04-20 11:10:52 +020062#include <linux/bsearch.h>
David Howells1d0059f2012-09-26 10:09:50 +010063#include <linux/fips.h>
Rusty Russell2f3238a2012-10-22 18:09:41 +103064#include <uapi/linux/module.h>
Rusty Russell106a4ee2012-09-26 10:09:40 +010065#include "module-internal.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070066
Li Zefan7ead8b82009-08-17 16:56:28 +080067#define CREATE_TRACE_POINTS
68#include <trace/events/module.h>
69
Linus Torvalds1da177e2005-04-16 15:20:36 -070070#ifndef ARCH_SHF_SMALL
71#define ARCH_SHF_SMALL 0
72#endif
73
matthieu castet84e1c6b2010-11-16 22:35:16 +010074/*
75 * Modules' sections will be aligned on page boundaries
76 * to ensure complete separation of code and data, but
77 * only when CONFIG_DEBUG_SET_MODULE_RONX=y
78 */
79#ifdef CONFIG_DEBUG_SET_MODULE_RONX
80# define debug_align(X) ALIGN(X, PAGE_SIZE)
81#else
82# define debug_align(X) (X)
83#endif
84
85/*
86 * Given BASE and SIZE this macro calculates the number of pages the
87 * memory regions occupies
88 */
89#define MOD_NUMBER_OF_PAGES(BASE, SIZE) (((SIZE) > 0) ? \
90 (PFN_DOWN((unsigned long)(BASE) + (SIZE) - 1) - \
91 PFN_DOWN((unsigned long)BASE) + 1) \
92 : (0UL))
93
Linus Torvalds1da177e2005-04-16 15:20:36 -070094/* If this is set, the section belongs in the init part of the module */
95#define INIT_OFFSET_MASK (1UL << (BITS_PER_LONG-1))
96
Rusty Russell75676502010-06-05 11:17:36 -060097/*
98 * Mutex protects:
99 * 1) List of modules (also safely readable with preempt_disable),
100 * 2) module_use links,
101 * 3) module_addr_min/module_addr_max.
Andi Kleend72b3752008-08-30 10:09:00 +0200102 * (delete uses stop_machine/add uses RCU list operations). */
Tim Abbottc6b37802008-12-05 19:03:59 -0500103DEFINE_MUTEX(module_mutex);
104EXPORT_SYMBOL_GPL(module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105static LIST_HEAD(modules);
Jason Wessel67fc4e02010-05-20 21:04:21 -0500106#ifdef CONFIG_KGDB_KDB
107struct list_head *kdb_modules = &modules; /* kdb needs the list of modules */
108#endif /* CONFIG_KGDB_KDB */
109
Rusty Russell106a4ee2012-09-26 10:09:40 +0100110#ifdef CONFIG_MODULE_SIG
111#ifdef CONFIG_MODULE_SIG_FORCE
112static bool sig_enforce = true;
113#else
114static bool sig_enforce = false;
115
116static int param_set_bool_enable_only(const char *val,
117 const struct kernel_param *kp)
118{
119 int err;
120 bool test;
121 struct kernel_param dummy_kp = *kp;
122
123 dummy_kp.arg = &test;
124
125 err = param_set_bool(val, &dummy_kp);
126 if (err)
127 return err;
128
129 /* Don't let them unset it once it's set! */
130 if (!test && sig_enforce)
131 return -EROFS;
132
133 if (test)
134 sig_enforce = true;
135 return 0;
136}
137
138static const struct kernel_param_ops param_ops_bool_enable_only = {
139 .set = param_set_bool_enable_only,
140 .get = param_get_bool,
141};
142#define param_check_bool_enable_only param_check_bool
143
144module_param(sig_enforce, bool_enable_only, 0644);
145#endif /* !CONFIG_MODULE_SIG_FORCE */
146#endif /* CONFIG_MODULE_SIG */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147
Stephen Rothwell19e45292009-04-14 17:27:18 +1000148/* Block module loading/unloading? */
149int modules_disabled = 0;
Dave Young02608be2012-02-01 10:33:14 +0800150core_param(nomodule, modules_disabled, bint, 0);
Stephen Rothwell19e45292009-04-14 17:27:18 +1000151
Rusty Russellc9a3ba52008-01-29 17:13:18 -0500152/* Waiting for a module to finish initializing? */
153static DECLARE_WAIT_QUEUE_HEAD(module_wq);
154
Alan Sterne041c682006-03-27 01:16:30 -0800155static BLOCKING_NOTIFIER_HEAD(module_notify_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156
Rusty Russell75676502010-06-05 11:17:36 -0600157/* Bounds of module allocation, for speeding __module_address.
158 * Protected by module_mutex. */
Rusty Russell3a642e92008-07-22 19:24:28 -0500159static unsigned long module_addr_min = -1UL, module_addr_max = 0;
160
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161int register_module_notifier(struct notifier_block * nb)
162{
Alan Sterne041c682006-03-27 01:16:30 -0800163 return blocking_notifier_chain_register(&module_notify_list, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164}
165EXPORT_SYMBOL(register_module_notifier);
166
167int unregister_module_notifier(struct notifier_block * nb)
168{
Alan Sterne041c682006-03-27 01:16:30 -0800169 return blocking_notifier_chain_unregister(&module_notify_list, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170}
171EXPORT_SYMBOL(unregister_module_notifier);
172
Rusty Russelleded41c2010-08-05 12:59:07 -0600173struct load_info {
174 Elf_Ehdr *hdr;
175 unsigned long len;
176 Elf_Shdr *sechdrs;
Rusty Russell6526c532010-08-05 12:59:10 -0600177 char *secstrings, *strtab;
Rusty Russelld9131882010-08-05 12:59:08 -0600178 unsigned long symoffs, stroffs;
Rusty Russell811d66a2010-08-05 12:59:12 -0600179 struct _ddebug *debug;
180 unsigned int num_debug;
Rusty Russell106a4ee2012-09-26 10:09:40 +0100181 bool sig_ok;
Rusty Russelleded41c2010-08-05 12:59:07 -0600182 struct {
183 unsigned int sym, str, mod, vers, info, pcpu;
184 } index;
185};
186
Matti Linnanvuori9a4b9702007-11-08 08:37:38 -0800187/* We require a truly strong try_module_get(): 0 means failure due to
188 ongoing or failed initialization etc. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189static inline int strong_try_module_get(struct module *mod)
190{
Rusty Russell0d21b0e2013-01-12 11:38:44 +1030191 BUG_ON(mod && mod->state == MODULE_STATE_UNFORMED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 if (mod && mod->state == MODULE_STATE_COMING)
Rusty Russellc9a3ba52008-01-29 17:13:18 -0500193 return -EBUSY;
194 if (try_module_get(mod))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 return 0;
Rusty Russellc9a3ba52008-01-29 17:13:18 -0500196 else
197 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198}
199
Florin Malitafa3ba2e82006-10-11 01:21:48 -0700200static inline void add_taint_module(struct module *mod, unsigned flag)
201{
202 add_taint(flag);
Andi Kleen25ddbb12008-10-15 22:01:41 -0700203 mod->taints |= (1U << flag);
Florin Malitafa3ba2e82006-10-11 01:21:48 -0700204}
205
Robert P. J. Day02a3e592007-05-09 07:26:28 +0200206/*
207 * A thread that wants to hold a reference to a module only while it
208 * is running can call this to safely exit. nfsd and lockd use this.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 */
210void __module_put_and_exit(struct module *mod, long code)
211{
212 module_put(mod);
213 do_exit(code);
214}
215EXPORT_SYMBOL(__module_put_and_exit);
Daniel Walker22a8bde2007-10-18 03:06:07 -0700216
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217/* Find a module section: 0 means not found. */
Rusty Russell49668682010-08-05 12:59:10 -0600218static unsigned int find_sec(const struct load_info *info, const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219{
220 unsigned int i;
221
Rusty Russell49668682010-08-05 12:59:10 -0600222 for (i = 1; i < info->hdr->e_shnum; i++) {
223 Elf_Shdr *shdr = &info->sechdrs[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 /* Alloc bit cleared means "ignore it." */
Rusty Russell49668682010-08-05 12:59:10 -0600225 if ((shdr->sh_flags & SHF_ALLOC)
226 && strcmp(info->secstrings + shdr->sh_name, name) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 return i;
Rusty Russell49668682010-08-05 12:59:10 -0600228 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 return 0;
230}
231
Rusty Russell5e458cc2008-10-22 10:00:13 -0500232/* Find a module section, or NULL. */
Rusty Russell49668682010-08-05 12:59:10 -0600233static void *section_addr(const struct load_info *info, const char *name)
Rusty Russell5e458cc2008-10-22 10:00:13 -0500234{
235 /* Section 0 has sh_addr 0. */
Rusty Russell49668682010-08-05 12:59:10 -0600236 return (void *)info->sechdrs[find_sec(info, name)].sh_addr;
Rusty Russell5e458cc2008-10-22 10:00:13 -0500237}
238
239/* Find a module section, or NULL. Fill in number of "objects" in section. */
Rusty Russell49668682010-08-05 12:59:10 -0600240static void *section_objs(const struct load_info *info,
Rusty Russell5e458cc2008-10-22 10:00:13 -0500241 const char *name,
242 size_t object_size,
243 unsigned int *num)
244{
Rusty Russell49668682010-08-05 12:59:10 -0600245 unsigned int sec = find_sec(info, name);
Rusty Russell5e458cc2008-10-22 10:00:13 -0500246
247 /* Section 0 has sh_addr 0 and sh_size 0. */
Rusty Russell49668682010-08-05 12:59:10 -0600248 *num = info->sechdrs[sec].sh_size / object_size;
249 return (void *)info->sechdrs[sec].sh_addr;
Rusty Russell5e458cc2008-10-22 10:00:13 -0500250}
251
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252/* Provided by the linker */
253extern const struct kernel_symbol __start___ksymtab[];
254extern const struct kernel_symbol __stop___ksymtab[];
255extern const struct kernel_symbol __start___ksymtab_gpl[];
256extern const struct kernel_symbol __stop___ksymtab_gpl[];
Greg Kroah-Hartman9f28bb72006-03-20 13:17:13 -0800257extern const struct kernel_symbol __start___ksymtab_gpl_future[];
258extern const struct kernel_symbol __stop___ksymtab_gpl_future[];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259extern const unsigned long __start___kcrctab[];
260extern const unsigned long __start___kcrctab_gpl[];
Greg Kroah-Hartman9f28bb72006-03-20 13:17:13 -0800261extern const unsigned long __start___kcrctab_gpl_future[];
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500262#ifdef CONFIG_UNUSED_SYMBOLS
263extern const struct kernel_symbol __start___ksymtab_unused[];
264extern const struct kernel_symbol __stop___ksymtab_unused[];
265extern const struct kernel_symbol __start___ksymtab_unused_gpl[];
266extern const struct kernel_symbol __stop___ksymtab_unused_gpl[];
Arjan van de Venf71d20e2006-06-28 04:26:45 -0700267extern const unsigned long __start___kcrctab_unused[];
268extern const unsigned long __start___kcrctab_unused_gpl[];
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500269#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270
271#ifndef CONFIG_MODVERSIONS
272#define symversion(base, idx) NULL
273#else
Andrew Mortonf83ca9f2006-03-28 01:56:20 -0800274#define symversion(base, idx) ((base != NULL) ? ((base) + (idx)) : NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275#endif
276
Rusty Russelldafd0942008-07-22 19:24:25 -0500277static bool each_symbol_in_section(const struct symsearch *arr,
278 unsigned int arrsize,
279 struct module *owner,
280 bool (*fn)(const struct symsearch *syms,
281 struct module *owner,
Rusty Russellde4d8d52011-04-19 21:49:58 +0200282 void *data),
Rusty Russelldafd0942008-07-22 19:24:25 -0500283 void *data)
Sam Ravnborg3fd68052006-02-08 21:16:45 +0100284{
Rusty Russellde4d8d52011-04-19 21:49:58 +0200285 unsigned int j;
Rusty Russelldafd0942008-07-22 19:24:25 -0500286
287 for (j = 0; j < arrsize; j++) {
Rusty Russellde4d8d52011-04-19 21:49:58 +0200288 if (fn(&arr[j], owner, data))
289 return true;
Rusty Russelldafd0942008-07-22 19:24:25 -0500290 }
291
292 return false;
Sam Ravnborg3fd68052006-02-08 21:16:45 +0100293}
294
Rusty Russelldafd0942008-07-22 19:24:25 -0500295/* Returns true as soon as fn returns true, otherwise false. */
Rusty Russellde4d8d52011-04-19 21:49:58 +0200296bool each_symbol_section(bool (*fn)(const struct symsearch *arr,
297 struct module *owner,
298 void *data),
299 void *data)
Arjan van de Venf71d20e2006-06-28 04:26:45 -0700300{
Rusty Russelldafd0942008-07-22 19:24:25 -0500301 struct module *mod;
Linus Torvalds44032e62010-08-05 12:59:05 -0600302 static const struct symsearch arr[] = {
Rusty Russelldafd0942008-07-22 19:24:25 -0500303 { __start___ksymtab, __stop___ksymtab, __start___kcrctab,
304 NOT_GPL_ONLY, false },
305 { __start___ksymtab_gpl, __stop___ksymtab_gpl,
306 __start___kcrctab_gpl,
307 GPL_ONLY, false },
308 { __start___ksymtab_gpl_future, __stop___ksymtab_gpl_future,
309 __start___kcrctab_gpl_future,
310 WILL_BE_GPL_ONLY, false },
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500311#ifdef CONFIG_UNUSED_SYMBOLS
Rusty Russelldafd0942008-07-22 19:24:25 -0500312 { __start___ksymtab_unused, __stop___ksymtab_unused,
313 __start___kcrctab_unused,
314 NOT_GPL_ONLY, true },
315 { __start___ksymtab_unused_gpl, __stop___ksymtab_unused_gpl,
316 __start___kcrctab_unused_gpl,
317 GPL_ONLY, true },
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500318#endif
Rusty Russelldafd0942008-07-22 19:24:25 -0500319 };
320
321 if (each_symbol_in_section(arr, ARRAY_SIZE(arr), NULL, fn, data))
322 return true;
323
Andi Kleend72b3752008-08-30 10:09:00 +0200324 list_for_each_entry_rcu(mod, &modules, list) {
Rusty Russelldafd0942008-07-22 19:24:25 -0500325 struct symsearch arr[] = {
326 { mod->syms, mod->syms + mod->num_syms, mod->crcs,
327 NOT_GPL_ONLY, false },
328 { mod->gpl_syms, mod->gpl_syms + mod->num_gpl_syms,
329 mod->gpl_crcs,
330 GPL_ONLY, false },
331 { mod->gpl_future_syms,
332 mod->gpl_future_syms + mod->num_gpl_future_syms,
333 mod->gpl_future_crcs,
334 WILL_BE_GPL_ONLY, false },
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500335#ifdef CONFIG_UNUSED_SYMBOLS
Rusty Russelldafd0942008-07-22 19:24:25 -0500336 { mod->unused_syms,
337 mod->unused_syms + mod->num_unused_syms,
338 mod->unused_crcs,
339 NOT_GPL_ONLY, true },
340 { mod->unused_gpl_syms,
341 mod->unused_gpl_syms + mod->num_unused_gpl_syms,
342 mod->unused_gpl_crcs,
343 GPL_ONLY, true },
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500344#endif
Rusty Russelldafd0942008-07-22 19:24:25 -0500345 };
346
Rusty Russell0d21b0e2013-01-12 11:38:44 +1030347 if (mod->state == MODULE_STATE_UNFORMED)
348 continue;
349
Rusty Russelldafd0942008-07-22 19:24:25 -0500350 if (each_symbol_in_section(arr, ARRAY_SIZE(arr), mod, fn, data))
351 return true;
352 }
353 return false;
Arjan van de Venf71d20e2006-06-28 04:26:45 -0700354}
Rusty Russellde4d8d52011-04-19 21:49:58 +0200355EXPORT_SYMBOL_GPL(each_symbol_section);
Arjan van de Venf71d20e2006-06-28 04:26:45 -0700356
Rusty Russelldafd0942008-07-22 19:24:25 -0500357struct find_symbol_arg {
358 /* Input */
359 const char *name;
360 bool gplok;
361 bool warn;
362
363 /* Output */
364 struct module *owner;
365 const unsigned long *crc;
Tim Abbott414fd312008-12-05 19:03:56 -0500366 const struct kernel_symbol *sym;
Rusty Russelldafd0942008-07-22 19:24:25 -0500367};
368
Rusty Russellde4d8d52011-04-19 21:49:58 +0200369static bool check_symbol(const struct symsearch *syms,
370 struct module *owner,
371 unsigned int symnum, void *data)
Rusty Russellad9546c2008-05-01 21:14:59 -0500372{
Rusty Russelldafd0942008-07-22 19:24:25 -0500373 struct find_symbol_arg *fsa = data;
374
Rusty Russelldafd0942008-07-22 19:24:25 -0500375 if (!fsa->gplok) {
376 if (syms->licence == GPL_ONLY)
377 return false;
378 if (syms->licence == WILL_BE_GPL_ONLY && fsa->warn) {
379 printk(KERN_WARNING "Symbol %s is being used "
380 "by a non-GPL module, which will not "
381 "be allowed in the future\n", fsa->name);
Rusty Russelldafd0942008-07-22 19:24:25 -0500382 }
383 }
384
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500385#ifdef CONFIG_UNUSED_SYMBOLS
Rusty Russelldafd0942008-07-22 19:24:25 -0500386 if (syms->unused && fsa->warn) {
Rusty Russellad9546c2008-05-01 21:14:59 -0500387 printk(KERN_WARNING "Symbol %s is marked as UNUSED, "
Rusty Russelldafd0942008-07-22 19:24:25 -0500388 "however this module is using it.\n", fsa->name);
Rusty Russellad9546c2008-05-01 21:14:59 -0500389 printk(KERN_WARNING
390 "This symbol will go away in the future.\n");
391 printk(KERN_WARNING
392 "Please evalute if this is the right api to use and if "
393 "it really is, submit a report the linux kernel "
394 "mailinglist together with submitting your code for "
395 "inclusion.\n");
396 }
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500397#endif
Rusty Russelldafd0942008-07-22 19:24:25 -0500398
399 fsa->owner = owner;
400 fsa->crc = symversion(syms->crcs, symnum);
Tim Abbott414fd312008-12-05 19:03:56 -0500401 fsa->sym = &syms->start[symnum];
Rusty Russellad9546c2008-05-01 21:14:59 -0500402 return true;
403}
404
Alessio Igor Bogani403ed272011-04-20 11:10:52 +0200405static int cmp_name(const void *va, const void *vb)
406{
407 const char *a;
408 const struct kernel_symbol *b;
409 a = va; b = vb;
410 return strcmp(a, b->name);
411}
412
Rusty Russellde4d8d52011-04-19 21:49:58 +0200413static bool find_symbol_in_section(const struct symsearch *syms,
414 struct module *owner,
415 void *data)
416{
417 struct find_symbol_arg *fsa = data;
Alessio Igor Bogani403ed272011-04-20 11:10:52 +0200418 struct kernel_symbol *sym;
Rusty Russellde4d8d52011-04-19 21:49:58 +0200419
Alessio Igor Bogani403ed272011-04-20 11:10:52 +0200420 sym = bsearch(fsa->name, syms->start, syms->stop - syms->start,
421 sizeof(struct kernel_symbol), cmp_name);
422
423 if (sym != NULL && check_symbol(syms, owner, sym - syms->start, data))
424 return true;
425
Rusty Russellde4d8d52011-04-19 21:49:58 +0200426 return false;
427}
428
Tim Abbott414fd312008-12-05 19:03:56 -0500429/* Find a symbol and return it, along with, (optional) crc and
Rusty Russell75676502010-06-05 11:17:36 -0600430 * (optional) module which owns it. Needs preempt disabled or module_mutex. */
Tim Abbottc6b37802008-12-05 19:03:59 -0500431const struct kernel_symbol *find_symbol(const char *name,
432 struct module **owner,
433 const unsigned long **crc,
434 bool gplok,
435 bool warn)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436{
Rusty Russelldafd0942008-07-22 19:24:25 -0500437 struct find_symbol_arg fsa;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438
Rusty Russelldafd0942008-07-22 19:24:25 -0500439 fsa.name = name;
440 fsa.gplok = gplok;
441 fsa.warn = warn;
442
Rusty Russellde4d8d52011-04-19 21:49:58 +0200443 if (each_symbol_section(find_symbol_in_section, &fsa)) {
Rusty Russellad9546c2008-05-01 21:14:59 -0500444 if (owner)
Rusty Russelldafd0942008-07-22 19:24:25 -0500445 *owner = fsa.owner;
446 if (crc)
447 *crc = fsa.crc;
Tim Abbott414fd312008-12-05 19:03:56 -0500448 return fsa.sym;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 }
Rusty Russellad9546c2008-05-01 21:14:59 -0500450
Jim Cromie5e124162011-12-06 12:11:31 -0700451 pr_debug("Failed to find symbol %s\n", name);
Tim Abbott414fd312008-12-05 19:03:56 -0500452 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453}
Tim Abbottc6b37802008-12-05 19:03:59 -0500454EXPORT_SYMBOL_GPL(find_symbol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456/* Search for module by name: must hold module_mutex. */
Rusty Russell0d21b0e2013-01-12 11:38:44 +1030457static struct module *find_module_all(const char *name,
458 bool even_unformed)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459{
460 struct module *mod;
461
462 list_for_each_entry(mod, &modules, list) {
Rusty Russell0d21b0e2013-01-12 11:38:44 +1030463 if (!even_unformed && mod->state == MODULE_STATE_UNFORMED)
464 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 if (strcmp(mod->name, name) == 0)
466 return mod;
467 }
468 return NULL;
469}
Rusty Russell0d21b0e2013-01-12 11:38:44 +1030470
471struct module *find_module(const char *name)
472{
473 return find_module_all(name, false);
474}
Tim Abbottc6b37802008-12-05 19:03:59 -0500475EXPORT_SYMBOL_GPL(find_module);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476
477#ifdef CONFIG_SMP
Tejun Heofbf59bc2009-02-20 16:29:08 +0900478
Tejun Heo259354d2010-03-10 18:56:10 +0900479static inline void __percpu *mod_percpu(struct module *mod)
Tejun Heofbf59bc2009-02-20 16:29:08 +0900480{
Tejun Heo259354d2010-03-10 18:56:10 +0900481 return mod->percpu;
482}
Tejun Heofbf59bc2009-02-20 16:29:08 +0900483
Tejun Heo259354d2010-03-10 18:56:10 +0900484static int percpu_modalloc(struct module *mod,
485 unsigned long size, unsigned long align)
486{
Tejun Heofbf59bc2009-02-20 16:29:08 +0900487 if (align > PAGE_SIZE) {
488 printk(KERN_WARNING "%s: per-cpu alignment %li > %li\n",
Tejun Heo259354d2010-03-10 18:56:10 +0900489 mod->name, align, PAGE_SIZE);
Tejun Heofbf59bc2009-02-20 16:29:08 +0900490 align = PAGE_SIZE;
491 }
492
Tejun Heo259354d2010-03-10 18:56:10 +0900493 mod->percpu = __alloc_reserved_percpu(size, align);
494 if (!mod->percpu) {
Tejun Heofbf59bc2009-02-20 16:29:08 +0900495 printk(KERN_WARNING
Rusty Russelld9131882010-08-05 12:59:08 -0600496 "%s: Could not allocate %lu bytes percpu data\n",
497 mod->name, size);
Tejun Heo259354d2010-03-10 18:56:10 +0900498 return -ENOMEM;
499 }
500 mod->percpu_size = size;
501 return 0;
Tejun Heofbf59bc2009-02-20 16:29:08 +0900502}
503
Tejun Heo259354d2010-03-10 18:56:10 +0900504static void percpu_modfree(struct module *mod)
Tejun Heofbf59bc2009-02-20 16:29:08 +0900505{
Tejun Heo259354d2010-03-10 18:56:10 +0900506 free_percpu(mod->percpu);
Tejun Heofbf59bc2009-02-20 16:29:08 +0900507}
508
Rusty Russell49668682010-08-05 12:59:10 -0600509static unsigned int find_pcpusec(struct load_info *info)
Tejun Heo6b588c12009-02-20 16:29:07 +0900510{
Rusty Russell49668682010-08-05 12:59:10 -0600511 return find_sec(info, ".data..percpu");
Tejun Heo6b588c12009-02-20 16:29:07 +0900512}
513
Tejun Heo259354d2010-03-10 18:56:10 +0900514static void percpu_modcopy(struct module *mod,
515 const void *from, unsigned long size)
Tejun Heo6b588c12009-02-20 16:29:07 +0900516{
517 int cpu;
518
519 for_each_possible_cpu(cpu)
Tejun Heo259354d2010-03-10 18:56:10 +0900520 memcpy(per_cpu_ptr(mod->percpu, cpu), from, size);
Tejun Heo6b588c12009-02-20 16:29:07 +0900521}
522
Tejun Heo10fad5e2010-03-10 18:57:54 +0900523/**
524 * is_module_percpu_address - test whether address is from module static percpu
525 * @addr: address to test
526 *
527 * Test whether @addr belongs to module static percpu area.
528 *
529 * RETURNS:
530 * %true if @addr is from module static percpu area
531 */
532bool is_module_percpu_address(unsigned long addr)
533{
534 struct module *mod;
535 unsigned int cpu;
536
537 preempt_disable();
538
539 list_for_each_entry_rcu(mod, &modules, list) {
Rusty Russell0d21b0e2013-01-12 11:38:44 +1030540 if (mod->state == MODULE_STATE_UNFORMED)
541 continue;
Tejun Heo10fad5e2010-03-10 18:57:54 +0900542 if (!mod->percpu_size)
543 continue;
544 for_each_possible_cpu(cpu) {
545 void *start = per_cpu_ptr(mod->percpu, cpu);
546
547 if ((void *)addr >= start &&
548 (void *)addr < start + mod->percpu_size) {
549 preempt_enable();
550 return true;
551 }
552 }
553 }
554
555 preempt_enable();
556 return false;
Daniel Walker22a8bde2007-10-18 03:06:07 -0700557}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558
559#else /* ... !CONFIG_SMP */
Tejun Heo6b588c12009-02-20 16:29:07 +0900560
Tejun Heo259354d2010-03-10 18:56:10 +0900561static inline void __percpu *mod_percpu(struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562{
563 return NULL;
564}
Tejun Heo259354d2010-03-10 18:56:10 +0900565static inline int percpu_modalloc(struct module *mod,
566 unsigned long size, unsigned long align)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567{
Tejun Heo259354d2010-03-10 18:56:10 +0900568 return -ENOMEM;
569}
570static inline void percpu_modfree(struct module *mod)
571{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572}
Rusty Russell49668682010-08-05 12:59:10 -0600573static unsigned int find_pcpusec(struct load_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574{
575 return 0;
576}
Tejun Heo259354d2010-03-10 18:56:10 +0900577static inline void percpu_modcopy(struct module *mod,
578 const void *from, unsigned long size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579{
580 /* pcpusec should be 0, and size of that section should be 0. */
581 BUG_ON(size != 0);
582}
Tejun Heo10fad5e2010-03-10 18:57:54 +0900583bool is_module_percpu_address(unsigned long addr)
584{
585 return false;
586}
Tejun Heo6b588c12009-02-20 16:29:07 +0900587
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588#endif /* CONFIG_SMP */
589
Matt Domschc988d2b2005-06-23 22:05:15 -0700590#define MODINFO_ATTR(field) \
591static void setup_modinfo_##field(struct module *mod, const char *s) \
592{ \
593 mod->field = kstrdup(s, GFP_KERNEL); \
594} \
595static ssize_t show_modinfo_##field(struct module_attribute *mattr, \
Kay Sievers4befb022011-07-24 22:06:04 +0930596 struct module_kobject *mk, char *buffer) \
Matt Domschc988d2b2005-06-23 22:05:15 -0700597{ \
Kay Sievers4befb022011-07-24 22:06:04 +0930598 return sprintf(buffer, "%s\n", mk->mod->field); \
Matt Domschc988d2b2005-06-23 22:05:15 -0700599} \
600static int modinfo_##field##_exists(struct module *mod) \
601{ \
602 return mod->field != NULL; \
603} \
604static void free_modinfo_##field(struct module *mod) \
605{ \
Daniel Walker22a8bde2007-10-18 03:06:07 -0700606 kfree(mod->field); \
607 mod->field = NULL; \
Matt Domschc988d2b2005-06-23 22:05:15 -0700608} \
609static struct module_attribute modinfo_##field = { \
Tejun Heo7b595752007-06-14 03:45:17 +0900610 .attr = { .name = __stringify(field), .mode = 0444 }, \
Matt Domschc988d2b2005-06-23 22:05:15 -0700611 .show = show_modinfo_##field, \
612 .setup = setup_modinfo_##field, \
613 .test = modinfo_##field##_exists, \
614 .free = free_modinfo_##field, \
615};
616
617MODINFO_ATTR(version);
618MODINFO_ATTR(srcversion);
619
Arjan van de Vene14af7e2008-01-25 21:08:33 +0100620static char last_unloaded_module[MODULE_NAME_LEN+1];
621
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -0800622#ifdef CONFIG_MODULE_UNLOAD
Steven Rostedteb0c5372010-03-29 14:25:18 -0400623
624EXPORT_TRACEPOINT_SYMBOL(module_get);
625
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626/* Init the unload section of the module. */
Rusty Russell9f85a4b2010-08-05 12:59:04 -0600627static int module_unload_init(struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628{
Rusty Russell9f85a4b2010-08-05 12:59:04 -0600629 mod->refptr = alloc_percpu(struct module_ref);
630 if (!mod->refptr)
631 return -ENOMEM;
632
Linus Torvalds2c02dfe2010-05-31 12:19:37 -0700633 INIT_LIST_HEAD(&mod->source_list);
634 INIT_LIST_HEAD(&mod->target_list);
Christoph Lametere1783a22010-01-05 15:34:50 +0900635
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 /* Hold reference count during initialization. */
Nick Piggin5fbfb182010-04-01 19:09:40 +1100637 __this_cpu_write(mod->refptr->incs, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 /* Backwards compatibility macros put refcount during init. */
639 mod->waiter = current;
Rusty Russell9f85a4b2010-08-05 12:59:04 -0600640
641 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642}
643
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644/* Does a already use b? */
645static int already_uses(struct module *a, struct module *b)
646{
647 struct module_use *use;
648
Linus Torvalds2c02dfe2010-05-31 12:19:37 -0700649 list_for_each_entry(use, &b->source_list, source_list) {
650 if (use->source == a) {
Jim Cromie5e124162011-12-06 12:11:31 -0700651 pr_debug("%s uses %s!\n", a->name, b->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 return 1;
653 }
654 }
Jim Cromie5e124162011-12-06 12:11:31 -0700655 pr_debug("%s does not use %s!\n", a->name, b->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 return 0;
657}
658
Linus Torvalds2c02dfe2010-05-31 12:19:37 -0700659/*
660 * Module a uses b
661 * - we add 'a' as a "source", 'b' as a "target" of module use
662 * - the module_use is added to the list of 'b' sources (so
663 * 'b' can walk the list to see who sourced them), and of 'a'
664 * targets (so 'a' can see what modules it targets).
665 */
666static int add_module_usage(struct module *a, struct module *b)
667{
Linus Torvalds2c02dfe2010-05-31 12:19:37 -0700668 struct module_use *use;
669
Jim Cromie5e124162011-12-06 12:11:31 -0700670 pr_debug("Allocating new usage for %s.\n", a->name);
Linus Torvalds2c02dfe2010-05-31 12:19:37 -0700671 use = kmalloc(sizeof(*use), GFP_ATOMIC);
672 if (!use) {
673 printk(KERN_WARNING "%s: out of memory loading\n", a->name);
674 return -ENOMEM;
675 }
676
677 use->source = a;
678 use->target = b;
679 list_add(&use->source_list, &b->source_list);
680 list_add(&use->target_list, &a->target_list);
Linus Torvalds2c02dfe2010-05-31 12:19:37 -0700681 return 0;
682}
683
Rusty Russell75676502010-06-05 11:17:36 -0600684/* Module a uses b: caller needs module_mutex() */
Rusty Russell9bea7f22010-06-05 11:17:37 -0600685int ref_module(struct module *a, struct module *b)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686{
Rusty Russellc8e21ce2010-06-05 11:17:35 -0600687 int err;
Kay Sievers270a6c42007-01-18 13:26:15 +0100688
Rusty Russell9bea7f22010-06-05 11:17:37 -0600689 if (b == NULL || already_uses(a, b))
Linus Torvalds218ce732010-05-25 16:48:30 -0700690 return 0;
Linus Torvalds218ce732010-05-25 16:48:30 -0700691
Rusty Russell9bea7f22010-06-05 11:17:37 -0600692 /* If module isn't available, we fail. */
693 err = strong_try_module_get(b);
Rusty Russellc9a3ba52008-01-29 17:13:18 -0500694 if (err)
Rusty Russell9bea7f22010-06-05 11:17:37 -0600695 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696
Linus Torvalds2c02dfe2010-05-31 12:19:37 -0700697 err = add_module_usage(a, b);
698 if (err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 module_put(b);
Rusty Russell9bea7f22010-06-05 11:17:37 -0600700 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 }
Rusty Russell9bea7f22010-06-05 11:17:37 -0600702 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703}
Rusty Russell9bea7f22010-06-05 11:17:37 -0600704EXPORT_SYMBOL_GPL(ref_module);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705
706/* Clear the unload stuff of the module. */
707static void module_unload_free(struct module *mod)
708{
Linus Torvalds2c02dfe2010-05-31 12:19:37 -0700709 struct module_use *use, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710
Rusty Russell75676502010-06-05 11:17:36 -0600711 mutex_lock(&module_mutex);
Linus Torvalds2c02dfe2010-05-31 12:19:37 -0700712 list_for_each_entry_safe(use, tmp, &mod->target_list, target_list) {
713 struct module *i = use->target;
Jim Cromie5e124162011-12-06 12:11:31 -0700714 pr_debug("%s unusing %s\n", mod->name, i->name);
Linus Torvalds2c02dfe2010-05-31 12:19:37 -0700715 module_put(i);
716 list_del(&use->source_list);
717 list_del(&use->target_list);
718 kfree(use);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 }
Rusty Russell75676502010-06-05 11:17:36 -0600720 mutex_unlock(&module_mutex);
Rusty Russell9f85a4b2010-08-05 12:59:04 -0600721
722 free_percpu(mod->refptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723}
724
725#ifdef CONFIG_MODULE_FORCE_UNLOAD
Akinobu Mitafb169792006-01-08 01:04:29 -0800726static inline int try_force_unload(unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727{
728 int ret = (flags & O_TRUNC);
729 if (ret)
Akinobu Mitafb169792006-01-08 01:04:29 -0800730 add_taint(TAINT_FORCED_RMMOD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 return ret;
732}
733#else
Akinobu Mitafb169792006-01-08 01:04:29 -0800734static inline int try_force_unload(unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735{
736 return 0;
737}
738#endif /* CONFIG_MODULE_FORCE_UNLOAD */
739
740struct stopref
741{
742 struct module *mod;
743 int flags;
744 int *forced;
745};
746
747/* Whole machine is stopped with interrupts off when this runs. */
748static int __try_stop_module(void *_sref)
749{
750 struct stopref *sref = _sref;
751
Rusty Russellda39ba52008-07-22 19:24:25 -0500752 /* If it's not unused, quit unless we're forcing. */
753 if (module_refcount(sref->mod) != 0) {
Akinobu Mitafb169792006-01-08 01:04:29 -0800754 if (!(*sref->forced = try_force_unload(sref->flags)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 return -EWOULDBLOCK;
756 }
757
758 /* Mark it as dying. */
759 sref->mod->state = MODULE_STATE_GOING;
760 return 0;
761}
762
763static int try_stop_module(struct module *mod, int flags, int *forced)
764{
Rusty Russellda39ba52008-07-22 19:24:25 -0500765 if (flags & O_NONBLOCK) {
766 struct stopref sref = { mod, flags, forced };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767
Rusty Russell9b1a4d32008-07-28 12:16:30 -0500768 return stop_machine(__try_stop_module, &sref, NULL);
Rusty Russellda39ba52008-07-22 19:24:25 -0500769 } else {
770 /* We don't need to stop the machine for this. */
771 mod->state = MODULE_STATE_GOING;
772 synchronize_sched();
773 return 0;
774 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775}
776
Eric Dumazetbd77c042012-01-13 09:32:14 +1030777unsigned long module_refcount(struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778{
Eric Dumazetbd77c042012-01-13 09:32:14 +1030779 unsigned long incs = 0, decs = 0;
Eric Dumazet720eba32009-02-03 13:31:36 +1030780 int cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781
Eric Dumazet720eba32009-02-03 13:31:36 +1030782 for_each_possible_cpu(cpu)
Nick Piggin5fbfb182010-04-01 19:09:40 +1100783 decs += per_cpu_ptr(mod->refptr, cpu)->decs;
784 /*
785 * ensure the incs are added up after the decs.
786 * module_put ensures incs are visible before decs with smp_wmb.
787 *
788 * This 2-count scheme avoids the situation where the refcount
789 * for CPU0 is read, then CPU0 increments the module refcount,
790 * then CPU1 drops that refcount, then the refcount for CPU1 is
791 * read. We would record a decrement but not its corresponding
792 * increment so we would see a low count (disaster).
793 *
794 * Rare situation? But module_refcount can be preempted, and we
795 * might be tallying up 4096+ CPUs. So it is not impossible.
796 */
797 smp_rmb();
798 for_each_possible_cpu(cpu)
799 incs += per_cpu_ptr(mod->refptr, cpu)->incs;
800 return incs - decs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801}
802EXPORT_SYMBOL(module_refcount);
803
804/* This exists whether we can unload or not */
805static void free_module(struct module *mod);
806
807static void wait_for_zero_refcount(struct module *mod)
808{
Matthew Wilcoxa6550202008-02-26 10:47:18 -0500809 /* Since we might sleep for some time, release the mutex first */
Ashutosh Naik6389a382006-03-23 03:00:46 -0800810 mutex_unlock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 for (;;) {
Jim Cromie5e124162011-12-06 12:11:31 -0700812 pr_debug("Looking at refcount...\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 set_current_state(TASK_UNINTERRUPTIBLE);
814 if (module_refcount(mod) == 0)
815 break;
816 schedule();
817 }
818 current->state = TASK_RUNNING;
Ashutosh Naik6389a382006-03-23 03:00:46 -0800819 mutex_lock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820}
821
Heiko Carstens17da2bd2009-01-14 14:14:10 +0100822SYSCALL_DEFINE2(delete_module, const char __user *, name_user,
823 unsigned int, flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824{
825 struct module *mod;
Greg Kroah-Hartmandfff0a02007-02-23 14:54:57 -0800826 char name[MODULE_NAME_LEN];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 int ret, forced = 0;
828
Kees Cook3d433212009-04-02 15:49:29 -0700829 if (!capable(CAP_SYS_MODULE) || modules_disabled)
Greg Kroah-Hartmandfff0a02007-02-23 14:54:57 -0800830 return -EPERM;
831
832 if (strncpy_from_user(name, name_user, MODULE_NAME_LEN-1) < 0)
833 return -EFAULT;
834 name[MODULE_NAME_LEN-1] = '\0';
835
Tejun Heo3fc1f1e2010-05-06 18:49:20 +0200836 if (mutex_lock_interruptible(&module_mutex) != 0)
837 return -EINTR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838
839 mod = find_module(name);
840 if (!mod) {
841 ret = -ENOENT;
842 goto out;
843 }
844
Linus Torvalds2c02dfe2010-05-31 12:19:37 -0700845 if (!list_empty(&mod->source_list)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 /* Other modules depend on us: get rid of them first. */
847 ret = -EWOULDBLOCK;
848 goto out;
849 }
850
851 /* Doing init or already dying? */
852 if (mod->state != MODULE_STATE_LIVE) {
853 /* FIXME: if (force), slam module count and wake up
854 waiter --RR */
Jim Cromie5e124162011-12-06 12:11:31 -0700855 pr_debug("%s already dying\n", mod->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 ret = -EBUSY;
857 goto out;
858 }
859
860 /* If it has an init func, it must have an exit func to unload */
Rusty Russellaf49d922007-10-16 23:26:27 -0700861 if (mod->init && !mod->exit) {
Akinobu Mitafb169792006-01-08 01:04:29 -0800862 forced = try_force_unload(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 if (!forced) {
864 /* This module can't be removed */
865 ret = -EBUSY;
866 goto out;
867 }
868 }
869
870 /* Set this up before setting mod->state */
871 mod->waiter = current;
872
873 /* Stop the machine so refcounts can't move and disable module. */
874 ret = try_stop_module(mod, flags, &forced);
875 if (ret != 0)
876 goto out;
877
878 /* Never wait if forced. */
879 if (!forced && module_refcount(mod) != 0)
880 wait_for_zero_refcount(mod);
881
Peter Oberparleiterdf4b5652008-04-21 14:34:31 +0200882 mutex_unlock(&module_mutex);
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300883 /* Final destruction now no one is using it. */
Peter Oberparleiterdf4b5652008-04-21 14:34:31 +0200884 if (mod->exit != NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 mod->exit();
Peter Oberparleiterdf4b5652008-04-21 14:34:31 +0200886 blocking_notifier_call_chain(&module_notify_list,
887 MODULE_STATE_GOING, mod);
Arjan van de Ven22a9d642009-01-07 08:45:46 -0800888 async_synchronize_full();
Rusty Russell75676502010-06-05 11:17:36 -0600889
Arjan van de Vene14af7e2008-01-25 21:08:33 +0100890 /* Store the name of the last unloaded module for diagnostic purposes */
Rusty Russellefa53452008-01-29 17:13:20 -0500891 strlcpy(last_unloaded_module, mod->name, sizeof(last_unloaded_module));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892
Rusty Russell75676502010-06-05 11:17:36 -0600893 free_module(mod);
894 return 0;
895out:
Ashutosh Naik6389a382006-03-23 03:00:46 -0800896 mutex_unlock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 return ret;
898}
899
Jianjun Kongd1e99d72008-12-08 14:26:29 +0800900static inline void print_unload_info(struct seq_file *m, struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901{
902 struct module_use *use;
903 int printed_something = 0;
904
Eric Dumazetbd77c042012-01-13 09:32:14 +1030905 seq_printf(m, " %lu ", module_refcount(mod));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906
907 /* Always include a trailing , so userspace can differentiate
908 between this and the old multi-field proc format. */
Linus Torvalds2c02dfe2010-05-31 12:19:37 -0700909 list_for_each_entry(use, &mod->source_list, source_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 printed_something = 1;
Linus Torvalds2c02dfe2010-05-31 12:19:37 -0700911 seq_printf(m, "%s,", use->source->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 }
913
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 if (mod->init != NULL && mod->exit == NULL) {
915 printed_something = 1;
916 seq_printf(m, "[permanent],");
917 }
918
919 if (!printed_something)
920 seq_printf(m, "-");
921}
922
923void __symbol_put(const char *symbol)
924{
925 struct module *owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926
Rusty Russell24da1cb2007-07-15 23:41:46 -0700927 preempt_disable();
Tim Abbott414fd312008-12-05 19:03:56 -0500928 if (!find_symbol(symbol, &owner, NULL, true, false))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 BUG();
930 module_put(owner);
Rusty Russell24da1cb2007-07-15 23:41:46 -0700931 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932}
933EXPORT_SYMBOL(__symbol_put);
934
Rusty Russell7d1d16e2009-08-26 22:02:54 +0930935/* Note this assumes addr is a function, which it currently always is. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936void symbol_put_addr(void *addr)
937{
Trent Piepho5e376612006-05-15 09:44:06 -0700938 struct module *modaddr;
Rusty Russell7d1d16e2009-08-26 22:02:54 +0930939 unsigned long a = (unsigned long)dereference_function_descriptor(addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940
Rusty Russell7d1d16e2009-08-26 22:02:54 +0930941 if (core_kernel_text(a))
Trent Piepho5e376612006-05-15 09:44:06 -0700942 return;
943
Rusty Russella6e6abd2009-03-31 13:05:31 -0600944 /* module_text_address is safe here: we're supposed to have reference
945 * to module from symbol_get, so it can't go away. */
Rusty Russell7d1d16e2009-08-26 22:02:54 +0930946 modaddr = __module_text_address(a);
Rusty Russella6e6abd2009-03-31 13:05:31 -0600947 BUG_ON(!modaddr);
Trent Piepho5e376612006-05-15 09:44:06 -0700948 module_put(modaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949}
950EXPORT_SYMBOL_GPL(symbol_put_addr);
951
952static ssize_t show_refcnt(struct module_attribute *mattr,
Kay Sievers4befb022011-07-24 22:06:04 +0930953 struct module_kobject *mk, char *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954{
Eric Dumazetbd77c042012-01-13 09:32:14 +1030955 return sprintf(buffer, "%lu\n", module_refcount(mk->mod));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956}
957
Kay Sieverscca3e702012-01-13 09:32:15 +1030958static struct module_attribute modinfo_refcnt =
959 __ATTR(refcnt, 0444, show_refcnt, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960
Steven Rostedtd53799b2012-03-26 12:50:52 +1030961void __module_get(struct module *module)
962{
963 if (module) {
964 preempt_disable();
965 __this_cpu_inc(module->refptr->incs);
966 trace_module_get(module, _RET_IP_);
967 preempt_enable();
968 }
969}
970EXPORT_SYMBOL(__module_get);
971
972bool try_module_get(struct module *module)
973{
974 bool ret = true;
975
976 if (module) {
977 preempt_disable();
978
979 if (likely(module_is_live(module))) {
980 __this_cpu_inc(module->refptr->incs);
981 trace_module_get(module, _RET_IP_);
982 } else
983 ret = false;
984
985 preempt_enable();
986 }
987 return ret;
988}
989EXPORT_SYMBOL(try_module_get);
990
Al Virof6a57032006-10-18 01:47:25 -0400991void module_put(struct module *module)
992{
993 if (module) {
Christoph Lametere1783a22010-01-05 15:34:50 +0900994 preempt_disable();
Nick Piggin5fbfb182010-04-01 19:09:40 +1100995 smp_wmb(); /* see comment in module_refcount */
996 __this_cpu_inc(module->refptr->decs);
Christoph Lametere1783a22010-01-05 15:34:50 +0900997
Li Zefanae832d12010-03-24 10:57:43 +0800998 trace_module_put(module, _RET_IP_);
Al Virof6a57032006-10-18 01:47:25 -0400999 /* Maybe they're waiting for us to drop reference? */
1000 if (unlikely(!module_is_live(module)))
1001 wake_up_process(module->waiter);
Christoph Lametere1783a22010-01-05 15:34:50 +09001002 preempt_enable();
Al Virof6a57032006-10-18 01:47:25 -04001003 }
1004}
1005EXPORT_SYMBOL(module_put);
1006
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007#else /* !CONFIG_MODULE_UNLOAD */
Jianjun Kongd1e99d72008-12-08 14:26:29 +08001008static inline void print_unload_info(struct seq_file *m, struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009{
1010 /* We don't know the usage count, or what modules are using. */
1011 seq_printf(m, " - -");
1012}
1013
1014static inline void module_unload_free(struct module *mod)
1015{
1016}
1017
Rusty Russell9bea7f22010-06-05 11:17:37 -06001018int ref_module(struct module *a, struct module *b)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019{
Rusty Russell9bea7f22010-06-05 11:17:37 -06001020 return strong_try_module_get(b);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021}
Rusty Russell9bea7f22010-06-05 11:17:37 -06001022EXPORT_SYMBOL_GPL(ref_module);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023
Rusty Russell9f85a4b2010-08-05 12:59:04 -06001024static inline int module_unload_init(struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025{
Rusty Russell9f85a4b2010-08-05 12:59:04 -06001026 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027}
1028#endif /* CONFIG_MODULE_UNLOAD */
1029
Kevin Winchester53999bf2012-01-15 19:32:55 -04001030static size_t module_flags_taint(struct module *mod, char *buf)
1031{
1032 size_t l = 0;
1033
1034 if (mod->taints & (1 << TAINT_PROPRIETARY_MODULE))
1035 buf[l++] = 'P';
1036 if (mod->taints & (1 << TAINT_OOT_MODULE))
1037 buf[l++] = 'O';
1038 if (mod->taints & (1 << TAINT_FORCED_MODULE))
1039 buf[l++] = 'F';
1040 if (mod->taints & (1 << TAINT_CRAP))
1041 buf[l++] = 'C';
1042 /*
1043 * TAINT_FORCED_RMMOD: could be added.
1044 * TAINT_UNSAFE_SMP, TAINT_MACHINE_CHECK, TAINT_BAD_PAGE don't
1045 * apply to modules.
1046 */
1047 return l;
1048}
1049
Kay Sievers1f717402006-11-24 12:15:25 +01001050static ssize_t show_initstate(struct module_attribute *mattr,
Kay Sievers4befb022011-07-24 22:06:04 +09301051 struct module_kobject *mk, char *buffer)
Kay Sievers1f717402006-11-24 12:15:25 +01001052{
1053 const char *state = "unknown";
1054
Kay Sievers4befb022011-07-24 22:06:04 +09301055 switch (mk->mod->state) {
Kay Sievers1f717402006-11-24 12:15:25 +01001056 case MODULE_STATE_LIVE:
1057 state = "live";
1058 break;
1059 case MODULE_STATE_COMING:
1060 state = "coming";
1061 break;
1062 case MODULE_STATE_GOING:
1063 state = "going";
1064 break;
Rusty Russell0d21b0e2013-01-12 11:38:44 +10301065 default:
1066 BUG();
Kay Sievers1f717402006-11-24 12:15:25 +01001067 }
1068 return sprintf(buffer, "%s\n", state);
1069}
1070
Kay Sieverscca3e702012-01-13 09:32:15 +10301071static struct module_attribute modinfo_initstate =
1072 __ATTR(initstate, 0444, show_initstate, NULL);
Kay Sievers1f717402006-11-24 12:15:25 +01001073
Kay Sievers88bfa322011-07-24 22:06:04 +09301074static ssize_t store_uevent(struct module_attribute *mattr,
1075 struct module_kobject *mk,
1076 const char *buffer, size_t count)
1077{
1078 enum kobject_action action;
1079
1080 if (kobject_action_type(buffer, count, &action) == 0)
1081 kobject_uevent(&mk->kobj, action);
1082 return count;
1083}
1084
Kay Sieverscca3e702012-01-13 09:32:15 +10301085struct module_attribute module_uevent =
1086 __ATTR(uevent, 0200, NULL, store_uevent);
1087
1088static ssize_t show_coresize(struct module_attribute *mattr,
1089 struct module_kobject *mk, char *buffer)
1090{
1091 return sprintf(buffer, "%u\n", mk->mod->core_size);
1092}
1093
1094static struct module_attribute modinfo_coresize =
1095 __ATTR(coresize, 0444, show_coresize, NULL);
1096
1097static ssize_t show_initsize(struct module_attribute *mattr,
1098 struct module_kobject *mk, char *buffer)
1099{
1100 return sprintf(buffer, "%u\n", mk->mod->init_size);
1101}
1102
1103static struct module_attribute modinfo_initsize =
1104 __ATTR(initsize, 0444, show_initsize, NULL);
1105
1106static ssize_t show_taint(struct module_attribute *mattr,
1107 struct module_kobject *mk, char *buffer)
1108{
1109 size_t l;
1110
1111 l = module_flags_taint(mk->mod, buffer);
1112 buffer[l++] = '\n';
1113 return l;
1114}
1115
1116static struct module_attribute modinfo_taint =
1117 __ATTR(taint, 0444, show_taint, NULL);
Kay Sievers88bfa322011-07-24 22:06:04 +09301118
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001119static struct module_attribute *modinfo_attrs[] = {
Kay Sieverscca3e702012-01-13 09:32:15 +10301120 &module_uevent,
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001121 &modinfo_version,
1122 &modinfo_srcversion,
Kay Sieverscca3e702012-01-13 09:32:15 +10301123 &modinfo_initstate,
1124 &modinfo_coresize,
1125 &modinfo_initsize,
1126 &modinfo_taint,
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001127#ifdef CONFIG_MODULE_UNLOAD
Kay Sieverscca3e702012-01-13 09:32:15 +10301128 &modinfo_refcnt,
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001129#endif
1130 NULL,
1131};
1132
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133static const char vermagic[] = VERMAGIC_STRING;
1134
Rusty Russellc6e665c2009-03-31 13:05:33 -06001135static int try_to_force_load(struct module *mod, const char *reason)
Linus Torvalds826e4502008-05-04 17:04:16 -07001136{
1137#ifdef CONFIG_MODULE_FORCE_LOAD
Andi Kleen25ddbb12008-10-15 22:01:41 -07001138 if (!test_taint(TAINT_FORCED_MODULE))
Rusty Russellc6e665c2009-03-31 13:05:33 -06001139 printk(KERN_WARNING "%s: %s: kernel tainted.\n",
1140 mod->name, reason);
Linus Torvalds826e4502008-05-04 17:04:16 -07001141 add_taint_module(mod, TAINT_FORCED_MODULE);
1142 return 0;
1143#else
1144 return -ENOEXEC;
1145#endif
1146}
1147
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148#ifdef CONFIG_MODVERSIONS
Rusty Russelld4703ae2009-12-15 16:28:32 -06001149/* If the arch applies (non-zero) relocations to kernel kcrctab, unapply it. */
1150static unsigned long maybe_relocated(unsigned long crc,
1151 const struct module *crc_owner)
1152{
1153#ifdef ARCH_RELOCATES_KCRCTAB
1154 if (crc_owner == NULL)
1155 return crc - (unsigned long)reloc_start;
1156#endif
1157 return crc;
1158}
1159
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160static int check_version(Elf_Shdr *sechdrs,
1161 unsigned int versindex,
1162 const char *symname,
1163 struct module *mod,
Rusty Russelld4703ae2009-12-15 16:28:32 -06001164 const unsigned long *crc,
1165 const struct module *crc_owner)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166{
1167 unsigned int i, num_versions;
1168 struct modversion_info *versions;
1169
1170 /* Exporting module didn't supply crcs? OK, we're already tainted. */
1171 if (!crc)
1172 return 1;
1173
Rusty Russella5dd6972008-05-09 16:24:21 +10001174 /* No versions at all? modprobe --force does this. */
1175 if (versindex == 0)
1176 return try_to_force_load(mod, symname) == 0;
1177
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178 versions = (void *) sechdrs[versindex].sh_addr;
1179 num_versions = sechdrs[versindex].sh_size
1180 / sizeof(struct modversion_info);
1181
1182 for (i = 0; i < num_versions; i++) {
1183 if (strcmp(versions[i].name, symname) != 0)
1184 continue;
1185
Rusty Russelld4703ae2009-12-15 16:28:32 -06001186 if (versions[i].crc == maybe_relocated(*crc, crc_owner))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 return 1;
Jim Cromie5e124162011-12-06 12:11:31 -07001188 pr_debug("Found checksum %lX vs module %lX\n",
Rusty Russelld4703ae2009-12-15 16:28:32 -06001189 maybe_relocated(*crc, crc_owner), versions[i].crc);
Linus Torvalds826e4502008-05-04 17:04:16 -07001190 goto bad_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191 }
Linus Torvalds826e4502008-05-04 17:04:16 -07001192
Rusty Russella5dd6972008-05-09 16:24:21 +10001193 printk(KERN_WARNING "%s: no symbol version for %s\n",
1194 mod->name, symname);
1195 return 0;
Linus Torvalds826e4502008-05-04 17:04:16 -07001196
1197bad_version:
1198 printk("%s: disagrees about version of symbol %s\n",
1199 mod->name, symname);
1200 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201}
1202
1203static inline int check_modstruct_version(Elf_Shdr *sechdrs,
1204 unsigned int versindex,
1205 struct module *mod)
1206{
1207 const unsigned long *crc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208
Rusty Russell75676502010-06-05 11:17:36 -06001209 /* Since this should be found in kernel (which can't be removed),
1210 * no locking is necessary. */
Mike Frysinger6560dc12009-07-23 23:42:08 +09301211 if (!find_symbol(MODULE_SYMBOL_PREFIX "module_layout", NULL,
1212 &crc, true, false))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213 BUG();
Rusty Russelld4703ae2009-12-15 16:28:32 -06001214 return check_version(sechdrs, versindex, "module_layout", mod, crc,
1215 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216}
1217
Rusty Russell91e37a72008-05-09 16:25:28 +10001218/* First part is kernel version, which we ignore if module has crcs. */
1219static inline int same_magic(const char *amagic, const char *bmagic,
1220 bool has_crcs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221{
Rusty Russell91e37a72008-05-09 16:25:28 +10001222 if (has_crcs) {
1223 amagic += strcspn(amagic, " ");
1224 bmagic += strcspn(bmagic, " ");
1225 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226 return strcmp(amagic, bmagic) == 0;
1227}
1228#else
1229static inline int check_version(Elf_Shdr *sechdrs,
1230 unsigned int versindex,
1231 const char *symname,
1232 struct module *mod,
Rusty Russelld4703ae2009-12-15 16:28:32 -06001233 const unsigned long *crc,
1234 const struct module *crc_owner)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235{
1236 return 1;
1237}
1238
1239static inline int check_modstruct_version(Elf_Shdr *sechdrs,
1240 unsigned int versindex,
1241 struct module *mod)
1242{
1243 return 1;
1244}
1245
Rusty Russell91e37a72008-05-09 16:25:28 +10001246static inline int same_magic(const char *amagic, const char *bmagic,
1247 bool has_crcs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248{
1249 return strcmp(amagic, bmagic) == 0;
1250}
1251#endif /* CONFIG_MODVERSIONS */
1252
Rusty Russell75676502010-06-05 11:17:36 -06001253/* Resolve a symbol for this module. I.e. if we find one, record usage. */
Rusty Russell49668682010-08-05 12:59:10 -06001254static const struct kernel_symbol *resolve_symbol(struct module *mod,
1255 const struct load_info *info,
Tim Abbott414fd312008-12-05 19:03:56 -05001256 const char *name,
Rusty Russell9bea7f22010-06-05 11:17:37 -06001257 char ownername[])
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258{
1259 struct module *owner;
Tim Abbott414fd312008-12-05 19:03:56 -05001260 const struct kernel_symbol *sym;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261 const unsigned long *crc;
Rusty Russell9bea7f22010-06-05 11:17:37 -06001262 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263
Rusty Russell75676502010-06-05 11:17:36 -06001264 mutex_lock(&module_mutex);
Tim Abbott414fd312008-12-05 19:03:56 -05001265 sym = find_symbol(name, &owner, &crc,
Andi Kleen25ddbb12008-10-15 22:01:41 -07001266 !(mod->taints & (1 << TAINT_PROPRIETARY_MODULE)), true);
Rusty Russell9bea7f22010-06-05 11:17:37 -06001267 if (!sym)
1268 goto unlock;
1269
Rusty Russell49668682010-08-05 12:59:10 -06001270 if (!check_version(info->sechdrs, info->index.vers, name, mod, crc,
1271 owner)) {
Rusty Russell9bea7f22010-06-05 11:17:37 -06001272 sym = ERR_PTR(-EINVAL);
1273 goto getname;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 }
Rusty Russell9bea7f22010-06-05 11:17:37 -06001275
1276 err = ref_module(mod, owner);
1277 if (err) {
1278 sym = ERR_PTR(err);
1279 goto getname;
1280 }
1281
1282getname:
1283 /* We must make copy under the lock if we failed to get ref. */
1284 strncpy(ownername, module_name(owner), MODULE_NAME_LEN);
1285unlock:
Rusty Russell75676502010-06-05 11:17:36 -06001286 mutex_unlock(&module_mutex);
Linus Torvalds218ce732010-05-25 16:48:30 -07001287 return sym;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288}
1289
Rusty Russell49668682010-08-05 12:59:10 -06001290static const struct kernel_symbol *
1291resolve_symbol_wait(struct module *mod,
1292 const struct load_info *info,
1293 const char *name)
Rusty Russell9bea7f22010-06-05 11:17:37 -06001294{
1295 const struct kernel_symbol *ksym;
Rusty Russell49668682010-08-05 12:59:10 -06001296 char owner[MODULE_NAME_LEN];
Rusty Russell9bea7f22010-06-05 11:17:37 -06001297
1298 if (wait_event_interruptible_timeout(module_wq,
Rusty Russell49668682010-08-05 12:59:10 -06001299 !IS_ERR(ksym = resolve_symbol(mod, info, name, owner))
1300 || PTR_ERR(ksym) != -EBUSY,
Rusty Russell9bea7f22010-06-05 11:17:37 -06001301 30 * HZ) <= 0) {
1302 printk(KERN_WARNING "%s: gave up waiting for init of module %s.\n",
Rusty Russell49668682010-08-05 12:59:10 -06001303 mod->name, owner);
Rusty Russell9bea7f22010-06-05 11:17:37 -06001304 }
1305 return ksym;
1306}
1307
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308/*
1309 * /sys/module/foo/sections stuff
1310 * J. Corbet <corbet@lwn.net>
1311 */
Rusty Russell8f6d0372010-08-05 12:59:09 -06001312#ifdef CONFIG_SYSFS
Ben Hutchings10b465a2009-12-19 14:43:01 +00001313
Rusty Russell8f6d0372010-08-05 12:59:09 -06001314#ifdef CONFIG_KALLSYMS
Ben Hutchings10b465a2009-12-19 14:43:01 +00001315static inline bool sect_empty(const Elf_Shdr *sect)
1316{
1317 return !(sect->sh_flags & SHF_ALLOC) || sect->sh_size == 0;
1318}
1319
Rusty Russella58730c2008-03-13 09:03:44 +00001320struct module_sect_attr
1321{
1322 struct module_attribute mattr;
1323 char *name;
1324 unsigned long address;
1325};
1326
1327struct module_sect_attrs
1328{
1329 struct attribute_group grp;
1330 unsigned int nsections;
1331 struct module_sect_attr attrs[0];
1332};
1333
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334static ssize_t module_sect_show(struct module_attribute *mattr,
Kay Sievers4befb022011-07-24 22:06:04 +09301335 struct module_kobject *mk, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336{
1337 struct module_sect_attr *sattr =
1338 container_of(mattr, struct module_sect_attr, mattr);
Kees Cook9f36e2c2011-03-22 16:34:22 -07001339 return sprintf(buf, "0x%pK\n", (void *)sattr->address);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340}
1341
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001342static void free_sect_attrs(struct module_sect_attrs *sect_attrs)
1343{
Rusty Russella58730c2008-03-13 09:03:44 +00001344 unsigned int section;
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001345
1346 for (section = 0; section < sect_attrs->nsections; section++)
1347 kfree(sect_attrs->attrs[section].name);
1348 kfree(sect_attrs);
1349}
1350
Rusty Russell8f6d0372010-08-05 12:59:09 -06001351static void add_sect_attrs(struct module *mod, const struct load_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352{
1353 unsigned int nloaded = 0, i, size[2];
1354 struct module_sect_attrs *sect_attrs;
1355 struct module_sect_attr *sattr;
1356 struct attribute **gattr;
Daniel Walker22a8bde2007-10-18 03:06:07 -07001357
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358 /* Count loaded sections and allocate structures */
Rusty Russell8f6d0372010-08-05 12:59:09 -06001359 for (i = 0; i < info->hdr->e_shnum; i++)
1360 if (!sect_empty(&info->sechdrs[i]))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361 nloaded++;
1362 size[0] = ALIGN(sizeof(*sect_attrs)
1363 + nloaded * sizeof(sect_attrs->attrs[0]),
1364 sizeof(sect_attrs->grp.attrs[0]));
1365 size[1] = (nloaded + 1) * sizeof(sect_attrs->grp.attrs[0]);
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001366 sect_attrs = kzalloc(size[0] + size[1], GFP_KERNEL);
1367 if (sect_attrs == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368 return;
1369
1370 /* Setup section attributes. */
1371 sect_attrs->grp.name = "sections";
1372 sect_attrs->grp.attrs = (void *)sect_attrs + size[0];
1373
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001374 sect_attrs->nsections = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375 sattr = &sect_attrs->attrs[0];
1376 gattr = &sect_attrs->grp.attrs[0];
Rusty Russell8f6d0372010-08-05 12:59:09 -06001377 for (i = 0; i < info->hdr->e_shnum; i++) {
1378 Elf_Shdr *sec = &info->sechdrs[i];
1379 if (sect_empty(sec))
Helge Deller35dead42009-12-03 00:29:15 +01001380 continue;
Rusty Russell8f6d0372010-08-05 12:59:09 -06001381 sattr->address = sec->sh_addr;
1382 sattr->name = kstrdup(info->secstrings + sec->sh_name,
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001383 GFP_KERNEL);
1384 if (sattr->name == NULL)
1385 goto out;
1386 sect_attrs->nsections++;
Eric W. Biederman361795b2010-02-12 13:41:56 -08001387 sysfs_attr_init(&sattr->mattr.attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388 sattr->mattr.show = module_sect_show;
1389 sattr->mattr.store = NULL;
1390 sattr->mattr.attr.name = sattr->name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 sattr->mattr.attr.mode = S_IRUGO;
1392 *(gattr++) = &(sattr++)->mattr.attr;
1393 }
1394 *gattr = NULL;
1395
1396 if (sysfs_create_group(&mod->mkobj.kobj, &sect_attrs->grp))
1397 goto out;
1398
1399 mod->sect_attrs = sect_attrs;
1400 return;
1401 out:
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001402 free_sect_attrs(sect_attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403}
1404
1405static void remove_sect_attrs(struct module *mod)
1406{
1407 if (mod->sect_attrs) {
1408 sysfs_remove_group(&mod->mkobj.kobj,
1409 &mod->sect_attrs->grp);
1410 /* We are positive that no one is using any sect attrs
1411 * at this point. Deallocate immediately. */
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001412 free_sect_attrs(mod->sect_attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413 mod->sect_attrs = NULL;
1414 }
1415}
1416
Roland McGrath6d760132007-10-16 23:26:40 -07001417/*
1418 * /sys/module/foo/notes/.section.name gives contents of SHT_NOTE sections.
1419 */
1420
1421struct module_notes_attrs {
1422 struct kobject *dir;
1423 unsigned int notes;
1424 struct bin_attribute attrs[0];
1425};
1426
Chris Wright2c3c8be2010-05-12 18:28:57 -07001427static ssize_t module_notes_read(struct file *filp, struct kobject *kobj,
Roland McGrath6d760132007-10-16 23:26:40 -07001428 struct bin_attribute *bin_attr,
1429 char *buf, loff_t pos, size_t count)
1430{
1431 /*
1432 * The caller checked the pos and count against our size.
1433 */
1434 memcpy(buf, bin_attr->private + pos, count);
1435 return count;
1436}
1437
1438static void free_notes_attrs(struct module_notes_attrs *notes_attrs,
1439 unsigned int i)
1440{
1441 if (notes_attrs->dir) {
1442 while (i-- > 0)
1443 sysfs_remove_bin_file(notes_attrs->dir,
1444 &notes_attrs->attrs[i]);
Alexey Dobriyane9432092008-09-23 23:51:11 +04001445 kobject_put(notes_attrs->dir);
Roland McGrath6d760132007-10-16 23:26:40 -07001446 }
1447 kfree(notes_attrs);
1448}
1449
Rusty Russell8f6d0372010-08-05 12:59:09 -06001450static void add_notes_attrs(struct module *mod, const struct load_info *info)
Roland McGrath6d760132007-10-16 23:26:40 -07001451{
1452 unsigned int notes, loaded, i;
1453 struct module_notes_attrs *notes_attrs;
1454 struct bin_attribute *nattr;
1455
Ingo Molnarea6bff32009-08-28 10:44:56 +02001456 /* failed to create section attributes, so can't create notes */
1457 if (!mod->sect_attrs)
1458 return;
1459
Roland McGrath6d760132007-10-16 23:26:40 -07001460 /* Count notes sections and allocate structures. */
1461 notes = 0;
Rusty Russell8f6d0372010-08-05 12:59:09 -06001462 for (i = 0; i < info->hdr->e_shnum; i++)
1463 if (!sect_empty(&info->sechdrs[i]) &&
1464 (info->sechdrs[i].sh_type == SHT_NOTE))
Roland McGrath6d760132007-10-16 23:26:40 -07001465 ++notes;
1466
1467 if (notes == 0)
1468 return;
1469
1470 notes_attrs = kzalloc(sizeof(*notes_attrs)
1471 + notes * sizeof(notes_attrs->attrs[0]),
1472 GFP_KERNEL);
1473 if (notes_attrs == NULL)
1474 return;
1475
1476 notes_attrs->notes = notes;
1477 nattr = &notes_attrs->attrs[0];
Rusty Russell8f6d0372010-08-05 12:59:09 -06001478 for (loaded = i = 0; i < info->hdr->e_shnum; ++i) {
1479 if (sect_empty(&info->sechdrs[i]))
Roland McGrath6d760132007-10-16 23:26:40 -07001480 continue;
Rusty Russell8f6d0372010-08-05 12:59:09 -06001481 if (info->sechdrs[i].sh_type == SHT_NOTE) {
Eric W. Biederman361795b2010-02-12 13:41:56 -08001482 sysfs_bin_attr_init(nattr);
Roland McGrath6d760132007-10-16 23:26:40 -07001483 nattr->attr.name = mod->sect_attrs->attrs[loaded].name;
1484 nattr->attr.mode = S_IRUGO;
Rusty Russell8f6d0372010-08-05 12:59:09 -06001485 nattr->size = info->sechdrs[i].sh_size;
1486 nattr->private = (void *) info->sechdrs[i].sh_addr;
Roland McGrath6d760132007-10-16 23:26:40 -07001487 nattr->read = module_notes_read;
1488 ++nattr;
1489 }
1490 ++loaded;
1491 }
1492
Greg Kroah-Hartman4ff6abf2007-11-05 22:24:43 -08001493 notes_attrs->dir = kobject_create_and_add("notes", &mod->mkobj.kobj);
Roland McGrath6d760132007-10-16 23:26:40 -07001494 if (!notes_attrs->dir)
1495 goto out;
1496
1497 for (i = 0; i < notes; ++i)
1498 if (sysfs_create_bin_file(notes_attrs->dir,
1499 &notes_attrs->attrs[i]))
1500 goto out;
1501
1502 mod->notes_attrs = notes_attrs;
1503 return;
1504
1505 out:
1506 free_notes_attrs(notes_attrs, i);
1507}
1508
1509static void remove_notes_attrs(struct module *mod)
1510{
1511 if (mod->notes_attrs)
1512 free_notes_attrs(mod->notes_attrs, mod->notes_attrs->notes);
1513}
1514
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515#else
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001516
Rusty Russell8f6d0372010-08-05 12:59:09 -06001517static inline void add_sect_attrs(struct module *mod,
1518 const struct load_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519{
1520}
1521
1522static inline void remove_sect_attrs(struct module *mod)
1523{
1524}
Roland McGrath6d760132007-10-16 23:26:40 -07001525
Rusty Russell8f6d0372010-08-05 12:59:09 -06001526static inline void add_notes_attrs(struct module *mod,
1527 const struct load_info *info)
Roland McGrath6d760132007-10-16 23:26:40 -07001528{
1529}
1530
1531static inline void remove_notes_attrs(struct module *mod)
1532{
1533}
Rusty Russell8f6d0372010-08-05 12:59:09 -06001534#endif /* CONFIG_KALLSYMS */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535
Rusty Russell80a3d1b2010-06-05 11:17:36 -06001536static void add_usage_links(struct module *mod)
1537{
1538#ifdef CONFIG_MODULE_UNLOAD
1539 struct module_use *use;
1540 int nowarn;
1541
Rusty Russell75676502010-06-05 11:17:36 -06001542 mutex_lock(&module_mutex);
Rusty Russell80a3d1b2010-06-05 11:17:36 -06001543 list_for_each_entry(use, &mod->target_list, target_list) {
1544 nowarn = sysfs_create_link(use->target->holders_dir,
1545 &mod->mkobj.kobj, mod->name);
1546 }
Rusty Russell75676502010-06-05 11:17:36 -06001547 mutex_unlock(&module_mutex);
Rusty Russell80a3d1b2010-06-05 11:17:36 -06001548#endif
1549}
1550
1551static void del_usage_links(struct module *mod)
1552{
1553#ifdef CONFIG_MODULE_UNLOAD
1554 struct module_use *use;
1555
Rusty Russell75676502010-06-05 11:17:36 -06001556 mutex_lock(&module_mutex);
Rusty Russell80a3d1b2010-06-05 11:17:36 -06001557 list_for_each_entry(use, &mod->target_list, target_list)
1558 sysfs_remove_link(use->target->holders_dir, mod->name);
Rusty Russell75676502010-06-05 11:17:36 -06001559 mutex_unlock(&module_mutex);
Rusty Russell80a3d1b2010-06-05 11:17:36 -06001560#endif
1561}
1562
Rusty Russell6407ebb22010-06-05 11:17:36 -06001563static int module_add_modinfo_attrs(struct module *mod)
Matt Domschc988d2b2005-06-23 22:05:15 -07001564{
1565 struct module_attribute *attr;
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001566 struct module_attribute *temp_attr;
Matt Domschc988d2b2005-06-23 22:05:15 -07001567 int error = 0;
1568 int i;
1569
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001570 mod->modinfo_attrs = kzalloc((sizeof(struct module_attribute) *
1571 (ARRAY_SIZE(modinfo_attrs) + 1)),
1572 GFP_KERNEL);
1573 if (!mod->modinfo_attrs)
1574 return -ENOMEM;
1575
1576 temp_attr = mod->modinfo_attrs;
Matt Domschc988d2b2005-06-23 22:05:15 -07001577 for (i = 0; (attr = modinfo_attrs[i]) && !error; i++) {
1578 if (!attr->test ||
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001579 (attr->test && attr->test(mod))) {
1580 memcpy(temp_attr, attr, sizeof(*temp_attr));
Eric W. Biederman361795b2010-02-12 13:41:56 -08001581 sysfs_attr_init(&temp_attr->attr);
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001582 error = sysfs_create_file(&mod->mkobj.kobj,&temp_attr->attr);
1583 ++temp_attr;
1584 }
Matt Domschc988d2b2005-06-23 22:05:15 -07001585 }
1586 return error;
1587}
1588
Rusty Russell6407ebb22010-06-05 11:17:36 -06001589static void module_remove_modinfo_attrs(struct module *mod)
Matt Domschc988d2b2005-06-23 22:05:15 -07001590{
1591 struct module_attribute *attr;
1592 int i;
1593
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001594 for (i = 0; (attr = &mod->modinfo_attrs[i]); i++) {
1595 /* pick a field to test for end of list */
1596 if (!attr->attr.name)
1597 break;
Matt Domschc988d2b2005-06-23 22:05:15 -07001598 sysfs_remove_file(&mod->mkobj.kobj,&attr->attr);
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001599 if (attr->free)
1600 attr->free(mod);
Matt Domschc988d2b2005-06-23 22:05:15 -07001601 }
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001602 kfree(mod->modinfo_attrs);
Matt Domschc988d2b2005-06-23 22:05:15 -07001603}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604
Rusty Russell6407ebb22010-06-05 11:17:36 -06001605static int mod_sysfs_init(struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606{
1607 int err;
Greg Kroah-Hartman6494a932008-01-27 15:38:40 -08001608 struct kobject *kobj;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609
Greg Kroah-Hartman823bccf2007-04-13 13:15:19 -07001610 if (!module_sysfs_initialized) {
1611 printk(KERN_ERR "%s: module sysfs not initialized\n",
Ed Swierk1cc5f712006-09-25 16:25:36 -07001612 mod->name);
1613 err = -EINVAL;
1614 goto out;
1615 }
Greg Kroah-Hartman6494a932008-01-27 15:38:40 -08001616
1617 kobj = kset_find_obj(module_kset, mod->name);
1618 if (kobj) {
1619 printk(KERN_ERR "%s: module is already loaded\n", mod->name);
1620 kobject_put(kobj);
1621 err = -EINVAL;
1622 goto out;
1623 }
1624
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625 mod->mkobj.mod = mod;
Kay Sieverse17e0f52006-11-24 12:15:25 +01001626
Greg Kroah-Hartmanac3c8142007-12-17 23:05:35 -07001627 memset(&mod->mkobj.kobj, 0, sizeof(mod->mkobj.kobj));
1628 mod->mkobj.kobj.kset = module_kset;
1629 err = kobject_init_and_add(&mod->mkobj.kobj, &module_ktype, NULL,
1630 "%s", mod->name);
1631 if (err)
1632 kobject_put(&mod->mkobj.kobj);
Kay Sievers270a6c42007-01-18 13:26:15 +01001633
Kay Sievers97c146e2007-11-29 23:46:11 +01001634 /* delay uevent until full sysfs population */
Kay Sievers270a6c42007-01-18 13:26:15 +01001635out:
1636 return err;
1637}
1638
Rusty Russell6407ebb22010-06-05 11:17:36 -06001639static int mod_sysfs_setup(struct module *mod,
Rusty Russell8f6d0372010-08-05 12:59:09 -06001640 const struct load_info *info,
Kay Sievers270a6c42007-01-18 13:26:15 +01001641 struct kernel_param *kparam,
1642 unsigned int num_params)
1643{
1644 int err;
1645
Rusty Russell80a3d1b2010-06-05 11:17:36 -06001646 err = mod_sysfs_init(mod);
1647 if (err)
1648 goto out;
1649
Greg Kroah-Hartman4ff6abf2007-11-05 22:24:43 -08001650 mod->holders_dir = kobject_create_and_add("holders", &mod->mkobj.kobj);
Akinobu Mita240936e2007-04-26 00:12:09 -07001651 if (!mod->holders_dir) {
1652 err = -ENOMEM;
Kay Sievers270a6c42007-01-18 13:26:15 +01001653 goto out_unreg;
Akinobu Mita240936e2007-04-26 00:12:09 -07001654 }
Kay Sievers270a6c42007-01-18 13:26:15 +01001655
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656 err = module_param_sysfs_setup(mod, kparam, num_params);
1657 if (err)
Kay Sievers270a6c42007-01-18 13:26:15 +01001658 goto out_unreg_holders;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659
Matt Domschc988d2b2005-06-23 22:05:15 -07001660 err = module_add_modinfo_attrs(mod);
1661 if (err)
Kay Sieverse17e0f52006-11-24 12:15:25 +01001662 goto out_unreg_param;
Matt Domschc988d2b2005-06-23 22:05:15 -07001663
Rusty Russell80a3d1b2010-06-05 11:17:36 -06001664 add_usage_links(mod);
Rusty Russell8f6d0372010-08-05 12:59:09 -06001665 add_sect_attrs(mod, info);
1666 add_notes_attrs(mod, info);
Rusty Russell80a3d1b2010-06-05 11:17:36 -06001667
Kay Sieverse17e0f52006-11-24 12:15:25 +01001668 kobject_uevent(&mod->mkobj.kobj, KOBJ_ADD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669 return 0;
1670
Kay Sieverse17e0f52006-11-24 12:15:25 +01001671out_unreg_param:
1672 module_param_sysfs_remove(mod);
Kay Sievers270a6c42007-01-18 13:26:15 +01001673out_unreg_holders:
Greg Kroah-Hartman78a2d902007-12-20 08:13:05 -08001674 kobject_put(mod->holders_dir);
Kay Sievers270a6c42007-01-18 13:26:15 +01001675out_unreg:
Kay Sieverse17e0f52006-11-24 12:15:25 +01001676 kobject_put(&mod->mkobj.kobj);
Rusty Russell80a3d1b2010-06-05 11:17:36 -06001677out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678 return err;
1679}
Denis V. Lunev34e4e2f2008-05-20 13:59:48 +04001680
1681static void mod_sysfs_fini(struct module *mod)
1682{
Rusty Russell8f6d0372010-08-05 12:59:09 -06001683 remove_notes_attrs(mod);
1684 remove_sect_attrs(mod);
Denis V. Lunev34e4e2f2008-05-20 13:59:48 +04001685 kobject_put(&mod->mkobj.kobj);
1686}
1687
Rusty Russell8f6d0372010-08-05 12:59:09 -06001688#else /* !CONFIG_SYSFS */
Denis V. Lunev34e4e2f2008-05-20 13:59:48 +04001689
Rusty Russell8f6d0372010-08-05 12:59:09 -06001690static int mod_sysfs_setup(struct module *mod,
1691 const struct load_info *info,
Rusty Russell6407ebb22010-06-05 11:17:36 -06001692 struct kernel_param *kparam,
1693 unsigned int num_params)
1694{
1695 return 0;
1696}
1697
Denis V. Lunev34e4e2f2008-05-20 13:59:48 +04001698static void mod_sysfs_fini(struct module *mod)
1699{
1700}
1701
Rusty Russell36b03602010-08-05 12:59:09 -06001702static void module_remove_modinfo_attrs(struct module *mod)
1703{
1704}
1705
Rusty Russell80a3d1b2010-06-05 11:17:36 -06001706static void del_usage_links(struct module *mod)
1707{
1708}
1709
Denis V. Lunev34e4e2f2008-05-20 13:59:48 +04001710#endif /* CONFIG_SYSFS */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711
Rusty Russell36b03602010-08-05 12:59:09 -06001712static void mod_sysfs_teardown(struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713{
Rusty Russell80a3d1b2010-06-05 11:17:36 -06001714 del_usage_links(mod);
Matt Domschc988d2b2005-06-23 22:05:15 -07001715 module_remove_modinfo_attrs(mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716 module_param_sysfs_remove(mod);
Greg Kroah-Hartman78a2d902007-12-20 08:13:05 -08001717 kobject_put(mod->mkobj.drivers_dir);
1718 kobject_put(mod->holders_dir);
Denis V. Lunev34e4e2f2008-05-20 13:59:48 +04001719 mod_sysfs_fini(mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720}
1721
1722/*
1723 * unlink the module with the whole machine is stopped with interrupts off
1724 * - this defends against kallsyms not taking locks
1725 */
1726static int __unlink_module(void *_mod)
1727{
1728 struct module *mod = _mod;
1729 list_del(&mod->list);
Linus Torvalds53363772010-10-05 11:29:27 -07001730 module_bug_cleanup(mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731 return 0;
1732}
1733
matthieu castet84e1c6b2010-11-16 22:35:16 +01001734#ifdef CONFIG_DEBUG_SET_MODULE_RONX
1735/*
1736 * LKM RO/NX protection: protect module's text/ro-data
1737 * from modification and any data from execution.
1738 */
1739void set_page_attributes(void *start, void *end, int (*set)(unsigned long start, int num_pages))
1740{
1741 unsigned long begin_pfn = PFN_DOWN((unsigned long)start);
1742 unsigned long end_pfn = PFN_DOWN((unsigned long)end);
1743
1744 if (end_pfn > begin_pfn)
1745 set(begin_pfn << PAGE_SHIFT, end_pfn - begin_pfn);
1746}
1747
1748static void set_section_ro_nx(void *base,
1749 unsigned long text_size,
1750 unsigned long ro_size,
1751 unsigned long total_size)
1752{
1753 /* begin and end PFNs of the current subsection */
1754 unsigned long begin_pfn;
1755 unsigned long end_pfn;
1756
1757 /*
1758 * Set RO for module text and RO-data:
1759 * - Always protect first page.
1760 * - Do not protect last partial page.
1761 */
1762 if (ro_size > 0)
1763 set_page_attributes(base, base + ro_size, set_memory_ro);
1764
1765 /*
1766 * Set NX permissions for module data:
1767 * - Do not protect first partial page.
1768 * - Always protect last page.
1769 */
1770 if (total_size > text_size) {
1771 begin_pfn = PFN_UP((unsigned long)base + text_size);
1772 end_pfn = PFN_UP((unsigned long)base + total_size);
1773 if (end_pfn > begin_pfn)
1774 set_memory_nx(begin_pfn << PAGE_SHIFT, end_pfn - begin_pfn);
1775 }
1776}
1777
Jan Glauber01526ed2011-05-19 16:55:26 -06001778static void unset_module_core_ro_nx(struct module *mod)
matthieu castet84e1c6b2010-11-16 22:35:16 +01001779{
Jan Glauber01526ed2011-05-19 16:55:26 -06001780 set_page_attributes(mod->module_core + mod->core_text_size,
1781 mod->module_core + mod->core_size,
1782 set_memory_x);
1783 set_page_attributes(mod->module_core,
1784 mod->module_core + mod->core_ro_size,
1785 set_memory_rw);
1786}
1787
1788static void unset_module_init_ro_nx(struct module *mod)
1789{
1790 set_page_attributes(mod->module_init + mod->init_text_size,
1791 mod->module_init + mod->init_size,
1792 set_memory_x);
1793 set_page_attributes(mod->module_init,
1794 mod->module_init + mod->init_ro_size,
1795 set_memory_rw);
matthieu castet84e1c6b2010-11-16 22:35:16 +01001796}
1797
1798/* Iterate through all modules and set each module's text as RW */
Daniel J Blueman5d05c702011-03-08 22:01:47 +08001799void set_all_modules_text_rw(void)
matthieu castet84e1c6b2010-11-16 22:35:16 +01001800{
1801 struct module *mod;
1802
1803 mutex_lock(&module_mutex);
1804 list_for_each_entry_rcu(mod, &modules, list) {
Rusty Russell0d21b0e2013-01-12 11:38:44 +10301805 if (mod->state == MODULE_STATE_UNFORMED)
1806 continue;
matthieu castet84e1c6b2010-11-16 22:35:16 +01001807 if ((mod->module_core) && (mod->core_text_size)) {
1808 set_page_attributes(mod->module_core,
1809 mod->module_core + mod->core_text_size,
1810 set_memory_rw);
1811 }
1812 if ((mod->module_init) && (mod->init_text_size)) {
1813 set_page_attributes(mod->module_init,
1814 mod->module_init + mod->init_text_size,
1815 set_memory_rw);
1816 }
1817 }
1818 mutex_unlock(&module_mutex);
1819}
1820
1821/* Iterate through all modules and set each module's text as RO */
Daniel J Blueman5d05c702011-03-08 22:01:47 +08001822void set_all_modules_text_ro(void)
matthieu castet84e1c6b2010-11-16 22:35:16 +01001823{
1824 struct module *mod;
1825
1826 mutex_lock(&module_mutex);
1827 list_for_each_entry_rcu(mod, &modules, list) {
Rusty Russell0d21b0e2013-01-12 11:38:44 +10301828 if (mod->state == MODULE_STATE_UNFORMED)
1829 continue;
matthieu castet84e1c6b2010-11-16 22:35:16 +01001830 if ((mod->module_core) && (mod->core_text_size)) {
1831 set_page_attributes(mod->module_core,
1832 mod->module_core + mod->core_text_size,
1833 set_memory_ro);
1834 }
1835 if ((mod->module_init) && (mod->init_text_size)) {
1836 set_page_attributes(mod->module_init,
1837 mod->module_init + mod->init_text_size,
1838 set_memory_ro);
1839 }
1840 }
1841 mutex_unlock(&module_mutex);
1842}
1843#else
1844static 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 -06001845static void unset_module_core_ro_nx(struct module *mod) { }
1846static void unset_module_init_ro_nx(struct module *mod) { }
matthieu castet84e1c6b2010-11-16 22:35:16 +01001847#endif
1848
Jonas Bonn74e08fc2011-06-30 21:22:11 +02001849void __weak module_free(struct module *mod, void *module_region)
1850{
1851 vfree(module_region);
1852}
1853
1854void __weak module_arch_cleanup(struct module *mod)
1855{
1856}
1857
Rusty Russell75676502010-06-05 11:17:36 -06001858/* Free a module, remove from lists, etc. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859static void free_module(struct module *mod)
1860{
Li Zefan7ead8b82009-08-17 16:56:28 +08001861 trace_module_free(mod);
1862
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863 /* Delete from various lists */
Rusty Russell75676502010-06-05 11:17:36 -06001864 mutex_lock(&module_mutex);
Rusty Russell9b1a4d32008-07-28 12:16:30 -05001865 stop_machine(__unlink_module, mod, NULL);
Rusty Russell75676502010-06-05 11:17:36 -06001866 mutex_unlock(&module_mutex);
Rusty Russell36b03602010-08-05 12:59:09 -06001867 mod_sysfs_teardown(mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868
Jason Baronb82bab4b2010-07-27 13:18:01 -07001869 /* Remove dynamic debug info */
1870 ddebug_remove_module(mod->name);
1871
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872 /* Arch-specific cleanup. */
1873 module_arch_cleanup(mod);
1874
1875 /* Module unload stuff */
1876 module_unload_free(mod);
1877
Rusty Russelle180a6b2009-03-31 13:05:29 -06001878 /* Free any allocated parameters. */
1879 destroy_params(mod->kp, mod->num_kp);
1880
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881 /* This may be NULL, but that's OK */
Jan Glauber01526ed2011-05-19 16:55:26 -06001882 unset_module_init_ro_nx(mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883 module_free(mod, mod->module_init);
1884 kfree(mod->args);
Tejun Heo259354d2010-03-10 18:56:10 +09001885 percpu_modfree(mod);
Rusty Russell9f85a4b2010-08-05 12:59:04 -06001886
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001887 /* Free lock-classes: */
1888 lockdep_free_key_range(mod->module_core, mod->core_size);
1889
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890 /* Finally, free the core (containing the module structure) */
Jan Glauber01526ed2011-05-19 16:55:26 -06001891 unset_module_core_ro_nx(mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892 module_free(mod, mod->module_core);
Bernd Schmidteb8cdec2009-09-21 17:03:57 -07001893
1894#ifdef CONFIG_MPU
1895 update_protections(current->mm);
1896#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897}
1898
1899void *__symbol_get(const char *symbol)
1900{
1901 struct module *owner;
Tim Abbott414fd312008-12-05 19:03:56 -05001902 const struct kernel_symbol *sym;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903
Rusty Russell24da1cb2007-07-15 23:41:46 -07001904 preempt_disable();
Tim Abbott414fd312008-12-05 19:03:56 -05001905 sym = find_symbol(symbol, &owner, NULL, true, true);
1906 if (sym && strong_try_module_get(owner))
1907 sym = NULL;
Rusty Russell24da1cb2007-07-15 23:41:46 -07001908 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909
Tim Abbott414fd312008-12-05 19:03:56 -05001910 return sym ? (void *)sym->value : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911}
1912EXPORT_SYMBOL_GPL(__symbol_get);
1913
Ashutosh Naikeea8b542006-01-08 01:04:25 -08001914/*
1915 * Ensure that an exported symbol [global namespace] does not already exist
Robert P. J. Day02a3e592007-05-09 07:26:28 +02001916 * in the kernel or in some other module's exported symbol table.
Rusty Russellbe593f42010-06-05 11:17:37 -06001917 *
1918 * You must hold the module_mutex.
Ashutosh Naikeea8b542006-01-08 01:04:25 -08001919 */
1920static int verify_export_symbols(struct module *mod)
1921{
Rusty Russellb2111042008-05-01 21:15:00 -05001922 unsigned int i;
Ashutosh Naikeea8b542006-01-08 01:04:25 -08001923 struct module *owner;
Rusty Russellb2111042008-05-01 21:15:00 -05001924 const struct kernel_symbol *s;
1925 struct {
1926 const struct kernel_symbol *sym;
1927 unsigned int num;
1928 } arr[] = {
1929 { mod->syms, mod->num_syms },
1930 { mod->gpl_syms, mod->num_gpl_syms },
1931 { mod->gpl_future_syms, mod->num_gpl_future_syms },
Denys Vlasenkof7f5b672008-07-22 19:24:26 -05001932#ifdef CONFIG_UNUSED_SYMBOLS
Rusty Russellb2111042008-05-01 21:15:00 -05001933 { mod->unused_syms, mod->num_unused_syms },
1934 { mod->unused_gpl_syms, mod->num_unused_gpl_syms },
Denys Vlasenkof7f5b672008-07-22 19:24:26 -05001935#endif
Rusty Russellb2111042008-05-01 21:15:00 -05001936 };
Ashutosh Naikeea8b542006-01-08 01:04:25 -08001937
Rusty Russellb2111042008-05-01 21:15:00 -05001938 for (i = 0; i < ARRAY_SIZE(arr); i++) {
1939 for (s = arr[i].sym; s < arr[i].sym + arr[i].num; s++) {
Rusty Russellbe593f42010-06-05 11:17:37 -06001940 if (find_symbol(s->name, &owner, NULL, true, false)) {
Rusty Russellb2111042008-05-01 21:15:00 -05001941 printk(KERN_ERR
1942 "%s: exports duplicate symbol %s"
1943 " (owned by %s)\n",
1944 mod->name, s->name, module_name(owner));
1945 return -ENOEXEC;
1946 }
Ashutosh Naikeea8b542006-01-08 01:04:25 -08001947 }
Rusty Russellb2111042008-05-01 21:15:00 -05001948 }
1949 return 0;
Ashutosh Naikeea8b542006-01-08 01:04:25 -08001950}
1951
Matti Linnanvuori9a4b9702007-11-08 08:37:38 -08001952/* Change all symbols so that st_value encodes the pointer directly. */
Rusty Russell49668682010-08-05 12:59:10 -06001953static int simplify_symbols(struct module *mod, const struct load_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954{
Rusty Russell49668682010-08-05 12:59:10 -06001955 Elf_Shdr *symsec = &info->sechdrs[info->index.sym];
1956 Elf_Sym *sym = (void *)symsec->sh_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957 unsigned long secbase;
Rusty Russell49668682010-08-05 12:59:10 -06001958 unsigned int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959 int ret = 0;
Tim Abbott414fd312008-12-05 19:03:56 -05001960 const struct kernel_symbol *ksym;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961
Rusty Russell49668682010-08-05 12:59:10 -06001962 for (i = 1; i < symsec->sh_size / sizeof(Elf_Sym); i++) {
1963 const char *name = info->strtab + sym[i].st_name;
1964
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965 switch (sym[i].st_shndx) {
1966 case SHN_COMMON:
1967 /* We compiled with -fno-common. These are not
1968 supposed to happen. */
Jim Cromie5e124162011-12-06 12:11:31 -07001969 pr_debug("Common symbol: %s\n", name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970 printk("%s: please compile with -fno-common\n",
1971 mod->name);
1972 ret = -ENOEXEC;
1973 break;
1974
1975 case SHN_ABS:
1976 /* Don't need to do anything */
Jim Cromie5e124162011-12-06 12:11:31 -07001977 pr_debug("Absolute symbol: 0x%08lx\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978 (long)sym[i].st_value);
1979 break;
1980
1981 case SHN_UNDEF:
Rusty Russell49668682010-08-05 12:59:10 -06001982 ksym = resolve_symbol_wait(mod, info, name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983 /* Ok if resolved. */
Rusty Russell9bea7f22010-06-05 11:17:37 -06001984 if (ksym && !IS_ERR(ksym)) {
Tim Abbott414fd312008-12-05 19:03:56 -05001985 sym[i].st_value = ksym->value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986 break;
Tim Abbott414fd312008-12-05 19:03:56 -05001987 }
1988
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989 /* Ok if weak. */
Rusty Russell9bea7f22010-06-05 11:17:37 -06001990 if (!ksym && ELF_ST_BIND(sym[i].st_info) == STB_WEAK)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991 break;
1992
Rusty Russell9bea7f22010-06-05 11:17:37 -06001993 printk(KERN_WARNING "%s: Unknown symbol %s (err %li)\n",
Rusty Russell49668682010-08-05 12:59:10 -06001994 mod->name, name, PTR_ERR(ksym));
Rusty Russell9bea7f22010-06-05 11:17:37 -06001995 ret = PTR_ERR(ksym) ?: -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996 break;
1997
1998 default:
1999 /* Divert to percpu allocation if a percpu var. */
Rusty Russell49668682010-08-05 12:59:10 -06002000 if (sym[i].st_shndx == info->index.pcpu)
Tejun Heo259354d2010-03-10 18:56:10 +09002001 secbase = (unsigned long)mod_percpu(mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002 else
Rusty Russell49668682010-08-05 12:59:10 -06002003 secbase = info->sechdrs[sym[i].st_shndx].sh_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004 sym[i].st_value += secbase;
2005 break;
2006 }
2007 }
2008
2009 return ret;
2010}
2011
Rusty Russell49668682010-08-05 12:59:10 -06002012static int apply_relocations(struct module *mod, const struct load_info *info)
Rusty Russell22e268e2010-08-05 12:59:05 -06002013{
2014 unsigned int i;
2015 int err = 0;
2016
2017 /* Now do relocations. */
Rusty Russell49668682010-08-05 12:59:10 -06002018 for (i = 1; i < info->hdr->e_shnum; i++) {
2019 unsigned int infosec = info->sechdrs[i].sh_info;
Rusty Russell22e268e2010-08-05 12:59:05 -06002020
2021 /* Not a valid relocation section? */
Rusty Russell49668682010-08-05 12:59:10 -06002022 if (infosec >= info->hdr->e_shnum)
Rusty Russell22e268e2010-08-05 12:59:05 -06002023 continue;
2024
2025 /* Don't bother with non-allocated sections */
Rusty Russell49668682010-08-05 12:59:10 -06002026 if (!(info->sechdrs[infosec].sh_flags & SHF_ALLOC))
Rusty Russell22e268e2010-08-05 12:59:05 -06002027 continue;
2028
Rusty Russell49668682010-08-05 12:59:10 -06002029 if (info->sechdrs[i].sh_type == SHT_REL)
2030 err = apply_relocate(info->sechdrs, info->strtab,
2031 info->index.sym, i, mod);
2032 else if (info->sechdrs[i].sh_type == SHT_RELA)
2033 err = apply_relocate_add(info->sechdrs, info->strtab,
2034 info->index.sym, i, mod);
Rusty Russell22e268e2010-08-05 12:59:05 -06002035 if (err < 0)
2036 break;
2037 }
2038 return err;
2039}
2040
Helge Deller088af9a2008-12-31 12:31:18 +01002041/* Additional bytes needed by arch in front of individual sections */
2042unsigned int __weak arch_mod_section_prepend(struct module *mod,
2043 unsigned int section)
2044{
2045 /* default implementation just returns zero */
2046 return 0;
2047}
2048
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049/* Update size with this section: return offset. */
Helge Deller088af9a2008-12-31 12:31:18 +01002050static long get_offset(struct module *mod, unsigned int *size,
2051 Elf_Shdr *sechdr, unsigned int section)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052{
2053 long ret;
2054
Helge Deller088af9a2008-12-31 12:31:18 +01002055 *size += arch_mod_section_prepend(mod, section);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056 ret = ALIGN(*size, sechdr->sh_addralign ?: 1);
2057 *size = ret + sechdr->sh_size;
2058 return ret;
2059}
2060
2061/* Lay out the SHF_ALLOC sections in a way not dissimilar to how ld
2062 might -- code, read-only data, read-write data, small data. Tally
2063 sizes, and place the offsets into sh_entsize fields: high bit means it
2064 belongs in init. */
Rusty Russell49668682010-08-05 12:59:10 -06002065static void layout_sections(struct module *mod, struct load_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066{
2067 static unsigned long const masks[][2] = {
2068 /* NOTE: all executable code must be the first section
2069 * in this array; otherwise modify the text_size
2070 * finder in the two loops below */
2071 { SHF_EXECINSTR | SHF_ALLOC, ARCH_SHF_SMALL },
2072 { SHF_ALLOC, SHF_WRITE | ARCH_SHF_SMALL },
2073 { SHF_WRITE | SHF_ALLOC, ARCH_SHF_SMALL },
2074 { ARCH_SHF_SMALL | SHF_ALLOC, 0 }
2075 };
2076 unsigned int m, i;
2077
Rusty Russell49668682010-08-05 12:59:10 -06002078 for (i = 0; i < info->hdr->e_shnum; i++)
2079 info->sechdrs[i].sh_entsize = ~0UL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080
Jim Cromie5e124162011-12-06 12:11:31 -07002081 pr_debug("Core section allocation order:\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082 for (m = 0; m < ARRAY_SIZE(masks); ++m) {
Rusty Russell49668682010-08-05 12:59:10 -06002083 for (i = 0; i < info->hdr->e_shnum; ++i) {
2084 Elf_Shdr *s = &info->sechdrs[i];
2085 const char *sname = info->secstrings + s->sh_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086
2087 if ((s->sh_flags & masks[m][0]) != masks[m][0]
2088 || (s->sh_flags & masks[m][1])
2089 || s->sh_entsize != ~0UL
Rusty Russell49668682010-08-05 12:59:10 -06002090 || strstarts(sname, ".init"))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091 continue;
Helge Deller088af9a2008-12-31 12:31:18 +01002092 s->sh_entsize = get_offset(mod, &mod->core_size, s, i);
Jim Cromie5e124162011-12-06 12:11:31 -07002093 pr_debug("\t%s\n", sname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094 }
matthieu castet84e1c6b2010-11-16 22:35:16 +01002095 switch (m) {
2096 case 0: /* executable */
2097 mod->core_size = debug_align(mod->core_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098 mod->core_text_size = mod->core_size;
matthieu castet84e1c6b2010-11-16 22:35:16 +01002099 break;
2100 case 1: /* RO: text and ro-data */
2101 mod->core_size = debug_align(mod->core_size);
2102 mod->core_ro_size = mod->core_size;
2103 break;
2104 case 3: /* whole core */
2105 mod->core_size = debug_align(mod->core_size);
2106 break;
2107 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002108 }
2109
Jim Cromie5e124162011-12-06 12:11:31 -07002110 pr_debug("Init section allocation order:\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111 for (m = 0; m < ARRAY_SIZE(masks); ++m) {
Rusty Russell49668682010-08-05 12:59:10 -06002112 for (i = 0; i < info->hdr->e_shnum; ++i) {
2113 Elf_Shdr *s = &info->sechdrs[i];
2114 const char *sname = info->secstrings + s->sh_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115
2116 if ((s->sh_flags & masks[m][0]) != masks[m][0]
2117 || (s->sh_flags & masks[m][1])
2118 || s->sh_entsize != ~0UL
Rusty Russell49668682010-08-05 12:59:10 -06002119 || !strstarts(sname, ".init"))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120 continue;
Helge Deller088af9a2008-12-31 12:31:18 +01002121 s->sh_entsize = (get_offset(mod, &mod->init_size, s, i)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122 | INIT_OFFSET_MASK);
Jim Cromie5e124162011-12-06 12:11:31 -07002123 pr_debug("\t%s\n", sname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124 }
matthieu castet84e1c6b2010-11-16 22:35:16 +01002125 switch (m) {
2126 case 0: /* executable */
2127 mod->init_size = debug_align(mod->init_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128 mod->init_text_size = mod->init_size;
matthieu castet84e1c6b2010-11-16 22:35:16 +01002129 break;
2130 case 1: /* RO: text and ro-data */
2131 mod->init_size = debug_align(mod->init_size);
2132 mod->init_ro_size = mod->init_size;
2133 break;
2134 case 3: /* whole init */
2135 mod->init_size = debug_align(mod->init_size);
2136 break;
2137 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002138 }
2139}
2140
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141static void set_license(struct module *mod, const char *license)
2142{
2143 if (!license)
2144 license = "unspecified";
2145
Florin Malitafa3ba2e82006-10-11 01:21:48 -07002146 if (!license_is_gpl_compatible(license)) {
Andi Kleen25ddbb12008-10-15 22:01:41 -07002147 if (!test_taint(TAINT_PROPRIETARY_MODULE))
Jan Dittmer1d4d2622006-10-28 10:38:38 -07002148 printk(KERN_WARNING "%s: module license '%s' taints "
Florin Malitafa3ba2e82006-10-11 01:21:48 -07002149 "kernel.\n", mod->name, license);
2150 add_taint_module(mod, TAINT_PROPRIETARY_MODULE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151 }
2152}
2153
2154/* Parse tag=value strings from .modinfo section */
2155static char *next_string(char *string, unsigned long *secsize)
2156{
2157 /* Skip non-zero chars */
2158 while (string[0]) {
2159 string++;
2160 if ((*secsize)-- <= 1)
2161 return NULL;
2162 }
2163
2164 /* Skip any zero padding. */
2165 while (!string[0]) {
2166 string++;
2167 if ((*secsize)-- <= 1)
2168 return NULL;
2169 }
2170 return string;
2171}
2172
Rusty Russell49668682010-08-05 12:59:10 -06002173static char *get_modinfo(struct load_info *info, const char *tag)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174{
2175 char *p;
2176 unsigned int taglen = strlen(tag);
Rusty Russell49668682010-08-05 12:59:10 -06002177 Elf_Shdr *infosec = &info->sechdrs[info->index.info];
2178 unsigned long size = infosec->sh_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179
Rusty Russell49668682010-08-05 12:59:10 -06002180 for (p = (char *)infosec->sh_addr; p; p = next_string(p, &size)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181 if (strncmp(p, tag, taglen) == 0 && p[taglen] == '=')
2182 return p + taglen + 1;
2183 }
2184 return NULL;
2185}
2186
Rusty Russell49668682010-08-05 12:59:10 -06002187static void setup_modinfo(struct module *mod, struct load_info *info)
Matt Domschc988d2b2005-06-23 22:05:15 -07002188{
2189 struct module_attribute *attr;
2190 int i;
2191
2192 for (i = 0; (attr = modinfo_attrs[i]); i++) {
2193 if (attr->setup)
Rusty Russell49668682010-08-05 12:59:10 -06002194 attr->setup(mod, get_modinfo(info, attr->attr.name));
Matt Domschc988d2b2005-06-23 22:05:15 -07002195 }
2196}
Matt Domschc988d2b2005-06-23 22:05:15 -07002197
Rusty Russella263f772009-09-25 00:32:58 -06002198static void free_modinfo(struct module *mod)
2199{
2200 struct module_attribute *attr;
2201 int i;
2202
2203 for (i = 0; (attr = modinfo_attrs[i]); i++) {
2204 if (attr->free)
2205 attr->free(mod);
2206 }
2207}
2208
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209#ifdef CONFIG_KALLSYMS
WANG Cong15bba372008-07-24 15:41:48 +01002210
2211/* lookup symbol in given range of kernel_symbols */
2212static const struct kernel_symbol *lookup_symbol(const char *name,
2213 const struct kernel_symbol *start,
2214 const struct kernel_symbol *stop)
2215{
Alessio Igor Bogani9d634872011-05-18 22:35:59 +02002216 return bsearch(name, start, stop - start,
2217 sizeof(struct kernel_symbol), cmp_name);
WANG Cong15bba372008-07-24 15:41:48 +01002218}
2219
Tim Abbottca4787b2009-01-05 08:40:10 -06002220static int is_exported(const char *name, unsigned long value,
2221 const struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002222{
Tim Abbottca4787b2009-01-05 08:40:10 -06002223 const struct kernel_symbol *ks;
2224 if (!mod)
2225 ks = lookup_symbol(name, __start___ksymtab, __stop___ksymtab);
Sam Ravnborg3fd68052006-02-08 21:16:45 +01002226 else
Tim Abbottca4787b2009-01-05 08:40:10 -06002227 ks = lookup_symbol(name, mod->syms, mod->syms + mod->num_syms);
2228 return ks != NULL && ks->value == value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002229}
2230
2231/* As per nm */
Rusty Russelleded41c2010-08-05 12:59:07 -06002232static char elf_type(const Elf_Sym *sym, const struct load_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002233{
Rusty Russelleded41c2010-08-05 12:59:07 -06002234 const Elf_Shdr *sechdrs = info->sechdrs;
2235
Linus Torvalds1da177e2005-04-16 15:20:36 -07002236 if (ELF_ST_BIND(sym->st_info) == STB_WEAK) {
2237 if (ELF_ST_TYPE(sym->st_info) == STT_OBJECT)
2238 return 'v';
2239 else
2240 return 'w';
2241 }
2242 if (sym->st_shndx == SHN_UNDEF)
2243 return 'U';
2244 if (sym->st_shndx == SHN_ABS)
2245 return 'a';
2246 if (sym->st_shndx >= SHN_LORESERVE)
2247 return '?';
2248 if (sechdrs[sym->st_shndx].sh_flags & SHF_EXECINSTR)
2249 return 't';
2250 if (sechdrs[sym->st_shndx].sh_flags & SHF_ALLOC
2251 && sechdrs[sym->st_shndx].sh_type != SHT_NOBITS) {
2252 if (!(sechdrs[sym->st_shndx].sh_flags & SHF_WRITE))
2253 return 'r';
2254 else if (sechdrs[sym->st_shndx].sh_flags & ARCH_SHF_SMALL)
2255 return 'g';
2256 else
2257 return 'd';
2258 }
2259 if (sechdrs[sym->st_shndx].sh_type == SHT_NOBITS) {
2260 if (sechdrs[sym->st_shndx].sh_flags & ARCH_SHF_SMALL)
2261 return 's';
2262 else
2263 return 'b';
2264 }
Rusty Russelleded41c2010-08-05 12:59:07 -06002265 if (strstarts(info->secstrings + sechdrs[sym->st_shndx].sh_name,
2266 ".debug")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267 return 'n';
Rusty Russelleded41c2010-08-05 12:59:07 -06002268 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002269 return '?';
2270}
2271
Jan Beulich4a496222009-07-06 14:50:42 +01002272static bool is_core_symbol(const Elf_Sym *src, const Elf_Shdr *sechdrs,
2273 unsigned int shnum)
2274{
2275 const Elf_Shdr *sec;
2276
2277 if (src->st_shndx == SHN_UNDEF
2278 || src->st_shndx >= shnum
2279 || !src->st_name)
2280 return false;
2281
2282 sec = sechdrs + src->st_shndx;
2283 if (!(sec->sh_flags & SHF_ALLOC)
2284#ifndef CONFIG_KALLSYMS_ALL
2285 || !(sec->sh_flags & SHF_EXECINSTR)
2286#endif
2287 || (sec->sh_entsize & INIT_OFFSET_MASK))
2288 return false;
2289
2290 return true;
2291}
2292
Kevin Cernekee48fd1182012-01-13 09:32:14 +10302293/*
2294 * We only allocate and copy the strings needed by the parts of symtab
2295 * we keep. This is simple, but has the effect of making multiple
2296 * copies of duplicates. We could be more sophisticated, see
2297 * linux-kernel thread starting with
2298 * <73defb5e4bca04a6431392cc341112b1@localhost>.
2299 */
Rusty Russell49668682010-08-05 12:59:10 -06002300static void layout_symtab(struct module *mod, struct load_info *info)
Jan Beulich4a496222009-07-06 14:50:42 +01002301{
Rusty Russell49668682010-08-05 12:59:10 -06002302 Elf_Shdr *symsect = info->sechdrs + info->index.sym;
2303 Elf_Shdr *strsect = info->sechdrs + info->index.str;
Jan Beulich4a496222009-07-06 14:50:42 +01002304 const Elf_Sym *src;
Satoru Takeuchi54523ec2012-12-05 12:29:04 +10302305 unsigned int i, nsrc, ndst, strtab_size = 0;
Jan Beulich4a496222009-07-06 14:50:42 +01002306
2307 /* Put symbol section at end of init part of module. */
2308 symsect->sh_flags |= SHF_ALLOC;
2309 symsect->sh_entsize = get_offset(mod, &mod->init_size, symsect,
Rusty Russell49668682010-08-05 12:59:10 -06002310 info->index.sym) | INIT_OFFSET_MASK;
Jim Cromie5e124162011-12-06 12:11:31 -07002311 pr_debug("\t%s\n", info->secstrings + symsect->sh_name);
Jan Beulich4a496222009-07-06 14:50:42 +01002312
Rusty Russell49668682010-08-05 12:59:10 -06002313 src = (void *)info->hdr + symsect->sh_offset;
Jan Beulich4a496222009-07-06 14:50:42 +01002314 nsrc = symsect->sh_size / sizeof(*src);
Kevin Cernekee70b1e9162011-11-12 19:08:55 -08002315
Kevin Cernekee48fd1182012-01-13 09:32:14 +10302316 /* Compute total space required for the core symbols' strtab. */
Rusty Russell59ef28b2012-10-25 10:49:25 +10302317 for (ndst = i = 0; i < nsrc; i++) {
2318 if (i == 0 ||
2319 is_core_symbol(src+i, info->sechdrs, info->hdr->e_shnum)) {
2320 strtab_size += strlen(&info->strtab[src[i].st_name])+1;
Kevin Cernekee48fd1182012-01-13 09:32:14 +10302321 ndst++;
Jan Beulich554bdfe2009-07-06 14:51:44 +01002322 }
Rusty Russell59ef28b2012-10-25 10:49:25 +10302323 }
Jan Beulich4a496222009-07-06 14:50:42 +01002324
2325 /* Append room for core symbols at end of core part. */
Rusty Russell49668682010-08-05 12:59:10 -06002326 info->symoffs = ALIGN(mod->core_size, symsect->sh_addralign ?: 1);
Kevin Cernekee48fd1182012-01-13 09:32:14 +10302327 info->stroffs = mod->core_size = info->symoffs + ndst * sizeof(Elf_Sym);
2328 mod->core_size += strtab_size;
Jan Beulich4a496222009-07-06 14:50:42 +01002329
Jan Beulich554bdfe2009-07-06 14:51:44 +01002330 /* Put string table section at end of init part of module. */
2331 strsect->sh_flags |= SHF_ALLOC;
2332 strsect->sh_entsize = get_offset(mod, &mod->init_size, strsect,
Rusty Russell49668682010-08-05 12:59:10 -06002333 info->index.str) | INIT_OFFSET_MASK;
Jim Cromie5e124162011-12-06 12:11:31 -07002334 pr_debug("\t%s\n", info->secstrings + strsect->sh_name);
Jan Beulich4a496222009-07-06 14:50:42 +01002335}
2336
Rusty Russell811d66a2010-08-05 12:59:12 -06002337static void add_kallsyms(struct module *mod, const struct load_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002338{
Jan Beulich4a496222009-07-06 14:50:42 +01002339 unsigned int i, ndst;
2340 const Elf_Sym *src;
2341 Elf_Sym *dst;
Jan Beulich554bdfe2009-07-06 14:51:44 +01002342 char *s;
Rusty Russelleded41c2010-08-05 12:59:07 -06002343 Elf_Shdr *symsec = &info->sechdrs[info->index.sym];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002344
Rusty Russelleded41c2010-08-05 12:59:07 -06002345 mod->symtab = (void *)symsec->sh_addr;
2346 mod->num_symtab = symsec->sh_size / sizeof(Elf_Sym);
Rusty Russell511ca6a2010-08-05 12:59:08 -06002347 /* Make sure we get permanent strtab: don't use info->strtab. */
2348 mod->strtab = (void *)info->sechdrs[info->index.str].sh_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002349
2350 /* Set types up while we still have access to sections. */
2351 for (i = 0; i < mod->num_symtab; i++)
Rusty Russelleded41c2010-08-05 12:59:07 -06002352 mod->symtab[i].st_info = elf_type(&mod->symtab[i], info);
Jan Beulich4a496222009-07-06 14:50:42 +01002353
Rusty Russelld9131882010-08-05 12:59:08 -06002354 mod->core_symtab = dst = mod->module_core + info->symoffs;
Kevin Cernekee48fd1182012-01-13 09:32:14 +10302355 mod->core_strtab = s = mod->module_core + info->stroffs;
Jan Beulich4a496222009-07-06 14:50:42 +01002356 src = mod->symtab;
Rusty Russell59ef28b2012-10-25 10:49:25 +10302357 for (ndst = i = 0; i < mod->num_symtab; i++) {
2358 if (i == 0 ||
2359 is_core_symbol(src+i, info->sechdrs, info->hdr->e_shnum)) {
2360 dst[ndst] = src[i];
2361 dst[ndst++].st_name = s - mod->core_strtab;
2362 s += strlcpy(s, &mod->strtab[src[i].st_name],
2363 KSYM_NAME_LEN) + 1;
2364 }
Jan Beulich4a496222009-07-06 14:50:42 +01002365 }
2366 mod->core_num_syms = ndst;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367}
2368#else
Rusty Russell49668682010-08-05 12:59:10 -06002369static inline void layout_symtab(struct module *mod, struct load_info *info)
Jan Beulich4a496222009-07-06 14:50:42 +01002370{
2371}
Paul Mundt3ae91c22009-10-01 15:43:54 -07002372
Michał Mirosławabbce902010-09-20 01:58:08 +02002373static void add_kallsyms(struct module *mod, const struct load_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002374{
2375}
2376#endif /* CONFIG_KALLSYMS */
2377
Jason Barone9d376f2009-02-05 11:51:38 -05002378static void dynamic_debug_setup(struct _ddebug *debug, unsigned int num)
Rusty Russell5e458cc2008-10-22 10:00:13 -05002379{
Rusty Russell811d66a2010-08-05 12:59:12 -06002380 if (!debug)
2381 return;
Jason Barone9d376f2009-02-05 11:51:38 -05002382#ifdef CONFIG_DYNAMIC_DEBUG
2383 if (ddebug_add_module(debug, num, debug->modname))
2384 printk(KERN_ERR "dynamic debug error adding module: %s\n",
2385 debug->modname);
2386#endif
Rusty Russell5e458cc2008-10-22 10:00:13 -05002387}
Jason Baron346e15b2008-08-12 16:46:19 -04002388
Yehuda Sadehff49d742010-07-03 13:07:35 +10002389static void dynamic_debug_remove(struct _ddebug *debug)
2390{
2391 if (debug)
2392 ddebug_remove_module(debug->modname);
2393}
2394
Jonas Bonn74e08fc2011-06-30 21:22:11 +02002395void * __weak module_alloc(unsigned long size)
2396{
Rusty Russell82fab442012-12-11 09:38:33 +10302397 return vmalloc_exec(size);
Jonas Bonn74e08fc2011-06-30 21:22:11 +02002398}
2399
Rusty Russell3a642e92008-07-22 19:24:28 -05002400static void *module_alloc_update_bounds(unsigned long size)
2401{
2402 void *ret = module_alloc(size);
2403
2404 if (ret) {
Rusty Russell75676502010-06-05 11:17:36 -06002405 mutex_lock(&module_mutex);
Rusty Russell3a642e92008-07-22 19:24:28 -05002406 /* Update module bounds. */
2407 if ((unsigned long)ret < module_addr_min)
2408 module_addr_min = (unsigned long)ret;
2409 if ((unsigned long)ret + size > module_addr_max)
2410 module_addr_max = (unsigned long)ret + size;
Rusty Russell75676502010-06-05 11:17:36 -06002411 mutex_unlock(&module_mutex);
Rusty Russell3a642e92008-07-22 19:24:28 -05002412 }
2413 return ret;
2414}
2415
Catalin Marinas4f2294b2009-06-11 13:23:20 +01002416#ifdef CONFIG_DEBUG_KMEMLEAK
Rusty Russell49668682010-08-05 12:59:10 -06002417static void kmemleak_load_module(const struct module *mod,
2418 const struct load_info *info)
Catalin Marinas4f2294b2009-06-11 13:23:20 +01002419{
2420 unsigned int i;
2421
2422 /* only scan the sections containing data */
Catalin Marinasc017b4b2009-10-28 13:33:09 +00002423 kmemleak_scan_area(mod, sizeof(struct module), GFP_KERNEL);
Catalin Marinas4f2294b2009-06-11 13:23:20 +01002424
Rusty Russell49668682010-08-05 12:59:10 -06002425 for (i = 1; i < info->hdr->e_shnum; i++) {
2426 const char *name = info->secstrings + info->sechdrs[i].sh_name;
2427 if (!(info->sechdrs[i].sh_flags & SHF_ALLOC))
Catalin Marinas4f2294b2009-06-11 13:23:20 +01002428 continue;
Rusty Russell49668682010-08-05 12:59:10 -06002429 if (!strstarts(name, ".data") && !strstarts(name, ".bss"))
Catalin Marinas4f2294b2009-06-11 13:23:20 +01002430 continue;
2431
Rusty Russell49668682010-08-05 12:59:10 -06002432 kmemleak_scan_area((void *)info->sechdrs[i].sh_addr,
2433 info->sechdrs[i].sh_size, GFP_KERNEL);
Catalin Marinas4f2294b2009-06-11 13:23:20 +01002434 }
2435}
2436#else
Rusty Russell49668682010-08-05 12:59:10 -06002437static inline void kmemleak_load_module(const struct module *mod,
2438 const struct load_info *info)
Catalin Marinas4f2294b2009-06-11 13:23:20 +01002439{
2440}
2441#endif
2442
Rusty Russell106a4ee2012-09-26 10:09:40 +01002443#ifdef CONFIG_MODULE_SIG
Kees Cook34e11692012-10-16 07:31:07 +10302444static int module_sig_check(struct load_info *info)
Rusty Russell106a4ee2012-09-26 10:09:40 +01002445{
2446 int err = -ENOKEY;
Kees Cook34e11692012-10-16 07:31:07 +10302447 const unsigned long markerlen = sizeof(MODULE_SIG_STRING) - 1;
2448 const void *mod = info->hdr;
Rusty Russell106a4ee2012-09-26 10:09:40 +01002449
Kees Cook34e11692012-10-16 07:31:07 +10302450 if (info->len > markerlen &&
2451 memcmp(mod + info->len - markerlen, MODULE_SIG_STRING, markerlen) == 0) {
David Howellscaabe242012-10-20 01:19:29 +01002452 /* We truncate the module to discard the signature */
Kees Cook34e11692012-10-16 07:31:07 +10302453 info->len -= markerlen;
2454 err = mod_verify_sig(mod, &info->len);
Rusty Russell106a4ee2012-09-26 10:09:40 +01002455 }
2456
2457 if (!err) {
2458 info->sig_ok = true;
2459 return 0;
2460 }
2461
2462 /* Not having a signature is only an error if we're strict. */
David Howells1d0059f2012-09-26 10:09:50 +01002463 if (err < 0 && fips_enabled)
2464 panic("Module verification failed with error %d in FIPS mode\n",
2465 err);
Rusty Russell106a4ee2012-09-26 10:09:40 +01002466 if (err == -ENOKEY && !sig_enforce)
2467 err = 0;
2468
2469 return err;
2470}
2471#else /* !CONFIG_MODULE_SIG */
Kees Cook34e11692012-10-16 07:31:07 +10302472static int module_sig_check(struct load_info *info)
Rusty Russell106a4ee2012-09-26 10:09:40 +01002473{
2474 return 0;
2475}
2476#endif /* !CONFIG_MODULE_SIG */
2477
Kees Cook34e11692012-10-16 07:31:07 +10302478/* Sanity checks against invalid binaries, wrong arch, weird elf version. */
2479static int elf_header_check(struct load_info *info)
Rusty Russell40dd2562010-08-05 12:59:03 -06002480{
Kees Cook34e11692012-10-16 07:31:07 +10302481 if (info->len < sizeof(*(info->hdr)))
Rusty Russell40dd2562010-08-05 12:59:03 -06002482 return -ENOEXEC;
2483
Kees Cook34e11692012-10-16 07:31:07 +10302484 if (memcmp(info->hdr->e_ident, ELFMAG, SELFMAG) != 0
2485 || info->hdr->e_type != ET_REL
2486 || !elf_check_arch(info->hdr)
2487 || info->hdr->e_shentsize != sizeof(Elf_Shdr))
2488 return -ENOEXEC;
2489
2490 if (info->hdr->e_shoff >= info->len
2491 || (info->hdr->e_shnum * sizeof(Elf_Shdr) >
2492 info->len - info->hdr->e_shoff))
2493 return -ENOEXEC;
2494
2495 return 0;
2496}
2497
2498/* Sets info->hdr and info->len. */
2499static int copy_module_from_user(const void __user *umod, unsigned long len,
2500 struct load_info *info)
2501{
Kees Cook2e72d512012-10-16 07:32:07 +10302502 int err;
2503
Kees Cook34e11692012-10-16 07:31:07 +10302504 info->len = len;
2505 if (info->len < sizeof(*(info->hdr)))
Rusty Russell40dd2562010-08-05 12:59:03 -06002506 return -ENOEXEC;
2507
Kees Cook2e72d512012-10-16 07:32:07 +10302508 err = security_kernel_module_from_file(NULL);
2509 if (err)
2510 return err;
2511
Rusty Russell40dd2562010-08-05 12:59:03 -06002512 /* Suck in entire file: we'll want most of it. */
Kees Cook34e11692012-10-16 07:31:07 +10302513 info->hdr = vmalloc(info->len);
2514 if (!info->hdr)
Rusty Russell40dd2562010-08-05 12:59:03 -06002515 return -ENOMEM;
2516
Kees Cook34e11692012-10-16 07:31:07 +10302517 if (copy_from_user(info->hdr, umod, info->len) != 0) {
2518 vfree(info->hdr);
2519 return -EFAULT;
Rusty Russell40dd2562010-08-05 12:59:03 -06002520 }
2521
Rusty Russell40dd2562010-08-05 12:59:03 -06002522 return 0;
Kees Cook34e11692012-10-16 07:31:07 +10302523}
Rusty Russell40dd2562010-08-05 12:59:03 -06002524
Kees Cook34e11692012-10-16 07:31:07 +10302525/* Sets info->hdr and info->len. */
2526static int copy_module_from_fd(int fd, struct load_info *info)
2527{
2528 struct file *file;
2529 int err;
2530 struct kstat stat;
2531 loff_t pos;
2532 ssize_t bytes = 0;
2533
2534 file = fget(fd);
2535 if (!file)
2536 return -ENOEXEC;
2537
Kees Cook2e72d512012-10-16 07:32:07 +10302538 err = security_kernel_module_from_file(file);
2539 if (err)
2540 goto out;
2541
Kees Cook34e11692012-10-16 07:31:07 +10302542 err = vfs_getattr(file->f_vfsmnt, file->f_dentry, &stat);
2543 if (err)
2544 goto out;
2545
2546 if (stat.size > INT_MAX) {
2547 err = -EFBIG;
2548 goto out;
2549 }
Sasha Levin52441fa2013-01-03 11:09:53 +10302550
2551 /* Don't hand 0 to vmalloc, it whines. */
2552 if (stat.size == 0) {
2553 err = -EINVAL;
2554 goto out;
2555 }
2556
Kees Cook34e11692012-10-16 07:31:07 +10302557 info->hdr = vmalloc(stat.size);
2558 if (!info->hdr) {
2559 err = -ENOMEM;
2560 goto out;
2561 }
2562
2563 pos = 0;
2564 while (pos < stat.size) {
2565 bytes = kernel_read(file, pos, (char *)(info->hdr) + pos,
2566 stat.size - pos);
2567 if (bytes < 0) {
2568 vfree(info->hdr);
2569 err = bytes;
2570 goto out;
2571 }
2572 if (bytes == 0)
2573 break;
2574 pos += bytes;
2575 }
2576 info->len = pos;
2577
2578out:
2579 fput(file);
Rusty Russell40dd2562010-08-05 12:59:03 -06002580 return err;
2581}
2582
Rusty Russelld9131882010-08-05 12:59:08 -06002583static void free_copy(struct load_info *info)
2584{
Rusty Russelld9131882010-08-05 12:59:08 -06002585 vfree(info->hdr);
2586}
2587
Rusty Russell2f3238a2012-10-22 18:09:41 +10302588static int rewrite_section_headers(struct load_info *info, int flags)
Rusty Russell8b5f61a2010-08-05 12:59:06 -06002589{
2590 unsigned int i;
2591
2592 /* This should always be true, but let's be sure. */
2593 info->sechdrs[0].sh_addr = 0;
2594
2595 for (i = 1; i < info->hdr->e_shnum; i++) {
2596 Elf_Shdr *shdr = &info->sechdrs[i];
2597 if (shdr->sh_type != SHT_NOBITS
2598 && info->len < shdr->sh_offset + shdr->sh_size) {
2599 printk(KERN_ERR "Module len %lu truncated\n",
2600 info->len);
2601 return -ENOEXEC;
2602 }
2603
2604 /* Mark all sections sh_addr with their address in the
2605 temporary image. */
2606 shdr->sh_addr = (size_t)info->hdr + shdr->sh_offset;
2607
2608#ifndef CONFIG_MODULE_UNLOAD
2609 /* Don't load .exit sections */
2610 if (strstarts(info->secstrings+shdr->sh_name, ".exit"))
2611 shdr->sh_flags &= ~(unsigned long)SHF_ALLOC;
2612#endif
Rusty Russell8b5f61a2010-08-05 12:59:06 -06002613 }
Rusty Russelld6df72a2010-08-05 12:59:07 -06002614
2615 /* Track but don't keep modinfo and version sections. */
Rusty Russell2f3238a2012-10-22 18:09:41 +10302616 if (flags & MODULE_INIT_IGNORE_MODVERSIONS)
2617 info->index.vers = 0; /* Pretend no __versions section! */
2618 else
2619 info->index.vers = find_sec(info, "__versions");
Rusty Russell49668682010-08-05 12:59:10 -06002620 info->index.info = find_sec(info, ".modinfo");
Rusty Russelld6df72a2010-08-05 12:59:07 -06002621 info->sechdrs[info->index.info].sh_flags &= ~(unsigned long)SHF_ALLOC;
2622 info->sechdrs[info->index.vers].sh_flags &= ~(unsigned long)SHF_ALLOC;
Rusty Russell8b5f61a2010-08-05 12:59:06 -06002623 return 0;
2624}
2625
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002626/*
2627 * Set up our basic convenience variables (pointers to section headers,
2628 * search for module section index etc), and do some basic section
2629 * verification.
2630 *
2631 * Return the temporary module pointer (we'll replace it with the final
2632 * one when we move the module sections around).
2633 */
Rusty Russell2f3238a2012-10-22 18:09:41 +10302634static struct module *setup_load_info(struct load_info *info, int flags)
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002635{
2636 unsigned int i;
Rusty Russell8b5f61a2010-08-05 12:59:06 -06002637 int err;
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002638 struct module *mod;
2639
2640 /* Set up the convenience variables */
2641 info->sechdrs = (void *)info->hdr + info->hdr->e_shoff;
Rusty Russell8b5f61a2010-08-05 12:59:06 -06002642 info->secstrings = (void *)info->hdr
2643 + info->sechdrs[info->hdr->e_shstrndx].sh_offset;
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002644
Rusty Russell2f3238a2012-10-22 18:09:41 +10302645 err = rewrite_section_headers(info, flags);
Rusty Russell8b5f61a2010-08-05 12:59:06 -06002646 if (err)
2647 return ERR_PTR(err);
2648
2649 /* Find internal symbols and strings. */
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002650 for (i = 1; i < info->hdr->e_shnum; i++) {
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002651 if (info->sechdrs[i].sh_type == SHT_SYMTAB) {
2652 info->index.sym = i;
2653 info->index.str = info->sechdrs[i].sh_link;
Rusty Russell8b5f61a2010-08-05 12:59:06 -06002654 info->strtab = (char *)info->hdr
2655 + info->sechdrs[info->index.str].sh_offset;
2656 break;
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002657 }
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002658 }
2659
Rusty Russell49668682010-08-05 12:59:10 -06002660 info->index.mod = find_sec(info, ".gnu.linkonce.this_module");
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002661 if (!info->index.mod) {
2662 printk(KERN_WARNING "No module found in object\n");
2663 return ERR_PTR(-ENOEXEC);
2664 }
2665 /* This is temporary: point mod into copy of data. */
2666 mod = (void *)info->sechdrs[info->index.mod].sh_addr;
2667
2668 if (info->index.sym == 0) {
2669 printk(KERN_WARNING "%s: module has no symbols (stripped?)\n",
2670 mod->name);
2671 return ERR_PTR(-ENOEXEC);
2672 }
2673
Rusty Russell49668682010-08-05 12:59:10 -06002674 info->index.pcpu = find_pcpusec(info);
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002675
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002676 /* Check module struct version now, before we try to use module. */
2677 if (!check_modstruct_version(info->sechdrs, info->index.vers, mod))
2678 return ERR_PTR(-ENOEXEC);
2679
2680 return mod;
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002681}
2682
Rusty Russell2f3238a2012-10-22 18:09:41 +10302683static int check_modinfo(struct module *mod, struct load_info *info, int flags)
Rusty Russell40dd2562010-08-05 12:59:03 -06002684{
Rusty Russell49668682010-08-05 12:59:10 -06002685 const char *modmagic = get_modinfo(info, "vermagic");
Rusty Russell40dd2562010-08-05 12:59:03 -06002686 int err;
2687
Rusty Russell2f3238a2012-10-22 18:09:41 +10302688 if (flags & MODULE_INIT_IGNORE_VERMAGIC)
2689 modmagic = NULL;
2690
Rusty Russell40dd2562010-08-05 12:59:03 -06002691 /* This is allowed: modprobe --force will invalidate it. */
2692 if (!modmagic) {
2693 err = try_to_force_load(mod, "bad vermagic");
2694 if (err)
2695 return err;
Rusty Russell49668682010-08-05 12:59:10 -06002696 } else if (!same_magic(modmagic, vermagic, info->index.vers)) {
Rusty Russell40dd2562010-08-05 12:59:03 -06002697 printk(KERN_ERR "%s: version magic '%s' should be '%s'\n",
2698 mod->name, modmagic, vermagic);
2699 return -ENOEXEC;
2700 }
2701
Ben Hutchings2449b8b2011-10-24 15:12:28 +02002702 if (!get_modinfo(info, "intree"))
2703 add_taint_module(mod, TAINT_OOT_MODULE);
2704
Rusty Russell49668682010-08-05 12:59:10 -06002705 if (get_modinfo(info, "staging")) {
Rusty Russell40dd2562010-08-05 12:59:03 -06002706 add_taint_module(mod, TAINT_CRAP);
2707 printk(KERN_WARNING "%s: module is from the staging directory,"
2708 " the quality is unknown, you have been warned.\n",
2709 mod->name);
2710 }
Rusty Russell22e268e2010-08-05 12:59:05 -06002711
2712 /* Set up license info based on the info section */
Rusty Russell49668682010-08-05 12:59:10 -06002713 set_license(mod, get_modinfo(info, "license"));
Rusty Russell22e268e2010-08-05 12:59:05 -06002714
Rusty Russell40dd2562010-08-05 12:59:03 -06002715 return 0;
2716}
2717
Rusty Russell811d66a2010-08-05 12:59:12 -06002718static void find_module_sections(struct module *mod, struct load_info *info)
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06002719{
Rusty Russell49668682010-08-05 12:59:10 -06002720 mod->kp = section_objs(info, "__param",
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06002721 sizeof(*mod->kp), &mod->num_kp);
Rusty Russell49668682010-08-05 12:59:10 -06002722 mod->syms = section_objs(info, "__ksymtab",
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06002723 sizeof(*mod->syms), &mod->num_syms);
Rusty Russell49668682010-08-05 12:59:10 -06002724 mod->crcs = section_addr(info, "__kcrctab");
2725 mod->gpl_syms = section_objs(info, "__ksymtab_gpl",
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06002726 sizeof(*mod->gpl_syms),
2727 &mod->num_gpl_syms);
Rusty Russell49668682010-08-05 12:59:10 -06002728 mod->gpl_crcs = section_addr(info, "__kcrctab_gpl");
2729 mod->gpl_future_syms = section_objs(info,
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06002730 "__ksymtab_gpl_future",
2731 sizeof(*mod->gpl_future_syms),
2732 &mod->num_gpl_future_syms);
Rusty Russell49668682010-08-05 12:59:10 -06002733 mod->gpl_future_crcs = section_addr(info, "__kcrctab_gpl_future");
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06002734
2735#ifdef CONFIG_UNUSED_SYMBOLS
Rusty Russell49668682010-08-05 12:59:10 -06002736 mod->unused_syms = section_objs(info, "__ksymtab_unused",
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06002737 sizeof(*mod->unused_syms),
2738 &mod->num_unused_syms);
Rusty Russell49668682010-08-05 12:59:10 -06002739 mod->unused_crcs = section_addr(info, "__kcrctab_unused");
2740 mod->unused_gpl_syms = section_objs(info, "__ksymtab_unused_gpl",
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06002741 sizeof(*mod->unused_gpl_syms),
2742 &mod->num_unused_gpl_syms);
Rusty Russell49668682010-08-05 12:59:10 -06002743 mod->unused_gpl_crcs = section_addr(info, "__kcrctab_unused_gpl");
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06002744#endif
2745#ifdef CONFIG_CONSTRUCTORS
Rusty Russell49668682010-08-05 12:59:10 -06002746 mod->ctors = section_objs(info, ".ctors",
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06002747 sizeof(*mod->ctors), &mod->num_ctors);
2748#endif
2749
2750#ifdef CONFIG_TRACEPOINTS
Mathieu Desnoyers65498642011-01-26 17:26:22 -05002751 mod->tracepoints_ptrs = section_objs(info, "__tracepoints_ptrs",
2752 sizeof(*mod->tracepoints_ptrs),
2753 &mod->num_tracepoints);
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06002754#endif
Jason Baronbf5438fc2010-09-17 11:09:00 -04002755#ifdef HAVE_JUMP_LABEL
2756 mod->jump_entries = section_objs(info, "__jump_table",
2757 sizeof(*mod->jump_entries),
2758 &mod->num_jump_entries);
2759#endif
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06002760#ifdef CONFIG_EVENT_TRACING
Rusty Russell49668682010-08-05 12:59:10 -06002761 mod->trace_events = section_objs(info, "_ftrace_events",
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06002762 sizeof(*mod->trace_events),
2763 &mod->num_trace_events);
2764 /*
2765 * This section contains pointers to allocated objects in the trace
2766 * code and not scanning it leads to false positives.
2767 */
2768 kmemleak_scan_area(mod->trace_events, sizeof(*mod->trace_events) *
2769 mod->num_trace_events, GFP_KERNEL);
2770#endif
Steven Rostedt13b9b6e2010-11-10 22:19:24 -05002771#ifdef CONFIG_TRACING
2772 mod->trace_bprintk_fmt_start = section_objs(info, "__trace_printk_fmt",
2773 sizeof(*mod->trace_bprintk_fmt_start),
2774 &mod->num_trace_bprintk_fmt);
2775 /*
2776 * This section contains pointers to allocated objects in the trace
2777 * code and not scanning it leads to false positives.
2778 */
2779 kmemleak_scan_area(mod->trace_bprintk_fmt_start,
2780 sizeof(*mod->trace_bprintk_fmt_start) *
2781 mod->num_trace_bprintk_fmt, GFP_KERNEL);
2782#endif
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06002783#ifdef CONFIG_FTRACE_MCOUNT_RECORD
2784 /* sechdrs[0].sh_size is always zero */
Rusty Russell49668682010-08-05 12:59:10 -06002785 mod->ftrace_callsites = section_objs(info, "__mcount_loc",
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06002786 sizeof(*mod->ftrace_callsites),
2787 &mod->num_ftrace_callsites);
2788#endif
Rusty Russell22e268e2010-08-05 12:59:05 -06002789
Rusty Russell811d66a2010-08-05 12:59:12 -06002790 mod->extable = section_objs(info, "__ex_table",
2791 sizeof(*mod->extable), &mod->num_exentries);
2792
Rusty Russell49668682010-08-05 12:59:10 -06002793 if (section_addr(info, "__obsparm"))
Rusty Russell22e268e2010-08-05 12:59:05 -06002794 printk(KERN_WARNING "%s: Ignoring obsolete parameters\n",
2795 mod->name);
Rusty Russell811d66a2010-08-05 12:59:12 -06002796
2797 info->debug = section_objs(info, "__verbose",
2798 sizeof(*info->debug), &info->num_debug);
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06002799}
2800
Rusty Russell49668682010-08-05 12:59:10 -06002801static int move_module(struct module *mod, struct load_info *info)
Linus Torvalds65b8a9b2010-08-05 12:59:02 -06002802{
2803 int i;
2804 void *ptr;
2805
2806 /* Do the allocs. */
2807 ptr = module_alloc_update_bounds(mod->core_size);
2808 /*
2809 * The pointer to this block is stored in the module structure
2810 * which is inside the block. Just mark it as not being a
2811 * leak.
2812 */
2813 kmemleak_not_leak(ptr);
2814 if (!ptr)
Rusty Russelld9131882010-08-05 12:59:08 -06002815 return -ENOMEM;
Linus Torvalds65b8a9b2010-08-05 12:59:02 -06002816
2817 memset(ptr, 0, mod->core_size);
2818 mod->module_core = ptr;
2819
Rusty Russell82fab442012-12-11 09:38:33 +10302820 if (mod->init_size) {
2821 ptr = module_alloc_update_bounds(mod->init_size);
2822 /*
2823 * The pointer to this block is stored in the module structure
2824 * which is inside the block. This block doesn't need to be
2825 * scanned as it contains data and code that will be freed
2826 * after the module is initialized.
2827 */
2828 kmemleak_ignore(ptr);
2829 if (!ptr) {
2830 module_free(mod, mod->module_core);
2831 return -ENOMEM;
2832 }
2833 memset(ptr, 0, mod->init_size);
2834 mod->module_init = ptr;
2835 } else
2836 mod->module_init = NULL;
Linus Torvalds65b8a9b2010-08-05 12:59:02 -06002837
2838 /* Transfer each section which specifies SHF_ALLOC */
Jim Cromie5e124162011-12-06 12:11:31 -07002839 pr_debug("final section addresses:\n");
Rusty Russell49668682010-08-05 12:59:10 -06002840 for (i = 0; i < info->hdr->e_shnum; i++) {
Linus Torvalds65b8a9b2010-08-05 12:59:02 -06002841 void *dest;
Rusty Russell49668682010-08-05 12:59:10 -06002842 Elf_Shdr *shdr = &info->sechdrs[i];
Linus Torvalds65b8a9b2010-08-05 12:59:02 -06002843
Rusty Russell49668682010-08-05 12:59:10 -06002844 if (!(shdr->sh_flags & SHF_ALLOC))
Linus Torvalds65b8a9b2010-08-05 12:59:02 -06002845 continue;
2846
Rusty Russell49668682010-08-05 12:59:10 -06002847 if (shdr->sh_entsize & INIT_OFFSET_MASK)
Linus Torvalds65b8a9b2010-08-05 12:59:02 -06002848 dest = mod->module_init
Rusty Russell49668682010-08-05 12:59:10 -06002849 + (shdr->sh_entsize & ~INIT_OFFSET_MASK);
Linus Torvalds65b8a9b2010-08-05 12:59:02 -06002850 else
Rusty Russell49668682010-08-05 12:59:10 -06002851 dest = mod->module_core + shdr->sh_entsize;
Linus Torvalds65b8a9b2010-08-05 12:59:02 -06002852
Rusty Russell49668682010-08-05 12:59:10 -06002853 if (shdr->sh_type != SHT_NOBITS)
2854 memcpy(dest, (void *)shdr->sh_addr, shdr->sh_size);
Linus Torvalds65b8a9b2010-08-05 12:59:02 -06002855 /* Update sh_addr to point to copy in image. */
Rusty Russell49668682010-08-05 12:59:10 -06002856 shdr->sh_addr = (unsigned long)dest;
Jim Cromie5e124162011-12-06 12:11:31 -07002857 pr_debug("\t0x%lx %s\n",
2858 (long)shdr->sh_addr, info->secstrings + shdr->sh_name);
Linus Torvalds65b8a9b2010-08-05 12:59:02 -06002859 }
Rusty Russelld9131882010-08-05 12:59:08 -06002860
2861 return 0;
Linus Torvalds65b8a9b2010-08-05 12:59:02 -06002862}
2863
Rusty Russell49668682010-08-05 12:59:10 -06002864static int check_module_license_and_versions(struct module *mod)
Rusty Russell22e268e2010-08-05 12:59:05 -06002865{
2866 /*
2867 * ndiswrapper is under GPL by itself, but loads proprietary modules.
2868 * Don't use add_taint_module(), as it would prevent ndiswrapper from
2869 * using GPL-only symbols it needs.
2870 */
2871 if (strcmp(mod->name, "ndiswrapper") == 0)
2872 add_taint(TAINT_PROPRIETARY_MODULE);
2873
2874 /* driverloader was caught wrongly pretending to be under GPL */
2875 if (strcmp(mod->name, "driverloader") == 0)
2876 add_taint_module(mod, TAINT_PROPRIETARY_MODULE);
2877
Matthew Garrettc99af372012-06-22 13:49:31 -04002878 /* lve claims to be GPL but upstream won't provide source */
2879 if (strcmp(mod->name, "lve") == 0)
2880 add_taint_module(mod, TAINT_PROPRIETARY_MODULE);
2881
Rusty Russell22e268e2010-08-05 12:59:05 -06002882#ifdef CONFIG_MODVERSIONS
2883 if ((mod->num_syms && !mod->crcs)
2884 || (mod->num_gpl_syms && !mod->gpl_crcs)
2885 || (mod->num_gpl_future_syms && !mod->gpl_future_crcs)
2886#ifdef CONFIG_UNUSED_SYMBOLS
2887 || (mod->num_unused_syms && !mod->unused_crcs)
2888 || (mod->num_unused_gpl_syms && !mod->unused_gpl_crcs)
2889#endif
2890 ) {
2891 return try_to_force_load(mod,
2892 "no versions for exported symbols");
2893 }
2894#endif
2895 return 0;
2896}
2897
2898static void flush_module_icache(const struct module *mod)
2899{
2900 mm_segment_t old_fs;
2901
2902 /* flush the icache in correct context */
2903 old_fs = get_fs();
2904 set_fs(KERNEL_DS);
2905
2906 /*
2907 * Flush the instruction cache, since we've played with text.
2908 * Do it before processing of module parameters, so the module
2909 * can provide parameter accessor functions of its own.
2910 */
2911 if (mod->module_init)
2912 flush_icache_range((unsigned long)mod->module_init,
2913 (unsigned long)mod->module_init
2914 + mod->init_size);
2915 flush_icache_range((unsigned long)mod->module_core,
2916 (unsigned long)mod->module_core + mod->core_size);
2917
2918 set_fs(old_fs);
2919}
2920
Jonas Bonn74e08fc2011-06-30 21:22:11 +02002921int __weak module_frob_arch_sections(Elf_Ehdr *hdr,
2922 Elf_Shdr *sechdrs,
2923 char *secstrings,
2924 struct module *mod)
2925{
2926 return 0;
2927}
2928
Rusty Russell2f3238a2012-10-22 18:09:41 +10302929static struct module *layout_and_allocate(struct load_info *info, int flags)
Rusty Russelld9131882010-08-05 12:59:08 -06002930{
2931 /* Module within temporary copy. */
2932 struct module *mod;
Rusty Russell49668682010-08-05 12:59:10 -06002933 Elf_Shdr *pcpusec;
Rusty Russelld9131882010-08-05 12:59:08 -06002934 int err;
2935
Rusty Russell2f3238a2012-10-22 18:09:41 +10302936 mod = setup_load_info(info, flags);
Rusty Russelld9131882010-08-05 12:59:08 -06002937 if (IS_ERR(mod))
2938 return mod;
2939
Rusty Russell2f3238a2012-10-22 18:09:41 +10302940 err = check_modinfo(mod, info, flags);
Rusty Russelld9131882010-08-05 12:59:08 -06002941 if (err)
2942 return ERR_PTR(err);
2943
2944 /* Allow arches to frob section contents and sizes. */
Rusty Russell49668682010-08-05 12:59:10 -06002945 err = module_frob_arch_sections(info->hdr, info->sechdrs,
2946 info->secstrings, mod);
Rusty Russelld9131882010-08-05 12:59:08 -06002947 if (err < 0)
Rusty Russell6526c532010-08-05 12:59:10 -06002948 goto out;
Rusty Russelld9131882010-08-05 12:59:08 -06002949
Rusty Russell49668682010-08-05 12:59:10 -06002950 pcpusec = &info->sechdrs[info->index.pcpu];
2951 if (pcpusec->sh_size) {
Rusty Russelld9131882010-08-05 12:59:08 -06002952 /* We have a special allocation for this section. */
Rusty Russell49668682010-08-05 12:59:10 -06002953 err = percpu_modalloc(mod,
2954 pcpusec->sh_size, pcpusec->sh_addralign);
Rusty Russelld9131882010-08-05 12:59:08 -06002955 if (err)
Rusty Russell6526c532010-08-05 12:59:10 -06002956 goto out;
Rusty Russell49668682010-08-05 12:59:10 -06002957 pcpusec->sh_flags &= ~(unsigned long)SHF_ALLOC;
Rusty Russelld9131882010-08-05 12:59:08 -06002958 }
2959
2960 /* Determine total sizes, and put offsets in sh_entsize. For now
2961 this is done generically; there doesn't appear to be any
2962 special cases for the architectures. */
Rusty Russell49668682010-08-05 12:59:10 -06002963 layout_sections(mod, info);
Rusty Russell49668682010-08-05 12:59:10 -06002964 layout_symtab(mod, info);
Rusty Russelld9131882010-08-05 12:59:08 -06002965
2966 /* Allocate and move to the final place */
Rusty Russell49668682010-08-05 12:59:10 -06002967 err = move_module(mod, info);
Rusty Russelld9131882010-08-05 12:59:08 -06002968 if (err)
Kevin Cernekee48fd1182012-01-13 09:32:14 +10302969 goto free_percpu;
Rusty Russelld9131882010-08-05 12:59:08 -06002970
2971 /* Module has been copied to its final place now: return it. */
2972 mod = (void *)info->sechdrs[info->index.mod].sh_addr;
Rusty Russell49668682010-08-05 12:59:10 -06002973 kmemleak_load_module(mod, info);
Rusty Russelld9131882010-08-05 12:59:08 -06002974 return mod;
2975
Rusty Russelld9131882010-08-05 12:59:08 -06002976free_percpu:
2977 percpu_modfree(mod);
Rusty Russell6526c532010-08-05 12:59:10 -06002978out:
Rusty Russelld9131882010-08-05 12:59:08 -06002979 return ERR_PTR(err);
2980}
2981
2982/* mod is no longer valid after this! */
2983static void module_deallocate(struct module *mod, struct load_info *info)
2984{
Rusty Russelld9131882010-08-05 12:59:08 -06002985 percpu_modfree(mod);
2986 module_free(mod, mod->module_init);
2987 module_free(mod, mod->module_core);
2988}
2989
Jonas Bonn74e08fc2011-06-30 21:22:11 +02002990int __weak module_finalize(const Elf_Ehdr *hdr,
2991 const Elf_Shdr *sechdrs,
2992 struct module *me)
2993{
2994 return 0;
2995}
2996
Rusty Russell811d66a2010-08-05 12:59:12 -06002997static int post_relocation(struct module *mod, const struct load_info *info)
2998{
Rusty Russell51f3d0f2010-08-05 12:59:13 -06002999 /* Sort exception table now relocations are done. */
Rusty Russell811d66a2010-08-05 12:59:12 -06003000 sort_extable(mod->extable, mod->extable + mod->num_exentries);
3001
3002 /* Copy relocated percpu area over. */
3003 percpu_modcopy(mod, (void *)info->sechdrs[info->index.pcpu].sh_addr,
3004 info->sechdrs[info->index.pcpu].sh_size);
3005
Rusty Russell51f3d0f2010-08-05 12:59:13 -06003006 /* Setup kallsyms-specific fields. */
Rusty Russell811d66a2010-08-05 12:59:12 -06003007 add_kallsyms(mod, info);
3008
3009 /* Arch-specific module finalizing. */
3010 return module_finalize(info->hdr, info->sechdrs, mod);
3011}
3012
Rusty Russell9bb9c3b2012-09-28 14:31:03 +09303013/* Is this module of this name done loading? No locks held. */
3014static bool finished_loading(const char *name)
3015{
3016 struct module *mod;
3017 bool ret;
3018
3019 mutex_lock(&module_mutex);
3020 mod = find_module(name);
Rusty Russell0d21b0e2013-01-12 11:38:44 +10303021 ret = !mod || mod->state == MODULE_STATE_LIVE
3022 || mod->state == MODULE_STATE_GOING;
Rusty Russell9bb9c3b2012-09-28 14:31:03 +09303023 mutex_unlock(&module_mutex);
3024
3025 return ret;
3026}
3027
Peter Oberparleiterb99b87f2009-06-17 16:28:03 -07003028/* Call module constructors. */
3029static void do_mod_ctors(struct module *mod)
3030{
3031#ifdef CONFIG_CONSTRUCTORS
3032 unsigned long i;
3033
3034 for (i = 0; i < mod->num_ctors; i++)
3035 mod->ctors[i]();
3036#endif
3037}
3038
Linus Torvalds1da177e2005-04-16 15:20:36 -07003039/* This is where the real work happens */
Kees Cook34e11692012-10-16 07:31:07 +10303040static int do_init_module(struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003041{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003042 int ret = 0;
3043
Alan Sterne041c682006-03-27 01:16:30 -08003044 blocking_notifier_call_chain(&module_notify_list,
3045 MODULE_STATE_COMING, mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003046
Steven Rostedt94462ad2010-11-29 13:15:42 -05003047 /* Set RO and NX regions for core */
3048 set_section_ro_nx(mod->module_core,
3049 mod->core_text_size,
3050 mod->core_ro_size,
3051 mod->core_size);
3052
3053 /* Set RO and NX regions for init */
3054 set_section_ro_nx(mod->module_init,
3055 mod->init_text_size,
3056 mod->init_ro_size,
3057 mod->init_size);
3058
Peter Oberparleiterb99b87f2009-06-17 16:28:03 -07003059 do_mod_ctors(mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003060 /* Start the module */
3061 if (mod->init != NULL)
Arjan van de Ven59f94152008-07-30 12:49:02 -07003062 ret = do_one_initcall(mod->init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003063 if (ret < 0) {
3064 /* Init routine failed: abort. Try to protect us from
3065 buggy refcounters. */
3066 mod->state = MODULE_STATE_GOING;
Paul E. McKenneyfbd568a3e2005-05-01 08:59:04 -07003067 synchronize_sched();
Rusty Russellaf49d922007-10-16 23:26:27 -07003068 module_put(mod);
Peter Oberparleiterdf4b5652008-04-21 14:34:31 +02003069 blocking_notifier_call_chain(&module_notify_list,
3070 MODULE_STATE_GOING, mod);
Rusty Russellaf49d922007-10-16 23:26:27 -07003071 free_module(mod);
Rusty Russell6f139092012-09-28 14:31:03 +09303072 wake_up_all(&module_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003073 return ret;
3074 }
Alexey Dobriyane24e2e62008-03-10 11:43:53 -07003075 if (ret > 0) {
Joe Perchesad361c92009-07-06 13:05:40 -07003076 printk(KERN_WARNING
3077"%s: '%s'->init suspiciously returned %d, it should follow 0/-E convention\n"
3078"%s: loading module anyway...\n",
Alexey Dobriyane24e2e62008-03-10 11:43:53 -07003079 __func__, mod->name, ret,
3080 __func__);
3081 dump_stack();
3082 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003083
Rusty Russell6f139092012-09-28 14:31:03 +09303084 /* Now it's a first class citizen! */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003085 mod->state = MODULE_STATE_LIVE;
Masami Hiramatsu0deddf432009-01-06 14:41:54 -08003086 blocking_notifier_call_chain(&module_notify_list,
3087 MODULE_STATE_LIVE, mod);
Rusty Russell6c5db222008-03-10 11:43:52 -07003088
Linus Torvaldsd6de2c82009-04-10 12:17:41 -07003089 /* We need to finish all async code before the module init sequence is done */
3090 async_synchronize_full();
3091
Rusty Russell6c5db222008-03-10 11:43:52 -07003092 mutex_lock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003093 /* Drop initial reference. */
3094 module_put(mod);
Rusty Russellad6561d2009-06-12 21:47:03 -06003095 trim_init_extable(mod);
Jan Beulich4a496222009-07-06 14:50:42 +01003096#ifdef CONFIG_KALLSYMS
3097 mod->num_symtab = mod->core_num_syms;
3098 mod->symtab = mod->core_symtab;
Jan Beulich554bdfe2009-07-06 14:51:44 +01003099 mod->strtab = mod->core_strtab;
Jan Beulich4a496222009-07-06 14:50:42 +01003100#endif
Jan Glauber01526ed2011-05-19 16:55:26 -06003101 unset_module_init_ro_nx(mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003102 module_free(mod, mod->module_init);
3103 mod->module_init = NULL;
3104 mod->init_size = 0;
Jan Glauber4d103802011-05-19 16:55:25 -06003105 mod->init_ro_size = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003106 mod->init_text_size = 0;
Ashutosh Naik6389a382006-03-23 03:00:46 -08003107 mutex_unlock(&module_mutex);
Rusty Russell6f139092012-09-28 14:31:03 +09303108 wake_up_all(&module_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003109
3110 return 0;
3111}
3112
Kees Cook34e11692012-10-16 07:31:07 +10303113static int may_init_module(void)
3114{
3115 if (!capable(CAP_SYS_MODULE) || modules_disabled)
3116 return -EPERM;
3117
3118 return 0;
3119}
3120
3121/* Allocate and load the module: note that size of section 0 is always
3122 zero, and we rely on this for optional sections. */
Rusty Russell2f3238a2012-10-22 18:09:41 +10303123static int load_module(struct load_info *info, const char __user *uargs,
3124 int flags)
Kees Cook34e11692012-10-16 07:31:07 +10303125{
3126 struct module *mod, *old;
3127 long err;
3128
3129 err = module_sig_check(info);
3130 if (err)
3131 goto free_copy;
3132
3133 err = elf_header_check(info);
3134 if (err)
3135 goto free_copy;
3136
3137 /* Figure out module layout, and allocate all the memory. */
Rusty Russell2f3238a2012-10-22 18:09:41 +10303138 mod = layout_and_allocate(info, flags);
Kees Cook34e11692012-10-16 07:31:07 +10303139 if (IS_ERR(mod)) {
3140 err = PTR_ERR(mod);
3141 goto free_copy;
3142 }
3143
3144#ifdef CONFIG_MODULE_SIG
3145 mod->sig_ok = info->sig_ok;
3146 if (!mod->sig_ok)
3147 add_taint_module(mod, TAINT_FORCED_MODULE);
3148#endif
3149
3150 /* Now module is in final location, initialize linked lists, etc. */
3151 err = module_unload_init(mod);
3152 if (err)
3153 goto free_module;
3154
3155 /* Now we've got everything in the final locations, we can
3156 * find optional sections. */
3157 find_module_sections(mod, info);
3158
3159 err = check_module_license_and_versions(mod);
3160 if (err)
3161 goto free_unload;
3162
3163 /* Set up MODINFO_ATTR fields */
3164 setup_modinfo(mod, info);
3165
3166 /* Fix up syms, so that st_value is a pointer to location. */
3167 err = simplify_symbols(mod, info);
3168 if (err < 0)
3169 goto free_modinfo;
3170
3171 err = apply_relocations(mod, info);
3172 if (err < 0)
3173 goto free_modinfo;
3174
3175 err = post_relocation(mod, info);
3176 if (err < 0)
3177 goto free_modinfo;
3178
3179 flush_module_icache(mod);
3180
3181 /* Now copy in args */
3182 mod->args = strndup_user(uargs, ~0UL >> 1);
3183 if (IS_ERR(mod->args)) {
3184 err = PTR_ERR(mod->args);
3185 goto free_arch_cleanup;
3186 }
3187
3188 /* Mark state as coming so strong_try_module_get() ignores us. */
3189 mod->state = MODULE_STATE_COMING;
3190
3191 /* Now sew it into the lists so we can get lockdep and oops
3192 * info during argument parsing. No one should access us, since
3193 * strong_try_module_get() will fail.
3194 * lockdep/oops can run asynchronous, so use the RCU list insertion
3195 * function to insert in a way safe to concurrent readers.
3196 * The mutex protects against concurrent writers.
3197 */
3198again:
3199 mutex_lock(&module_mutex);
3200 if ((old = find_module(mod->name)) != NULL) {
3201 if (old->state == MODULE_STATE_COMING) {
3202 /* Wait in case it fails to load. */
3203 mutex_unlock(&module_mutex);
3204 err = wait_event_interruptible(module_wq,
3205 finished_loading(mod->name));
3206 if (err)
3207 goto free_arch_cleanup;
3208 goto again;
3209 }
3210 err = -EEXIST;
3211 goto unlock;
3212 }
3213
3214 /* This has to be done once we're sure module name is unique. */
3215 dynamic_debug_setup(info->debug, info->num_debug);
3216
3217 /* Find duplicate symbols */
3218 err = verify_export_symbols(mod);
3219 if (err < 0)
3220 goto ddebug;
3221
3222 module_bug_finalize(info->hdr, info->sechdrs, mod);
3223 list_add_rcu(&mod->list, &modules);
3224 mutex_unlock(&module_mutex);
3225
3226 /* Module is ready to execute: parsing args may do that. */
3227 err = parse_args(mod->name, mod->args, mod->kp, mod->num_kp,
3228 -32768, 32767, &ddebug_dyndbg_module_param_cb);
3229 if (err < 0)
3230 goto unlink;
3231
3232 /* Link in to syfs. */
3233 err = mod_sysfs_setup(mod, info, mod->kp, mod->num_kp);
3234 if (err < 0)
3235 goto unlink;
3236
3237 /* Get rid of temporary copy. */
3238 free_copy(info);
3239
3240 /* Done! */
3241 trace_module_load(mod);
3242
3243 return do_init_module(mod);
3244
3245 unlink:
3246 mutex_lock(&module_mutex);
3247 /* Unlink carefully: kallsyms could be walking list. */
3248 list_del_rcu(&mod->list);
3249 module_bug_cleanup(mod);
3250 wake_up_all(&module_wq);
3251 ddebug:
3252 dynamic_debug_remove(info->debug);
3253 unlock:
3254 mutex_unlock(&module_mutex);
3255 synchronize_sched();
3256 kfree(mod->args);
3257 free_arch_cleanup:
3258 module_arch_cleanup(mod);
3259 free_modinfo:
3260 free_modinfo(mod);
3261 free_unload:
3262 module_unload_free(mod);
3263 free_module:
3264 module_deallocate(mod, info);
3265 free_copy:
3266 free_copy(info);
3267 return err;
3268}
3269
3270SYSCALL_DEFINE3(init_module, void __user *, umod,
3271 unsigned long, len, const char __user *, uargs)
3272{
3273 int err;
3274 struct load_info info = { };
3275
3276 err = may_init_module();
3277 if (err)
3278 return err;
3279
3280 pr_debug("init_module: umod=%p, len=%lu, uargs=%p\n",
3281 umod, len, uargs);
3282
3283 err = copy_module_from_user(umod, len, &info);
3284 if (err)
3285 return err;
3286
Rusty Russell2f3238a2012-10-22 18:09:41 +10303287 return load_module(&info, uargs, 0);
Kees Cook34e11692012-10-16 07:31:07 +10303288}
3289
Rusty Russell2f3238a2012-10-22 18:09:41 +10303290SYSCALL_DEFINE3(finit_module, int, fd, const char __user *, uargs, int, flags)
Kees Cook34e11692012-10-16 07:31:07 +10303291{
3292 int err;
3293 struct load_info info = { };
3294
3295 err = may_init_module();
3296 if (err)
3297 return err;
3298
Rusty Russell2f3238a2012-10-22 18:09:41 +10303299 pr_debug("finit_module: fd=%d, uargs=%p, flags=%i\n", fd, uargs, flags);
3300
3301 if (flags & ~(MODULE_INIT_IGNORE_MODVERSIONS
3302 |MODULE_INIT_IGNORE_VERMAGIC))
3303 return -EINVAL;
Kees Cook34e11692012-10-16 07:31:07 +10303304
3305 err = copy_module_from_fd(fd, &info);
3306 if (err)
3307 return err;
3308
Rusty Russell2f3238a2012-10-22 18:09:41 +10303309 return load_module(&info, uargs, flags);
Kees Cook34e11692012-10-16 07:31:07 +10303310}
3311
Linus Torvalds1da177e2005-04-16 15:20:36 -07003312static inline int within(unsigned long addr, void *start, unsigned long size)
3313{
3314 return ((void *)addr >= start && (void *)addr < start + size);
3315}
3316
3317#ifdef CONFIG_KALLSYMS
3318/*
3319 * This ignores the intensely annoying "mapping symbols" found
3320 * in ARM ELF files: $a, $t and $d.
3321 */
3322static inline int is_arm_mapping_symbol(const char *str)
3323{
Daniel Walker22a8bde2007-10-18 03:06:07 -07003324 return str[0] == '$' && strchr("atd", str[1])
Linus Torvalds1da177e2005-04-16 15:20:36 -07003325 && (str[2] == '\0' || str[2] == '.');
3326}
3327
3328static const char *get_ksymbol(struct module *mod,
3329 unsigned long addr,
3330 unsigned long *size,
3331 unsigned long *offset)
3332{
3333 unsigned int i, best = 0;
3334 unsigned long nextval;
3335
3336 /* At worse, next value is at end of module */
Masami Hiramatsua06f6212009-01-06 14:41:49 -08003337 if (within_module_init(addr, mod))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003338 nextval = (unsigned long)mod->module_init+mod->init_text_size;
Daniel Walker22a8bde2007-10-18 03:06:07 -07003339 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003340 nextval = (unsigned long)mod->module_core+mod->core_text_size;
3341
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003342 /* Scan for closest preceding symbol, and next symbol. (ELF
Daniel Walker22a8bde2007-10-18 03:06:07 -07003343 starts real symbols at 1). */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003344 for (i = 1; i < mod->num_symtab; i++) {
3345 if (mod->symtab[i].st_shndx == SHN_UNDEF)
3346 continue;
3347
3348 /* We ignore unnamed symbols: they're uninformative
3349 * and inserted at a whim. */
3350 if (mod->symtab[i].st_value <= addr
3351 && mod->symtab[i].st_value > mod->symtab[best].st_value
3352 && *(mod->strtab + mod->symtab[i].st_name) != '\0'
3353 && !is_arm_mapping_symbol(mod->strtab + mod->symtab[i].st_name))
3354 best = i;
3355 if (mod->symtab[i].st_value > addr
3356 && mod->symtab[i].st_value < nextval
3357 && *(mod->strtab + mod->symtab[i].st_name) != '\0'
3358 && !is_arm_mapping_symbol(mod->strtab + mod->symtab[i].st_name))
3359 nextval = mod->symtab[i].st_value;
3360 }
3361
3362 if (!best)
3363 return NULL;
3364
Alexey Dobriyanffb45122007-05-08 00:28:41 -07003365 if (size)
3366 *size = nextval - mod->symtab[best].st_value;
3367 if (offset)
3368 *offset = addr - mod->symtab[best].st_value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003369 return mod->strtab + mod->symtab[best].st_name;
3370}
3371
Rusty Russell6dd06c92008-01-29 17:13:22 -05003372/* For kallsyms to ask for address resolution. NULL means not found. Careful
3373 * not to lock to avoid deadlock on oopses, simply disable preemption. */
Andrew Morton92dfc9d2008-02-08 04:18:43 -08003374const char *module_address_lookup(unsigned long addr,
Rusty Russell6dd06c92008-01-29 17:13:22 -05003375 unsigned long *size,
3376 unsigned long *offset,
3377 char **modname,
3378 char *namebuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003379{
3380 struct module *mod;
Rusty Russellcb2a5202008-01-14 00:55:03 -08003381 const char *ret = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003382
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) {
Rusty Russell0d21b0e2013-01-12 11:38:44 +10303385 if (mod->state == MODULE_STATE_UNFORMED)
3386 continue;
Masami Hiramatsua06f6212009-01-06 14:41:49 -08003387 if (within_module_init(addr, mod) ||
3388 within_module_core(addr, mod)) {
Franck Bui-Huuffc50892006-10-03 01:13:48 -07003389 if (modname)
3390 *modname = mod->name;
Rusty Russellcb2a5202008-01-14 00:55:03 -08003391 ret = get_ksymbol(mod, addr, size, offset);
3392 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003393 }
3394 }
Rusty Russell6dd06c92008-01-29 17:13:22 -05003395 /* Make a copy in here where it's safe */
3396 if (ret) {
3397 strncpy(namebuf, ret, KSYM_NAME_LEN - 1);
3398 ret = namebuf;
3399 }
Rusty Russellcb2a5202008-01-14 00:55:03 -08003400 preempt_enable();
Andrew Morton92dfc9d2008-02-08 04:18:43 -08003401 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003402}
3403
Alexey Dobriyan9d65cb42007-05-08 00:28:43 -07003404int lookup_module_symbol_name(unsigned long addr, char *symname)
3405{
3406 struct module *mod;
3407
Rusty Russellcb2a5202008-01-14 00:55:03 -08003408 preempt_disable();
Andi Kleend72b3752008-08-30 10:09:00 +02003409 list_for_each_entry_rcu(mod, &modules, list) {
Rusty Russell0d21b0e2013-01-12 11:38:44 +10303410 if (mod->state == MODULE_STATE_UNFORMED)
3411 continue;
Masami Hiramatsua06f6212009-01-06 14:41:49 -08003412 if (within_module_init(addr, mod) ||
3413 within_module_core(addr, mod)) {
Alexey Dobriyan9d65cb42007-05-08 00:28:43 -07003414 const char *sym;
3415
3416 sym = get_ksymbol(mod, addr, NULL, NULL);
3417 if (!sym)
3418 goto out;
Tejun Heo9281ace2007-07-17 04:03:51 -07003419 strlcpy(symname, sym, KSYM_NAME_LEN);
Rusty Russellcb2a5202008-01-14 00:55:03 -08003420 preempt_enable();
Alexey Dobriyan9d65cb42007-05-08 00:28:43 -07003421 return 0;
3422 }
3423 }
3424out:
Rusty Russellcb2a5202008-01-14 00:55:03 -08003425 preempt_enable();
Alexey Dobriyan9d65cb42007-05-08 00:28:43 -07003426 return -ERANGE;
3427}
3428
Alexey Dobriyana5c43da2007-05-08 00:28:47 -07003429int lookup_module_symbol_attrs(unsigned long addr, unsigned long *size,
3430 unsigned long *offset, char *modname, char *name)
3431{
3432 struct module *mod;
3433
Rusty Russellcb2a5202008-01-14 00:55:03 -08003434 preempt_disable();
Andi Kleend72b3752008-08-30 10:09:00 +02003435 list_for_each_entry_rcu(mod, &modules, list) {
Rusty Russell0d21b0e2013-01-12 11:38:44 +10303436 if (mod->state == MODULE_STATE_UNFORMED)
3437 continue;
Masami Hiramatsua06f6212009-01-06 14:41:49 -08003438 if (within_module_init(addr, mod) ||
3439 within_module_core(addr, mod)) {
Alexey Dobriyana5c43da2007-05-08 00:28:47 -07003440 const char *sym;
3441
3442 sym = get_ksymbol(mod, addr, size, offset);
3443 if (!sym)
3444 goto out;
3445 if (modname)
Tejun Heo9281ace2007-07-17 04:03:51 -07003446 strlcpy(modname, mod->name, MODULE_NAME_LEN);
Alexey Dobriyana5c43da2007-05-08 00:28:47 -07003447 if (name)
Tejun Heo9281ace2007-07-17 04:03:51 -07003448 strlcpy(name, sym, KSYM_NAME_LEN);
Rusty Russellcb2a5202008-01-14 00:55:03 -08003449 preempt_enable();
Alexey Dobriyana5c43da2007-05-08 00:28:47 -07003450 return 0;
3451 }
3452 }
3453out:
Rusty Russellcb2a5202008-01-14 00:55:03 -08003454 preempt_enable();
Alexey Dobriyana5c43da2007-05-08 00:28:47 -07003455 return -ERANGE;
3456}
3457
Alexey Dobriyanea078902007-05-08 00:28:39 -07003458int module_get_kallsym(unsigned int symnum, unsigned long *value, char *type,
3459 char *name, char *module_name, int *exported)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003460{
3461 struct module *mod;
3462
Rusty Russellcb2a5202008-01-14 00:55:03 -08003463 preempt_disable();
Andi Kleend72b3752008-08-30 10:09:00 +02003464 list_for_each_entry_rcu(mod, &modules, list) {
Rusty Russell0d21b0e2013-01-12 11:38:44 +10303465 if (mod->state == MODULE_STATE_UNFORMED)
3466 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003467 if (symnum < mod->num_symtab) {
3468 *value = mod->symtab[symnum].st_value;
3469 *type = mod->symtab[symnum].st_info;
Andreas Gruenbacher098c5ee2006-07-14 00:24:04 -07003470 strlcpy(name, mod->strtab + mod->symtab[symnum].st_name,
Tejun Heo9281ace2007-07-17 04:03:51 -07003471 KSYM_NAME_LEN);
3472 strlcpy(module_name, mod->name, MODULE_NAME_LEN);
Tim Abbottca4787b2009-01-05 08:40:10 -06003473 *exported = is_exported(name, *value, mod);
Rusty Russellcb2a5202008-01-14 00:55:03 -08003474 preempt_enable();
Alexey Dobriyanea078902007-05-08 00:28:39 -07003475 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003476 }
3477 symnum -= mod->num_symtab;
3478 }
Rusty Russellcb2a5202008-01-14 00:55:03 -08003479 preempt_enable();
Alexey Dobriyanea078902007-05-08 00:28:39 -07003480 return -ERANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003481}
3482
3483static unsigned long mod_find_symname(struct module *mod, const char *name)
3484{
3485 unsigned int i;
3486
3487 for (i = 0; i < mod->num_symtab; i++)
Keith Owens54e8ce42006-02-03 03:03:53 -08003488 if (strcmp(name, mod->strtab+mod->symtab[i].st_name) == 0 &&
3489 mod->symtab[i].st_info != 'U')
Linus Torvalds1da177e2005-04-16 15:20:36 -07003490 return mod->symtab[i].st_value;
3491 return 0;
3492}
3493
3494/* Look for this name: can be of form module:name. */
3495unsigned long module_kallsyms_lookup_name(const char *name)
3496{
3497 struct module *mod;
3498 char *colon;
3499 unsigned long ret = 0;
3500
3501 /* Don't lock: we're in enough trouble already. */
Rusty Russellcb2a5202008-01-14 00:55:03 -08003502 preempt_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003503 if ((colon = strchr(name, ':')) != NULL) {
3504 *colon = '\0';
3505 if ((mod = find_module(name)) != NULL)
3506 ret = mod_find_symname(mod, colon+1);
3507 *colon = ':';
3508 } else {
Rusty Russell0d21b0e2013-01-12 11:38:44 +10303509 list_for_each_entry_rcu(mod, &modules, list) {
3510 if (mod->state == MODULE_STATE_UNFORMED)
3511 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003512 if ((ret = mod_find_symname(mod, name)) != 0)
3513 break;
Rusty Russell0d21b0e2013-01-12 11:38:44 +10303514 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003515 }
Rusty Russellcb2a5202008-01-14 00:55:03 -08003516 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003517 return ret;
3518}
Anders Kaseorg75a66612008-12-05 19:03:58 -05003519
3520int module_kallsyms_on_each_symbol(int (*fn)(void *, const char *,
3521 struct module *, unsigned long),
3522 void *data)
3523{
3524 struct module *mod;
3525 unsigned int i;
3526 int ret;
3527
3528 list_for_each_entry(mod, &modules, list) {
Rusty Russell0d21b0e2013-01-12 11:38:44 +10303529 if (mod->state == MODULE_STATE_UNFORMED)
3530 continue;
Anders Kaseorg75a66612008-12-05 19:03:58 -05003531 for (i = 0; i < mod->num_symtab; i++) {
3532 ret = fn(data, mod->strtab + mod->symtab[i].st_name,
3533 mod, mod->symtab[i].st_value);
3534 if (ret != 0)
3535 return ret;
3536 }
3537 }
3538 return 0;
3539}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003540#endif /* CONFIG_KALLSYMS */
3541
Arjan van de Ven21aa9282008-01-25 21:08:33 +01003542static char *module_flags(struct module *mod, char *buf)
Florin Malitafa3ba2e82006-10-11 01:21:48 -07003543{
3544 int bx = 0;
3545
Rusty Russell0d21b0e2013-01-12 11:38:44 +10303546 BUG_ON(mod->state == MODULE_STATE_UNFORMED);
Arjan van de Ven21aa9282008-01-25 21:08:33 +01003547 if (mod->taints ||
3548 mod->state == MODULE_STATE_GOING ||
3549 mod->state == MODULE_STATE_COMING) {
Florin Malitafa3ba2e82006-10-11 01:21:48 -07003550 buf[bx++] = '(';
Kay Sieverscca3e702012-01-13 09:32:15 +10303551 bx += module_flags_taint(mod, buf + bx);
Arjan van de Ven21aa9282008-01-25 21:08:33 +01003552 /* Show a - for module-is-being-unloaded */
3553 if (mod->state == MODULE_STATE_GOING)
3554 buf[bx++] = '-';
3555 /* Show a + for module-is-being-loaded */
3556 if (mod->state == MODULE_STATE_COMING)
3557 buf[bx++] = '+';
Florin Malitafa3ba2e82006-10-11 01:21:48 -07003558 buf[bx++] = ')';
3559 }
3560 buf[bx] = '\0';
3561
3562 return buf;
3563}
3564
Alexey Dobriyan3b5d5c62008-10-06 13:19:27 +04003565#ifdef CONFIG_PROC_FS
3566/* Called by the /proc file system to return a list of modules. */
3567static void *m_start(struct seq_file *m, loff_t *pos)
3568{
3569 mutex_lock(&module_mutex);
3570 return seq_list_start(&modules, *pos);
3571}
3572
3573static void *m_next(struct seq_file *m, void *p, loff_t *pos)
3574{
3575 return seq_list_next(p, &modules, pos);
3576}
3577
3578static void m_stop(struct seq_file *m, void *p)
3579{
3580 mutex_unlock(&module_mutex);
3581}
3582
Linus Torvalds1da177e2005-04-16 15:20:36 -07003583static int m_show(struct seq_file *m, void *p)
3584{
3585 struct module *mod = list_entry(p, struct module, list);
Florin Malitafa3ba2e82006-10-11 01:21:48 -07003586 char buf[8];
3587
Rusty Russell0d21b0e2013-01-12 11:38:44 +10303588 /* We always ignore unformed modules. */
3589 if (mod->state == MODULE_STATE_UNFORMED)
3590 return 0;
3591
Denys Vlasenko2f0f2a32008-07-22 19:24:27 -05003592 seq_printf(m, "%s %u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003593 mod->name, mod->init_size + mod->core_size);
3594 print_unload_info(m, mod);
3595
3596 /* Informative for users. */
3597 seq_printf(m, " %s",
3598 mod->state == MODULE_STATE_GOING ? "Unloading":
3599 mod->state == MODULE_STATE_COMING ? "Loading":
3600 "Live");
3601 /* Used by oprofile and other similar tools. */
Kees Cook9f36e2c2011-03-22 16:34:22 -07003602 seq_printf(m, " 0x%pK", mod->module_core);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003603
Florin Malitafa3ba2e82006-10-11 01:21:48 -07003604 /* Taints info */
3605 if (mod->taints)
Arjan van de Ven21aa9282008-01-25 21:08:33 +01003606 seq_printf(m, " %s", module_flags(mod, buf));
Florin Malitafa3ba2e82006-10-11 01:21:48 -07003607
Linus Torvalds1da177e2005-04-16 15:20:36 -07003608 seq_printf(m, "\n");
3609 return 0;
3610}
3611
3612/* Format: modulename size refcount deps address
3613
3614 Where refcount is a number or -, and deps is a comma-separated list
3615 of depends or -.
3616*/
Alexey Dobriyan3b5d5c62008-10-06 13:19:27 +04003617static const struct seq_operations modules_op = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003618 .start = m_start,
3619 .next = m_next,
3620 .stop = m_stop,
3621 .show = m_show
3622};
3623
Alexey Dobriyan3b5d5c62008-10-06 13:19:27 +04003624static int modules_open(struct inode *inode, struct file *file)
3625{
3626 return seq_open(file, &modules_op);
3627}
3628
3629static const struct file_operations proc_modules_operations = {
3630 .open = modules_open,
3631 .read = seq_read,
3632 .llseek = seq_lseek,
3633 .release = seq_release,
3634};
3635
3636static int __init proc_modules_init(void)
3637{
3638 proc_create("modules", 0, NULL, &proc_modules_operations);
3639 return 0;
3640}
3641module_init(proc_modules_init);
3642#endif
3643
Linus Torvalds1da177e2005-04-16 15:20:36 -07003644/* Given an address, look for it in the module exception tables. */
3645const struct exception_table_entry *search_module_extables(unsigned long addr)
3646{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003647 const struct exception_table_entry *e = NULL;
3648 struct module *mod;
3649
Rusty Russell24da1cb2007-07-15 23:41:46 -07003650 preempt_disable();
Andi Kleend72b3752008-08-30 10:09:00 +02003651 list_for_each_entry_rcu(mod, &modules, list) {
Rusty Russell0d21b0e2013-01-12 11:38:44 +10303652 if (mod->state == MODULE_STATE_UNFORMED)
3653 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003654 if (mod->num_exentries == 0)
3655 continue;
Daniel Walker22a8bde2007-10-18 03:06:07 -07003656
Linus Torvalds1da177e2005-04-16 15:20:36 -07003657 e = search_extable(mod->extable,
3658 mod->extable + mod->num_exentries - 1,
3659 addr);
3660 if (e)
3661 break;
3662 }
Rusty Russell24da1cb2007-07-15 23:41:46 -07003663 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003664
3665 /* Now, if we found one, we are running inside it now, hence
Daniel Walker22a8bde2007-10-18 03:06:07 -07003666 we cannot unload the module, hence no refcnt needed. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003667 return e;
3668}
3669
Ingo Molnar4d435f92006-07-03 00:24:24 -07003670/*
Rusty Russelle6104992009-03-31 13:05:31 -06003671 * is_module_address - is this address inside a module?
3672 * @addr: the address to check.
3673 *
3674 * See is_module_text_address() if you simply want to see if the address
3675 * is code (not data).
Ingo Molnar4d435f92006-07-03 00:24:24 -07003676 */
Rusty Russelle6104992009-03-31 13:05:31 -06003677bool is_module_address(unsigned long addr)
Ingo Molnar4d435f92006-07-03 00:24:24 -07003678{
Rusty Russelle6104992009-03-31 13:05:31 -06003679 bool ret;
Ingo Molnar4d435f92006-07-03 00:24:24 -07003680
Rusty Russell24da1cb2007-07-15 23:41:46 -07003681 preempt_disable();
Rusty Russelle6104992009-03-31 13:05:31 -06003682 ret = __module_address(addr) != NULL;
Rusty Russell24da1cb2007-07-15 23:41:46 -07003683 preempt_enable();
Ingo Molnar4d435f92006-07-03 00:24:24 -07003684
Rusty Russelle6104992009-03-31 13:05:31 -06003685 return ret;
Ingo Molnar4d435f92006-07-03 00:24:24 -07003686}
3687
Rusty Russelle6104992009-03-31 13:05:31 -06003688/*
3689 * __module_address - get the module which contains an address.
3690 * @addr: the address.
3691 *
3692 * Must be called with preempt disabled or module mutex held so that
3693 * module doesn't get freed during this.
3694 */
Linus Torvalds714f83d2009-04-05 11:04:19 -07003695struct module *__module_address(unsigned long addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003696{
3697 struct module *mod;
3698
Rusty Russell3a642e92008-07-22 19:24:28 -05003699 if (addr < module_addr_min || addr > module_addr_max)
3700 return NULL;
3701
Rusty Russell0d21b0e2013-01-12 11:38:44 +10303702 list_for_each_entry_rcu(mod, &modules, list) {
3703 if (mod->state == MODULE_STATE_UNFORMED)
3704 continue;
Rusty Russelle6104992009-03-31 13:05:31 -06003705 if (within_module_core(addr, mod)
3706 || within_module_init(addr, mod))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003707 return mod;
Rusty Russell0d21b0e2013-01-12 11:38:44 +10303708 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003709 return NULL;
3710}
Tim Abbottc6b37802008-12-05 19:03:59 -05003711EXPORT_SYMBOL_GPL(__module_address);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003712
Rusty Russelle6104992009-03-31 13:05:31 -06003713/*
3714 * is_module_text_address - is this address inside module code?
3715 * @addr: the address to check.
3716 *
3717 * See is_module_address() if you simply want to see if the address is
3718 * anywhere in a module. See kernel_text_address() for testing if an
3719 * address corresponds to kernel or module code.
3720 */
3721bool is_module_text_address(unsigned long addr)
3722{
3723 bool ret;
3724
3725 preempt_disable();
3726 ret = __module_text_address(addr) != NULL;
3727 preempt_enable();
3728
3729 return ret;
3730}
3731
3732/*
3733 * __module_text_address - get the module whose code contains an address.
3734 * @addr: the address.
3735 *
3736 * Must be called with preempt disabled or module mutex held so that
3737 * module doesn't get freed during this.
3738 */
3739struct module *__module_text_address(unsigned long addr)
3740{
3741 struct module *mod = __module_address(addr);
3742 if (mod) {
3743 /* Make sure it's within the text section. */
3744 if (!within(addr, mod->module_init, mod->init_text_size)
3745 && !within(addr, mod->module_core, mod->core_text_size))
3746 mod = NULL;
3747 }
3748 return mod;
3749}
Tim Abbottc6b37802008-12-05 19:03:59 -05003750EXPORT_SYMBOL_GPL(__module_text_address);
Rusty Russelle6104992009-03-31 13:05:31 -06003751
Linus Torvalds1da177e2005-04-16 15:20:36 -07003752/* Don't grab lock, we're oopsing. */
3753void print_modules(void)
3754{
3755 struct module *mod;
Randy Dunlap2bc2d612006-10-02 02:17:02 -07003756 char buf[8];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003757
Linus Torvaldsb2311252009-06-16 11:07:14 -07003758 printk(KERN_DEFAULT "Modules linked in:");
Andi Kleend72b3752008-08-30 10:09:00 +02003759 /* Most callers should already have preempt disabled, but make sure */
3760 preempt_disable();
Rusty Russell0d21b0e2013-01-12 11:38:44 +10303761 list_for_each_entry_rcu(mod, &modules, list) {
3762 if (mod->state == MODULE_STATE_UNFORMED)
3763 continue;
Arjan van de Ven21aa9282008-01-25 21:08:33 +01003764 printk(" %s%s", mod->name, module_flags(mod, buf));
Rusty Russell0d21b0e2013-01-12 11:38:44 +10303765 }
Andi Kleend72b3752008-08-30 10:09:00 +02003766 preempt_enable();
Arjan van de Vene14af7e2008-01-25 21:08:33 +01003767 if (last_unloaded_module[0])
3768 printk(" [last unloaded: %s]", last_unloaded_module);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003769 printk("\n");
3770}
3771
Linus Torvalds1da177e2005-04-16 15:20:36 -07003772#ifdef CONFIG_MODVERSIONS
Rusty Russell8c8ef422009-03-31 13:05:34 -06003773/* Generate the signature for all relevant module structures here.
3774 * If these change, we don't want to try to parse the module. */
3775void module_layout(struct module *mod,
3776 struct modversion_info *ver,
3777 struct kernel_param *kp,
3778 struct kernel_symbol *ks,
Mathieu Desnoyers65498642011-01-26 17:26:22 -05003779 struct tracepoint * const *tp)
Rusty Russell8c8ef422009-03-31 13:05:34 -06003780{
3781}
3782EXPORT_SYMBOL(module_layout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003783#endif