blob: 4542ff3f7ef9668bf7e163eed50080f47953745d [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{
191 if (mod && mod->state == MODULE_STATE_COMING)
Rusty Russellc9a3ba52008-01-29 17:13:18 -0500192 return -EBUSY;
193 if (try_module_get(mod))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 return 0;
Rusty Russellc9a3ba52008-01-29 17:13:18 -0500195 else
196 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197}
198
Florin Malitafa3ba2e82006-10-11 01:21:48 -0700199static inline void add_taint_module(struct module *mod, unsigned flag)
200{
201 add_taint(flag);
Andi Kleen25ddbb12008-10-15 22:01:41 -0700202 mod->taints |= (1U << flag);
Florin Malitafa3ba2e82006-10-11 01:21:48 -0700203}
204
Robert P. J. Day02a3e592007-05-09 07:26:28 +0200205/*
206 * A thread that wants to hold a reference to a module only while it
207 * is running can call this to safely exit. nfsd and lockd use this.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 */
209void __module_put_and_exit(struct module *mod, long code)
210{
211 module_put(mod);
212 do_exit(code);
213}
214EXPORT_SYMBOL(__module_put_and_exit);
Daniel Walker22a8bde2007-10-18 03:06:07 -0700215
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216/* Find a module section: 0 means not found. */
Rusty Russell49668682010-08-05 12:59:10 -0600217static unsigned int find_sec(const struct load_info *info, const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218{
219 unsigned int i;
220
Rusty Russell49668682010-08-05 12:59:10 -0600221 for (i = 1; i < info->hdr->e_shnum; i++) {
222 Elf_Shdr *shdr = &info->sechdrs[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 /* Alloc bit cleared means "ignore it." */
Rusty Russell49668682010-08-05 12:59:10 -0600224 if ((shdr->sh_flags & SHF_ALLOC)
225 && strcmp(info->secstrings + shdr->sh_name, name) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 return i;
Rusty Russell49668682010-08-05 12:59:10 -0600227 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 return 0;
229}
230
Rusty Russell5e458cc2008-10-22 10:00:13 -0500231/* Find a module section, or NULL. */
Rusty Russell49668682010-08-05 12:59:10 -0600232static void *section_addr(const struct load_info *info, const char *name)
Rusty Russell5e458cc2008-10-22 10:00:13 -0500233{
234 /* Section 0 has sh_addr 0. */
Rusty Russell49668682010-08-05 12:59:10 -0600235 return (void *)info->sechdrs[find_sec(info, name)].sh_addr;
Rusty Russell5e458cc2008-10-22 10:00:13 -0500236}
237
238/* Find a module section, or NULL. Fill in number of "objects" in section. */
Rusty Russell49668682010-08-05 12:59:10 -0600239static void *section_objs(const struct load_info *info,
Rusty Russell5e458cc2008-10-22 10:00:13 -0500240 const char *name,
241 size_t object_size,
242 unsigned int *num)
243{
Rusty Russell49668682010-08-05 12:59:10 -0600244 unsigned int sec = find_sec(info, name);
Rusty Russell5e458cc2008-10-22 10:00:13 -0500245
246 /* Section 0 has sh_addr 0 and sh_size 0. */
Rusty Russell49668682010-08-05 12:59:10 -0600247 *num = info->sechdrs[sec].sh_size / object_size;
248 return (void *)info->sechdrs[sec].sh_addr;
Rusty Russell5e458cc2008-10-22 10:00:13 -0500249}
250
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251/* Provided by the linker */
252extern const struct kernel_symbol __start___ksymtab[];
253extern const struct kernel_symbol __stop___ksymtab[];
254extern const struct kernel_symbol __start___ksymtab_gpl[];
255extern const struct kernel_symbol __stop___ksymtab_gpl[];
Greg Kroah-Hartman9f28bb72006-03-20 13:17:13 -0800256extern const struct kernel_symbol __start___ksymtab_gpl_future[];
257extern const struct kernel_symbol __stop___ksymtab_gpl_future[];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258extern const unsigned long __start___kcrctab[];
259extern const unsigned long __start___kcrctab_gpl[];
Greg Kroah-Hartman9f28bb72006-03-20 13:17:13 -0800260extern const unsigned long __start___kcrctab_gpl_future[];
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500261#ifdef CONFIG_UNUSED_SYMBOLS
262extern const struct kernel_symbol __start___ksymtab_unused[];
263extern const struct kernel_symbol __stop___ksymtab_unused[];
264extern const struct kernel_symbol __start___ksymtab_unused_gpl[];
265extern const struct kernel_symbol __stop___ksymtab_unused_gpl[];
Arjan van de Venf71d20e2006-06-28 04:26:45 -0700266extern const unsigned long __start___kcrctab_unused[];
267extern const unsigned long __start___kcrctab_unused_gpl[];
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500268#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269
270#ifndef CONFIG_MODVERSIONS
271#define symversion(base, idx) NULL
272#else
Andrew Mortonf83ca9f2006-03-28 01:56:20 -0800273#define symversion(base, idx) ((base != NULL) ? ((base) + (idx)) : NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274#endif
275
Rusty Russelldafd0942008-07-22 19:24:25 -0500276static bool each_symbol_in_section(const struct symsearch *arr,
277 unsigned int arrsize,
278 struct module *owner,
279 bool (*fn)(const struct symsearch *syms,
280 struct module *owner,
Rusty Russellde4d8d52011-04-19 21:49:58 +0200281 void *data),
Rusty Russelldafd0942008-07-22 19:24:25 -0500282 void *data)
Sam Ravnborg3fd68052006-02-08 21:16:45 +0100283{
Rusty Russellde4d8d52011-04-19 21:49:58 +0200284 unsigned int j;
Rusty Russelldafd0942008-07-22 19:24:25 -0500285
286 for (j = 0; j < arrsize; j++) {
Rusty Russellde4d8d52011-04-19 21:49:58 +0200287 if (fn(&arr[j], owner, data))
288 return true;
Rusty Russelldafd0942008-07-22 19:24:25 -0500289 }
290
291 return false;
Sam Ravnborg3fd68052006-02-08 21:16:45 +0100292}
293
Rusty Russelldafd0942008-07-22 19:24:25 -0500294/* Returns true as soon as fn returns true, otherwise false. */
Rusty Russellde4d8d52011-04-19 21:49:58 +0200295bool each_symbol_section(bool (*fn)(const struct symsearch *arr,
296 struct module *owner,
297 void *data),
298 void *data)
Arjan van de Venf71d20e2006-06-28 04:26:45 -0700299{
Rusty Russelldafd0942008-07-22 19:24:25 -0500300 struct module *mod;
Linus Torvalds44032e62010-08-05 12:59:05 -0600301 static const struct symsearch arr[] = {
Rusty Russelldafd0942008-07-22 19:24:25 -0500302 { __start___ksymtab, __stop___ksymtab, __start___kcrctab,
303 NOT_GPL_ONLY, false },
304 { __start___ksymtab_gpl, __stop___ksymtab_gpl,
305 __start___kcrctab_gpl,
306 GPL_ONLY, false },
307 { __start___ksymtab_gpl_future, __stop___ksymtab_gpl_future,
308 __start___kcrctab_gpl_future,
309 WILL_BE_GPL_ONLY, false },
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500310#ifdef CONFIG_UNUSED_SYMBOLS
Rusty Russelldafd0942008-07-22 19:24:25 -0500311 { __start___ksymtab_unused, __stop___ksymtab_unused,
312 __start___kcrctab_unused,
313 NOT_GPL_ONLY, true },
314 { __start___ksymtab_unused_gpl, __stop___ksymtab_unused_gpl,
315 __start___kcrctab_unused_gpl,
316 GPL_ONLY, true },
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500317#endif
Rusty Russelldafd0942008-07-22 19:24:25 -0500318 };
319
320 if (each_symbol_in_section(arr, ARRAY_SIZE(arr), NULL, fn, data))
321 return true;
322
Andi Kleend72b3752008-08-30 10:09:00 +0200323 list_for_each_entry_rcu(mod, &modules, list) {
Rusty Russelldafd0942008-07-22 19:24:25 -0500324 struct symsearch arr[] = {
325 { mod->syms, mod->syms + mod->num_syms, mod->crcs,
326 NOT_GPL_ONLY, false },
327 { mod->gpl_syms, mod->gpl_syms + mod->num_gpl_syms,
328 mod->gpl_crcs,
329 GPL_ONLY, false },
330 { mod->gpl_future_syms,
331 mod->gpl_future_syms + mod->num_gpl_future_syms,
332 mod->gpl_future_crcs,
333 WILL_BE_GPL_ONLY, false },
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500334#ifdef CONFIG_UNUSED_SYMBOLS
Rusty Russelldafd0942008-07-22 19:24:25 -0500335 { mod->unused_syms,
336 mod->unused_syms + mod->num_unused_syms,
337 mod->unused_crcs,
338 NOT_GPL_ONLY, true },
339 { mod->unused_gpl_syms,
340 mod->unused_gpl_syms + mod->num_unused_gpl_syms,
341 mod->unused_gpl_crcs,
342 GPL_ONLY, true },
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500343#endif
Rusty Russelldafd0942008-07-22 19:24:25 -0500344 };
345
346 if (each_symbol_in_section(arr, ARRAY_SIZE(arr), mod, fn, data))
347 return true;
348 }
349 return false;
Arjan van de Venf71d20e2006-06-28 04:26:45 -0700350}
Rusty Russellde4d8d52011-04-19 21:49:58 +0200351EXPORT_SYMBOL_GPL(each_symbol_section);
Arjan van de Venf71d20e2006-06-28 04:26:45 -0700352
Rusty Russelldafd0942008-07-22 19:24:25 -0500353struct find_symbol_arg {
354 /* Input */
355 const char *name;
356 bool gplok;
357 bool warn;
358
359 /* Output */
360 struct module *owner;
361 const unsigned long *crc;
Tim Abbott414fd312008-12-05 19:03:56 -0500362 const struct kernel_symbol *sym;
Rusty Russelldafd0942008-07-22 19:24:25 -0500363};
364
Rusty Russellde4d8d52011-04-19 21:49:58 +0200365static bool check_symbol(const struct symsearch *syms,
366 struct module *owner,
367 unsigned int symnum, void *data)
Rusty Russellad9546c2008-05-01 21:14:59 -0500368{
Rusty Russelldafd0942008-07-22 19:24:25 -0500369 struct find_symbol_arg *fsa = data;
370
Rusty Russelldafd0942008-07-22 19:24:25 -0500371 if (!fsa->gplok) {
372 if (syms->licence == GPL_ONLY)
373 return false;
374 if (syms->licence == WILL_BE_GPL_ONLY && fsa->warn) {
375 printk(KERN_WARNING "Symbol %s is being used "
376 "by a non-GPL module, which will not "
377 "be allowed in the future\n", fsa->name);
378 printk(KERN_WARNING "Please see the file "
379 "Documentation/feature-removal-schedule.txt "
380 "in the kernel source tree for more details.\n");
381 }
382 }
383
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500384#ifdef CONFIG_UNUSED_SYMBOLS
Rusty Russelldafd0942008-07-22 19:24:25 -0500385 if (syms->unused && fsa->warn) {
Rusty Russellad9546c2008-05-01 21:14:59 -0500386 printk(KERN_WARNING "Symbol %s is marked as UNUSED, "
Rusty Russelldafd0942008-07-22 19:24:25 -0500387 "however this module is using it.\n", fsa->name);
Rusty Russellad9546c2008-05-01 21:14:59 -0500388 printk(KERN_WARNING
389 "This symbol will go away in the future.\n");
390 printk(KERN_WARNING
391 "Please evalute if this is the right api to use and if "
392 "it really is, submit a report the linux kernel "
393 "mailinglist together with submitting your code for "
394 "inclusion.\n");
395 }
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500396#endif
Rusty Russelldafd0942008-07-22 19:24:25 -0500397
398 fsa->owner = owner;
399 fsa->crc = symversion(syms->crcs, symnum);
Tim Abbott414fd312008-12-05 19:03:56 -0500400 fsa->sym = &syms->start[symnum];
Rusty Russellad9546c2008-05-01 21:14:59 -0500401 return true;
402}
403
Alessio Igor Bogani403ed272011-04-20 11:10:52 +0200404static int cmp_name(const void *va, const void *vb)
405{
406 const char *a;
407 const struct kernel_symbol *b;
408 a = va; b = vb;
409 return strcmp(a, b->name);
410}
411
Rusty Russellde4d8d52011-04-19 21:49:58 +0200412static bool find_symbol_in_section(const struct symsearch *syms,
413 struct module *owner,
414 void *data)
415{
416 struct find_symbol_arg *fsa = data;
Alessio Igor Bogani403ed272011-04-20 11:10:52 +0200417 struct kernel_symbol *sym;
Rusty Russellde4d8d52011-04-19 21:49:58 +0200418
Alessio Igor Bogani403ed272011-04-20 11:10:52 +0200419 sym = bsearch(fsa->name, syms->start, syms->stop - syms->start,
420 sizeof(struct kernel_symbol), cmp_name);
421
422 if (sym != NULL && check_symbol(syms, owner, sym - syms->start, data))
423 return true;
424
Rusty Russellde4d8d52011-04-19 21:49:58 +0200425 return false;
426}
427
Tim Abbott414fd312008-12-05 19:03:56 -0500428/* Find a symbol and return it, along with, (optional) crc and
Rusty Russell75676502010-06-05 11:17:36 -0600429 * (optional) module which owns it. Needs preempt disabled or module_mutex. */
Tim Abbottc6b37802008-12-05 19:03:59 -0500430const struct kernel_symbol *find_symbol(const char *name,
431 struct module **owner,
432 const unsigned long **crc,
433 bool gplok,
434 bool warn)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435{
Rusty Russelldafd0942008-07-22 19:24:25 -0500436 struct find_symbol_arg fsa;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437
Rusty Russelldafd0942008-07-22 19:24:25 -0500438 fsa.name = name;
439 fsa.gplok = gplok;
440 fsa.warn = warn;
441
Rusty Russellde4d8d52011-04-19 21:49:58 +0200442 if (each_symbol_section(find_symbol_in_section, &fsa)) {
Rusty Russellad9546c2008-05-01 21:14:59 -0500443 if (owner)
Rusty Russelldafd0942008-07-22 19:24:25 -0500444 *owner = fsa.owner;
445 if (crc)
446 *crc = fsa.crc;
Tim Abbott414fd312008-12-05 19:03:56 -0500447 return fsa.sym;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 }
Rusty Russellad9546c2008-05-01 21:14:59 -0500449
Jim Cromie5e124162011-12-06 12:11:31 -0700450 pr_debug("Failed to find symbol %s\n", name);
Tim Abbott414fd312008-12-05 19:03:56 -0500451 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452}
Tim Abbottc6b37802008-12-05 19:03:59 -0500453EXPORT_SYMBOL_GPL(find_symbol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455/* Search for module by name: must hold module_mutex. */
Tim Abbottc6b37802008-12-05 19:03:59 -0500456struct module *find_module(const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457{
458 struct module *mod;
459
460 list_for_each_entry(mod, &modules, list) {
461 if (strcmp(mod->name, name) == 0)
462 return mod;
463 }
464 return NULL;
465}
Tim Abbottc6b37802008-12-05 19:03:59 -0500466EXPORT_SYMBOL_GPL(find_module);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467
468#ifdef CONFIG_SMP
Tejun Heofbf59bc2009-02-20 16:29:08 +0900469
Tejun Heo259354d2010-03-10 18:56:10 +0900470static inline void __percpu *mod_percpu(struct module *mod)
Tejun Heofbf59bc2009-02-20 16:29:08 +0900471{
Tejun Heo259354d2010-03-10 18:56:10 +0900472 return mod->percpu;
473}
Tejun Heofbf59bc2009-02-20 16:29:08 +0900474
Tejun Heo259354d2010-03-10 18:56:10 +0900475static int percpu_modalloc(struct module *mod,
476 unsigned long size, unsigned long align)
477{
Tejun Heofbf59bc2009-02-20 16:29:08 +0900478 if (align > PAGE_SIZE) {
479 printk(KERN_WARNING "%s: per-cpu alignment %li > %li\n",
Tejun Heo259354d2010-03-10 18:56:10 +0900480 mod->name, align, PAGE_SIZE);
Tejun Heofbf59bc2009-02-20 16:29:08 +0900481 align = PAGE_SIZE;
482 }
483
Tejun Heo259354d2010-03-10 18:56:10 +0900484 mod->percpu = __alloc_reserved_percpu(size, align);
485 if (!mod->percpu) {
Tejun Heofbf59bc2009-02-20 16:29:08 +0900486 printk(KERN_WARNING
Rusty Russelld9131882010-08-05 12:59:08 -0600487 "%s: Could not allocate %lu bytes percpu data\n",
488 mod->name, size);
Tejun Heo259354d2010-03-10 18:56:10 +0900489 return -ENOMEM;
490 }
491 mod->percpu_size = size;
492 return 0;
Tejun Heofbf59bc2009-02-20 16:29:08 +0900493}
494
Tejun Heo259354d2010-03-10 18:56:10 +0900495static void percpu_modfree(struct module *mod)
Tejun Heofbf59bc2009-02-20 16:29:08 +0900496{
Tejun Heo259354d2010-03-10 18:56:10 +0900497 free_percpu(mod->percpu);
Tejun Heofbf59bc2009-02-20 16:29:08 +0900498}
499
Rusty Russell49668682010-08-05 12:59:10 -0600500static unsigned int find_pcpusec(struct load_info *info)
Tejun Heo6b588c12009-02-20 16:29:07 +0900501{
Rusty Russell49668682010-08-05 12:59:10 -0600502 return find_sec(info, ".data..percpu");
Tejun Heo6b588c12009-02-20 16:29:07 +0900503}
504
Tejun Heo259354d2010-03-10 18:56:10 +0900505static void percpu_modcopy(struct module *mod,
506 const void *from, unsigned long size)
Tejun Heo6b588c12009-02-20 16:29:07 +0900507{
508 int cpu;
509
510 for_each_possible_cpu(cpu)
Tejun Heo259354d2010-03-10 18:56:10 +0900511 memcpy(per_cpu_ptr(mod->percpu, cpu), from, size);
Tejun Heo6b588c12009-02-20 16:29:07 +0900512}
513
Tejun Heo10fad5e2010-03-10 18:57:54 +0900514/**
515 * is_module_percpu_address - test whether address is from module static percpu
516 * @addr: address to test
517 *
518 * Test whether @addr belongs to module static percpu area.
519 *
520 * RETURNS:
521 * %true if @addr is from module static percpu area
522 */
523bool is_module_percpu_address(unsigned long addr)
524{
525 struct module *mod;
526 unsigned int cpu;
527
528 preempt_disable();
529
530 list_for_each_entry_rcu(mod, &modules, list) {
531 if (!mod->percpu_size)
532 continue;
533 for_each_possible_cpu(cpu) {
534 void *start = per_cpu_ptr(mod->percpu, cpu);
535
536 if ((void *)addr >= start &&
537 (void *)addr < start + mod->percpu_size) {
538 preempt_enable();
539 return true;
540 }
541 }
542 }
543
544 preempt_enable();
545 return false;
Daniel Walker22a8bde2007-10-18 03:06:07 -0700546}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547
548#else /* ... !CONFIG_SMP */
Tejun Heo6b588c12009-02-20 16:29:07 +0900549
Tejun Heo259354d2010-03-10 18:56:10 +0900550static inline void __percpu *mod_percpu(struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551{
552 return NULL;
553}
Tejun Heo259354d2010-03-10 18:56:10 +0900554static inline int percpu_modalloc(struct module *mod,
555 unsigned long size, unsigned long align)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556{
Tejun Heo259354d2010-03-10 18:56:10 +0900557 return -ENOMEM;
558}
559static inline void percpu_modfree(struct module *mod)
560{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561}
Rusty Russell49668682010-08-05 12:59:10 -0600562static unsigned int find_pcpusec(struct load_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563{
564 return 0;
565}
Tejun Heo259354d2010-03-10 18:56:10 +0900566static inline void percpu_modcopy(struct module *mod,
567 const void *from, unsigned long size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568{
569 /* pcpusec should be 0, and size of that section should be 0. */
570 BUG_ON(size != 0);
571}
Tejun Heo10fad5e2010-03-10 18:57:54 +0900572bool is_module_percpu_address(unsigned long addr)
573{
574 return false;
575}
Tejun Heo6b588c12009-02-20 16:29:07 +0900576
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577#endif /* CONFIG_SMP */
578
Matt Domschc988d2b2005-06-23 22:05:15 -0700579#define MODINFO_ATTR(field) \
580static void setup_modinfo_##field(struct module *mod, const char *s) \
581{ \
582 mod->field = kstrdup(s, GFP_KERNEL); \
583} \
584static ssize_t show_modinfo_##field(struct module_attribute *mattr, \
Kay Sievers4befb022011-07-24 22:06:04 +0930585 struct module_kobject *mk, char *buffer) \
Matt Domschc988d2b2005-06-23 22:05:15 -0700586{ \
Kay Sievers4befb022011-07-24 22:06:04 +0930587 return sprintf(buffer, "%s\n", mk->mod->field); \
Matt Domschc988d2b2005-06-23 22:05:15 -0700588} \
589static int modinfo_##field##_exists(struct module *mod) \
590{ \
591 return mod->field != NULL; \
592} \
593static void free_modinfo_##field(struct module *mod) \
594{ \
Daniel Walker22a8bde2007-10-18 03:06:07 -0700595 kfree(mod->field); \
596 mod->field = NULL; \
Matt Domschc988d2b2005-06-23 22:05:15 -0700597} \
598static struct module_attribute modinfo_##field = { \
Tejun Heo7b595752007-06-14 03:45:17 +0900599 .attr = { .name = __stringify(field), .mode = 0444 }, \
Matt Domschc988d2b2005-06-23 22:05:15 -0700600 .show = show_modinfo_##field, \
601 .setup = setup_modinfo_##field, \
602 .test = modinfo_##field##_exists, \
603 .free = free_modinfo_##field, \
604};
605
606MODINFO_ATTR(version);
607MODINFO_ATTR(srcversion);
608
Arjan van de Vene14af7e2008-01-25 21:08:33 +0100609static char last_unloaded_module[MODULE_NAME_LEN+1];
610
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -0800611#ifdef CONFIG_MODULE_UNLOAD
Steven Rostedteb0c5372010-03-29 14:25:18 -0400612
613EXPORT_TRACEPOINT_SYMBOL(module_get);
614
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615/* Init the unload section of the module. */
Rusty Russell9f85a4b2010-08-05 12:59:04 -0600616static int module_unload_init(struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617{
Rusty Russell9f85a4b2010-08-05 12:59:04 -0600618 mod->refptr = alloc_percpu(struct module_ref);
619 if (!mod->refptr)
620 return -ENOMEM;
621
Linus Torvalds2c02dfe2010-05-31 12:19:37 -0700622 INIT_LIST_HEAD(&mod->source_list);
623 INIT_LIST_HEAD(&mod->target_list);
Christoph Lametere1783a22010-01-05 15:34:50 +0900624
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 /* Hold reference count during initialization. */
Nick Piggin5fbfb182010-04-01 19:09:40 +1100626 __this_cpu_write(mod->refptr->incs, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 /* Backwards compatibility macros put refcount during init. */
628 mod->waiter = current;
Rusty Russell9f85a4b2010-08-05 12:59:04 -0600629
630 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631}
632
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633/* Does a already use b? */
634static int already_uses(struct module *a, struct module *b)
635{
636 struct module_use *use;
637
Linus Torvalds2c02dfe2010-05-31 12:19:37 -0700638 list_for_each_entry(use, &b->source_list, source_list) {
639 if (use->source == a) {
Jim Cromie5e124162011-12-06 12:11:31 -0700640 pr_debug("%s uses %s!\n", a->name, b->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 return 1;
642 }
643 }
Jim Cromie5e124162011-12-06 12:11:31 -0700644 pr_debug("%s does not use %s!\n", a->name, b->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 return 0;
646}
647
Linus Torvalds2c02dfe2010-05-31 12:19:37 -0700648/*
649 * Module a uses b
650 * - we add 'a' as a "source", 'b' as a "target" of module use
651 * - the module_use is added to the list of 'b' sources (so
652 * 'b' can walk the list to see who sourced them), and of 'a'
653 * targets (so 'a' can see what modules it targets).
654 */
655static int add_module_usage(struct module *a, struct module *b)
656{
Linus Torvalds2c02dfe2010-05-31 12:19:37 -0700657 struct module_use *use;
658
Jim Cromie5e124162011-12-06 12:11:31 -0700659 pr_debug("Allocating new usage for %s.\n", a->name);
Linus Torvalds2c02dfe2010-05-31 12:19:37 -0700660 use = kmalloc(sizeof(*use), GFP_ATOMIC);
661 if (!use) {
662 printk(KERN_WARNING "%s: out of memory loading\n", a->name);
663 return -ENOMEM;
664 }
665
666 use->source = a;
667 use->target = b;
668 list_add(&use->source_list, &b->source_list);
669 list_add(&use->target_list, &a->target_list);
Linus Torvalds2c02dfe2010-05-31 12:19:37 -0700670 return 0;
671}
672
Rusty Russell75676502010-06-05 11:17:36 -0600673/* Module a uses b: caller needs module_mutex() */
Rusty Russell9bea7f22010-06-05 11:17:37 -0600674int ref_module(struct module *a, struct module *b)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675{
Rusty Russellc8e21ce2010-06-05 11:17:35 -0600676 int err;
Kay Sievers270a6c42007-01-18 13:26:15 +0100677
Rusty Russell9bea7f22010-06-05 11:17:37 -0600678 if (b == NULL || already_uses(a, b))
Linus Torvalds218ce732010-05-25 16:48:30 -0700679 return 0;
Linus Torvalds218ce732010-05-25 16:48:30 -0700680
Rusty Russell9bea7f22010-06-05 11:17:37 -0600681 /* If module isn't available, we fail. */
682 err = strong_try_module_get(b);
Rusty Russellc9a3ba52008-01-29 17:13:18 -0500683 if (err)
Rusty Russell9bea7f22010-06-05 11:17:37 -0600684 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685
Linus Torvalds2c02dfe2010-05-31 12:19:37 -0700686 err = add_module_usage(a, b);
687 if (err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 module_put(b);
Rusty Russell9bea7f22010-06-05 11:17:37 -0600689 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 }
Rusty Russell9bea7f22010-06-05 11:17:37 -0600691 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692}
Rusty Russell9bea7f22010-06-05 11:17:37 -0600693EXPORT_SYMBOL_GPL(ref_module);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694
695/* Clear the unload stuff of the module. */
696static void module_unload_free(struct module *mod)
697{
Linus Torvalds2c02dfe2010-05-31 12:19:37 -0700698 struct module_use *use, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699
Rusty Russell75676502010-06-05 11:17:36 -0600700 mutex_lock(&module_mutex);
Linus Torvalds2c02dfe2010-05-31 12:19:37 -0700701 list_for_each_entry_safe(use, tmp, &mod->target_list, target_list) {
702 struct module *i = use->target;
Jim Cromie5e124162011-12-06 12:11:31 -0700703 pr_debug("%s unusing %s\n", mod->name, i->name);
Linus Torvalds2c02dfe2010-05-31 12:19:37 -0700704 module_put(i);
705 list_del(&use->source_list);
706 list_del(&use->target_list);
707 kfree(use);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 }
Rusty Russell75676502010-06-05 11:17:36 -0600709 mutex_unlock(&module_mutex);
Rusty Russell9f85a4b2010-08-05 12:59:04 -0600710
711 free_percpu(mod->refptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712}
713
714#ifdef CONFIG_MODULE_FORCE_UNLOAD
Akinobu Mitafb169792006-01-08 01:04:29 -0800715static inline int try_force_unload(unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716{
717 int ret = (flags & O_TRUNC);
718 if (ret)
Akinobu Mitafb169792006-01-08 01:04:29 -0800719 add_taint(TAINT_FORCED_RMMOD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 return ret;
721}
722#else
Akinobu Mitafb169792006-01-08 01:04:29 -0800723static inline int try_force_unload(unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724{
725 return 0;
726}
727#endif /* CONFIG_MODULE_FORCE_UNLOAD */
728
729struct stopref
730{
731 struct module *mod;
732 int flags;
733 int *forced;
734};
735
736/* Whole machine is stopped with interrupts off when this runs. */
737static int __try_stop_module(void *_sref)
738{
739 struct stopref *sref = _sref;
740
Rusty Russellda39ba52008-07-22 19:24:25 -0500741 /* If it's not unused, quit unless we're forcing. */
742 if (module_refcount(sref->mod) != 0) {
Akinobu Mitafb169792006-01-08 01:04:29 -0800743 if (!(*sref->forced = try_force_unload(sref->flags)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 return -EWOULDBLOCK;
745 }
746
747 /* Mark it as dying. */
748 sref->mod->state = MODULE_STATE_GOING;
749 return 0;
750}
751
752static int try_stop_module(struct module *mod, int flags, int *forced)
753{
Rusty Russellda39ba52008-07-22 19:24:25 -0500754 if (flags & O_NONBLOCK) {
755 struct stopref sref = { mod, flags, forced };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756
Rusty Russell9b1a4d32008-07-28 12:16:30 -0500757 return stop_machine(__try_stop_module, &sref, NULL);
Rusty Russellda39ba52008-07-22 19:24:25 -0500758 } else {
759 /* We don't need to stop the machine for this. */
760 mod->state = MODULE_STATE_GOING;
761 synchronize_sched();
762 return 0;
763 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764}
765
Eric Dumazetbd77c042012-01-13 09:32:14 +1030766unsigned long module_refcount(struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767{
Eric Dumazetbd77c042012-01-13 09:32:14 +1030768 unsigned long incs = 0, decs = 0;
Eric Dumazet720eba32009-02-03 13:31:36 +1030769 int cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770
Eric Dumazet720eba32009-02-03 13:31:36 +1030771 for_each_possible_cpu(cpu)
Nick Piggin5fbfb182010-04-01 19:09:40 +1100772 decs += per_cpu_ptr(mod->refptr, cpu)->decs;
773 /*
774 * ensure the incs are added up after the decs.
775 * module_put ensures incs are visible before decs with smp_wmb.
776 *
777 * This 2-count scheme avoids the situation where the refcount
778 * for CPU0 is read, then CPU0 increments the module refcount,
779 * then CPU1 drops that refcount, then the refcount for CPU1 is
780 * read. We would record a decrement but not its corresponding
781 * increment so we would see a low count (disaster).
782 *
783 * Rare situation? But module_refcount can be preempted, and we
784 * might be tallying up 4096+ CPUs. So it is not impossible.
785 */
786 smp_rmb();
787 for_each_possible_cpu(cpu)
788 incs += per_cpu_ptr(mod->refptr, cpu)->incs;
789 return incs - decs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790}
791EXPORT_SYMBOL(module_refcount);
792
793/* This exists whether we can unload or not */
794static void free_module(struct module *mod);
795
796static void wait_for_zero_refcount(struct module *mod)
797{
Matthew Wilcoxa6550202008-02-26 10:47:18 -0500798 /* Since we might sleep for some time, release the mutex first */
Ashutosh Naik6389a382006-03-23 03:00:46 -0800799 mutex_unlock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 for (;;) {
Jim Cromie5e124162011-12-06 12:11:31 -0700801 pr_debug("Looking at refcount...\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 set_current_state(TASK_UNINTERRUPTIBLE);
803 if (module_refcount(mod) == 0)
804 break;
805 schedule();
806 }
807 current->state = TASK_RUNNING;
Ashutosh Naik6389a382006-03-23 03:00:46 -0800808 mutex_lock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809}
810
Heiko Carstens17da2bd2009-01-14 14:14:10 +0100811SYSCALL_DEFINE2(delete_module, const char __user *, name_user,
812 unsigned int, flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813{
814 struct module *mod;
Greg Kroah-Hartmandfff0a02007-02-23 14:54:57 -0800815 char name[MODULE_NAME_LEN];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 int ret, forced = 0;
817
Kees Cook3d433212009-04-02 15:49:29 -0700818 if (!capable(CAP_SYS_MODULE) || modules_disabled)
Greg Kroah-Hartmandfff0a02007-02-23 14:54:57 -0800819 return -EPERM;
820
821 if (strncpy_from_user(name, name_user, MODULE_NAME_LEN-1) < 0)
822 return -EFAULT;
823 name[MODULE_NAME_LEN-1] = '\0';
824
Tejun Heo3fc1f1e2010-05-06 18:49:20 +0200825 if (mutex_lock_interruptible(&module_mutex) != 0)
826 return -EINTR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827
828 mod = find_module(name);
829 if (!mod) {
830 ret = -ENOENT;
831 goto out;
832 }
833
Linus Torvalds2c02dfe2010-05-31 12:19:37 -0700834 if (!list_empty(&mod->source_list)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 /* Other modules depend on us: get rid of them first. */
836 ret = -EWOULDBLOCK;
837 goto out;
838 }
839
840 /* Doing init or already dying? */
841 if (mod->state != MODULE_STATE_LIVE) {
842 /* FIXME: if (force), slam module count and wake up
843 waiter --RR */
Jim Cromie5e124162011-12-06 12:11:31 -0700844 pr_debug("%s already dying\n", mod->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 ret = -EBUSY;
846 goto out;
847 }
848
849 /* If it has an init func, it must have an exit func to unload */
Rusty Russellaf49d922007-10-16 23:26:27 -0700850 if (mod->init && !mod->exit) {
Akinobu Mitafb169792006-01-08 01:04:29 -0800851 forced = try_force_unload(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 if (!forced) {
853 /* This module can't be removed */
854 ret = -EBUSY;
855 goto out;
856 }
857 }
858
859 /* Set this up before setting mod->state */
860 mod->waiter = current;
861
862 /* Stop the machine so refcounts can't move and disable module. */
863 ret = try_stop_module(mod, flags, &forced);
864 if (ret != 0)
865 goto out;
866
867 /* Never wait if forced. */
868 if (!forced && module_refcount(mod) != 0)
869 wait_for_zero_refcount(mod);
870
Peter Oberparleiterdf4b5652008-04-21 14:34:31 +0200871 mutex_unlock(&module_mutex);
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300872 /* Final destruction now no one is using it. */
Peter Oberparleiterdf4b5652008-04-21 14:34:31 +0200873 if (mod->exit != NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 mod->exit();
Peter Oberparleiterdf4b5652008-04-21 14:34:31 +0200875 blocking_notifier_call_chain(&module_notify_list,
876 MODULE_STATE_GOING, mod);
Arjan van de Ven22a9d642009-01-07 08:45:46 -0800877 async_synchronize_full();
Rusty Russell75676502010-06-05 11:17:36 -0600878
Arjan van de Vene14af7e2008-01-25 21:08:33 +0100879 /* Store the name of the last unloaded module for diagnostic purposes */
Rusty Russellefa53452008-01-29 17:13:20 -0500880 strlcpy(last_unloaded_module, mod->name, sizeof(last_unloaded_module));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881
Rusty Russell75676502010-06-05 11:17:36 -0600882 free_module(mod);
883 return 0;
884out:
Ashutosh Naik6389a382006-03-23 03:00:46 -0800885 mutex_unlock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 return ret;
887}
888
Jianjun Kongd1e99d72008-12-08 14:26:29 +0800889static inline void print_unload_info(struct seq_file *m, struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890{
891 struct module_use *use;
892 int printed_something = 0;
893
Eric Dumazetbd77c042012-01-13 09:32:14 +1030894 seq_printf(m, " %lu ", module_refcount(mod));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895
896 /* Always include a trailing , so userspace can differentiate
897 between this and the old multi-field proc format. */
Linus Torvalds2c02dfe2010-05-31 12:19:37 -0700898 list_for_each_entry(use, &mod->source_list, source_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 printed_something = 1;
Linus Torvalds2c02dfe2010-05-31 12:19:37 -0700900 seq_printf(m, "%s,", use->source->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 }
902
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 if (mod->init != NULL && mod->exit == NULL) {
904 printed_something = 1;
905 seq_printf(m, "[permanent],");
906 }
907
908 if (!printed_something)
909 seq_printf(m, "-");
910}
911
912void __symbol_put(const char *symbol)
913{
914 struct module *owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915
Rusty Russell24da1cb2007-07-15 23:41:46 -0700916 preempt_disable();
Tim Abbott414fd312008-12-05 19:03:56 -0500917 if (!find_symbol(symbol, &owner, NULL, true, false))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 BUG();
919 module_put(owner);
Rusty Russell24da1cb2007-07-15 23:41:46 -0700920 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921}
922EXPORT_SYMBOL(__symbol_put);
923
Rusty Russell7d1d16e2009-08-26 22:02:54 +0930924/* Note this assumes addr is a function, which it currently always is. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925void symbol_put_addr(void *addr)
926{
Trent Piepho5e376612006-05-15 09:44:06 -0700927 struct module *modaddr;
Rusty Russell7d1d16e2009-08-26 22:02:54 +0930928 unsigned long a = (unsigned long)dereference_function_descriptor(addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929
Rusty Russell7d1d16e2009-08-26 22:02:54 +0930930 if (core_kernel_text(a))
Trent Piepho5e376612006-05-15 09:44:06 -0700931 return;
932
Rusty Russella6e6abd2009-03-31 13:05:31 -0600933 /* module_text_address is safe here: we're supposed to have reference
934 * to module from symbol_get, so it can't go away. */
Rusty Russell7d1d16e2009-08-26 22:02:54 +0930935 modaddr = __module_text_address(a);
Rusty Russella6e6abd2009-03-31 13:05:31 -0600936 BUG_ON(!modaddr);
Trent Piepho5e376612006-05-15 09:44:06 -0700937 module_put(modaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938}
939EXPORT_SYMBOL_GPL(symbol_put_addr);
940
941static ssize_t show_refcnt(struct module_attribute *mattr,
Kay Sievers4befb022011-07-24 22:06:04 +0930942 struct module_kobject *mk, char *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943{
Eric Dumazetbd77c042012-01-13 09:32:14 +1030944 return sprintf(buffer, "%lu\n", module_refcount(mk->mod));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945}
946
Kay Sieverscca3e702012-01-13 09:32:15 +1030947static struct module_attribute modinfo_refcnt =
948 __ATTR(refcnt, 0444, show_refcnt, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949
Steven Rostedtd53799b2012-03-26 12:50:52 +1030950void __module_get(struct module *module)
951{
952 if (module) {
953 preempt_disable();
954 __this_cpu_inc(module->refptr->incs);
955 trace_module_get(module, _RET_IP_);
956 preempt_enable();
957 }
958}
959EXPORT_SYMBOL(__module_get);
960
961bool try_module_get(struct module *module)
962{
963 bool ret = true;
964
965 if (module) {
966 preempt_disable();
967
968 if (likely(module_is_live(module))) {
969 __this_cpu_inc(module->refptr->incs);
970 trace_module_get(module, _RET_IP_);
971 } else
972 ret = false;
973
974 preempt_enable();
975 }
976 return ret;
977}
978EXPORT_SYMBOL(try_module_get);
979
Al Virof6a57032006-10-18 01:47:25 -0400980void module_put(struct module *module)
981{
982 if (module) {
Christoph Lametere1783a22010-01-05 15:34:50 +0900983 preempt_disable();
Nick Piggin5fbfb182010-04-01 19:09:40 +1100984 smp_wmb(); /* see comment in module_refcount */
985 __this_cpu_inc(module->refptr->decs);
Christoph Lametere1783a22010-01-05 15:34:50 +0900986
Li Zefanae832d12010-03-24 10:57:43 +0800987 trace_module_put(module, _RET_IP_);
Al Virof6a57032006-10-18 01:47:25 -0400988 /* Maybe they're waiting for us to drop reference? */
989 if (unlikely(!module_is_live(module)))
990 wake_up_process(module->waiter);
Christoph Lametere1783a22010-01-05 15:34:50 +0900991 preempt_enable();
Al Virof6a57032006-10-18 01:47:25 -0400992 }
993}
994EXPORT_SYMBOL(module_put);
995
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996#else /* !CONFIG_MODULE_UNLOAD */
Jianjun Kongd1e99d72008-12-08 14:26:29 +0800997static inline void print_unload_info(struct seq_file *m, struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998{
999 /* We don't know the usage count, or what modules are using. */
1000 seq_printf(m, " - -");
1001}
1002
1003static inline void module_unload_free(struct module *mod)
1004{
1005}
1006
Rusty Russell9bea7f22010-06-05 11:17:37 -06001007int ref_module(struct module *a, struct module *b)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008{
Rusty Russell9bea7f22010-06-05 11:17:37 -06001009 return strong_try_module_get(b);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010}
Rusty Russell9bea7f22010-06-05 11:17:37 -06001011EXPORT_SYMBOL_GPL(ref_module);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012
Rusty Russell9f85a4b2010-08-05 12:59:04 -06001013static inline int module_unload_init(struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014{
Rusty Russell9f85a4b2010-08-05 12:59:04 -06001015 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016}
1017#endif /* CONFIG_MODULE_UNLOAD */
1018
Kevin Winchester53999bf2012-01-15 19:32:55 -04001019static size_t module_flags_taint(struct module *mod, char *buf)
1020{
1021 size_t l = 0;
1022
1023 if (mod->taints & (1 << TAINT_PROPRIETARY_MODULE))
1024 buf[l++] = 'P';
1025 if (mod->taints & (1 << TAINT_OOT_MODULE))
1026 buf[l++] = 'O';
1027 if (mod->taints & (1 << TAINT_FORCED_MODULE))
1028 buf[l++] = 'F';
1029 if (mod->taints & (1 << TAINT_CRAP))
1030 buf[l++] = 'C';
1031 /*
1032 * TAINT_FORCED_RMMOD: could be added.
1033 * TAINT_UNSAFE_SMP, TAINT_MACHINE_CHECK, TAINT_BAD_PAGE don't
1034 * apply to modules.
1035 */
1036 return l;
1037}
1038
Kay Sievers1f717402006-11-24 12:15:25 +01001039static ssize_t show_initstate(struct module_attribute *mattr,
Kay Sievers4befb022011-07-24 22:06:04 +09301040 struct module_kobject *mk, char *buffer)
Kay Sievers1f717402006-11-24 12:15:25 +01001041{
1042 const char *state = "unknown";
1043
Kay Sievers4befb022011-07-24 22:06:04 +09301044 switch (mk->mod->state) {
Kay Sievers1f717402006-11-24 12:15:25 +01001045 case MODULE_STATE_LIVE:
1046 state = "live";
1047 break;
1048 case MODULE_STATE_COMING:
1049 state = "coming";
1050 break;
1051 case MODULE_STATE_GOING:
1052 state = "going";
1053 break;
1054 }
1055 return sprintf(buffer, "%s\n", state);
1056}
1057
Kay Sieverscca3e702012-01-13 09:32:15 +10301058static struct module_attribute modinfo_initstate =
1059 __ATTR(initstate, 0444, show_initstate, NULL);
Kay Sievers1f717402006-11-24 12:15:25 +01001060
Kay Sievers88bfa322011-07-24 22:06:04 +09301061static ssize_t store_uevent(struct module_attribute *mattr,
1062 struct module_kobject *mk,
1063 const char *buffer, size_t count)
1064{
1065 enum kobject_action action;
1066
1067 if (kobject_action_type(buffer, count, &action) == 0)
1068 kobject_uevent(&mk->kobj, action);
1069 return count;
1070}
1071
Kay Sieverscca3e702012-01-13 09:32:15 +10301072struct module_attribute module_uevent =
1073 __ATTR(uevent, 0200, NULL, store_uevent);
1074
1075static ssize_t show_coresize(struct module_attribute *mattr,
1076 struct module_kobject *mk, char *buffer)
1077{
1078 return sprintf(buffer, "%u\n", mk->mod->core_size);
1079}
1080
1081static struct module_attribute modinfo_coresize =
1082 __ATTR(coresize, 0444, show_coresize, NULL);
1083
1084static ssize_t show_initsize(struct module_attribute *mattr,
1085 struct module_kobject *mk, char *buffer)
1086{
1087 return sprintf(buffer, "%u\n", mk->mod->init_size);
1088}
1089
1090static struct module_attribute modinfo_initsize =
1091 __ATTR(initsize, 0444, show_initsize, NULL);
1092
1093static ssize_t show_taint(struct module_attribute *mattr,
1094 struct module_kobject *mk, char *buffer)
1095{
1096 size_t l;
1097
1098 l = module_flags_taint(mk->mod, buffer);
1099 buffer[l++] = '\n';
1100 return l;
1101}
1102
1103static struct module_attribute modinfo_taint =
1104 __ATTR(taint, 0444, show_taint, NULL);
Kay Sievers88bfa322011-07-24 22:06:04 +09301105
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001106static struct module_attribute *modinfo_attrs[] = {
Kay Sieverscca3e702012-01-13 09:32:15 +10301107 &module_uevent,
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001108 &modinfo_version,
1109 &modinfo_srcversion,
Kay Sieverscca3e702012-01-13 09:32:15 +10301110 &modinfo_initstate,
1111 &modinfo_coresize,
1112 &modinfo_initsize,
1113 &modinfo_taint,
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001114#ifdef CONFIG_MODULE_UNLOAD
Kay Sieverscca3e702012-01-13 09:32:15 +10301115 &modinfo_refcnt,
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001116#endif
1117 NULL,
1118};
1119
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120static const char vermagic[] = VERMAGIC_STRING;
1121
Rusty Russellc6e665c2009-03-31 13:05:33 -06001122static int try_to_force_load(struct module *mod, const char *reason)
Linus Torvalds826e4502008-05-04 17:04:16 -07001123{
1124#ifdef CONFIG_MODULE_FORCE_LOAD
Andi Kleen25ddbb12008-10-15 22:01:41 -07001125 if (!test_taint(TAINT_FORCED_MODULE))
Rusty Russellc6e665c2009-03-31 13:05:33 -06001126 printk(KERN_WARNING "%s: %s: kernel tainted.\n",
1127 mod->name, reason);
Linus Torvalds826e4502008-05-04 17:04:16 -07001128 add_taint_module(mod, TAINT_FORCED_MODULE);
1129 return 0;
1130#else
1131 return -ENOEXEC;
1132#endif
1133}
1134
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135#ifdef CONFIG_MODVERSIONS
Rusty Russelld4703ae2009-12-15 16:28:32 -06001136/* If the arch applies (non-zero) relocations to kernel kcrctab, unapply it. */
1137static unsigned long maybe_relocated(unsigned long crc,
1138 const struct module *crc_owner)
1139{
1140#ifdef ARCH_RELOCATES_KCRCTAB
1141 if (crc_owner == NULL)
1142 return crc - (unsigned long)reloc_start;
1143#endif
1144 return crc;
1145}
1146
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147static int check_version(Elf_Shdr *sechdrs,
1148 unsigned int versindex,
1149 const char *symname,
1150 struct module *mod,
Rusty Russelld4703ae2009-12-15 16:28:32 -06001151 const unsigned long *crc,
1152 const struct module *crc_owner)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153{
1154 unsigned int i, num_versions;
1155 struct modversion_info *versions;
1156
1157 /* Exporting module didn't supply crcs? OK, we're already tainted. */
1158 if (!crc)
1159 return 1;
1160
Rusty Russella5dd6972008-05-09 16:24:21 +10001161 /* No versions at all? modprobe --force does this. */
1162 if (versindex == 0)
1163 return try_to_force_load(mod, symname) == 0;
1164
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 versions = (void *) sechdrs[versindex].sh_addr;
1166 num_versions = sechdrs[versindex].sh_size
1167 / sizeof(struct modversion_info);
1168
1169 for (i = 0; i < num_versions; i++) {
1170 if (strcmp(versions[i].name, symname) != 0)
1171 continue;
1172
Rusty Russelld4703ae2009-12-15 16:28:32 -06001173 if (versions[i].crc == maybe_relocated(*crc, crc_owner))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174 return 1;
Jim Cromie5e124162011-12-06 12:11:31 -07001175 pr_debug("Found checksum %lX vs module %lX\n",
Rusty Russelld4703ae2009-12-15 16:28:32 -06001176 maybe_relocated(*crc, crc_owner), versions[i].crc);
Linus Torvalds826e4502008-05-04 17:04:16 -07001177 goto bad_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178 }
Linus Torvalds826e4502008-05-04 17:04:16 -07001179
Rusty Russella5dd6972008-05-09 16:24:21 +10001180 printk(KERN_WARNING "%s: no symbol version for %s\n",
1181 mod->name, symname);
1182 return 0;
Linus Torvalds826e4502008-05-04 17:04:16 -07001183
1184bad_version:
1185 printk("%s: disagrees about version of symbol %s\n",
1186 mod->name, symname);
1187 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188}
1189
1190static inline int check_modstruct_version(Elf_Shdr *sechdrs,
1191 unsigned int versindex,
1192 struct module *mod)
1193{
1194 const unsigned long *crc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195
Rusty Russell75676502010-06-05 11:17:36 -06001196 /* Since this should be found in kernel (which can't be removed),
1197 * no locking is necessary. */
Mike Frysinger6560dc12009-07-23 23:42:08 +09301198 if (!find_symbol(MODULE_SYMBOL_PREFIX "module_layout", NULL,
1199 &crc, true, false))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 BUG();
Rusty Russelld4703ae2009-12-15 16:28:32 -06001201 return check_version(sechdrs, versindex, "module_layout", mod, crc,
1202 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203}
1204
Rusty Russell91e37a72008-05-09 16:25:28 +10001205/* First part is kernel version, which we ignore if module has crcs. */
1206static inline int same_magic(const char *amagic, const char *bmagic,
1207 bool has_crcs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208{
Rusty Russell91e37a72008-05-09 16:25:28 +10001209 if (has_crcs) {
1210 amagic += strcspn(amagic, " ");
1211 bmagic += strcspn(bmagic, " ");
1212 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213 return strcmp(amagic, bmagic) == 0;
1214}
1215#else
1216static inline int check_version(Elf_Shdr *sechdrs,
1217 unsigned int versindex,
1218 const char *symname,
1219 struct module *mod,
Rusty Russelld4703ae2009-12-15 16:28:32 -06001220 const unsigned long *crc,
1221 const struct module *crc_owner)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222{
1223 return 1;
1224}
1225
1226static inline int check_modstruct_version(Elf_Shdr *sechdrs,
1227 unsigned int versindex,
1228 struct module *mod)
1229{
1230 return 1;
1231}
1232
Rusty Russell91e37a72008-05-09 16:25:28 +10001233static inline int same_magic(const char *amagic, const char *bmagic,
1234 bool has_crcs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235{
1236 return strcmp(amagic, bmagic) == 0;
1237}
1238#endif /* CONFIG_MODVERSIONS */
1239
Rusty Russell75676502010-06-05 11:17:36 -06001240/* Resolve a symbol for this module. I.e. if we find one, record usage. */
Rusty Russell49668682010-08-05 12:59:10 -06001241static const struct kernel_symbol *resolve_symbol(struct module *mod,
1242 const struct load_info *info,
Tim Abbott414fd312008-12-05 19:03:56 -05001243 const char *name,
Rusty Russell9bea7f22010-06-05 11:17:37 -06001244 char ownername[])
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245{
1246 struct module *owner;
Tim Abbott414fd312008-12-05 19:03:56 -05001247 const struct kernel_symbol *sym;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248 const unsigned long *crc;
Rusty Russell9bea7f22010-06-05 11:17:37 -06001249 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250
Rusty Russell75676502010-06-05 11:17:36 -06001251 mutex_lock(&module_mutex);
Tim Abbott414fd312008-12-05 19:03:56 -05001252 sym = find_symbol(name, &owner, &crc,
Andi Kleen25ddbb12008-10-15 22:01:41 -07001253 !(mod->taints & (1 << TAINT_PROPRIETARY_MODULE)), true);
Rusty Russell9bea7f22010-06-05 11:17:37 -06001254 if (!sym)
1255 goto unlock;
1256
Rusty Russell49668682010-08-05 12:59:10 -06001257 if (!check_version(info->sechdrs, info->index.vers, name, mod, crc,
1258 owner)) {
Rusty Russell9bea7f22010-06-05 11:17:37 -06001259 sym = ERR_PTR(-EINVAL);
1260 goto getname;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261 }
Rusty Russell9bea7f22010-06-05 11:17:37 -06001262
1263 err = ref_module(mod, owner);
1264 if (err) {
1265 sym = ERR_PTR(err);
1266 goto getname;
1267 }
1268
1269getname:
1270 /* We must make copy under the lock if we failed to get ref. */
1271 strncpy(ownername, module_name(owner), MODULE_NAME_LEN);
1272unlock:
Rusty Russell75676502010-06-05 11:17:36 -06001273 mutex_unlock(&module_mutex);
Linus Torvalds218ce732010-05-25 16:48:30 -07001274 return sym;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275}
1276
Rusty Russell49668682010-08-05 12:59:10 -06001277static const struct kernel_symbol *
1278resolve_symbol_wait(struct module *mod,
1279 const struct load_info *info,
1280 const char *name)
Rusty Russell9bea7f22010-06-05 11:17:37 -06001281{
1282 const struct kernel_symbol *ksym;
Rusty Russell49668682010-08-05 12:59:10 -06001283 char owner[MODULE_NAME_LEN];
Rusty Russell9bea7f22010-06-05 11:17:37 -06001284
1285 if (wait_event_interruptible_timeout(module_wq,
Rusty Russell49668682010-08-05 12:59:10 -06001286 !IS_ERR(ksym = resolve_symbol(mod, info, name, owner))
1287 || PTR_ERR(ksym) != -EBUSY,
Rusty Russell9bea7f22010-06-05 11:17:37 -06001288 30 * HZ) <= 0) {
1289 printk(KERN_WARNING "%s: gave up waiting for init of module %s.\n",
Rusty Russell49668682010-08-05 12:59:10 -06001290 mod->name, owner);
Rusty Russell9bea7f22010-06-05 11:17:37 -06001291 }
1292 return ksym;
1293}
1294
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295/*
1296 * /sys/module/foo/sections stuff
1297 * J. Corbet <corbet@lwn.net>
1298 */
Rusty Russell8f6d0372010-08-05 12:59:09 -06001299#ifdef CONFIG_SYSFS
Ben Hutchings10b465a2009-12-19 14:43:01 +00001300
Rusty Russell8f6d0372010-08-05 12:59:09 -06001301#ifdef CONFIG_KALLSYMS
Ben Hutchings10b465a2009-12-19 14:43:01 +00001302static inline bool sect_empty(const Elf_Shdr *sect)
1303{
1304 return !(sect->sh_flags & SHF_ALLOC) || sect->sh_size == 0;
1305}
1306
Rusty Russella58730c2008-03-13 09:03:44 +00001307struct module_sect_attr
1308{
1309 struct module_attribute mattr;
1310 char *name;
1311 unsigned long address;
1312};
1313
1314struct module_sect_attrs
1315{
1316 struct attribute_group grp;
1317 unsigned int nsections;
1318 struct module_sect_attr attrs[0];
1319};
1320
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321static ssize_t module_sect_show(struct module_attribute *mattr,
Kay Sievers4befb022011-07-24 22:06:04 +09301322 struct module_kobject *mk, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323{
1324 struct module_sect_attr *sattr =
1325 container_of(mattr, struct module_sect_attr, mattr);
Kees Cook9f36e2c2011-03-22 16:34:22 -07001326 return sprintf(buf, "0x%pK\n", (void *)sattr->address);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327}
1328
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001329static void free_sect_attrs(struct module_sect_attrs *sect_attrs)
1330{
Rusty Russella58730c2008-03-13 09:03:44 +00001331 unsigned int section;
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001332
1333 for (section = 0; section < sect_attrs->nsections; section++)
1334 kfree(sect_attrs->attrs[section].name);
1335 kfree(sect_attrs);
1336}
1337
Rusty Russell8f6d0372010-08-05 12:59:09 -06001338static void add_sect_attrs(struct module *mod, const struct load_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339{
1340 unsigned int nloaded = 0, i, size[2];
1341 struct module_sect_attrs *sect_attrs;
1342 struct module_sect_attr *sattr;
1343 struct attribute **gattr;
Daniel Walker22a8bde2007-10-18 03:06:07 -07001344
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345 /* Count loaded sections and allocate structures */
Rusty Russell8f6d0372010-08-05 12:59:09 -06001346 for (i = 0; i < info->hdr->e_shnum; i++)
1347 if (!sect_empty(&info->sechdrs[i]))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348 nloaded++;
1349 size[0] = ALIGN(sizeof(*sect_attrs)
1350 + nloaded * sizeof(sect_attrs->attrs[0]),
1351 sizeof(sect_attrs->grp.attrs[0]));
1352 size[1] = (nloaded + 1) * sizeof(sect_attrs->grp.attrs[0]);
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001353 sect_attrs = kzalloc(size[0] + size[1], GFP_KERNEL);
1354 if (sect_attrs == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355 return;
1356
1357 /* Setup section attributes. */
1358 sect_attrs->grp.name = "sections";
1359 sect_attrs->grp.attrs = (void *)sect_attrs + size[0];
1360
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001361 sect_attrs->nsections = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362 sattr = &sect_attrs->attrs[0];
1363 gattr = &sect_attrs->grp.attrs[0];
Rusty Russell8f6d0372010-08-05 12:59:09 -06001364 for (i = 0; i < info->hdr->e_shnum; i++) {
1365 Elf_Shdr *sec = &info->sechdrs[i];
1366 if (sect_empty(sec))
Helge Deller35dead42009-12-03 00:29:15 +01001367 continue;
Rusty Russell8f6d0372010-08-05 12:59:09 -06001368 sattr->address = sec->sh_addr;
1369 sattr->name = kstrdup(info->secstrings + sec->sh_name,
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001370 GFP_KERNEL);
1371 if (sattr->name == NULL)
1372 goto out;
1373 sect_attrs->nsections++;
Eric W. Biederman361795b2010-02-12 13:41:56 -08001374 sysfs_attr_init(&sattr->mattr.attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375 sattr->mattr.show = module_sect_show;
1376 sattr->mattr.store = NULL;
1377 sattr->mattr.attr.name = sattr->name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378 sattr->mattr.attr.mode = S_IRUGO;
1379 *(gattr++) = &(sattr++)->mattr.attr;
1380 }
1381 *gattr = NULL;
1382
1383 if (sysfs_create_group(&mod->mkobj.kobj, &sect_attrs->grp))
1384 goto out;
1385
1386 mod->sect_attrs = sect_attrs;
1387 return;
1388 out:
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001389 free_sect_attrs(sect_attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390}
1391
1392static void remove_sect_attrs(struct module *mod)
1393{
1394 if (mod->sect_attrs) {
1395 sysfs_remove_group(&mod->mkobj.kobj,
1396 &mod->sect_attrs->grp);
1397 /* We are positive that no one is using any sect attrs
1398 * at this point. Deallocate immediately. */
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001399 free_sect_attrs(mod->sect_attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400 mod->sect_attrs = NULL;
1401 }
1402}
1403
Roland McGrath6d760132007-10-16 23:26:40 -07001404/*
1405 * /sys/module/foo/notes/.section.name gives contents of SHT_NOTE sections.
1406 */
1407
1408struct module_notes_attrs {
1409 struct kobject *dir;
1410 unsigned int notes;
1411 struct bin_attribute attrs[0];
1412};
1413
Chris Wright2c3c8be2010-05-12 18:28:57 -07001414static ssize_t module_notes_read(struct file *filp, struct kobject *kobj,
Roland McGrath6d760132007-10-16 23:26:40 -07001415 struct bin_attribute *bin_attr,
1416 char *buf, loff_t pos, size_t count)
1417{
1418 /*
1419 * The caller checked the pos and count against our size.
1420 */
1421 memcpy(buf, bin_attr->private + pos, count);
1422 return count;
1423}
1424
1425static void free_notes_attrs(struct module_notes_attrs *notes_attrs,
1426 unsigned int i)
1427{
1428 if (notes_attrs->dir) {
1429 while (i-- > 0)
1430 sysfs_remove_bin_file(notes_attrs->dir,
1431 &notes_attrs->attrs[i]);
Alexey Dobriyane9432092008-09-23 23:51:11 +04001432 kobject_put(notes_attrs->dir);
Roland McGrath6d760132007-10-16 23:26:40 -07001433 }
1434 kfree(notes_attrs);
1435}
1436
Rusty Russell8f6d0372010-08-05 12:59:09 -06001437static void add_notes_attrs(struct module *mod, const struct load_info *info)
Roland McGrath6d760132007-10-16 23:26:40 -07001438{
1439 unsigned int notes, loaded, i;
1440 struct module_notes_attrs *notes_attrs;
1441 struct bin_attribute *nattr;
1442
Ingo Molnarea6bff32009-08-28 10:44:56 +02001443 /* failed to create section attributes, so can't create notes */
1444 if (!mod->sect_attrs)
1445 return;
1446
Roland McGrath6d760132007-10-16 23:26:40 -07001447 /* Count notes sections and allocate structures. */
1448 notes = 0;
Rusty Russell8f6d0372010-08-05 12:59:09 -06001449 for (i = 0; i < info->hdr->e_shnum; i++)
1450 if (!sect_empty(&info->sechdrs[i]) &&
1451 (info->sechdrs[i].sh_type == SHT_NOTE))
Roland McGrath6d760132007-10-16 23:26:40 -07001452 ++notes;
1453
1454 if (notes == 0)
1455 return;
1456
1457 notes_attrs = kzalloc(sizeof(*notes_attrs)
1458 + notes * sizeof(notes_attrs->attrs[0]),
1459 GFP_KERNEL);
1460 if (notes_attrs == NULL)
1461 return;
1462
1463 notes_attrs->notes = notes;
1464 nattr = &notes_attrs->attrs[0];
Rusty Russell8f6d0372010-08-05 12:59:09 -06001465 for (loaded = i = 0; i < info->hdr->e_shnum; ++i) {
1466 if (sect_empty(&info->sechdrs[i]))
Roland McGrath6d760132007-10-16 23:26:40 -07001467 continue;
Rusty Russell8f6d0372010-08-05 12:59:09 -06001468 if (info->sechdrs[i].sh_type == SHT_NOTE) {
Eric W. Biederman361795b2010-02-12 13:41:56 -08001469 sysfs_bin_attr_init(nattr);
Roland McGrath6d760132007-10-16 23:26:40 -07001470 nattr->attr.name = mod->sect_attrs->attrs[loaded].name;
1471 nattr->attr.mode = S_IRUGO;
Rusty Russell8f6d0372010-08-05 12:59:09 -06001472 nattr->size = info->sechdrs[i].sh_size;
1473 nattr->private = (void *) info->sechdrs[i].sh_addr;
Roland McGrath6d760132007-10-16 23:26:40 -07001474 nattr->read = module_notes_read;
1475 ++nattr;
1476 }
1477 ++loaded;
1478 }
1479
Greg Kroah-Hartman4ff6abf2007-11-05 22:24:43 -08001480 notes_attrs->dir = kobject_create_and_add("notes", &mod->mkobj.kobj);
Roland McGrath6d760132007-10-16 23:26:40 -07001481 if (!notes_attrs->dir)
1482 goto out;
1483
1484 for (i = 0; i < notes; ++i)
1485 if (sysfs_create_bin_file(notes_attrs->dir,
1486 &notes_attrs->attrs[i]))
1487 goto out;
1488
1489 mod->notes_attrs = notes_attrs;
1490 return;
1491
1492 out:
1493 free_notes_attrs(notes_attrs, i);
1494}
1495
1496static void remove_notes_attrs(struct module *mod)
1497{
1498 if (mod->notes_attrs)
1499 free_notes_attrs(mod->notes_attrs, mod->notes_attrs->notes);
1500}
1501
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502#else
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001503
Rusty Russell8f6d0372010-08-05 12:59:09 -06001504static inline void add_sect_attrs(struct module *mod,
1505 const struct load_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506{
1507}
1508
1509static inline void remove_sect_attrs(struct module *mod)
1510{
1511}
Roland McGrath6d760132007-10-16 23:26:40 -07001512
Rusty Russell8f6d0372010-08-05 12:59:09 -06001513static inline void add_notes_attrs(struct module *mod,
1514 const struct load_info *info)
Roland McGrath6d760132007-10-16 23:26:40 -07001515{
1516}
1517
1518static inline void remove_notes_attrs(struct module *mod)
1519{
1520}
Rusty Russell8f6d0372010-08-05 12:59:09 -06001521#endif /* CONFIG_KALLSYMS */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522
Rusty Russell80a3d1b2010-06-05 11:17:36 -06001523static void add_usage_links(struct module *mod)
1524{
1525#ifdef CONFIG_MODULE_UNLOAD
1526 struct module_use *use;
1527 int nowarn;
1528
Rusty Russell75676502010-06-05 11:17:36 -06001529 mutex_lock(&module_mutex);
Rusty Russell80a3d1b2010-06-05 11:17:36 -06001530 list_for_each_entry(use, &mod->target_list, target_list) {
1531 nowarn = sysfs_create_link(use->target->holders_dir,
1532 &mod->mkobj.kobj, mod->name);
1533 }
Rusty Russell75676502010-06-05 11:17:36 -06001534 mutex_unlock(&module_mutex);
Rusty Russell80a3d1b2010-06-05 11:17:36 -06001535#endif
1536}
1537
1538static void del_usage_links(struct module *mod)
1539{
1540#ifdef CONFIG_MODULE_UNLOAD
1541 struct module_use *use;
1542
Rusty Russell75676502010-06-05 11:17:36 -06001543 mutex_lock(&module_mutex);
Rusty Russell80a3d1b2010-06-05 11:17:36 -06001544 list_for_each_entry(use, &mod->target_list, target_list)
1545 sysfs_remove_link(use->target->holders_dir, mod->name);
Rusty Russell75676502010-06-05 11:17:36 -06001546 mutex_unlock(&module_mutex);
Rusty Russell80a3d1b2010-06-05 11:17:36 -06001547#endif
1548}
1549
Rusty Russell6407ebb22010-06-05 11:17:36 -06001550static int module_add_modinfo_attrs(struct module *mod)
Matt Domschc988d2b2005-06-23 22:05:15 -07001551{
1552 struct module_attribute *attr;
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001553 struct module_attribute *temp_attr;
Matt Domschc988d2b2005-06-23 22:05:15 -07001554 int error = 0;
1555 int i;
1556
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001557 mod->modinfo_attrs = kzalloc((sizeof(struct module_attribute) *
1558 (ARRAY_SIZE(modinfo_attrs) + 1)),
1559 GFP_KERNEL);
1560 if (!mod->modinfo_attrs)
1561 return -ENOMEM;
1562
1563 temp_attr = mod->modinfo_attrs;
Matt Domschc988d2b2005-06-23 22:05:15 -07001564 for (i = 0; (attr = modinfo_attrs[i]) && !error; i++) {
1565 if (!attr->test ||
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001566 (attr->test && attr->test(mod))) {
1567 memcpy(temp_attr, attr, sizeof(*temp_attr));
Eric W. Biederman361795b2010-02-12 13:41:56 -08001568 sysfs_attr_init(&temp_attr->attr);
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001569 error = sysfs_create_file(&mod->mkobj.kobj,&temp_attr->attr);
1570 ++temp_attr;
1571 }
Matt Domschc988d2b2005-06-23 22:05:15 -07001572 }
1573 return error;
1574}
1575
Rusty Russell6407ebb22010-06-05 11:17:36 -06001576static void module_remove_modinfo_attrs(struct module *mod)
Matt Domschc988d2b2005-06-23 22:05:15 -07001577{
1578 struct module_attribute *attr;
1579 int i;
1580
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001581 for (i = 0; (attr = &mod->modinfo_attrs[i]); i++) {
1582 /* pick a field to test for end of list */
1583 if (!attr->attr.name)
1584 break;
Matt Domschc988d2b2005-06-23 22:05:15 -07001585 sysfs_remove_file(&mod->mkobj.kobj,&attr->attr);
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001586 if (attr->free)
1587 attr->free(mod);
Matt Domschc988d2b2005-06-23 22:05:15 -07001588 }
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001589 kfree(mod->modinfo_attrs);
Matt Domschc988d2b2005-06-23 22:05:15 -07001590}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591
Rusty Russell6407ebb22010-06-05 11:17:36 -06001592static int mod_sysfs_init(struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593{
1594 int err;
Greg Kroah-Hartman6494a932008-01-27 15:38:40 -08001595 struct kobject *kobj;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596
Greg Kroah-Hartman823bccf2007-04-13 13:15:19 -07001597 if (!module_sysfs_initialized) {
1598 printk(KERN_ERR "%s: module sysfs not initialized\n",
Ed Swierk1cc5f712006-09-25 16:25:36 -07001599 mod->name);
1600 err = -EINVAL;
1601 goto out;
1602 }
Greg Kroah-Hartman6494a932008-01-27 15:38:40 -08001603
1604 kobj = kset_find_obj(module_kset, mod->name);
1605 if (kobj) {
1606 printk(KERN_ERR "%s: module is already loaded\n", mod->name);
1607 kobject_put(kobj);
1608 err = -EINVAL;
1609 goto out;
1610 }
1611
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612 mod->mkobj.mod = mod;
Kay Sieverse17e0f52006-11-24 12:15:25 +01001613
Greg Kroah-Hartmanac3c8142007-12-17 23:05:35 -07001614 memset(&mod->mkobj.kobj, 0, sizeof(mod->mkobj.kobj));
1615 mod->mkobj.kobj.kset = module_kset;
1616 err = kobject_init_and_add(&mod->mkobj.kobj, &module_ktype, NULL,
1617 "%s", mod->name);
1618 if (err)
1619 kobject_put(&mod->mkobj.kobj);
Kay Sievers270a6c42007-01-18 13:26:15 +01001620
Kay Sievers97c146e2007-11-29 23:46:11 +01001621 /* delay uevent until full sysfs population */
Kay Sievers270a6c42007-01-18 13:26:15 +01001622out:
1623 return err;
1624}
1625
Rusty Russell6407ebb22010-06-05 11:17:36 -06001626static int mod_sysfs_setup(struct module *mod,
Rusty Russell8f6d0372010-08-05 12:59:09 -06001627 const struct load_info *info,
Kay Sievers270a6c42007-01-18 13:26:15 +01001628 struct kernel_param *kparam,
1629 unsigned int num_params)
1630{
1631 int err;
1632
Rusty Russell80a3d1b2010-06-05 11:17:36 -06001633 err = mod_sysfs_init(mod);
1634 if (err)
1635 goto out;
1636
Greg Kroah-Hartman4ff6abf2007-11-05 22:24:43 -08001637 mod->holders_dir = kobject_create_and_add("holders", &mod->mkobj.kobj);
Akinobu Mita240936e2007-04-26 00:12:09 -07001638 if (!mod->holders_dir) {
1639 err = -ENOMEM;
Kay Sievers270a6c42007-01-18 13:26:15 +01001640 goto out_unreg;
Akinobu Mita240936e2007-04-26 00:12:09 -07001641 }
Kay Sievers270a6c42007-01-18 13:26:15 +01001642
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643 err = module_param_sysfs_setup(mod, kparam, num_params);
1644 if (err)
Kay Sievers270a6c42007-01-18 13:26:15 +01001645 goto out_unreg_holders;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646
Matt Domschc988d2b2005-06-23 22:05:15 -07001647 err = module_add_modinfo_attrs(mod);
1648 if (err)
Kay Sieverse17e0f52006-11-24 12:15:25 +01001649 goto out_unreg_param;
Matt Domschc988d2b2005-06-23 22:05:15 -07001650
Rusty Russell80a3d1b2010-06-05 11:17:36 -06001651 add_usage_links(mod);
Rusty Russell8f6d0372010-08-05 12:59:09 -06001652 add_sect_attrs(mod, info);
1653 add_notes_attrs(mod, info);
Rusty Russell80a3d1b2010-06-05 11:17:36 -06001654
Kay Sieverse17e0f52006-11-24 12:15:25 +01001655 kobject_uevent(&mod->mkobj.kobj, KOBJ_ADD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656 return 0;
1657
Kay Sieverse17e0f52006-11-24 12:15:25 +01001658out_unreg_param:
1659 module_param_sysfs_remove(mod);
Kay Sievers270a6c42007-01-18 13:26:15 +01001660out_unreg_holders:
Greg Kroah-Hartman78a2d902007-12-20 08:13:05 -08001661 kobject_put(mod->holders_dir);
Kay Sievers270a6c42007-01-18 13:26:15 +01001662out_unreg:
Kay Sieverse17e0f52006-11-24 12:15:25 +01001663 kobject_put(&mod->mkobj.kobj);
Rusty Russell80a3d1b2010-06-05 11:17:36 -06001664out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665 return err;
1666}
Denis V. Lunev34e4e2f2008-05-20 13:59:48 +04001667
1668static void mod_sysfs_fini(struct module *mod)
1669{
Rusty Russell8f6d0372010-08-05 12:59:09 -06001670 remove_notes_attrs(mod);
1671 remove_sect_attrs(mod);
Denis V. Lunev34e4e2f2008-05-20 13:59:48 +04001672 kobject_put(&mod->mkobj.kobj);
1673}
1674
Rusty Russell8f6d0372010-08-05 12:59:09 -06001675#else /* !CONFIG_SYSFS */
Denis V. Lunev34e4e2f2008-05-20 13:59:48 +04001676
Rusty Russell8f6d0372010-08-05 12:59:09 -06001677static int mod_sysfs_setup(struct module *mod,
1678 const struct load_info *info,
Rusty Russell6407ebb22010-06-05 11:17:36 -06001679 struct kernel_param *kparam,
1680 unsigned int num_params)
1681{
1682 return 0;
1683}
1684
Denis V. Lunev34e4e2f2008-05-20 13:59:48 +04001685static void mod_sysfs_fini(struct module *mod)
1686{
1687}
1688
Rusty Russell36b03602010-08-05 12:59:09 -06001689static void module_remove_modinfo_attrs(struct module *mod)
1690{
1691}
1692
Rusty Russell80a3d1b2010-06-05 11:17:36 -06001693static void del_usage_links(struct module *mod)
1694{
1695}
1696
Denis V. Lunev34e4e2f2008-05-20 13:59:48 +04001697#endif /* CONFIG_SYSFS */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698
Rusty Russell36b03602010-08-05 12:59:09 -06001699static void mod_sysfs_teardown(struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700{
Rusty Russell80a3d1b2010-06-05 11:17:36 -06001701 del_usage_links(mod);
Matt Domschc988d2b2005-06-23 22:05:15 -07001702 module_remove_modinfo_attrs(mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703 module_param_sysfs_remove(mod);
Greg Kroah-Hartman78a2d902007-12-20 08:13:05 -08001704 kobject_put(mod->mkobj.drivers_dir);
1705 kobject_put(mod->holders_dir);
Denis V. Lunev34e4e2f2008-05-20 13:59:48 +04001706 mod_sysfs_fini(mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707}
1708
1709/*
1710 * unlink the module with the whole machine is stopped with interrupts off
1711 * - this defends against kallsyms not taking locks
1712 */
1713static int __unlink_module(void *_mod)
1714{
1715 struct module *mod = _mod;
1716 list_del(&mod->list);
Linus Torvalds53363772010-10-05 11:29:27 -07001717 module_bug_cleanup(mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718 return 0;
1719}
1720
matthieu castet84e1c6b2010-11-16 22:35:16 +01001721#ifdef CONFIG_DEBUG_SET_MODULE_RONX
1722/*
1723 * LKM RO/NX protection: protect module's text/ro-data
1724 * from modification and any data from execution.
1725 */
1726void set_page_attributes(void *start, void *end, int (*set)(unsigned long start, int num_pages))
1727{
1728 unsigned long begin_pfn = PFN_DOWN((unsigned long)start);
1729 unsigned long end_pfn = PFN_DOWN((unsigned long)end);
1730
1731 if (end_pfn > begin_pfn)
1732 set(begin_pfn << PAGE_SHIFT, end_pfn - begin_pfn);
1733}
1734
1735static void set_section_ro_nx(void *base,
1736 unsigned long text_size,
1737 unsigned long ro_size,
1738 unsigned long total_size)
1739{
1740 /* begin and end PFNs of the current subsection */
1741 unsigned long begin_pfn;
1742 unsigned long end_pfn;
1743
1744 /*
1745 * Set RO for module text and RO-data:
1746 * - Always protect first page.
1747 * - Do not protect last partial page.
1748 */
1749 if (ro_size > 0)
1750 set_page_attributes(base, base + ro_size, set_memory_ro);
1751
1752 /*
1753 * Set NX permissions for module data:
1754 * - Do not protect first partial page.
1755 * - Always protect last page.
1756 */
1757 if (total_size > text_size) {
1758 begin_pfn = PFN_UP((unsigned long)base + text_size);
1759 end_pfn = PFN_UP((unsigned long)base + total_size);
1760 if (end_pfn > begin_pfn)
1761 set_memory_nx(begin_pfn << PAGE_SHIFT, end_pfn - begin_pfn);
1762 }
1763}
1764
Jan Glauber01526ed2011-05-19 16:55:26 -06001765static void unset_module_core_ro_nx(struct module *mod)
matthieu castet84e1c6b2010-11-16 22:35:16 +01001766{
Jan Glauber01526ed2011-05-19 16:55:26 -06001767 set_page_attributes(mod->module_core + mod->core_text_size,
1768 mod->module_core + mod->core_size,
1769 set_memory_x);
1770 set_page_attributes(mod->module_core,
1771 mod->module_core + mod->core_ro_size,
1772 set_memory_rw);
1773}
1774
1775static void unset_module_init_ro_nx(struct module *mod)
1776{
1777 set_page_attributes(mod->module_init + mod->init_text_size,
1778 mod->module_init + mod->init_size,
1779 set_memory_x);
1780 set_page_attributes(mod->module_init,
1781 mod->module_init + mod->init_ro_size,
1782 set_memory_rw);
matthieu castet84e1c6b2010-11-16 22:35:16 +01001783}
1784
1785/* Iterate through all modules and set each module's text as RW */
Daniel J Blueman5d05c702011-03-08 22:01:47 +08001786void set_all_modules_text_rw(void)
matthieu castet84e1c6b2010-11-16 22:35:16 +01001787{
1788 struct module *mod;
1789
1790 mutex_lock(&module_mutex);
1791 list_for_each_entry_rcu(mod, &modules, list) {
1792 if ((mod->module_core) && (mod->core_text_size)) {
1793 set_page_attributes(mod->module_core,
1794 mod->module_core + mod->core_text_size,
1795 set_memory_rw);
1796 }
1797 if ((mod->module_init) && (mod->init_text_size)) {
1798 set_page_attributes(mod->module_init,
1799 mod->module_init + mod->init_text_size,
1800 set_memory_rw);
1801 }
1802 }
1803 mutex_unlock(&module_mutex);
1804}
1805
1806/* Iterate through all modules and set each module's text as RO */
Daniel J Blueman5d05c702011-03-08 22:01:47 +08001807void set_all_modules_text_ro(void)
matthieu castet84e1c6b2010-11-16 22:35:16 +01001808{
1809 struct module *mod;
1810
1811 mutex_lock(&module_mutex);
1812 list_for_each_entry_rcu(mod, &modules, list) {
1813 if ((mod->module_core) && (mod->core_text_size)) {
1814 set_page_attributes(mod->module_core,
1815 mod->module_core + mod->core_text_size,
1816 set_memory_ro);
1817 }
1818 if ((mod->module_init) && (mod->init_text_size)) {
1819 set_page_attributes(mod->module_init,
1820 mod->module_init + mod->init_text_size,
1821 set_memory_ro);
1822 }
1823 }
1824 mutex_unlock(&module_mutex);
1825}
1826#else
1827static 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 -06001828static void unset_module_core_ro_nx(struct module *mod) { }
1829static void unset_module_init_ro_nx(struct module *mod) { }
matthieu castet84e1c6b2010-11-16 22:35:16 +01001830#endif
1831
Jonas Bonn74e08fc2011-06-30 21:22:11 +02001832void __weak module_free(struct module *mod, void *module_region)
1833{
1834 vfree(module_region);
1835}
1836
1837void __weak module_arch_cleanup(struct module *mod)
1838{
1839}
1840
Rusty Russell75676502010-06-05 11:17:36 -06001841/* Free a module, remove from lists, etc. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842static void free_module(struct module *mod)
1843{
Li Zefan7ead8b82009-08-17 16:56:28 +08001844 trace_module_free(mod);
1845
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846 /* Delete from various lists */
Rusty Russell75676502010-06-05 11:17:36 -06001847 mutex_lock(&module_mutex);
Rusty Russell9b1a4d32008-07-28 12:16:30 -05001848 stop_machine(__unlink_module, mod, NULL);
Rusty Russell75676502010-06-05 11:17:36 -06001849 mutex_unlock(&module_mutex);
Rusty Russell36b03602010-08-05 12:59:09 -06001850 mod_sysfs_teardown(mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851
Jason Baronb82bab4b2010-07-27 13:18:01 -07001852 /* Remove dynamic debug info */
1853 ddebug_remove_module(mod->name);
1854
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855 /* Arch-specific cleanup. */
1856 module_arch_cleanup(mod);
1857
1858 /* Module unload stuff */
1859 module_unload_free(mod);
1860
Rusty Russelle180a6b2009-03-31 13:05:29 -06001861 /* Free any allocated parameters. */
1862 destroy_params(mod->kp, mod->num_kp);
1863
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864 /* This may be NULL, but that's OK */
Jan Glauber01526ed2011-05-19 16:55:26 -06001865 unset_module_init_ro_nx(mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866 module_free(mod, mod->module_init);
1867 kfree(mod->args);
Tejun Heo259354d2010-03-10 18:56:10 +09001868 percpu_modfree(mod);
Rusty Russell9f85a4b2010-08-05 12:59:04 -06001869
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001870 /* Free lock-classes: */
1871 lockdep_free_key_range(mod->module_core, mod->core_size);
1872
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873 /* Finally, free the core (containing the module structure) */
Jan Glauber01526ed2011-05-19 16:55:26 -06001874 unset_module_core_ro_nx(mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875 module_free(mod, mod->module_core);
Bernd Schmidteb8cdec2009-09-21 17:03:57 -07001876
1877#ifdef CONFIG_MPU
1878 update_protections(current->mm);
1879#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880}
1881
1882void *__symbol_get(const char *symbol)
1883{
1884 struct module *owner;
Tim Abbott414fd312008-12-05 19:03:56 -05001885 const struct kernel_symbol *sym;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886
Rusty Russell24da1cb2007-07-15 23:41:46 -07001887 preempt_disable();
Tim Abbott414fd312008-12-05 19:03:56 -05001888 sym = find_symbol(symbol, &owner, NULL, true, true);
1889 if (sym && strong_try_module_get(owner))
1890 sym = NULL;
Rusty Russell24da1cb2007-07-15 23:41:46 -07001891 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892
Tim Abbott414fd312008-12-05 19:03:56 -05001893 return sym ? (void *)sym->value : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894}
1895EXPORT_SYMBOL_GPL(__symbol_get);
1896
Ashutosh Naikeea8b542006-01-08 01:04:25 -08001897/*
1898 * Ensure that an exported symbol [global namespace] does not already exist
Robert P. J. Day02a3e592007-05-09 07:26:28 +02001899 * in the kernel or in some other module's exported symbol table.
Rusty Russellbe593f42010-06-05 11:17:37 -06001900 *
1901 * You must hold the module_mutex.
Ashutosh Naikeea8b542006-01-08 01:04:25 -08001902 */
1903static int verify_export_symbols(struct module *mod)
1904{
Rusty Russellb2111042008-05-01 21:15:00 -05001905 unsigned int i;
Ashutosh Naikeea8b542006-01-08 01:04:25 -08001906 struct module *owner;
Rusty Russellb2111042008-05-01 21:15:00 -05001907 const struct kernel_symbol *s;
1908 struct {
1909 const struct kernel_symbol *sym;
1910 unsigned int num;
1911 } arr[] = {
1912 { mod->syms, mod->num_syms },
1913 { mod->gpl_syms, mod->num_gpl_syms },
1914 { mod->gpl_future_syms, mod->num_gpl_future_syms },
Denys Vlasenkof7f5b672008-07-22 19:24:26 -05001915#ifdef CONFIG_UNUSED_SYMBOLS
Rusty Russellb2111042008-05-01 21:15:00 -05001916 { mod->unused_syms, mod->num_unused_syms },
1917 { mod->unused_gpl_syms, mod->num_unused_gpl_syms },
Denys Vlasenkof7f5b672008-07-22 19:24:26 -05001918#endif
Rusty Russellb2111042008-05-01 21:15:00 -05001919 };
Ashutosh Naikeea8b542006-01-08 01:04:25 -08001920
Rusty Russellb2111042008-05-01 21:15:00 -05001921 for (i = 0; i < ARRAY_SIZE(arr); i++) {
1922 for (s = arr[i].sym; s < arr[i].sym + arr[i].num; s++) {
Rusty Russellbe593f42010-06-05 11:17:37 -06001923 if (find_symbol(s->name, &owner, NULL, true, false)) {
Rusty Russellb2111042008-05-01 21:15:00 -05001924 printk(KERN_ERR
1925 "%s: exports duplicate symbol %s"
1926 " (owned by %s)\n",
1927 mod->name, s->name, module_name(owner));
1928 return -ENOEXEC;
1929 }
Ashutosh Naikeea8b542006-01-08 01:04:25 -08001930 }
Rusty Russellb2111042008-05-01 21:15:00 -05001931 }
1932 return 0;
Ashutosh Naikeea8b542006-01-08 01:04:25 -08001933}
1934
Matti Linnanvuori9a4b9702007-11-08 08:37:38 -08001935/* Change all symbols so that st_value encodes the pointer directly. */
Rusty Russell49668682010-08-05 12:59:10 -06001936static int simplify_symbols(struct module *mod, const struct load_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937{
Rusty Russell49668682010-08-05 12:59:10 -06001938 Elf_Shdr *symsec = &info->sechdrs[info->index.sym];
1939 Elf_Sym *sym = (void *)symsec->sh_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940 unsigned long secbase;
Rusty Russell49668682010-08-05 12:59:10 -06001941 unsigned int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942 int ret = 0;
Tim Abbott414fd312008-12-05 19:03:56 -05001943 const struct kernel_symbol *ksym;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944
Rusty Russell49668682010-08-05 12:59:10 -06001945 for (i = 1; i < symsec->sh_size / sizeof(Elf_Sym); i++) {
1946 const char *name = info->strtab + sym[i].st_name;
1947
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948 switch (sym[i].st_shndx) {
1949 case SHN_COMMON:
1950 /* We compiled with -fno-common. These are not
1951 supposed to happen. */
Jim Cromie5e124162011-12-06 12:11:31 -07001952 pr_debug("Common symbol: %s\n", name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953 printk("%s: please compile with -fno-common\n",
1954 mod->name);
1955 ret = -ENOEXEC;
1956 break;
1957
1958 case SHN_ABS:
1959 /* Don't need to do anything */
Jim Cromie5e124162011-12-06 12:11:31 -07001960 pr_debug("Absolute symbol: 0x%08lx\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961 (long)sym[i].st_value);
1962 break;
1963
1964 case SHN_UNDEF:
Rusty Russell49668682010-08-05 12:59:10 -06001965 ksym = resolve_symbol_wait(mod, info, name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966 /* Ok if resolved. */
Rusty Russell9bea7f22010-06-05 11:17:37 -06001967 if (ksym && !IS_ERR(ksym)) {
Tim Abbott414fd312008-12-05 19:03:56 -05001968 sym[i].st_value = ksym->value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969 break;
Tim Abbott414fd312008-12-05 19:03:56 -05001970 }
1971
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972 /* Ok if weak. */
Rusty Russell9bea7f22010-06-05 11:17:37 -06001973 if (!ksym && ELF_ST_BIND(sym[i].st_info) == STB_WEAK)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974 break;
1975
Rusty Russell9bea7f22010-06-05 11:17:37 -06001976 printk(KERN_WARNING "%s: Unknown symbol %s (err %li)\n",
Rusty Russell49668682010-08-05 12:59:10 -06001977 mod->name, name, PTR_ERR(ksym));
Rusty Russell9bea7f22010-06-05 11:17:37 -06001978 ret = PTR_ERR(ksym) ?: -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979 break;
1980
1981 default:
1982 /* Divert to percpu allocation if a percpu var. */
Rusty Russell49668682010-08-05 12:59:10 -06001983 if (sym[i].st_shndx == info->index.pcpu)
Tejun Heo259354d2010-03-10 18:56:10 +09001984 secbase = (unsigned long)mod_percpu(mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985 else
Rusty Russell49668682010-08-05 12:59:10 -06001986 secbase = info->sechdrs[sym[i].st_shndx].sh_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987 sym[i].st_value += secbase;
1988 break;
1989 }
1990 }
1991
1992 return ret;
1993}
1994
Rusty Russell49668682010-08-05 12:59:10 -06001995static int apply_relocations(struct module *mod, const struct load_info *info)
Rusty Russell22e268e2010-08-05 12:59:05 -06001996{
1997 unsigned int i;
1998 int err = 0;
1999
2000 /* Now do relocations. */
Rusty Russell49668682010-08-05 12:59:10 -06002001 for (i = 1; i < info->hdr->e_shnum; i++) {
2002 unsigned int infosec = info->sechdrs[i].sh_info;
Rusty Russell22e268e2010-08-05 12:59:05 -06002003
2004 /* Not a valid relocation section? */
Rusty Russell49668682010-08-05 12:59:10 -06002005 if (infosec >= info->hdr->e_shnum)
Rusty Russell22e268e2010-08-05 12:59:05 -06002006 continue;
2007
2008 /* Don't bother with non-allocated sections */
Rusty Russell49668682010-08-05 12:59:10 -06002009 if (!(info->sechdrs[infosec].sh_flags & SHF_ALLOC))
Rusty Russell22e268e2010-08-05 12:59:05 -06002010 continue;
2011
Rusty Russell49668682010-08-05 12:59:10 -06002012 if (info->sechdrs[i].sh_type == SHT_REL)
2013 err = apply_relocate(info->sechdrs, info->strtab,
2014 info->index.sym, i, mod);
2015 else if (info->sechdrs[i].sh_type == SHT_RELA)
2016 err = apply_relocate_add(info->sechdrs, info->strtab,
2017 info->index.sym, i, mod);
Rusty Russell22e268e2010-08-05 12:59:05 -06002018 if (err < 0)
2019 break;
2020 }
2021 return err;
2022}
2023
Helge Deller088af9a2008-12-31 12:31:18 +01002024/* Additional bytes needed by arch in front of individual sections */
2025unsigned int __weak arch_mod_section_prepend(struct module *mod,
2026 unsigned int section)
2027{
2028 /* default implementation just returns zero */
2029 return 0;
2030}
2031
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032/* Update size with this section: return offset. */
Helge Deller088af9a2008-12-31 12:31:18 +01002033static long get_offset(struct module *mod, unsigned int *size,
2034 Elf_Shdr *sechdr, unsigned int section)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035{
2036 long ret;
2037
Helge Deller088af9a2008-12-31 12:31:18 +01002038 *size += arch_mod_section_prepend(mod, section);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039 ret = ALIGN(*size, sechdr->sh_addralign ?: 1);
2040 *size = ret + sechdr->sh_size;
2041 return ret;
2042}
2043
2044/* Lay out the SHF_ALLOC sections in a way not dissimilar to how ld
2045 might -- code, read-only data, read-write data, small data. Tally
2046 sizes, and place the offsets into sh_entsize fields: high bit means it
2047 belongs in init. */
Rusty Russell49668682010-08-05 12:59:10 -06002048static void layout_sections(struct module *mod, struct load_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049{
2050 static unsigned long const masks[][2] = {
2051 /* NOTE: all executable code must be the first section
2052 * in this array; otherwise modify the text_size
2053 * finder in the two loops below */
2054 { SHF_EXECINSTR | SHF_ALLOC, ARCH_SHF_SMALL },
2055 { SHF_ALLOC, SHF_WRITE | ARCH_SHF_SMALL },
2056 { SHF_WRITE | SHF_ALLOC, ARCH_SHF_SMALL },
2057 { ARCH_SHF_SMALL | SHF_ALLOC, 0 }
2058 };
2059 unsigned int m, i;
2060
Rusty Russell49668682010-08-05 12:59:10 -06002061 for (i = 0; i < info->hdr->e_shnum; i++)
2062 info->sechdrs[i].sh_entsize = ~0UL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063
Jim Cromie5e124162011-12-06 12:11:31 -07002064 pr_debug("Core section allocation order:\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065 for (m = 0; m < ARRAY_SIZE(masks); ++m) {
Rusty Russell49668682010-08-05 12:59:10 -06002066 for (i = 0; i < info->hdr->e_shnum; ++i) {
2067 Elf_Shdr *s = &info->sechdrs[i];
2068 const char *sname = info->secstrings + s->sh_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069
2070 if ((s->sh_flags & masks[m][0]) != masks[m][0]
2071 || (s->sh_flags & masks[m][1])
2072 || s->sh_entsize != ~0UL
Rusty Russell49668682010-08-05 12:59:10 -06002073 || strstarts(sname, ".init"))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074 continue;
Helge Deller088af9a2008-12-31 12:31:18 +01002075 s->sh_entsize = get_offset(mod, &mod->core_size, s, i);
Jim Cromie5e124162011-12-06 12:11:31 -07002076 pr_debug("\t%s\n", sname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077 }
matthieu castet84e1c6b2010-11-16 22:35:16 +01002078 switch (m) {
2079 case 0: /* executable */
2080 mod->core_size = debug_align(mod->core_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081 mod->core_text_size = mod->core_size;
matthieu castet84e1c6b2010-11-16 22:35:16 +01002082 break;
2083 case 1: /* RO: text and ro-data */
2084 mod->core_size = debug_align(mod->core_size);
2085 mod->core_ro_size = mod->core_size;
2086 break;
2087 case 3: /* whole core */
2088 mod->core_size = debug_align(mod->core_size);
2089 break;
2090 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091 }
2092
Jim Cromie5e124162011-12-06 12:11:31 -07002093 pr_debug("Init section allocation order:\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094 for (m = 0; m < ARRAY_SIZE(masks); ++m) {
Rusty Russell49668682010-08-05 12:59:10 -06002095 for (i = 0; i < info->hdr->e_shnum; ++i) {
2096 Elf_Shdr *s = &info->sechdrs[i];
2097 const char *sname = info->secstrings + s->sh_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098
2099 if ((s->sh_flags & masks[m][0]) != masks[m][0]
2100 || (s->sh_flags & masks[m][1])
2101 || s->sh_entsize != ~0UL
Rusty Russell49668682010-08-05 12:59:10 -06002102 || !strstarts(sname, ".init"))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103 continue;
Helge Deller088af9a2008-12-31 12:31:18 +01002104 s->sh_entsize = (get_offset(mod, &mod->init_size, s, i)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105 | INIT_OFFSET_MASK);
Jim Cromie5e124162011-12-06 12:11:31 -07002106 pr_debug("\t%s\n", sname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107 }
matthieu castet84e1c6b2010-11-16 22:35:16 +01002108 switch (m) {
2109 case 0: /* executable */
2110 mod->init_size = debug_align(mod->init_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111 mod->init_text_size = mod->init_size;
matthieu castet84e1c6b2010-11-16 22:35:16 +01002112 break;
2113 case 1: /* RO: text and ro-data */
2114 mod->init_size = debug_align(mod->init_size);
2115 mod->init_ro_size = mod->init_size;
2116 break;
2117 case 3: /* whole init */
2118 mod->init_size = debug_align(mod->init_size);
2119 break;
2120 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121 }
2122}
2123
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124static void set_license(struct module *mod, const char *license)
2125{
2126 if (!license)
2127 license = "unspecified";
2128
Florin Malitafa3ba2e82006-10-11 01:21:48 -07002129 if (!license_is_gpl_compatible(license)) {
Andi Kleen25ddbb12008-10-15 22:01:41 -07002130 if (!test_taint(TAINT_PROPRIETARY_MODULE))
Jan Dittmer1d4d2622006-10-28 10:38:38 -07002131 printk(KERN_WARNING "%s: module license '%s' taints "
Florin Malitafa3ba2e82006-10-11 01:21:48 -07002132 "kernel.\n", mod->name, license);
2133 add_taint_module(mod, TAINT_PROPRIETARY_MODULE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002134 }
2135}
2136
2137/* Parse tag=value strings from .modinfo section */
2138static char *next_string(char *string, unsigned long *secsize)
2139{
2140 /* Skip non-zero chars */
2141 while (string[0]) {
2142 string++;
2143 if ((*secsize)-- <= 1)
2144 return NULL;
2145 }
2146
2147 /* Skip any zero padding. */
2148 while (!string[0]) {
2149 string++;
2150 if ((*secsize)-- <= 1)
2151 return NULL;
2152 }
2153 return string;
2154}
2155
Rusty Russell49668682010-08-05 12:59:10 -06002156static char *get_modinfo(struct load_info *info, const char *tag)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157{
2158 char *p;
2159 unsigned int taglen = strlen(tag);
Rusty Russell49668682010-08-05 12:59:10 -06002160 Elf_Shdr *infosec = &info->sechdrs[info->index.info];
2161 unsigned long size = infosec->sh_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162
Rusty Russell49668682010-08-05 12:59:10 -06002163 for (p = (char *)infosec->sh_addr; p; p = next_string(p, &size)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164 if (strncmp(p, tag, taglen) == 0 && p[taglen] == '=')
2165 return p + taglen + 1;
2166 }
2167 return NULL;
2168}
2169
Rusty Russell49668682010-08-05 12:59:10 -06002170static void setup_modinfo(struct module *mod, struct load_info *info)
Matt Domschc988d2b2005-06-23 22:05:15 -07002171{
2172 struct module_attribute *attr;
2173 int i;
2174
2175 for (i = 0; (attr = modinfo_attrs[i]); i++) {
2176 if (attr->setup)
Rusty Russell49668682010-08-05 12:59:10 -06002177 attr->setup(mod, get_modinfo(info, attr->attr.name));
Matt Domschc988d2b2005-06-23 22:05:15 -07002178 }
2179}
Matt Domschc988d2b2005-06-23 22:05:15 -07002180
Rusty Russella263f772009-09-25 00:32:58 -06002181static void free_modinfo(struct module *mod)
2182{
2183 struct module_attribute *attr;
2184 int i;
2185
2186 for (i = 0; (attr = modinfo_attrs[i]); i++) {
2187 if (attr->free)
2188 attr->free(mod);
2189 }
2190}
2191
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192#ifdef CONFIG_KALLSYMS
WANG Cong15bba372008-07-24 15:41:48 +01002193
2194/* lookup symbol in given range of kernel_symbols */
2195static const struct kernel_symbol *lookup_symbol(const char *name,
2196 const struct kernel_symbol *start,
2197 const struct kernel_symbol *stop)
2198{
Alessio Igor Bogani9d634872011-05-18 22:35:59 +02002199 return bsearch(name, start, stop - start,
2200 sizeof(struct kernel_symbol), cmp_name);
WANG Cong15bba372008-07-24 15:41:48 +01002201}
2202
Tim Abbottca4787b2009-01-05 08:40:10 -06002203static int is_exported(const char *name, unsigned long value,
2204 const struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002205{
Tim Abbottca4787b2009-01-05 08:40:10 -06002206 const struct kernel_symbol *ks;
2207 if (!mod)
2208 ks = lookup_symbol(name, __start___ksymtab, __stop___ksymtab);
Sam Ravnborg3fd68052006-02-08 21:16:45 +01002209 else
Tim Abbottca4787b2009-01-05 08:40:10 -06002210 ks = lookup_symbol(name, mod->syms, mod->syms + mod->num_syms);
2211 return ks != NULL && ks->value == value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212}
2213
2214/* As per nm */
Rusty Russelleded41c2010-08-05 12:59:07 -06002215static char elf_type(const Elf_Sym *sym, const struct load_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002216{
Rusty Russelleded41c2010-08-05 12:59:07 -06002217 const Elf_Shdr *sechdrs = info->sechdrs;
2218
Linus Torvalds1da177e2005-04-16 15:20:36 -07002219 if (ELF_ST_BIND(sym->st_info) == STB_WEAK) {
2220 if (ELF_ST_TYPE(sym->st_info) == STT_OBJECT)
2221 return 'v';
2222 else
2223 return 'w';
2224 }
2225 if (sym->st_shndx == SHN_UNDEF)
2226 return 'U';
2227 if (sym->st_shndx == SHN_ABS)
2228 return 'a';
2229 if (sym->st_shndx >= SHN_LORESERVE)
2230 return '?';
2231 if (sechdrs[sym->st_shndx].sh_flags & SHF_EXECINSTR)
2232 return 't';
2233 if (sechdrs[sym->st_shndx].sh_flags & SHF_ALLOC
2234 && sechdrs[sym->st_shndx].sh_type != SHT_NOBITS) {
2235 if (!(sechdrs[sym->st_shndx].sh_flags & SHF_WRITE))
2236 return 'r';
2237 else if (sechdrs[sym->st_shndx].sh_flags & ARCH_SHF_SMALL)
2238 return 'g';
2239 else
2240 return 'd';
2241 }
2242 if (sechdrs[sym->st_shndx].sh_type == SHT_NOBITS) {
2243 if (sechdrs[sym->st_shndx].sh_flags & ARCH_SHF_SMALL)
2244 return 's';
2245 else
2246 return 'b';
2247 }
Rusty Russelleded41c2010-08-05 12:59:07 -06002248 if (strstarts(info->secstrings + sechdrs[sym->st_shndx].sh_name,
2249 ".debug")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002250 return 'n';
Rusty Russelleded41c2010-08-05 12:59:07 -06002251 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252 return '?';
2253}
2254
Jan Beulich4a496222009-07-06 14:50:42 +01002255static bool is_core_symbol(const Elf_Sym *src, const Elf_Shdr *sechdrs,
2256 unsigned int shnum)
2257{
2258 const Elf_Shdr *sec;
2259
2260 if (src->st_shndx == SHN_UNDEF
2261 || src->st_shndx >= shnum
2262 || !src->st_name)
2263 return false;
2264
2265 sec = sechdrs + src->st_shndx;
2266 if (!(sec->sh_flags & SHF_ALLOC)
2267#ifndef CONFIG_KALLSYMS_ALL
2268 || !(sec->sh_flags & SHF_EXECINSTR)
2269#endif
2270 || (sec->sh_entsize & INIT_OFFSET_MASK))
2271 return false;
2272
2273 return true;
2274}
2275
Kevin Cernekee48fd1182012-01-13 09:32:14 +10302276/*
2277 * We only allocate and copy the strings needed by the parts of symtab
2278 * we keep. This is simple, but has the effect of making multiple
2279 * copies of duplicates. We could be more sophisticated, see
2280 * linux-kernel thread starting with
2281 * <73defb5e4bca04a6431392cc341112b1@localhost>.
2282 */
Rusty Russell49668682010-08-05 12:59:10 -06002283static void layout_symtab(struct module *mod, struct load_info *info)
Jan Beulich4a496222009-07-06 14:50:42 +01002284{
Rusty Russell49668682010-08-05 12:59:10 -06002285 Elf_Shdr *symsect = info->sechdrs + info->index.sym;
2286 Elf_Shdr *strsect = info->sechdrs + info->index.str;
Jan Beulich4a496222009-07-06 14:50:42 +01002287 const Elf_Sym *src;
Satoru Takeuchi54523ec2012-12-05 12:29:04 +10302288 unsigned int i, nsrc, ndst, strtab_size = 0;
Jan Beulich4a496222009-07-06 14:50:42 +01002289
2290 /* Put symbol section at end of init part of module. */
2291 symsect->sh_flags |= SHF_ALLOC;
2292 symsect->sh_entsize = get_offset(mod, &mod->init_size, symsect,
Rusty Russell49668682010-08-05 12:59:10 -06002293 info->index.sym) | INIT_OFFSET_MASK;
Jim Cromie5e124162011-12-06 12:11:31 -07002294 pr_debug("\t%s\n", info->secstrings + symsect->sh_name);
Jan Beulich4a496222009-07-06 14:50:42 +01002295
Rusty Russell49668682010-08-05 12:59:10 -06002296 src = (void *)info->hdr + symsect->sh_offset;
Jan Beulich4a496222009-07-06 14:50:42 +01002297 nsrc = symsect->sh_size / sizeof(*src);
Kevin Cernekee70b1e9162011-11-12 19:08:55 -08002298
Kevin Cernekee48fd1182012-01-13 09:32:14 +10302299 /* Compute total space required for the core symbols' strtab. */
Rusty Russell59ef28b2012-10-25 10:49:25 +10302300 for (ndst = i = 0; i < nsrc; i++) {
2301 if (i == 0 ||
2302 is_core_symbol(src+i, info->sechdrs, info->hdr->e_shnum)) {
2303 strtab_size += strlen(&info->strtab[src[i].st_name])+1;
Kevin Cernekee48fd1182012-01-13 09:32:14 +10302304 ndst++;
Jan Beulich554bdfe2009-07-06 14:51:44 +01002305 }
Rusty Russell59ef28b2012-10-25 10:49:25 +10302306 }
Jan Beulich4a496222009-07-06 14:50:42 +01002307
2308 /* Append room for core symbols at end of core part. */
Rusty Russell49668682010-08-05 12:59:10 -06002309 info->symoffs = ALIGN(mod->core_size, symsect->sh_addralign ?: 1);
Kevin Cernekee48fd1182012-01-13 09:32:14 +10302310 info->stroffs = mod->core_size = info->symoffs + ndst * sizeof(Elf_Sym);
2311 mod->core_size += strtab_size;
Jan Beulich4a496222009-07-06 14:50:42 +01002312
Jan Beulich554bdfe2009-07-06 14:51:44 +01002313 /* Put string table section at end of init part of module. */
2314 strsect->sh_flags |= SHF_ALLOC;
2315 strsect->sh_entsize = get_offset(mod, &mod->init_size, strsect,
Rusty Russell49668682010-08-05 12:59:10 -06002316 info->index.str) | INIT_OFFSET_MASK;
Jim Cromie5e124162011-12-06 12:11:31 -07002317 pr_debug("\t%s\n", info->secstrings + strsect->sh_name);
Jan Beulich4a496222009-07-06 14:50:42 +01002318}
2319
Rusty Russell811d66a2010-08-05 12:59:12 -06002320static void add_kallsyms(struct module *mod, const struct load_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002321{
Jan Beulich4a496222009-07-06 14:50:42 +01002322 unsigned int i, ndst;
2323 const Elf_Sym *src;
2324 Elf_Sym *dst;
Jan Beulich554bdfe2009-07-06 14:51:44 +01002325 char *s;
Rusty Russelleded41c2010-08-05 12:59:07 -06002326 Elf_Shdr *symsec = &info->sechdrs[info->index.sym];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002327
Rusty Russelleded41c2010-08-05 12:59:07 -06002328 mod->symtab = (void *)symsec->sh_addr;
2329 mod->num_symtab = symsec->sh_size / sizeof(Elf_Sym);
Rusty Russell511ca6a2010-08-05 12:59:08 -06002330 /* Make sure we get permanent strtab: don't use info->strtab. */
2331 mod->strtab = (void *)info->sechdrs[info->index.str].sh_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002332
2333 /* Set types up while we still have access to sections. */
2334 for (i = 0; i < mod->num_symtab; i++)
Rusty Russelleded41c2010-08-05 12:59:07 -06002335 mod->symtab[i].st_info = elf_type(&mod->symtab[i], info);
Jan Beulich4a496222009-07-06 14:50:42 +01002336
Rusty Russelld9131882010-08-05 12:59:08 -06002337 mod->core_symtab = dst = mod->module_core + info->symoffs;
Kevin Cernekee48fd1182012-01-13 09:32:14 +10302338 mod->core_strtab = s = mod->module_core + info->stroffs;
Jan Beulich4a496222009-07-06 14:50:42 +01002339 src = mod->symtab;
Rusty Russell59ef28b2012-10-25 10:49:25 +10302340 for (ndst = i = 0; i < mod->num_symtab; i++) {
2341 if (i == 0 ||
2342 is_core_symbol(src+i, info->sechdrs, info->hdr->e_shnum)) {
2343 dst[ndst] = src[i];
2344 dst[ndst++].st_name = s - mod->core_strtab;
2345 s += strlcpy(s, &mod->strtab[src[i].st_name],
2346 KSYM_NAME_LEN) + 1;
2347 }
Jan Beulich4a496222009-07-06 14:50:42 +01002348 }
2349 mod->core_num_syms = ndst;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002350}
2351#else
Rusty Russell49668682010-08-05 12:59:10 -06002352static inline void layout_symtab(struct module *mod, struct load_info *info)
Jan Beulich4a496222009-07-06 14:50:42 +01002353{
2354}
Paul Mundt3ae91c22009-10-01 15:43:54 -07002355
Michał Mirosławabbce902010-09-20 01:58:08 +02002356static void add_kallsyms(struct module *mod, const struct load_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002357{
2358}
2359#endif /* CONFIG_KALLSYMS */
2360
Jason Barone9d376f2009-02-05 11:51:38 -05002361static void dynamic_debug_setup(struct _ddebug *debug, unsigned int num)
Rusty Russell5e458cc2008-10-22 10:00:13 -05002362{
Rusty Russell811d66a2010-08-05 12:59:12 -06002363 if (!debug)
2364 return;
Jason Barone9d376f2009-02-05 11:51:38 -05002365#ifdef CONFIG_DYNAMIC_DEBUG
2366 if (ddebug_add_module(debug, num, debug->modname))
2367 printk(KERN_ERR "dynamic debug error adding module: %s\n",
2368 debug->modname);
2369#endif
Rusty Russell5e458cc2008-10-22 10:00:13 -05002370}
Jason Baron346e15b2008-08-12 16:46:19 -04002371
Yehuda Sadehff49d742010-07-03 13:07:35 +10002372static void dynamic_debug_remove(struct _ddebug *debug)
2373{
2374 if (debug)
2375 ddebug_remove_module(debug->modname);
2376}
2377
Jonas Bonn74e08fc2011-06-30 21:22:11 +02002378void * __weak module_alloc(unsigned long size)
2379{
Rusty Russell82fab442012-12-11 09:38:33 +10302380 return vmalloc_exec(size);
Jonas Bonn74e08fc2011-06-30 21:22:11 +02002381}
2382
Rusty Russell3a642e92008-07-22 19:24:28 -05002383static void *module_alloc_update_bounds(unsigned long size)
2384{
2385 void *ret = module_alloc(size);
2386
2387 if (ret) {
Rusty Russell75676502010-06-05 11:17:36 -06002388 mutex_lock(&module_mutex);
Rusty Russell3a642e92008-07-22 19:24:28 -05002389 /* Update module bounds. */
2390 if ((unsigned long)ret < module_addr_min)
2391 module_addr_min = (unsigned long)ret;
2392 if ((unsigned long)ret + size > module_addr_max)
2393 module_addr_max = (unsigned long)ret + size;
Rusty Russell75676502010-06-05 11:17:36 -06002394 mutex_unlock(&module_mutex);
Rusty Russell3a642e92008-07-22 19:24:28 -05002395 }
2396 return ret;
2397}
2398
Catalin Marinas4f2294b2009-06-11 13:23:20 +01002399#ifdef CONFIG_DEBUG_KMEMLEAK
Rusty Russell49668682010-08-05 12:59:10 -06002400static void kmemleak_load_module(const struct module *mod,
2401 const struct load_info *info)
Catalin Marinas4f2294b2009-06-11 13:23:20 +01002402{
2403 unsigned int i;
2404
2405 /* only scan the sections containing data */
Catalin Marinasc017b4b2009-10-28 13:33:09 +00002406 kmemleak_scan_area(mod, sizeof(struct module), GFP_KERNEL);
Catalin Marinas4f2294b2009-06-11 13:23:20 +01002407
Rusty Russell49668682010-08-05 12:59:10 -06002408 for (i = 1; i < info->hdr->e_shnum; i++) {
2409 const char *name = info->secstrings + info->sechdrs[i].sh_name;
2410 if (!(info->sechdrs[i].sh_flags & SHF_ALLOC))
Catalin Marinas4f2294b2009-06-11 13:23:20 +01002411 continue;
Rusty Russell49668682010-08-05 12:59:10 -06002412 if (!strstarts(name, ".data") && !strstarts(name, ".bss"))
Catalin Marinas4f2294b2009-06-11 13:23:20 +01002413 continue;
2414
Rusty Russell49668682010-08-05 12:59:10 -06002415 kmemleak_scan_area((void *)info->sechdrs[i].sh_addr,
2416 info->sechdrs[i].sh_size, GFP_KERNEL);
Catalin Marinas4f2294b2009-06-11 13:23:20 +01002417 }
2418}
2419#else
Rusty Russell49668682010-08-05 12:59:10 -06002420static inline void kmemleak_load_module(const struct module *mod,
2421 const struct load_info *info)
Catalin Marinas4f2294b2009-06-11 13:23:20 +01002422{
2423}
2424#endif
2425
Rusty Russell106a4ee2012-09-26 10:09:40 +01002426#ifdef CONFIG_MODULE_SIG
Kees Cook34e11692012-10-16 07:31:07 +10302427static int module_sig_check(struct load_info *info)
Rusty Russell106a4ee2012-09-26 10:09:40 +01002428{
2429 int err = -ENOKEY;
Kees Cook34e11692012-10-16 07:31:07 +10302430 const unsigned long markerlen = sizeof(MODULE_SIG_STRING) - 1;
2431 const void *mod = info->hdr;
Rusty Russell106a4ee2012-09-26 10:09:40 +01002432
Kees Cook34e11692012-10-16 07:31:07 +10302433 if (info->len > markerlen &&
2434 memcmp(mod + info->len - markerlen, MODULE_SIG_STRING, markerlen) == 0) {
David Howellscaabe242012-10-20 01:19:29 +01002435 /* We truncate the module to discard the signature */
Kees Cook34e11692012-10-16 07:31:07 +10302436 info->len -= markerlen;
2437 err = mod_verify_sig(mod, &info->len);
Rusty Russell106a4ee2012-09-26 10:09:40 +01002438 }
2439
2440 if (!err) {
2441 info->sig_ok = true;
2442 return 0;
2443 }
2444
2445 /* Not having a signature is only an error if we're strict. */
David Howells1d0059f2012-09-26 10:09:50 +01002446 if (err < 0 && fips_enabled)
2447 panic("Module verification failed with error %d in FIPS mode\n",
2448 err);
Rusty Russell106a4ee2012-09-26 10:09:40 +01002449 if (err == -ENOKEY && !sig_enforce)
2450 err = 0;
2451
2452 return err;
2453}
2454#else /* !CONFIG_MODULE_SIG */
Kees Cook34e11692012-10-16 07:31:07 +10302455static int module_sig_check(struct load_info *info)
Rusty Russell106a4ee2012-09-26 10:09:40 +01002456{
2457 return 0;
2458}
2459#endif /* !CONFIG_MODULE_SIG */
2460
Kees Cook34e11692012-10-16 07:31:07 +10302461/* Sanity checks against invalid binaries, wrong arch, weird elf version. */
2462static int elf_header_check(struct load_info *info)
Rusty Russell40dd2562010-08-05 12:59:03 -06002463{
Kees Cook34e11692012-10-16 07:31:07 +10302464 if (info->len < sizeof(*(info->hdr)))
2465 return -ENOEXEC;
Rusty Russell40dd2562010-08-05 12:59:03 -06002466
Kees Cook34e11692012-10-16 07:31:07 +10302467 if (memcmp(info->hdr->e_ident, ELFMAG, SELFMAG) != 0
2468 || info->hdr->e_type != ET_REL
2469 || !elf_check_arch(info->hdr)
2470 || info->hdr->e_shentsize != sizeof(Elf_Shdr))
2471 return -ENOEXEC;
2472
2473 if (info->hdr->e_shoff >= info->len
2474 || (info->hdr->e_shnum * sizeof(Elf_Shdr) >
2475 info->len - info->hdr->e_shoff))
2476 return -ENOEXEC;
2477
2478 return 0;
2479}
2480
2481/* Sets info->hdr and info->len. */
2482static int copy_module_from_user(const void __user *umod, unsigned long len,
2483 struct load_info *info)
2484{
Kees Cook2e72d512012-10-16 07:32:07 +10302485 int err;
2486
Kees Cook34e11692012-10-16 07:31:07 +10302487 info->len = len;
2488 if (info->len < sizeof(*(info->hdr)))
Rusty Russell40dd2562010-08-05 12:59:03 -06002489 return -ENOEXEC;
2490
Kees Cook2e72d512012-10-16 07:32:07 +10302491 err = security_kernel_module_from_file(NULL);
2492 if (err)
2493 return err;
2494
Rusty Russell40dd2562010-08-05 12:59:03 -06002495 /* Suck in entire file: we'll want most of it. */
Kees Cook34e11692012-10-16 07:31:07 +10302496 info->hdr = vmalloc(info->len);
2497 if (!info->hdr)
Rusty Russell40dd2562010-08-05 12:59:03 -06002498 return -ENOMEM;
2499
Kees Cook34e11692012-10-16 07:31:07 +10302500 if (copy_from_user(info->hdr, umod, info->len) != 0) {
2501 vfree(info->hdr);
2502 return -EFAULT;
Rusty Russell40dd2562010-08-05 12:59:03 -06002503 }
2504
Rusty Russell40dd2562010-08-05 12:59:03 -06002505 return 0;
Kees Cook34e11692012-10-16 07:31:07 +10302506}
Rusty Russell40dd2562010-08-05 12:59:03 -06002507
Kees Cook34e11692012-10-16 07:31:07 +10302508/* Sets info->hdr and info->len. */
2509static int copy_module_from_fd(int fd, struct load_info *info)
2510{
2511 struct file *file;
2512 int err;
2513 struct kstat stat;
2514 loff_t pos;
2515 ssize_t bytes = 0;
2516
2517 file = fget(fd);
2518 if (!file)
2519 return -ENOEXEC;
2520
Kees Cook2e72d512012-10-16 07:32:07 +10302521 err = security_kernel_module_from_file(file);
2522 if (err)
2523 goto out;
2524
Kees Cook34e11692012-10-16 07:31:07 +10302525 err = vfs_getattr(file->f_vfsmnt, file->f_dentry, &stat);
2526 if (err)
2527 goto out;
2528
2529 if (stat.size > INT_MAX) {
2530 err = -EFBIG;
2531 goto out;
2532 }
2533 info->hdr = vmalloc(stat.size);
2534 if (!info->hdr) {
2535 err = -ENOMEM;
2536 goto out;
2537 }
2538
2539 pos = 0;
2540 while (pos < stat.size) {
2541 bytes = kernel_read(file, pos, (char *)(info->hdr) + pos,
2542 stat.size - pos);
2543 if (bytes < 0) {
2544 vfree(info->hdr);
2545 err = bytes;
2546 goto out;
2547 }
2548 if (bytes == 0)
2549 break;
2550 pos += bytes;
2551 }
2552 info->len = pos;
2553
2554out:
2555 fput(file);
Rusty Russell40dd2562010-08-05 12:59:03 -06002556 return err;
2557}
2558
Rusty Russelld9131882010-08-05 12:59:08 -06002559static void free_copy(struct load_info *info)
2560{
Rusty Russelld9131882010-08-05 12:59:08 -06002561 vfree(info->hdr);
2562}
2563
Rusty Russell2f3238a2012-10-22 18:09:41 +10302564static int rewrite_section_headers(struct load_info *info, int flags)
Rusty Russell8b5f61a2010-08-05 12:59:06 -06002565{
2566 unsigned int i;
2567
2568 /* This should always be true, but let's be sure. */
2569 info->sechdrs[0].sh_addr = 0;
2570
2571 for (i = 1; i < info->hdr->e_shnum; i++) {
2572 Elf_Shdr *shdr = &info->sechdrs[i];
2573 if (shdr->sh_type != SHT_NOBITS
2574 && info->len < shdr->sh_offset + shdr->sh_size) {
2575 printk(KERN_ERR "Module len %lu truncated\n",
2576 info->len);
2577 return -ENOEXEC;
2578 }
2579
2580 /* Mark all sections sh_addr with their address in the
2581 temporary image. */
2582 shdr->sh_addr = (size_t)info->hdr + shdr->sh_offset;
2583
2584#ifndef CONFIG_MODULE_UNLOAD
2585 /* Don't load .exit sections */
2586 if (strstarts(info->secstrings+shdr->sh_name, ".exit"))
2587 shdr->sh_flags &= ~(unsigned long)SHF_ALLOC;
2588#endif
Rusty Russell8b5f61a2010-08-05 12:59:06 -06002589 }
Rusty Russelld6df72a2010-08-05 12:59:07 -06002590
2591 /* Track but don't keep modinfo and version sections. */
Rusty Russell2f3238a2012-10-22 18:09:41 +10302592 if (flags & MODULE_INIT_IGNORE_MODVERSIONS)
2593 info->index.vers = 0; /* Pretend no __versions section! */
2594 else
2595 info->index.vers = find_sec(info, "__versions");
Rusty Russell49668682010-08-05 12:59:10 -06002596 info->index.info = find_sec(info, ".modinfo");
Rusty Russelld6df72a2010-08-05 12:59:07 -06002597 info->sechdrs[info->index.info].sh_flags &= ~(unsigned long)SHF_ALLOC;
2598 info->sechdrs[info->index.vers].sh_flags &= ~(unsigned long)SHF_ALLOC;
Rusty Russell8b5f61a2010-08-05 12:59:06 -06002599 return 0;
2600}
2601
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002602/*
2603 * Set up our basic convenience variables (pointers to section headers,
2604 * search for module section index etc), and do some basic section
2605 * verification.
2606 *
2607 * Return the temporary module pointer (we'll replace it with the final
2608 * one when we move the module sections around).
2609 */
Rusty Russell2f3238a2012-10-22 18:09:41 +10302610static struct module *setup_load_info(struct load_info *info, int flags)
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002611{
2612 unsigned int i;
Rusty Russell8b5f61a2010-08-05 12:59:06 -06002613 int err;
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002614 struct module *mod;
2615
2616 /* Set up the convenience variables */
2617 info->sechdrs = (void *)info->hdr + info->hdr->e_shoff;
Rusty Russell8b5f61a2010-08-05 12:59:06 -06002618 info->secstrings = (void *)info->hdr
2619 + info->sechdrs[info->hdr->e_shstrndx].sh_offset;
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002620
Rusty Russell2f3238a2012-10-22 18:09:41 +10302621 err = rewrite_section_headers(info, flags);
Rusty Russell8b5f61a2010-08-05 12:59:06 -06002622 if (err)
2623 return ERR_PTR(err);
2624
2625 /* Find internal symbols and strings. */
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002626 for (i = 1; i < info->hdr->e_shnum; i++) {
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002627 if (info->sechdrs[i].sh_type == SHT_SYMTAB) {
2628 info->index.sym = i;
2629 info->index.str = info->sechdrs[i].sh_link;
Rusty Russell8b5f61a2010-08-05 12:59:06 -06002630 info->strtab = (char *)info->hdr
2631 + info->sechdrs[info->index.str].sh_offset;
2632 break;
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002633 }
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002634 }
2635
Rusty Russell49668682010-08-05 12:59:10 -06002636 info->index.mod = find_sec(info, ".gnu.linkonce.this_module");
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002637 if (!info->index.mod) {
2638 printk(KERN_WARNING "No module found in object\n");
2639 return ERR_PTR(-ENOEXEC);
2640 }
2641 /* This is temporary: point mod into copy of data. */
2642 mod = (void *)info->sechdrs[info->index.mod].sh_addr;
2643
2644 if (info->index.sym == 0) {
2645 printk(KERN_WARNING "%s: module has no symbols (stripped?)\n",
2646 mod->name);
2647 return ERR_PTR(-ENOEXEC);
2648 }
2649
Rusty Russell49668682010-08-05 12:59:10 -06002650 info->index.pcpu = find_pcpusec(info);
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002651
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002652 /* Check module struct version now, before we try to use module. */
2653 if (!check_modstruct_version(info->sechdrs, info->index.vers, mod))
2654 return ERR_PTR(-ENOEXEC);
2655
2656 return mod;
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002657}
2658
Rusty Russell2f3238a2012-10-22 18:09:41 +10302659static int check_modinfo(struct module *mod, struct load_info *info, int flags)
Rusty Russell40dd2562010-08-05 12:59:03 -06002660{
Rusty Russell49668682010-08-05 12:59:10 -06002661 const char *modmagic = get_modinfo(info, "vermagic");
Rusty Russell40dd2562010-08-05 12:59:03 -06002662 int err;
2663
Rusty Russell2f3238a2012-10-22 18:09:41 +10302664 if (flags & MODULE_INIT_IGNORE_VERMAGIC)
2665 modmagic = NULL;
2666
Rusty Russell40dd2562010-08-05 12:59:03 -06002667 /* This is allowed: modprobe --force will invalidate it. */
2668 if (!modmagic) {
2669 err = try_to_force_load(mod, "bad vermagic");
2670 if (err)
2671 return err;
Rusty Russell49668682010-08-05 12:59:10 -06002672 } else if (!same_magic(modmagic, vermagic, info->index.vers)) {
Rusty Russell40dd2562010-08-05 12:59:03 -06002673 printk(KERN_ERR "%s: version magic '%s' should be '%s'\n",
2674 mod->name, modmagic, vermagic);
2675 return -ENOEXEC;
2676 }
2677
Ben Hutchings2449b8b2011-10-24 15:12:28 +02002678 if (!get_modinfo(info, "intree"))
2679 add_taint_module(mod, TAINT_OOT_MODULE);
2680
Rusty Russell49668682010-08-05 12:59:10 -06002681 if (get_modinfo(info, "staging")) {
Rusty Russell40dd2562010-08-05 12:59:03 -06002682 add_taint_module(mod, TAINT_CRAP);
2683 printk(KERN_WARNING "%s: module is from the staging directory,"
2684 " the quality is unknown, you have been warned.\n",
2685 mod->name);
2686 }
Rusty Russell22e268e2010-08-05 12:59:05 -06002687
2688 /* Set up license info based on the info section */
Rusty Russell49668682010-08-05 12:59:10 -06002689 set_license(mod, get_modinfo(info, "license"));
Rusty Russell22e268e2010-08-05 12:59:05 -06002690
Rusty Russell40dd2562010-08-05 12:59:03 -06002691 return 0;
2692}
2693
Rusty Russell811d66a2010-08-05 12:59:12 -06002694static void find_module_sections(struct module *mod, struct load_info *info)
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06002695{
Rusty Russell49668682010-08-05 12:59:10 -06002696 mod->kp = section_objs(info, "__param",
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06002697 sizeof(*mod->kp), &mod->num_kp);
Rusty Russell49668682010-08-05 12:59:10 -06002698 mod->syms = section_objs(info, "__ksymtab",
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06002699 sizeof(*mod->syms), &mod->num_syms);
Rusty Russell49668682010-08-05 12:59:10 -06002700 mod->crcs = section_addr(info, "__kcrctab");
2701 mod->gpl_syms = section_objs(info, "__ksymtab_gpl",
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06002702 sizeof(*mod->gpl_syms),
2703 &mod->num_gpl_syms);
Rusty Russell49668682010-08-05 12:59:10 -06002704 mod->gpl_crcs = section_addr(info, "__kcrctab_gpl");
2705 mod->gpl_future_syms = section_objs(info,
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06002706 "__ksymtab_gpl_future",
2707 sizeof(*mod->gpl_future_syms),
2708 &mod->num_gpl_future_syms);
Rusty Russell49668682010-08-05 12:59:10 -06002709 mod->gpl_future_crcs = section_addr(info, "__kcrctab_gpl_future");
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06002710
2711#ifdef CONFIG_UNUSED_SYMBOLS
Rusty Russell49668682010-08-05 12:59:10 -06002712 mod->unused_syms = section_objs(info, "__ksymtab_unused",
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06002713 sizeof(*mod->unused_syms),
2714 &mod->num_unused_syms);
Rusty Russell49668682010-08-05 12:59:10 -06002715 mod->unused_crcs = section_addr(info, "__kcrctab_unused");
2716 mod->unused_gpl_syms = section_objs(info, "__ksymtab_unused_gpl",
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06002717 sizeof(*mod->unused_gpl_syms),
2718 &mod->num_unused_gpl_syms);
Rusty Russell49668682010-08-05 12:59:10 -06002719 mod->unused_gpl_crcs = section_addr(info, "__kcrctab_unused_gpl");
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06002720#endif
2721#ifdef CONFIG_CONSTRUCTORS
Rusty Russell49668682010-08-05 12:59:10 -06002722 mod->ctors = section_objs(info, ".ctors",
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06002723 sizeof(*mod->ctors), &mod->num_ctors);
2724#endif
2725
2726#ifdef CONFIG_TRACEPOINTS
Mathieu Desnoyers65498642011-01-26 17:26:22 -05002727 mod->tracepoints_ptrs = section_objs(info, "__tracepoints_ptrs",
2728 sizeof(*mod->tracepoints_ptrs),
2729 &mod->num_tracepoints);
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06002730#endif
Jason Baronbf5438fc2010-09-17 11:09:00 -04002731#ifdef HAVE_JUMP_LABEL
2732 mod->jump_entries = section_objs(info, "__jump_table",
2733 sizeof(*mod->jump_entries),
2734 &mod->num_jump_entries);
2735#endif
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06002736#ifdef CONFIG_EVENT_TRACING
Rusty Russell49668682010-08-05 12:59:10 -06002737 mod->trace_events = section_objs(info, "_ftrace_events",
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06002738 sizeof(*mod->trace_events),
2739 &mod->num_trace_events);
2740 /*
2741 * This section contains pointers to allocated objects in the trace
2742 * code and not scanning it leads to false positives.
2743 */
2744 kmemleak_scan_area(mod->trace_events, sizeof(*mod->trace_events) *
2745 mod->num_trace_events, GFP_KERNEL);
2746#endif
Steven Rostedt13b9b6e2010-11-10 22:19:24 -05002747#ifdef CONFIG_TRACING
2748 mod->trace_bprintk_fmt_start = section_objs(info, "__trace_printk_fmt",
2749 sizeof(*mod->trace_bprintk_fmt_start),
2750 &mod->num_trace_bprintk_fmt);
2751 /*
2752 * This section contains pointers to allocated objects in the trace
2753 * code and not scanning it leads to false positives.
2754 */
2755 kmemleak_scan_area(mod->trace_bprintk_fmt_start,
2756 sizeof(*mod->trace_bprintk_fmt_start) *
2757 mod->num_trace_bprintk_fmt, GFP_KERNEL);
2758#endif
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06002759#ifdef CONFIG_FTRACE_MCOUNT_RECORD
2760 /* sechdrs[0].sh_size is always zero */
Rusty Russell49668682010-08-05 12:59:10 -06002761 mod->ftrace_callsites = section_objs(info, "__mcount_loc",
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06002762 sizeof(*mod->ftrace_callsites),
2763 &mod->num_ftrace_callsites);
2764#endif
Rusty Russell22e268e2010-08-05 12:59:05 -06002765
Rusty Russell811d66a2010-08-05 12:59:12 -06002766 mod->extable = section_objs(info, "__ex_table",
2767 sizeof(*mod->extable), &mod->num_exentries);
2768
Rusty Russell49668682010-08-05 12:59:10 -06002769 if (section_addr(info, "__obsparm"))
Rusty Russell22e268e2010-08-05 12:59:05 -06002770 printk(KERN_WARNING "%s: Ignoring obsolete parameters\n",
2771 mod->name);
Rusty Russell811d66a2010-08-05 12:59:12 -06002772
2773 info->debug = section_objs(info, "__verbose",
2774 sizeof(*info->debug), &info->num_debug);
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06002775}
2776
Rusty Russell49668682010-08-05 12:59:10 -06002777static int move_module(struct module *mod, struct load_info *info)
Linus Torvalds65b8a9b2010-08-05 12:59:02 -06002778{
2779 int i;
2780 void *ptr;
2781
2782 /* Do the allocs. */
2783 ptr = module_alloc_update_bounds(mod->core_size);
2784 /*
2785 * The pointer to this block is stored in the module structure
2786 * which is inside the block. Just mark it as not being a
2787 * leak.
2788 */
2789 kmemleak_not_leak(ptr);
2790 if (!ptr)
Rusty Russelld9131882010-08-05 12:59:08 -06002791 return -ENOMEM;
Linus Torvalds65b8a9b2010-08-05 12:59:02 -06002792
2793 memset(ptr, 0, mod->core_size);
2794 mod->module_core = ptr;
2795
Rusty Russell82fab442012-12-11 09:38:33 +10302796 if (mod->init_size) {
2797 ptr = module_alloc_update_bounds(mod->init_size);
2798 /*
2799 * The pointer to this block is stored in the module structure
2800 * which is inside the block. This block doesn't need to be
2801 * scanned as it contains data and code that will be freed
2802 * after the module is initialized.
2803 */
2804 kmemleak_ignore(ptr);
2805 if (!ptr) {
2806 module_free(mod, mod->module_core);
2807 return -ENOMEM;
2808 }
2809 memset(ptr, 0, mod->init_size);
2810 mod->module_init = ptr;
2811 } else
2812 mod->module_init = NULL;
Linus Torvalds65b8a9b2010-08-05 12:59:02 -06002813
2814 /* Transfer each section which specifies SHF_ALLOC */
Jim Cromie5e124162011-12-06 12:11:31 -07002815 pr_debug("final section addresses:\n");
Rusty Russell49668682010-08-05 12:59:10 -06002816 for (i = 0; i < info->hdr->e_shnum; i++) {
Linus Torvalds65b8a9b2010-08-05 12:59:02 -06002817 void *dest;
Rusty Russell49668682010-08-05 12:59:10 -06002818 Elf_Shdr *shdr = &info->sechdrs[i];
Linus Torvalds65b8a9b2010-08-05 12:59:02 -06002819
Rusty Russell49668682010-08-05 12:59:10 -06002820 if (!(shdr->sh_flags & SHF_ALLOC))
Linus Torvalds65b8a9b2010-08-05 12:59:02 -06002821 continue;
2822
Rusty Russell49668682010-08-05 12:59:10 -06002823 if (shdr->sh_entsize & INIT_OFFSET_MASK)
Linus Torvalds65b8a9b2010-08-05 12:59:02 -06002824 dest = mod->module_init
Rusty Russell49668682010-08-05 12:59:10 -06002825 + (shdr->sh_entsize & ~INIT_OFFSET_MASK);
Linus Torvalds65b8a9b2010-08-05 12:59:02 -06002826 else
Rusty Russell49668682010-08-05 12:59:10 -06002827 dest = mod->module_core + shdr->sh_entsize;
Linus Torvalds65b8a9b2010-08-05 12:59:02 -06002828
Rusty Russell49668682010-08-05 12:59:10 -06002829 if (shdr->sh_type != SHT_NOBITS)
2830 memcpy(dest, (void *)shdr->sh_addr, shdr->sh_size);
Linus Torvalds65b8a9b2010-08-05 12:59:02 -06002831 /* Update sh_addr to point to copy in image. */
Rusty Russell49668682010-08-05 12:59:10 -06002832 shdr->sh_addr = (unsigned long)dest;
Jim Cromie5e124162011-12-06 12:11:31 -07002833 pr_debug("\t0x%lx %s\n",
2834 (long)shdr->sh_addr, info->secstrings + shdr->sh_name);
Linus Torvalds65b8a9b2010-08-05 12:59:02 -06002835 }
Rusty Russelld9131882010-08-05 12:59:08 -06002836
2837 return 0;
Linus Torvalds65b8a9b2010-08-05 12:59:02 -06002838}
2839
Rusty Russell49668682010-08-05 12:59:10 -06002840static int check_module_license_and_versions(struct module *mod)
Rusty Russell22e268e2010-08-05 12:59:05 -06002841{
2842 /*
2843 * ndiswrapper is under GPL by itself, but loads proprietary modules.
2844 * Don't use add_taint_module(), as it would prevent ndiswrapper from
2845 * using GPL-only symbols it needs.
2846 */
2847 if (strcmp(mod->name, "ndiswrapper") == 0)
2848 add_taint(TAINT_PROPRIETARY_MODULE);
2849
2850 /* driverloader was caught wrongly pretending to be under GPL */
2851 if (strcmp(mod->name, "driverloader") == 0)
2852 add_taint_module(mod, TAINT_PROPRIETARY_MODULE);
2853
Matthew Garrettc99af372012-06-22 13:49:31 -04002854 /* lve claims to be GPL but upstream won't provide source */
2855 if (strcmp(mod->name, "lve") == 0)
2856 add_taint_module(mod, TAINT_PROPRIETARY_MODULE);
2857
Rusty Russell22e268e2010-08-05 12:59:05 -06002858#ifdef CONFIG_MODVERSIONS
2859 if ((mod->num_syms && !mod->crcs)
2860 || (mod->num_gpl_syms && !mod->gpl_crcs)
2861 || (mod->num_gpl_future_syms && !mod->gpl_future_crcs)
2862#ifdef CONFIG_UNUSED_SYMBOLS
2863 || (mod->num_unused_syms && !mod->unused_crcs)
2864 || (mod->num_unused_gpl_syms && !mod->unused_gpl_crcs)
2865#endif
2866 ) {
2867 return try_to_force_load(mod,
2868 "no versions for exported symbols");
2869 }
2870#endif
2871 return 0;
2872}
2873
2874static void flush_module_icache(const struct module *mod)
2875{
2876 mm_segment_t old_fs;
2877
2878 /* flush the icache in correct context */
2879 old_fs = get_fs();
2880 set_fs(KERNEL_DS);
2881
2882 /*
2883 * Flush the instruction cache, since we've played with text.
2884 * Do it before processing of module parameters, so the module
2885 * can provide parameter accessor functions of its own.
2886 */
2887 if (mod->module_init)
2888 flush_icache_range((unsigned long)mod->module_init,
2889 (unsigned long)mod->module_init
2890 + mod->init_size);
2891 flush_icache_range((unsigned long)mod->module_core,
2892 (unsigned long)mod->module_core + mod->core_size);
2893
2894 set_fs(old_fs);
2895}
2896
Jonas Bonn74e08fc2011-06-30 21:22:11 +02002897int __weak module_frob_arch_sections(Elf_Ehdr *hdr,
2898 Elf_Shdr *sechdrs,
2899 char *secstrings,
2900 struct module *mod)
2901{
2902 return 0;
2903}
2904
Rusty Russell2f3238a2012-10-22 18:09:41 +10302905static struct module *layout_and_allocate(struct load_info *info, int flags)
Rusty Russelld9131882010-08-05 12:59:08 -06002906{
2907 /* Module within temporary copy. */
2908 struct module *mod;
Rusty Russell49668682010-08-05 12:59:10 -06002909 Elf_Shdr *pcpusec;
Rusty Russelld9131882010-08-05 12:59:08 -06002910 int err;
2911
Rusty Russell2f3238a2012-10-22 18:09:41 +10302912 mod = setup_load_info(info, flags);
Rusty Russelld9131882010-08-05 12:59:08 -06002913 if (IS_ERR(mod))
2914 return mod;
2915
Rusty Russell2f3238a2012-10-22 18:09:41 +10302916 err = check_modinfo(mod, info, flags);
Rusty Russelld9131882010-08-05 12:59:08 -06002917 if (err)
2918 return ERR_PTR(err);
2919
2920 /* Allow arches to frob section contents and sizes. */
Rusty Russell49668682010-08-05 12:59:10 -06002921 err = module_frob_arch_sections(info->hdr, info->sechdrs,
2922 info->secstrings, mod);
Rusty Russelld9131882010-08-05 12:59:08 -06002923 if (err < 0)
Rusty Russell6526c532010-08-05 12:59:10 -06002924 goto out;
Rusty Russelld9131882010-08-05 12:59:08 -06002925
Rusty Russell49668682010-08-05 12:59:10 -06002926 pcpusec = &info->sechdrs[info->index.pcpu];
2927 if (pcpusec->sh_size) {
Rusty Russelld9131882010-08-05 12:59:08 -06002928 /* We have a special allocation for this section. */
Rusty Russell49668682010-08-05 12:59:10 -06002929 err = percpu_modalloc(mod,
2930 pcpusec->sh_size, pcpusec->sh_addralign);
Rusty Russelld9131882010-08-05 12:59:08 -06002931 if (err)
Rusty Russell6526c532010-08-05 12:59:10 -06002932 goto out;
Rusty Russell49668682010-08-05 12:59:10 -06002933 pcpusec->sh_flags &= ~(unsigned long)SHF_ALLOC;
Rusty Russelld9131882010-08-05 12:59:08 -06002934 }
2935
2936 /* Determine total sizes, and put offsets in sh_entsize. For now
2937 this is done generically; there doesn't appear to be any
2938 special cases for the architectures. */
Rusty Russell49668682010-08-05 12:59:10 -06002939 layout_sections(mod, info);
Rusty Russell49668682010-08-05 12:59:10 -06002940 layout_symtab(mod, info);
Rusty Russelld9131882010-08-05 12:59:08 -06002941
2942 /* Allocate and move to the final place */
Rusty Russell49668682010-08-05 12:59:10 -06002943 err = move_module(mod, info);
Rusty Russelld9131882010-08-05 12:59:08 -06002944 if (err)
Kevin Cernekee48fd1182012-01-13 09:32:14 +10302945 goto free_percpu;
Rusty Russelld9131882010-08-05 12:59:08 -06002946
2947 /* Module has been copied to its final place now: return it. */
2948 mod = (void *)info->sechdrs[info->index.mod].sh_addr;
Rusty Russell49668682010-08-05 12:59:10 -06002949 kmemleak_load_module(mod, info);
Rusty Russelld9131882010-08-05 12:59:08 -06002950 return mod;
2951
Rusty Russelld9131882010-08-05 12:59:08 -06002952free_percpu:
2953 percpu_modfree(mod);
Rusty Russell6526c532010-08-05 12:59:10 -06002954out:
Rusty Russelld9131882010-08-05 12:59:08 -06002955 return ERR_PTR(err);
2956}
2957
2958/* mod is no longer valid after this! */
2959static void module_deallocate(struct module *mod, struct load_info *info)
2960{
Rusty Russelld9131882010-08-05 12:59:08 -06002961 percpu_modfree(mod);
2962 module_free(mod, mod->module_init);
2963 module_free(mod, mod->module_core);
2964}
2965
Jonas Bonn74e08fc2011-06-30 21:22:11 +02002966int __weak module_finalize(const Elf_Ehdr *hdr,
2967 const Elf_Shdr *sechdrs,
2968 struct module *me)
2969{
2970 return 0;
2971}
2972
Rusty Russell811d66a2010-08-05 12:59:12 -06002973static int post_relocation(struct module *mod, const struct load_info *info)
2974{
Rusty Russell51f3d0f2010-08-05 12:59:13 -06002975 /* Sort exception table now relocations are done. */
Rusty Russell811d66a2010-08-05 12:59:12 -06002976 sort_extable(mod->extable, mod->extable + mod->num_exentries);
2977
2978 /* Copy relocated percpu area over. */
2979 percpu_modcopy(mod, (void *)info->sechdrs[info->index.pcpu].sh_addr,
2980 info->sechdrs[info->index.pcpu].sh_size);
2981
Rusty Russell51f3d0f2010-08-05 12:59:13 -06002982 /* Setup kallsyms-specific fields. */
Rusty Russell811d66a2010-08-05 12:59:12 -06002983 add_kallsyms(mod, info);
2984
2985 /* Arch-specific module finalizing. */
2986 return module_finalize(info->hdr, info->sechdrs, mod);
2987}
2988
Rusty Russell9bb9c3b2012-09-28 14:31:03 +09302989/* Is this module of this name done loading? No locks held. */
2990static bool finished_loading(const char *name)
2991{
2992 struct module *mod;
2993 bool ret;
2994
2995 mutex_lock(&module_mutex);
2996 mod = find_module(name);
2997 ret = !mod || mod->state != MODULE_STATE_COMING;
2998 mutex_unlock(&module_mutex);
2999
3000 return ret;
3001}
3002
Peter Oberparleiterb99b87f2009-06-17 16:28:03 -07003003/* Call module constructors. */
3004static void do_mod_ctors(struct module *mod)
3005{
3006#ifdef CONFIG_CONSTRUCTORS
3007 unsigned long i;
3008
3009 for (i = 0; i < mod->num_ctors; i++)
3010 mod->ctors[i]();
3011#endif
3012}
3013
Linus Torvalds1da177e2005-04-16 15:20:36 -07003014/* This is where the real work happens */
Kees Cook34e11692012-10-16 07:31:07 +10303015static int do_init_module(struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003016{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003017 int ret = 0;
3018
Alan Sterne041c682006-03-27 01:16:30 -08003019 blocking_notifier_call_chain(&module_notify_list,
3020 MODULE_STATE_COMING, mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003021
Steven Rostedt94462ad2010-11-29 13:15:42 -05003022 /* Set RO and NX regions for core */
3023 set_section_ro_nx(mod->module_core,
3024 mod->core_text_size,
3025 mod->core_ro_size,
3026 mod->core_size);
3027
3028 /* Set RO and NX regions for init */
3029 set_section_ro_nx(mod->module_init,
3030 mod->init_text_size,
3031 mod->init_ro_size,
3032 mod->init_size);
3033
Peter Oberparleiterb99b87f2009-06-17 16:28:03 -07003034 do_mod_ctors(mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003035 /* Start the module */
3036 if (mod->init != NULL)
Arjan van de Ven59f94152008-07-30 12:49:02 -07003037 ret = do_one_initcall(mod->init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003038 if (ret < 0) {
3039 /* Init routine failed: abort. Try to protect us from
3040 buggy refcounters. */
3041 mod->state = MODULE_STATE_GOING;
Paul E. McKenneyfbd568a3e2005-05-01 08:59:04 -07003042 synchronize_sched();
Rusty Russellaf49d922007-10-16 23:26:27 -07003043 module_put(mod);
Peter Oberparleiterdf4b5652008-04-21 14:34:31 +02003044 blocking_notifier_call_chain(&module_notify_list,
3045 MODULE_STATE_GOING, mod);
Rusty Russellaf49d922007-10-16 23:26:27 -07003046 free_module(mod);
Rusty Russell6f139092012-09-28 14:31:03 +09303047 wake_up_all(&module_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003048 return ret;
3049 }
Alexey Dobriyane24e2e62008-03-10 11:43:53 -07003050 if (ret > 0) {
Joe Perchesad361c92009-07-06 13:05:40 -07003051 printk(KERN_WARNING
3052"%s: '%s'->init suspiciously returned %d, it should follow 0/-E convention\n"
3053"%s: loading module anyway...\n",
Alexey Dobriyane24e2e62008-03-10 11:43:53 -07003054 __func__, mod->name, ret,
3055 __func__);
3056 dump_stack();
3057 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003058
Rusty Russell6f139092012-09-28 14:31:03 +09303059 /* Now it's a first class citizen! */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003060 mod->state = MODULE_STATE_LIVE;
Masami Hiramatsu0deddf432009-01-06 14:41:54 -08003061 blocking_notifier_call_chain(&module_notify_list,
3062 MODULE_STATE_LIVE, mod);
Rusty Russell6c5db222008-03-10 11:43:52 -07003063
Linus Torvaldsd6de2c82009-04-10 12:17:41 -07003064 /* We need to finish all async code before the module init sequence is done */
3065 async_synchronize_full();
3066
Rusty Russell6c5db222008-03-10 11:43:52 -07003067 mutex_lock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003068 /* Drop initial reference. */
3069 module_put(mod);
Rusty Russellad6561d2009-06-12 21:47:03 -06003070 trim_init_extable(mod);
Jan Beulich4a496222009-07-06 14:50:42 +01003071#ifdef CONFIG_KALLSYMS
3072 mod->num_symtab = mod->core_num_syms;
3073 mod->symtab = mod->core_symtab;
Jan Beulich554bdfe2009-07-06 14:51:44 +01003074 mod->strtab = mod->core_strtab;
Jan Beulich4a496222009-07-06 14:50:42 +01003075#endif
Jan Glauber01526ed2011-05-19 16:55:26 -06003076 unset_module_init_ro_nx(mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003077 module_free(mod, mod->module_init);
3078 mod->module_init = NULL;
3079 mod->init_size = 0;
Jan Glauber4d103802011-05-19 16:55:25 -06003080 mod->init_ro_size = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003081 mod->init_text_size = 0;
Ashutosh Naik6389a382006-03-23 03:00:46 -08003082 mutex_unlock(&module_mutex);
Rusty Russell6f139092012-09-28 14:31:03 +09303083 wake_up_all(&module_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003084
3085 return 0;
3086}
3087
Kees Cook34e11692012-10-16 07:31:07 +10303088static int may_init_module(void)
3089{
3090 if (!capable(CAP_SYS_MODULE) || modules_disabled)
3091 return -EPERM;
3092
3093 return 0;
3094}
3095
3096/* Allocate and load the module: note that size of section 0 is always
3097 zero, and we rely on this for optional sections. */
Rusty Russell2f3238a2012-10-22 18:09:41 +10303098static int load_module(struct load_info *info, const char __user *uargs,
3099 int flags)
Kees Cook34e11692012-10-16 07:31:07 +10303100{
3101 struct module *mod, *old;
3102 long err;
3103
3104 err = module_sig_check(info);
3105 if (err)
3106 goto free_copy;
3107
3108 err = elf_header_check(info);
3109 if (err)
3110 goto free_copy;
3111
3112 /* Figure out module layout, and allocate all the memory. */
Rusty Russell2f3238a2012-10-22 18:09:41 +10303113 mod = layout_and_allocate(info, flags);
Kees Cook34e11692012-10-16 07:31:07 +10303114 if (IS_ERR(mod)) {
3115 err = PTR_ERR(mod);
3116 goto free_copy;
3117 }
3118
3119#ifdef CONFIG_MODULE_SIG
3120 mod->sig_ok = info->sig_ok;
3121 if (!mod->sig_ok)
3122 add_taint_module(mod, TAINT_FORCED_MODULE);
3123#endif
3124
3125 /* Now module is in final location, initialize linked lists, etc. */
3126 err = module_unload_init(mod);
3127 if (err)
3128 goto free_module;
3129
3130 /* Now we've got everything in the final locations, we can
3131 * find optional sections. */
3132 find_module_sections(mod, info);
3133
3134 err = check_module_license_and_versions(mod);
3135 if (err)
3136 goto free_unload;
3137
3138 /* Set up MODINFO_ATTR fields */
3139 setup_modinfo(mod, info);
3140
3141 /* Fix up syms, so that st_value is a pointer to location. */
3142 err = simplify_symbols(mod, info);
3143 if (err < 0)
3144 goto free_modinfo;
3145
3146 err = apply_relocations(mod, info);
3147 if (err < 0)
3148 goto free_modinfo;
3149
3150 err = post_relocation(mod, info);
3151 if (err < 0)
3152 goto free_modinfo;
3153
3154 flush_module_icache(mod);
3155
3156 /* Now copy in args */
3157 mod->args = strndup_user(uargs, ~0UL >> 1);
3158 if (IS_ERR(mod->args)) {
3159 err = PTR_ERR(mod->args);
3160 goto free_arch_cleanup;
3161 }
3162
3163 /* Mark state as coming so strong_try_module_get() ignores us. */
3164 mod->state = MODULE_STATE_COMING;
3165
3166 /* Now sew it into the lists so we can get lockdep and oops
3167 * info during argument parsing. No one should access us, since
3168 * strong_try_module_get() will fail.
3169 * lockdep/oops can run asynchronous, so use the RCU list insertion
3170 * function to insert in a way safe to concurrent readers.
3171 * The mutex protects against concurrent writers.
3172 */
3173again:
3174 mutex_lock(&module_mutex);
3175 if ((old = find_module(mod->name)) != NULL) {
3176 if (old->state == MODULE_STATE_COMING) {
3177 /* Wait in case it fails to load. */
3178 mutex_unlock(&module_mutex);
3179 err = wait_event_interruptible(module_wq,
3180 finished_loading(mod->name));
3181 if (err)
3182 goto free_arch_cleanup;
3183 goto again;
3184 }
3185 err = -EEXIST;
3186 goto unlock;
3187 }
3188
3189 /* This has to be done once we're sure module name is unique. */
3190 dynamic_debug_setup(info->debug, info->num_debug);
3191
3192 /* Find duplicate symbols */
3193 err = verify_export_symbols(mod);
3194 if (err < 0)
3195 goto ddebug;
3196
3197 module_bug_finalize(info->hdr, info->sechdrs, mod);
3198 list_add_rcu(&mod->list, &modules);
3199 mutex_unlock(&module_mutex);
3200
3201 /* Module is ready to execute: parsing args may do that. */
3202 err = parse_args(mod->name, mod->args, mod->kp, mod->num_kp,
3203 -32768, 32767, &ddebug_dyndbg_module_param_cb);
3204 if (err < 0)
3205 goto unlink;
3206
3207 /* Link in to syfs. */
3208 err = mod_sysfs_setup(mod, info, mod->kp, mod->num_kp);
3209 if (err < 0)
3210 goto unlink;
3211
3212 /* Get rid of temporary copy. */
3213 free_copy(info);
3214
3215 /* Done! */
3216 trace_module_load(mod);
3217
3218 return do_init_module(mod);
3219
3220 unlink:
3221 mutex_lock(&module_mutex);
3222 /* Unlink carefully: kallsyms could be walking list. */
3223 list_del_rcu(&mod->list);
3224 module_bug_cleanup(mod);
3225 wake_up_all(&module_wq);
3226 ddebug:
3227 dynamic_debug_remove(info->debug);
3228 unlock:
3229 mutex_unlock(&module_mutex);
3230 synchronize_sched();
3231 kfree(mod->args);
3232 free_arch_cleanup:
3233 module_arch_cleanup(mod);
3234 free_modinfo:
3235 free_modinfo(mod);
3236 free_unload:
3237 module_unload_free(mod);
3238 free_module:
3239 module_deallocate(mod, info);
3240 free_copy:
3241 free_copy(info);
3242 return err;
3243}
3244
3245SYSCALL_DEFINE3(init_module, void __user *, umod,
3246 unsigned long, len, const char __user *, uargs)
3247{
3248 int err;
3249 struct load_info info = { };
3250
3251 err = may_init_module();
3252 if (err)
3253 return err;
3254
3255 pr_debug("init_module: umod=%p, len=%lu, uargs=%p\n",
3256 umod, len, uargs);
3257
3258 err = copy_module_from_user(umod, len, &info);
3259 if (err)
3260 return err;
3261
Rusty Russell2f3238a2012-10-22 18:09:41 +10303262 return load_module(&info, uargs, 0);
Kees Cook34e11692012-10-16 07:31:07 +10303263}
3264
Rusty Russell2f3238a2012-10-22 18:09:41 +10303265SYSCALL_DEFINE3(finit_module, int, fd, const char __user *, uargs, int, flags)
Kees Cook34e11692012-10-16 07:31:07 +10303266{
3267 int err;
3268 struct load_info info = { };
3269
3270 err = may_init_module();
3271 if (err)
3272 return err;
3273
Rusty Russell2f3238a2012-10-22 18:09:41 +10303274 pr_debug("finit_module: fd=%d, uargs=%p, flags=%i\n", fd, uargs, flags);
3275
3276 if (flags & ~(MODULE_INIT_IGNORE_MODVERSIONS
3277 |MODULE_INIT_IGNORE_VERMAGIC))
3278 return -EINVAL;
Kees Cook34e11692012-10-16 07:31:07 +10303279
3280 err = copy_module_from_fd(fd, &info);
3281 if (err)
3282 return err;
3283
Rusty Russell2f3238a2012-10-22 18:09:41 +10303284 return load_module(&info, uargs, flags);
Kees Cook34e11692012-10-16 07:31:07 +10303285}
3286
Linus Torvalds1da177e2005-04-16 15:20:36 -07003287static inline int within(unsigned long addr, void *start, unsigned long size)
3288{
3289 return ((void *)addr >= start && (void *)addr < start + size);
3290}
3291
3292#ifdef CONFIG_KALLSYMS
3293/*
3294 * This ignores the intensely annoying "mapping symbols" found
3295 * in ARM ELF files: $a, $t and $d.
3296 */
3297static inline int is_arm_mapping_symbol(const char *str)
3298{
Daniel Walker22a8bde2007-10-18 03:06:07 -07003299 return str[0] == '$' && strchr("atd", str[1])
Linus Torvalds1da177e2005-04-16 15:20:36 -07003300 && (str[2] == '\0' || str[2] == '.');
3301}
3302
3303static const char *get_ksymbol(struct module *mod,
3304 unsigned long addr,
3305 unsigned long *size,
3306 unsigned long *offset)
3307{
3308 unsigned int i, best = 0;
3309 unsigned long nextval;
3310
3311 /* At worse, next value is at end of module */
Masami Hiramatsua06f6212009-01-06 14:41:49 -08003312 if (within_module_init(addr, mod))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003313 nextval = (unsigned long)mod->module_init+mod->init_text_size;
Daniel Walker22a8bde2007-10-18 03:06:07 -07003314 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003315 nextval = (unsigned long)mod->module_core+mod->core_text_size;
3316
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003317 /* Scan for closest preceding symbol, and next symbol. (ELF
Daniel Walker22a8bde2007-10-18 03:06:07 -07003318 starts real symbols at 1). */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003319 for (i = 1; i < mod->num_symtab; i++) {
3320 if (mod->symtab[i].st_shndx == SHN_UNDEF)
3321 continue;
3322
3323 /* We ignore unnamed symbols: they're uninformative
3324 * and inserted at a whim. */
3325 if (mod->symtab[i].st_value <= addr
3326 && mod->symtab[i].st_value > mod->symtab[best].st_value
3327 && *(mod->strtab + mod->symtab[i].st_name) != '\0'
3328 && !is_arm_mapping_symbol(mod->strtab + mod->symtab[i].st_name))
3329 best = i;
3330 if (mod->symtab[i].st_value > addr
3331 && mod->symtab[i].st_value < nextval
3332 && *(mod->strtab + mod->symtab[i].st_name) != '\0'
3333 && !is_arm_mapping_symbol(mod->strtab + mod->symtab[i].st_name))
3334 nextval = mod->symtab[i].st_value;
3335 }
3336
3337 if (!best)
3338 return NULL;
3339
Alexey Dobriyanffb45122007-05-08 00:28:41 -07003340 if (size)
3341 *size = nextval - mod->symtab[best].st_value;
3342 if (offset)
3343 *offset = addr - mod->symtab[best].st_value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003344 return mod->strtab + mod->symtab[best].st_name;
3345}
3346
Rusty Russell6dd06c92008-01-29 17:13:22 -05003347/* For kallsyms to ask for address resolution. NULL means not found. Careful
3348 * not to lock to avoid deadlock on oopses, simply disable preemption. */
Andrew Morton92dfc9d2008-02-08 04:18:43 -08003349const char *module_address_lookup(unsigned long addr,
Rusty Russell6dd06c92008-01-29 17:13:22 -05003350 unsigned long *size,
3351 unsigned long *offset,
3352 char **modname,
3353 char *namebuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003354{
3355 struct module *mod;
Rusty Russellcb2a5202008-01-14 00:55:03 -08003356 const char *ret = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003357
Rusty Russellcb2a5202008-01-14 00:55:03 -08003358 preempt_disable();
Andi Kleend72b3752008-08-30 10:09:00 +02003359 list_for_each_entry_rcu(mod, &modules, list) {
Masami Hiramatsua06f6212009-01-06 14:41:49 -08003360 if (within_module_init(addr, mod) ||
3361 within_module_core(addr, mod)) {
Franck Bui-Huuffc50892006-10-03 01:13:48 -07003362 if (modname)
3363 *modname = mod->name;
Rusty Russellcb2a5202008-01-14 00:55:03 -08003364 ret = get_ksymbol(mod, addr, size, offset);
3365 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003366 }
3367 }
Rusty Russell6dd06c92008-01-29 17:13:22 -05003368 /* Make a copy in here where it's safe */
3369 if (ret) {
3370 strncpy(namebuf, ret, KSYM_NAME_LEN - 1);
3371 ret = namebuf;
3372 }
Rusty Russellcb2a5202008-01-14 00:55:03 -08003373 preempt_enable();
Andrew Morton92dfc9d2008-02-08 04:18:43 -08003374 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003375}
3376
Alexey Dobriyan9d65cb42007-05-08 00:28:43 -07003377int lookup_module_symbol_name(unsigned long addr, char *symname)
3378{
3379 struct module *mod;
3380
Rusty Russellcb2a5202008-01-14 00:55:03 -08003381 preempt_disable();
Andi Kleend72b3752008-08-30 10:09:00 +02003382 list_for_each_entry_rcu(mod, &modules, list) {
Masami Hiramatsua06f6212009-01-06 14:41:49 -08003383 if (within_module_init(addr, mod) ||
3384 within_module_core(addr, mod)) {
Alexey Dobriyan9d65cb42007-05-08 00:28:43 -07003385 const char *sym;
3386
3387 sym = get_ksymbol(mod, addr, NULL, NULL);
3388 if (!sym)
3389 goto out;
Tejun Heo9281ace2007-07-17 04:03:51 -07003390 strlcpy(symname, sym, KSYM_NAME_LEN);
Rusty Russellcb2a5202008-01-14 00:55:03 -08003391 preempt_enable();
Alexey Dobriyan9d65cb42007-05-08 00:28:43 -07003392 return 0;
3393 }
3394 }
3395out:
Rusty Russellcb2a5202008-01-14 00:55:03 -08003396 preempt_enable();
Alexey Dobriyan9d65cb42007-05-08 00:28:43 -07003397 return -ERANGE;
3398}
3399
Alexey Dobriyana5c43da2007-05-08 00:28:47 -07003400int lookup_module_symbol_attrs(unsigned long addr, unsigned long *size,
3401 unsigned long *offset, char *modname, char *name)
3402{
3403 struct module *mod;
3404
Rusty Russellcb2a5202008-01-14 00:55:03 -08003405 preempt_disable();
Andi Kleend72b3752008-08-30 10:09:00 +02003406 list_for_each_entry_rcu(mod, &modules, list) {
Masami Hiramatsua06f6212009-01-06 14:41:49 -08003407 if (within_module_init(addr, mod) ||
3408 within_module_core(addr, mod)) {
Alexey Dobriyana5c43da2007-05-08 00:28:47 -07003409 const char *sym;
3410
3411 sym = get_ksymbol(mod, addr, size, offset);
3412 if (!sym)
3413 goto out;
3414 if (modname)
Tejun Heo9281ace2007-07-17 04:03:51 -07003415 strlcpy(modname, mod->name, MODULE_NAME_LEN);
Alexey Dobriyana5c43da2007-05-08 00:28:47 -07003416 if (name)
Tejun Heo9281ace2007-07-17 04:03:51 -07003417 strlcpy(name, sym, KSYM_NAME_LEN);
Rusty Russellcb2a5202008-01-14 00:55:03 -08003418 preempt_enable();
Alexey Dobriyana5c43da2007-05-08 00:28:47 -07003419 return 0;
3420 }
3421 }
3422out:
Rusty Russellcb2a5202008-01-14 00:55:03 -08003423 preempt_enable();
Alexey Dobriyana5c43da2007-05-08 00:28:47 -07003424 return -ERANGE;
3425}
3426
Alexey Dobriyanea078902007-05-08 00:28:39 -07003427int module_get_kallsym(unsigned int symnum, unsigned long *value, char *type,
3428 char *name, char *module_name, int *exported)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003429{
3430 struct module *mod;
3431
Rusty Russellcb2a5202008-01-14 00:55:03 -08003432 preempt_disable();
Andi Kleend72b3752008-08-30 10:09:00 +02003433 list_for_each_entry_rcu(mod, &modules, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003434 if (symnum < mod->num_symtab) {
3435 *value = mod->symtab[symnum].st_value;
3436 *type = mod->symtab[symnum].st_info;
Andreas Gruenbacher098c5ee2006-07-14 00:24:04 -07003437 strlcpy(name, mod->strtab + mod->symtab[symnum].st_name,
Tejun Heo9281ace2007-07-17 04:03:51 -07003438 KSYM_NAME_LEN);
3439 strlcpy(module_name, mod->name, MODULE_NAME_LEN);
Tim Abbottca4787b2009-01-05 08:40:10 -06003440 *exported = is_exported(name, *value, mod);
Rusty Russellcb2a5202008-01-14 00:55:03 -08003441 preempt_enable();
Alexey Dobriyanea078902007-05-08 00:28:39 -07003442 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003443 }
3444 symnum -= mod->num_symtab;
3445 }
Rusty Russellcb2a5202008-01-14 00:55:03 -08003446 preempt_enable();
Alexey Dobriyanea078902007-05-08 00:28:39 -07003447 return -ERANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003448}
3449
3450static unsigned long mod_find_symname(struct module *mod, const char *name)
3451{
3452 unsigned int i;
3453
3454 for (i = 0; i < mod->num_symtab; i++)
Keith Owens54e8ce42006-02-03 03:03:53 -08003455 if (strcmp(name, mod->strtab+mod->symtab[i].st_name) == 0 &&
3456 mod->symtab[i].st_info != 'U')
Linus Torvalds1da177e2005-04-16 15:20:36 -07003457 return mod->symtab[i].st_value;
3458 return 0;
3459}
3460
3461/* Look for this name: can be of form module:name. */
3462unsigned long module_kallsyms_lookup_name(const char *name)
3463{
3464 struct module *mod;
3465 char *colon;
3466 unsigned long ret = 0;
3467
3468 /* Don't lock: we're in enough trouble already. */
Rusty Russellcb2a5202008-01-14 00:55:03 -08003469 preempt_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003470 if ((colon = strchr(name, ':')) != NULL) {
3471 *colon = '\0';
3472 if ((mod = find_module(name)) != NULL)
3473 ret = mod_find_symname(mod, colon+1);
3474 *colon = ':';
3475 } else {
Andi Kleend72b3752008-08-30 10:09:00 +02003476 list_for_each_entry_rcu(mod, &modules, list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003477 if ((ret = mod_find_symname(mod, name)) != 0)
3478 break;
3479 }
Rusty Russellcb2a5202008-01-14 00:55:03 -08003480 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003481 return ret;
3482}
Anders Kaseorg75a66612008-12-05 19:03:58 -05003483
3484int module_kallsyms_on_each_symbol(int (*fn)(void *, const char *,
3485 struct module *, unsigned long),
3486 void *data)
3487{
3488 struct module *mod;
3489 unsigned int i;
3490 int ret;
3491
3492 list_for_each_entry(mod, &modules, list) {
3493 for (i = 0; i < mod->num_symtab; i++) {
3494 ret = fn(data, mod->strtab + mod->symtab[i].st_name,
3495 mod, mod->symtab[i].st_value);
3496 if (ret != 0)
3497 return ret;
3498 }
3499 }
3500 return 0;
3501}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003502#endif /* CONFIG_KALLSYMS */
3503
Arjan van de Ven21aa9282008-01-25 21:08:33 +01003504static char *module_flags(struct module *mod, char *buf)
Florin Malitafa3ba2e82006-10-11 01:21:48 -07003505{
3506 int bx = 0;
3507
Arjan van de Ven21aa9282008-01-25 21:08:33 +01003508 if (mod->taints ||
3509 mod->state == MODULE_STATE_GOING ||
3510 mod->state == MODULE_STATE_COMING) {
Florin Malitafa3ba2e82006-10-11 01:21:48 -07003511 buf[bx++] = '(';
Kay Sieverscca3e702012-01-13 09:32:15 +10303512 bx += module_flags_taint(mod, buf + bx);
Arjan van de Ven21aa9282008-01-25 21:08:33 +01003513 /* Show a - for module-is-being-unloaded */
3514 if (mod->state == MODULE_STATE_GOING)
3515 buf[bx++] = '-';
3516 /* Show a + for module-is-being-loaded */
3517 if (mod->state == MODULE_STATE_COMING)
3518 buf[bx++] = '+';
Florin Malitafa3ba2e82006-10-11 01:21:48 -07003519 buf[bx++] = ')';
3520 }
3521 buf[bx] = '\0';
3522
3523 return buf;
3524}
3525
Alexey Dobriyan3b5d5c62008-10-06 13:19:27 +04003526#ifdef CONFIG_PROC_FS
3527/* Called by the /proc file system to return a list of modules. */
3528static void *m_start(struct seq_file *m, loff_t *pos)
3529{
3530 mutex_lock(&module_mutex);
3531 return seq_list_start(&modules, *pos);
3532}
3533
3534static void *m_next(struct seq_file *m, void *p, loff_t *pos)
3535{
3536 return seq_list_next(p, &modules, pos);
3537}
3538
3539static void m_stop(struct seq_file *m, void *p)
3540{
3541 mutex_unlock(&module_mutex);
3542}
3543
Linus Torvalds1da177e2005-04-16 15:20:36 -07003544static int m_show(struct seq_file *m, void *p)
3545{
3546 struct module *mod = list_entry(p, struct module, list);
Florin Malitafa3ba2e82006-10-11 01:21:48 -07003547 char buf[8];
3548
Denys Vlasenko2f0f2a32008-07-22 19:24:27 -05003549 seq_printf(m, "%s %u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003550 mod->name, mod->init_size + mod->core_size);
3551 print_unload_info(m, mod);
3552
3553 /* Informative for users. */
3554 seq_printf(m, " %s",
3555 mod->state == MODULE_STATE_GOING ? "Unloading":
3556 mod->state == MODULE_STATE_COMING ? "Loading":
3557 "Live");
3558 /* Used by oprofile and other similar tools. */
Kees Cook9f36e2c2011-03-22 16:34:22 -07003559 seq_printf(m, " 0x%pK", mod->module_core);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003560
Florin Malitafa3ba2e82006-10-11 01:21:48 -07003561 /* Taints info */
3562 if (mod->taints)
Arjan van de Ven21aa9282008-01-25 21:08:33 +01003563 seq_printf(m, " %s", module_flags(mod, buf));
Florin Malitafa3ba2e82006-10-11 01:21:48 -07003564
Linus Torvalds1da177e2005-04-16 15:20:36 -07003565 seq_printf(m, "\n");
3566 return 0;
3567}
3568
3569/* Format: modulename size refcount deps address
3570
3571 Where refcount is a number or -, and deps is a comma-separated list
3572 of depends or -.
3573*/
Alexey Dobriyan3b5d5c62008-10-06 13:19:27 +04003574static const struct seq_operations modules_op = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003575 .start = m_start,
3576 .next = m_next,
3577 .stop = m_stop,
3578 .show = m_show
3579};
3580
Alexey Dobriyan3b5d5c62008-10-06 13:19:27 +04003581static int modules_open(struct inode *inode, struct file *file)
3582{
3583 return seq_open(file, &modules_op);
3584}
3585
3586static const struct file_operations proc_modules_operations = {
3587 .open = modules_open,
3588 .read = seq_read,
3589 .llseek = seq_lseek,
3590 .release = seq_release,
3591};
3592
3593static int __init proc_modules_init(void)
3594{
3595 proc_create("modules", 0, NULL, &proc_modules_operations);
3596 return 0;
3597}
3598module_init(proc_modules_init);
3599#endif
3600
Linus Torvalds1da177e2005-04-16 15:20:36 -07003601/* Given an address, look for it in the module exception tables. */
3602const struct exception_table_entry *search_module_extables(unsigned long addr)
3603{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003604 const struct exception_table_entry *e = NULL;
3605 struct module *mod;
3606
Rusty Russell24da1cb2007-07-15 23:41:46 -07003607 preempt_disable();
Andi Kleend72b3752008-08-30 10:09:00 +02003608 list_for_each_entry_rcu(mod, &modules, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003609 if (mod->num_exentries == 0)
3610 continue;
Daniel Walker22a8bde2007-10-18 03:06:07 -07003611
Linus Torvalds1da177e2005-04-16 15:20:36 -07003612 e = search_extable(mod->extable,
3613 mod->extable + mod->num_exentries - 1,
3614 addr);
3615 if (e)
3616 break;
3617 }
Rusty Russell24da1cb2007-07-15 23:41:46 -07003618 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003619
3620 /* Now, if we found one, we are running inside it now, hence
Daniel Walker22a8bde2007-10-18 03:06:07 -07003621 we cannot unload the module, hence no refcnt needed. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003622 return e;
3623}
3624
Ingo Molnar4d435f92006-07-03 00:24:24 -07003625/*
Rusty Russelle6104992009-03-31 13:05:31 -06003626 * is_module_address - is this address inside a module?
3627 * @addr: the address to check.
3628 *
3629 * See is_module_text_address() if you simply want to see if the address
3630 * is code (not data).
Ingo Molnar4d435f92006-07-03 00:24:24 -07003631 */
Rusty Russelle6104992009-03-31 13:05:31 -06003632bool is_module_address(unsigned long addr)
Ingo Molnar4d435f92006-07-03 00:24:24 -07003633{
Rusty Russelle6104992009-03-31 13:05:31 -06003634 bool ret;
Ingo Molnar4d435f92006-07-03 00:24:24 -07003635
Rusty Russell24da1cb2007-07-15 23:41:46 -07003636 preempt_disable();
Rusty Russelle6104992009-03-31 13:05:31 -06003637 ret = __module_address(addr) != NULL;
Rusty Russell24da1cb2007-07-15 23:41:46 -07003638 preempt_enable();
Ingo Molnar4d435f92006-07-03 00:24:24 -07003639
Rusty Russelle6104992009-03-31 13:05:31 -06003640 return ret;
Ingo Molnar4d435f92006-07-03 00:24:24 -07003641}
3642
Rusty Russelle6104992009-03-31 13:05:31 -06003643/*
3644 * __module_address - get the module which contains an address.
3645 * @addr: the address.
3646 *
3647 * Must be called with preempt disabled or module mutex held so that
3648 * module doesn't get freed during this.
3649 */
Linus Torvalds714f83d2009-04-05 11:04:19 -07003650struct module *__module_address(unsigned long addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003651{
3652 struct module *mod;
3653
Rusty Russell3a642e92008-07-22 19:24:28 -05003654 if (addr < module_addr_min || addr > module_addr_max)
3655 return NULL;
3656
Andi Kleend72b3752008-08-30 10:09:00 +02003657 list_for_each_entry_rcu(mod, &modules, list)
Rusty Russelle6104992009-03-31 13:05:31 -06003658 if (within_module_core(addr, mod)
3659 || within_module_init(addr, mod))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003660 return mod;
3661 return NULL;
3662}
Tim Abbottc6b37802008-12-05 19:03:59 -05003663EXPORT_SYMBOL_GPL(__module_address);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003664
Rusty Russelle6104992009-03-31 13:05:31 -06003665/*
3666 * is_module_text_address - is this address inside module code?
3667 * @addr: the address to check.
3668 *
3669 * See is_module_address() if you simply want to see if the address is
3670 * anywhere in a module. See kernel_text_address() for testing if an
3671 * address corresponds to kernel or module code.
3672 */
3673bool is_module_text_address(unsigned long addr)
3674{
3675 bool ret;
3676
3677 preempt_disable();
3678 ret = __module_text_address(addr) != NULL;
3679 preempt_enable();
3680
3681 return ret;
3682}
3683
3684/*
3685 * __module_text_address - get the module whose code contains an address.
3686 * @addr: the address.
3687 *
3688 * Must be called with preempt disabled or module mutex held so that
3689 * module doesn't get freed during this.
3690 */
3691struct module *__module_text_address(unsigned long addr)
3692{
3693 struct module *mod = __module_address(addr);
3694 if (mod) {
3695 /* Make sure it's within the text section. */
3696 if (!within(addr, mod->module_init, mod->init_text_size)
3697 && !within(addr, mod->module_core, mod->core_text_size))
3698 mod = NULL;
3699 }
3700 return mod;
3701}
Tim Abbottc6b37802008-12-05 19:03:59 -05003702EXPORT_SYMBOL_GPL(__module_text_address);
Rusty Russelle6104992009-03-31 13:05:31 -06003703
Linus Torvalds1da177e2005-04-16 15:20:36 -07003704/* Don't grab lock, we're oopsing. */
3705void print_modules(void)
3706{
3707 struct module *mod;
Randy Dunlap2bc2d612006-10-02 02:17:02 -07003708 char buf[8];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003709
Linus Torvaldsb2311252009-06-16 11:07:14 -07003710 printk(KERN_DEFAULT "Modules linked in:");
Andi Kleend72b3752008-08-30 10:09:00 +02003711 /* Most callers should already have preempt disabled, but make sure */
3712 preempt_disable();
3713 list_for_each_entry_rcu(mod, &modules, list)
Arjan van de Ven21aa9282008-01-25 21:08:33 +01003714 printk(" %s%s", mod->name, module_flags(mod, buf));
Andi Kleend72b3752008-08-30 10:09:00 +02003715 preempt_enable();
Arjan van de Vene14af7e2008-01-25 21:08:33 +01003716 if (last_unloaded_module[0])
3717 printk(" [last unloaded: %s]", last_unloaded_module);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003718 printk("\n");
3719}
3720
Linus Torvalds1da177e2005-04-16 15:20:36 -07003721#ifdef CONFIG_MODVERSIONS
Rusty Russell8c8ef422009-03-31 13:05:34 -06003722/* Generate the signature for all relevant module structures here.
3723 * If these change, we don't want to try to parse the module. */
3724void module_layout(struct module *mod,
3725 struct modversion_info *ver,
3726 struct kernel_param *kp,
3727 struct kernel_symbol *ks,
Mathieu Desnoyers65498642011-01-26 17:26:22 -05003728 struct tracepoint * const *tp)
Rusty Russell8c8ef422009-03-31 13:05:34 -06003729{
3730}
3731EXPORT_SYMBOL(module_layout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003732#endif