blob: a15899e00ca9d9f60a1201aeaf0038377a5027b8 [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/device.h>
Matt Domschc988d2b2005-06-23 22:05:15 -070046#include <linux/string.h>
Arjan van de Ven97d1f152006-03-23 03:00:24 -080047#include <linux/mutex.h>
Andi Kleend72b3752008-08-30 10:09:00 +020048#include <linux/rculist.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070049#include <asm/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070050#include <asm/cacheflush.h>
Bernd Schmidteb8cdec2009-09-21 17:03:57 -070051#include <asm/mmu_context.h>
Sam Ravnborgb817f6f2006-06-09 21:53:55 +020052#include <linux/license.h>
Christoph Lameter6d762392008-02-08 04:18:42 -080053#include <asm/sections.h>
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -040054#include <linux/tracepoint.h>
Steven Rostedt90d595f2008-08-14 15:45:09 -040055#include <linux/ftrace.h>
Arjan van de Ven22a9d642009-01-07 08:45:46 -080056#include <linux/async.h>
Tejun Heofbf59bc2009-02-20 16:29:08 +090057#include <linux/percpu.h>
Catalin Marinas4f2294b2009-06-11 13:23:20 +010058#include <linux/kmemleak.h>
Jason Baronbf5438fc2010-09-17 11:09:00 -040059#include <linux/jump_label.h>
matthieu castet84e1c6b2010-11-16 22:35:16 +010060#include <linux/pfn.h>
Alessio Igor Bogani403ed272011-04-20 11:10:52 +020061#include <linux/bsearch.h>
Rusty Russell2f3238a2012-10-22 18:09:41 +103062#include <uapi/linux/module.h>
Rusty Russell106a4ee2012-09-26 10:09:40 +010063#include "module-internal.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
Li Zefan7ead8b82009-08-17 16:56:28 +080065#define CREATE_TRACE_POINTS
66#include <trace/events/module.h>
67
Linus Torvalds1da177e2005-04-16 15:20:36 -070068#ifndef ARCH_SHF_SMALL
69#define ARCH_SHF_SMALL 0
70#endif
71
matthieu castet84e1c6b2010-11-16 22:35:16 +010072/*
73 * Modules' sections will be aligned on page boundaries
74 * to ensure complete separation of code and data, but
75 * only when CONFIG_DEBUG_SET_MODULE_RONX=y
76 */
77#ifdef CONFIG_DEBUG_SET_MODULE_RONX
78# define debug_align(X) ALIGN(X, PAGE_SIZE)
79#else
80# define debug_align(X) (X)
81#endif
82
83/*
84 * Given BASE and SIZE this macro calculates the number of pages the
85 * memory regions occupies
86 */
87#define MOD_NUMBER_OF_PAGES(BASE, SIZE) (((SIZE) > 0) ? \
88 (PFN_DOWN((unsigned long)(BASE) + (SIZE) - 1) - \
89 PFN_DOWN((unsigned long)BASE) + 1) \
90 : (0UL))
91
Linus Torvalds1da177e2005-04-16 15:20:36 -070092/* If this is set, the section belongs in the init part of the module */
93#define INIT_OFFSET_MASK (1UL << (BITS_PER_LONG-1))
94
Rusty Russell75676502010-06-05 11:17:36 -060095/*
96 * Mutex protects:
97 * 1) List of modules (also safely readable with preempt_disable),
98 * 2) module_use links,
99 * 3) module_addr_min/module_addr_max.
Masami Hiramatsue513cc12014-11-10 09:30:29 +1030100 * (delete and add uses RCU list operations). */
Tim Abbottc6b37802008-12-05 19:03:59 -0500101DEFINE_MUTEX(module_mutex);
102EXPORT_SYMBOL_GPL(module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103static LIST_HEAD(modules);
Jason Wessel67fc4e02010-05-20 21:04:21 -0500104#ifdef CONFIG_KGDB_KDB
105struct list_head *kdb_modules = &modules; /* kdb needs the list of modules */
106#endif /* CONFIG_KGDB_KDB */
107
Peter Zijlstra0be964b2015-05-27 11:09:35 +0930108static void module_assert_mutex(void)
109{
110 lockdep_assert_held(&module_mutex);
111}
112
113static void module_assert_mutex_or_preempt(void)
114{
115#ifdef CONFIG_LOCKDEP
116 if (unlikely(!debug_locks))
117 return;
118
119 WARN_ON(!rcu_read_lock_sched_held() &&
120 !lockdep_is_held(&module_mutex));
121#endif
122}
123
Rusty Russell106a4ee2012-09-26 10:09:40 +0100124#ifdef CONFIG_MODULE_SIG
125#ifdef CONFIG_MODULE_SIG_FORCE
126static bool sig_enforce = true;
127#else
128static bool sig_enforce = false;
129
130static int param_set_bool_enable_only(const char *val,
131 const struct kernel_param *kp)
132{
133 int err;
134 bool test;
135 struct kernel_param dummy_kp = *kp;
136
137 dummy_kp.arg = &test;
138
139 err = param_set_bool(val, &dummy_kp);
140 if (err)
141 return err;
142
143 /* Don't let them unset it once it's set! */
144 if (!test && sig_enforce)
145 return -EROFS;
146
147 if (test)
148 sig_enforce = true;
149 return 0;
150}
151
152static const struct kernel_param_ops param_ops_bool_enable_only = {
Jani Nikula6a4c2642014-08-27 06:21:23 +0930153 .flags = KERNEL_PARAM_OPS_FL_NOARG,
Rusty Russell106a4ee2012-09-26 10:09:40 +0100154 .set = param_set_bool_enable_only,
155 .get = param_get_bool,
156};
157#define param_check_bool_enable_only param_check_bool
158
159module_param(sig_enforce, bool_enable_only, 0644);
160#endif /* !CONFIG_MODULE_SIG_FORCE */
161#endif /* CONFIG_MODULE_SIG */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162
Stephen Rothwell19e45292009-04-14 17:27:18 +1000163/* Block module loading/unloading? */
164int modules_disabled = 0;
Dave Young02608be2012-02-01 10:33:14 +0800165core_param(nomodule, modules_disabled, bint, 0);
Stephen Rothwell19e45292009-04-14 17:27:18 +1000166
Rusty Russellc9a3ba52008-01-29 17:13:18 -0500167/* Waiting for a module to finish initializing? */
168static DECLARE_WAIT_QUEUE_HEAD(module_wq);
169
Alan Sterne041c682006-03-27 01:16:30 -0800170static BLOCKING_NOTIFIER_HEAD(module_notify_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171
Rusty Russell75676502010-06-05 11:17:36 -0600172/* Bounds of module allocation, for speeding __module_address.
173 * Protected by module_mutex. */
Rusty Russell3a642e92008-07-22 19:24:28 -0500174static unsigned long module_addr_min = -1UL, module_addr_max = 0;
175
Ionut Alexa6da0b562014-11-10 09:31:29 +1030176int register_module_notifier(struct notifier_block *nb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177{
Alan Sterne041c682006-03-27 01:16:30 -0800178 return blocking_notifier_chain_register(&module_notify_list, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179}
180EXPORT_SYMBOL(register_module_notifier);
181
Ionut Alexa6da0b562014-11-10 09:31:29 +1030182int unregister_module_notifier(struct notifier_block *nb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183{
Alan Sterne041c682006-03-27 01:16:30 -0800184 return blocking_notifier_chain_unregister(&module_notify_list, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185}
186EXPORT_SYMBOL(unregister_module_notifier);
187
Rusty Russelleded41c2010-08-05 12:59:07 -0600188struct load_info {
189 Elf_Ehdr *hdr;
190 unsigned long len;
191 Elf_Shdr *sechdrs;
Rusty Russell6526c532010-08-05 12:59:10 -0600192 char *secstrings, *strtab;
Rusty Russelld9131882010-08-05 12:59:08 -0600193 unsigned long symoffs, stroffs;
Rusty Russell811d66a2010-08-05 12:59:12 -0600194 struct _ddebug *debug;
195 unsigned int num_debug;
Rusty Russell106a4ee2012-09-26 10:09:40 +0100196 bool sig_ok;
Rusty Russelleded41c2010-08-05 12:59:07 -0600197 struct {
198 unsigned int sym, str, mod, vers, info, pcpu;
199 } index;
200};
201
Matti Linnanvuori9a4b9702007-11-08 08:37:38 -0800202/* We require a truly strong try_module_get(): 0 means failure due to
203 ongoing or failed initialization etc. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204static inline int strong_try_module_get(struct module *mod)
205{
Rusty Russell0d21b0e2013-01-12 11:38:44 +1030206 BUG_ON(mod && mod->state == MODULE_STATE_UNFORMED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 if (mod && mod->state == MODULE_STATE_COMING)
Rusty Russellc9a3ba52008-01-29 17:13:18 -0500208 return -EBUSY;
209 if (try_module_get(mod))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 return 0;
Rusty Russellc9a3ba52008-01-29 17:13:18 -0500211 else
212 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213}
214
Rusty Russell373d4d02013-01-21 17:17:39 +1030215static inline void add_taint_module(struct module *mod, unsigned flag,
216 enum lockdep_ok lockdep_ok)
Florin Malitafa3ba2e82006-10-11 01:21:48 -0700217{
Rusty Russell373d4d02013-01-21 17:17:39 +1030218 add_taint(flag, lockdep_ok);
Andi Kleen25ddbb12008-10-15 22:01:41 -0700219 mod->taints |= (1U << flag);
Florin Malitafa3ba2e82006-10-11 01:21:48 -0700220}
221
Robert P. J. Day02a3e592007-05-09 07:26:28 +0200222/*
223 * A thread that wants to hold a reference to a module only while it
224 * is running can call this to safely exit. nfsd and lockd use this.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 */
226void __module_put_and_exit(struct module *mod, long code)
227{
228 module_put(mod);
229 do_exit(code);
230}
231EXPORT_SYMBOL(__module_put_and_exit);
Daniel Walker22a8bde2007-10-18 03:06:07 -0700232
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233/* Find a module section: 0 means not found. */
Rusty Russell49668682010-08-05 12:59:10 -0600234static unsigned int find_sec(const struct load_info *info, const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235{
236 unsigned int i;
237
Rusty Russell49668682010-08-05 12:59:10 -0600238 for (i = 1; i < info->hdr->e_shnum; i++) {
239 Elf_Shdr *shdr = &info->sechdrs[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 /* Alloc bit cleared means "ignore it." */
Rusty Russell49668682010-08-05 12:59:10 -0600241 if ((shdr->sh_flags & SHF_ALLOC)
242 && strcmp(info->secstrings + shdr->sh_name, name) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 return i;
Rusty Russell49668682010-08-05 12:59:10 -0600244 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 return 0;
246}
247
Rusty Russell5e458cc2008-10-22 10:00:13 -0500248/* Find a module section, or NULL. */
Rusty Russell49668682010-08-05 12:59:10 -0600249static void *section_addr(const struct load_info *info, const char *name)
Rusty Russell5e458cc2008-10-22 10:00:13 -0500250{
251 /* Section 0 has sh_addr 0. */
Rusty Russell49668682010-08-05 12:59:10 -0600252 return (void *)info->sechdrs[find_sec(info, name)].sh_addr;
Rusty Russell5e458cc2008-10-22 10:00:13 -0500253}
254
255/* Find a module section, or NULL. Fill in number of "objects" in section. */
Rusty Russell49668682010-08-05 12:59:10 -0600256static void *section_objs(const struct load_info *info,
Rusty Russell5e458cc2008-10-22 10:00:13 -0500257 const char *name,
258 size_t object_size,
259 unsigned int *num)
260{
Rusty Russell49668682010-08-05 12:59:10 -0600261 unsigned int sec = find_sec(info, name);
Rusty Russell5e458cc2008-10-22 10:00:13 -0500262
263 /* Section 0 has sh_addr 0 and sh_size 0. */
Rusty Russell49668682010-08-05 12:59:10 -0600264 *num = info->sechdrs[sec].sh_size / object_size;
265 return (void *)info->sechdrs[sec].sh_addr;
Rusty Russell5e458cc2008-10-22 10:00:13 -0500266}
267
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268/* Provided by the linker */
269extern const struct kernel_symbol __start___ksymtab[];
270extern const struct kernel_symbol __stop___ksymtab[];
271extern const struct kernel_symbol __start___ksymtab_gpl[];
272extern const struct kernel_symbol __stop___ksymtab_gpl[];
Greg Kroah-Hartman9f28bb72006-03-20 13:17:13 -0800273extern const struct kernel_symbol __start___ksymtab_gpl_future[];
274extern const struct kernel_symbol __stop___ksymtab_gpl_future[];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275extern const unsigned long __start___kcrctab[];
276extern const unsigned long __start___kcrctab_gpl[];
Greg Kroah-Hartman9f28bb72006-03-20 13:17:13 -0800277extern const unsigned long __start___kcrctab_gpl_future[];
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500278#ifdef CONFIG_UNUSED_SYMBOLS
279extern const struct kernel_symbol __start___ksymtab_unused[];
280extern const struct kernel_symbol __stop___ksymtab_unused[];
281extern const struct kernel_symbol __start___ksymtab_unused_gpl[];
282extern const struct kernel_symbol __stop___ksymtab_unused_gpl[];
Arjan van de Venf71d20e2006-06-28 04:26:45 -0700283extern const unsigned long __start___kcrctab_unused[];
284extern const unsigned long __start___kcrctab_unused_gpl[];
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500285#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286
287#ifndef CONFIG_MODVERSIONS
288#define symversion(base, idx) NULL
289#else
Andrew Mortonf83ca9f2006-03-28 01:56:20 -0800290#define symversion(base, idx) ((base != NULL) ? ((base) + (idx)) : NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291#endif
292
Rusty Russelldafd0942008-07-22 19:24:25 -0500293static bool each_symbol_in_section(const struct symsearch *arr,
294 unsigned int arrsize,
295 struct module *owner,
296 bool (*fn)(const struct symsearch *syms,
297 struct module *owner,
Rusty Russellde4d8d52011-04-19 21:49:58 +0200298 void *data),
Rusty Russelldafd0942008-07-22 19:24:25 -0500299 void *data)
Sam Ravnborg3fd68052006-02-08 21:16:45 +0100300{
Rusty Russellde4d8d52011-04-19 21:49:58 +0200301 unsigned int j;
Rusty Russelldafd0942008-07-22 19:24:25 -0500302
303 for (j = 0; j < arrsize; j++) {
Rusty Russellde4d8d52011-04-19 21:49:58 +0200304 if (fn(&arr[j], owner, data))
305 return true;
Rusty Russelldafd0942008-07-22 19:24:25 -0500306 }
307
308 return false;
Sam Ravnborg3fd68052006-02-08 21:16:45 +0100309}
310
Rusty Russelldafd0942008-07-22 19:24:25 -0500311/* Returns true as soon as fn returns true, otherwise false. */
Rusty Russellde4d8d52011-04-19 21:49:58 +0200312bool each_symbol_section(bool (*fn)(const struct symsearch *arr,
313 struct module *owner,
314 void *data),
315 void *data)
Arjan van de Venf71d20e2006-06-28 04:26:45 -0700316{
Rusty Russelldafd0942008-07-22 19:24:25 -0500317 struct module *mod;
Linus Torvalds44032e62010-08-05 12:59:05 -0600318 static const struct symsearch arr[] = {
Rusty Russelldafd0942008-07-22 19:24:25 -0500319 { __start___ksymtab, __stop___ksymtab, __start___kcrctab,
320 NOT_GPL_ONLY, false },
321 { __start___ksymtab_gpl, __stop___ksymtab_gpl,
322 __start___kcrctab_gpl,
323 GPL_ONLY, false },
324 { __start___ksymtab_gpl_future, __stop___ksymtab_gpl_future,
325 __start___kcrctab_gpl_future,
326 WILL_BE_GPL_ONLY, false },
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500327#ifdef CONFIG_UNUSED_SYMBOLS
Rusty Russelldafd0942008-07-22 19:24:25 -0500328 { __start___ksymtab_unused, __stop___ksymtab_unused,
329 __start___kcrctab_unused,
330 NOT_GPL_ONLY, true },
331 { __start___ksymtab_unused_gpl, __stop___ksymtab_unused_gpl,
332 __start___kcrctab_unused_gpl,
333 GPL_ONLY, true },
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500334#endif
Rusty Russelldafd0942008-07-22 19:24:25 -0500335 };
336
Peter Zijlstra0be964b2015-05-27 11:09:35 +0930337 module_assert_mutex_or_preempt();
338
Rusty Russelldafd0942008-07-22 19:24:25 -0500339 if (each_symbol_in_section(arr, ARRAY_SIZE(arr), NULL, fn, data))
340 return true;
341
Andi Kleend72b3752008-08-30 10:09:00 +0200342 list_for_each_entry_rcu(mod, &modules, list) {
Rusty Russelldafd0942008-07-22 19:24:25 -0500343 struct symsearch arr[] = {
344 { mod->syms, mod->syms + mod->num_syms, mod->crcs,
345 NOT_GPL_ONLY, false },
346 { mod->gpl_syms, mod->gpl_syms + mod->num_gpl_syms,
347 mod->gpl_crcs,
348 GPL_ONLY, false },
349 { mod->gpl_future_syms,
350 mod->gpl_future_syms + mod->num_gpl_future_syms,
351 mod->gpl_future_crcs,
352 WILL_BE_GPL_ONLY, false },
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500353#ifdef CONFIG_UNUSED_SYMBOLS
Rusty Russelldafd0942008-07-22 19:24:25 -0500354 { mod->unused_syms,
355 mod->unused_syms + mod->num_unused_syms,
356 mod->unused_crcs,
357 NOT_GPL_ONLY, true },
358 { mod->unused_gpl_syms,
359 mod->unused_gpl_syms + mod->num_unused_gpl_syms,
360 mod->unused_gpl_crcs,
361 GPL_ONLY, true },
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500362#endif
Rusty Russelldafd0942008-07-22 19:24:25 -0500363 };
364
Rusty Russell0d21b0e2013-01-12 11:38:44 +1030365 if (mod->state == MODULE_STATE_UNFORMED)
366 continue;
367
Rusty Russelldafd0942008-07-22 19:24:25 -0500368 if (each_symbol_in_section(arr, ARRAY_SIZE(arr), mod, fn, data))
369 return true;
370 }
371 return false;
Arjan van de Venf71d20e2006-06-28 04:26:45 -0700372}
Rusty Russellde4d8d52011-04-19 21:49:58 +0200373EXPORT_SYMBOL_GPL(each_symbol_section);
Arjan van de Venf71d20e2006-06-28 04:26:45 -0700374
Rusty Russelldafd0942008-07-22 19:24:25 -0500375struct find_symbol_arg {
376 /* Input */
377 const char *name;
378 bool gplok;
379 bool warn;
380
381 /* Output */
382 struct module *owner;
383 const unsigned long *crc;
Tim Abbott414fd312008-12-05 19:03:56 -0500384 const struct kernel_symbol *sym;
Rusty Russelldafd0942008-07-22 19:24:25 -0500385};
386
Rusty Russellde4d8d52011-04-19 21:49:58 +0200387static bool check_symbol(const struct symsearch *syms,
388 struct module *owner,
389 unsigned int symnum, void *data)
Rusty Russellad9546c2008-05-01 21:14:59 -0500390{
Rusty Russelldafd0942008-07-22 19:24:25 -0500391 struct find_symbol_arg *fsa = data;
392
Rusty Russelldafd0942008-07-22 19:24:25 -0500393 if (!fsa->gplok) {
394 if (syms->licence == GPL_ONLY)
395 return false;
396 if (syms->licence == WILL_BE_GPL_ONLY && fsa->warn) {
Andrew Mortonbddb12b2013-11-12 15:11:28 -0800397 pr_warn("Symbol %s is being used by a non-GPL module, "
398 "which will not be allowed in the future\n",
399 fsa->name);
Rusty Russelldafd0942008-07-22 19:24:25 -0500400 }
401 }
402
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500403#ifdef CONFIG_UNUSED_SYMBOLS
Rusty Russelldafd0942008-07-22 19:24:25 -0500404 if (syms->unused && fsa->warn) {
Andrew Mortonbddb12b2013-11-12 15:11:28 -0800405 pr_warn("Symbol %s is marked as UNUSED, however this module is "
406 "using it.\n", fsa->name);
407 pr_warn("This symbol will go away in the future.\n");
Yannick Guerrini7b63c3a2015-03-24 12:31:40 +1030408 pr_warn("Please evaluate if this is the right api to use and "
409 "if it really is, submit a report to the linux kernel "
410 "mailing list together with submitting your code for "
Andrew Mortonbddb12b2013-11-12 15:11:28 -0800411 "inclusion.\n");
Rusty Russellad9546c2008-05-01 21:14:59 -0500412 }
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500413#endif
Rusty Russelldafd0942008-07-22 19:24:25 -0500414
415 fsa->owner = owner;
416 fsa->crc = symversion(syms->crcs, symnum);
Tim Abbott414fd312008-12-05 19:03:56 -0500417 fsa->sym = &syms->start[symnum];
Rusty Russellad9546c2008-05-01 21:14:59 -0500418 return true;
419}
420
Alessio Igor Bogani403ed272011-04-20 11:10:52 +0200421static int cmp_name(const void *va, const void *vb)
422{
423 const char *a;
424 const struct kernel_symbol *b;
425 a = va; b = vb;
426 return strcmp(a, b->name);
427}
428
Rusty Russellde4d8d52011-04-19 21:49:58 +0200429static bool find_symbol_in_section(const struct symsearch *syms,
430 struct module *owner,
431 void *data)
432{
433 struct find_symbol_arg *fsa = data;
Alessio Igor Bogani403ed272011-04-20 11:10:52 +0200434 struct kernel_symbol *sym;
Rusty Russellde4d8d52011-04-19 21:49:58 +0200435
Alessio Igor Bogani403ed272011-04-20 11:10:52 +0200436 sym = bsearch(fsa->name, syms->start, syms->stop - syms->start,
437 sizeof(struct kernel_symbol), cmp_name);
438
439 if (sym != NULL && check_symbol(syms, owner, sym - syms->start, data))
440 return true;
441
Rusty Russellde4d8d52011-04-19 21:49:58 +0200442 return false;
443}
444
Tim Abbott414fd312008-12-05 19:03:56 -0500445/* Find a symbol and return it, along with, (optional) crc and
Rusty Russell75676502010-06-05 11:17:36 -0600446 * (optional) module which owns it. Needs preempt disabled or module_mutex. */
Tim Abbottc6b37802008-12-05 19:03:59 -0500447const struct kernel_symbol *find_symbol(const char *name,
448 struct module **owner,
449 const unsigned long **crc,
450 bool gplok,
451 bool warn)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452{
Rusty Russelldafd0942008-07-22 19:24:25 -0500453 struct find_symbol_arg fsa;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454
Rusty Russelldafd0942008-07-22 19:24:25 -0500455 fsa.name = name;
456 fsa.gplok = gplok;
457 fsa.warn = warn;
458
Rusty Russellde4d8d52011-04-19 21:49:58 +0200459 if (each_symbol_section(find_symbol_in_section, &fsa)) {
Rusty Russellad9546c2008-05-01 21:14:59 -0500460 if (owner)
Rusty Russelldafd0942008-07-22 19:24:25 -0500461 *owner = fsa.owner;
462 if (crc)
463 *crc = fsa.crc;
Tim Abbott414fd312008-12-05 19:03:56 -0500464 return fsa.sym;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 }
Rusty Russellad9546c2008-05-01 21:14:59 -0500466
Jim Cromie5e124162011-12-06 12:11:31 -0700467 pr_debug("Failed to find symbol %s\n", name);
Tim Abbott414fd312008-12-05 19:03:56 -0500468 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469}
Tim Abbottc6b37802008-12-05 19:03:59 -0500470EXPORT_SYMBOL_GPL(find_symbol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472/* Search for module by name: must hold module_mutex. */
Mathias Krause4f6de4d2013-07-02 15:35:11 +0930473static struct module *find_module_all(const char *name, size_t len,
Rusty Russell0d21b0e2013-01-12 11:38:44 +1030474 bool even_unformed)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475{
476 struct module *mod;
477
Peter Zijlstra0be964b2015-05-27 11:09:35 +0930478 module_assert_mutex();
479
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 list_for_each_entry(mod, &modules, list) {
Rusty Russell0d21b0e2013-01-12 11:38:44 +1030481 if (!even_unformed && mod->state == MODULE_STATE_UNFORMED)
482 continue;
Mathias Krause4f6de4d2013-07-02 15:35:11 +0930483 if (strlen(mod->name) == len && !memcmp(mod->name, name, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 return mod;
485 }
486 return NULL;
487}
Rusty Russell0d21b0e2013-01-12 11:38:44 +1030488
489struct module *find_module(const char *name)
490{
Mathias Krause4f6de4d2013-07-02 15:35:11 +0930491 return find_module_all(name, strlen(name), false);
Rusty Russell0d21b0e2013-01-12 11:38:44 +1030492}
Tim Abbottc6b37802008-12-05 19:03:59 -0500493EXPORT_SYMBOL_GPL(find_module);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494
495#ifdef CONFIG_SMP
Tejun Heofbf59bc2009-02-20 16:29:08 +0900496
Tejun Heo259354d2010-03-10 18:56:10 +0900497static inline void __percpu *mod_percpu(struct module *mod)
Tejun Heofbf59bc2009-02-20 16:29:08 +0900498{
Tejun Heo259354d2010-03-10 18:56:10 +0900499 return mod->percpu;
500}
Tejun Heofbf59bc2009-02-20 16:29:08 +0900501
Rusty Russell9eb76d72013-07-03 10:06:29 +0930502static int percpu_modalloc(struct module *mod, struct load_info *info)
Tejun Heo259354d2010-03-10 18:56:10 +0900503{
Rusty Russell9eb76d72013-07-03 10:06:29 +0930504 Elf_Shdr *pcpusec = &info->sechdrs[info->index.pcpu];
505 unsigned long align = pcpusec->sh_addralign;
506
507 if (!pcpusec->sh_size)
508 return 0;
509
Tejun Heofbf59bc2009-02-20 16:29:08 +0900510 if (align > PAGE_SIZE) {
Andrew Mortonbddb12b2013-11-12 15:11:28 -0800511 pr_warn("%s: per-cpu alignment %li > %li\n",
512 mod->name, align, PAGE_SIZE);
Tejun Heofbf59bc2009-02-20 16:29:08 +0900513 align = PAGE_SIZE;
514 }
515
Rusty Russell9eb76d72013-07-03 10:06:29 +0930516 mod->percpu = __alloc_reserved_percpu(pcpusec->sh_size, align);
Tejun Heo259354d2010-03-10 18:56:10 +0900517 if (!mod->percpu) {
Andrew Mortonbddb12b2013-11-12 15:11:28 -0800518 pr_warn("%s: Could not allocate %lu bytes percpu data\n",
519 mod->name, (unsigned long)pcpusec->sh_size);
Tejun Heo259354d2010-03-10 18:56:10 +0900520 return -ENOMEM;
521 }
Rusty Russell9eb76d72013-07-03 10:06:29 +0930522 mod->percpu_size = pcpusec->sh_size;
Tejun Heo259354d2010-03-10 18:56:10 +0900523 return 0;
Tejun Heofbf59bc2009-02-20 16:29:08 +0900524}
525
Tejun Heo259354d2010-03-10 18:56:10 +0900526static void percpu_modfree(struct module *mod)
Tejun Heofbf59bc2009-02-20 16:29:08 +0900527{
Tejun Heo259354d2010-03-10 18:56:10 +0900528 free_percpu(mod->percpu);
Tejun Heofbf59bc2009-02-20 16:29:08 +0900529}
530
Rusty Russell49668682010-08-05 12:59:10 -0600531static unsigned int find_pcpusec(struct load_info *info)
Tejun Heo6b588c12009-02-20 16:29:07 +0900532{
Rusty Russell49668682010-08-05 12:59:10 -0600533 return find_sec(info, ".data..percpu");
Tejun Heo6b588c12009-02-20 16:29:07 +0900534}
535
Tejun Heo259354d2010-03-10 18:56:10 +0900536static void percpu_modcopy(struct module *mod,
537 const void *from, unsigned long size)
Tejun Heo6b588c12009-02-20 16:29:07 +0900538{
539 int cpu;
540
541 for_each_possible_cpu(cpu)
Tejun Heo259354d2010-03-10 18:56:10 +0900542 memcpy(per_cpu_ptr(mod->percpu, cpu), from, size);
Tejun Heo6b588c12009-02-20 16:29:07 +0900543}
544
Tejun Heo10fad5e2010-03-10 18:57:54 +0900545/**
546 * is_module_percpu_address - test whether address is from module static percpu
547 * @addr: address to test
548 *
549 * Test whether @addr belongs to module static percpu area.
550 *
551 * RETURNS:
552 * %true if @addr is from module static percpu area
553 */
554bool is_module_percpu_address(unsigned long addr)
555{
556 struct module *mod;
557 unsigned int cpu;
558
559 preempt_disable();
560
561 list_for_each_entry_rcu(mod, &modules, list) {
Rusty Russell0d21b0e2013-01-12 11:38:44 +1030562 if (mod->state == MODULE_STATE_UNFORMED)
563 continue;
Tejun Heo10fad5e2010-03-10 18:57:54 +0900564 if (!mod->percpu_size)
565 continue;
566 for_each_possible_cpu(cpu) {
567 void *start = per_cpu_ptr(mod->percpu, cpu);
568
569 if ((void *)addr >= start &&
570 (void *)addr < start + mod->percpu_size) {
571 preempt_enable();
572 return true;
573 }
574 }
575 }
576
577 preempt_enable();
578 return false;
Daniel Walker22a8bde2007-10-18 03:06:07 -0700579}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580
581#else /* ... !CONFIG_SMP */
Tejun Heo6b588c12009-02-20 16:29:07 +0900582
Tejun Heo259354d2010-03-10 18:56:10 +0900583static inline void __percpu *mod_percpu(struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584{
585 return NULL;
586}
Rusty Russell9eb76d72013-07-03 10:06:29 +0930587static int percpu_modalloc(struct module *mod, struct load_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588{
Rusty Russell9eb76d72013-07-03 10:06:29 +0930589 /* UP modules shouldn't have this section: ENOMEM isn't quite right */
590 if (info->sechdrs[info->index.pcpu].sh_size != 0)
591 return -ENOMEM;
592 return 0;
Tejun Heo259354d2010-03-10 18:56:10 +0900593}
594static inline void percpu_modfree(struct module *mod)
595{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596}
Rusty Russell49668682010-08-05 12:59:10 -0600597static unsigned int find_pcpusec(struct load_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598{
599 return 0;
600}
Tejun Heo259354d2010-03-10 18:56:10 +0900601static inline void percpu_modcopy(struct module *mod,
602 const void *from, unsigned long size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603{
604 /* pcpusec should be 0, and size of that section should be 0. */
605 BUG_ON(size != 0);
606}
Tejun Heo10fad5e2010-03-10 18:57:54 +0900607bool is_module_percpu_address(unsigned long addr)
608{
609 return false;
610}
Tejun Heo6b588c12009-02-20 16:29:07 +0900611
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612#endif /* CONFIG_SMP */
613
Matt Domschc988d2b2005-06-23 22:05:15 -0700614#define MODINFO_ATTR(field) \
615static void setup_modinfo_##field(struct module *mod, const char *s) \
616{ \
617 mod->field = kstrdup(s, GFP_KERNEL); \
618} \
619static ssize_t show_modinfo_##field(struct module_attribute *mattr, \
Kay Sievers4befb022011-07-24 22:06:04 +0930620 struct module_kobject *mk, char *buffer) \
Matt Domschc988d2b2005-06-23 22:05:15 -0700621{ \
Chen Gangcc56ded2013-08-20 15:34:21 +0930622 return scnprintf(buffer, PAGE_SIZE, "%s\n", mk->mod->field); \
Matt Domschc988d2b2005-06-23 22:05:15 -0700623} \
624static int modinfo_##field##_exists(struct module *mod) \
625{ \
626 return mod->field != NULL; \
627} \
628static void free_modinfo_##field(struct module *mod) \
629{ \
Daniel Walker22a8bde2007-10-18 03:06:07 -0700630 kfree(mod->field); \
631 mod->field = NULL; \
Matt Domschc988d2b2005-06-23 22:05:15 -0700632} \
633static struct module_attribute modinfo_##field = { \
Tejun Heo7b595752007-06-14 03:45:17 +0900634 .attr = { .name = __stringify(field), .mode = 0444 }, \
Matt Domschc988d2b2005-06-23 22:05:15 -0700635 .show = show_modinfo_##field, \
636 .setup = setup_modinfo_##field, \
637 .test = modinfo_##field##_exists, \
638 .free = free_modinfo_##field, \
639};
640
641MODINFO_ATTR(version);
642MODINFO_ATTR(srcversion);
643
Arjan van de Vene14af7e2008-01-25 21:08:33 +0100644static char last_unloaded_module[MODULE_NAME_LEN+1];
645
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -0800646#ifdef CONFIG_MODULE_UNLOAD
Steven Rostedteb0c5372010-03-29 14:25:18 -0400647
648EXPORT_TRACEPOINT_SYMBOL(module_get);
649
Masami Hiramatsue513cc12014-11-10 09:30:29 +1030650/* MODULE_REF_BASE is the base reference count by kmodule loader. */
651#define MODULE_REF_BASE 1
652
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653/* Init the unload section of the module. */
Rusty Russell9f85a4b2010-08-05 12:59:04 -0600654static int module_unload_init(struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655{
Masami Hiramatsue513cc12014-11-10 09:30:29 +1030656 /*
657 * Initialize reference counter to MODULE_REF_BASE.
658 * refcnt == 0 means module is going.
659 */
660 atomic_set(&mod->refcnt, MODULE_REF_BASE);
Rusty Russell9f85a4b2010-08-05 12:59:04 -0600661
Linus Torvalds2c02dfe2010-05-31 12:19:37 -0700662 INIT_LIST_HEAD(&mod->source_list);
663 INIT_LIST_HEAD(&mod->target_list);
Christoph Lametere1783a22010-01-05 15:34:50 +0900664
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 /* Hold reference count during initialization. */
Masami Hiramatsue513cc12014-11-10 09:30:29 +1030666 atomic_inc(&mod->refcnt);
Rusty Russell9f85a4b2010-08-05 12:59:04 -0600667
668 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669}
670
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671/* Does a already use b? */
672static int already_uses(struct module *a, struct module *b)
673{
674 struct module_use *use;
675
Linus Torvalds2c02dfe2010-05-31 12:19:37 -0700676 list_for_each_entry(use, &b->source_list, source_list) {
677 if (use->source == a) {
Jim Cromie5e124162011-12-06 12:11:31 -0700678 pr_debug("%s uses %s!\n", a->name, b->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 return 1;
680 }
681 }
Jim Cromie5e124162011-12-06 12:11:31 -0700682 pr_debug("%s does not use %s!\n", a->name, b->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 return 0;
684}
685
Linus Torvalds2c02dfe2010-05-31 12:19:37 -0700686/*
687 * Module a uses b
688 * - we add 'a' as a "source", 'b' as a "target" of module use
689 * - the module_use is added to the list of 'b' sources (so
690 * 'b' can walk the list to see who sourced them), and of 'a'
691 * targets (so 'a' can see what modules it targets).
692 */
693static int add_module_usage(struct module *a, struct module *b)
694{
Linus Torvalds2c02dfe2010-05-31 12:19:37 -0700695 struct module_use *use;
696
Jim Cromie5e124162011-12-06 12:11:31 -0700697 pr_debug("Allocating new usage for %s.\n", a->name);
Linus Torvalds2c02dfe2010-05-31 12:19:37 -0700698 use = kmalloc(sizeof(*use), GFP_ATOMIC);
699 if (!use) {
Andrew Mortonbddb12b2013-11-12 15:11:28 -0800700 pr_warn("%s: out of memory loading\n", a->name);
Linus Torvalds2c02dfe2010-05-31 12:19:37 -0700701 return -ENOMEM;
702 }
703
704 use->source = a;
705 use->target = b;
706 list_add(&use->source_list, &b->source_list);
707 list_add(&use->target_list, &a->target_list);
Linus Torvalds2c02dfe2010-05-31 12:19:37 -0700708 return 0;
709}
710
Rusty Russell75676502010-06-05 11:17:36 -0600711/* Module a uses b: caller needs module_mutex() */
Rusty Russell9bea7f22010-06-05 11:17:37 -0600712int ref_module(struct module *a, struct module *b)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713{
Rusty Russellc8e21ce2010-06-05 11:17:35 -0600714 int err;
Kay Sievers270a6c42007-01-18 13:26:15 +0100715
Rusty Russell9bea7f22010-06-05 11:17:37 -0600716 if (b == NULL || already_uses(a, b))
Linus Torvalds218ce732010-05-25 16:48:30 -0700717 return 0;
Linus Torvalds218ce732010-05-25 16:48:30 -0700718
Rusty Russell9bea7f22010-06-05 11:17:37 -0600719 /* If module isn't available, we fail. */
720 err = strong_try_module_get(b);
Rusty Russellc9a3ba52008-01-29 17:13:18 -0500721 if (err)
Rusty Russell9bea7f22010-06-05 11:17:37 -0600722 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723
Linus Torvalds2c02dfe2010-05-31 12:19:37 -0700724 err = add_module_usage(a, b);
725 if (err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 module_put(b);
Rusty Russell9bea7f22010-06-05 11:17:37 -0600727 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 }
Rusty Russell9bea7f22010-06-05 11:17:37 -0600729 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730}
Rusty Russell9bea7f22010-06-05 11:17:37 -0600731EXPORT_SYMBOL_GPL(ref_module);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732
733/* Clear the unload stuff of the module. */
734static void module_unload_free(struct module *mod)
735{
Linus Torvalds2c02dfe2010-05-31 12:19:37 -0700736 struct module_use *use, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737
Rusty Russell75676502010-06-05 11:17:36 -0600738 mutex_lock(&module_mutex);
Linus Torvalds2c02dfe2010-05-31 12:19:37 -0700739 list_for_each_entry_safe(use, tmp, &mod->target_list, target_list) {
740 struct module *i = use->target;
Jim Cromie5e124162011-12-06 12:11:31 -0700741 pr_debug("%s unusing %s\n", mod->name, i->name);
Linus Torvalds2c02dfe2010-05-31 12:19:37 -0700742 module_put(i);
743 list_del(&use->source_list);
744 list_del(&use->target_list);
745 kfree(use);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 }
Rusty Russell75676502010-06-05 11:17:36 -0600747 mutex_unlock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748}
749
750#ifdef CONFIG_MODULE_FORCE_UNLOAD
Akinobu Mitafb169792006-01-08 01:04:29 -0800751static inline int try_force_unload(unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752{
753 int ret = (flags & O_TRUNC);
754 if (ret)
Rusty Russell373d4d02013-01-21 17:17:39 +1030755 add_taint(TAINT_FORCED_RMMOD, LOCKDEP_NOW_UNRELIABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 return ret;
757}
758#else
Akinobu Mitafb169792006-01-08 01:04:29 -0800759static inline int try_force_unload(unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760{
761 return 0;
762}
763#endif /* CONFIG_MODULE_FORCE_UNLOAD */
764
Masami Hiramatsue513cc12014-11-10 09:30:29 +1030765/* Try to release refcount of module, 0 means success. */
766static int try_release_module_ref(struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767{
Masami Hiramatsue513cc12014-11-10 09:30:29 +1030768 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769
Masami Hiramatsue513cc12014-11-10 09:30:29 +1030770 /* Try to decrement refcnt which we set at loading */
771 ret = atomic_sub_return(MODULE_REF_BASE, &mod->refcnt);
772 BUG_ON(ret < 0);
773 if (ret)
774 /* Someone can put this right now, recover with checking */
775 ret = atomic_add_unless(&mod->refcnt, MODULE_REF_BASE, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776
Masami Hiramatsue513cc12014-11-10 09:30:29 +1030777 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778}
779
780static int try_stop_module(struct module *mod, int flags, int *forced)
781{
Masami Hiramatsue513cc12014-11-10 09:30:29 +1030782 /* If it's not unused, quit unless we're forcing. */
783 if (try_release_module_ref(mod) != 0) {
784 *forced = try_force_unload(flags);
785 if (!(*forced))
786 return -EWOULDBLOCK;
787 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788
Masami Hiramatsue513cc12014-11-10 09:30:29 +1030789 /* Mark it as dying. */
790 mod->state = MODULE_STATE_GOING;
791
792 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793}
794
Rusty Russelld5db1392015-01-22 11:13:14 +1030795/**
796 * module_refcount - return the refcount or -1 if unloading
797 *
798 * @mod: the module we're checking
799 *
800 * Returns:
801 * -1 if the module is in the process of unloading
802 * otherwise the number of references in the kernel to the module
803 */
804int module_refcount(struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805{
Rusty Russelld5db1392015-01-22 11:13:14 +1030806 return atomic_read(&mod->refcnt) - MODULE_REF_BASE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807}
808EXPORT_SYMBOL(module_refcount);
809
810/* This exists whether we can unload or not */
811static void free_module(struct module *mod);
812
Heiko Carstens17da2bd2009-01-14 14:14:10 +0100813SYSCALL_DEFINE2(delete_module, const char __user *, name_user,
814 unsigned int, flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815{
816 struct module *mod;
Greg Kroah-Hartmandfff0a02007-02-23 14:54:57 -0800817 char name[MODULE_NAME_LEN];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 int ret, forced = 0;
819
Kees Cook3d433212009-04-02 15:49:29 -0700820 if (!capable(CAP_SYS_MODULE) || modules_disabled)
Greg Kroah-Hartmandfff0a02007-02-23 14:54:57 -0800821 return -EPERM;
822
823 if (strncpy_from_user(name, name_user, MODULE_NAME_LEN-1) < 0)
824 return -EFAULT;
825 name[MODULE_NAME_LEN-1] = '\0';
826
Tejun Heo3fc1f1e2010-05-06 18:49:20 +0200827 if (mutex_lock_interruptible(&module_mutex) != 0)
828 return -EINTR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829
830 mod = find_module(name);
831 if (!mod) {
832 ret = -ENOENT;
833 goto out;
834 }
835
Linus Torvalds2c02dfe2010-05-31 12:19:37 -0700836 if (!list_empty(&mod->source_list)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 /* Other modules depend on us: get rid of them first. */
838 ret = -EWOULDBLOCK;
839 goto out;
840 }
841
842 /* Doing init or already dying? */
843 if (mod->state != MODULE_STATE_LIVE) {
Rusty Russell3f2b9c92013-09-17 05:48:51 +0930844 /* FIXME: if (force), slam module count damn the torpedoes */
Jim Cromie5e124162011-12-06 12:11:31 -0700845 pr_debug("%s already dying\n", mod->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 ret = -EBUSY;
847 goto out;
848 }
849
850 /* If it has an init func, it must have an exit func to unload */
Rusty Russellaf49d922007-10-16 23:26:27 -0700851 if (mod->init && !mod->exit) {
Akinobu Mitafb169792006-01-08 01:04:29 -0800852 forced = try_force_unload(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 if (!forced) {
854 /* This module can't be removed */
855 ret = -EBUSY;
856 goto out;
857 }
858 }
859
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 /* Stop the machine so refcounts can't move and disable module. */
861 ret = try_stop_module(mod, flags, &forced);
862 if (ret != 0)
863 goto out;
864
Peter Oberparleiterdf4b5652008-04-21 14:34:31 +0200865 mutex_unlock(&module_mutex);
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300866 /* Final destruction now no one is using it. */
Peter Oberparleiterdf4b5652008-04-21 14:34:31 +0200867 if (mod->exit != NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 mod->exit();
Peter Oberparleiterdf4b5652008-04-21 14:34:31 +0200869 blocking_notifier_call_chain(&module_notify_list,
870 MODULE_STATE_GOING, mod);
Arjan van de Ven22a9d642009-01-07 08:45:46 -0800871 async_synchronize_full();
Rusty Russell75676502010-06-05 11:17:36 -0600872
Arjan van de Vene14af7e2008-01-25 21:08:33 +0100873 /* Store the name of the last unloaded module for diagnostic purposes */
Rusty Russellefa53452008-01-29 17:13:20 -0500874 strlcpy(last_unloaded_module, mod->name, sizeof(last_unloaded_module));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875
Rusty Russell75676502010-06-05 11:17:36 -0600876 free_module(mod);
877 return 0;
878out:
Ashutosh Naik6389a382006-03-23 03:00:46 -0800879 mutex_unlock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 return ret;
881}
882
Jianjun Kongd1e99d72008-12-08 14:26:29 +0800883static inline void print_unload_info(struct seq_file *m, struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884{
885 struct module_use *use;
886 int printed_something = 0;
887
Rusty Russelld5db1392015-01-22 11:13:14 +1030888 seq_printf(m, " %i ", module_refcount(mod));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889
Ionut Alexa6da0b562014-11-10 09:31:29 +1030890 /*
891 * Always include a trailing , so userspace can differentiate
892 * between this and the old multi-field proc format.
893 */
Linus Torvalds2c02dfe2010-05-31 12:19:37 -0700894 list_for_each_entry(use, &mod->source_list, source_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 printed_something = 1;
Linus Torvalds2c02dfe2010-05-31 12:19:37 -0700896 seq_printf(m, "%s,", use->source->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 }
898
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 if (mod->init != NULL && mod->exit == NULL) {
900 printed_something = 1;
Ionut Alexa6da0b562014-11-10 09:31:29 +1030901 seq_puts(m, "[permanent],");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 }
903
904 if (!printed_something)
Ionut Alexa6da0b562014-11-10 09:31:29 +1030905 seq_puts(m, "-");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906}
907
908void __symbol_put(const char *symbol)
909{
910 struct module *owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911
Rusty Russell24da1cb2007-07-15 23:41:46 -0700912 preempt_disable();
Tim Abbott414fd312008-12-05 19:03:56 -0500913 if (!find_symbol(symbol, &owner, NULL, true, false))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 BUG();
915 module_put(owner);
Rusty Russell24da1cb2007-07-15 23:41:46 -0700916 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917}
918EXPORT_SYMBOL(__symbol_put);
919
Rusty Russell7d1d16e2009-08-26 22:02:54 +0930920/* Note this assumes addr is a function, which it currently always is. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921void symbol_put_addr(void *addr)
922{
Trent Piepho5e376612006-05-15 09:44:06 -0700923 struct module *modaddr;
Rusty Russell7d1d16e2009-08-26 22:02:54 +0930924 unsigned long a = (unsigned long)dereference_function_descriptor(addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925
Rusty Russell7d1d16e2009-08-26 22:02:54 +0930926 if (core_kernel_text(a))
Trent Piepho5e376612006-05-15 09:44:06 -0700927 return;
928
Rusty Russella6e6abd2009-03-31 13:05:31 -0600929 /* module_text_address is safe here: we're supposed to have reference
930 * to module from symbol_get, so it can't go away. */
Rusty Russell7d1d16e2009-08-26 22:02:54 +0930931 modaddr = __module_text_address(a);
Rusty Russella6e6abd2009-03-31 13:05:31 -0600932 BUG_ON(!modaddr);
Trent Piepho5e376612006-05-15 09:44:06 -0700933 module_put(modaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934}
935EXPORT_SYMBOL_GPL(symbol_put_addr);
936
937static ssize_t show_refcnt(struct module_attribute *mattr,
Kay Sievers4befb022011-07-24 22:06:04 +0930938 struct module_kobject *mk, char *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939{
Rusty Russelld5db1392015-01-22 11:13:14 +1030940 return sprintf(buffer, "%i\n", module_refcount(mk->mod));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941}
942
Kay Sieverscca3e702012-01-13 09:32:15 +1030943static struct module_attribute modinfo_refcnt =
944 __ATTR(refcnt, 0444, show_refcnt, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945
Steven Rostedtd53799b2012-03-26 12:50:52 +1030946void __module_get(struct module *module)
947{
948 if (module) {
949 preempt_disable();
Masami Hiramatsu2f35c412014-11-10 09:29:29 +1030950 atomic_inc(&module->refcnt);
Steven Rostedtd53799b2012-03-26 12:50:52 +1030951 trace_module_get(module, _RET_IP_);
952 preempt_enable();
953 }
954}
955EXPORT_SYMBOL(__module_get);
956
957bool try_module_get(struct module *module)
958{
959 bool ret = true;
960
961 if (module) {
962 preempt_disable();
Masami Hiramatsue513cc12014-11-10 09:30:29 +1030963 /* Note: here, we can fail to get a reference */
964 if (likely(module_is_live(module) &&
965 atomic_inc_not_zero(&module->refcnt) != 0))
Steven Rostedtd53799b2012-03-26 12:50:52 +1030966 trace_module_get(module, _RET_IP_);
Masami Hiramatsue513cc12014-11-10 09:30:29 +1030967 else
Steven Rostedtd53799b2012-03-26 12:50:52 +1030968 ret = false;
969
970 preempt_enable();
971 }
972 return ret;
973}
974EXPORT_SYMBOL(try_module_get);
975
Al Virof6a57032006-10-18 01:47:25 -0400976void module_put(struct module *module)
977{
Masami Hiramatsue513cc12014-11-10 09:30:29 +1030978 int ret;
979
Al Virof6a57032006-10-18 01:47:25 -0400980 if (module) {
Christoph Lametere1783a22010-01-05 15:34:50 +0900981 preempt_disable();
Masami Hiramatsue513cc12014-11-10 09:30:29 +1030982 ret = atomic_dec_if_positive(&module->refcnt);
983 WARN_ON(ret < 0); /* Failed to put refcount */
Li Zefanae832d12010-03-24 10:57:43 +0800984 trace_module_put(module, _RET_IP_);
Christoph Lametere1783a22010-01-05 15:34:50 +0900985 preempt_enable();
Al Virof6a57032006-10-18 01:47:25 -0400986 }
987}
988EXPORT_SYMBOL(module_put);
989
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990#else /* !CONFIG_MODULE_UNLOAD */
Jianjun Kongd1e99d72008-12-08 14:26:29 +0800991static inline void print_unload_info(struct seq_file *m, struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992{
993 /* We don't know the usage count, or what modules are using. */
Ionut Alexa6da0b562014-11-10 09:31:29 +1030994 seq_puts(m, " - -");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995}
996
997static inline void module_unload_free(struct module *mod)
998{
999}
1000
Rusty Russell9bea7f22010-06-05 11:17:37 -06001001int ref_module(struct module *a, struct module *b)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002{
Rusty Russell9bea7f22010-06-05 11:17:37 -06001003 return strong_try_module_get(b);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004}
Rusty Russell9bea7f22010-06-05 11:17:37 -06001005EXPORT_SYMBOL_GPL(ref_module);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006
Rusty Russell9f85a4b2010-08-05 12:59:04 -06001007static inline int module_unload_init(struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008{
Rusty Russell9f85a4b2010-08-05 12:59:04 -06001009 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010}
1011#endif /* CONFIG_MODULE_UNLOAD */
1012
Kevin Winchester53999bf2012-01-15 19:32:55 -04001013static size_t module_flags_taint(struct module *mod, char *buf)
1014{
1015 size_t l = 0;
1016
1017 if (mod->taints & (1 << TAINT_PROPRIETARY_MODULE))
1018 buf[l++] = 'P';
1019 if (mod->taints & (1 << TAINT_OOT_MODULE))
1020 buf[l++] = 'O';
1021 if (mod->taints & (1 << TAINT_FORCED_MODULE))
1022 buf[l++] = 'F';
1023 if (mod->taints & (1 << TAINT_CRAP))
1024 buf[l++] = 'C';
Mathieu Desnoyers66cc69e2014-03-13 12:11:30 +10301025 if (mod->taints & (1 << TAINT_UNSIGNED_MODULE))
Rusty Russell57673c22014-03-31 14:39:57 +10301026 buf[l++] = 'E';
Kevin Winchester53999bf2012-01-15 19:32:55 -04001027 /*
1028 * TAINT_FORCED_RMMOD: could be added.
Dave Jones8c904872014-02-26 10:49:49 -05001029 * TAINT_CPU_OUT_OF_SPEC, TAINT_MACHINE_CHECK, TAINT_BAD_PAGE don't
Kevin Winchester53999bf2012-01-15 19:32:55 -04001030 * apply to modules.
1031 */
1032 return l;
1033}
1034
Kay Sievers1f717402006-11-24 12:15:25 +01001035static ssize_t show_initstate(struct module_attribute *mattr,
Kay Sievers4befb022011-07-24 22:06:04 +09301036 struct module_kobject *mk, char *buffer)
Kay Sievers1f717402006-11-24 12:15:25 +01001037{
1038 const char *state = "unknown";
1039
Kay Sievers4befb022011-07-24 22:06:04 +09301040 switch (mk->mod->state) {
Kay Sievers1f717402006-11-24 12:15:25 +01001041 case MODULE_STATE_LIVE:
1042 state = "live";
1043 break;
1044 case MODULE_STATE_COMING:
1045 state = "coming";
1046 break;
1047 case MODULE_STATE_GOING:
1048 state = "going";
1049 break;
Rusty Russell0d21b0e2013-01-12 11:38:44 +10301050 default:
1051 BUG();
Kay Sievers1f717402006-11-24 12:15:25 +01001052 }
1053 return sprintf(buffer, "%s\n", state);
1054}
1055
Kay Sieverscca3e702012-01-13 09:32:15 +10301056static struct module_attribute modinfo_initstate =
1057 __ATTR(initstate, 0444, show_initstate, NULL);
Kay Sievers1f717402006-11-24 12:15:25 +01001058
Kay Sievers88bfa322011-07-24 22:06:04 +09301059static ssize_t store_uevent(struct module_attribute *mattr,
1060 struct module_kobject *mk,
1061 const char *buffer, size_t count)
1062{
1063 enum kobject_action action;
1064
1065 if (kobject_action_type(buffer, count, &action) == 0)
1066 kobject_uevent(&mk->kobj, action);
1067 return count;
1068}
1069
Kay Sieverscca3e702012-01-13 09:32:15 +10301070struct module_attribute module_uevent =
1071 __ATTR(uevent, 0200, NULL, store_uevent);
1072
1073static ssize_t show_coresize(struct module_attribute *mattr,
1074 struct module_kobject *mk, char *buffer)
1075{
1076 return sprintf(buffer, "%u\n", mk->mod->core_size);
1077}
1078
1079static struct module_attribute modinfo_coresize =
1080 __ATTR(coresize, 0444, show_coresize, NULL);
1081
1082static ssize_t show_initsize(struct module_attribute *mattr,
1083 struct module_kobject *mk, char *buffer)
1084{
1085 return sprintf(buffer, "%u\n", mk->mod->init_size);
1086}
1087
1088static struct module_attribute modinfo_initsize =
1089 __ATTR(initsize, 0444, show_initsize, NULL);
1090
1091static ssize_t show_taint(struct module_attribute *mattr,
1092 struct module_kobject *mk, char *buffer)
1093{
1094 size_t l;
1095
1096 l = module_flags_taint(mk->mod, buffer);
1097 buffer[l++] = '\n';
1098 return l;
1099}
1100
1101static struct module_attribute modinfo_taint =
1102 __ATTR(taint, 0444, show_taint, NULL);
Kay Sievers88bfa322011-07-24 22:06:04 +09301103
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001104static struct module_attribute *modinfo_attrs[] = {
Kay Sieverscca3e702012-01-13 09:32:15 +10301105 &module_uevent,
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001106 &modinfo_version,
1107 &modinfo_srcversion,
Kay Sieverscca3e702012-01-13 09:32:15 +10301108 &modinfo_initstate,
1109 &modinfo_coresize,
1110 &modinfo_initsize,
1111 &modinfo_taint,
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001112#ifdef CONFIG_MODULE_UNLOAD
Kay Sieverscca3e702012-01-13 09:32:15 +10301113 &modinfo_refcnt,
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001114#endif
1115 NULL,
1116};
1117
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118static const char vermagic[] = VERMAGIC_STRING;
1119
Rusty Russellc6e665c2009-03-31 13:05:33 -06001120static int try_to_force_load(struct module *mod, const char *reason)
Linus Torvalds826e4502008-05-04 17:04:16 -07001121{
1122#ifdef CONFIG_MODULE_FORCE_LOAD
Andi Kleen25ddbb12008-10-15 22:01:41 -07001123 if (!test_taint(TAINT_FORCED_MODULE))
Andrew Mortonbddb12b2013-11-12 15:11:28 -08001124 pr_warn("%s: %s: kernel tainted.\n", mod->name, reason);
Rusty Russell373d4d02013-01-21 17:17:39 +10301125 add_taint_module(mod, TAINT_FORCED_MODULE, LOCKDEP_NOW_UNRELIABLE);
Linus Torvalds826e4502008-05-04 17:04:16 -07001126 return 0;
1127#else
1128 return -ENOEXEC;
1129#endif
1130}
1131
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132#ifdef CONFIG_MODVERSIONS
Rusty Russelld4703ae2009-12-15 16:28:32 -06001133/* If the arch applies (non-zero) relocations to kernel kcrctab, unapply it. */
1134static unsigned long maybe_relocated(unsigned long crc,
1135 const struct module *crc_owner)
1136{
1137#ifdef ARCH_RELOCATES_KCRCTAB
1138 if (crc_owner == NULL)
1139 return crc - (unsigned long)reloc_start;
1140#endif
1141 return crc;
1142}
1143
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144static int check_version(Elf_Shdr *sechdrs,
1145 unsigned int versindex,
1146 const char *symname,
Ionut Alexa6da0b562014-11-10 09:31:29 +10301147 struct module *mod,
Rusty Russelld4703ae2009-12-15 16:28:32 -06001148 const unsigned long *crc,
1149 const struct module *crc_owner)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150{
1151 unsigned int i, num_versions;
1152 struct modversion_info *versions;
1153
1154 /* Exporting module didn't supply crcs? OK, we're already tainted. */
1155 if (!crc)
1156 return 1;
1157
Rusty Russella5dd6972008-05-09 16:24:21 +10001158 /* No versions at all? modprobe --force does this. */
1159 if (versindex == 0)
1160 return try_to_force_load(mod, symname) == 0;
1161
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 versions = (void *) sechdrs[versindex].sh_addr;
1163 num_versions = sechdrs[versindex].sh_size
1164 / sizeof(struct modversion_info);
1165
1166 for (i = 0; i < num_versions; i++) {
1167 if (strcmp(versions[i].name, symname) != 0)
1168 continue;
1169
Rusty Russelld4703ae2009-12-15 16:28:32 -06001170 if (versions[i].crc == maybe_relocated(*crc, crc_owner))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 return 1;
Jim Cromie5e124162011-12-06 12:11:31 -07001172 pr_debug("Found checksum %lX vs module %lX\n",
Rusty Russelld4703ae2009-12-15 16:28:32 -06001173 maybe_relocated(*crc, crc_owner), versions[i].crc);
Linus Torvalds826e4502008-05-04 17:04:16 -07001174 goto bad_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175 }
Linus Torvalds826e4502008-05-04 17:04:16 -07001176
Andrew Mortonbddb12b2013-11-12 15:11:28 -08001177 pr_warn("%s: no symbol version for %s\n", mod->name, symname);
Rusty Russella5dd6972008-05-09 16:24:21 +10001178 return 0;
Linus Torvalds826e4502008-05-04 17:04:16 -07001179
1180bad_version:
Ionut Alexa6da0b562014-11-10 09:31:29 +10301181 pr_warn("%s: disagrees about version of symbol %s\n",
Linus Torvalds826e4502008-05-04 17:04:16 -07001182 mod->name, symname);
1183 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184}
1185
1186static inline int check_modstruct_version(Elf_Shdr *sechdrs,
1187 unsigned int versindex,
1188 struct module *mod)
1189{
1190 const unsigned long *crc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191
Peter Zijlstra926a59b2015-05-27 11:09:35 +09301192 /*
1193 * Since this should be found in kernel (which can't be removed), no
1194 * locking is necessary -- use preempt_disable() to placate lockdep.
1195 */
1196 preempt_disable();
Rusty Russellb92021b2013-03-15 15:04:17 +10301197 if (!find_symbol(VMLINUX_SYMBOL_STR(module_layout), NULL,
Peter Zijlstra926a59b2015-05-27 11:09:35 +09301198 &crc, true, false)) {
1199 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 BUG();
Peter Zijlstra926a59b2015-05-27 11:09:35 +09301201 }
1202 preempt_enable();
James Hogana4b6a772013-03-18 19:38:56 +10301203 return check_version(sechdrs, versindex,
1204 VMLINUX_SYMBOL_STR(module_layout), mod, crc,
Rusty Russelld4703ae2009-12-15 16:28:32 -06001205 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206}
1207
Rusty Russell91e37a72008-05-09 16:25:28 +10001208/* First part is kernel version, which we ignore if module has crcs. */
1209static inline int same_magic(const char *amagic, const char *bmagic,
1210 bool has_crcs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211{
Rusty Russell91e37a72008-05-09 16:25:28 +10001212 if (has_crcs) {
1213 amagic += strcspn(amagic, " ");
1214 bmagic += strcspn(bmagic, " ");
1215 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 return strcmp(amagic, bmagic) == 0;
1217}
1218#else
1219static inline int check_version(Elf_Shdr *sechdrs,
1220 unsigned int versindex,
1221 const char *symname,
Ionut Alexa6da0b562014-11-10 09:31:29 +10301222 struct module *mod,
Rusty Russelld4703ae2009-12-15 16:28:32 -06001223 const unsigned long *crc,
1224 const struct module *crc_owner)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225{
1226 return 1;
1227}
1228
1229static inline int check_modstruct_version(Elf_Shdr *sechdrs,
1230 unsigned int versindex,
1231 struct module *mod)
1232{
1233 return 1;
1234}
1235
Rusty Russell91e37a72008-05-09 16:25:28 +10001236static inline int same_magic(const char *amagic, const char *bmagic,
1237 bool has_crcs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238{
1239 return strcmp(amagic, bmagic) == 0;
1240}
1241#endif /* CONFIG_MODVERSIONS */
1242
Rusty Russell75676502010-06-05 11:17:36 -06001243/* Resolve a symbol for this module. I.e. if we find one, record usage. */
Rusty Russell49668682010-08-05 12:59:10 -06001244static const struct kernel_symbol *resolve_symbol(struct module *mod,
1245 const struct load_info *info,
Tim Abbott414fd312008-12-05 19:03:56 -05001246 const char *name,
Rusty Russell9bea7f22010-06-05 11:17:37 -06001247 char ownername[])
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248{
1249 struct module *owner;
Tim Abbott414fd312008-12-05 19:03:56 -05001250 const struct kernel_symbol *sym;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251 const unsigned long *crc;
Rusty Russell9bea7f22010-06-05 11:17:37 -06001252 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253
Peter Zijlstrad64810f2015-02-11 15:01:13 +10301254 /*
1255 * The module_mutex should not be a heavily contended lock;
1256 * if we get the occasional sleep here, we'll go an extra iteration
1257 * in the wait_event_interruptible(), which is harmless.
1258 */
1259 sched_annotate_sleep();
Rusty Russell75676502010-06-05 11:17:36 -06001260 mutex_lock(&module_mutex);
Tim Abbott414fd312008-12-05 19:03:56 -05001261 sym = find_symbol(name, &owner, &crc,
Andi Kleen25ddbb12008-10-15 22:01:41 -07001262 !(mod->taints & (1 << TAINT_PROPRIETARY_MODULE)), true);
Rusty Russell9bea7f22010-06-05 11:17:37 -06001263 if (!sym)
1264 goto unlock;
1265
Rusty Russell49668682010-08-05 12:59:10 -06001266 if (!check_version(info->sechdrs, info->index.vers, name, mod, crc,
1267 owner)) {
Rusty Russell9bea7f22010-06-05 11:17:37 -06001268 sym = ERR_PTR(-EINVAL);
1269 goto getname;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 }
Rusty Russell9bea7f22010-06-05 11:17:37 -06001271
1272 err = ref_module(mod, owner);
1273 if (err) {
1274 sym = ERR_PTR(err);
1275 goto getname;
1276 }
1277
1278getname:
1279 /* We must make copy under the lock if we failed to get ref. */
1280 strncpy(ownername, module_name(owner), MODULE_NAME_LEN);
1281unlock:
Rusty Russell75676502010-06-05 11:17:36 -06001282 mutex_unlock(&module_mutex);
Linus Torvalds218ce732010-05-25 16:48:30 -07001283 return sym;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284}
1285
Rusty Russell49668682010-08-05 12:59:10 -06001286static const struct kernel_symbol *
1287resolve_symbol_wait(struct module *mod,
1288 const struct load_info *info,
1289 const char *name)
Rusty Russell9bea7f22010-06-05 11:17:37 -06001290{
1291 const struct kernel_symbol *ksym;
Rusty Russell49668682010-08-05 12:59:10 -06001292 char owner[MODULE_NAME_LEN];
Rusty Russell9bea7f22010-06-05 11:17:37 -06001293
1294 if (wait_event_interruptible_timeout(module_wq,
Rusty Russell49668682010-08-05 12:59:10 -06001295 !IS_ERR(ksym = resolve_symbol(mod, info, name, owner))
1296 || PTR_ERR(ksym) != -EBUSY,
Rusty Russell9bea7f22010-06-05 11:17:37 -06001297 30 * HZ) <= 0) {
Andrew Mortonbddb12b2013-11-12 15:11:28 -08001298 pr_warn("%s: gave up waiting for init of module %s.\n",
1299 mod->name, owner);
Rusty Russell9bea7f22010-06-05 11:17:37 -06001300 }
1301 return ksym;
1302}
1303
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304/*
1305 * /sys/module/foo/sections stuff
1306 * J. Corbet <corbet@lwn.net>
1307 */
Rusty Russell8f6d0372010-08-05 12:59:09 -06001308#ifdef CONFIG_SYSFS
Ben Hutchings10b465a2009-12-19 14:43:01 +00001309
Rusty Russell8f6d0372010-08-05 12:59:09 -06001310#ifdef CONFIG_KALLSYMS
Ben Hutchings10b465a2009-12-19 14:43:01 +00001311static inline bool sect_empty(const Elf_Shdr *sect)
1312{
1313 return !(sect->sh_flags & SHF_ALLOC) || sect->sh_size == 0;
1314}
1315
Ionut Alexa6da0b562014-11-10 09:31:29 +10301316struct module_sect_attr {
Rusty Russella58730c2008-03-13 09:03:44 +00001317 struct module_attribute mattr;
1318 char *name;
1319 unsigned long address;
1320};
1321
Ionut Alexa6da0b562014-11-10 09:31:29 +10301322struct module_sect_attrs {
Rusty Russella58730c2008-03-13 09:03:44 +00001323 struct attribute_group grp;
1324 unsigned int nsections;
1325 struct module_sect_attr attrs[0];
1326};
1327
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328static ssize_t module_sect_show(struct module_attribute *mattr,
Kay Sievers4befb022011-07-24 22:06:04 +09301329 struct module_kobject *mk, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330{
1331 struct module_sect_attr *sattr =
1332 container_of(mattr, struct module_sect_attr, mattr);
Kees Cook9f36e2c2011-03-22 16:34:22 -07001333 return sprintf(buf, "0x%pK\n", (void *)sattr->address);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334}
1335
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001336static void free_sect_attrs(struct module_sect_attrs *sect_attrs)
1337{
Rusty Russella58730c2008-03-13 09:03:44 +00001338 unsigned int section;
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001339
1340 for (section = 0; section < sect_attrs->nsections; section++)
1341 kfree(sect_attrs->attrs[section].name);
1342 kfree(sect_attrs);
1343}
1344
Rusty Russell8f6d0372010-08-05 12:59:09 -06001345static void add_sect_attrs(struct module *mod, const struct load_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346{
1347 unsigned int nloaded = 0, i, size[2];
1348 struct module_sect_attrs *sect_attrs;
1349 struct module_sect_attr *sattr;
1350 struct attribute **gattr;
Daniel Walker22a8bde2007-10-18 03:06:07 -07001351
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352 /* Count loaded sections and allocate structures */
Rusty Russell8f6d0372010-08-05 12:59:09 -06001353 for (i = 0; i < info->hdr->e_shnum; i++)
1354 if (!sect_empty(&info->sechdrs[i]))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355 nloaded++;
1356 size[0] = ALIGN(sizeof(*sect_attrs)
1357 + nloaded * sizeof(sect_attrs->attrs[0]),
1358 sizeof(sect_attrs->grp.attrs[0]));
1359 size[1] = (nloaded + 1) * sizeof(sect_attrs->grp.attrs[0]);
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001360 sect_attrs = kzalloc(size[0] + size[1], GFP_KERNEL);
1361 if (sect_attrs == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362 return;
1363
1364 /* Setup section attributes. */
1365 sect_attrs->grp.name = "sections";
1366 sect_attrs->grp.attrs = (void *)sect_attrs + size[0];
1367
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001368 sect_attrs->nsections = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369 sattr = &sect_attrs->attrs[0];
1370 gattr = &sect_attrs->grp.attrs[0];
Rusty Russell8f6d0372010-08-05 12:59:09 -06001371 for (i = 0; i < info->hdr->e_shnum; i++) {
1372 Elf_Shdr *sec = &info->sechdrs[i];
1373 if (sect_empty(sec))
Helge Deller35dead42009-12-03 00:29:15 +01001374 continue;
Rusty Russell8f6d0372010-08-05 12:59:09 -06001375 sattr->address = sec->sh_addr;
1376 sattr->name = kstrdup(info->secstrings + sec->sh_name,
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001377 GFP_KERNEL);
1378 if (sattr->name == NULL)
1379 goto out;
1380 sect_attrs->nsections++;
Eric W. Biederman361795b2010-02-12 13:41:56 -08001381 sysfs_attr_init(&sattr->mattr.attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382 sattr->mattr.show = module_sect_show;
1383 sattr->mattr.store = NULL;
1384 sattr->mattr.attr.name = sattr->name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385 sattr->mattr.attr.mode = S_IRUGO;
1386 *(gattr++) = &(sattr++)->mattr.attr;
1387 }
1388 *gattr = NULL;
1389
1390 if (sysfs_create_group(&mod->mkobj.kobj, &sect_attrs->grp))
1391 goto out;
1392
1393 mod->sect_attrs = sect_attrs;
1394 return;
1395 out:
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001396 free_sect_attrs(sect_attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397}
1398
1399static void remove_sect_attrs(struct module *mod)
1400{
1401 if (mod->sect_attrs) {
1402 sysfs_remove_group(&mod->mkobj.kobj,
1403 &mod->sect_attrs->grp);
1404 /* We are positive that no one is using any sect attrs
1405 * at this point. Deallocate immediately. */
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001406 free_sect_attrs(mod->sect_attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407 mod->sect_attrs = NULL;
1408 }
1409}
1410
Roland McGrath6d760132007-10-16 23:26:40 -07001411/*
1412 * /sys/module/foo/notes/.section.name gives contents of SHT_NOTE sections.
1413 */
1414
1415struct module_notes_attrs {
1416 struct kobject *dir;
1417 unsigned int notes;
1418 struct bin_attribute attrs[0];
1419};
1420
Chris Wright2c3c8be2010-05-12 18:28:57 -07001421static ssize_t module_notes_read(struct file *filp, struct kobject *kobj,
Roland McGrath6d760132007-10-16 23:26:40 -07001422 struct bin_attribute *bin_attr,
1423 char *buf, loff_t pos, size_t count)
1424{
1425 /*
1426 * The caller checked the pos and count against our size.
1427 */
1428 memcpy(buf, bin_attr->private + pos, count);
1429 return count;
1430}
1431
1432static void free_notes_attrs(struct module_notes_attrs *notes_attrs,
1433 unsigned int i)
1434{
1435 if (notes_attrs->dir) {
1436 while (i-- > 0)
1437 sysfs_remove_bin_file(notes_attrs->dir,
1438 &notes_attrs->attrs[i]);
Alexey Dobriyane9432092008-09-23 23:51:11 +04001439 kobject_put(notes_attrs->dir);
Roland McGrath6d760132007-10-16 23:26:40 -07001440 }
1441 kfree(notes_attrs);
1442}
1443
Rusty Russell8f6d0372010-08-05 12:59:09 -06001444static void add_notes_attrs(struct module *mod, const struct load_info *info)
Roland McGrath6d760132007-10-16 23:26:40 -07001445{
1446 unsigned int notes, loaded, i;
1447 struct module_notes_attrs *notes_attrs;
1448 struct bin_attribute *nattr;
1449
Ingo Molnarea6bff32009-08-28 10:44:56 +02001450 /* failed to create section attributes, so can't create notes */
1451 if (!mod->sect_attrs)
1452 return;
1453
Roland McGrath6d760132007-10-16 23:26:40 -07001454 /* Count notes sections and allocate structures. */
1455 notes = 0;
Rusty Russell8f6d0372010-08-05 12:59:09 -06001456 for (i = 0; i < info->hdr->e_shnum; i++)
1457 if (!sect_empty(&info->sechdrs[i]) &&
1458 (info->sechdrs[i].sh_type == SHT_NOTE))
Roland McGrath6d760132007-10-16 23:26:40 -07001459 ++notes;
1460
1461 if (notes == 0)
1462 return;
1463
1464 notes_attrs = kzalloc(sizeof(*notes_attrs)
1465 + notes * sizeof(notes_attrs->attrs[0]),
1466 GFP_KERNEL);
1467 if (notes_attrs == NULL)
1468 return;
1469
1470 notes_attrs->notes = notes;
1471 nattr = &notes_attrs->attrs[0];
Rusty Russell8f6d0372010-08-05 12:59:09 -06001472 for (loaded = i = 0; i < info->hdr->e_shnum; ++i) {
1473 if (sect_empty(&info->sechdrs[i]))
Roland McGrath6d760132007-10-16 23:26:40 -07001474 continue;
Rusty Russell8f6d0372010-08-05 12:59:09 -06001475 if (info->sechdrs[i].sh_type == SHT_NOTE) {
Eric W. Biederman361795b2010-02-12 13:41:56 -08001476 sysfs_bin_attr_init(nattr);
Roland McGrath6d760132007-10-16 23:26:40 -07001477 nattr->attr.name = mod->sect_attrs->attrs[loaded].name;
1478 nattr->attr.mode = S_IRUGO;
Rusty Russell8f6d0372010-08-05 12:59:09 -06001479 nattr->size = info->sechdrs[i].sh_size;
1480 nattr->private = (void *) info->sechdrs[i].sh_addr;
Roland McGrath6d760132007-10-16 23:26:40 -07001481 nattr->read = module_notes_read;
1482 ++nattr;
1483 }
1484 ++loaded;
1485 }
1486
Greg Kroah-Hartman4ff6abf2007-11-05 22:24:43 -08001487 notes_attrs->dir = kobject_create_and_add("notes", &mod->mkobj.kobj);
Roland McGrath6d760132007-10-16 23:26:40 -07001488 if (!notes_attrs->dir)
1489 goto out;
1490
1491 for (i = 0; i < notes; ++i)
1492 if (sysfs_create_bin_file(notes_attrs->dir,
1493 &notes_attrs->attrs[i]))
1494 goto out;
1495
1496 mod->notes_attrs = notes_attrs;
1497 return;
1498
1499 out:
1500 free_notes_attrs(notes_attrs, i);
1501}
1502
1503static void remove_notes_attrs(struct module *mod)
1504{
1505 if (mod->notes_attrs)
1506 free_notes_attrs(mod->notes_attrs, mod->notes_attrs->notes);
1507}
1508
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509#else
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001510
Rusty Russell8f6d0372010-08-05 12:59:09 -06001511static inline void add_sect_attrs(struct module *mod,
1512 const struct load_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513{
1514}
1515
1516static inline void remove_sect_attrs(struct module *mod)
1517{
1518}
Roland McGrath6d760132007-10-16 23:26:40 -07001519
Rusty Russell8f6d0372010-08-05 12:59:09 -06001520static inline void add_notes_attrs(struct module *mod,
1521 const struct load_info *info)
Roland McGrath6d760132007-10-16 23:26:40 -07001522{
1523}
1524
1525static inline void remove_notes_attrs(struct module *mod)
1526{
1527}
Rusty Russell8f6d0372010-08-05 12:59:09 -06001528#endif /* CONFIG_KALLSYMS */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529
Rusty Russell80a3d1b2010-06-05 11:17:36 -06001530static void add_usage_links(struct module *mod)
1531{
1532#ifdef CONFIG_MODULE_UNLOAD
1533 struct module_use *use;
1534 int nowarn;
1535
Rusty Russell75676502010-06-05 11:17:36 -06001536 mutex_lock(&module_mutex);
Rusty Russell80a3d1b2010-06-05 11:17:36 -06001537 list_for_each_entry(use, &mod->target_list, target_list) {
1538 nowarn = sysfs_create_link(use->target->holders_dir,
1539 &mod->mkobj.kobj, mod->name);
1540 }
Rusty Russell75676502010-06-05 11:17:36 -06001541 mutex_unlock(&module_mutex);
Rusty Russell80a3d1b2010-06-05 11:17:36 -06001542#endif
1543}
1544
1545static void del_usage_links(struct module *mod)
1546{
1547#ifdef CONFIG_MODULE_UNLOAD
1548 struct module_use *use;
1549
Rusty Russell75676502010-06-05 11:17:36 -06001550 mutex_lock(&module_mutex);
Rusty Russell80a3d1b2010-06-05 11:17:36 -06001551 list_for_each_entry(use, &mod->target_list, target_list)
1552 sysfs_remove_link(use->target->holders_dir, mod->name);
Rusty Russell75676502010-06-05 11:17:36 -06001553 mutex_unlock(&module_mutex);
Rusty Russell80a3d1b2010-06-05 11:17:36 -06001554#endif
1555}
1556
Rusty Russell6407ebb22010-06-05 11:17:36 -06001557static int module_add_modinfo_attrs(struct module *mod)
Matt Domschc988d2b2005-06-23 22:05:15 -07001558{
1559 struct module_attribute *attr;
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001560 struct module_attribute *temp_attr;
Matt Domschc988d2b2005-06-23 22:05:15 -07001561 int error = 0;
1562 int i;
1563
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001564 mod->modinfo_attrs = kzalloc((sizeof(struct module_attribute) *
1565 (ARRAY_SIZE(modinfo_attrs) + 1)),
1566 GFP_KERNEL);
1567 if (!mod->modinfo_attrs)
1568 return -ENOMEM;
1569
1570 temp_attr = mod->modinfo_attrs;
Matt Domschc988d2b2005-06-23 22:05:15 -07001571 for (i = 0; (attr = modinfo_attrs[i]) && !error; i++) {
1572 if (!attr->test ||
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001573 (attr->test && attr->test(mod))) {
1574 memcpy(temp_attr, attr, sizeof(*temp_attr));
Eric W. Biederman361795b2010-02-12 13:41:56 -08001575 sysfs_attr_init(&temp_attr->attr);
Ionut Alexa6da0b562014-11-10 09:31:29 +10301576 error = sysfs_create_file(&mod->mkobj.kobj,
1577 &temp_attr->attr);
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001578 ++temp_attr;
1579 }
Matt Domschc988d2b2005-06-23 22:05:15 -07001580 }
1581 return error;
1582}
1583
Rusty Russell6407ebb22010-06-05 11:17:36 -06001584static void module_remove_modinfo_attrs(struct module *mod)
Matt Domschc988d2b2005-06-23 22:05:15 -07001585{
1586 struct module_attribute *attr;
1587 int i;
1588
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001589 for (i = 0; (attr = &mod->modinfo_attrs[i]); i++) {
1590 /* pick a field to test for end of list */
1591 if (!attr->attr.name)
1592 break;
Ionut Alexa6da0b562014-11-10 09:31:29 +10301593 sysfs_remove_file(&mod->mkobj.kobj, &attr->attr);
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001594 if (attr->free)
1595 attr->free(mod);
Matt Domschc988d2b2005-06-23 22:05:15 -07001596 }
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001597 kfree(mod->modinfo_attrs);
Matt Domschc988d2b2005-06-23 22:05:15 -07001598}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599
Li Zhong942e4432013-09-03 16:33:57 +09301600static void mod_kobject_put(struct module *mod)
1601{
1602 DECLARE_COMPLETION_ONSTACK(c);
1603 mod->mkobj.kobj_completion = &c;
1604 kobject_put(&mod->mkobj.kobj);
1605 wait_for_completion(&c);
1606}
1607
Rusty Russell6407ebb22010-06-05 11:17:36 -06001608static int mod_sysfs_init(struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609{
1610 int err;
Greg Kroah-Hartman6494a932008-01-27 15:38:40 -08001611 struct kobject *kobj;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612
Greg Kroah-Hartman823bccf2007-04-13 13:15:19 -07001613 if (!module_sysfs_initialized) {
Andrew Mortonbddb12b2013-11-12 15:11:28 -08001614 pr_err("%s: module sysfs not initialized\n", mod->name);
Ed Swierk1cc5f712006-09-25 16:25:36 -07001615 err = -EINVAL;
1616 goto out;
1617 }
Greg Kroah-Hartman6494a932008-01-27 15:38:40 -08001618
1619 kobj = kset_find_obj(module_kset, mod->name);
1620 if (kobj) {
Andrew Mortonbddb12b2013-11-12 15:11:28 -08001621 pr_err("%s: module is already loaded\n", mod->name);
Greg Kroah-Hartman6494a932008-01-27 15:38:40 -08001622 kobject_put(kobj);
1623 err = -EINVAL;
1624 goto out;
1625 }
1626
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627 mod->mkobj.mod = mod;
Kay Sieverse17e0f52006-11-24 12:15:25 +01001628
Greg Kroah-Hartmanac3c8142007-12-17 23:05:35 -07001629 memset(&mod->mkobj.kobj, 0, sizeof(mod->mkobj.kobj));
1630 mod->mkobj.kobj.kset = module_kset;
1631 err = kobject_init_and_add(&mod->mkobj.kobj, &module_ktype, NULL,
1632 "%s", mod->name);
1633 if (err)
Li Zhong942e4432013-09-03 16:33:57 +09301634 mod_kobject_put(mod);
Kay Sievers270a6c42007-01-18 13:26:15 +01001635
Kay Sievers97c146e2007-11-29 23:46:11 +01001636 /* delay uevent until full sysfs population */
Kay Sievers270a6c42007-01-18 13:26:15 +01001637out:
1638 return err;
1639}
1640
Rusty Russell6407ebb22010-06-05 11:17:36 -06001641static int mod_sysfs_setup(struct module *mod,
Rusty Russell8f6d0372010-08-05 12:59:09 -06001642 const struct load_info *info,
Kay Sievers270a6c42007-01-18 13:26:15 +01001643 struct kernel_param *kparam,
1644 unsigned int num_params)
1645{
1646 int err;
1647
Rusty Russell80a3d1b2010-06-05 11:17:36 -06001648 err = mod_sysfs_init(mod);
1649 if (err)
1650 goto out;
1651
Greg Kroah-Hartman4ff6abf2007-11-05 22:24:43 -08001652 mod->holders_dir = kobject_create_and_add("holders", &mod->mkobj.kobj);
Akinobu Mita240936e2007-04-26 00:12:09 -07001653 if (!mod->holders_dir) {
1654 err = -ENOMEM;
Kay Sievers270a6c42007-01-18 13:26:15 +01001655 goto out_unreg;
Akinobu Mita240936e2007-04-26 00:12:09 -07001656 }
Kay Sievers270a6c42007-01-18 13:26:15 +01001657
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658 err = module_param_sysfs_setup(mod, kparam, num_params);
1659 if (err)
Kay Sievers270a6c42007-01-18 13:26:15 +01001660 goto out_unreg_holders;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661
Matt Domschc988d2b2005-06-23 22:05:15 -07001662 err = module_add_modinfo_attrs(mod);
1663 if (err)
Kay Sieverse17e0f52006-11-24 12:15:25 +01001664 goto out_unreg_param;
Matt Domschc988d2b2005-06-23 22:05:15 -07001665
Rusty Russell80a3d1b2010-06-05 11:17:36 -06001666 add_usage_links(mod);
Rusty Russell8f6d0372010-08-05 12:59:09 -06001667 add_sect_attrs(mod, info);
1668 add_notes_attrs(mod, info);
Rusty Russell80a3d1b2010-06-05 11:17:36 -06001669
Kay Sieverse17e0f52006-11-24 12:15:25 +01001670 kobject_uevent(&mod->mkobj.kobj, KOBJ_ADD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671 return 0;
1672
Kay Sieverse17e0f52006-11-24 12:15:25 +01001673out_unreg_param:
1674 module_param_sysfs_remove(mod);
Kay Sievers270a6c42007-01-18 13:26:15 +01001675out_unreg_holders:
Greg Kroah-Hartman78a2d902007-12-20 08:13:05 -08001676 kobject_put(mod->holders_dir);
Kay Sievers270a6c42007-01-18 13:26:15 +01001677out_unreg:
Li Zhong942e4432013-09-03 16:33:57 +09301678 mod_kobject_put(mod);
Rusty Russell80a3d1b2010-06-05 11:17:36 -06001679out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680 return err;
1681}
Denis V. Lunev34e4e2f2008-05-20 13:59:48 +04001682
1683static void mod_sysfs_fini(struct module *mod)
1684{
Rusty Russell8f6d0372010-08-05 12:59:09 -06001685 remove_notes_attrs(mod);
1686 remove_sect_attrs(mod);
Li Zhong942e4432013-09-03 16:33:57 +09301687 mod_kobject_put(mod);
Denis V. Lunev34e4e2f2008-05-20 13:59:48 +04001688}
1689
Rusty Russell8f6d0372010-08-05 12:59:09 -06001690#else /* !CONFIG_SYSFS */
Denis V. Lunev34e4e2f2008-05-20 13:59:48 +04001691
Rusty Russell8f6d0372010-08-05 12:59:09 -06001692static int mod_sysfs_setup(struct module *mod,
1693 const struct load_info *info,
Rusty Russell6407ebb22010-06-05 11:17:36 -06001694 struct kernel_param *kparam,
1695 unsigned int num_params)
1696{
1697 return 0;
1698}
1699
Denis V. Lunev34e4e2f2008-05-20 13:59:48 +04001700static void mod_sysfs_fini(struct module *mod)
1701{
1702}
1703
Rusty Russell36b03602010-08-05 12:59:09 -06001704static void module_remove_modinfo_attrs(struct module *mod)
1705{
1706}
1707
Rusty Russell80a3d1b2010-06-05 11:17:36 -06001708static void del_usage_links(struct module *mod)
1709{
1710}
1711
Denis V. Lunev34e4e2f2008-05-20 13:59:48 +04001712#endif /* CONFIG_SYSFS */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713
Rusty Russell36b03602010-08-05 12:59:09 -06001714static void mod_sysfs_teardown(struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715{
Rusty Russell80a3d1b2010-06-05 11:17:36 -06001716 del_usage_links(mod);
Matt Domschc988d2b2005-06-23 22:05:15 -07001717 module_remove_modinfo_attrs(mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718 module_param_sysfs_remove(mod);
Greg Kroah-Hartman78a2d902007-12-20 08:13:05 -08001719 kobject_put(mod->mkobj.drivers_dir);
1720 kobject_put(mod->holders_dir);
Denis V. Lunev34e4e2f2008-05-20 13:59:48 +04001721 mod_sysfs_fini(mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722}
1723
matthieu castet84e1c6b2010-11-16 22:35:16 +01001724#ifdef CONFIG_DEBUG_SET_MODULE_RONX
1725/*
1726 * LKM RO/NX protection: protect module's text/ro-data
1727 * from modification and any data from execution.
1728 */
1729void set_page_attributes(void *start, void *end, int (*set)(unsigned long start, int num_pages))
1730{
1731 unsigned long begin_pfn = PFN_DOWN((unsigned long)start);
1732 unsigned long end_pfn = PFN_DOWN((unsigned long)end);
1733
1734 if (end_pfn > begin_pfn)
1735 set(begin_pfn << PAGE_SHIFT, end_pfn - begin_pfn);
1736}
1737
1738static void set_section_ro_nx(void *base,
1739 unsigned long text_size,
1740 unsigned long ro_size,
1741 unsigned long total_size)
1742{
1743 /* begin and end PFNs of the current subsection */
1744 unsigned long begin_pfn;
1745 unsigned long end_pfn;
1746
1747 /*
1748 * Set RO for module text and RO-data:
1749 * - Always protect first page.
1750 * - Do not protect last partial page.
1751 */
1752 if (ro_size > 0)
1753 set_page_attributes(base, base + ro_size, set_memory_ro);
1754
1755 /*
1756 * Set NX permissions for module data:
1757 * - Do not protect first partial page.
1758 * - Always protect last page.
1759 */
1760 if (total_size > text_size) {
1761 begin_pfn = PFN_UP((unsigned long)base + text_size);
1762 end_pfn = PFN_UP((unsigned long)base + total_size);
1763 if (end_pfn > begin_pfn)
1764 set_memory_nx(begin_pfn << PAGE_SHIFT, end_pfn - begin_pfn);
1765 }
1766}
1767
Jan Glauber01526ed2011-05-19 16:55:26 -06001768static void unset_module_core_ro_nx(struct module *mod)
matthieu castet84e1c6b2010-11-16 22:35:16 +01001769{
Jan Glauber01526ed2011-05-19 16:55:26 -06001770 set_page_attributes(mod->module_core + mod->core_text_size,
1771 mod->module_core + mod->core_size,
1772 set_memory_x);
1773 set_page_attributes(mod->module_core,
1774 mod->module_core + mod->core_ro_size,
1775 set_memory_rw);
1776}
1777
1778static void unset_module_init_ro_nx(struct module *mod)
1779{
1780 set_page_attributes(mod->module_init + mod->init_text_size,
1781 mod->module_init + mod->init_size,
1782 set_memory_x);
1783 set_page_attributes(mod->module_init,
1784 mod->module_init + mod->init_ro_size,
1785 set_memory_rw);
matthieu castet84e1c6b2010-11-16 22:35:16 +01001786}
1787
1788/* Iterate through all modules and set each module's text as RW */
Daniel J Blueman5d05c702011-03-08 22:01:47 +08001789void set_all_modules_text_rw(void)
matthieu castet84e1c6b2010-11-16 22:35:16 +01001790{
1791 struct module *mod;
1792
1793 mutex_lock(&module_mutex);
1794 list_for_each_entry_rcu(mod, &modules, list) {
Rusty Russell0d21b0e2013-01-12 11:38:44 +10301795 if (mod->state == MODULE_STATE_UNFORMED)
1796 continue;
matthieu castet84e1c6b2010-11-16 22:35:16 +01001797 if ((mod->module_core) && (mod->core_text_size)) {
1798 set_page_attributes(mod->module_core,
1799 mod->module_core + mod->core_text_size,
1800 set_memory_rw);
1801 }
1802 if ((mod->module_init) && (mod->init_text_size)) {
1803 set_page_attributes(mod->module_init,
1804 mod->module_init + mod->init_text_size,
1805 set_memory_rw);
1806 }
1807 }
1808 mutex_unlock(&module_mutex);
1809}
1810
1811/* Iterate through all modules and set each module's text as RO */
Daniel J Blueman5d05c702011-03-08 22:01:47 +08001812void set_all_modules_text_ro(void)
matthieu castet84e1c6b2010-11-16 22:35:16 +01001813{
1814 struct module *mod;
1815
1816 mutex_lock(&module_mutex);
1817 list_for_each_entry_rcu(mod, &modules, list) {
Rusty Russell0d21b0e2013-01-12 11:38:44 +10301818 if (mod->state == MODULE_STATE_UNFORMED)
1819 continue;
matthieu castet84e1c6b2010-11-16 22:35:16 +01001820 if ((mod->module_core) && (mod->core_text_size)) {
1821 set_page_attributes(mod->module_core,
1822 mod->module_core + mod->core_text_size,
1823 set_memory_ro);
1824 }
1825 if ((mod->module_init) && (mod->init_text_size)) {
1826 set_page_attributes(mod->module_init,
1827 mod->module_init + mod->init_text_size,
1828 set_memory_ro);
1829 }
1830 }
1831 mutex_unlock(&module_mutex);
1832}
1833#else
1834static 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 -06001835static void unset_module_core_ro_nx(struct module *mod) { }
1836static void unset_module_init_ro_nx(struct module *mod) { }
matthieu castet84e1c6b2010-11-16 22:35:16 +01001837#endif
1838
Rusty Russellbe1f2212015-01-20 09:07:05 +10301839void __weak module_memfree(void *module_region)
Jonas Bonn74e08fc2011-06-30 21:22:11 +02001840{
1841 vfree(module_region);
1842}
1843
1844void __weak module_arch_cleanup(struct module *mod)
1845{
1846}
1847
Rusty Russelld453cde2015-01-20 09:07:04 +10301848void __weak module_arch_freeing_init(struct module *mod)
1849{
1850}
1851
Rusty Russell75676502010-06-05 11:17:36 -06001852/* Free a module, remove from lists, etc. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853static void free_module(struct module *mod)
1854{
Li Zefan7ead8b82009-08-17 16:56:28 +08001855 trace_module_free(mod);
1856
Rusty Russell36b03602010-08-05 12:59:09 -06001857 mod_sysfs_teardown(mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858
Rusty Russell944a1fa2013-04-17 13:20:03 +09301859 /* We leave it in list to prevent duplicate loads, but make sure
1860 * that noone uses it while it's being deconstructed. */
Prarit Bhargavad3051b42014-10-14 02:51:39 +10301861 mutex_lock(&module_mutex);
Rusty Russell944a1fa2013-04-17 13:20:03 +09301862 mod->state = MODULE_STATE_UNFORMED;
Prarit Bhargavad3051b42014-10-14 02:51:39 +10301863 mutex_unlock(&module_mutex);
Rusty Russell944a1fa2013-04-17 13:20:03 +09301864
Jason Baronb82bab4b2010-07-27 13:18:01 -07001865 /* Remove dynamic debug info */
1866 ddebug_remove_module(mod->name);
1867
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868 /* Arch-specific cleanup. */
1869 module_arch_cleanup(mod);
1870
1871 /* Module unload stuff */
1872 module_unload_free(mod);
1873
Rusty Russelle180a6b2009-03-31 13:05:29 -06001874 /* Free any allocated parameters. */
1875 destroy_params(mod->kp, mod->num_kp);
1876
Rusty Russell944a1fa2013-04-17 13:20:03 +09301877 /* Now we can delete it from the lists */
1878 mutex_lock(&module_mutex);
Masami Hiramatsu461e34a2014-11-10 09:27:29 +10301879 /* Unlink carefully: kallsyms could be walking list. */
1880 list_del_rcu(&mod->list);
Masami Hiramatsu0286b5e2014-11-10 09:28:29 +10301881 /* Remove this module from bug list, this uses list_del_rcu */
Masami Hiramatsu461e34a2014-11-10 09:27:29 +10301882 module_bug_cleanup(mod);
Peter Zijlstra0be964b2015-05-27 11:09:35 +09301883 /* Wait for RCU-sched synchronizing before releasing mod->list and buglist. */
1884 synchronize_sched();
Rusty Russell944a1fa2013-04-17 13:20:03 +09301885 mutex_unlock(&module_mutex);
1886
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887 /* This may be NULL, but that's OK */
Jan Glauber01526ed2011-05-19 16:55:26 -06001888 unset_module_init_ro_nx(mod);
Rusty Russelld453cde2015-01-20 09:07:04 +10301889 module_arch_freeing_init(mod);
Rusty Russellbe1f2212015-01-20 09:07:05 +10301890 module_memfree(mod->module_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891 kfree(mod->args);
Tejun Heo259354d2010-03-10 18:56:10 +09001892 percpu_modfree(mod);
Rusty Russell9f85a4b2010-08-05 12:59:04 -06001893
Peter Zijlstra35a93932015-02-26 16:23:11 +01001894 /* Free lock-classes; relies on the preceding sync_rcu(). */
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001895 lockdep_free_key_range(mod->module_core, mod->core_size);
1896
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897 /* Finally, free the core (containing the module structure) */
Jan Glauber01526ed2011-05-19 16:55:26 -06001898 unset_module_core_ro_nx(mod);
Rusty Russellbe1f2212015-01-20 09:07:05 +10301899 module_memfree(mod->module_core);
Bernd Schmidteb8cdec2009-09-21 17:03:57 -07001900
1901#ifdef CONFIG_MPU
1902 update_protections(current->mm);
1903#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904}
1905
1906void *__symbol_get(const char *symbol)
1907{
1908 struct module *owner;
Tim Abbott414fd312008-12-05 19:03:56 -05001909 const struct kernel_symbol *sym;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910
Rusty Russell24da1cb2007-07-15 23:41:46 -07001911 preempt_disable();
Tim Abbott414fd312008-12-05 19:03:56 -05001912 sym = find_symbol(symbol, &owner, NULL, true, true);
1913 if (sym && strong_try_module_get(owner))
1914 sym = NULL;
Rusty Russell24da1cb2007-07-15 23:41:46 -07001915 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916
Tim Abbott414fd312008-12-05 19:03:56 -05001917 return sym ? (void *)sym->value : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918}
1919EXPORT_SYMBOL_GPL(__symbol_get);
1920
Ashutosh Naikeea8b542006-01-08 01:04:25 -08001921/*
1922 * Ensure that an exported symbol [global namespace] does not already exist
Robert P. J. Day02a3e592007-05-09 07:26:28 +02001923 * in the kernel or in some other module's exported symbol table.
Rusty Russellbe593f42010-06-05 11:17:37 -06001924 *
1925 * You must hold the module_mutex.
Ashutosh Naikeea8b542006-01-08 01:04:25 -08001926 */
1927static int verify_export_symbols(struct module *mod)
1928{
Rusty Russellb2111042008-05-01 21:15:00 -05001929 unsigned int i;
Ashutosh Naikeea8b542006-01-08 01:04:25 -08001930 struct module *owner;
Rusty Russellb2111042008-05-01 21:15:00 -05001931 const struct kernel_symbol *s;
1932 struct {
1933 const struct kernel_symbol *sym;
1934 unsigned int num;
1935 } arr[] = {
1936 { mod->syms, mod->num_syms },
1937 { mod->gpl_syms, mod->num_gpl_syms },
1938 { mod->gpl_future_syms, mod->num_gpl_future_syms },
Denys Vlasenkof7f5b672008-07-22 19:24:26 -05001939#ifdef CONFIG_UNUSED_SYMBOLS
Rusty Russellb2111042008-05-01 21:15:00 -05001940 { mod->unused_syms, mod->num_unused_syms },
1941 { mod->unused_gpl_syms, mod->num_unused_gpl_syms },
Denys Vlasenkof7f5b672008-07-22 19:24:26 -05001942#endif
Rusty Russellb2111042008-05-01 21:15:00 -05001943 };
Ashutosh Naikeea8b542006-01-08 01:04:25 -08001944
Rusty Russellb2111042008-05-01 21:15:00 -05001945 for (i = 0; i < ARRAY_SIZE(arr); i++) {
1946 for (s = arr[i].sym; s < arr[i].sym + arr[i].num; s++) {
Rusty Russellbe593f42010-06-05 11:17:37 -06001947 if (find_symbol(s->name, &owner, NULL, true, false)) {
Andrew Mortonbddb12b2013-11-12 15:11:28 -08001948 pr_err("%s: exports duplicate symbol %s"
Rusty Russellb2111042008-05-01 21:15:00 -05001949 " (owned by %s)\n",
1950 mod->name, s->name, module_name(owner));
1951 return -ENOEXEC;
1952 }
Ashutosh Naikeea8b542006-01-08 01:04:25 -08001953 }
Rusty Russellb2111042008-05-01 21:15:00 -05001954 }
1955 return 0;
Ashutosh Naikeea8b542006-01-08 01:04:25 -08001956}
1957
Matti Linnanvuori9a4b9702007-11-08 08:37:38 -08001958/* Change all symbols so that st_value encodes the pointer directly. */
Rusty Russell49668682010-08-05 12:59:10 -06001959static int simplify_symbols(struct module *mod, const struct load_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960{
Rusty Russell49668682010-08-05 12:59:10 -06001961 Elf_Shdr *symsec = &info->sechdrs[info->index.sym];
1962 Elf_Sym *sym = (void *)symsec->sh_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963 unsigned long secbase;
Rusty Russell49668682010-08-05 12:59:10 -06001964 unsigned int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965 int ret = 0;
Tim Abbott414fd312008-12-05 19:03:56 -05001966 const struct kernel_symbol *ksym;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967
Rusty Russell49668682010-08-05 12:59:10 -06001968 for (i = 1; i < symsec->sh_size / sizeof(Elf_Sym); i++) {
1969 const char *name = info->strtab + sym[i].st_name;
1970
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971 switch (sym[i].st_shndx) {
1972 case SHN_COMMON:
Joe Mario80375982014-02-08 09:01:09 +01001973 /* Ignore common symbols */
1974 if (!strncmp(name, "__gnu_lto", 9))
1975 break;
1976
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977 /* We compiled with -fno-common. These are not
1978 supposed to happen. */
Jim Cromie5e124162011-12-06 12:11:31 -07001979 pr_debug("Common symbol: %s\n", name);
Ionut Alexa6da0b562014-11-10 09:31:29 +10301980 pr_warn("%s: please compile with -fno-common\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981 mod->name);
1982 ret = -ENOEXEC;
1983 break;
1984
1985 case SHN_ABS:
1986 /* Don't need to do anything */
Jim Cromie5e124162011-12-06 12:11:31 -07001987 pr_debug("Absolute symbol: 0x%08lx\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988 (long)sym[i].st_value);
1989 break;
1990
1991 case SHN_UNDEF:
Rusty Russell49668682010-08-05 12:59:10 -06001992 ksym = resolve_symbol_wait(mod, info, name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993 /* Ok if resolved. */
Rusty Russell9bea7f22010-06-05 11:17:37 -06001994 if (ksym && !IS_ERR(ksym)) {
Tim Abbott414fd312008-12-05 19:03:56 -05001995 sym[i].st_value = ksym->value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996 break;
Tim Abbott414fd312008-12-05 19:03:56 -05001997 }
1998
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999 /* Ok if weak. */
Rusty Russell9bea7f22010-06-05 11:17:37 -06002000 if (!ksym && ELF_ST_BIND(sym[i].st_info) == STB_WEAK)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001 break;
2002
Andrew Mortonbddb12b2013-11-12 15:11:28 -08002003 pr_warn("%s: Unknown symbol %s (err %li)\n",
2004 mod->name, name, PTR_ERR(ksym));
Rusty Russell9bea7f22010-06-05 11:17:37 -06002005 ret = PTR_ERR(ksym) ?: -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006 break;
2007
2008 default:
2009 /* Divert to percpu allocation if a percpu var. */
Rusty Russell49668682010-08-05 12:59:10 -06002010 if (sym[i].st_shndx == info->index.pcpu)
Tejun Heo259354d2010-03-10 18:56:10 +09002011 secbase = (unsigned long)mod_percpu(mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012 else
Rusty Russell49668682010-08-05 12:59:10 -06002013 secbase = info->sechdrs[sym[i].st_shndx].sh_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014 sym[i].st_value += secbase;
2015 break;
2016 }
2017 }
2018
2019 return ret;
2020}
2021
Rusty Russell49668682010-08-05 12:59:10 -06002022static int apply_relocations(struct module *mod, const struct load_info *info)
Rusty Russell22e268e2010-08-05 12:59:05 -06002023{
2024 unsigned int i;
2025 int err = 0;
2026
2027 /* Now do relocations. */
Rusty Russell49668682010-08-05 12:59:10 -06002028 for (i = 1; i < info->hdr->e_shnum; i++) {
2029 unsigned int infosec = info->sechdrs[i].sh_info;
Rusty Russell22e268e2010-08-05 12:59:05 -06002030
2031 /* Not a valid relocation section? */
Rusty Russell49668682010-08-05 12:59:10 -06002032 if (infosec >= info->hdr->e_shnum)
Rusty Russell22e268e2010-08-05 12:59:05 -06002033 continue;
2034
2035 /* Don't bother with non-allocated sections */
Rusty Russell49668682010-08-05 12:59:10 -06002036 if (!(info->sechdrs[infosec].sh_flags & SHF_ALLOC))
Rusty Russell22e268e2010-08-05 12:59:05 -06002037 continue;
2038
Rusty Russell49668682010-08-05 12:59:10 -06002039 if (info->sechdrs[i].sh_type == SHT_REL)
2040 err = apply_relocate(info->sechdrs, info->strtab,
2041 info->index.sym, i, mod);
2042 else if (info->sechdrs[i].sh_type == SHT_RELA)
2043 err = apply_relocate_add(info->sechdrs, info->strtab,
2044 info->index.sym, i, mod);
Rusty Russell22e268e2010-08-05 12:59:05 -06002045 if (err < 0)
2046 break;
2047 }
2048 return err;
2049}
2050
Helge Deller088af9a2008-12-31 12:31:18 +01002051/* Additional bytes needed by arch in front of individual sections */
2052unsigned int __weak arch_mod_section_prepend(struct module *mod,
2053 unsigned int section)
2054{
2055 /* default implementation just returns zero */
2056 return 0;
2057}
2058
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059/* Update size with this section: return offset. */
Helge Deller088af9a2008-12-31 12:31:18 +01002060static long get_offset(struct module *mod, unsigned int *size,
2061 Elf_Shdr *sechdr, unsigned int section)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062{
2063 long ret;
2064
Helge Deller088af9a2008-12-31 12:31:18 +01002065 *size += arch_mod_section_prepend(mod, section);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066 ret = ALIGN(*size, sechdr->sh_addralign ?: 1);
2067 *size = ret + sechdr->sh_size;
2068 return ret;
2069}
2070
2071/* Lay out the SHF_ALLOC sections in a way not dissimilar to how ld
2072 might -- code, read-only data, read-write data, small data. Tally
2073 sizes, and place the offsets into sh_entsize fields: high bit means it
2074 belongs in init. */
Rusty Russell49668682010-08-05 12:59:10 -06002075static void layout_sections(struct module *mod, struct load_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076{
2077 static unsigned long const masks[][2] = {
2078 /* NOTE: all executable code must be the first section
2079 * in this array; otherwise modify the text_size
2080 * finder in the two loops below */
2081 { SHF_EXECINSTR | SHF_ALLOC, ARCH_SHF_SMALL },
2082 { SHF_ALLOC, SHF_WRITE | ARCH_SHF_SMALL },
2083 { SHF_WRITE | SHF_ALLOC, ARCH_SHF_SMALL },
2084 { ARCH_SHF_SMALL | SHF_ALLOC, 0 }
2085 };
2086 unsigned int m, i;
2087
Rusty Russell49668682010-08-05 12:59:10 -06002088 for (i = 0; i < info->hdr->e_shnum; i++)
2089 info->sechdrs[i].sh_entsize = ~0UL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090
Jim Cromie5e124162011-12-06 12:11:31 -07002091 pr_debug("Core section allocation order:\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092 for (m = 0; m < ARRAY_SIZE(masks); ++m) {
Rusty Russell49668682010-08-05 12:59:10 -06002093 for (i = 0; i < info->hdr->e_shnum; ++i) {
2094 Elf_Shdr *s = &info->sechdrs[i];
2095 const char *sname = info->secstrings + s->sh_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096
2097 if ((s->sh_flags & masks[m][0]) != masks[m][0]
2098 || (s->sh_flags & masks[m][1])
2099 || s->sh_entsize != ~0UL
Rusty Russell49668682010-08-05 12:59:10 -06002100 || strstarts(sname, ".init"))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101 continue;
Helge Deller088af9a2008-12-31 12:31:18 +01002102 s->sh_entsize = get_offset(mod, &mod->core_size, s, i);
Jim Cromie5e124162011-12-06 12:11:31 -07002103 pr_debug("\t%s\n", sname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104 }
matthieu castet84e1c6b2010-11-16 22:35:16 +01002105 switch (m) {
2106 case 0: /* executable */
2107 mod->core_size = debug_align(mod->core_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002108 mod->core_text_size = mod->core_size;
matthieu castet84e1c6b2010-11-16 22:35:16 +01002109 break;
2110 case 1: /* RO: text and ro-data */
2111 mod->core_size = debug_align(mod->core_size);
2112 mod->core_ro_size = mod->core_size;
2113 break;
2114 case 3: /* whole core */
2115 mod->core_size = debug_align(mod->core_size);
2116 break;
2117 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118 }
2119
Jim Cromie5e124162011-12-06 12:11:31 -07002120 pr_debug("Init section allocation order:\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121 for (m = 0; m < ARRAY_SIZE(masks); ++m) {
Rusty Russell49668682010-08-05 12:59:10 -06002122 for (i = 0; i < info->hdr->e_shnum; ++i) {
2123 Elf_Shdr *s = &info->sechdrs[i];
2124 const char *sname = info->secstrings + s->sh_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125
2126 if ((s->sh_flags & masks[m][0]) != masks[m][0]
2127 || (s->sh_flags & masks[m][1])
2128 || s->sh_entsize != ~0UL
Rusty Russell49668682010-08-05 12:59:10 -06002129 || !strstarts(sname, ".init"))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130 continue;
Helge Deller088af9a2008-12-31 12:31:18 +01002131 s->sh_entsize = (get_offset(mod, &mod->init_size, s, i)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132 | INIT_OFFSET_MASK);
Jim Cromie5e124162011-12-06 12:11:31 -07002133 pr_debug("\t%s\n", sname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002134 }
matthieu castet84e1c6b2010-11-16 22:35:16 +01002135 switch (m) {
2136 case 0: /* executable */
2137 mod->init_size = debug_align(mod->init_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002138 mod->init_text_size = mod->init_size;
matthieu castet84e1c6b2010-11-16 22:35:16 +01002139 break;
2140 case 1: /* RO: text and ro-data */
2141 mod->init_size = debug_align(mod->init_size);
2142 mod->init_ro_size = mod->init_size;
2143 break;
2144 case 3: /* whole init */
2145 mod->init_size = debug_align(mod->init_size);
2146 break;
2147 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148 }
2149}
2150
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151static void set_license(struct module *mod, const char *license)
2152{
2153 if (!license)
2154 license = "unspecified";
2155
Florin Malitafa3ba2e82006-10-11 01:21:48 -07002156 if (!license_is_gpl_compatible(license)) {
Andi Kleen25ddbb12008-10-15 22:01:41 -07002157 if (!test_taint(TAINT_PROPRIETARY_MODULE))
Andrew Mortonbddb12b2013-11-12 15:11:28 -08002158 pr_warn("%s: module license '%s' taints kernel.\n",
2159 mod->name, license);
Rusty Russell373d4d02013-01-21 17:17:39 +10302160 add_taint_module(mod, TAINT_PROPRIETARY_MODULE,
2161 LOCKDEP_NOW_UNRELIABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162 }
2163}
2164
2165/* Parse tag=value strings from .modinfo section */
2166static char *next_string(char *string, unsigned long *secsize)
2167{
2168 /* Skip non-zero chars */
2169 while (string[0]) {
2170 string++;
2171 if ((*secsize)-- <= 1)
2172 return NULL;
2173 }
2174
2175 /* Skip any zero padding. */
2176 while (!string[0]) {
2177 string++;
2178 if ((*secsize)-- <= 1)
2179 return NULL;
2180 }
2181 return string;
2182}
2183
Rusty Russell49668682010-08-05 12:59:10 -06002184static char *get_modinfo(struct load_info *info, const char *tag)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185{
2186 char *p;
2187 unsigned int taglen = strlen(tag);
Rusty Russell49668682010-08-05 12:59:10 -06002188 Elf_Shdr *infosec = &info->sechdrs[info->index.info];
2189 unsigned long size = infosec->sh_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190
Rusty Russell49668682010-08-05 12:59:10 -06002191 for (p = (char *)infosec->sh_addr; p; p = next_string(p, &size)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192 if (strncmp(p, tag, taglen) == 0 && p[taglen] == '=')
2193 return p + taglen + 1;
2194 }
2195 return NULL;
2196}
2197
Rusty Russell49668682010-08-05 12:59:10 -06002198static void setup_modinfo(struct module *mod, struct load_info *info)
Matt Domschc988d2b2005-06-23 22:05:15 -07002199{
2200 struct module_attribute *attr;
2201 int i;
2202
2203 for (i = 0; (attr = modinfo_attrs[i]); i++) {
2204 if (attr->setup)
Rusty Russell49668682010-08-05 12:59:10 -06002205 attr->setup(mod, get_modinfo(info, attr->attr.name));
Matt Domschc988d2b2005-06-23 22:05:15 -07002206 }
2207}
Matt Domschc988d2b2005-06-23 22:05:15 -07002208
Rusty Russella263f772009-09-25 00:32:58 -06002209static void free_modinfo(struct module *mod)
2210{
2211 struct module_attribute *attr;
2212 int i;
2213
2214 for (i = 0; (attr = modinfo_attrs[i]); i++) {
2215 if (attr->free)
2216 attr->free(mod);
2217 }
2218}
2219
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220#ifdef CONFIG_KALLSYMS
WANG Cong15bba372008-07-24 15:41:48 +01002221
2222/* lookup symbol in given range of kernel_symbols */
2223static const struct kernel_symbol *lookup_symbol(const char *name,
2224 const struct kernel_symbol *start,
2225 const struct kernel_symbol *stop)
2226{
Alessio Igor Bogani9d634872011-05-18 22:35:59 +02002227 return bsearch(name, start, stop - start,
2228 sizeof(struct kernel_symbol), cmp_name);
WANG Cong15bba372008-07-24 15:41:48 +01002229}
2230
Tim Abbottca4787b2009-01-05 08:40:10 -06002231static int is_exported(const char *name, unsigned long value,
2232 const struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002233{
Tim Abbottca4787b2009-01-05 08:40:10 -06002234 const struct kernel_symbol *ks;
2235 if (!mod)
2236 ks = lookup_symbol(name, __start___ksymtab, __stop___ksymtab);
Sam Ravnborg3fd68052006-02-08 21:16:45 +01002237 else
Tim Abbottca4787b2009-01-05 08:40:10 -06002238 ks = lookup_symbol(name, mod->syms, mod->syms + mod->num_syms);
2239 return ks != NULL && ks->value == value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002240}
2241
2242/* As per nm */
Rusty Russelleded41c2010-08-05 12:59:07 -06002243static char elf_type(const Elf_Sym *sym, const struct load_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002244{
Rusty Russelleded41c2010-08-05 12:59:07 -06002245 const Elf_Shdr *sechdrs = info->sechdrs;
2246
Linus Torvalds1da177e2005-04-16 15:20:36 -07002247 if (ELF_ST_BIND(sym->st_info) == STB_WEAK) {
2248 if (ELF_ST_TYPE(sym->st_info) == STT_OBJECT)
2249 return 'v';
2250 else
2251 return 'w';
2252 }
2253 if (sym->st_shndx == SHN_UNDEF)
2254 return 'U';
2255 if (sym->st_shndx == SHN_ABS)
2256 return 'a';
2257 if (sym->st_shndx >= SHN_LORESERVE)
2258 return '?';
2259 if (sechdrs[sym->st_shndx].sh_flags & SHF_EXECINSTR)
2260 return 't';
2261 if (sechdrs[sym->st_shndx].sh_flags & SHF_ALLOC
2262 && sechdrs[sym->st_shndx].sh_type != SHT_NOBITS) {
2263 if (!(sechdrs[sym->st_shndx].sh_flags & SHF_WRITE))
2264 return 'r';
2265 else if (sechdrs[sym->st_shndx].sh_flags & ARCH_SHF_SMALL)
2266 return 'g';
2267 else
2268 return 'd';
2269 }
2270 if (sechdrs[sym->st_shndx].sh_type == SHT_NOBITS) {
2271 if (sechdrs[sym->st_shndx].sh_flags & ARCH_SHF_SMALL)
2272 return 's';
2273 else
2274 return 'b';
2275 }
Rusty Russelleded41c2010-08-05 12:59:07 -06002276 if (strstarts(info->secstrings + sechdrs[sym->st_shndx].sh_name,
2277 ".debug")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002278 return 'n';
Rusty Russelleded41c2010-08-05 12:59:07 -06002279 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002280 return '?';
2281}
2282
Jan Beulich4a496222009-07-06 14:50:42 +01002283static bool is_core_symbol(const Elf_Sym *src, const Elf_Shdr *sechdrs,
Ionut Alexa6da0b562014-11-10 09:31:29 +10302284 unsigned int shnum)
Jan Beulich4a496222009-07-06 14:50:42 +01002285{
2286 const Elf_Shdr *sec;
2287
2288 if (src->st_shndx == SHN_UNDEF
2289 || src->st_shndx >= shnum
2290 || !src->st_name)
2291 return false;
2292
2293 sec = sechdrs + src->st_shndx;
2294 if (!(sec->sh_flags & SHF_ALLOC)
2295#ifndef CONFIG_KALLSYMS_ALL
2296 || !(sec->sh_flags & SHF_EXECINSTR)
2297#endif
2298 || (sec->sh_entsize & INIT_OFFSET_MASK))
2299 return false;
2300
2301 return true;
2302}
2303
Kevin Cernekee48fd1182012-01-13 09:32:14 +10302304/*
2305 * We only allocate and copy the strings needed by the parts of symtab
2306 * we keep. This is simple, but has the effect of making multiple
2307 * copies of duplicates. We could be more sophisticated, see
2308 * linux-kernel thread starting with
2309 * <73defb5e4bca04a6431392cc341112b1@localhost>.
2310 */
Rusty Russell49668682010-08-05 12:59:10 -06002311static void layout_symtab(struct module *mod, struct load_info *info)
Jan Beulich4a496222009-07-06 14:50:42 +01002312{
Rusty Russell49668682010-08-05 12:59:10 -06002313 Elf_Shdr *symsect = info->sechdrs + info->index.sym;
2314 Elf_Shdr *strsect = info->sechdrs + info->index.str;
Jan Beulich4a496222009-07-06 14:50:42 +01002315 const Elf_Sym *src;
Satoru Takeuchi54523ec2012-12-05 12:29:04 +10302316 unsigned int i, nsrc, ndst, strtab_size = 0;
Jan Beulich4a496222009-07-06 14:50:42 +01002317
2318 /* Put symbol section at end of init part of module. */
2319 symsect->sh_flags |= SHF_ALLOC;
2320 symsect->sh_entsize = get_offset(mod, &mod->init_size, symsect,
Rusty Russell49668682010-08-05 12:59:10 -06002321 info->index.sym) | INIT_OFFSET_MASK;
Jim Cromie5e124162011-12-06 12:11:31 -07002322 pr_debug("\t%s\n", info->secstrings + symsect->sh_name);
Jan Beulich4a496222009-07-06 14:50:42 +01002323
Rusty Russell49668682010-08-05 12:59:10 -06002324 src = (void *)info->hdr + symsect->sh_offset;
Jan Beulich4a496222009-07-06 14:50:42 +01002325 nsrc = symsect->sh_size / sizeof(*src);
Kevin Cernekee70b1e9162011-11-12 19:08:55 -08002326
Kevin Cernekee48fd1182012-01-13 09:32:14 +10302327 /* Compute total space required for the core symbols' strtab. */
Rusty Russell59ef28b2012-10-25 10:49:25 +10302328 for (ndst = i = 0; i < nsrc; i++) {
2329 if (i == 0 ||
2330 is_core_symbol(src+i, info->sechdrs, info->hdr->e_shnum)) {
2331 strtab_size += strlen(&info->strtab[src[i].st_name])+1;
Kevin Cernekee48fd1182012-01-13 09:32:14 +10302332 ndst++;
Jan Beulich554bdfe2009-07-06 14:51:44 +01002333 }
Rusty Russell59ef28b2012-10-25 10:49:25 +10302334 }
Jan Beulich4a496222009-07-06 14:50:42 +01002335
2336 /* Append room for core symbols at end of core part. */
Rusty Russell49668682010-08-05 12:59:10 -06002337 info->symoffs = ALIGN(mod->core_size, symsect->sh_addralign ?: 1);
Kevin Cernekee48fd1182012-01-13 09:32:14 +10302338 info->stroffs = mod->core_size = info->symoffs + ndst * sizeof(Elf_Sym);
2339 mod->core_size += strtab_size;
Laura Abbott168e47f2015-02-25 14:14:57 -08002340 mod->core_size = debug_align(mod->core_size);
Jan Beulich4a496222009-07-06 14:50:42 +01002341
Jan Beulich554bdfe2009-07-06 14:51:44 +01002342 /* Put string table section at end of init part of module. */
2343 strsect->sh_flags |= SHF_ALLOC;
2344 strsect->sh_entsize = get_offset(mod, &mod->init_size, strsect,
Rusty Russell49668682010-08-05 12:59:10 -06002345 info->index.str) | INIT_OFFSET_MASK;
Laura Abbott168e47f2015-02-25 14:14:57 -08002346 mod->init_size = debug_align(mod->init_size);
Jim Cromie5e124162011-12-06 12:11:31 -07002347 pr_debug("\t%s\n", info->secstrings + strsect->sh_name);
Jan Beulich4a496222009-07-06 14:50:42 +01002348}
2349
Rusty Russell811d66a2010-08-05 12:59:12 -06002350static void add_kallsyms(struct module *mod, const struct load_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002351{
Jan Beulich4a496222009-07-06 14:50:42 +01002352 unsigned int i, ndst;
2353 const Elf_Sym *src;
2354 Elf_Sym *dst;
Jan Beulich554bdfe2009-07-06 14:51:44 +01002355 char *s;
Rusty Russelleded41c2010-08-05 12:59:07 -06002356 Elf_Shdr *symsec = &info->sechdrs[info->index.sym];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002357
Rusty Russelleded41c2010-08-05 12:59:07 -06002358 mod->symtab = (void *)symsec->sh_addr;
2359 mod->num_symtab = symsec->sh_size / sizeof(Elf_Sym);
Rusty Russell511ca6a2010-08-05 12:59:08 -06002360 /* Make sure we get permanent strtab: don't use info->strtab. */
2361 mod->strtab = (void *)info->sechdrs[info->index.str].sh_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002362
2363 /* Set types up while we still have access to sections. */
2364 for (i = 0; i < mod->num_symtab; i++)
Rusty Russelleded41c2010-08-05 12:59:07 -06002365 mod->symtab[i].st_info = elf_type(&mod->symtab[i], info);
Jan Beulich4a496222009-07-06 14:50:42 +01002366
Rusty Russelld9131882010-08-05 12:59:08 -06002367 mod->core_symtab = dst = mod->module_core + info->symoffs;
Kevin Cernekee48fd1182012-01-13 09:32:14 +10302368 mod->core_strtab = s = mod->module_core + info->stroffs;
Jan Beulich4a496222009-07-06 14:50:42 +01002369 src = mod->symtab;
Rusty Russell59ef28b2012-10-25 10:49:25 +10302370 for (ndst = i = 0; i < mod->num_symtab; i++) {
2371 if (i == 0 ||
2372 is_core_symbol(src+i, info->sechdrs, info->hdr->e_shnum)) {
2373 dst[ndst] = src[i];
2374 dst[ndst++].st_name = s - mod->core_strtab;
2375 s += strlcpy(s, &mod->strtab[src[i].st_name],
2376 KSYM_NAME_LEN) + 1;
2377 }
Jan Beulich4a496222009-07-06 14:50:42 +01002378 }
2379 mod->core_num_syms = ndst;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002380}
2381#else
Rusty Russell49668682010-08-05 12:59:10 -06002382static inline void layout_symtab(struct module *mod, struct load_info *info)
Jan Beulich4a496222009-07-06 14:50:42 +01002383{
2384}
Paul Mundt3ae91c22009-10-01 15:43:54 -07002385
Michał Mirosławabbce902010-09-20 01:58:08 +02002386static void add_kallsyms(struct module *mod, const struct load_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002387{
2388}
2389#endif /* CONFIG_KALLSYMS */
2390
Jason Barone9d376f2009-02-05 11:51:38 -05002391static void dynamic_debug_setup(struct _ddebug *debug, unsigned int num)
Rusty Russell5e458cc2008-10-22 10:00:13 -05002392{
Rusty Russell811d66a2010-08-05 12:59:12 -06002393 if (!debug)
2394 return;
Jason Barone9d376f2009-02-05 11:51:38 -05002395#ifdef CONFIG_DYNAMIC_DEBUG
2396 if (ddebug_add_module(debug, num, debug->modname))
Andrew Mortonbddb12b2013-11-12 15:11:28 -08002397 pr_err("dynamic debug error adding module: %s\n",
2398 debug->modname);
Jason Barone9d376f2009-02-05 11:51:38 -05002399#endif
Rusty Russell5e458cc2008-10-22 10:00:13 -05002400}
Jason Baron346e15b2008-08-12 16:46:19 -04002401
Yehuda Sadehff49d742010-07-03 13:07:35 +10002402static void dynamic_debug_remove(struct _ddebug *debug)
2403{
2404 if (debug)
2405 ddebug_remove_module(debug->modname);
2406}
2407
Jonas Bonn74e08fc2011-06-30 21:22:11 +02002408void * __weak module_alloc(unsigned long size)
2409{
Rusty Russell82fab442012-12-11 09:38:33 +10302410 return vmalloc_exec(size);
Jonas Bonn74e08fc2011-06-30 21:22:11 +02002411}
2412
Rusty Russell3a642e92008-07-22 19:24:28 -05002413static void *module_alloc_update_bounds(unsigned long size)
2414{
2415 void *ret = module_alloc(size);
2416
2417 if (ret) {
Rusty Russell75676502010-06-05 11:17:36 -06002418 mutex_lock(&module_mutex);
Rusty Russell3a642e92008-07-22 19:24:28 -05002419 /* Update module bounds. */
2420 if ((unsigned long)ret < module_addr_min)
2421 module_addr_min = (unsigned long)ret;
2422 if ((unsigned long)ret + size > module_addr_max)
2423 module_addr_max = (unsigned long)ret + size;
Rusty Russell75676502010-06-05 11:17:36 -06002424 mutex_unlock(&module_mutex);
Rusty Russell3a642e92008-07-22 19:24:28 -05002425 }
2426 return ret;
2427}
2428
Catalin Marinas4f2294b2009-06-11 13:23:20 +01002429#ifdef CONFIG_DEBUG_KMEMLEAK
Rusty Russell49668682010-08-05 12:59:10 -06002430static void kmemleak_load_module(const struct module *mod,
2431 const struct load_info *info)
Catalin Marinas4f2294b2009-06-11 13:23:20 +01002432{
2433 unsigned int i;
2434
2435 /* only scan the sections containing data */
Catalin Marinasc017b4b2009-10-28 13:33:09 +00002436 kmemleak_scan_area(mod, sizeof(struct module), GFP_KERNEL);
Catalin Marinas4f2294b2009-06-11 13:23:20 +01002437
Rusty Russell49668682010-08-05 12:59:10 -06002438 for (i = 1; i < info->hdr->e_shnum; i++) {
Steven Rostedt06c94942013-05-15 20:33:01 +01002439 /* Scan all writable sections that's not executable */
2440 if (!(info->sechdrs[i].sh_flags & SHF_ALLOC) ||
2441 !(info->sechdrs[i].sh_flags & SHF_WRITE) ||
2442 (info->sechdrs[i].sh_flags & SHF_EXECINSTR))
Catalin Marinas4f2294b2009-06-11 13:23:20 +01002443 continue;
2444
Rusty Russell49668682010-08-05 12:59:10 -06002445 kmemleak_scan_area((void *)info->sechdrs[i].sh_addr,
2446 info->sechdrs[i].sh_size, GFP_KERNEL);
Catalin Marinas4f2294b2009-06-11 13:23:20 +01002447 }
2448}
2449#else
Rusty Russell49668682010-08-05 12:59:10 -06002450static inline void kmemleak_load_module(const struct module *mod,
2451 const struct load_info *info)
Catalin Marinas4f2294b2009-06-11 13:23:20 +01002452{
2453}
2454#endif
2455
Rusty Russell106a4ee2012-09-26 10:09:40 +01002456#ifdef CONFIG_MODULE_SIG
Kees Cook34e11692012-10-16 07:31:07 +10302457static int module_sig_check(struct load_info *info)
Rusty Russell106a4ee2012-09-26 10:09:40 +01002458{
2459 int err = -ENOKEY;
Kees Cook34e11692012-10-16 07:31:07 +10302460 const unsigned long markerlen = sizeof(MODULE_SIG_STRING) - 1;
2461 const void *mod = info->hdr;
Rusty Russell106a4ee2012-09-26 10:09:40 +01002462
Kees Cook34e11692012-10-16 07:31:07 +10302463 if (info->len > markerlen &&
2464 memcmp(mod + info->len - markerlen, MODULE_SIG_STRING, markerlen) == 0) {
David Howellscaabe242012-10-20 01:19:29 +01002465 /* We truncate the module to discard the signature */
Kees Cook34e11692012-10-16 07:31:07 +10302466 info->len -= markerlen;
2467 err = mod_verify_sig(mod, &info->len);
Rusty Russell106a4ee2012-09-26 10:09:40 +01002468 }
2469
2470 if (!err) {
2471 info->sig_ok = true;
2472 return 0;
2473 }
2474
2475 /* Not having a signature is only an error if we're strict. */
2476 if (err == -ENOKEY && !sig_enforce)
2477 err = 0;
2478
2479 return err;
2480}
2481#else /* !CONFIG_MODULE_SIG */
Kees Cook34e11692012-10-16 07:31:07 +10302482static int module_sig_check(struct load_info *info)
Rusty Russell106a4ee2012-09-26 10:09:40 +01002483{
2484 return 0;
2485}
2486#endif /* !CONFIG_MODULE_SIG */
2487
Kees Cook34e11692012-10-16 07:31:07 +10302488/* Sanity checks against invalid binaries, wrong arch, weird elf version. */
2489static int elf_header_check(struct load_info *info)
Rusty Russell40dd2562010-08-05 12:59:03 -06002490{
Kees Cook34e11692012-10-16 07:31:07 +10302491 if (info->len < sizeof(*(info->hdr)))
Rusty Russell40dd2562010-08-05 12:59:03 -06002492 return -ENOEXEC;
2493
Kees Cook34e11692012-10-16 07:31:07 +10302494 if (memcmp(info->hdr->e_ident, ELFMAG, SELFMAG) != 0
2495 || info->hdr->e_type != ET_REL
2496 || !elf_check_arch(info->hdr)
2497 || info->hdr->e_shentsize != sizeof(Elf_Shdr))
2498 return -ENOEXEC;
2499
2500 if (info->hdr->e_shoff >= info->len
2501 || (info->hdr->e_shnum * sizeof(Elf_Shdr) >
2502 info->len - info->hdr->e_shoff))
2503 return -ENOEXEC;
2504
2505 return 0;
2506}
2507
Linus Torvalds3afe9f82015-04-07 10:33:49 -07002508#define COPY_CHUNK_SIZE (16*PAGE_SIZE)
2509
2510static int copy_chunked_from_user(void *dst, const void __user *usrc, unsigned long len)
2511{
2512 do {
2513 unsigned long n = min(len, COPY_CHUNK_SIZE);
2514
2515 if (copy_from_user(dst, usrc, n) != 0)
2516 return -EFAULT;
2517 cond_resched();
2518 dst += n;
2519 usrc += n;
2520 len -= n;
2521 } while (len);
2522 return 0;
2523}
2524
Kees Cook34e11692012-10-16 07:31:07 +10302525/* Sets info->hdr and info->len. */
2526static int copy_module_from_user(const void __user *umod, unsigned long len,
2527 struct load_info *info)
2528{
Kees Cook2e72d512012-10-16 07:32:07 +10302529 int err;
2530
Kees Cook34e11692012-10-16 07:31:07 +10302531 info->len = len;
2532 if (info->len < sizeof(*(info->hdr)))
Rusty Russell40dd2562010-08-05 12:59:03 -06002533 return -ENOEXEC;
2534
Kees Cook2e72d512012-10-16 07:32:07 +10302535 err = security_kernel_module_from_file(NULL);
2536 if (err)
2537 return err;
2538
Rusty Russell40dd2562010-08-05 12:59:03 -06002539 /* Suck in entire file: we'll want most of it. */
Kirill A. Shutemovcc9e6052015-03-24 12:31:40 +10302540 info->hdr = __vmalloc(info->len,
2541 GFP_KERNEL | __GFP_HIGHMEM | __GFP_NOWARN, PAGE_KERNEL);
Kees Cook34e11692012-10-16 07:31:07 +10302542 if (!info->hdr)
Rusty Russell40dd2562010-08-05 12:59:03 -06002543 return -ENOMEM;
2544
Linus Torvalds3afe9f82015-04-07 10:33:49 -07002545 if (copy_chunked_from_user(info->hdr, umod, info->len) != 0) {
Kees Cook34e11692012-10-16 07:31:07 +10302546 vfree(info->hdr);
2547 return -EFAULT;
Rusty Russell40dd2562010-08-05 12:59:03 -06002548 }
2549
Rusty Russell40dd2562010-08-05 12:59:03 -06002550 return 0;
Kees Cook34e11692012-10-16 07:31:07 +10302551}
Rusty Russell40dd2562010-08-05 12:59:03 -06002552
Kees Cook34e11692012-10-16 07:31:07 +10302553/* Sets info->hdr and info->len. */
2554static int copy_module_from_fd(int fd, struct load_info *info)
2555{
Al Viroa2e05782013-08-30 12:41:41 -04002556 struct fd f = fdget(fd);
Kees Cook34e11692012-10-16 07:31:07 +10302557 int err;
2558 struct kstat stat;
2559 loff_t pos;
2560 ssize_t bytes = 0;
2561
Al Viroa2e05782013-08-30 12:41:41 -04002562 if (!f.file)
Kees Cook34e11692012-10-16 07:31:07 +10302563 return -ENOEXEC;
2564
Al Viroa2e05782013-08-30 12:41:41 -04002565 err = security_kernel_module_from_file(f.file);
Kees Cook2e72d512012-10-16 07:32:07 +10302566 if (err)
2567 goto out;
2568
Al Viroa2e05782013-08-30 12:41:41 -04002569 err = vfs_getattr(&f.file->f_path, &stat);
Kees Cook34e11692012-10-16 07:31:07 +10302570 if (err)
2571 goto out;
2572
2573 if (stat.size > INT_MAX) {
2574 err = -EFBIG;
2575 goto out;
2576 }
Sasha Levin52441fa2013-01-03 11:09:53 +10302577
2578 /* Don't hand 0 to vmalloc, it whines. */
2579 if (stat.size == 0) {
2580 err = -EINVAL;
2581 goto out;
2582 }
2583
Kees Cook34e11692012-10-16 07:31:07 +10302584 info->hdr = vmalloc(stat.size);
2585 if (!info->hdr) {
2586 err = -ENOMEM;
2587 goto out;
2588 }
2589
2590 pos = 0;
2591 while (pos < stat.size) {
Al Viroa2e05782013-08-30 12:41:41 -04002592 bytes = kernel_read(f.file, pos, (char *)(info->hdr) + pos,
Kees Cook34e11692012-10-16 07:31:07 +10302593 stat.size - pos);
2594 if (bytes < 0) {
2595 vfree(info->hdr);
2596 err = bytes;
2597 goto out;
2598 }
2599 if (bytes == 0)
2600 break;
2601 pos += bytes;
2602 }
2603 info->len = pos;
2604
2605out:
Al Viroa2e05782013-08-30 12:41:41 -04002606 fdput(f);
Rusty Russell40dd2562010-08-05 12:59:03 -06002607 return err;
2608}
2609
Rusty Russelld9131882010-08-05 12:59:08 -06002610static void free_copy(struct load_info *info)
2611{
Rusty Russelld9131882010-08-05 12:59:08 -06002612 vfree(info->hdr);
2613}
2614
Rusty Russell2f3238a2012-10-22 18:09:41 +10302615static int rewrite_section_headers(struct load_info *info, int flags)
Rusty Russell8b5f61a2010-08-05 12:59:06 -06002616{
2617 unsigned int i;
2618
2619 /* This should always be true, but let's be sure. */
2620 info->sechdrs[0].sh_addr = 0;
2621
2622 for (i = 1; i < info->hdr->e_shnum; i++) {
2623 Elf_Shdr *shdr = &info->sechdrs[i];
2624 if (shdr->sh_type != SHT_NOBITS
2625 && info->len < shdr->sh_offset + shdr->sh_size) {
Andrew Mortonbddb12b2013-11-12 15:11:28 -08002626 pr_err("Module len %lu truncated\n", info->len);
Rusty Russell8b5f61a2010-08-05 12:59:06 -06002627 return -ENOEXEC;
2628 }
2629
2630 /* Mark all sections sh_addr with their address in the
2631 temporary image. */
2632 shdr->sh_addr = (size_t)info->hdr + shdr->sh_offset;
2633
2634#ifndef CONFIG_MODULE_UNLOAD
2635 /* Don't load .exit sections */
2636 if (strstarts(info->secstrings+shdr->sh_name, ".exit"))
2637 shdr->sh_flags &= ~(unsigned long)SHF_ALLOC;
2638#endif
Rusty Russell8b5f61a2010-08-05 12:59:06 -06002639 }
Rusty Russelld6df72a2010-08-05 12:59:07 -06002640
2641 /* Track but don't keep modinfo and version sections. */
Rusty Russell2f3238a2012-10-22 18:09:41 +10302642 if (flags & MODULE_INIT_IGNORE_MODVERSIONS)
2643 info->index.vers = 0; /* Pretend no __versions section! */
2644 else
2645 info->index.vers = find_sec(info, "__versions");
Rusty Russell49668682010-08-05 12:59:10 -06002646 info->index.info = find_sec(info, ".modinfo");
Rusty Russelld6df72a2010-08-05 12:59:07 -06002647 info->sechdrs[info->index.info].sh_flags &= ~(unsigned long)SHF_ALLOC;
2648 info->sechdrs[info->index.vers].sh_flags &= ~(unsigned long)SHF_ALLOC;
Rusty Russell8b5f61a2010-08-05 12:59:06 -06002649 return 0;
2650}
2651
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002652/*
2653 * Set up our basic convenience variables (pointers to section headers,
2654 * search for module section index etc), and do some basic section
2655 * verification.
2656 *
2657 * Return the temporary module pointer (we'll replace it with the final
2658 * one when we move the module sections around).
2659 */
Rusty Russell2f3238a2012-10-22 18:09:41 +10302660static struct module *setup_load_info(struct load_info *info, int flags)
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002661{
2662 unsigned int i;
Rusty Russell8b5f61a2010-08-05 12:59:06 -06002663 int err;
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002664 struct module *mod;
2665
2666 /* Set up the convenience variables */
2667 info->sechdrs = (void *)info->hdr + info->hdr->e_shoff;
Rusty Russell8b5f61a2010-08-05 12:59:06 -06002668 info->secstrings = (void *)info->hdr
2669 + info->sechdrs[info->hdr->e_shstrndx].sh_offset;
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002670
Rusty Russell2f3238a2012-10-22 18:09:41 +10302671 err = rewrite_section_headers(info, flags);
Rusty Russell8b5f61a2010-08-05 12:59:06 -06002672 if (err)
2673 return ERR_PTR(err);
2674
2675 /* Find internal symbols and strings. */
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002676 for (i = 1; i < info->hdr->e_shnum; i++) {
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002677 if (info->sechdrs[i].sh_type == SHT_SYMTAB) {
2678 info->index.sym = i;
2679 info->index.str = info->sechdrs[i].sh_link;
Rusty Russell8b5f61a2010-08-05 12:59:06 -06002680 info->strtab = (char *)info->hdr
2681 + info->sechdrs[info->index.str].sh_offset;
2682 break;
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002683 }
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002684 }
2685
Rusty Russell49668682010-08-05 12:59:10 -06002686 info->index.mod = find_sec(info, ".gnu.linkonce.this_module");
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002687 if (!info->index.mod) {
Andrew Mortonbddb12b2013-11-12 15:11:28 -08002688 pr_warn("No module found in object\n");
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002689 return ERR_PTR(-ENOEXEC);
2690 }
2691 /* This is temporary: point mod into copy of data. */
2692 mod = (void *)info->sechdrs[info->index.mod].sh_addr;
2693
2694 if (info->index.sym == 0) {
Andrew Mortonbddb12b2013-11-12 15:11:28 -08002695 pr_warn("%s: module has no symbols (stripped?)\n", mod->name);
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002696 return ERR_PTR(-ENOEXEC);
2697 }
2698
Rusty Russell49668682010-08-05 12:59:10 -06002699 info->index.pcpu = find_pcpusec(info);
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002700
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002701 /* Check module struct version now, before we try to use module. */
2702 if (!check_modstruct_version(info->sechdrs, info->index.vers, mod))
2703 return ERR_PTR(-ENOEXEC);
2704
2705 return mod;
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002706}
2707
Rusty Russell2f3238a2012-10-22 18:09:41 +10302708static int check_modinfo(struct module *mod, struct load_info *info, int flags)
Rusty Russell40dd2562010-08-05 12:59:03 -06002709{
Rusty Russell49668682010-08-05 12:59:10 -06002710 const char *modmagic = get_modinfo(info, "vermagic");
Rusty Russell40dd2562010-08-05 12:59:03 -06002711 int err;
2712
Rusty Russell2f3238a2012-10-22 18:09:41 +10302713 if (flags & MODULE_INIT_IGNORE_VERMAGIC)
2714 modmagic = NULL;
2715
Rusty Russell40dd2562010-08-05 12:59:03 -06002716 /* This is allowed: modprobe --force will invalidate it. */
2717 if (!modmagic) {
2718 err = try_to_force_load(mod, "bad vermagic");
2719 if (err)
2720 return err;
Rusty Russell49668682010-08-05 12:59:10 -06002721 } else if (!same_magic(modmagic, vermagic, info->index.vers)) {
Andrew Mortonbddb12b2013-11-12 15:11:28 -08002722 pr_err("%s: version magic '%s' should be '%s'\n",
Rusty Russell40dd2562010-08-05 12:59:03 -06002723 mod->name, modmagic, vermagic);
2724 return -ENOEXEC;
2725 }
2726
Ben Hutchings2449b8b2011-10-24 15:12:28 +02002727 if (!get_modinfo(info, "intree"))
Rusty Russell373d4d02013-01-21 17:17:39 +10302728 add_taint_module(mod, TAINT_OOT_MODULE, LOCKDEP_STILL_OK);
Ben Hutchings2449b8b2011-10-24 15:12:28 +02002729
Rusty Russell49668682010-08-05 12:59:10 -06002730 if (get_modinfo(info, "staging")) {
Rusty Russell373d4d02013-01-21 17:17:39 +10302731 add_taint_module(mod, TAINT_CRAP, LOCKDEP_STILL_OK);
Andrew Mortonbddb12b2013-11-12 15:11:28 -08002732 pr_warn("%s: module is from the staging directory, the quality "
2733 "is unknown, you have been warned.\n", mod->name);
Rusty Russell40dd2562010-08-05 12:59:03 -06002734 }
Rusty Russell22e268e2010-08-05 12:59:05 -06002735
2736 /* Set up license info based on the info section */
Rusty Russell49668682010-08-05 12:59:10 -06002737 set_license(mod, get_modinfo(info, "license"));
Rusty Russell22e268e2010-08-05 12:59:05 -06002738
Rusty Russell40dd2562010-08-05 12:59:03 -06002739 return 0;
2740}
2741
Frantisek Hrbataeb3057d2013-10-14 18:08:46 +10302742static int find_module_sections(struct module *mod, struct load_info *info)
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06002743{
Rusty Russell49668682010-08-05 12:59:10 -06002744 mod->kp = section_objs(info, "__param",
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06002745 sizeof(*mod->kp), &mod->num_kp);
Rusty Russell49668682010-08-05 12:59:10 -06002746 mod->syms = section_objs(info, "__ksymtab",
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06002747 sizeof(*mod->syms), &mod->num_syms);
Rusty Russell49668682010-08-05 12:59:10 -06002748 mod->crcs = section_addr(info, "__kcrctab");
2749 mod->gpl_syms = section_objs(info, "__ksymtab_gpl",
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06002750 sizeof(*mod->gpl_syms),
2751 &mod->num_gpl_syms);
Rusty Russell49668682010-08-05 12:59:10 -06002752 mod->gpl_crcs = section_addr(info, "__kcrctab_gpl");
2753 mod->gpl_future_syms = section_objs(info,
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06002754 "__ksymtab_gpl_future",
2755 sizeof(*mod->gpl_future_syms),
2756 &mod->num_gpl_future_syms);
Rusty Russell49668682010-08-05 12:59:10 -06002757 mod->gpl_future_crcs = section_addr(info, "__kcrctab_gpl_future");
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06002758
2759#ifdef CONFIG_UNUSED_SYMBOLS
Rusty Russell49668682010-08-05 12:59:10 -06002760 mod->unused_syms = section_objs(info, "__ksymtab_unused",
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06002761 sizeof(*mod->unused_syms),
2762 &mod->num_unused_syms);
Rusty Russell49668682010-08-05 12:59:10 -06002763 mod->unused_crcs = section_addr(info, "__kcrctab_unused");
2764 mod->unused_gpl_syms = section_objs(info, "__ksymtab_unused_gpl",
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06002765 sizeof(*mod->unused_gpl_syms),
2766 &mod->num_unused_gpl_syms);
Rusty Russell49668682010-08-05 12:59:10 -06002767 mod->unused_gpl_crcs = section_addr(info, "__kcrctab_unused_gpl");
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06002768#endif
2769#ifdef CONFIG_CONSTRUCTORS
Rusty Russell49668682010-08-05 12:59:10 -06002770 mod->ctors = section_objs(info, ".ctors",
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06002771 sizeof(*mod->ctors), &mod->num_ctors);
Frantisek Hrbataeb3057d2013-10-14 18:08:46 +10302772 if (!mod->ctors)
2773 mod->ctors = section_objs(info, ".init_array",
2774 sizeof(*mod->ctors), &mod->num_ctors);
2775 else if (find_sec(info, ".init_array")) {
2776 /*
2777 * This shouldn't happen with same compiler and binutils
2778 * building all parts of the module.
2779 */
Ionut Alexa6da0b562014-11-10 09:31:29 +10302780 pr_warn("%s: has both .ctors and .init_array.\n",
Frantisek Hrbataeb3057d2013-10-14 18:08:46 +10302781 mod->name);
2782 return -EINVAL;
2783 }
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06002784#endif
2785
2786#ifdef CONFIG_TRACEPOINTS
Mathieu Desnoyers65498642011-01-26 17:26:22 -05002787 mod->tracepoints_ptrs = section_objs(info, "__tracepoints_ptrs",
2788 sizeof(*mod->tracepoints_ptrs),
2789 &mod->num_tracepoints);
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06002790#endif
Jason Baronbf5438fc2010-09-17 11:09:00 -04002791#ifdef HAVE_JUMP_LABEL
2792 mod->jump_entries = section_objs(info, "__jump_table",
2793 sizeof(*mod->jump_entries),
2794 &mod->num_jump_entries);
2795#endif
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06002796#ifdef CONFIG_EVENT_TRACING
Rusty Russell49668682010-08-05 12:59:10 -06002797 mod->trace_events = section_objs(info, "_ftrace_events",
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06002798 sizeof(*mod->trace_events),
2799 &mod->num_trace_events);
Steven Rostedt (Red Hat)3673b8e2015-03-25 15:44:21 -04002800 mod->trace_enums = section_objs(info, "_ftrace_enum_map",
2801 sizeof(*mod->trace_enums),
2802 &mod->num_trace_enums);
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06002803#endif
Steven Rostedt13b9b6e2010-11-10 22:19:24 -05002804#ifdef CONFIG_TRACING
2805 mod->trace_bprintk_fmt_start = section_objs(info, "__trace_printk_fmt",
2806 sizeof(*mod->trace_bprintk_fmt_start),
2807 &mod->num_trace_bprintk_fmt);
Steven Rostedt13b9b6e2010-11-10 22:19:24 -05002808#endif
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06002809#ifdef CONFIG_FTRACE_MCOUNT_RECORD
2810 /* sechdrs[0].sh_size is always zero */
Rusty Russell49668682010-08-05 12:59:10 -06002811 mod->ftrace_callsites = section_objs(info, "__mcount_loc",
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06002812 sizeof(*mod->ftrace_callsites),
2813 &mod->num_ftrace_callsites);
2814#endif
Rusty Russell22e268e2010-08-05 12:59:05 -06002815
Rusty Russell811d66a2010-08-05 12:59:12 -06002816 mod->extable = section_objs(info, "__ex_table",
2817 sizeof(*mod->extable), &mod->num_exentries);
2818
Rusty Russell49668682010-08-05 12:59:10 -06002819 if (section_addr(info, "__obsparm"))
Andrew Mortonbddb12b2013-11-12 15:11:28 -08002820 pr_warn("%s: Ignoring obsolete parameters\n", mod->name);
Rusty Russell811d66a2010-08-05 12:59:12 -06002821
2822 info->debug = section_objs(info, "__verbose",
2823 sizeof(*info->debug), &info->num_debug);
Frantisek Hrbataeb3057d2013-10-14 18:08:46 +10302824
2825 return 0;
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06002826}
2827
Rusty Russell49668682010-08-05 12:59:10 -06002828static int move_module(struct module *mod, struct load_info *info)
Linus Torvalds65b8a9b2010-08-05 12:59:02 -06002829{
2830 int i;
2831 void *ptr;
2832
2833 /* Do the allocs. */
2834 ptr = module_alloc_update_bounds(mod->core_size);
2835 /*
2836 * The pointer to this block is stored in the module structure
2837 * which is inside the block. Just mark it as not being a
2838 * leak.
2839 */
2840 kmemleak_not_leak(ptr);
2841 if (!ptr)
Rusty Russelld9131882010-08-05 12:59:08 -06002842 return -ENOMEM;
Linus Torvalds65b8a9b2010-08-05 12:59:02 -06002843
2844 memset(ptr, 0, mod->core_size);
2845 mod->module_core = ptr;
2846
Rusty Russell82fab442012-12-11 09:38:33 +10302847 if (mod->init_size) {
2848 ptr = module_alloc_update_bounds(mod->init_size);
2849 /*
2850 * The pointer to this block is stored in the module structure
2851 * which is inside the block. This block doesn't need to be
2852 * scanned as it contains data and code that will be freed
2853 * after the module is initialized.
2854 */
2855 kmemleak_ignore(ptr);
2856 if (!ptr) {
Rusty Russellbe1f2212015-01-20 09:07:05 +10302857 module_memfree(mod->module_core);
Rusty Russell82fab442012-12-11 09:38:33 +10302858 return -ENOMEM;
2859 }
2860 memset(ptr, 0, mod->init_size);
2861 mod->module_init = ptr;
2862 } else
2863 mod->module_init = NULL;
Linus Torvalds65b8a9b2010-08-05 12:59:02 -06002864
2865 /* Transfer each section which specifies SHF_ALLOC */
Jim Cromie5e124162011-12-06 12:11:31 -07002866 pr_debug("final section addresses:\n");
Rusty Russell49668682010-08-05 12:59:10 -06002867 for (i = 0; i < info->hdr->e_shnum; i++) {
Linus Torvalds65b8a9b2010-08-05 12:59:02 -06002868 void *dest;
Rusty Russell49668682010-08-05 12:59:10 -06002869 Elf_Shdr *shdr = &info->sechdrs[i];
Linus Torvalds65b8a9b2010-08-05 12:59:02 -06002870
Rusty Russell49668682010-08-05 12:59:10 -06002871 if (!(shdr->sh_flags & SHF_ALLOC))
Linus Torvalds65b8a9b2010-08-05 12:59:02 -06002872 continue;
2873
Rusty Russell49668682010-08-05 12:59:10 -06002874 if (shdr->sh_entsize & INIT_OFFSET_MASK)
Linus Torvalds65b8a9b2010-08-05 12:59:02 -06002875 dest = mod->module_init
Rusty Russell49668682010-08-05 12:59:10 -06002876 + (shdr->sh_entsize & ~INIT_OFFSET_MASK);
Linus Torvalds65b8a9b2010-08-05 12:59:02 -06002877 else
Rusty Russell49668682010-08-05 12:59:10 -06002878 dest = mod->module_core + shdr->sh_entsize;
Linus Torvalds65b8a9b2010-08-05 12:59:02 -06002879
Rusty Russell49668682010-08-05 12:59:10 -06002880 if (shdr->sh_type != SHT_NOBITS)
2881 memcpy(dest, (void *)shdr->sh_addr, shdr->sh_size);
Linus Torvalds65b8a9b2010-08-05 12:59:02 -06002882 /* Update sh_addr to point to copy in image. */
Rusty Russell49668682010-08-05 12:59:10 -06002883 shdr->sh_addr = (unsigned long)dest;
Jim Cromie5e124162011-12-06 12:11:31 -07002884 pr_debug("\t0x%lx %s\n",
2885 (long)shdr->sh_addr, info->secstrings + shdr->sh_name);
Linus Torvalds65b8a9b2010-08-05 12:59:02 -06002886 }
Rusty Russelld9131882010-08-05 12:59:08 -06002887
2888 return 0;
Linus Torvalds65b8a9b2010-08-05 12:59:02 -06002889}
2890
Rusty Russell49668682010-08-05 12:59:10 -06002891static int check_module_license_and_versions(struct module *mod)
Rusty Russell22e268e2010-08-05 12:59:05 -06002892{
2893 /*
2894 * ndiswrapper is under GPL by itself, but loads proprietary modules.
2895 * Don't use add_taint_module(), as it would prevent ndiswrapper from
2896 * using GPL-only symbols it needs.
2897 */
2898 if (strcmp(mod->name, "ndiswrapper") == 0)
Rusty Russell373d4d02013-01-21 17:17:39 +10302899 add_taint(TAINT_PROPRIETARY_MODULE, LOCKDEP_NOW_UNRELIABLE);
Rusty Russell22e268e2010-08-05 12:59:05 -06002900
2901 /* driverloader was caught wrongly pretending to be under GPL */
2902 if (strcmp(mod->name, "driverloader") == 0)
Rusty Russell373d4d02013-01-21 17:17:39 +10302903 add_taint_module(mod, TAINT_PROPRIETARY_MODULE,
2904 LOCKDEP_NOW_UNRELIABLE);
Rusty Russell22e268e2010-08-05 12:59:05 -06002905
Matthew Garrettc99af372012-06-22 13:49:31 -04002906 /* lve claims to be GPL but upstream won't provide source */
2907 if (strcmp(mod->name, "lve") == 0)
Rusty Russell373d4d02013-01-21 17:17:39 +10302908 add_taint_module(mod, TAINT_PROPRIETARY_MODULE,
2909 LOCKDEP_NOW_UNRELIABLE);
Matthew Garrettc99af372012-06-22 13:49:31 -04002910
Rusty Russell22e268e2010-08-05 12:59:05 -06002911#ifdef CONFIG_MODVERSIONS
2912 if ((mod->num_syms && !mod->crcs)
2913 || (mod->num_gpl_syms && !mod->gpl_crcs)
2914 || (mod->num_gpl_future_syms && !mod->gpl_future_crcs)
2915#ifdef CONFIG_UNUSED_SYMBOLS
2916 || (mod->num_unused_syms && !mod->unused_crcs)
2917 || (mod->num_unused_gpl_syms && !mod->unused_gpl_crcs)
2918#endif
2919 ) {
2920 return try_to_force_load(mod,
2921 "no versions for exported symbols");
2922 }
2923#endif
2924 return 0;
2925}
2926
2927static void flush_module_icache(const struct module *mod)
2928{
2929 mm_segment_t old_fs;
2930
2931 /* flush the icache in correct context */
2932 old_fs = get_fs();
2933 set_fs(KERNEL_DS);
2934
2935 /*
2936 * Flush the instruction cache, since we've played with text.
2937 * Do it before processing of module parameters, so the module
2938 * can provide parameter accessor functions of its own.
2939 */
2940 if (mod->module_init)
2941 flush_icache_range((unsigned long)mod->module_init,
2942 (unsigned long)mod->module_init
2943 + mod->init_size);
2944 flush_icache_range((unsigned long)mod->module_core,
2945 (unsigned long)mod->module_core + mod->core_size);
2946
2947 set_fs(old_fs);
2948}
2949
Jonas Bonn74e08fc2011-06-30 21:22:11 +02002950int __weak module_frob_arch_sections(Elf_Ehdr *hdr,
2951 Elf_Shdr *sechdrs,
2952 char *secstrings,
2953 struct module *mod)
2954{
2955 return 0;
2956}
2957
Rusty Russell2f3238a2012-10-22 18:09:41 +10302958static struct module *layout_and_allocate(struct load_info *info, int flags)
Rusty Russelld9131882010-08-05 12:59:08 -06002959{
2960 /* Module within temporary copy. */
2961 struct module *mod;
2962 int err;
2963
Rusty Russell2f3238a2012-10-22 18:09:41 +10302964 mod = setup_load_info(info, flags);
Rusty Russelld9131882010-08-05 12:59:08 -06002965 if (IS_ERR(mod))
2966 return mod;
2967
Rusty Russell2f3238a2012-10-22 18:09:41 +10302968 err = check_modinfo(mod, info, flags);
Rusty Russelld9131882010-08-05 12:59:08 -06002969 if (err)
2970 return ERR_PTR(err);
2971
2972 /* Allow arches to frob section contents and sizes. */
Rusty Russell49668682010-08-05 12:59:10 -06002973 err = module_frob_arch_sections(info->hdr, info->sechdrs,
2974 info->secstrings, mod);
Rusty Russelld9131882010-08-05 12:59:08 -06002975 if (err < 0)
Rusty Russell8d8022e2013-07-03 10:06:28 +09302976 return ERR_PTR(err);
Rusty Russelld9131882010-08-05 12:59:08 -06002977
Rusty Russell8d8022e2013-07-03 10:06:28 +09302978 /* We will do a special allocation for per-cpu sections later. */
2979 info->sechdrs[info->index.pcpu].sh_flags &= ~(unsigned long)SHF_ALLOC;
Rusty Russelld9131882010-08-05 12:59:08 -06002980
2981 /* Determine total sizes, and put offsets in sh_entsize. For now
2982 this is done generically; there doesn't appear to be any
2983 special cases for the architectures. */
Rusty Russell49668682010-08-05 12:59:10 -06002984 layout_sections(mod, info);
Rusty Russell49668682010-08-05 12:59:10 -06002985 layout_symtab(mod, info);
Rusty Russelld9131882010-08-05 12:59:08 -06002986
2987 /* Allocate and move to the final place */
Rusty Russell49668682010-08-05 12:59:10 -06002988 err = move_module(mod, info);
Rusty Russelld9131882010-08-05 12:59:08 -06002989 if (err)
Rusty Russell8d8022e2013-07-03 10:06:28 +09302990 return ERR_PTR(err);
Rusty Russelld9131882010-08-05 12:59:08 -06002991
2992 /* Module has been copied to its final place now: return it. */
2993 mod = (void *)info->sechdrs[info->index.mod].sh_addr;
Rusty Russell49668682010-08-05 12:59:10 -06002994 kmemleak_load_module(mod, info);
Rusty Russelld9131882010-08-05 12:59:08 -06002995 return mod;
Rusty Russelld9131882010-08-05 12:59:08 -06002996}
2997
2998/* mod is no longer valid after this! */
2999static void module_deallocate(struct module *mod, struct load_info *info)
3000{
Rusty Russelld9131882010-08-05 12:59:08 -06003001 percpu_modfree(mod);
Rusty Russelld453cde2015-01-20 09:07:04 +10303002 module_arch_freeing_init(mod);
Rusty Russellbe1f2212015-01-20 09:07:05 +10303003 module_memfree(mod->module_init);
3004 module_memfree(mod->module_core);
Rusty Russelld9131882010-08-05 12:59:08 -06003005}
3006
Jonas Bonn74e08fc2011-06-30 21:22:11 +02003007int __weak module_finalize(const Elf_Ehdr *hdr,
3008 const Elf_Shdr *sechdrs,
3009 struct module *me)
3010{
3011 return 0;
3012}
3013
Rusty Russell811d66a2010-08-05 12:59:12 -06003014static int post_relocation(struct module *mod, const struct load_info *info)
3015{
Rusty Russell51f3d0f2010-08-05 12:59:13 -06003016 /* Sort exception table now relocations are done. */
Rusty Russell811d66a2010-08-05 12:59:12 -06003017 sort_extable(mod->extable, mod->extable + mod->num_exentries);
3018
3019 /* Copy relocated percpu area over. */
3020 percpu_modcopy(mod, (void *)info->sechdrs[info->index.pcpu].sh_addr,
3021 info->sechdrs[info->index.pcpu].sh_size);
3022
Rusty Russell51f3d0f2010-08-05 12:59:13 -06003023 /* Setup kallsyms-specific fields. */
Rusty Russell811d66a2010-08-05 12:59:12 -06003024 add_kallsyms(mod, info);
3025
3026 /* Arch-specific module finalizing. */
3027 return module_finalize(info->hdr, info->sechdrs, mod);
3028}
3029
Rusty Russell9bb9c3b2012-09-28 14:31:03 +09303030/* Is this module of this name done loading? No locks held. */
3031static bool finished_loading(const char *name)
3032{
3033 struct module *mod;
3034 bool ret;
3035
Peter Zijlstra9cc019b2015-02-11 15:01:13 +10303036 /*
3037 * The module_mutex should not be a heavily contended lock;
3038 * if we get the occasional sleep here, we'll go an extra iteration
3039 * in the wait_event_interruptible(), which is harmless.
3040 */
3041 sched_annotate_sleep();
Rusty Russell9bb9c3b2012-09-28 14:31:03 +09303042 mutex_lock(&module_mutex);
Mathias Krause4f6de4d2013-07-02 15:35:11 +09303043 mod = find_module_all(name, strlen(name), true);
Rusty Russell0d21b0e2013-01-12 11:38:44 +10303044 ret = !mod || mod->state == MODULE_STATE_LIVE
3045 || mod->state == MODULE_STATE_GOING;
Rusty Russell9bb9c3b2012-09-28 14:31:03 +09303046 mutex_unlock(&module_mutex);
3047
3048 return ret;
3049}
3050
Peter Oberparleiterb99b87f2009-06-17 16:28:03 -07003051/* Call module constructors. */
3052static void do_mod_ctors(struct module *mod)
3053{
3054#ifdef CONFIG_CONSTRUCTORS
3055 unsigned long i;
3056
3057 for (i = 0; i < mod->num_ctors; i++)
3058 mod->ctors[i]();
3059#endif
3060}
3061
Rusty Russellc7496372015-01-20 09:07:05 +10303062/* For freeing module_init on success, in case kallsyms traversing */
3063struct mod_initfree {
3064 struct rcu_head rcu;
3065 void *module_init;
3066};
3067
3068static void do_free_init(struct rcu_head *head)
3069{
3070 struct mod_initfree *m = container_of(head, struct mod_initfree, rcu);
3071 module_memfree(m->module_init);
3072 kfree(m);
3073}
3074
Jan Kiszkabe02a182015-02-17 13:46:50 -08003075/*
3076 * This is where the real work happens.
3077 *
3078 * Keep it uninlined to provide a reliable breakpoint target, e.g. for the gdb
3079 * helper command 'lx-symbols'.
3080 */
3081static noinline int do_init_module(struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003082{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003083 int ret = 0;
Rusty Russellc7496372015-01-20 09:07:05 +10303084 struct mod_initfree *freeinit;
3085
3086 freeinit = kmalloc(sizeof(*freeinit), GFP_KERNEL);
3087 if (!freeinit) {
3088 ret = -ENOMEM;
3089 goto fail;
3090 }
3091 freeinit->module_init = mod->module_init;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003092
Tejun Heo774a1222013-01-15 18:52:51 -08003093 /*
3094 * We want to find out whether @mod uses async during init. Clear
3095 * PF_USED_ASYNC. async_schedule*() will set it.
3096 */
3097 current->flags &= ~PF_USED_ASYNC;
3098
Peter Oberparleiterb99b87f2009-06-17 16:28:03 -07003099 do_mod_ctors(mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003100 /* Start the module */
3101 if (mod->init != NULL)
Arjan van de Ven59f94152008-07-30 12:49:02 -07003102 ret = do_one_initcall(mod->init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003103 if (ret < 0) {
Rusty Russellc7496372015-01-20 09:07:05 +10303104 goto fail_free_freeinit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003105 }
Alexey Dobriyane24e2e62008-03-10 11:43:53 -07003106 if (ret > 0) {
Andrew Mortonbddb12b2013-11-12 15:11:28 -08003107 pr_warn("%s: '%s'->init suspiciously returned %d, it should "
3108 "follow 0/-E convention\n"
3109 "%s: loading module anyway...\n",
3110 __func__, mod->name, ret, __func__);
Alexey Dobriyane24e2e62008-03-10 11:43:53 -07003111 dump_stack();
3112 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003113
Rusty Russell6f139092012-09-28 14:31:03 +09303114 /* Now it's a first class citizen! */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003115 mod->state = MODULE_STATE_LIVE;
Masami Hiramatsu0deddf432009-01-06 14:41:54 -08003116 blocking_notifier_call_chain(&module_notify_list,
3117 MODULE_STATE_LIVE, mod);
Rusty Russell6c5db222008-03-10 11:43:52 -07003118
Tejun Heo774a1222013-01-15 18:52:51 -08003119 /*
3120 * We need to finish all async code before the module init sequence
3121 * is done. This has potential to deadlock. For example, a newly
3122 * detected block device can trigger request_module() of the
3123 * default iosched from async probing task. Once userland helper
3124 * reaches here, async_synchronize_full() will wait on the async
3125 * task waiting on request_module() and deadlock.
3126 *
3127 * This deadlock is avoided by perfomring async_synchronize_full()
3128 * iff module init queued any async jobs. This isn't a full
3129 * solution as it will deadlock the same if module loading from
3130 * async jobs nests more than once; however, due to the various
3131 * constraints, this hack seems to be the best option for now.
3132 * Please refer to the following thread for details.
3133 *
3134 * http://thread.gmane.org/gmane.linux.kernel/1420814
3135 */
3136 if (current->flags & PF_USED_ASYNC)
3137 async_synchronize_full();
Linus Torvaldsd6de2c82009-04-10 12:17:41 -07003138
Rusty Russell6c5db222008-03-10 11:43:52 -07003139 mutex_lock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003140 /* Drop initial reference. */
3141 module_put(mod);
Rusty Russellad6561d2009-06-12 21:47:03 -06003142 trim_init_extable(mod);
Jan Beulich4a496222009-07-06 14:50:42 +01003143#ifdef CONFIG_KALLSYMS
3144 mod->num_symtab = mod->core_num_syms;
3145 mod->symtab = mod->core_symtab;
Jan Beulich554bdfe2009-07-06 14:51:44 +01003146 mod->strtab = mod->core_strtab;
Jan Beulich4a496222009-07-06 14:50:42 +01003147#endif
Jan Glauber01526ed2011-05-19 16:55:26 -06003148 unset_module_init_ro_nx(mod);
Rusty Russelld453cde2015-01-20 09:07:04 +10303149 module_arch_freeing_init(mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003150 mod->module_init = NULL;
3151 mod->init_size = 0;
Jan Glauber4d103802011-05-19 16:55:25 -06003152 mod->init_ro_size = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003153 mod->init_text_size = 0;
Rusty Russellc7496372015-01-20 09:07:05 +10303154 /*
3155 * We want to free module_init, but be aware that kallsyms may be
Peter Zijlstra0be964b2015-05-27 11:09:35 +09303156 * walking this with preempt disabled. In all the failure paths, we
3157 * call synchronize_sched(), but we don't want to slow down the success
3158 * path, so use actual RCU here.
Rusty Russellc7496372015-01-20 09:07:05 +10303159 */
Peter Zijlstra0be964b2015-05-27 11:09:35 +09303160 call_rcu_sched(&freeinit->rcu, do_free_init);
Ashutosh Naik6389a382006-03-23 03:00:46 -08003161 mutex_unlock(&module_mutex);
Rusty Russell6f139092012-09-28 14:31:03 +09303162 wake_up_all(&module_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003163
3164 return 0;
Rusty Russellc7496372015-01-20 09:07:05 +10303165
3166fail_free_freeinit:
3167 kfree(freeinit);
3168fail:
3169 /* Try to protect us from buggy refcounters. */
3170 mod->state = MODULE_STATE_GOING;
3171 synchronize_sched();
3172 module_put(mod);
3173 blocking_notifier_call_chain(&module_notify_list,
3174 MODULE_STATE_GOING, mod);
3175 free_module(mod);
3176 wake_up_all(&module_wq);
3177 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003178}
3179
Kees Cook34e11692012-10-16 07:31:07 +10303180static int may_init_module(void)
3181{
3182 if (!capable(CAP_SYS_MODULE) || modules_disabled)
3183 return -EPERM;
3184
3185 return 0;
3186}
3187
Rusty Russella3535c72013-01-21 17:18:59 +10303188/*
3189 * We try to place it in the list now to make sure it's unique before
3190 * we dedicate too many resources. In particular, temporary percpu
3191 * memory exhaustion.
3192 */
3193static int add_unformed_module(struct module *mod)
3194{
3195 int err;
3196 struct module *old;
3197
3198 mod->state = MODULE_STATE_UNFORMED;
3199
3200again:
3201 mutex_lock(&module_mutex);
Mathias Krause4f6de4d2013-07-02 15:35:11 +09303202 old = find_module_all(mod->name, strlen(mod->name), true);
3203 if (old != NULL) {
Rusty Russella3535c72013-01-21 17:18:59 +10303204 if (old->state == MODULE_STATE_COMING
3205 || old->state == MODULE_STATE_UNFORMED) {
3206 /* Wait in case it fails to load. */
3207 mutex_unlock(&module_mutex);
Peter Zijlstra9cc019b2015-02-11 15:01:13 +10303208 err = wait_event_interruptible(module_wq,
3209 finished_loading(mod->name));
Rusty Russella3535c72013-01-21 17:18:59 +10303210 if (err)
3211 goto out_unlocked;
3212 goto again;
3213 }
3214 err = -EEXIST;
3215 goto out;
3216 }
3217 list_add_rcu(&mod->list, &modules);
3218 err = 0;
3219
3220out:
3221 mutex_unlock(&module_mutex);
3222out_unlocked:
3223 return err;
3224}
3225
3226static int complete_formation(struct module *mod, struct load_info *info)
3227{
3228 int err;
3229
3230 mutex_lock(&module_mutex);
3231
3232 /* Find duplicate symbols (must be called under lock). */
3233 err = verify_export_symbols(mod);
3234 if (err < 0)
3235 goto out;
3236
3237 /* This relies on module_mutex for list integrity. */
3238 module_bug_finalize(info->hdr, info->sechdrs, mod);
3239
Rusty Russell49822232014-05-14 10:54:19 +09303240 /* Set RO and NX regions for core */
3241 set_section_ro_nx(mod->module_core,
3242 mod->core_text_size,
3243 mod->core_ro_size,
3244 mod->core_size);
3245
3246 /* Set RO and NX regions for init */
3247 set_section_ro_nx(mod->module_init,
3248 mod->init_text_size,
3249 mod->init_ro_size,
3250 mod->init_size);
3251
Rusty Russella3535c72013-01-21 17:18:59 +10303252 /* Mark state as coming so strong_try_module_get() ignores us,
3253 * but kallsyms etc. can see us. */
3254 mod->state = MODULE_STATE_COMING;
Rusty Russell49822232014-05-14 10:54:19 +09303255 mutex_unlock(&module_mutex);
3256
3257 blocking_notifier_call_chain(&module_notify_list,
3258 MODULE_STATE_COMING, mod);
3259 return 0;
Rusty Russella3535c72013-01-21 17:18:59 +10303260
3261out:
3262 mutex_unlock(&module_mutex);
3263 return err;
3264}
3265
Rusty Russell54041d82013-07-02 15:35:12 +09303266static int unknown_module_param_cb(char *param, char *val, const char *modname)
3267{
Ionut Alexa6da0b562014-11-10 09:31:29 +10303268 /* Check for magic 'dyndbg' arg */
Rusty Russell54041d82013-07-02 15:35:12 +09303269 int ret = ddebug_dyndbg_module_param_cb(param, val, modname);
Andrew Mortonbddb12b2013-11-12 15:11:28 -08003270 if (ret != 0)
3271 pr_warn("%s: unknown parameter '%s' ignored\n", modname, param);
Rusty Russell54041d82013-07-02 15:35:12 +09303272 return 0;
3273}
3274
Kees Cook34e11692012-10-16 07:31:07 +10303275/* Allocate and load the module: note that size of section 0 is always
3276 zero, and we rely on this for optional sections. */
Rusty Russell2f3238a2012-10-22 18:09:41 +10303277static int load_module(struct load_info *info, const char __user *uargs,
3278 int flags)
Kees Cook34e11692012-10-16 07:31:07 +10303279{
Rusty Russella3535c72013-01-21 17:18:59 +10303280 struct module *mod;
Kees Cook34e11692012-10-16 07:31:07 +10303281 long err;
Rusty Russell51e158c2014-04-28 11:34:33 +09303282 char *after_dashes;
Kees Cook34e11692012-10-16 07:31:07 +10303283
3284 err = module_sig_check(info);
3285 if (err)
3286 goto free_copy;
3287
3288 err = elf_header_check(info);
3289 if (err)
3290 goto free_copy;
3291
3292 /* Figure out module layout, and allocate all the memory. */
Rusty Russell2f3238a2012-10-22 18:09:41 +10303293 mod = layout_and_allocate(info, flags);
Kees Cook34e11692012-10-16 07:31:07 +10303294 if (IS_ERR(mod)) {
3295 err = PTR_ERR(mod);
3296 goto free_copy;
3297 }
3298
Rusty Russella3535c72013-01-21 17:18:59 +10303299 /* Reserve our place in the list. */
3300 err = add_unformed_module(mod);
3301 if (err)
Rusty Russell1fb93412013-01-12 13:27:34 +10303302 goto free_module;
Rusty Russell1fb93412013-01-12 13:27:34 +10303303
Kees Cook34e11692012-10-16 07:31:07 +10303304#ifdef CONFIG_MODULE_SIG
3305 mod->sig_ok = info->sig_ok;
Rusty Russell64748a22013-01-21 17:03:02 +10303306 if (!mod->sig_ok) {
Andrew Mortonbddb12b2013-11-12 15:11:28 -08003307 pr_notice_once("%s: module verification failed: signature "
Marcel Holtmannab92ebb2015-02-06 15:09:57 +10303308 "and/or required key missing - tainting "
Andrew Mortonbddb12b2013-11-12 15:11:28 -08003309 "kernel\n", mod->name);
Mathieu Desnoyers66cc69e2014-03-13 12:11:30 +10303310 add_taint_module(mod, TAINT_UNSIGNED_MODULE, LOCKDEP_STILL_OK);
Rusty Russell64748a22013-01-21 17:03:02 +10303311 }
Kees Cook34e11692012-10-16 07:31:07 +10303312#endif
3313
Rusty Russell8d8022e2013-07-03 10:06:28 +09303314 /* To avoid stressing percpu allocator, do this once we're unique. */
Rusty Russell9eb76d72013-07-03 10:06:29 +09303315 err = percpu_modalloc(mod, info);
Rusty Russell8d8022e2013-07-03 10:06:28 +09303316 if (err)
3317 goto unlink_mod;
3318
Kees Cook34e11692012-10-16 07:31:07 +10303319 /* Now module is in final location, initialize linked lists, etc. */
3320 err = module_unload_init(mod);
3321 if (err)
Rusty Russell1fb93412013-01-12 13:27:34 +10303322 goto unlink_mod;
Kees Cook34e11692012-10-16 07:31:07 +10303323
3324 /* Now we've got everything in the final locations, we can
3325 * find optional sections. */
Frantisek Hrbataeb3057d2013-10-14 18:08:46 +10303326 err = find_module_sections(mod, info);
3327 if (err)
3328 goto free_unload;
Kees Cook34e11692012-10-16 07:31:07 +10303329
3330 err = check_module_license_and_versions(mod);
3331 if (err)
3332 goto free_unload;
3333
3334 /* Set up MODINFO_ATTR fields */
3335 setup_modinfo(mod, info);
3336
3337 /* Fix up syms, so that st_value is a pointer to location. */
3338 err = simplify_symbols(mod, info);
3339 if (err < 0)
3340 goto free_modinfo;
3341
3342 err = apply_relocations(mod, info);
3343 if (err < 0)
3344 goto free_modinfo;
3345
3346 err = post_relocation(mod, info);
3347 if (err < 0)
3348 goto free_modinfo;
3349
3350 flush_module_icache(mod);
3351
3352 /* Now copy in args */
3353 mod->args = strndup_user(uargs, ~0UL >> 1);
3354 if (IS_ERR(mod->args)) {
3355 err = PTR_ERR(mod->args);
3356 goto free_arch_cleanup;
3357 }
3358
Kees Cook34e11692012-10-16 07:31:07 +10303359 dynamic_debug_setup(info->debug, info->num_debug);
3360
Steven Rostedt (Red Hat)a949ae52014-04-24 10:40:12 -04003361 /* Ftrace init must be called in the MODULE_STATE_UNFORMED state */
3362 ftrace_module_init(mod);
3363
Rusty Russella3535c72013-01-21 17:18:59 +10303364 /* Finally it's fully formed, ready to start executing. */
3365 err = complete_formation(mod, info);
3366 if (err)
Rusty Russell1fb93412013-01-12 13:27:34 +10303367 goto ddebug_cleanup;
Kees Cook34e11692012-10-16 07:31:07 +10303368
Kees Cook34e11692012-10-16 07:31:07 +10303369 /* Module is ready to execute: parsing args may do that. */
Rusty Russell51e158c2014-04-28 11:34:33 +09303370 after_dashes = parse_args(mod->name, mod->args, mod->kp, mod->num_kp,
3371 -32768, 32767, unknown_module_param_cb);
3372 if (IS_ERR(after_dashes)) {
3373 err = PTR_ERR(after_dashes);
Rusty Russell1fb93412013-01-12 13:27:34 +10303374 goto bug_cleanup;
Rusty Russell51e158c2014-04-28 11:34:33 +09303375 } else if (after_dashes) {
3376 pr_warn("%s: parameters '%s' after `--' ignored\n",
3377 mod->name, after_dashes);
3378 }
Kees Cook34e11692012-10-16 07:31:07 +10303379
3380 /* Link in to syfs. */
3381 err = mod_sysfs_setup(mod, info, mod->kp, mod->num_kp);
3382 if (err < 0)
Rusty Russell1fb93412013-01-12 13:27:34 +10303383 goto bug_cleanup;
Kees Cook34e11692012-10-16 07:31:07 +10303384
3385 /* Get rid of temporary copy. */
3386 free_copy(info);
3387
3388 /* Done! */
3389 trace_module_load(mod);
3390
3391 return do_init_module(mod);
3392
Rusty Russell1fb93412013-01-12 13:27:34 +10303393 bug_cleanup:
3394 /* module_bug_cleanup needs module_mutex protection */
Kees Cook34e11692012-10-16 07:31:07 +10303395 mutex_lock(&module_mutex);
Kees Cook34e11692012-10-16 07:31:07 +10303396 module_bug_cleanup(mod);
Linus Torvaldsee61abb2013-01-20 20:22:58 -08003397 mutex_unlock(&module_mutex);
Andy Lutomirskiff7e0052014-08-16 04:13:37 +09303398
3399 /* we can't deallocate the module until we clear memory protection */
3400 unset_module_init_ro_nx(mod);
3401 unset_module_core_ro_nx(mod);
3402
Rusty Russella3535c72013-01-21 17:18:59 +10303403 ddebug_cleanup:
Rusty Russell1fb93412013-01-12 13:27:34 +10303404 dynamic_debug_remove(info->debug);
Kees Cook34e11692012-10-16 07:31:07 +10303405 synchronize_sched();
3406 kfree(mod->args);
3407 free_arch_cleanup:
3408 module_arch_cleanup(mod);
3409 free_modinfo:
3410 free_modinfo(mod);
3411 free_unload:
3412 module_unload_free(mod);
Rusty Russell1fb93412013-01-12 13:27:34 +10303413 unlink_mod:
3414 mutex_lock(&module_mutex);
3415 /* Unlink carefully: kallsyms could be walking list. */
3416 list_del_rcu(&mod->list);
3417 wake_up_all(&module_wq);
Peter Zijlstra0be964b2015-05-27 11:09:35 +09303418 /* Wait for RCU-sched synchronizing before releasing mod->list. */
3419 synchronize_sched();
Rusty Russell1fb93412013-01-12 13:27:34 +10303420 mutex_unlock(&module_mutex);
Kees Cook34e11692012-10-16 07:31:07 +10303421 free_module:
Peter Zijlstra35a93932015-02-26 16:23:11 +01003422 /* Free lock-classes; relies on the preceding sync_rcu() */
3423 lockdep_free_key_range(mod->module_core, mod->core_size);
3424
Kees Cook34e11692012-10-16 07:31:07 +10303425 module_deallocate(mod, info);
3426 free_copy:
3427 free_copy(info);
3428 return err;
3429}
3430
3431SYSCALL_DEFINE3(init_module, void __user *, umod,
3432 unsigned long, len, const char __user *, uargs)
3433{
3434 int err;
3435 struct load_info info = { };
3436
3437 err = may_init_module();
3438 if (err)
3439 return err;
3440
3441 pr_debug("init_module: umod=%p, len=%lu, uargs=%p\n",
3442 umod, len, uargs);
3443
3444 err = copy_module_from_user(umod, len, &info);
3445 if (err)
3446 return err;
3447
Rusty Russell2f3238a2012-10-22 18:09:41 +10303448 return load_module(&info, uargs, 0);
Kees Cook34e11692012-10-16 07:31:07 +10303449}
3450
Rusty Russell2f3238a2012-10-22 18:09:41 +10303451SYSCALL_DEFINE3(finit_module, int, fd, const char __user *, uargs, int, flags)
Kees Cook34e11692012-10-16 07:31:07 +10303452{
3453 int err;
3454 struct load_info info = { };
3455
3456 err = may_init_module();
3457 if (err)
3458 return err;
3459
Rusty Russell2f3238a2012-10-22 18:09:41 +10303460 pr_debug("finit_module: fd=%d, uargs=%p, flags=%i\n", fd, uargs, flags);
3461
3462 if (flags & ~(MODULE_INIT_IGNORE_MODVERSIONS
3463 |MODULE_INIT_IGNORE_VERMAGIC))
3464 return -EINVAL;
Kees Cook34e11692012-10-16 07:31:07 +10303465
3466 err = copy_module_from_fd(fd, &info);
3467 if (err)
3468 return err;
3469
Rusty Russell2f3238a2012-10-22 18:09:41 +10303470 return load_module(&info, uargs, flags);
Kees Cook34e11692012-10-16 07:31:07 +10303471}
3472
Linus Torvalds1da177e2005-04-16 15:20:36 -07003473static inline int within(unsigned long addr, void *start, unsigned long size)
3474{
3475 return ((void *)addr >= start && (void *)addr < start + size);
3476}
3477
3478#ifdef CONFIG_KALLSYMS
3479/*
3480 * This ignores the intensely annoying "mapping symbols" found
3481 * in ARM ELF files: $a, $t and $d.
3482 */
3483static inline int is_arm_mapping_symbol(const char *str)
3484{
Russell King2e3a10a2014-07-27 07:29:01 +09303485 if (str[0] == '.' && str[1] == 'L')
3486 return true;
Kyle McMartin6c34f1f2014-09-16 22:37:18 +01003487 return str[0] == '$' && strchr("axtd", str[1])
Linus Torvalds1da177e2005-04-16 15:20:36 -07003488 && (str[2] == '\0' || str[2] == '.');
3489}
3490
3491static const char *get_ksymbol(struct module *mod,
3492 unsigned long addr,
3493 unsigned long *size,
3494 unsigned long *offset)
3495{
3496 unsigned int i, best = 0;
3497 unsigned long nextval;
3498
3499 /* At worse, next value is at end of module */
Masami Hiramatsua06f6212009-01-06 14:41:49 -08003500 if (within_module_init(addr, mod))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003501 nextval = (unsigned long)mod->module_init+mod->init_text_size;
Daniel Walker22a8bde2007-10-18 03:06:07 -07003502 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003503 nextval = (unsigned long)mod->module_core+mod->core_text_size;
3504
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003505 /* Scan for closest preceding symbol, and next symbol. (ELF
Daniel Walker22a8bde2007-10-18 03:06:07 -07003506 starts real symbols at 1). */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003507 for (i = 1; i < mod->num_symtab; i++) {
3508 if (mod->symtab[i].st_shndx == SHN_UNDEF)
3509 continue;
3510
3511 /* We ignore unnamed symbols: they're uninformative
3512 * and inserted at a whim. */
3513 if (mod->symtab[i].st_value <= addr
3514 && mod->symtab[i].st_value > mod->symtab[best].st_value
3515 && *(mod->strtab + mod->symtab[i].st_name) != '\0'
3516 && !is_arm_mapping_symbol(mod->strtab + mod->symtab[i].st_name))
3517 best = i;
3518 if (mod->symtab[i].st_value > addr
3519 && mod->symtab[i].st_value < nextval
3520 && *(mod->strtab + mod->symtab[i].st_name) != '\0'
3521 && !is_arm_mapping_symbol(mod->strtab + mod->symtab[i].st_name))
3522 nextval = mod->symtab[i].st_value;
3523 }
3524
3525 if (!best)
3526 return NULL;
3527
Alexey Dobriyanffb45122007-05-08 00:28:41 -07003528 if (size)
3529 *size = nextval - mod->symtab[best].st_value;
3530 if (offset)
3531 *offset = addr - mod->symtab[best].st_value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003532 return mod->strtab + mod->symtab[best].st_name;
3533}
3534
Rusty Russell6dd06c92008-01-29 17:13:22 -05003535/* For kallsyms to ask for address resolution. NULL means not found. Careful
3536 * not to lock to avoid deadlock on oopses, simply disable preemption. */
Andrew Morton92dfc9d2008-02-08 04:18:43 -08003537const char *module_address_lookup(unsigned long addr,
Rusty Russell6dd06c92008-01-29 17:13:22 -05003538 unsigned long *size,
3539 unsigned long *offset,
3540 char **modname,
3541 char *namebuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003542{
3543 struct module *mod;
Rusty Russellcb2a5202008-01-14 00:55:03 -08003544 const char *ret = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003545
Rusty Russellcb2a5202008-01-14 00:55:03 -08003546 preempt_disable();
Andi Kleend72b3752008-08-30 10:09:00 +02003547 list_for_each_entry_rcu(mod, &modules, list) {
Rusty Russell0d21b0e2013-01-12 11:38:44 +10303548 if (mod->state == MODULE_STATE_UNFORMED)
3549 continue;
Petr Mladek9b20a352014-07-27 07:24:01 +09303550 if (within_module(addr, mod)) {
Franck Bui-Huuffc50892006-10-03 01:13:48 -07003551 if (modname)
3552 *modname = mod->name;
Rusty Russellcb2a5202008-01-14 00:55:03 -08003553 ret = get_ksymbol(mod, addr, size, offset);
3554 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003555 }
3556 }
Rusty Russell6dd06c92008-01-29 17:13:22 -05003557 /* Make a copy in here where it's safe */
3558 if (ret) {
3559 strncpy(namebuf, ret, KSYM_NAME_LEN - 1);
3560 ret = namebuf;
3561 }
Rusty Russellcb2a5202008-01-14 00:55:03 -08003562 preempt_enable();
Andrew Morton92dfc9d2008-02-08 04:18:43 -08003563 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003564}
3565
Alexey Dobriyan9d65cb42007-05-08 00:28:43 -07003566int lookup_module_symbol_name(unsigned long addr, char *symname)
3567{
3568 struct module *mod;
3569
Rusty Russellcb2a5202008-01-14 00:55:03 -08003570 preempt_disable();
Andi Kleend72b3752008-08-30 10:09:00 +02003571 list_for_each_entry_rcu(mod, &modules, list) {
Rusty Russell0d21b0e2013-01-12 11:38:44 +10303572 if (mod->state == MODULE_STATE_UNFORMED)
3573 continue;
Petr Mladek9b20a352014-07-27 07:24:01 +09303574 if (within_module(addr, mod)) {
Alexey Dobriyan9d65cb42007-05-08 00:28:43 -07003575 const char *sym;
3576
3577 sym = get_ksymbol(mod, addr, NULL, NULL);
3578 if (!sym)
3579 goto out;
Tejun Heo9281ace2007-07-17 04:03:51 -07003580 strlcpy(symname, sym, KSYM_NAME_LEN);
Rusty Russellcb2a5202008-01-14 00:55:03 -08003581 preempt_enable();
Alexey Dobriyan9d65cb42007-05-08 00:28:43 -07003582 return 0;
3583 }
3584 }
3585out:
Rusty Russellcb2a5202008-01-14 00:55:03 -08003586 preempt_enable();
Alexey Dobriyan9d65cb42007-05-08 00:28:43 -07003587 return -ERANGE;
3588}
3589
Alexey Dobriyana5c43da2007-05-08 00:28:47 -07003590int lookup_module_symbol_attrs(unsigned long addr, unsigned long *size,
3591 unsigned long *offset, char *modname, char *name)
3592{
3593 struct module *mod;
3594
Rusty Russellcb2a5202008-01-14 00:55:03 -08003595 preempt_disable();
Andi Kleend72b3752008-08-30 10:09:00 +02003596 list_for_each_entry_rcu(mod, &modules, list) {
Rusty Russell0d21b0e2013-01-12 11:38:44 +10303597 if (mod->state == MODULE_STATE_UNFORMED)
3598 continue;
Petr Mladek9b20a352014-07-27 07:24:01 +09303599 if (within_module(addr, mod)) {
Alexey Dobriyana5c43da2007-05-08 00:28:47 -07003600 const char *sym;
3601
3602 sym = get_ksymbol(mod, addr, size, offset);
3603 if (!sym)
3604 goto out;
3605 if (modname)
Tejun Heo9281ace2007-07-17 04:03:51 -07003606 strlcpy(modname, mod->name, MODULE_NAME_LEN);
Alexey Dobriyana5c43da2007-05-08 00:28:47 -07003607 if (name)
Tejun Heo9281ace2007-07-17 04:03:51 -07003608 strlcpy(name, sym, KSYM_NAME_LEN);
Rusty Russellcb2a5202008-01-14 00:55:03 -08003609 preempt_enable();
Alexey Dobriyana5c43da2007-05-08 00:28:47 -07003610 return 0;
3611 }
3612 }
3613out:
Rusty Russellcb2a5202008-01-14 00:55:03 -08003614 preempt_enable();
Alexey Dobriyana5c43da2007-05-08 00:28:47 -07003615 return -ERANGE;
3616}
3617
Alexey Dobriyanea078902007-05-08 00:28:39 -07003618int module_get_kallsym(unsigned int symnum, unsigned long *value, char *type,
3619 char *name, char *module_name, int *exported)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003620{
3621 struct module *mod;
3622
Rusty Russellcb2a5202008-01-14 00:55:03 -08003623 preempt_disable();
Andi Kleend72b3752008-08-30 10:09:00 +02003624 list_for_each_entry_rcu(mod, &modules, list) {
Rusty Russell0d21b0e2013-01-12 11:38:44 +10303625 if (mod->state == MODULE_STATE_UNFORMED)
3626 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003627 if (symnum < mod->num_symtab) {
3628 *value = mod->symtab[symnum].st_value;
3629 *type = mod->symtab[symnum].st_info;
Andreas Gruenbacher098c5ee2006-07-14 00:24:04 -07003630 strlcpy(name, mod->strtab + mod->symtab[symnum].st_name,
Tejun Heo9281ace2007-07-17 04:03:51 -07003631 KSYM_NAME_LEN);
3632 strlcpy(module_name, mod->name, MODULE_NAME_LEN);
Tim Abbottca4787b2009-01-05 08:40:10 -06003633 *exported = is_exported(name, *value, mod);
Rusty Russellcb2a5202008-01-14 00:55:03 -08003634 preempt_enable();
Alexey Dobriyanea078902007-05-08 00:28:39 -07003635 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003636 }
3637 symnum -= mod->num_symtab;
3638 }
Rusty Russellcb2a5202008-01-14 00:55:03 -08003639 preempt_enable();
Alexey Dobriyanea078902007-05-08 00:28:39 -07003640 return -ERANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003641}
3642
3643static unsigned long mod_find_symname(struct module *mod, const char *name)
3644{
3645 unsigned int i;
3646
3647 for (i = 0; i < mod->num_symtab; i++)
Keith Owens54e8ce42006-02-03 03:03:53 -08003648 if (strcmp(name, mod->strtab+mod->symtab[i].st_name) == 0 &&
3649 mod->symtab[i].st_info != 'U')
Linus Torvalds1da177e2005-04-16 15:20:36 -07003650 return mod->symtab[i].st_value;
3651 return 0;
3652}
3653
3654/* Look for this name: can be of form module:name. */
3655unsigned long module_kallsyms_lookup_name(const char *name)
3656{
3657 struct module *mod;
3658 char *colon;
3659 unsigned long ret = 0;
3660
3661 /* Don't lock: we're in enough trouble already. */
Rusty Russellcb2a5202008-01-14 00:55:03 -08003662 preempt_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003663 if ((colon = strchr(name, ':')) != NULL) {
Mathias Krause4f6de4d2013-07-02 15:35:11 +09303664 if ((mod = find_module_all(name, colon - name, false)) != NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003665 ret = mod_find_symname(mod, colon+1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003666 } else {
Rusty Russell0d21b0e2013-01-12 11:38:44 +10303667 list_for_each_entry_rcu(mod, &modules, list) {
3668 if (mod->state == MODULE_STATE_UNFORMED)
3669 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003670 if ((ret = mod_find_symname(mod, name)) != 0)
3671 break;
Rusty Russell0d21b0e2013-01-12 11:38:44 +10303672 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003673 }
Rusty Russellcb2a5202008-01-14 00:55:03 -08003674 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003675 return ret;
3676}
Anders Kaseorg75a66612008-12-05 19:03:58 -05003677
3678int module_kallsyms_on_each_symbol(int (*fn)(void *, const char *,
3679 struct module *, unsigned long),
3680 void *data)
3681{
3682 struct module *mod;
3683 unsigned int i;
3684 int ret;
3685
Peter Zijlstra0be964b2015-05-27 11:09:35 +09303686 module_assert_mutex();
3687
Anders Kaseorg75a66612008-12-05 19:03:58 -05003688 list_for_each_entry(mod, &modules, list) {
Rusty Russell0d21b0e2013-01-12 11:38:44 +10303689 if (mod->state == MODULE_STATE_UNFORMED)
3690 continue;
Anders Kaseorg75a66612008-12-05 19:03:58 -05003691 for (i = 0; i < mod->num_symtab; i++) {
3692 ret = fn(data, mod->strtab + mod->symtab[i].st_name,
3693 mod, mod->symtab[i].st_value);
3694 if (ret != 0)
3695 return ret;
3696 }
3697 }
3698 return 0;
3699}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003700#endif /* CONFIG_KALLSYMS */
3701
Arjan van de Ven21aa9282008-01-25 21:08:33 +01003702static char *module_flags(struct module *mod, char *buf)
Florin Malitafa3ba2e82006-10-11 01:21:48 -07003703{
3704 int bx = 0;
3705
Rusty Russell0d21b0e2013-01-12 11:38:44 +10303706 BUG_ON(mod->state == MODULE_STATE_UNFORMED);
Arjan van de Ven21aa9282008-01-25 21:08:33 +01003707 if (mod->taints ||
3708 mod->state == MODULE_STATE_GOING ||
3709 mod->state == MODULE_STATE_COMING) {
Florin Malitafa3ba2e82006-10-11 01:21:48 -07003710 buf[bx++] = '(';
Kay Sieverscca3e702012-01-13 09:32:15 +10303711 bx += module_flags_taint(mod, buf + bx);
Arjan van de Ven21aa9282008-01-25 21:08:33 +01003712 /* Show a - for module-is-being-unloaded */
3713 if (mod->state == MODULE_STATE_GOING)
3714 buf[bx++] = '-';
3715 /* Show a + for module-is-being-loaded */
3716 if (mod->state == MODULE_STATE_COMING)
3717 buf[bx++] = '+';
Florin Malitafa3ba2e82006-10-11 01:21:48 -07003718 buf[bx++] = ')';
3719 }
3720 buf[bx] = '\0';
3721
3722 return buf;
3723}
3724
Alexey Dobriyan3b5d5c62008-10-06 13:19:27 +04003725#ifdef CONFIG_PROC_FS
3726/* Called by the /proc file system to return a list of modules. */
3727static void *m_start(struct seq_file *m, loff_t *pos)
3728{
3729 mutex_lock(&module_mutex);
3730 return seq_list_start(&modules, *pos);
3731}
3732
3733static void *m_next(struct seq_file *m, void *p, loff_t *pos)
3734{
3735 return seq_list_next(p, &modules, pos);
3736}
3737
3738static void m_stop(struct seq_file *m, void *p)
3739{
3740 mutex_unlock(&module_mutex);
3741}
3742
Linus Torvalds1da177e2005-04-16 15:20:36 -07003743static int m_show(struct seq_file *m, void *p)
3744{
3745 struct module *mod = list_entry(p, struct module, list);
Florin Malitafa3ba2e82006-10-11 01:21:48 -07003746 char buf[8];
3747
Rusty Russell0d21b0e2013-01-12 11:38:44 +10303748 /* We always ignore unformed modules. */
3749 if (mod->state == MODULE_STATE_UNFORMED)
3750 return 0;
3751
Denys Vlasenko2f0f2a32008-07-22 19:24:27 -05003752 seq_printf(m, "%s %u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003753 mod->name, mod->init_size + mod->core_size);
3754 print_unload_info(m, mod);
3755
3756 /* Informative for users. */
3757 seq_printf(m, " %s",
Ionut Alexa6da0b562014-11-10 09:31:29 +10303758 mod->state == MODULE_STATE_GOING ? "Unloading" :
3759 mod->state == MODULE_STATE_COMING ? "Loading" :
Linus Torvalds1da177e2005-04-16 15:20:36 -07003760 "Live");
3761 /* Used by oprofile and other similar tools. */
Kees Cook9f36e2c2011-03-22 16:34:22 -07003762 seq_printf(m, " 0x%pK", mod->module_core);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003763
Florin Malitafa3ba2e82006-10-11 01:21:48 -07003764 /* Taints info */
3765 if (mod->taints)
Arjan van de Ven21aa9282008-01-25 21:08:33 +01003766 seq_printf(m, " %s", module_flags(mod, buf));
Florin Malitafa3ba2e82006-10-11 01:21:48 -07003767
Ionut Alexa6da0b562014-11-10 09:31:29 +10303768 seq_puts(m, "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003769 return 0;
3770}
3771
3772/* Format: modulename size refcount deps address
3773
3774 Where refcount is a number or -, and deps is a comma-separated list
3775 of depends or -.
3776*/
Alexey Dobriyan3b5d5c62008-10-06 13:19:27 +04003777static const struct seq_operations modules_op = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003778 .start = m_start,
3779 .next = m_next,
3780 .stop = m_stop,
3781 .show = m_show
3782};
3783
Alexey Dobriyan3b5d5c62008-10-06 13:19:27 +04003784static int modules_open(struct inode *inode, struct file *file)
3785{
3786 return seq_open(file, &modules_op);
3787}
3788
3789static const struct file_operations proc_modules_operations = {
3790 .open = modules_open,
3791 .read = seq_read,
3792 .llseek = seq_lseek,
3793 .release = seq_release,
3794};
3795
3796static int __init proc_modules_init(void)
3797{
3798 proc_create("modules", 0, NULL, &proc_modules_operations);
3799 return 0;
3800}
3801module_init(proc_modules_init);
3802#endif
3803
Linus Torvalds1da177e2005-04-16 15:20:36 -07003804/* Given an address, look for it in the module exception tables. */
3805const struct exception_table_entry *search_module_extables(unsigned long addr)
3806{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003807 const struct exception_table_entry *e = NULL;
3808 struct module *mod;
3809
Rusty Russell24da1cb2007-07-15 23:41:46 -07003810 preempt_disable();
Andi Kleend72b3752008-08-30 10:09:00 +02003811 list_for_each_entry_rcu(mod, &modules, list) {
Rusty Russell0d21b0e2013-01-12 11:38:44 +10303812 if (mod->state == MODULE_STATE_UNFORMED)
3813 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003814 if (mod->num_exentries == 0)
3815 continue;
Daniel Walker22a8bde2007-10-18 03:06:07 -07003816
Linus Torvalds1da177e2005-04-16 15:20:36 -07003817 e = search_extable(mod->extable,
3818 mod->extable + mod->num_exentries - 1,
3819 addr);
3820 if (e)
3821 break;
3822 }
Rusty Russell24da1cb2007-07-15 23:41:46 -07003823 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003824
3825 /* Now, if we found one, we are running inside it now, hence
Daniel Walker22a8bde2007-10-18 03:06:07 -07003826 we cannot unload the module, hence no refcnt needed. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003827 return e;
3828}
3829
Ingo Molnar4d435f92006-07-03 00:24:24 -07003830/*
Rusty Russelle6104992009-03-31 13:05:31 -06003831 * is_module_address - is this address inside a module?
3832 * @addr: the address to check.
3833 *
3834 * See is_module_text_address() if you simply want to see if the address
3835 * is code (not data).
Ingo Molnar4d435f92006-07-03 00:24:24 -07003836 */
Rusty Russelle6104992009-03-31 13:05:31 -06003837bool is_module_address(unsigned long addr)
Ingo Molnar4d435f92006-07-03 00:24:24 -07003838{
Rusty Russelle6104992009-03-31 13:05:31 -06003839 bool ret;
Ingo Molnar4d435f92006-07-03 00:24:24 -07003840
Rusty Russell24da1cb2007-07-15 23:41:46 -07003841 preempt_disable();
Rusty Russelle6104992009-03-31 13:05:31 -06003842 ret = __module_address(addr) != NULL;
Rusty Russell24da1cb2007-07-15 23:41:46 -07003843 preempt_enable();
Ingo Molnar4d435f92006-07-03 00:24:24 -07003844
Rusty Russelle6104992009-03-31 13:05:31 -06003845 return ret;
Ingo Molnar4d435f92006-07-03 00:24:24 -07003846}
3847
Rusty Russelle6104992009-03-31 13:05:31 -06003848/*
3849 * __module_address - get the module which contains an address.
3850 * @addr: the address.
3851 *
3852 * Must be called with preempt disabled or module mutex held so that
3853 * module doesn't get freed during this.
3854 */
Linus Torvalds714f83d2009-04-05 11:04:19 -07003855struct module *__module_address(unsigned long addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003856{
3857 struct module *mod;
3858
Rusty Russell3a642e92008-07-22 19:24:28 -05003859 if (addr < module_addr_min || addr > module_addr_max)
3860 return NULL;
3861
Peter Zijlstra0be964b2015-05-27 11:09:35 +09303862 module_assert_mutex_or_preempt();
3863
Rusty Russell0d21b0e2013-01-12 11:38:44 +10303864 list_for_each_entry_rcu(mod, &modules, list) {
3865 if (mod->state == MODULE_STATE_UNFORMED)
3866 continue;
Petr Mladek9b20a352014-07-27 07:24:01 +09303867 if (within_module(addr, mod))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003868 return mod;
Rusty Russell0d21b0e2013-01-12 11:38:44 +10303869 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003870 return NULL;
3871}
Tim Abbottc6b37802008-12-05 19:03:59 -05003872EXPORT_SYMBOL_GPL(__module_address);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003873
Rusty Russelle6104992009-03-31 13:05:31 -06003874/*
3875 * is_module_text_address - is this address inside module code?
3876 * @addr: the address to check.
3877 *
3878 * See is_module_address() if you simply want to see if the address is
3879 * anywhere in a module. See kernel_text_address() for testing if an
3880 * address corresponds to kernel or module code.
3881 */
3882bool is_module_text_address(unsigned long addr)
3883{
3884 bool ret;
3885
3886 preempt_disable();
3887 ret = __module_text_address(addr) != NULL;
3888 preempt_enable();
3889
3890 return ret;
3891}
3892
3893/*
3894 * __module_text_address - get the module whose code contains an address.
3895 * @addr: the address.
3896 *
3897 * Must be called with preempt disabled or module mutex held so that
3898 * module doesn't get freed during this.
3899 */
3900struct module *__module_text_address(unsigned long addr)
3901{
3902 struct module *mod = __module_address(addr);
3903 if (mod) {
3904 /* Make sure it's within the text section. */
3905 if (!within(addr, mod->module_init, mod->init_text_size)
3906 && !within(addr, mod->module_core, mod->core_text_size))
3907 mod = NULL;
3908 }
3909 return mod;
3910}
Tim Abbottc6b37802008-12-05 19:03:59 -05003911EXPORT_SYMBOL_GPL(__module_text_address);
Rusty Russelle6104992009-03-31 13:05:31 -06003912
Linus Torvalds1da177e2005-04-16 15:20:36 -07003913/* Don't grab lock, we're oopsing. */
3914void print_modules(void)
3915{
3916 struct module *mod;
Randy Dunlap2bc2d612006-10-02 02:17:02 -07003917 char buf[8];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003918
Linus Torvaldsb2311252009-06-16 11:07:14 -07003919 printk(KERN_DEFAULT "Modules linked in:");
Andi Kleend72b3752008-08-30 10:09:00 +02003920 /* Most callers should already have preempt disabled, but make sure */
3921 preempt_disable();
Rusty Russell0d21b0e2013-01-12 11:38:44 +10303922 list_for_each_entry_rcu(mod, &modules, list) {
3923 if (mod->state == MODULE_STATE_UNFORMED)
3924 continue;
Jiri Slaby27bba4d2014-02-03 11:13:13 +10303925 pr_cont(" %s%s", mod->name, module_flags(mod, buf));
Rusty Russell0d21b0e2013-01-12 11:38:44 +10303926 }
Andi Kleend72b3752008-08-30 10:09:00 +02003927 preempt_enable();
Arjan van de Vene14af7e2008-01-25 21:08:33 +01003928 if (last_unloaded_module[0])
Jiri Slaby27bba4d2014-02-03 11:13:13 +10303929 pr_cont(" [last unloaded: %s]", last_unloaded_module);
3930 pr_cont("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003931}
3932
Linus Torvalds1da177e2005-04-16 15:20:36 -07003933#ifdef CONFIG_MODVERSIONS
Rusty Russell8c8ef422009-03-31 13:05:34 -06003934/* Generate the signature for all relevant module structures here.
3935 * If these change, we don't want to try to parse the module. */
3936void module_layout(struct module *mod,
3937 struct modversion_info *ver,
3938 struct kernel_param *kp,
3939 struct kernel_symbol *ks,
Mathieu Desnoyers65498642011-01-26 17:26:22 -05003940 struct tracepoint * const *tp)
Rusty Russell8c8ef422009-03-31 13:05:34 -06003941{
3942}
3943EXPORT_SYMBOL(module_layout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003944#endif