blob: c8da2a59ebf735cc29cd9ae712e5ef568310afa6 [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);
Peter Zijlstra93c2e102015-05-27 11:09:37 +0930104
Peter Zijlstra6c9692e2015-05-27 11:09:37 +0930105#ifdef CONFIG_MODULES_TREE_LOOKUP
106
Peter Zijlstra93c2e102015-05-27 11:09:37 +0930107/*
108 * Use a latched RB-tree for __module_address(); this allows us to use
109 * RCU-sched lookups of the address from any context.
110 *
111 * Because modules have two address ranges: init and core, we need two
112 * latch_tree_nodes entries. Therefore we need the back-pointer from
113 * mod_tree_node.
114 *
115 * Because init ranges are short lived we mark them unlikely and have placed
116 * them outside the critical cacheline in struct module.
Peter Zijlstra6c9692e2015-05-27 11:09:37 +0930117 *
118 * This is conditional on PERF_EVENTS || TRACING because those can really hit
119 * __module_address() hard by doing a lot of stack unwinding; potentially from
120 * NMI context.
Peter Zijlstra93c2e102015-05-27 11:09:37 +0930121 */
122
123static __always_inline unsigned long __mod_tree_val(struct latch_tree_node *n)
124{
125 struct mod_tree_node *mtn = container_of(n, struct mod_tree_node, node);
126 struct module *mod = mtn->mod;
127
128 if (unlikely(mtn == &mod->mtn_init))
129 return (unsigned long)mod->module_init;
130
131 return (unsigned long)mod->module_core;
132}
133
134static __always_inline unsigned long __mod_tree_size(struct latch_tree_node *n)
135{
136 struct mod_tree_node *mtn = container_of(n, struct mod_tree_node, node);
137 struct module *mod = mtn->mod;
138
139 if (unlikely(mtn == &mod->mtn_init))
140 return (unsigned long)mod->init_size;
141
142 return (unsigned long)mod->core_size;
143}
144
145static __always_inline bool
146mod_tree_less(struct latch_tree_node *a, struct latch_tree_node *b)
147{
148 return __mod_tree_val(a) < __mod_tree_val(b);
149}
150
151static __always_inline int
152mod_tree_comp(void *key, struct latch_tree_node *n)
153{
154 unsigned long val = (unsigned long)key;
155 unsigned long start, end;
156
157 start = __mod_tree_val(n);
158 if (val < start)
159 return -1;
160
161 end = start + __mod_tree_size(n);
162 if (val >= end)
163 return 1;
164
165 return 0;
166}
167
168static const struct latch_tree_ops mod_tree_ops = {
169 .less = mod_tree_less,
170 .comp = mod_tree_comp,
171};
172
Peter Zijlstra4f6665462015-05-27 11:09:38 +0930173static struct mod_tree_root {
174 struct latch_tree_root root;
175 unsigned long addr_min;
176 unsigned long addr_max;
177} mod_tree __cacheline_aligned = {
178 .addr_min = -1UL,
179};
180
181#define module_addr_min mod_tree.addr_min
182#define module_addr_max mod_tree.addr_max
183
184static noinline void __mod_tree_insert(struct mod_tree_node *node)
185{
186 latch_tree_insert(&node->node, &mod_tree.root, &mod_tree_ops);
187}
188
189static void __mod_tree_remove(struct mod_tree_node *node)
190{
191 latch_tree_erase(&node->node, &mod_tree.root, &mod_tree_ops);
192}
Peter Zijlstra93c2e102015-05-27 11:09:37 +0930193
194/*
195 * These modifications: insert, remove_init and remove; are serialized by the
196 * module_mutex.
197 */
198static void mod_tree_insert(struct module *mod)
199{
200 mod->mtn_core.mod = mod;
201 mod->mtn_init.mod = mod;
202
Peter Zijlstra4f6665462015-05-27 11:09:38 +0930203 __mod_tree_insert(&mod->mtn_core);
Peter Zijlstra93c2e102015-05-27 11:09:37 +0930204 if (mod->init_size)
Peter Zijlstra4f6665462015-05-27 11:09:38 +0930205 __mod_tree_insert(&mod->mtn_init);
Peter Zijlstra93c2e102015-05-27 11:09:37 +0930206}
207
208static void mod_tree_remove_init(struct module *mod)
209{
210 if (mod->init_size)
Peter Zijlstra4f6665462015-05-27 11:09:38 +0930211 __mod_tree_remove(&mod->mtn_init);
Peter Zijlstra93c2e102015-05-27 11:09:37 +0930212}
213
214static void mod_tree_remove(struct module *mod)
215{
Peter Zijlstra4f6665462015-05-27 11:09:38 +0930216 __mod_tree_remove(&mod->mtn_core);
Peter Zijlstra93c2e102015-05-27 11:09:37 +0930217 mod_tree_remove_init(mod);
218}
219
Peter Zijlstra6c9692e2015-05-27 11:09:37 +0930220static struct module *mod_find(unsigned long addr)
Peter Zijlstra93c2e102015-05-27 11:09:37 +0930221{
222 struct latch_tree_node *ltn;
223
Peter Zijlstra4f6665462015-05-27 11:09:38 +0930224 ltn = latch_tree_find((void *)addr, &mod_tree.root, &mod_tree_ops);
Peter Zijlstra93c2e102015-05-27 11:09:37 +0930225 if (!ltn)
226 return NULL;
227
228 return container_of(ltn, struct mod_tree_node, node)->mod;
229}
230
Peter Zijlstra6c9692e2015-05-27 11:09:37 +0930231#else /* MODULES_TREE_LOOKUP */
232
Peter Zijlstra4f6665462015-05-27 11:09:38 +0930233static unsigned long module_addr_min = -1UL, module_addr_max = 0;
234
Peter Zijlstra6c9692e2015-05-27 11:09:37 +0930235static void mod_tree_insert(struct module *mod) { }
236static void mod_tree_remove_init(struct module *mod) { }
237static void mod_tree_remove(struct module *mod) { }
238
239static struct module *mod_find(unsigned long addr)
240{
241 struct module *mod;
242
243 list_for_each_entry_rcu(mod, &modules, list) {
244 if (within_module(addr, mod))
245 return mod;
246 }
247
248 return NULL;
249}
250
251#endif /* MODULES_TREE_LOOKUP */
252
Peter Zijlstra4f6665462015-05-27 11:09:38 +0930253/*
254 * Bounds of module text, for speeding up __module_address.
255 * Protected by module_mutex.
256 */
257static void __mod_update_bounds(void *base, unsigned int size)
258{
259 unsigned long min = (unsigned long)base;
260 unsigned long max = min + size;
261
262 if (min < module_addr_min)
263 module_addr_min = min;
264 if (max > module_addr_max)
265 module_addr_max = max;
266}
267
268static void mod_update_bounds(struct module *mod)
269{
270 __mod_update_bounds(mod->module_core, mod->core_size);
271 if (mod->init_size)
272 __mod_update_bounds(mod->module_init, mod->init_size);
273}
274
Jason Wessel67fc4e02010-05-20 21:04:21 -0500275#ifdef CONFIG_KGDB_KDB
276struct list_head *kdb_modules = &modules; /* kdb needs the list of modules */
277#endif /* CONFIG_KGDB_KDB */
278
Peter Zijlstra0be964b2015-05-27 11:09:35 +0930279static void module_assert_mutex(void)
280{
281 lockdep_assert_held(&module_mutex);
282}
283
284static void module_assert_mutex_or_preempt(void)
285{
286#ifdef CONFIG_LOCKDEP
287 if (unlikely(!debug_locks))
288 return;
289
290 WARN_ON(!rcu_read_lock_sched_held() &&
291 !lockdep_is_held(&module_mutex));
292#endif
293}
294
Rusty Russell106a4ee2012-09-26 10:09:40 +0100295#ifdef CONFIG_MODULE_SIG
296#ifdef CONFIG_MODULE_SIG_FORCE
297static bool sig_enforce = true;
298#else
299static bool sig_enforce = false;
300
301static int param_set_bool_enable_only(const char *val,
302 const struct kernel_param *kp)
303{
304 int err;
305 bool test;
306 struct kernel_param dummy_kp = *kp;
307
308 dummy_kp.arg = &test;
309
310 err = param_set_bool(val, &dummy_kp);
311 if (err)
312 return err;
313
314 /* Don't let them unset it once it's set! */
315 if (!test && sig_enforce)
316 return -EROFS;
317
318 if (test)
319 sig_enforce = true;
320 return 0;
321}
322
323static const struct kernel_param_ops param_ops_bool_enable_only = {
Jani Nikula6a4c2642014-08-27 06:21:23 +0930324 .flags = KERNEL_PARAM_OPS_FL_NOARG,
Rusty Russell106a4ee2012-09-26 10:09:40 +0100325 .set = param_set_bool_enable_only,
326 .get = param_get_bool,
327};
328#define param_check_bool_enable_only param_check_bool
329
330module_param(sig_enforce, bool_enable_only, 0644);
331#endif /* !CONFIG_MODULE_SIG_FORCE */
332#endif /* CONFIG_MODULE_SIG */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333
Stephen Rothwell19e45292009-04-14 17:27:18 +1000334/* Block module loading/unloading? */
335int modules_disabled = 0;
Dave Young02608be2012-02-01 10:33:14 +0800336core_param(nomodule, modules_disabled, bint, 0);
Stephen Rothwell19e45292009-04-14 17:27:18 +1000337
Rusty Russellc9a3ba52008-01-29 17:13:18 -0500338/* Waiting for a module to finish initializing? */
339static DECLARE_WAIT_QUEUE_HEAD(module_wq);
340
Alan Sterne041c682006-03-27 01:16:30 -0800341static BLOCKING_NOTIFIER_HEAD(module_notify_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342
Ionut Alexa6da0b562014-11-10 09:31:29 +1030343int register_module_notifier(struct notifier_block *nb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344{
Alan Sterne041c682006-03-27 01:16:30 -0800345 return blocking_notifier_chain_register(&module_notify_list, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346}
347EXPORT_SYMBOL(register_module_notifier);
348
Ionut Alexa6da0b562014-11-10 09:31:29 +1030349int unregister_module_notifier(struct notifier_block *nb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350{
Alan Sterne041c682006-03-27 01:16:30 -0800351 return blocking_notifier_chain_unregister(&module_notify_list, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352}
353EXPORT_SYMBOL(unregister_module_notifier);
354
Rusty Russelleded41c2010-08-05 12:59:07 -0600355struct load_info {
356 Elf_Ehdr *hdr;
357 unsigned long len;
358 Elf_Shdr *sechdrs;
Rusty Russell6526c532010-08-05 12:59:10 -0600359 char *secstrings, *strtab;
Rusty Russelld9131882010-08-05 12:59:08 -0600360 unsigned long symoffs, stroffs;
Rusty Russell811d66a2010-08-05 12:59:12 -0600361 struct _ddebug *debug;
362 unsigned int num_debug;
Rusty Russell106a4ee2012-09-26 10:09:40 +0100363 bool sig_ok;
Rusty Russelleded41c2010-08-05 12:59:07 -0600364 struct {
365 unsigned int sym, str, mod, vers, info, pcpu;
366 } index;
367};
368
Matti Linnanvuori9a4b9702007-11-08 08:37:38 -0800369/* We require a truly strong try_module_get(): 0 means failure due to
370 ongoing or failed initialization etc. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371static inline int strong_try_module_get(struct module *mod)
372{
Rusty Russell0d21b0e2013-01-12 11:38:44 +1030373 BUG_ON(mod && mod->state == MODULE_STATE_UNFORMED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 if (mod && mod->state == MODULE_STATE_COMING)
Rusty Russellc9a3ba52008-01-29 17:13:18 -0500375 return -EBUSY;
376 if (try_module_get(mod))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 return 0;
Rusty Russellc9a3ba52008-01-29 17:13:18 -0500378 else
379 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380}
381
Rusty Russell373d4d02013-01-21 17:17:39 +1030382static inline void add_taint_module(struct module *mod, unsigned flag,
383 enum lockdep_ok lockdep_ok)
Florin Malitafa3ba2e82006-10-11 01:21:48 -0700384{
Rusty Russell373d4d02013-01-21 17:17:39 +1030385 add_taint(flag, lockdep_ok);
Andi Kleen25ddbb12008-10-15 22:01:41 -0700386 mod->taints |= (1U << flag);
Florin Malitafa3ba2e82006-10-11 01:21:48 -0700387}
388
Robert P. J. Day02a3e592007-05-09 07:26:28 +0200389/*
390 * A thread that wants to hold a reference to a module only while it
391 * is running can call this to safely exit. nfsd and lockd use this.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 */
393void __module_put_and_exit(struct module *mod, long code)
394{
395 module_put(mod);
396 do_exit(code);
397}
398EXPORT_SYMBOL(__module_put_and_exit);
Daniel Walker22a8bde2007-10-18 03:06:07 -0700399
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400/* Find a module section: 0 means not found. */
Rusty Russell49668682010-08-05 12:59:10 -0600401static unsigned int find_sec(const struct load_info *info, const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402{
403 unsigned int i;
404
Rusty Russell49668682010-08-05 12:59:10 -0600405 for (i = 1; i < info->hdr->e_shnum; i++) {
406 Elf_Shdr *shdr = &info->sechdrs[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 /* Alloc bit cleared means "ignore it." */
Rusty Russell49668682010-08-05 12:59:10 -0600408 if ((shdr->sh_flags & SHF_ALLOC)
409 && strcmp(info->secstrings + shdr->sh_name, name) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 return i;
Rusty Russell49668682010-08-05 12:59:10 -0600411 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 return 0;
413}
414
Rusty Russell5e458cc2008-10-22 10:00:13 -0500415/* Find a module section, or NULL. */
Rusty Russell49668682010-08-05 12:59:10 -0600416static void *section_addr(const struct load_info *info, const char *name)
Rusty Russell5e458cc2008-10-22 10:00:13 -0500417{
418 /* Section 0 has sh_addr 0. */
Rusty Russell49668682010-08-05 12:59:10 -0600419 return (void *)info->sechdrs[find_sec(info, name)].sh_addr;
Rusty Russell5e458cc2008-10-22 10:00:13 -0500420}
421
422/* Find a module section, or NULL. Fill in number of "objects" in section. */
Rusty Russell49668682010-08-05 12:59:10 -0600423static void *section_objs(const struct load_info *info,
Rusty Russell5e458cc2008-10-22 10:00:13 -0500424 const char *name,
425 size_t object_size,
426 unsigned int *num)
427{
Rusty Russell49668682010-08-05 12:59:10 -0600428 unsigned int sec = find_sec(info, name);
Rusty Russell5e458cc2008-10-22 10:00:13 -0500429
430 /* Section 0 has sh_addr 0 and sh_size 0. */
Rusty Russell49668682010-08-05 12:59:10 -0600431 *num = info->sechdrs[sec].sh_size / object_size;
432 return (void *)info->sechdrs[sec].sh_addr;
Rusty Russell5e458cc2008-10-22 10:00:13 -0500433}
434
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435/* Provided by the linker */
436extern const struct kernel_symbol __start___ksymtab[];
437extern const struct kernel_symbol __stop___ksymtab[];
438extern const struct kernel_symbol __start___ksymtab_gpl[];
439extern const struct kernel_symbol __stop___ksymtab_gpl[];
Greg Kroah-Hartman9f28bb72006-03-20 13:17:13 -0800440extern const struct kernel_symbol __start___ksymtab_gpl_future[];
441extern const struct kernel_symbol __stop___ksymtab_gpl_future[];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442extern const unsigned long __start___kcrctab[];
443extern const unsigned long __start___kcrctab_gpl[];
Greg Kroah-Hartman9f28bb72006-03-20 13:17:13 -0800444extern const unsigned long __start___kcrctab_gpl_future[];
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500445#ifdef CONFIG_UNUSED_SYMBOLS
446extern const struct kernel_symbol __start___ksymtab_unused[];
447extern const struct kernel_symbol __stop___ksymtab_unused[];
448extern const struct kernel_symbol __start___ksymtab_unused_gpl[];
449extern const struct kernel_symbol __stop___ksymtab_unused_gpl[];
Arjan van de Venf71d20e2006-06-28 04:26:45 -0700450extern const unsigned long __start___kcrctab_unused[];
451extern const unsigned long __start___kcrctab_unused_gpl[];
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500452#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453
454#ifndef CONFIG_MODVERSIONS
455#define symversion(base, idx) NULL
456#else
Andrew Mortonf83ca9f2006-03-28 01:56:20 -0800457#define symversion(base, idx) ((base != NULL) ? ((base) + (idx)) : NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458#endif
459
Rusty Russelldafd0942008-07-22 19:24:25 -0500460static bool each_symbol_in_section(const struct symsearch *arr,
461 unsigned int arrsize,
462 struct module *owner,
463 bool (*fn)(const struct symsearch *syms,
464 struct module *owner,
Rusty Russellde4d8d52011-04-19 21:49:58 +0200465 void *data),
Rusty Russelldafd0942008-07-22 19:24:25 -0500466 void *data)
Sam Ravnborg3fd68052006-02-08 21:16:45 +0100467{
Rusty Russellde4d8d52011-04-19 21:49:58 +0200468 unsigned int j;
Rusty Russelldafd0942008-07-22 19:24:25 -0500469
470 for (j = 0; j < arrsize; j++) {
Rusty Russellde4d8d52011-04-19 21:49:58 +0200471 if (fn(&arr[j], owner, data))
472 return true;
Rusty Russelldafd0942008-07-22 19:24:25 -0500473 }
474
475 return false;
Sam Ravnborg3fd68052006-02-08 21:16:45 +0100476}
477
Rusty Russelldafd0942008-07-22 19:24:25 -0500478/* Returns true as soon as fn returns true, otherwise false. */
Rusty Russellde4d8d52011-04-19 21:49:58 +0200479bool each_symbol_section(bool (*fn)(const struct symsearch *arr,
480 struct module *owner,
481 void *data),
482 void *data)
Arjan van de Venf71d20e2006-06-28 04:26:45 -0700483{
Rusty Russelldafd0942008-07-22 19:24:25 -0500484 struct module *mod;
Linus Torvalds44032e62010-08-05 12:59:05 -0600485 static const struct symsearch arr[] = {
Rusty Russelldafd0942008-07-22 19:24:25 -0500486 { __start___ksymtab, __stop___ksymtab, __start___kcrctab,
487 NOT_GPL_ONLY, false },
488 { __start___ksymtab_gpl, __stop___ksymtab_gpl,
489 __start___kcrctab_gpl,
490 GPL_ONLY, false },
491 { __start___ksymtab_gpl_future, __stop___ksymtab_gpl_future,
492 __start___kcrctab_gpl_future,
493 WILL_BE_GPL_ONLY, false },
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500494#ifdef CONFIG_UNUSED_SYMBOLS
Rusty Russelldafd0942008-07-22 19:24:25 -0500495 { __start___ksymtab_unused, __stop___ksymtab_unused,
496 __start___kcrctab_unused,
497 NOT_GPL_ONLY, true },
498 { __start___ksymtab_unused_gpl, __stop___ksymtab_unused_gpl,
499 __start___kcrctab_unused_gpl,
500 GPL_ONLY, true },
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500501#endif
Rusty Russelldafd0942008-07-22 19:24:25 -0500502 };
503
Peter Zijlstra0be964b2015-05-27 11:09:35 +0930504 module_assert_mutex_or_preempt();
505
Rusty Russelldafd0942008-07-22 19:24:25 -0500506 if (each_symbol_in_section(arr, ARRAY_SIZE(arr), NULL, fn, data))
507 return true;
508
Andi Kleend72b3752008-08-30 10:09:00 +0200509 list_for_each_entry_rcu(mod, &modules, list) {
Rusty Russelldafd0942008-07-22 19:24:25 -0500510 struct symsearch arr[] = {
511 { mod->syms, mod->syms + mod->num_syms, mod->crcs,
512 NOT_GPL_ONLY, false },
513 { mod->gpl_syms, mod->gpl_syms + mod->num_gpl_syms,
514 mod->gpl_crcs,
515 GPL_ONLY, false },
516 { mod->gpl_future_syms,
517 mod->gpl_future_syms + mod->num_gpl_future_syms,
518 mod->gpl_future_crcs,
519 WILL_BE_GPL_ONLY, false },
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500520#ifdef CONFIG_UNUSED_SYMBOLS
Rusty Russelldafd0942008-07-22 19:24:25 -0500521 { mod->unused_syms,
522 mod->unused_syms + mod->num_unused_syms,
523 mod->unused_crcs,
524 NOT_GPL_ONLY, true },
525 { mod->unused_gpl_syms,
526 mod->unused_gpl_syms + mod->num_unused_gpl_syms,
527 mod->unused_gpl_crcs,
528 GPL_ONLY, true },
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500529#endif
Rusty Russelldafd0942008-07-22 19:24:25 -0500530 };
531
Rusty Russell0d21b0e2013-01-12 11:38:44 +1030532 if (mod->state == MODULE_STATE_UNFORMED)
533 continue;
534
Rusty Russelldafd0942008-07-22 19:24:25 -0500535 if (each_symbol_in_section(arr, ARRAY_SIZE(arr), mod, fn, data))
536 return true;
537 }
538 return false;
Arjan van de Venf71d20e2006-06-28 04:26:45 -0700539}
Rusty Russellde4d8d52011-04-19 21:49:58 +0200540EXPORT_SYMBOL_GPL(each_symbol_section);
Arjan van de Venf71d20e2006-06-28 04:26:45 -0700541
Rusty Russelldafd0942008-07-22 19:24:25 -0500542struct find_symbol_arg {
543 /* Input */
544 const char *name;
545 bool gplok;
546 bool warn;
547
548 /* Output */
549 struct module *owner;
550 const unsigned long *crc;
Tim Abbott414fd312008-12-05 19:03:56 -0500551 const struct kernel_symbol *sym;
Rusty Russelldafd0942008-07-22 19:24:25 -0500552};
553
Rusty Russellde4d8d52011-04-19 21:49:58 +0200554static bool check_symbol(const struct symsearch *syms,
555 struct module *owner,
556 unsigned int symnum, void *data)
Rusty Russellad9546c2008-05-01 21:14:59 -0500557{
Rusty Russelldafd0942008-07-22 19:24:25 -0500558 struct find_symbol_arg *fsa = data;
559
Rusty Russelldafd0942008-07-22 19:24:25 -0500560 if (!fsa->gplok) {
561 if (syms->licence == GPL_ONLY)
562 return false;
563 if (syms->licence == WILL_BE_GPL_ONLY && fsa->warn) {
Andrew Mortonbddb12b2013-11-12 15:11:28 -0800564 pr_warn("Symbol %s is being used by a non-GPL module, "
565 "which will not be allowed in the future\n",
566 fsa->name);
Rusty Russelldafd0942008-07-22 19:24:25 -0500567 }
568 }
569
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500570#ifdef CONFIG_UNUSED_SYMBOLS
Rusty Russelldafd0942008-07-22 19:24:25 -0500571 if (syms->unused && fsa->warn) {
Andrew Mortonbddb12b2013-11-12 15:11:28 -0800572 pr_warn("Symbol %s is marked as UNUSED, however this module is "
573 "using it.\n", fsa->name);
574 pr_warn("This symbol will go away in the future.\n");
Yannick Guerrini7b63c3a2015-03-24 12:31:40 +1030575 pr_warn("Please evaluate if this is the right api to use and "
576 "if it really is, submit a report to the linux kernel "
577 "mailing list together with submitting your code for "
Andrew Mortonbddb12b2013-11-12 15:11:28 -0800578 "inclusion.\n");
Rusty Russellad9546c2008-05-01 21:14:59 -0500579 }
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500580#endif
Rusty Russelldafd0942008-07-22 19:24:25 -0500581
582 fsa->owner = owner;
583 fsa->crc = symversion(syms->crcs, symnum);
Tim Abbott414fd312008-12-05 19:03:56 -0500584 fsa->sym = &syms->start[symnum];
Rusty Russellad9546c2008-05-01 21:14:59 -0500585 return true;
586}
587
Alessio Igor Bogani403ed272011-04-20 11:10:52 +0200588static int cmp_name(const void *va, const void *vb)
589{
590 const char *a;
591 const struct kernel_symbol *b;
592 a = va; b = vb;
593 return strcmp(a, b->name);
594}
595
Rusty Russellde4d8d52011-04-19 21:49:58 +0200596static bool find_symbol_in_section(const struct symsearch *syms,
597 struct module *owner,
598 void *data)
599{
600 struct find_symbol_arg *fsa = data;
Alessio Igor Bogani403ed272011-04-20 11:10:52 +0200601 struct kernel_symbol *sym;
Rusty Russellde4d8d52011-04-19 21:49:58 +0200602
Alessio Igor Bogani403ed272011-04-20 11:10:52 +0200603 sym = bsearch(fsa->name, syms->start, syms->stop - syms->start,
604 sizeof(struct kernel_symbol), cmp_name);
605
606 if (sym != NULL && check_symbol(syms, owner, sym - syms->start, data))
607 return true;
608
Rusty Russellde4d8d52011-04-19 21:49:58 +0200609 return false;
610}
611
Tim Abbott414fd312008-12-05 19:03:56 -0500612/* Find a symbol and return it, along with, (optional) crc and
Rusty Russell75676502010-06-05 11:17:36 -0600613 * (optional) module which owns it. Needs preempt disabled or module_mutex. */
Tim Abbottc6b37802008-12-05 19:03:59 -0500614const struct kernel_symbol *find_symbol(const char *name,
615 struct module **owner,
616 const unsigned long **crc,
617 bool gplok,
618 bool warn)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619{
Rusty Russelldafd0942008-07-22 19:24:25 -0500620 struct find_symbol_arg fsa;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621
Rusty Russelldafd0942008-07-22 19:24:25 -0500622 fsa.name = name;
623 fsa.gplok = gplok;
624 fsa.warn = warn;
625
Rusty Russellde4d8d52011-04-19 21:49:58 +0200626 if (each_symbol_section(find_symbol_in_section, &fsa)) {
Rusty Russellad9546c2008-05-01 21:14:59 -0500627 if (owner)
Rusty Russelldafd0942008-07-22 19:24:25 -0500628 *owner = fsa.owner;
629 if (crc)
630 *crc = fsa.crc;
Tim Abbott414fd312008-12-05 19:03:56 -0500631 return fsa.sym;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 }
Rusty Russellad9546c2008-05-01 21:14:59 -0500633
Jim Cromie5e124162011-12-06 12:11:31 -0700634 pr_debug("Failed to find symbol %s\n", name);
Tim Abbott414fd312008-12-05 19:03:56 -0500635 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636}
Tim Abbottc6b37802008-12-05 19:03:59 -0500637EXPORT_SYMBOL_GPL(find_symbol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639/* Search for module by name: must hold module_mutex. */
Mathias Krause4f6de4d2013-07-02 15:35:11 +0930640static struct module *find_module_all(const char *name, size_t len,
Rusty Russell0d21b0e2013-01-12 11:38:44 +1030641 bool even_unformed)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642{
643 struct module *mod;
644
Peter Zijlstra0be964b2015-05-27 11:09:35 +0930645 module_assert_mutex();
646
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 list_for_each_entry(mod, &modules, list) {
Rusty Russell0d21b0e2013-01-12 11:38:44 +1030648 if (!even_unformed && mod->state == MODULE_STATE_UNFORMED)
649 continue;
Mathias Krause4f6de4d2013-07-02 15:35:11 +0930650 if (strlen(mod->name) == len && !memcmp(mod->name, name, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 return mod;
652 }
653 return NULL;
654}
Rusty Russell0d21b0e2013-01-12 11:38:44 +1030655
656struct module *find_module(const char *name)
657{
Mathias Krause4f6de4d2013-07-02 15:35:11 +0930658 return find_module_all(name, strlen(name), false);
Rusty Russell0d21b0e2013-01-12 11:38:44 +1030659}
Tim Abbottc6b37802008-12-05 19:03:59 -0500660EXPORT_SYMBOL_GPL(find_module);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661
662#ifdef CONFIG_SMP
Tejun Heofbf59bc2009-02-20 16:29:08 +0900663
Tejun Heo259354d2010-03-10 18:56:10 +0900664static inline void __percpu *mod_percpu(struct module *mod)
Tejun Heofbf59bc2009-02-20 16:29:08 +0900665{
Tejun Heo259354d2010-03-10 18:56:10 +0900666 return mod->percpu;
667}
Tejun Heofbf59bc2009-02-20 16:29:08 +0900668
Rusty Russell9eb76d72013-07-03 10:06:29 +0930669static int percpu_modalloc(struct module *mod, struct load_info *info)
Tejun Heo259354d2010-03-10 18:56:10 +0900670{
Rusty Russell9eb76d72013-07-03 10:06:29 +0930671 Elf_Shdr *pcpusec = &info->sechdrs[info->index.pcpu];
672 unsigned long align = pcpusec->sh_addralign;
673
674 if (!pcpusec->sh_size)
675 return 0;
676
Tejun Heofbf59bc2009-02-20 16:29:08 +0900677 if (align > PAGE_SIZE) {
Andrew Mortonbddb12b2013-11-12 15:11:28 -0800678 pr_warn("%s: per-cpu alignment %li > %li\n",
679 mod->name, align, PAGE_SIZE);
Tejun Heofbf59bc2009-02-20 16:29:08 +0900680 align = PAGE_SIZE;
681 }
682
Rusty Russell9eb76d72013-07-03 10:06:29 +0930683 mod->percpu = __alloc_reserved_percpu(pcpusec->sh_size, align);
Tejun Heo259354d2010-03-10 18:56:10 +0900684 if (!mod->percpu) {
Andrew Mortonbddb12b2013-11-12 15:11:28 -0800685 pr_warn("%s: Could not allocate %lu bytes percpu data\n",
686 mod->name, (unsigned long)pcpusec->sh_size);
Tejun Heo259354d2010-03-10 18:56:10 +0900687 return -ENOMEM;
688 }
Rusty Russell9eb76d72013-07-03 10:06:29 +0930689 mod->percpu_size = pcpusec->sh_size;
Tejun Heo259354d2010-03-10 18:56:10 +0900690 return 0;
Tejun Heofbf59bc2009-02-20 16:29:08 +0900691}
692
Tejun Heo259354d2010-03-10 18:56:10 +0900693static void percpu_modfree(struct module *mod)
Tejun Heofbf59bc2009-02-20 16:29:08 +0900694{
Tejun Heo259354d2010-03-10 18:56:10 +0900695 free_percpu(mod->percpu);
Tejun Heofbf59bc2009-02-20 16:29:08 +0900696}
697
Rusty Russell49668682010-08-05 12:59:10 -0600698static unsigned int find_pcpusec(struct load_info *info)
Tejun Heo6b588c12009-02-20 16:29:07 +0900699{
Rusty Russell49668682010-08-05 12:59:10 -0600700 return find_sec(info, ".data..percpu");
Tejun Heo6b588c12009-02-20 16:29:07 +0900701}
702
Tejun Heo259354d2010-03-10 18:56:10 +0900703static void percpu_modcopy(struct module *mod,
704 const void *from, unsigned long size)
Tejun Heo6b588c12009-02-20 16:29:07 +0900705{
706 int cpu;
707
708 for_each_possible_cpu(cpu)
Tejun Heo259354d2010-03-10 18:56:10 +0900709 memcpy(per_cpu_ptr(mod->percpu, cpu), from, size);
Tejun Heo6b588c12009-02-20 16:29:07 +0900710}
711
Tejun Heo10fad5e2010-03-10 18:57:54 +0900712/**
713 * is_module_percpu_address - test whether address is from module static percpu
714 * @addr: address to test
715 *
716 * Test whether @addr belongs to module static percpu area.
717 *
718 * RETURNS:
719 * %true if @addr is from module static percpu area
720 */
721bool is_module_percpu_address(unsigned long addr)
722{
723 struct module *mod;
724 unsigned int cpu;
725
726 preempt_disable();
727
728 list_for_each_entry_rcu(mod, &modules, list) {
Rusty Russell0d21b0e2013-01-12 11:38:44 +1030729 if (mod->state == MODULE_STATE_UNFORMED)
730 continue;
Tejun Heo10fad5e2010-03-10 18:57:54 +0900731 if (!mod->percpu_size)
732 continue;
733 for_each_possible_cpu(cpu) {
734 void *start = per_cpu_ptr(mod->percpu, cpu);
735
736 if ((void *)addr >= start &&
737 (void *)addr < start + mod->percpu_size) {
738 preempt_enable();
739 return true;
740 }
741 }
742 }
743
744 preempt_enable();
745 return false;
Daniel Walker22a8bde2007-10-18 03:06:07 -0700746}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747
748#else /* ... !CONFIG_SMP */
Tejun Heo6b588c12009-02-20 16:29:07 +0900749
Tejun Heo259354d2010-03-10 18:56:10 +0900750static inline void __percpu *mod_percpu(struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751{
752 return NULL;
753}
Rusty Russell9eb76d72013-07-03 10:06:29 +0930754static int percpu_modalloc(struct module *mod, struct load_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755{
Rusty Russell9eb76d72013-07-03 10:06:29 +0930756 /* UP modules shouldn't have this section: ENOMEM isn't quite right */
757 if (info->sechdrs[info->index.pcpu].sh_size != 0)
758 return -ENOMEM;
759 return 0;
Tejun Heo259354d2010-03-10 18:56:10 +0900760}
761static inline void percpu_modfree(struct module *mod)
762{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763}
Rusty Russell49668682010-08-05 12:59:10 -0600764static unsigned int find_pcpusec(struct load_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765{
766 return 0;
767}
Tejun Heo259354d2010-03-10 18:56:10 +0900768static inline void percpu_modcopy(struct module *mod,
769 const void *from, unsigned long size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770{
771 /* pcpusec should be 0, and size of that section should be 0. */
772 BUG_ON(size != 0);
773}
Tejun Heo10fad5e2010-03-10 18:57:54 +0900774bool is_module_percpu_address(unsigned long addr)
775{
776 return false;
777}
Tejun Heo6b588c12009-02-20 16:29:07 +0900778
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779#endif /* CONFIG_SMP */
780
Matt Domschc988d2b2005-06-23 22:05:15 -0700781#define MODINFO_ATTR(field) \
782static void setup_modinfo_##field(struct module *mod, const char *s) \
783{ \
784 mod->field = kstrdup(s, GFP_KERNEL); \
785} \
786static ssize_t show_modinfo_##field(struct module_attribute *mattr, \
Kay Sievers4befb022011-07-24 22:06:04 +0930787 struct module_kobject *mk, char *buffer) \
Matt Domschc988d2b2005-06-23 22:05:15 -0700788{ \
Chen Gangcc56ded2013-08-20 15:34:21 +0930789 return scnprintf(buffer, PAGE_SIZE, "%s\n", mk->mod->field); \
Matt Domschc988d2b2005-06-23 22:05:15 -0700790} \
791static int modinfo_##field##_exists(struct module *mod) \
792{ \
793 return mod->field != NULL; \
794} \
795static void free_modinfo_##field(struct module *mod) \
796{ \
Daniel Walker22a8bde2007-10-18 03:06:07 -0700797 kfree(mod->field); \
798 mod->field = NULL; \
Matt Domschc988d2b2005-06-23 22:05:15 -0700799} \
800static struct module_attribute modinfo_##field = { \
Tejun Heo7b595752007-06-14 03:45:17 +0900801 .attr = { .name = __stringify(field), .mode = 0444 }, \
Matt Domschc988d2b2005-06-23 22:05:15 -0700802 .show = show_modinfo_##field, \
803 .setup = setup_modinfo_##field, \
804 .test = modinfo_##field##_exists, \
805 .free = free_modinfo_##field, \
806};
807
808MODINFO_ATTR(version);
809MODINFO_ATTR(srcversion);
810
Arjan van de Vene14af7e2008-01-25 21:08:33 +0100811static char last_unloaded_module[MODULE_NAME_LEN+1];
812
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -0800813#ifdef CONFIG_MODULE_UNLOAD
Steven Rostedteb0c5372010-03-29 14:25:18 -0400814
815EXPORT_TRACEPOINT_SYMBOL(module_get);
816
Masami Hiramatsue513cc12014-11-10 09:30:29 +1030817/* MODULE_REF_BASE is the base reference count by kmodule loader. */
818#define MODULE_REF_BASE 1
819
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820/* Init the unload section of the module. */
Rusty Russell9f85a4b2010-08-05 12:59:04 -0600821static int module_unload_init(struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822{
Masami Hiramatsue513cc12014-11-10 09:30:29 +1030823 /*
824 * Initialize reference counter to MODULE_REF_BASE.
825 * refcnt == 0 means module is going.
826 */
827 atomic_set(&mod->refcnt, MODULE_REF_BASE);
Rusty Russell9f85a4b2010-08-05 12:59:04 -0600828
Linus Torvalds2c02dfe2010-05-31 12:19:37 -0700829 INIT_LIST_HEAD(&mod->source_list);
830 INIT_LIST_HEAD(&mod->target_list);
Christoph Lametere1783a22010-01-05 15:34:50 +0900831
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 /* Hold reference count during initialization. */
Masami Hiramatsue513cc12014-11-10 09:30:29 +1030833 atomic_inc(&mod->refcnt);
Rusty Russell9f85a4b2010-08-05 12:59:04 -0600834
835 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836}
837
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838/* Does a already use b? */
839static int already_uses(struct module *a, struct module *b)
840{
841 struct module_use *use;
842
Linus Torvalds2c02dfe2010-05-31 12:19:37 -0700843 list_for_each_entry(use, &b->source_list, source_list) {
844 if (use->source == a) {
Jim Cromie5e124162011-12-06 12:11:31 -0700845 pr_debug("%s uses %s!\n", a->name, b->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 return 1;
847 }
848 }
Jim Cromie5e124162011-12-06 12:11:31 -0700849 pr_debug("%s does not use %s!\n", a->name, b->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 return 0;
851}
852
Linus Torvalds2c02dfe2010-05-31 12:19:37 -0700853/*
854 * Module a uses b
855 * - we add 'a' as a "source", 'b' as a "target" of module use
856 * - the module_use is added to the list of 'b' sources (so
857 * 'b' can walk the list to see who sourced them), and of 'a'
858 * targets (so 'a' can see what modules it targets).
859 */
860static int add_module_usage(struct module *a, struct module *b)
861{
Linus Torvalds2c02dfe2010-05-31 12:19:37 -0700862 struct module_use *use;
863
Jim Cromie5e124162011-12-06 12:11:31 -0700864 pr_debug("Allocating new usage for %s.\n", a->name);
Linus Torvalds2c02dfe2010-05-31 12:19:37 -0700865 use = kmalloc(sizeof(*use), GFP_ATOMIC);
866 if (!use) {
Andrew Mortonbddb12b2013-11-12 15:11:28 -0800867 pr_warn("%s: out of memory loading\n", a->name);
Linus Torvalds2c02dfe2010-05-31 12:19:37 -0700868 return -ENOMEM;
869 }
870
871 use->source = a;
872 use->target = b;
873 list_add(&use->source_list, &b->source_list);
874 list_add(&use->target_list, &a->target_list);
Linus Torvalds2c02dfe2010-05-31 12:19:37 -0700875 return 0;
876}
877
Rusty Russell75676502010-06-05 11:17:36 -0600878/* Module a uses b: caller needs module_mutex() */
Rusty Russell9bea7f22010-06-05 11:17:37 -0600879int ref_module(struct module *a, struct module *b)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880{
Rusty Russellc8e21ce2010-06-05 11:17:35 -0600881 int err;
Kay Sievers270a6c42007-01-18 13:26:15 +0100882
Rusty Russell9bea7f22010-06-05 11:17:37 -0600883 if (b == NULL || already_uses(a, b))
Linus Torvalds218ce732010-05-25 16:48:30 -0700884 return 0;
Linus Torvalds218ce732010-05-25 16:48:30 -0700885
Rusty Russell9bea7f22010-06-05 11:17:37 -0600886 /* If module isn't available, we fail. */
887 err = strong_try_module_get(b);
Rusty Russellc9a3ba52008-01-29 17:13:18 -0500888 if (err)
Rusty Russell9bea7f22010-06-05 11:17:37 -0600889 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890
Linus Torvalds2c02dfe2010-05-31 12:19:37 -0700891 err = add_module_usage(a, b);
892 if (err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 module_put(b);
Rusty Russell9bea7f22010-06-05 11:17:37 -0600894 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 }
Rusty Russell9bea7f22010-06-05 11:17:37 -0600896 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897}
Rusty Russell9bea7f22010-06-05 11:17:37 -0600898EXPORT_SYMBOL_GPL(ref_module);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899
900/* Clear the unload stuff of the module. */
901static void module_unload_free(struct module *mod)
902{
Linus Torvalds2c02dfe2010-05-31 12:19:37 -0700903 struct module_use *use, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904
Rusty Russell75676502010-06-05 11:17:36 -0600905 mutex_lock(&module_mutex);
Linus Torvalds2c02dfe2010-05-31 12:19:37 -0700906 list_for_each_entry_safe(use, tmp, &mod->target_list, target_list) {
907 struct module *i = use->target;
Jim Cromie5e124162011-12-06 12:11:31 -0700908 pr_debug("%s unusing %s\n", mod->name, i->name);
Linus Torvalds2c02dfe2010-05-31 12:19:37 -0700909 module_put(i);
910 list_del(&use->source_list);
911 list_del(&use->target_list);
912 kfree(use);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 }
Rusty Russell75676502010-06-05 11:17:36 -0600914 mutex_unlock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915}
916
917#ifdef CONFIG_MODULE_FORCE_UNLOAD
Akinobu Mitafb169792006-01-08 01:04:29 -0800918static inline int try_force_unload(unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919{
920 int ret = (flags & O_TRUNC);
921 if (ret)
Rusty Russell373d4d02013-01-21 17:17:39 +1030922 add_taint(TAINT_FORCED_RMMOD, LOCKDEP_NOW_UNRELIABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 return ret;
924}
925#else
Akinobu Mitafb169792006-01-08 01:04:29 -0800926static inline int try_force_unload(unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927{
928 return 0;
929}
930#endif /* CONFIG_MODULE_FORCE_UNLOAD */
931
Masami Hiramatsue513cc12014-11-10 09:30:29 +1030932/* Try to release refcount of module, 0 means success. */
933static int try_release_module_ref(struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934{
Masami Hiramatsue513cc12014-11-10 09:30:29 +1030935 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936
Masami Hiramatsue513cc12014-11-10 09:30:29 +1030937 /* Try to decrement refcnt which we set at loading */
938 ret = atomic_sub_return(MODULE_REF_BASE, &mod->refcnt);
939 BUG_ON(ret < 0);
940 if (ret)
941 /* Someone can put this right now, recover with checking */
942 ret = atomic_add_unless(&mod->refcnt, MODULE_REF_BASE, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943
Masami Hiramatsue513cc12014-11-10 09:30:29 +1030944 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945}
946
947static int try_stop_module(struct module *mod, int flags, int *forced)
948{
Masami Hiramatsue513cc12014-11-10 09:30:29 +1030949 /* If it's not unused, quit unless we're forcing. */
950 if (try_release_module_ref(mod) != 0) {
951 *forced = try_force_unload(flags);
952 if (!(*forced))
953 return -EWOULDBLOCK;
954 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955
Masami Hiramatsue513cc12014-11-10 09:30:29 +1030956 /* Mark it as dying. */
957 mod->state = MODULE_STATE_GOING;
958
959 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960}
961
Rusty Russelld5db1392015-01-22 11:13:14 +1030962/**
963 * module_refcount - return the refcount or -1 if unloading
964 *
965 * @mod: the module we're checking
966 *
967 * Returns:
968 * -1 if the module is in the process of unloading
969 * otherwise the number of references in the kernel to the module
970 */
971int module_refcount(struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972{
Rusty Russelld5db1392015-01-22 11:13:14 +1030973 return atomic_read(&mod->refcnt) - MODULE_REF_BASE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974}
975EXPORT_SYMBOL(module_refcount);
976
977/* This exists whether we can unload or not */
978static void free_module(struct module *mod);
979
Heiko Carstens17da2bd2009-01-14 14:14:10 +0100980SYSCALL_DEFINE2(delete_module, const char __user *, name_user,
981 unsigned int, flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982{
983 struct module *mod;
Greg Kroah-Hartmandfff0a02007-02-23 14:54:57 -0800984 char name[MODULE_NAME_LEN];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 int ret, forced = 0;
986
Kees Cook3d433212009-04-02 15:49:29 -0700987 if (!capable(CAP_SYS_MODULE) || modules_disabled)
Greg Kroah-Hartmandfff0a02007-02-23 14:54:57 -0800988 return -EPERM;
989
990 if (strncpy_from_user(name, name_user, MODULE_NAME_LEN-1) < 0)
991 return -EFAULT;
992 name[MODULE_NAME_LEN-1] = '\0';
993
Tejun Heo3fc1f1e2010-05-06 18:49:20 +0200994 if (mutex_lock_interruptible(&module_mutex) != 0)
995 return -EINTR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996
997 mod = find_module(name);
998 if (!mod) {
999 ret = -ENOENT;
1000 goto out;
1001 }
1002
Linus Torvalds2c02dfe2010-05-31 12:19:37 -07001003 if (!list_empty(&mod->source_list)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 /* Other modules depend on us: get rid of them first. */
1005 ret = -EWOULDBLOCK;
1006 goto out;
1007 }
1008
1009 /* Doing init or already dying? */
1010 if (mod->state != MODULE_STATE_LIVE) {
Rusty Russell3f2b9c92013-09-17 05:48:51 +09301011 /* FIXME: if (force), slam module count damn the torpedoes */
Jim Cromie5e124162011-12-06 12:11:31 -07001012 pr_debug("%s already dying\n", mod->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 ret = -EBUSY;
1014 goto out;
1015 }
1016
1017 /* If it has an init func, it must have an exit func to unload */
Rusty Russellaf49d922007-10-16 23:26:27 -07001018 if (mod->init && !mod->exit) {
Akinobu Mitafb169792006-01-08 01:04:29 -08001019 forced = try_force_unload(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 if (!forced) {
1021 /* This module can't be removed */
1022 ret = -EBUSY;
1023 goto out;
1024 }
1025 }
1026
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 /* Stop the machine so refcounts can't move and disable module. */
1028 ret = try_stop_module(mod, flags, &forced);
1029 if (ret != 0)
1030 goto out;
1031
Peter Oberparleiterdf4b5652008-04-21 14:34:31 +02001032 mutex_unlock(&module_mutex);
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001033 /* Final destruction now no one is using it. */
Peter Oberparleiterdf4b5652008-04-21 14:34:31 +02001034 if (mod->exit != NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035 mod->exit();
Peter Oberparleiterdf4b5652008-04-21 14:34:31 +02001036 blocking_notifier_call_chain(&module_notify_list,
1037 MODULE_STATE_GOING, mod);
Arjan van de Ven22a9d642009-01-07 08:45:46 -08001038 async_synchronize_full();
Rusty Russell75676502010-06-05 11:17:36 -06001039
Arjan van de Vene14af7e2008-01-25 21:08:33 +01001040 /* Store the name of the last unloaded module for diagnostic purposes */
Rusty Russellefa53452008-01-29 17:13:20 -05001041 strlcpy(last_unloaded_module, mod->name, sizeof(last_unloaded_module));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042
Rusty Russell75676502010-06-05 11:17:36 -06001043 free_module(mod);
1044 return 0;
1045out:
Ashutosh Naik6389a382006-03-23 03:00:46 -08001046 mutex_unlock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 return ret;
1048}
1049
Jianjun Kongd1e99d72008-12-08 14:26:29 +08001050static inline void print_unload_info(struct seq_file *m, struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051{
1052 struct module_use *use;
1053 int printed_something = 0;
1054
Rusty Russelld5db1392015-01-22 11:13:14 +10301055 seq_printf(m, " %i ", module_refcount(mod));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056
Ionut Alexa6da0b562014-11-10 09:31:29 +10301057 /*
1058 * Always include a trailing , so userspace can differentiate
1059 * between this and the old multi-field proc format.
1060 */
Linus Torvalds2c02dfe2010-05-31 12:19:37 -07001061 list_for_each_entry(use, &mod->source_list, source_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 printed_something = 1;
Linus Torvalds2c02dfe2010-05-31 12:19:37 -07001063 seq_printf(m, "%s,", use->source->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 }
1065
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 if (mod->init != NULL && mod->exit == NULL) {
1067 printed_something = 1;
Ionut Alexa6da0b562014-11-10 09:31:29 +10301068 seq_puts(m, "[permanent],");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 }
1070
1071 if (!printed_something)
Ionut Alexa6da0b562014-11-10 09:31:29 +10301072 seq_puts(m, "-");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073}
1074
1075void __symbol_put(const char *symbol)
1076{
1077 struct module *owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078
Rusty Russell24da1cb2007-07-15 23:41:46 -07001079 preempt_disable();
Tim Abbott414fd312008-12-05 19:03:56 -05001080 if (!find_symbol(symbol, &owner, NULL, true, false))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 BUG();
1082 module_put(owner);
Rusty Russell24da1cb2007-07-15 23:41:46 -07001083 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084}
1085EXPORT_SYMBOL(__symbol_put);
1086
Rusty Russell7d1d16e2009-08-26 22:02:54 +09301087/* Note this assumes addr is a function, which it currently always is. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088void symbol_put_addr(void *addr)
1089{
Trent Piepho5e376612006-05-15 09:44:06 -07001090 struct module *modaddr;
Rusty Russell7d1d16e2009-08-26 22:02:54 +09301091 unsigned long a = (unsigned long)dereference_function_descriptor(addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092
Rusty Russell7d1d16e2009-08-26 22:02:54 +09301093 if (core_kernel_text(a))
Trent Piepho5e376612006-05-15 09:44:06 -07001094 return;
1095
Rusty Russella6e6abd2009-03-31 13:05:31 -06001096 /* module_text_address is safe here: we're supposed to have reference
1097 * to module from symbol_get, so it can't go away. */
Rusty Russell7d1d16e2009-08-26 22:02:54 +09301098 modaddr = __module_text_address(a);
Rusty Russella6e6abd2009-03-31 13:05:31 -06001099 BUG_ON(!modaddr);
Trent Piepho5e376612006-05-15 09:44:06 -07001100 module_put(modaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101}
1102EXPORT_SYMBOL_GPL(symbol_put_addr);
1103
1104static ssize_t show_refcnt(struct module_attribute *mattr,
Kay Sievers4befb022011-07-24 22:06:04 +09301105 struct module_kobject *mk, char *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106{
Rusty Russelld5db1392015-01-22 11:13:14 +10301107 return sprintf(buffer, "%i\n", module_refcount(mk->mod));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108}
1109
Kay Sieverscca3e702012-01-13 09:32:15 +10301110static struct module_attribute modinfo_refcnt =
1111 __ATTR(refcnt, 0444, show_refcnt, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112
Steven Rostedtd53799b2012-03-26 12:50:52 +10301113void __module_get(struct module *module)
1114{
1115 if (module) {
1116 preempt_disable();
Masami Hiramatsu2f35c412014-11-10 09:29:29 +10301117 atomic_inc(&module->refcnt);
Steven Rostedtd53799b2012-03-26 12:50:52 +10301118 trace_module_get(module, _RET_IP_);
1119 preempt_enable();
1120 }
1121}
1122EXPORT_SYMBOL(__module_get);
1123
1124bool try_module_get(struct module *module)
1125{
1126 bool ret = true;
1127
1128 if (module) {
1129 preempt_disable();
Masami Hiramatsue513cc12014-11-10 09:30:29 +10301130 /* Note: here, we can fail to get a reference */
1131 if (likely(module_is_live(module) &&
1132 atomic_inc_not_zero(&module->refcnt) != 0))
Steven Rostedtd53799b2012-03-26 12:50:52 +10301133 trace_module_get(module, _RET_IP_);
Masami Hiramatsue513cc12014-11-10 09:30:29 +10301134 else
Steven Rostedtd53799b2012-03-26 12:50:52 +10301135 ret = false;
1136
1137 preempt_enable();
1138 }
1139 return ret;
1140}
1141EXPORT_SYMBOL(try_module_get);
1142
Al Virof6a57032006-10-18 01:47:25 -04001143void module_put(struct module *module)
1144{
Masami Hiramatsue513cc12014-11-10 09:30:29 +10301145 int ret;
1146
Al Virof6a57032006-10-18 01:47:25 -04001147 if (module) {
Christoph Lametere1783a22010-01-05 15:34:50 +09001148 preempt_disable();
Masami Hiramatsue513cc12014-11-10 09:30:29 +10301149 ret = atomic_dec_if_positive(&module->refcnt);
1150 WARN_ON(ret < 0); /* Failed to put refcount */
Li Zefanae832d12010-03-24 10:57:43 +08001151 trace_module_put(module, _RET_IP_);
Christoph Lametere1783a22010-01-05 15:34:50 +09001152 preempt_enable();
Al Virof6a57032006-10-18 01:47:25 -04001153 }
1154}
1155EXPORT_SYMBOL(module_put);
1156
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157#else /* !CONFIG_MODULE_UNLOAD */
Jianjun Kongd1e99d72008-12-08 14:26:29 +08001158static inline void print_unload_info(struct seq_file *m, struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159{
1160 /* We don't know the usage count, or what modules are using. */
Ionut Alexa6da0b562014-11-10 09:31:29 +10301161 seq_puts(m, " - -");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162}
1163
1164static inline void module_unload_free(struct module *mod)
1165{
1166}
1167
Rusty Russell9bea7f22010-06-05 11:17:37 -06001168int ref_module(struct module *a, struct module *b)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169{
Rusty Russell9bea7f22010-06-05 11:17:37 -06001170 return strong_try_module_get(b);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171}
Rusty Russell9bea7f22010-06-05 11:17:37 -06001172EXPORT_SYMBOL_GPL(ref_module);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173
Rusty Russell9f85a4b2010-08-05 12:59:04 -06001174static inline int module_unload_init(struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175{
Rusty Russell9f85a4b2010-08-05 12:59:04 -06001176 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177}
1178#endif /* CONFIG_MODULE_UNLOAD */
1179
Kevin Winchester53999bf2012-01-15 19:32:55 -04001180static size_t module_flags_taint(struct module *mod, char *buf)
1181{
1182 size_t l = 0;
1183
1184 if (mod->taints & (1 << TAINT_PROPRIETARY_MODULE))
1185 buf[l++] = 'P';
1186 if (mod->taints & (1 << TAINT_OOT_MODULE))
1187 buf[l++] = 'O';
1188 if (mod->taints & (1 << TAINT_FORCED_MODULE))
1189 buf[l++] = 'F';
1190 if (mod->taints & (1 << TAINT_CRAP))
1191 buf[l++] = 'C';
Mathieu Desnoyers66cc69e2014-03-13 12:11:30 +10301192 if (mod->taints & (1 << TAINT_UNSIGNED_MODULE))
Rusty Russell57673c22014-03-31 14:39:57 +10301193 buf[l++] = 'E';
Kevin Winchester53999bf2012-01-15 19:32:55 -04001194 /*
1195 * TAINT_FORCED_RMMOD: could be added.
Dave Jones8c904872014-02-26 10:49:49 -05001196 * TAINT_CPU_OUT_OF_SPEC, TAINT_MACHINE_CHECK, TAINT_BAD_PAGE don't
Kevin Winchester53999bf2012-01-15 19:32:55 -04001197 * apply to modules.
1198 */
1199 return l;
1200}
1201
Kay Sievers1f717402006-11-24 12:15:25 +01001202static ssize_t show_initstate(struct module_attribute *mattr,
Kay Sievers4befb022011-07-24 22:06:04 +09301203 struct module_kobject *mk, char *buffer)
Kay Sievers1f717402006-11-24 12:15:25 +01001204{
1205 const char *state = "unknown";
1206
Kay Sievers4befb022011-07-24 22:06:04 +09301207 switch (mk->mod->state) {
Kay Sievers1f717402006-11-24 12:15:25 +01001208 case MODULE_STATE_LIVE:
1209 state = "live";
1210 break;
1211 case MODULE_STATE_COMING:
1212 state = "coming";
1213 break;
1214 case MODULE_STATE_GOING:
1215 state = "going";
1216 break;
Rusty Russell0d21b0e2013-01-12 11:38:44 +10301217 default:
1218 BUG();
Kay Sievers1f717402006-11-24 12:15:25 +01001219 }
1220 return sprintf(buffer, "%s\n", state);
1221}
1222
Kay Sieverscca3e702012-01-13 09:32:15 +10301223static struct module_attribute modinfo_initstate =
1224 __ATTR(initstate, 0444, show_initstate, NULL);
Kay Sievers1f717402006-11-24 12:15:25 +01001225
Kay Sievers88bfa322011-07-24 22:06:04 +09301226static ssize_t store_uevent(struct module_attribute *mattr,
1227 struct module_kobject *mk,
1228 const char *buffer, size_t count)
1229{
1230 enum kobject_action action;
1231
1232 if (kobject_action_type(buffer, count, &action) == 0)
1233 kobject_uevent(&mk->kobj, action);
1234 return count;
1235}
1236
Kay Sieverscca3e702012-01-13 09:32:15 +10301237struct module_attribute module_uevent =
1238 __ATTR(uevent, 0200, NULL, store_uevent);
1239
1240static ssize_t show_coresize(struct module_attribute *mattr,
1241 struct module_kobject *mk, char *buffer)
1242{
1243 return sprintf(buffer, "%u\n", mk->mod->core_size);
1244}
1245
1246static struct module_attribute modinfo_coresize =
1247 __ATTR(coresize, 0444, show_coresize, NULL);
1248
1249static ssize_t show_initsize(struct module_attribute *mattr,
1250 struct module_kobject *mk, char *buffer)
1251{
1252 return sprintf(buffer, "%u\n", mk->mod->init_size);
1253}
1254
1255static struct module_attribute modinfo_initsize =
1256 __ATTR(initsize, 0444, show_initsize, NULL);
1257
1258static ssize_t show_taint(struct module_attribute *mattr,
1259 struct module_kobject *mk, char *buffer)
1260{
1261 size_t l;
1262
1263 l = module_flags_taint(mk->mod, buffer);
1264 buffer[l++] = '\n';
1265 return l;
1266}
1267
1268static struct module_attribute modinfo_taint =
1269 __ATTR(taint, 0444, show_taint, NULL);
Kay Sievers88bfa322011-07-24 22:06:04 +09301270
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001271static struct module_attribute *modinfo_attrs[] = {
Kay Sieverscca3e702012-01-13 09:32:15 +10301272 &module_uevent,
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001273 &modinfo_version,
1274 &modinfo_srcversion,
Kay Sieverscca3e702012-01-13 09:32:15 +10301275 &modinfo_initstate,
1276 &modinfo_coresize,
1277 &modinfo_initsize,
1278 &modinfo_taint,
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001279#ifdef CONFIG_MODULE_UNLOAD
Kay Sieverscca3e702012-01-13 09:32:15 +10301280 &modinfo_refcnt,
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001281#endif
1282 NULL,
1283};
1284
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285static const char vermagic[] = VERMAGIC_STRING;
1286
Rusty Russellc6e665c2009-03-31 13:05:33 -06001287static int try_to_force_load(struct module *mod, const char *reason)
Linus Torvalds826e4502008-05-04 17:04:16 -07001288{
1289#ifdef CONFIG_MODULE_FORCE_LOAD
Andi Kleen25ddbb12008-10-15 22:01:41 -07001290 if (!test_taint(TAINT_FORCED_MODULE))
Andrew Mortonbddb12b2013-11-12 15:11:28 -08001291 pr_warn("%s: %s: kernel tainted.\n", mod->name, reason);
Rusty Russell373d4d02013-01-21 17:17:39 +10301292 add_taint_module(mod, TAINT_FORCED_MODULE, LOCKDEP_NOW_UNRELIABLE);
Linus Torvalds826e4502008-05-04 17:04:16 -07001293 return 0;
1294#else
1295 return -ENOEXEC;
1296#endif
1297}
1298
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299#ifdef CONFIG_MODVERSIONS
Rusty Russelld4703ae2009-12-15 16:28:32 -06001300/* If the arch applies (non-zero) relocations to kernel kcrctab, unapply it. */
1301static unsigned long maybe_relocated(unsigned long crc,
1302 const struct module *crc_owner)
1303{
1304#ifdef ARCH_RELOCATES_KCRCTAB
1305 if (crc_owner == NULL)
1306 return crc - (unsigned long)reloc_start;
1307#endif
1308 return crc;
1309}
1310
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311static int check_version(Elf_Shdr *sechdrs,
1312 unsigned int versindex,
1313 const char *symname,
Ionut Alexa6da0b562014-11-10 09:31:29 +10301314 struct module *mod,
Rusty Russelld4703ae2009-12-15 16:28:32 -06001315 const unsigned long *crc,
1316 const struct module *crc_owner)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317{
1318 unsigned int i, num_versions;
1319 struct modversion_info *versions;
1320
1321 /* Exporting module didn't supply crcs? OK, we're already tainted. */
1322 if (!crc)
1323 return 1;
1324
Rusty Russella5dd6972008-05-09 16:24:21 +10001325 /* No versions at all? modprobe --force does this. */
1326 if (versindex == 0)
1327 return try_to_force_load(mod, symname) == 0;
1328
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329 versions = (void *) sechdrs[versindex].sh_addr;
1330 num_versions = sechdrs[versindex].sh_size
1331 / sizeof(struct modversion_info);
1332
1333 for (i = 0; i < num_versions; i++) {
1334 if (strcmp(versions[i].name, symname) != 0)
1335 continue;
1336
Rusty Russelld4703ae2009-12-15 16:28:32 -06001337 if (versions[i].crc == maybe_relocated(*crc, crc_owner))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338 return 1;
Jim Cromie5e124162011-12-06 12:11:31 -07001339 pr_debug("Found checksum %lX vs module %lX\n",
Rusty Russelld4703ae2009-12-15 16:28:32 -06001340 maybe_relocated(*crc, crc_owner), versions[i].crc);
Linus Torvalds826e4502008-05-04 17:04:16 -07001341 goto bad_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342 }
Linus Torvalds826e4502008-05-04 17:04:16 -07001343
Andrew Mortonbddb12b2013-11-12 15:11:28 -08001344 pr_warn("%s: no symbol version for %s\n", mod->name, symname);
Rusty Russella5dd6972008-05-09 16:24:21 +10001345 return 0;
Linus Torvalds826e4502008-05-04 17:04:16 -07001346
1347bad_version:
Ionut Alexa6da0b562014-11-10 09:31:29 +10301348 pr_warn("%s: disagrees about version of symbol %s\n",
Linus Torvalds826e4502008-05-04 17:04:16 -07001349 mod->name, symname);
1350 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351}
1352
1353static inline int check_modstruct_version(Elf_Shdr *sechdrs,
1354 unsigned int versindex,
1355 struct module *mod)
1356{
1357 const unsigned long *crc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358
Peter Zijlstra926a59b2015-05-27 11:09:35 +09301359 /*
1360 * Since this should be found in kernel (which can't be removed), no
1361 * locking is necessary -- use preempt_disable() to placate lockdep.
1362 */
1363 preempt_disable();
Rusty Russellb92021b2013-03-15 15:04:17 +10301364 if (!find_symbol(VMLINUX_SYMBOL_STR(module_layout), NULL,
Peter Zijlstra926a59b2015-05-27 11:09:35 +09301365 &crc, true, false)) {
1366 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367 BUG();
Peter Zijlstra926a59b2015-05-27 11:09:35 +09301368 }
1369 preempt_enable();
James Hogana4b6a772013-03-18 19:38:56 +10301370 return check_version(sechdrs, versindex,
1371 VMLINUX_SYMBOL_STR(module_layout), mod, crc,
Rusty Russelld4703ae2009-12-15 16:28:32 -06001372 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373}
1374
Rusty Russell91e37a72008-05-09 16:25:28 +10001375/* First part is kernel version, which we ignore if module has crcs. */
1376static inline int same_magic(const char *amagic, const char *bmagic,
1377 bool has_crcs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378{
Rusty Russell91e37a72008-05-09 16:25:28 +10001379 if (has_crcs) {
1380 amagic += strcspn(amagic, " ");
1381 bmagic += strcspn(bmagic, " ");
1382 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383 return strcmp(amagic, bmagic) == 0;
1384}
1385#else
1386static inline int check_version(Elf_Shdr *sechdrs,
1387 unsigned int versindex,
1388 const char *symname,
Ionut Alexa6da0b562014-11-10 09:31:29 +10301389 struct module *mod,
Rusty Russelld4703ae2009-12-15 16:28:32 -06001390 const unsigned long *crc,
1391 const struct module *crc_owner)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392{
1393 return 1;
1394}
1395
1396static inline int check_modstruct_version(Elf_Shdr *sechdrs,
1397 unsigned int versindex,
1398 struct module *mod)
1399{
1400 return 1;
1401}
1402
Rusty Russell91e37a72008-05-09 16:25:28 +10001403static inline int same_magic(const char *amagic, const char *bmagic,
1404 bool has_crcs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405{
1406 return strcmp(amagic, bmagic) == 0;
1407}
1408#endif /* CONFIG_MODVERSIONS */
1409
Rusty Russell75676502010-06-05 11:17:36 -06001410/* Resolve a symbol for this module. I.e. if we find one, record usage. */
Rusty Russell49668682010-08-05 12:59:10 -06001411static const struct kernel_symbol *resolve_symbol(struct module *mod,
1412 const struct load_info *info,
Tim Abbott414fd312008-12-05 19:03:56 -05001413 const char *name,
Rusty Russell9bea7f22010-06-05 11:17:37 -06001414 char ownername[])
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415{
1416 struct module *owner;
Tim Abbott414fd312008-12-05 19:03:56 -05001417 const struct kernel_symbol *sym;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418 const unsigned long *crc;
Rusty Russell9bea7f22010-06-05 11:17:37 -06001419 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420
Peter Zijlstrad64810f2015-02-11 15:01:13 +10301421 /*
1422 * The module_mutex should not be a heavily contended lock;
1423 * if we get the occasional sleep here, we'll go an extra iteration
1424 * in the wait_event_interruptible(), which is harmless.
1425 */
1426 sched_annotate_sleep();
Rusty Russell75676502010-06-05 11:17:36 -06001427 mutex_lock(&module_mutex);
Tim Abbott414fd312008-12-05 19:03:56 -05001428 sym = find_symbol(name, &owner, &crc,
Andi Kleen25ddbb12008-10-15 22:01:41 -07001429 !(mod->taints & (1 << TAINT_PROPRIETARY_MODULE)), true);
Rusty Russell9bea7f22010-06-05 11:17:37 -06001430 if (!sym)
1431 goto unlock;
1432
Rusty Russell49668682010-08-05 12:59:10 -06001433 if (!check_version(info->sechdrs, info->index.vers, name, mod, crc,
1434 owner)) {
Rusty Russell9bea7f22010-06-05 11:17:37 -06001435 sym = ERR_PTR(-EINVAL);
1436 goto getname;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437 }
Rusty Russell9bea7f22010-06-05 11:17:37 -06001438
1439 err = ref_module(mod, owner);
1440 if (err) {
1441 sym = ERR_PTR(err);
1442 goto getname;
1443 }
1444
1445getname:
1446 /* We must make copy under the lock if we failed to get ref. */
1447 strncpy(ownername, module_name(owner), MODULE_NAME_LEN);
1448unlock:
Rusty Russell75676502010-06-05 11:17:36 -06001449 mutex_unlock(&module_mutex);
Linus Torvalds218ce732010-05-25 16:48:30 -07001450 return sym;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451}
1452
Rusty Russell49668682010-08-05 12:59:10 -06001453static const struct kernel_symbol *
1454resolve_symbol_wait(struct module *mod,
1455 const struct load_info *info,
1456 const char *name)
Rusty Russell9bea7f22010-06-05 11:17:37 -06001457{
1458 const struct kernel_symbol *ksym;
Rusty Russell49668682010-08-05 12:59:10 -06001459 char owner[MODULE_NAME_LEN];
Rusty Russell9bea7f22010-06-05 11:17:37 -06001460
1461 if (wait_event_interruptible_timeout(module_wq,
Rusty Russell49668682010-08-05 12:59:10 -06001462 !IS_ERR(ksym = resolve_symbol(mod, info, name, owner))
1463 || PTR_ERR(ksym) != -EBUSY,
Rusty Russell9bea7f22010-06-05 11:17:37 -06001464 30 * HZ) <= 0) {
Andrew Mortonbddb12b2013-11-12 15:11:28 -08001465 pr_warn("%s: gave up waiting for init of module %s.\n",
1466 mod->name, owner);
Rusty Russell9bea7f22010-06-05 11:17:37 -06001467 }
1468 return ksym;
1469}
1470
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471/*
1472 * /sys/module/foo/sections stuff
1473 * J. Corbet <corbet@lwn.net>
1474 */
Rusty Russell8f6d0372010-08-05 12:59:09 -06001475#ifdef CONFIG_SYSFS
Ben Hutchings10b465a2009-12-19 14:43:01 +00001476
Rusty Russell8f6d0372010-08-05 12:59:09 -06001477#ifdef CONFIG_KALLSYMS
Ben Hutchings10b465a2009-12-19 14:43:01 +00001478static inline bool sect_empty(const Elf_Shdr *sect)
1479{
1480 return !(sect->sh_flags & SHF_ALLOC) || sect->sh_size == 0;
1481}
1482
Ionut Alexa6da0b562014-11-10 09:31:29 +10301483struct module_sect_attr {
Rusty Russella58730c2008-03-13 09:03:44 +00001484 struct module_attribute mattr;
1485 char *name;
1486 unsigned long address;
1487};
1488
Ionut Alexa6da0b562014-11-10 09:31:29 +10301489struct module_sect_attrs {
Rusty Russella58730c2008-03-13 09:03:44 +00001490 struct attribute_group grp;
1491 unsigned int nsections;
1492 struct module_sect_attr attrs[0];
1493};
1494
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495static ssize_t module_sect_show(struct module_attribute *mattr,
Kay Sievers4befb022011-07-24 22:06:04 +09301496 struct module_kobject *mk, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497{
1498 struct module_sect_attr *sattr =
1499 container_of(mattr, struct module_sect_attr, mattr);
Kees Cook9f36e2c2011-03-22 16:34:22 -07001500 return sprintf(buf, "0x%pK\n", (void *)sattr->address);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501}
1502
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001503static void free_sect_attrs(struct module_sect_attrs *sect_attrs)
1504{
Rusty Russella58730c2008-03-13 09:03:44 +00001505 unsigned int section;
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001506
1507 for (section = 0; section < sect_attrs->nsections; section++)
1508 kfree(sect_attrs->attrs[section].name);
1509 kfree(sect_attrs);
1510}
1511
Rusty Russell8f6d0372010-08-05 12:59:09 -06001512static void add_sect_attrs(struct module *mod, const struct load_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513{
1514 unsigned int nloaded = 0, i, size[2];
1515 struct module_sect_attrs *sect_attrs;
1516 struct module_sect_attr *sattr;
1517 struct attribute **gattr;
Daniel Walker22a8bde2007-10-18 03:06:07 -07001518
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519 /* Count loaded sections and allocate structures */
Rusty Russell8f6d0372010-08-05 12:59:09 -06001520 for (i = 0; i < info->hdr->e_shnum; i++)
1521 if (!sect_empty(&info->sechdrs[i]))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522 nloaded++;
1523 size[0] = ALIGN(sizeof(*sect_attrs)
1524 + nloaded * sizeof(sect_attrs->attrs[0]),
1525 sizeof(sect_attrs->grp.attrs[0]));
1526 size[1] = (nloaded + 1) * sizeof(sect_attrs->grp.attrs[0]);
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001527 sect_attrs = kzalloc(size[0] + size[1], GFP_KERNEL);
1528 if (sect_attrs == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529 return;
1530
1531 /* Setup section attributes. */
1532 sect_attrs->grp.name = "sections";
1533 sect_attrs->grp.attrs = (void *)sect_attrs + size[0];
1534
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001535 sect_attrs->nsections = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536 sattr = &sect_attrs->attrs[0];
1537 gattr = &sect_attrs->grp.attrs[0];
Rusty Russell8f6d0372010-08-05 12:59:09 -06001538 for (i = 0; i < info->hdr->e_shnum; i++) {
1539 Elf_Shdr *sec = &info->sechdrs[i];
1540 if (sect_empty(sec))
Helge Deller35dead42009-12-03 00:29:15 +01001541 continue;
Rusty Russell8f6d0372010-08-05 12:59:09 -06001542 sattr->address = sec->sh_addr;
1543 sattr->name = kstrdup(info->secstrings + sec->sh_name,
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001544 GFP_KERNEL);
1545 if (sattr->name == NULL)
1546 goto out;
1547 sect_attrs->nsections++;
Eric W. Biederman361795b2010-02-12 13:41:56 -08001548 sysfs_attr_init(&sattr->mattr.attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549 sattr->mattr.show = module_sect_show;
1550 sattr->mattr.store = NULL;
1551 sattr->mattr.attr.name = sattr->name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552 sattr->mattr.attr.mode = S_IRUGO;
1553 *(gattr++) = &(sattr++)->mattr.attr;
1554 }
1555 *gattr = NULL;
1556
1557 if (sysfs_create_group(&mod->mkobj.kobj, &sect_attrs->grp))
1558 goto out;
1559
1560 mod->sect_attrs = sect_attrs;
1561 return;
1562 out:
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001563 free_sect_attrs(sect_attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564}
1565
1566static void remove_sect_attrs(struct module *mod)
1567{
1568 if (mod->sect_attrs) {
1569 sysfs_remove_group(&mod->mkobj.kobj,
1570 &mod->sect_attrs->grp);
1571 /* We are positive that no one is using any sect attrs
1572 * at this point. Deallocate immediately. */
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001573 free_sect_attrs(mod->sect_attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574 mod->sect_attrs = NULL;
1575 }
1576}
1577
Roland McGrath6d760132007-10-16 23:26:40 -07001578/*
1579 * /sys/module/foo/notes/.section.name gives contents of SHT_NOTE sections.
1580 */
1581
1582struct module_notes_attrs {
1583 struct kobject *dir;
1584 unsigned int notes;
1585 struct bin_attribute attrs[0];
1586};
1587
Chris Wright2c3c8be2010-05-12 18:28:57 -07001588static ssize_t module_notes_read(struct file *filp, struct kobject *kobj,
Roland McGrath6d760132007-10-16 23:26:40 -07001589 struct bin_attribute *bin_attr,
1590 char *buf, loff_t pos, size_t count)
1591{
1592 /*
1593 * The caller checked the pos and count against our size.
1594 */
1595 memcpy(buf, bin_attr->private + pos, count);
1596 return count;
1597}
1598
1599static void free_notes_attrs(struct module_notes_attrs *notes_attrs,
1600 unsigned int i)
1601{
1602 if (notes_attrs->dir) {
1603 while (i-- > 0)
1604 sysfs_remove_bin_file(notes_attrs->dir,
1605 &notes_attrs->attrs[i]);
Alexey Dobriyane9432092008-09-23 23:51:11 +04001606 kobject_put(notes_attrs->dir);
Roland McGrath6d760132007-10-16 23:26:40 -07001607 }
1608 kfree(notes_attrs);
1609}
1610
Rusty Russell8f6d0372010-08-05 12:59:09 -06001611static void add_notes_attrs(struct module *mod, const struct load_info *info)
Roland McGrath6d760132007-10-16 23:26:40 -07001612{
1613 unsigned int notes, loaded, i;
1614 struct module_notes_attrs *notes_attrs;
1615 struct bin_attribute *nattr;
1616
Ingo Molnarea6bff32009-08-28 10:44:56 +02001617 /* failed to create section attributes, so can't create notes */
1618 if (!mod->sect_attrs)
1619 return;
1620
Roland McGrath6d760132007-10-16 23:26:40 -07001621 /* Count notes sections and allocate structures. */
1622 notes = 0;
Rusty Russell8f6d0372010-08-05 12:59:09 -06001623 for (i = 0; i < info->hdr->e_shnum; i++)
1624 if (!sect_empty(&info->sechdrs[i]) &&
1625 (info->sechdrs[i].sh_type == SHT_NOTE))
Roland McGrath6d760132007-10-16 23:26:40 -07001626 ++notes;
1627
1628 if (notes == 0)
1629 return;
1630
1631 notes_attrs = kzalloc(sizeof(*notes_attrs)
1632 + notes * sizeof(notes_attrs->attrs[0]),
1633 GFP_KERNEL);
1634 if (notes_attrs == NULL)
1635 return;
1636
1637 notes_attrs->notes = notes;
1638 nattr = &notes_attrs->attrs[0];
Rusty Russell8f6d0372010-08-05 12:59:09 -06001639 for (loaded = i = 0; i < info->hdr->e_shnum; ++i) {
1640 if (sect_empty(&info->sechdrs[i]))
Roland McGrath6d760132007-10-16 23:26:40 -07001641 continue;
Rusty Russell8f6d0372010-08-05 12:59:09 -06001642 if (info->sechdrs[i].sh_type == SHT_NOTE) {
Eric W. Biederman361795b2010-02-12 13:41:56 -08001643 sysfs_bin_attr_init(nattr);
Roland McGrath6d760132007-10-16 23:26:40 -07001644 nattr->attr.name = mod->sect_attrs->attrs[loaded].name;
1645 nattr->attr.mode = S_IRUGO;
Rusty Russell8f6d0372010-08-05 12:59:09 -06001646 nattr->size = info->sechdrs[i].sh_size;
1647 nattr->private = (void *) info->sechdrs[i].sh_addr;
Roland McGrath6d760132007-10-16 23:26:40 -07001648 nattr->read = module_notes_read;
1649 ++nattr;
1650 }
1651 ++loaded;
1652 }
1653
Greg Kroah-Hartman4ff6abf2007-11-05 22:24:43 -08001654 notes_attrs->dir = kobject_create_and_add("notes", &mod->mkobj.kobj);
Roland McGrath6d760132007-10-16 23:26:40 -07001655 if (!notes_attrs->dir)
1656 goto out;
1657
1658 for (i = 0; i < notes; ++i)
1659 if (sysfs_create_bin_file(notes_attrs->dir,
1660 &notes_attrs->attrs[i]))
1661 goto out;
1662
1663 mod->notes_attrs = notes_attrs;
1664 return;
1665
1666 out:
1667 free_notes_attrs(notes_attrs, i);
1668}
1669
1670static void remove_notes_attrs(struct module *mod)
1671{
1672 if (mod->notes_attrs)
1673 free_notes_attrs(mod->notes_attrs, mod->notes_attrs->notes);
1674}
1675
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676#else
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001677
Rusty Russell8f6d0372010-08-05 12:59:09 -06001678static inline void add_sect_attrs(struct module *mod,
1679 const struct load_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680{
1681}
1682
1683static inline void remove_sect_attrs(struct module *mod)
1684{
1685}
Roland McGrath6d760132007-10-16 23:26:40 -07001686
Rusty Russell8f6d0372010-08-05 12:59:09 -06001687static inline void add_notes_attrs(struct module *mod,
1688 const struct load_info *info)
Roland McGrath6d760132007-10-16 23:26:40 -07001689{
1690}
1691
1692static inline void remove_notes_attrs(struct module *mod)
1693{
1694}
Rusty Russell8f6d0372010-08-05 12:59:09 -06001695#endif /* CONFIG_KALLSYMS */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696
Rusty Russell80a3d1b2010-06-05 11:17:36 -06001697static void add_usage_links(struct module *mod)
1698{
1699#ifdef CONFIG_MODULE_UNLOAD
1700 struct module_use *use;
1701 int nowarn;
1702
Rusty Russell75676502010-06-05 11:17:36 -06001703 mutex_lock(&module_mutex);
Rusty Russell80a3d1b2010-06-05 11:17:36 -06001704 list_for_each_entry(use, &mod->target_list, target_list) {
1705 nowarn = sysfs_create_link(use->target->holders_dir,
1706 &mod->mkobj.kobj, mod->name);
1707 }
Rusty Russell75676502010-06-05 11:17:36 -06001708 mutex_unlock(&module_mutex);
Rusty Russell80a3d1b2010-06-05 11:17:36 -06001709#endif
1710}
1711
1712static void del_usage_links(struct module *mod)
1713{
1714#ifdef CONFIG_MODULE_UNLOAD
1715 struct module_use *use;
1716
Rusty Russell75676502010-06-05 11:17:36 -06001717 mutex_lock(&module_mutex);
Rusty Russell80a3d1b2010-06-05 11:17:36 -06001718 list_for_each_entry(use, &mod->target_list, target_list)
1719 sysfs_remove_link(use->target->holders_dir, mod->name);
Rusty Russell75676502010-06-05 11:17:36 -06001720 mutex_unlock(&module_mutex);
Rusty Russell80a3d1b2010-06-05 11:17:36 -06001721#endif
1722}
1723
Rusty Russell6407ebb22010-06-05 11:17:36 -06001724static int module_add_modinfo_attrs(struct module *mod)
Matt Domschc988d2b2005-06-23 22:05:15 -07001725{
1726 struct module_attribute *attr;
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001727 struct module_attribute *temp_attr;
Matt Domschc988d2b2005-06-23 22:05:15 -07001728 int error = 0;
1729 int i;
1730
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001731 mod->modinfo_attrs = kzalloc((sizeof(struct module_attribute) *
1732 (ARRAY_SIZE(modinfo_attrs) + 1)),
1733 GFP_KERNEL);
1734 if (!mod->modinfo_attrs)
1735 return -ENOMEM;
1736
1737 temp_attr = mod->modinfo_attrs;
Matt Domschc988d2b2005-06-23 22:05:15 -07001738 for (i = 0; (attr = modinfo_attrs[i]) && !error; i++) {
1739 if (!attr->test ||
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001740 (attr->test && attr->test(mod))) {
1741 memcpy(temp_attr, attr, sizeof(*temp_attr));
Eric W. Biederman361795b2010-02-12 13:41:56 -08001742 sysfs_attr_init(&temp_attr->attr);
Ionut Alexa6da0b562014-11-10 09:31:29 +10301743 error = sysfs_create_file(&mod->mkobj.kobj,
1744 &temp_attr->attr);
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001745 ++temp_attr;
1746 }
Matt Domschc988d2b2005-06-23 22:05:15 -07001747 }
1748 return error;
1749}
1750
Rusty Russell6407ebb22010-06-05 11:17:36 -06001751static void module_remove_modinfo_attrs(struct module *mod)
Matt Domschc988d2b2005-06-23 22:05:15 -07001752{
1753 struct module_attribute *attr;
1754 int i;
1755
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001756 for (i = 0; (attr = &mod->modinfo_attrs[i]); i++) {
1757 /* pick a field to test for end of list */
1758 if (!attr->attr.name)
1759 break;
Ionut Alexa6da0b562014-11-10 09:31:29 +10301760 sysfs_remove_file(&mod->mkobj.kobj, &attr->attr);
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001761 if (attr->free)
1762 attr->free(mod);
Matt Domschc988d2b2005-06-23 22:05:15 -07001763 }
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001764 kfree(mod->modinfo_attrs);
Matt Domschc988d2b2005-06-23 22:05:15 -07001765}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766
Li Zhong942e4432013-09-03 16:33:57 +09301767static void mod_kobject_put(struct module *mod)
1768{
1769 DECLARE_COMPLETION_ONSTACK(c);
1770 mod->mkobj.kobj_completion = &c;
1771 kobject_put(&mod->mkobj.kobj);
1772 wait_for_completion(&c);
1773}
1774
Rusty Russell6407ebb22010-06-05 11:17:36 -06001775static int mod_sysfs_init(struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776{
1777 int err;
Greg Kroah-Hartman6494a932008-01-27 15:38:40 -08001778 struct kobject *kobj;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779
Greg Kroah-Hartman823bccf2007-04-13 13:15:19 -07001780 if (!module_sysfs_initialized) {
Andrew Mortonbddb12b2013-11-12 15:11:28 -08001781 pr_err("%s: module sysfs not initialized\n", mod->name);
Ed Swierk1cc5f712006-09-25 16:25:36 -07001782 err = -EINVAL;
1783 goto out;
1784 }
Greg Kroah-Hartman6494a932008-01-27 15:38:40 -08001785
1786 kobj = kset_find_obj(module_kset, mod->name);
1787 if (kobj) {
Andrew Mortonbddb12b2013-11-12 15:11:28 -08001788 pr_err("%s: module is already loaded\n", mod->name);
Greg Kroah-Hartman6494a932008-01-27 15:38:40 -08001789 kobject_put(kobj);
1790 err = -EINVAL;
1791 goto out;
1792 }
1793
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794 mod->mkobj.mod = mod;
Kay Sieverse17e0f52006-11-24 12:15:25 +01001795
Greg Kroah-Hartmanac3c8142007-12-17 23:05:35 -07001796 memset(&mod->mkobj.kobj, 0, sizeof(mod->mkobj.kobj));
1797 mod->mkobj.kobj.kset = module_kset;
1798 err = kobject_init_and_add(&mod->mkobj.kobj, &module_ktype, NULL,
1799 "%s", mod->name);
1800 if (err)
Li Zhong942e4432013-09-03 16:33:57 +09301801 mod_kobject_put(mod);
Kay Sievers270a6c42007-01-18 13:26:15 +01001802
Kay Sievers97c146e2007-11-29 23:46:11 +01001803 /* delay uevent until full sysfs population */
Kay Sievers270a6c42007-01-18 13:26:15 +01001804out:
1805 return err;
1806}
1807
Rusty Russell6407ebb22010-06-05 11:17:36 -06001808static int mod_sysfs_setup(struct module *mod,
Rusty Russell8f6d0372010-08-05 12:59:09 -06001809 const struct load_info *info,
Kay Sievers270a6c42007-01-18 13:26:15 +01001810 struct kernel_param *kparam,
1811 unsigned int num_params)
1812{
1813 int err;
1814
Rusty Russell80a3d1b2010-06-05 11:17:36 -06001815 err = mod_sysfs_init(mod);
1816 if (err)
1817 goto out;
1818
Greg Kroah-Hartman4ff6abf2007-11-05 22:24:43 -08001819 mod->holders_dir = kobject_create_and_add("holders", &mod->mkobj.kobj);
Akinobu Mita240936e2007-04-26 00:12:09 -07001820 if (!mod->holders_dir) {
1821 err = -ENOMEM;
Kay Sievers270a6c42007-01-18 13:26:15 +01001822 goto out_unreg;
Akinobu Mita240936e2007-04-26 00:12:09 -07001823 }
Kay Sievers270a6c42007-01-18 13:26:15 +01001824
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825 err = module_param_sysfs_setup(mod, kparam, num_params);
1826 if (err)
Kay Sievers270a6c42007-01-18 13:26:15 +01001827 goto out_unreg_holders;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828
Matt Domschc988d2b2005-06-23 22:05:15 -07001829 err = module_add_modinfo_attrs(mod);
1830 if (err)
Kay Sieverse17e0f52006-11-24 12:15:25 +01001831 goto out_unreg_param;
Matt Domschc988d2b2005-06-23 22:05:15 -07001832
Rusty Russell80a3d1b2010-06-05 11:17:36 -06001833 add_usage_links(mod);
Rusty Russell8f6d0372010-08-05 12:59:09 -06001834 add_sect_attrs(mod, info);
1835 add_notes_attrs(mod, info);
Rusty Russell80a3d1b2010-06-05 11:17:36 -06001836
Kay Sieverse17e0f52006-11-24 12:15:25 +01001837 kobject_uevent(&mod->mkobj.kobj, KOBJ_ADD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838 return 0;
1839
Kay Sieverse17e0f52006-11-24 12:15:25 +01001840out_unreg_param:
1841 module_param_sysfs_remove(mod);
Kay Sievers270a6c42007-01-18 13:26:15 +01001842out_unreg_holders:
Greg Kroah-Hartman78a2d902007-12-20 08:13:05 -08001843 kobject_put(mod->holders_dir);
Kay Sievers270a6c42007-01-18 13:26:15 +01001844out_unreg:
Li Zhong942e4432013-09-03 16:33:57 +09301845 mod_kobject_put(mod);
Rusty Russell80a3d1b2010-06-05 11:17:36 -06001846out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847 return err;
1848}
Denis V. Lunev34e4e2f2008-05-20 13:59:48 +04001849
1850static void mod_sysfs_fini(struct module *mod)
1851{
Rusty Russell8f6d0372010-08-05 12:59:09 -06001852 remove_notes_attrs(mod);
1853 remove_sect_attrs(mod);
Li Zhong942e4432013-09-03 16:33:57 +09301854 mod_kobject_put(mod);
Denis V. Lunev34e4e2f2008-05-20 13:59:48 +04001855}
1856
Rusty Russell8f6d0372010-08-05 12:59:09 -06001857#else /* !CONFIG_SYSFS */
Denis V. Lunev34e4e2f2008-05-20 13:59:48 +04001858
Rusty Russell8f6d0372010-08-05 12:59:09 -06001859static int mod_sysfs_setup(struct module *mod,
1860 const struct load_info *info,
Rusty Russell6407ebb22010-06-05 11:17:36 -06001861 struct kernel_param *kparam,
1862 unsigned int num_params)
1863{
1864 return 0;
1865}
1866
Denis V. Lunev34e4e2f2008-05-20 13:59:48 +04001867static void mod_sysfs_fini(struct module *mod)
1868{
1869}
1870
Rusty Russell36b03602010-08-05 12:59:09 -06001871static void module_remove_modinfo_attrs(struct module *mod)
1872{
1873}
1874
Rusty Russell80a3d1b2010-06-05 11:17:36 -06001875static void del_usage_links(struct module *mod)
1876{
1877}
1878
Denis V. Lunev34e4e2f2008-05-20 13:59:48 +04001879#endif /* CONFIG_SYSFS */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880
Rusty Russell36b03602010-08-05 12:59:09 -06001881static void mod_sysfs_teardown(struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882{
Rusty Russell80a3d1b2010-06-05 11:17:36 -06001883 del_usage_links(mod);
Matt Domschc988d2b2005-06-23 22:05:15 -07001884 module_remove_modinfo_attrs(mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885 module_param_sysfs_remove(mod);
Greg Kroah-Hartman78a2d902007-12-20 08:13:05 -08001886 kobject_put(mod->mkobj.drivers_dir);
1887 kobject_put(mod->holders_dir);
Denis V. Lunev34e4e2f2008-05-20 13:59:48 +04001888 mod_sysfs_fini(mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889}
1890
matthieu castet84e1c6b2010-11-16 22:35:16 +01001891#ifdef CONFIG_DEBUG_SET_MODULE_RONX
1892/*
1893 * LKM RO/NX protection: protect module's text/ro-data
1894 * from modification and any data from execution.
1895 */
1896void set_page_attributes(void *start, void *end, int (*set)(unsigned long start, int num_pages))
1897{
1898 unsigned long begin_pfn = PFN_DOWN((unsigned long)start);
1899 unsigned long end_pfn = PFN_DOWN((unsigned long)end);
1900
1901 if (end_pfn > begin_pfn)
1902 set(begin_pfn << PAGE_SHIFT, end_pfn - begin_pfn);
1903}
1904
1905static void set_section_ro_nx(void *base,
1906 unsigned long text_size,
1907 unsigned long ro_size,
1908 unsigned long total_size)
1909{
1910 /* begin and end PFNs of the current subsection */
1911 unsigned long begin_pfn;
1912 unsigned long end_pfn;
1913
1914 /*
1915 * Set RO for module text and RO-data:
1916 * - Always protect first page.
1917 * - Do not protect last partial page.
1918 */
1919 if (ro_size > 0)
1920 set_page_attributes(base, base + ro_size, set_memory_ro);
1921
1922 /*
1923 * Set NX permissions for module data:
1924 * - Do not protect first partial page.
1925 * - Always protect last page.
1926 */
1927 if (total_size > text_size) {
1928 begin_pfn = PFN_UP((unsigned long)base + text_size);
1929 end_pfn = PFN_UP((unsigned long)base + total_size);
1930 if (end_pfn > begin_pfn)
1931 set_memory_nx(begin_pfn << PAGE_SHIFT, end_pfn - begin_pfn);
1932 }
1933}
1934
Jan Glauber01526ed2011-05-19 16:55:26 -06001935static void unset_module_core_ro_nx(struct module *mod)
matthieu castet84e1c6b2010-11-16 22:35:16 +01001936{
Jan Glauber01526ed2011-05-19 16:55:26 -06001937 set_page_attributes(mod->module_core + mod->core_text_size,
1938 mod->module_core + mod->core_size,
1939 set_memory_x);
1940 set_page_attributes(mod->module_core,
1941 mod->module_core + mod->core_ro_size,
1942 set_memory_rw);
1943}
1944
1945static void unset_module_init_ro_nx(struct module *mod)
1946{
1947 set_page_attributes(mod->module_init + mod->init_text_size,
1948 mod->module_init + mod->init_size,
1949 set_memory_x);
1950 set_page_attributes(mod->module_init,
1951 mod->module_init + mod->init_ro_size,
1952 set_memory_rw);
matthieu castet84e1c6b2010-11-16 22:35:16 +01001953}
1954
1955/* Iterate through all modules and set each module's text as RW */
Daniel J Blueman5d05c702011-03-08 22:01:47 +08001956void set_all_modules_text_rw(void)
matthieu castet84e1c6b2010-11-16 22:35:16 +01001957{
1958 struct module *mod;
1959
1960 mutex_lock(&module_mutex);
1961 list_for_each_entry_rcu(mod, &modules, list) {
Rusty Russell0d21b0e2013-01-12 11:38:44 +10301962 if (mod->state == MODULE_STATE_UNFORMED)
1963 continue;
matthieu castet84e1c6b2010-11-16 22:35:16 +01001964 if ((mod->module_core) && (mod->core_text_size)) {
1965 set_page_attributes(mod->module_core,
1966 mod->module_core + mod->core_text_size,
1967 set_memory_rw);
1968 }
1969 if ((mod->module_init) && (mod->init_text_size)) {
1970 set_page_attributes(mod->module_init,
1971 mod->module_init + mod->init_text_size,
1972 set_memory_rw);
1973 }
1974 }
1975 mutex_unlock(&module_mutex);
1976}
1977
1978/* Iterate through all modules and set each module's text as RO */
Daniel J Blueman5d05c702011-03-08 22:01:47 +08001979void set_all_modules_text_ro(void)
matthieu castet84e1c6b2010-11-16 22:35:16 +01001980{
1981 struct module *mod;
1982
1983 mutex_lock(&module_mutex);
1984 list_for_each_entry_rcu(mod, &modules, list) {
Rusty Russell0d21b0e2013-01-12 11:38:44 +10301985 if (mod->state == MODULE_STATE_UNFORMED)
1986 continue;
matthieu castet84e1c6b2010-11-16 22:35:16 +01001987 if ((mod->module_core) && (mod->core_text_size)) {
1988 set_page_attributes(mod->module_core,
1989 mod->module_core + mod->core_text_size,
1990 set_memory_ro);
1991 }
1992 if ((mod->module_init) && (mod->init_text_size)) {
1993 set_page_attributes(mod->module_init,
1994 mod->module_init + mod->init_text_size,
1995 set_memory_ro);
1996 }
1997 }
1998 mutex_unlock(&module_mutex);
1999}
2000#else
2001static 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 -06002002static void unset_module_core_ro_nx(struct module *mod) { }
2003static void unset_module_init_ro_nx(struct module *mod) { }
matthieu castet84e1c6b2010-11-16 22:35:16 +01002004#endif
2005
Rusty Russellbe1f2212015-01-20 09:07:05 +10302006void __weak module_memfree(void *module_region)
Jonas Bonn74e08fc2011-06-30 21:22:11 +02002007{
2008 vfree(module_region);
2009}
2010
2011void __weak module_arch_cleanup(struct module *mod)
2012{
2013}
2014
Rusty Russelld453cde2015-01-20 09:07:04 +10302015void __weak module_arch_freeing_init(struct module *mod)
2016{
2017}
2018
Rusty Russell75676502010-06-05 11:17:36 -06002019/* Free a module, remove from lists, etc. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020static void free_module(struct module *mod)
2021{
Li Zefan7ead8b82009-08-17 16:56:28 +08002022 trace_module_free(mod);
2023
Rusty Russell36b03602010-08-05 12:59:09 -06002024 mod_sysfs_teardown(mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025
Rusty Russell944a1fa2013-04-17 13:20:03 +09302026 /* We leave it in list to prevent duplicate loads, but make sure
2027 * that noone uses it while it's being deconstructed. */
Prarit Bhargavad3051b42014-10-14 02:51:39 +10302028 mutex_lock(&module_mutex);
Rusty Russell944a1fa2013-04-17 13:20:03 +09302029 mod->state = MODULE_STATE_UNFORMED;
Prarit Bhargavad3051b42014-10-14 02:51:39 +10302030 mutex_unlock(&module_mutex);
Rusty Russell944a1fa2013-04-17 13:20:03 +09302031
Jason Baronb82bab4b2010-07-27 13:18:01 -07002032 /* Remove dynamic debug info */
2033 ddebug_remove_module(mod->name);
2034
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035 /* Arch-specific cleanup. */
2036 module_arch_cleanup(mod);
2037
2038 /* Module unload stuff */
2039 module_unload_free(mod);
2040
Rusty Russelle180a6b2009-03-31 13:05:29 -06002041 /* Free any allocated parameters. */
2042 destroy_params(mod->kp, mod->num_kp);
2043
Rusty Russell944a1fa2013-04-17 13:20:03 +09302044 /* Now we can delete it from the lists */
2045 mutex_lock(&module_mutex);
Masami Hiramatsu461e34a2014-11-10 09:27:29 +10302046 /* Unlink carefully: kallsyms could be walking list. */
2047 list_del_rcu(&mod->list);
Peter Zijlstra93c2e102015-05-27 11:09:37 +09302048 mod_tree_remove(mod);
Masami Hiramatsu0286b5e2014-11-10 09:28:29 +10302049 /* Remove this module from bug list, this uses list_del_rcu */
Masami Hiramatsu461e34a2014-11-10 09:27:29 +10302050 module_bug_cleanup(mod);
Peter Zijlstra0be964b2015-05-27 11:09:35 +09302051 /* Wait for RCU-sched synchronizing before releasing mod->list and buglist. */
2052 synchronize_sched();
Rusty Russell944a1fa2013-04-17 13:20:03 +09302053 mutex_unlock(&module_mutex);
2054
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055 /* This may be NULL, but that's OK */
Jan Glauber01526ed2011-05-19 16:55:26 -06002056 unset_module_init_ro_nx(mod);
Rusty Russelld453cde2015-01-20 09:07:04 +10302057 module_arch_freeing_init(mod);
Rusty Russellbe1f2212015-01-20 09:07:05 +10302058 module_memfree(mod->module_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059 kfree(mod->args);
Tejun Heo259354d2010-03-10 18:56:10 +09002060 percpu_modfree(mod);
Rusty Russell9f85a4b2010-08-05 12:59:04 -06002061
Peter Zijlstra35a93932015-02-26 16:23:11 +01002062 /* Free lock-classes; relies on the preceding sync_rcu(). */
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002063 lockdep_free_key_range(mod->module_core, mod->core_size);
2064
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065 /* Finally, free the core (containing the module structure) */
Jan Glauber01526ed2011-05-19 16:55:26 -06002066 unset_module_core_ro_nx(mod);
Rusty Russellbe1f2212015-01-20 09:07:05 +10302067 module_memfree(mod->module_core);
Bernd Schmidteb8cdec2009-09-21 17:03:57 -07002068
2069#ifdef CONFIG_MPU
2070 update_protections(current->mm);
2071#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072}
2073
2074void *__symbol_get(const char *symbol)
2075{
2076 struct module *owner;
Tim Abbott414fd312008-12-05 19:03:56 -05002077 const struct kernel_symbol *sym;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078
Rusty Russell24da1cb2007-07-15 23:41:46 -07002079 preempt_disable();
Tim Abbott414fd312008-12-05 19:03:56 -05002080 sym = find_symbol(symbol, &owner, NULL, true, true);
2081 if (sym && strong_try_module_get(owner))
2082 sym = NULL;
Rusty Russell24da1cb2007-07-15 23:41:46 -07002083 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084
Tim Abbott414fd312008-12-05 19:03:56 -05002085 return sym ? (void *)sym->value : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086}
2087EXPORT_SYMBOL_GPL(__symbol_get);
2088
Ashutosh Naikeea8b542006-01-08 01:04:25 -08002089/*
2090 * Ensure that an exported symbol [global namespace] does not already exist
Robert P. J. Day02a3e592007-05-09 07:26:28 +02002091 * in the kernel or in some other module's exported symbol table.
Rusty Russellbe593f42010-06-05 11:17:37 -06002092 *
2093 * You must hold the module_mutex.
Ashutosh Naikeea8b542006-01-08 01:04:25 -08002094 */
2095static int verify_export_symbols(struct module *mod)
2096{
Rusty Russellb2111042008-05-01 21:15:00 -05002097 unsigned int i;
Ashutosh Naikeea8b542006-01-08 01:04:25 -08002098 struct module *owner;
Rusty Russellb2111042008-05-01 21:15:00 -05002099 const struct kernel_symbol *s;
2100 struct {
2101 const struct kernel_symbol *sym;
2102 unsigned int num;
2103 } arr[] = {
2104 { mod->syms, mod->num_syms },
2105 { mod->gpl_syms, mod->num_gpl_syms },
2106 { mod->gpl_future_syms, mod->num_gpl_future_syms },
Denys Vlasenkof7f5b672008-07-22 19:24:26 -05002107#ifdef CONFIG_UNUSED_SYMBOLS
Rusty Russellb2111042008-05-01 21:15:00 -05002108 { mod->unused_syms, mod->num_unused_syms },
2109 { mod->unused_gpl_syms, mod->num_unused_gpl_syms },
Denys Vlasenkof7f5b672008-07-22 19:24:26 -05002110#endif
Rusty Russellb2111042008-05-01 21:15:00 -05002111 };
Ashutosh Naikeea8b542006-01-08 01:04:25 -08002112
Rusty Russellb2111042008-05-01 21:15:00 -05002113 for (i = 0; i < ARRAY_SIZE(arr); i++) {
2114 for (s = arr[i].sym; s < arr[i].sym + arr[i].num; s++) {
Rusty Russellbe593f42010-06-05 11:17:37 -06002115 if (find_symbol(s->name, &owner, NULL, true, false)) {
Andrew Mortonbddb12b2013-11-12 15:11:28 -08002116 pr_err("%s: exports duplicate symbol %s"
Rusty Russellb2111042008-05-01 21:15:00 -05002117 " (owned by %s)\n",
2118 mod->name, s->name, module_name(owner));
2119 return -ENOEXEC;
2120 }
Ashutosh Naikeea8b542006-01-08 01:04:25 -08002121 }
Rusty Russellb2111042008-05-01 21:15:00 -05002122 }
2123 return 0;
Ashutosh Naikeea8b542006-01-08 01:04:25 -08002124}
2125
Matti Linnanvuori9a4b9702007-11-08 08:37:38 -08002126/* Change all symbols so that st_value encodes the pointer directly. */
Rusty Russell49668682010-08-05 12:59:10 -06002127static int simplify_symbols(struct module *mod, const struct load_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128{
Rusty Russell49668682010-08-05 12:59:10 -06002129 Elf_Shdr *symsec = &info->sechdrs[info->index.sym];
2130 Elf_Sym *sym = (void *)symsec->sh_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131 unsigned long secbase;
Rusty Russell49668682010-08-05 12:59:10 -06002132 unsigned int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133 int ret = 0;
Tim Abbott414fd312008-12-05 19:03:56 -05002134 const struct kernel_symbol *ksym;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135
Rusty Russell49668682010-08-05 12:59:10 -06002136 for (i = 1; i < symsec->sh_size / sizeof(Elf_Sym); i++) {
2137 const char *name = info->strtab + sym[i].st_name;
2138
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139 switch (sym[i].st_shndx) {
2140 case SHN_COMMON:
Joe Mario80375982014-02-08 09:01:09 +01002141 /* Ignore common symbols */
2142 if (!strncmp(name, "__gnu_lto", 9))
2143 break;
2144
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145 /* We compiled with -fno-common. These are not
2146 supposed to happen. */
Jim Cromie5e124162011-12-06 12:11:31 -07002147 pr_debug("Common symbol: %s\n", name);
Ionut Alexa6da0b562014-11-10 09:31:29 +10302148 pr_warn("%s: please compile with -fno-common\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149 mod->name);
2150 ret = -ENOEXEC;
2151 break;
2152
2153 case SHN_ABS:
2154 /* Don't need to do anything */
Jim Cromie5e124162011-12-06 12:11:31 -07002155 pr_debug("Absolute symbol: 0x%08lx\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156 (long)sym[i].st_value);
2157 break;
2158
2159 case SHN_UNDEF:
Rusty Russell49668682010-08-05 12:59:10 -06002160 ksym = resolve_symbol_wait(mod, info, name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161 /* Ok if resolved. */
Rusty Russell9bea7f22010-06-05 11:17:37 -06002162 if (ksym && !IS_ERR(ksym)) {
Tim Abbott414fd312008-12-05 19:03:56 -05002163 sym[i].st_value = ksym->value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164 break;
Tim Abbott414fd312008-12-05 19:03:56 -05002165 }
2166
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167 /* Ok if weak. */
Rusty Russell9bea7f22010-06-05 11:17:37 -06002168 if (!ksym && ELF_ST_BIND(sym[i].st_info) == STB_WEAK)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169 break;
2170
Andrew Mortonbddb12b2013-11-12 15:11:28 -08002171 pr_warn("%s: Unknown symbol %s (err %li)\n",
2172 mod->name, name, PTR_ERR(ksym));
Rusty Russell9bea7f22010-06-05 11:17:37 -06002173 ret = PTR_ERR(ksym) ?: -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174 break;
2175
2176 default:
2177 /* Divert to percpu allocation if a percpu var. */
Rusty Russell49668682010-08-05 12:59:10 -06002178 if (sym[i].st_shndx == info->index.pcpu)
Tejun Heo259354d2010-03-10 18:56:10 +09002179 secbase = (unsigned long)mod_percpu(mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180 else
Rusty Russell49668682010-08-05 12:59:10 -06002181 secbase = info->sechdrs[sym[i].st_shndx].sh_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182 sym[i].st_value += secbase;
2183 break;
2184 }
2185 }
2186
2187 return ret;
2188}
2189
Rusty Russell49668682010-08-05 12:59:10 -06002190static int apply_relocations(struct module *mod, const struct load_info *info)
Rusty Russell22e268e2010-08-05 12:59:05 -06002191{
2192 unsigned int i;
2193 int err = 0;
2194
2195 /* Now do relocations. */
Rusty Russell49668682010-08-05 12:59:10 -06002196 for (i = 1; i < info->hdr->e_shnum; i++) {
2197 unsigned int infosec = info->sechdrs[i].sh_info;
Rusty Russell22e268e2010-08-05 12:59:05 -06002198
2199 /* Not a valid relocation section? */
Rusty Russell49668682010-08-05 12:59:10 -06002200 if (infosec >= info->hdr->e_shnum)
Rusty Russell22e268e2010-08-05 12:59:05 -06002201 continue;
2202
2203 /* Don't bother with non-allocated sections */
Rusty Russell49668682010-08-05 12:59:10 -06002204 if (!(info->sechdrs[infosec].sh_flags & SHF_ALLOC))
Rusty Russell22e268e2010-08-05 12:59:05 -06002205 continue;
2206
Rusty Russell49668682010-08-05 12:59:10 -06002207 if (info->sechdrs[i].sh_type == SHT_REL)
2208 err = apply_relocate(info->sechdrs, info->strtab,
2209 info->index.sym, i, mod);
2210 else if (info->sechdrs[i].sh_type == SHT_RELA)
2211 err = apply_relocate_add(info->sechdrs, info->strtab,
2212 info->index.sym, i, mod);
Rusty Russell22e268e2010-08-05 12:59:05 -06002213 if (err < 0)
2214 break;
2215 }
2216 return err;
2217}
2218
Helge Deller088af9a2008-12-31 12:31:18 +01002219/* Additional bytes needed by arch in front of individual sections */
2220unsigned int __weak arch_mod_section_prepend(struct module *mod,
2221 unsigned int section)
2222{
2223 /* default implementation just returns zero */
2224 return 0;
2225}
2226
Linus Torvalds1da177e2005-04-16 15:20:36 -07002227/* Update size with this section: return offset. */
Helge Deller088af9a2008-12-31 12:31:18 +01002228static long get_offset(struct module *mod, unsigned int *size,
2229 Elf_Shdr *sechdr, unsigned int section)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002230{
2231 long ret;
2232
Helge Deller088af9a2008-12-31 12:31:18 +01002233 *size += arch_mod_section_prepend(mod, section);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002234 ret = ALIGN(*size, sechdr->sh_addralign ?: 1);
2235 *size = ret + sechdr->sh_size;
2236 return ret;
2237}
2238
2239/* Lay out the SHF_ALLOC sections in a way not dissimilar to how ld
2240 might -- code, read-only data, read-write data, small data. Tally
2241 sizes, and place the offsets into sh_entsize fields: high bit means it
2242 belongs in init. */
Rusty Russell49668682010-08-05 12:59:10 -06002243static void layout_sections(struct module *mod, struct load_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002244{
2245 static unsigned long const masks[][2] = {
2246 /* NOTE: all executable code must be the first section
2247 * in this array; otherwise modify the text_size
2248 * finder in the two loops below */
2249 { SHF_EXECINSTR | SHF_ALLOC, ARCH_SHF_SMALL },
2250 { SHF_ALLOC, SHF_WRITE | ARCH_SHF_SMALL },
2251 { SHF_WRITE | SHF_ALLOC, ARCH_SHF_SMALL },
2252 { ARCH_SHF_SMALL | SHF_ALLOC, 0 }
2253 };
2254 unsigned int m, i;
2255
Rusty Russell49668682010-08-05 12:59:10 -06002256 for (i = 0; i < info->hdr->e_shnum; i++)
2257 info->sechdrs[i].sh_entsize = ~0UL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258
Jim Cromie5e124162011-12-06 12:11:31 -07002259 pr_debug("Core section allocation order:\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002260 for (m = 0; m < ARRAY_SIZE(masks); ++m) {
Rusty Russell49668682010-08-05 12:59:10 -06002261 for (i = 0; i < info->hdr->e_shnum; ++i) {
2262 Elf_Shdr *s = &info->sechdrs[i];
2263 const char *sname = info->secstrings + s->sh_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002264
2265 if ((s->sh_flags & masks[m][0]) != masks[m][0]
2266 || (s->sh_flags & masks[m][1])
2267 || s->sh_entsize != ~0UL
Rusty Russell49668682010-08-05 12:59:10 -06002268 || strstarts(sname, ".init"))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002269 continue;
Helge Deller088af9a2008-12-31 12:31:18 +01002270 s->sh_entsize = get_offset(mod, &mod->core_size, s, i);
Jim Cromie5e124162011-12-06 12:11:31 -07002271 pr_debug("\t%s\n", sname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002272 }
matthieu castet84e1c6b2010-11-16 22:35:16 +01002273 switch (m) {
2274 case 0: /* executable */
2275 mod->core_size = debug_align(mod->core_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002276 mod->core_text_size = mod->core_size;
matthieu castet84e1c6b2010-11-16 22:35:16 +01002277 break;
2278 case 1: /* RO: text and ro-data */
2279 mod->core_size = debug_align(mod->core_size);
2280 mod->core_ro_size = mod->core_size;
2281 break;
2282 case 3: /* whole core */
2283 mod->core_size = debug_align(mod->core_size);
2284 break;
2285 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002286 }
2287
Jim Cromie5e124162011-12-06 12:11:31 -07002288 pr_debug("Init section allocation order:\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002289 for (m = 0; m < ARRAY_SIZE(masks); ++m) {
Rusty Russell49668682010-08-05 12:59:10 -06002290 for (i = 0; i < info->hdr->e_shnum; ++i) {
2291 Elf_Shdr *s = &info->sechdrs[i];
2292 const char *sname = info->secstrings + s->sh_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002293
2294 if ((s->sh_flags & masks[m][0]) != masks[m][0]
2295 || (s->sh_flags & masks[m][1])
2296 || s->sh_entsize != ~0UL
Rusty Russell49668682010-08-05 12:59:10 -06002297 || !strstarts(sname, ".init"))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298 continue;
Helge Deller088af9a2008-12-31 12:31:18 +01002299 s->sh_entsize = (get_offset(mod, &mod->init_size, s, i)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300 | INIT_OFFSET_MASK);
Jim Cromie5e124162011-12-06 12:11:31 -07002301 pr_debug("\t%s\n", sname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002302 }
matthieu castet84e1c6b2010-11-16 22:35:16 +01002303 switch (m) {
2304 case 0: /* executable */
2305 mod->init_size = debug_align(mod->init_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306 mod->init_text_size = mod->init_size;
matthieu castet84e1c6b2010-11-16 22:35:16 +01002307 break;
2308 case 1: /* RO: text and ro-data */
2309 mod->init_size = debug_align(mod->init_size);
2310 mod->init_ro_size = mod->init_size;
2311 break;
2312 case 3: /* whole init */
2313 mod->init_size = debug_align(mod->init_size);
2314 break;
2315 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002316 }
2317}
2318
Linus Torvalds1da177e2005-04-16 15:20:36 -07002319static void set_license(struct module *mod, const char *license)
2320{
2321 if (!license)
2322 license = "unspecified";
2323
Florin Malitafa3ba2e82006-10-11 01:21:48 -07002324 if (!license_is_gpl_compatible(license)) {
Andi Kleen25ddbb12008-10-15 22:01:41 -07002325 if (!test_taint(TAINT_PROPRIETARY_MODULE))
Andrew Mortonbddb12b2013-11-12 15:11:28 -08002326 pr_warn("%s: module license '%s' taints kernel.\n",
2327 mod->name, license);
Rusty Russell373d4d02013-01-21 17:17:39 +10302328 add_taint_module(mod, TAINT_PROPRIETARY_MODULE,
2329 LOCKDEP_NOW_UNRELIABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002330 }
2331}
2332
2333/* Parse tag=value strings from .modinfo section */
2334static char *next_string(char *string, unsigned long *secsize)
2335{
2336 /* Skip non-zero chars */
2337 while (string[0]) {
2338 string++;
2339 if ((*secsize)-- <= 1)
2340 return NULL;
2341 }
2342
2343 /* Skip any zero padding. */
2344 while (!string[0]) {
2345 string++;
2346 if ((*secsize)-- <= 1)
2347 return NULL;
2348 }
2349 return string;
2350}
2351
Rusty Russell49668682010-08-05 12:59:10 -06002352static char *get_modinfo(struct load_info *info, const char *tag)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002353{
2354 char *p;
2355 unsigned int taglen = strlen(tag);
Rusty Russell49668682010-08-05 12:59:10 -06002356 Elf_Shdr *infosec = &info->sechdrs[info->index.info];
2357 unsigned long size = infosec->sh_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002358
Rusty Russell49668682010-08-05 12:59:10 -06002359 for (p = (char *)infosec->sh_addr; p; p = next_string(p, &size)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002360 if (strncmp(p, tag, taglen) == 0 && p[taglen] == '=')
2361 return p + taglen + 1;
2362 }
2363 return NULL;
2364}
2365
Rusty Russell49668682010-08-05 12:59:10 -06002366static void setup_modinfo(struct module *mod, struct load_info *info)
Matt Domschc988d2b2005-06-23 22:05:15 -07002367{
2368 struct module_attribute *attr;
2369 int i;
2370
2371 for (i = 0; (attr = modinfo_attrs[i]); i++) {
2372 if (attr->setup)
Rusty Russell49668682010-08-05 12:59:10 -06002373 attr->setup(mod, get_modinfo(info, attr->attr.name));
Matt Domschc988d2b2005-06-23 22:05:15 -07002374 }
2375}
Matt Domschc988d2b2005-06-23 22:05:15 -07002376
Rusty Russella263f772009-09-25 00:32:58 -06002377static void free_modinfo(struct module *mod)
2378{
2379 struct module_attribute *attr;
2380 int i;
2381
2382 for (i = 0; (attr = modinfo_attrs[i]); i++) {
2383 if (attr->free)
2384 attr->free(mod);
2385 }
2386}
2387
Linus Torvalds1da177e2005-04-16 15:20:36 -07002388#ifdef CONFIG_KALLSYMS
WANG Cong15bba372008-07-24 15:41:48 +01002389
2390/* lookup symbol in given range of kernel_symbols */
2391static const struct kernel_symbol *lookup_symbol(const char *name,
2392 const struct kernel_symbol *start,
2393 const struct kernel_symbol *stop)
2394{
Alessio Igor Bogani9d634872011-05-18 22:35:59 +02002395 return bsearch(name, start, stop - start,
2396 sizeof(struct kernel_symbol), cmp_name);
WANG Cong15bba372008-07-24 15:41:48 +01002397}
2398
Tim Abbottca4787b2009-01-05 08:40:10 -06002399static int is_exported(const char *name, unsigned long value,
2400 const struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002401{
Tim Abbottca4787b2009-01-05 08:40:10 -06002402 const struct kernel_symbol *ks;
2403 if (!mod)
2404 ks = lookup_symbol(name, __start___ksymtab, __stop___ksymtab);
Sam Ravnborg3fd68052006-02-08 21:16:45 +01002405 else
Tim Abbottca4787b2009-01-05 08:40:10 -06002406 ks = lookup_symbol(name, mod->syms, mod->syms + mod->num_syms);
2407 return ks != NULL && ks->value == value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002408}
2409
2410/* As per nm */
Rusty Russelleded41c2010-08-05 12:59:07 -06002411static char elf_type(const Elf_Sym *sym, const struct load_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002412{
Rusty Russelleded41c2010-08-05 12:59:07 -06002413 const Elf_Shdr *sechdrs = info->sechdrs;
2414
Linus Torvalds1da177e2005-04-16 15:20:36 -07002415 if (ELF_ST_BIND(sym->st_info) == STB_WEAK) {
2416 if (ELF_ST_TYPE(sym->st_info) == STT_OBJECT)
2417 return 'v';
2418 else
2419 return 'w';
2420 }
2421 if (sym->st_shndx == SHN_UNDEF)
2422 return 'U';
2423 if (sym->st_shndx == SHN_ABS)
2424 return 'a';
2425 if (sym->st_shndx >= SHN_LORESERVE)
2426 return '?';
2427 if (sechdrs[sym->st_shndx].sh_flags & SHF_EXECINSTR)
2428 return 't';
2429 if (sechdrs[sym->st_shndx].sh_flags & SHF_ALLOC
2430 && sechdrs[sym->st_shndx].sh_type != SHT_NOBITS) {
2431 if (!(sechdrs[sym->st_shndx].sh_flags & SHF_WRITE))
2432 return 'r';
2433 else if (sechdrs[sym->st_shndx].sh_flags & ARCH_SHF_SMALL)
2434 return 'g';
2435 else
2436 return 'd';
2437 }
2438 if (sechdrs[sym->st_shndx].sh_type == SHT_NOBITS) {
2439 if (sechdrs[sym->st_shndx].sh_flags & ARCH_SHF_SMALL)
2440 return 's';
2441 else
2442 return 'b';
2443 }
Rusty Russelleded41c2010-08-05 12:59:07 -06002444 if (strstarts(info->secstrings + sechdrs[sym->st_shndx].sh_name,
2445 ".debug")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002446 return 'n';
Rusty Russelleded41c2010-08-05 12:59:07 -06002447 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002448 return '?';
2449}
2450
Jan Beulich4a496222009-07-06 14:50:42 +01002451static bool is_core_symbol(const Elf_Sym *src, const Elf_Shdr *sechdrs,
Ionut Alexa6da0b562014-11-10 09:31:29 +10302452 unsigned int shnum)
Jan Beulich4a496222009-07-06 14:50:42 +01002453{
2454 const Elf_Shdr *sec;
2455
2456 if (src->st_shndx == SHN_UNDEF
2457 || src->st_shndx >= shnum
2458 || !src->st_name)
2459 return false;
2460
2461 sec = sechdrs + src->st_shndx;
2462 if (!(sec->sh_flags & SHF_ALLOC)
2463#ifndef CONFIG_KALLSYMS_ALL
2464 || !(sec->sh_flags & SHF_EXECINSTR)
2465#endif
2466 || (sec->sh_entsize & INIT_OFFSET_MASK))
2467 return false;
2468
2469 return true;
2470}
2471
Kevin Cernekee48fd1182012-01-13 09:32:14 +10302472/*
2473 * We only allocate and copy the strings needed by the parts of symtab
2474 * we keep. This is simple, but has the effect of making multiple
2475 * copies of duplicates. We could be more sophisticated, see
2476 * linux-kernel thread starting with
2477 * <73defb5e4bca04a6431392cc341112b1@localhost>.
2478 */
Rusty Russell49668682010-08-05 12:59:10 -06002479static void layout_symtab(struct module *mod, struct load_info *info)
Jan Beulich4a496222009-07-06 14:50:42 +01002480{
Rusty Russell49668682010-08-05 12:59:10 -06002481 Elf_Shdr *symsect = info->sechdrs + info->index.sym;
2482 Elf_Shdr *strsect = info->sechdrs + info->index.str;
Jan Beulich4a496222009-07-06 14:50:42 +01002483 const Elf_Sym *src;
Satoru Takeuchi54523ec2012-12-05 12:29:04 +10302484 unsigned int i, nsrc, ndst, strtab_size = 0;
Jan Beulich4a496222009-07-06 14:50:42 +01002485
2486 /* Put symbol section at end of init part of module. */
2487 symsect->sh_flags |= SHF_ALLOC;
2488 symsect->sh_entsize = get_offset(mod, &mod->init_size, symsect,
Rusty Russell49668682010-08-05 12:59:10 -06002489 info->index.sym) | INIT_OFFSET_MASK;
Jim Cromie5e124162011-12-06 12:11:31 -07002490 pr_debug("\t%s\n", info->secstrings + symsect->sh_name);
Jan Beulich4a496222009-07-06 14:50:42 +01002491
Rusty Russell49668682010-08-05 12:59:10 -06002492 src = (void *)info->hdr + symsect->sh_offset;
Jan Beulich4a496222009-07-06 14:50:42 +01002493 nsrc = symsect->sh_size / sizeof(*src);
Kevin Cernekee70b1e9162011-11-12 19:08:55 -08002494
Kevin Cernekee48fd1182012-01-13 09:32:14 +10302495 /* Compute total space required for the core symbols' strtab. */
Rusty Russell59ef28b2012-10-25 10:49:25 +10302496 for (ndst = i = 0; i < nsrc; i++) {
2497 if (i == 0 ||
2498 is_core_symbol(src+i, info->sechdrs, info->hdr->e_shnum)) {
2499 strtab_size += strlen(&info->strtab[src[i].st_name])+1;
Kevin Cernekee48fd1182012-01-13 09:32:14 +10302500 ndst++;
Jan Beulich554bdfe2009-07-06 14:51:44 +01002501 }
Rusty Russell59ef28b2012-10-25 10:49:25 +10302502 }
Jan Beulich4a496222009-07-06 14:50:42 +01002503
2504 /* Append room for core symbols at end of core part. */
Rusty Russell49668682010-08-05 12:59:10 -06002505 info->symoffs = ALIGN(mod->core_size, symsect->sh_addralign ?: 1);
Kevin Cernekee48fd1182012-01-13 09:32:14 +10302506 info->stroffs = mod->core_size = info->symoffs + ndst * sizeof(Elf_Sym);
2507 mod->core_size += strtab_size;
Laura Abbott168e47f2015-02-25 14:14:57 -08002508 mod->core_size = debug_align(mod->core_size);
Jan Beulich4a496222009-07-06 14:50:42 +01002509
Jan Beulich554bdfe2009-07-06 14:51:44 +01002510 /* Put string table section at end of init part of module. */
2511 strsect->sh_flags |= SHF_ALLOC;
2512 strsect->sh_entsize = get_offset(mod, &mod->init_size, strsect,
Rusty Russell49668682010-08-05 12:59:10 -06002513 info->index.str) | INIT_OFFSET_MASK;
Laura Abbott168e47f2015-02-25 14:14:57 -08002514 mod->init_size = debug_align(mod->init_size);
Jim Cromie5e124162011-12-06 12:11:31 -07002515 pr_debug("\t%s\n", info->secstrings + strsect->sh_name);
Jan Beulich4a496222009-07-06 14:50:42 +01002516}
2517
Rusty Russell811d66a2010-08-05 12:59:12 -06002518static void add_kallsyms(struct module *mod, const struct load_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002519{
Jan Beulich4a496222009-07-06 14:50:42 +01002520 unsigned int i, ndst;
2521 const Elf_Sym *src;
2522 Elf_Sym *dst;
Jan Beulich554bdfe2009-07-06 14:51:44 +01002523 char *s;
Rusty Russelleded41c2010-08-05 12:59:07 -06002524 Elf_Shdr *symsec = &info->sechdrs[info->index.sym];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002525
Rusty Russelleded41c2010-08-05 12:59:07 -06002526 mod->symtab = (void *)symsec->sh_addr;
2527 mod->num_symtab = symsec->sh_size / sizeof(Elf_Sym);
Rusty Russell511ca6a2010-08-05 12:59:08 -06002528 /* Make sure we get permanent strtab: don't use info->strtab. */
2529 mod->strtab = (void *)info->sechdrs[info->index.str].sh_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002530
2531 /* Set types up while we still have access to sections. */
2532 for (i = 0; i < mod->num_symtab; i++)
Rusty Russelleded41c2010-08-05 12:59:07 -06002533 mod->symtab[i].st_info = elf_type(&mod->symtab[i], info);
Jan Beulich4a496222009-07-06 14:50:42 +01002534
Rusty Russelld9131882010-08-05 12:59:08 -06002535 mod->core_symtab = dst = mod->module_core + info->symoffs;
Kevin Cernekee48fd1182012-01-13 09:32:14 +10302536 mod->core_strtab = s = mod->module_core + info->stroffs;
Jan Beulich4a496222009-07-06 14:50:42 +01002537 src = mod->symtab;
Rusty Russell59ef28b2012-10-25 10:49:25 +10302538 for (ndst = i = 0; i < mod->num_symtab; i++) {
2539 if (i == 0 ||
2540 is_core_symbol(src+i, info->sechdrs, info->hdr->e_shnum)) {
2541 dst[ndst] = src[i];
2542 dst[ndst++].st_name = s - mod->core_strtab;
2543 s += strlcpy(s, &mod->strtab[src[i].st_name],
2544 KSYM_NAME_LEN) + 1;
2545 }
Jan Beulich4a496222009-07-06 14:50:42 +01002546 }
2547 mod->core_num_syms = ndst;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002548}
2549#else
Rusty Russell49668682010-08-05 12:59:10 -06002550static inline void layout_symtab(struct module *mod, struct load_info *info)
Jan Beulich4a496222009-07-06 14:50:42 +01002551{
2552}
Paul Mundt3ae91c22009-10-01 15:43:54 -07002553
Michał Mirosławabbce902010-09-20 01:58:08 +02002554static void add_kallsyms(struct module *mod, const struct load_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002555{
2556}
2557#endif /* CONFIG_KALLSYMS */
2558
Jason Barone9d376f2009-02-05 11:51:38 -05002559static void dynamic_debug_setup(struct _ddebug *debug, unsigned int num)
Rusty Russell5e458cc2008-10-22 10:00:13 -05002560{
Rusty Russell811d66a2010-08-05 12:59:12 -06002561 if (!debug)
2562 return;
Jason Barone9d376f2009-02-05 11:51:38 -05002563#ifdef CONFIG_DYNAMIC_DEBUG
2564 if (ddebug_add_module(debug, num, debug->modname))
Andrew Mortonbddb12b2013-11-12 15:11:28 -08002565 pr_err("dynamic debug error adding module: %s\n",
2566 debug->modname);
Jason Barone9d376f2009-02-05 11:51:38 -05002567#endif
Rusty Russell5e458cc2008-10-22 10:00:13 -05002568}
Jason Baron346e15b2008-08-12 16:46:19 -04002569
Yehuda Sadehff49d742010-07-03 13:07:35 +10002570static void dynamic_debug_remove(struct _ddebug *debug)
2571{
2572 if (debug)
2573 ddebug_remove_module(debug->modname);
2574}
2575
Jonas Bonn74e08fc2011-06-30 21:22:11 +02002576void * __weak module_alloc(unsigned long size)
2577{
Rusty Russell82fab442012-12-11 09:38:33 +10302578 return vmalloc_exec(size);
Jonas Bonn74e08fc2011-06-30 21:22:11 +02002579}
2580
Catalin Marinas4f2294b2009-06-11 13:23:20 +01002581#ifdef CONFIG_DEBUG_KMEMLEAK
Rusty Russell49668682010-08-05 12:59:10 -06002582static void kmemleak_load_module(const struct module *mod,
2583 const struct load_info *info)
Catalin Marinas4f2294b2009-06-11 13:23:20 +01002584{
2585 unsigned int i;
2586
2587 /* only scan the sections containing data */
Catalin Marinasc017b4b2009-10-28 13:33:09 +00002588 kmemleak_scan_area(mod, sizeof(struct module), GFP_KERNEL);
Catalin Marinas4f2294b2009-06-11 13:23:20 +01002589
Rusty Russell49668682010-08-05 12:59:10 -06002590 for (i = 1; i < info->hdr->e_shnum; i++) {
Steven Rostedt06c94942013-05-15 20:33:01 +01002591 /* Scan all writable sections that's not executable */
2592 if (!(info->sechdrs[i].sh_flags & SHF_ALLOC) ||
2593 !(info->sechdrs[i].sh_flags & SHF_WRITE) ||
2594 (info->sechdrs[i].sh_flags & SHF_EXECINSTR))
Catalin Marinas4f2294b2009-06-11 13:23:20 +01002595 continue;
2596
Rusty Russell49668682010-08-05 12:59:10 -06002597 kmemleak_scan_area((void *)info->sechdrs[i].sh_addr,
2598 info->sechdrs[i].sh_size, GFP_KERNEL);
Catalin Marinas4f2294b2009-06-11 13:23:20 +01002599 }
2600}
2601#else
Rusty Russell49668682010-08-05 12:59:10 -06002602static inline void kmemleak_load_module(const struct module *mod,
2603 const struct load_info *info)
Catalin Marinas4f2294b2009-06-11 13:23:20 +01002604{
2605}
2606#endif
2607
Rusty Russell106a4ee2012-09-26 10:09:40 +01002608#ifdef CONFIG_MODULE_SIG
Kees Cook34e11692012-10-16 07:31:07 +10302609static int module_sig_check(struct load_info *info)
Rusty Russell106a4ee2012-09-26 10:09:40 +01002610{
2611 int err = -ENOKEY;
Kees Cook34e11692012-10-16 07:31:07 +10302612 const unsigned long markerlen = sizeof(MODULE_SIG_STRING) - 1;
2613 const void *mod = info->hdr;
Rusty Russell106a4ee2012-09-26 10:09:40 +01002614
Kees Cook34e11692012-10-16 07:31:07 +10302615 if (info->len > markerlen &&
2616 memcmp(mod + info->len - markerlen, MODULE_SIG_STRING, markerlen) == 0) {
David Howellscaabe242012-10-20 01:19:29 +01002617 /* We truncate the module to discard the signature */
Kees Cook34e11692012-10-16 07:31:07 +10302618 info->len -= markerlen;
2619 err = mod_verify_sig(mod, &info->len);
Rusty Russell106a4ee2012-09-26 10:09:40 +01002620 }
2621
2622 if (!err) {
2623 info->sig_ok = true;
2624 return 0;
2625 }
2626
2627 /* Not having a signature is only an error if we're strict. */
2628 if (err == -ENOKEY && !sig_enforce)
2629 err = 0;
2630
2631 return err;
2632}
2633#else /* !CONFIG_MODULE_SIG */
Kees Cook34e11692012-10-16 07:31:07 +10302634static int module_sig_check(struct load_info *info)
Rusty Russell106a4ee2012-09-26 10:09:40 +01002635{
2636 return 0;
2637}
2638#endif /* !CONFIG_MODULE_SIG */
2639
Kees Cook34e11692012-10-16 07:31:07 +10302640/* Sanity checks against invalid binaries, wrong arch, weird elf version. */
2641static int elf_header_check(struct load_info *info)
Rusty Russell40dd2562010-08-05 12:59:03 -06002642{
Kees Cook34e11692012-10-16 07:31:07 +10302643 if (info->len < sizeof(*(info->hdr)))
Rusty Russell40dd2562010-08-05 12:59:03 -06002644 return -ENOEXEC;
2645
Kees Cook34e11692012-10-16 07:31:07 +10302646 if (memcmp(info->hdr->e_ident, ELFMAG, SELFMAG) != 0
2647 || info->hdr->e_type != ET_REL
2648 || !elf_check_arch(info->hdr)
2649 || info->hdr->e_shentsize != sizeof(Elf_Shdr))
2650 return -ENOEXEC;
2651
2652 if (info->hdr->e_shoff >= info->len
2653 || (info->hdr->e_shnum * sizeof(Elf_Shdr) >
2654 info->len - info->hdr->e_shoff))
2655 return -ENOEXEC;
2656
2657 return 0;
2658}
2659
Linus Torvalds3afe9f82015-04-07 10:33:49 -07002660#define COPY_CHUNK_SIZE (16*PAGE_SIZE)
2661
2662static int copy_chunked_from_user(void *dst, const void __user *usrc, unsigned long len)
2663{
2664 do {
2665 unsigned long n = min(len, COPY_CHUNK_SIZE);
2666
2667 if (copy_from_user(dst, usrc, n) != 0)
2668 return -EFAULT;
2669 cond_resched();
2670 dst += n;
2671 usrc += n;
2672 len -= n;
2673 } while (len);
2674 return 0;
2675}
2676
Kees Cook34e11692012-10-16 07:31:07 +10302677/* Sets info->hdr and info->len. */
2678static int copy_module_from_user(const void __user *umod, unsigned long len,
2679 struct load_info *info)
2680{
Kees Cook2e72d512012-10-16 07:32:07 +10302681 int err;
2682
Kees Cook34e11692012-10-16 07:31:07 +10302683 info->len = len;
2684 if (info->len < sizeof(*(info->hdr)))
Rusty Russell40dd2562010-08-05 12:59:03 -06002685 return -ENOEXEC;
2686
Kees Cook2e72d512012-10-16 07:32:07 +10302687 err = security_kernel_module_from_file(NULL);
2688 if (err)
2689 return err;
2690
Rusty Russell40dd2562010-08-05 12:59:03 -06002691 /* Suck in entire file: we'll want most of it. */
Kirill A. Shutemovcc9e6052015-03-24 12:31:40 +10302692 info->hdr = __vmalloc(info->len,
2693 GFP_KERNEL | __GFP_HIGHMEM | __GFP_NOWARN, PAGE_KERNEL);
Kees Cook34e11692012-10-16 07:31:07 +10302694 if (!info->hdr)
Rusty Russell40dd2562010-08-05 12:59:03 -06002695 return -ENOMEM;
2696
Linus Torvalds3afe9f82015-04-07 10:33:49 -07002697 if (copy_chunked_from_user(info->hdr, umod, info->len) != 0) {
Kees Cook34e11692012-10-16 07:31:07 +10302698 vfree(info->hdr);
2699 return -EFAULT;
Rusty Russell40dd2562010-08-05 12:59:03 -06002700 }
2701
Rusty Russell40dd2562010-08-05 12:59:03 -06002702 return 0;
Kees Cook34e11692012-10-16 07:31:07 +10302703}
Rusty Russell40dd2562010-08-05 12:59:03 -06002704
Kees Cook34e11692012-10-16 07:31:07 +10302705/* Sets info->hdr and info->len. */
2706static int copy_module_from_fd(int fd, struct load_info *info)
2707{
Al Viroa2e05782013-08-30 12:41:41 -04002708 struct fd f = fdget(fd);
Kees Cook34e11692012-10-16 07:31:07 +10302709 int err;
2710 struct kstat stat;
2711 loff_t pos;
2712 ssize_t bytes = 0;
2713
Al Viroa2e05782013-08-30 12:41:41 -04002714 if (!f.file)
Kees Cook34e11692012-10-16 07:31:07 +10302715 return -ENOEXEC;
2716
Al Viroa2e05782013-08-30 12:41:41 -04002717 err = security_kernel_module_from_file(f.file);
Kees Cook2e72d512012-10-16 07:32:07 +10302718 if (err)
2719 goto out;
2720
Al Viroa2e05782013-08-30 12:41:41 -04002721 err = vfs_getattr(&f.file->f_path, &stat);
Kees Cook34e11692012-10-16 07:31:07 +10302722 if (err)
2723 goto out;
2724
2725 if (stat.size > INT_MAX) {
2726 err = -EFBIG;
2727 goto out;
2728 }
Sasha Levin52441fa2013-01-03 11:09:53 +10302729
2730 /* Don't hand 0 to vmalloc, it whines. */
2731 if (stat.size == 0) {
2732 err = -EINVAL;
2733 goto out;
2734 }
2735
Kees Cook34e11692012-10-16 07:31:07 +10302736 info->hdr = vmalloc(stat.size);
2737 if (!info->hdr) {
2738 err = -ENOMEM;
2739 goto out;
2740 }
2741
2742 pos = 0;
2743 while (pos < stat.size) {
Al Viroa2e05782013-08-30 12:41:41 -04002744 bytes = kernel_read(f.file, pos, (char *)(info->hdr) + pos,
Kees Cook34e11692012-10-16 07:31:07 +10302745 stat.size - pos);
2746 if (bytes < 0) {
2747 vfree(info->hdr);
2748 err = bytes;
2749 goto out;
2750 }
2751 if (bytes == 0)
2752 break;
2753 pos += bytes;
2754 }
2755 info->len = pos;
2756
2757out:
Al Viroa2e05782013-08-30 12:41:41 -04002758 fdput(f);
Rusty Russell40dd2562010-08-05 12:59:03 -06002759 return err;
2760}
2761
Rusty Russelld9131882010-08-05 12:59:08 -06002762static void free_copy(struct load_info *info)
2763{
Rusty Russelld9131882010-08-05 12:59:08 -06002764 vfree(info->hdr);
2765}
2766
Rusty Russell2f3238a2012-10-22 18:09:41 +10302767static int rewrite_section_headers(struct load_info *info, int flags)
Rusty Russell8b5f61a2010-08-05 12:59:06 -06002768{
2769 unsigned int i;
2770
2771 /* This should always be true, but let's be sure. */
2772 info->sechdrs[0].sh_addr = 0;
2773
2774 for (i = 1; i < info->hdr->e_shnum; i++) {
2775 Elf_Shdr *shdr = &info->sechdrs[i];
2776 if (shdr->sh_type != SHT_NOBITS
2777 && info->len < shdr->sh_offset + shdr->sh_size) {
Andrew Mortonbddb12b2013-11-12 15:11:28 -08002778 pr_err("Module len %lu truncated\n", info->len);
Rusty Russell8b5f61a2010-08-05 12:59:06 -06002779 return -ENOEXEC;
2780 }
2781
2782 /* Mark all sections sh_addr with their address in the
2783 temporary image. */
2784 shdr->sh_addr = (size_t)info->hdr + shdr->sh_offset;
2785
2786#ifndef CONFIG_MODULE_UNLOAD
2787 /* Don't load .exit sections */
2788 if (strstarts(info->secstrings+shdr->sh_name, ".exit"))
2789 shdr->sh_flags &= ~(unsigned long)SHF_ALLOC;
2790#endif
Rusty Russell8b5f61a2010-08-05 12:59:06 -06002791 }
Rusty Russelld6df72a2010-08-05 12:59:07 -06002792
2793 /* Track but don't keep modinfo and version sections. */
Rusty Russell2f3238a2012-10-22 18:09:41 +10302794 if (flags & MODULE_INIT_IGNORE_MODVERSIONS)
2795 info->index.vers = 0; /* Pretend no __versions section! */
2796 else
2797 info->index.vers = find_sec(info, "__versions");
Rusty Russell49668682010-08-05 12:59:10 -06002798 info->index.info = find_sec(info, ".modinfo");
Rusty Russelld6df72a2010-08-05 12:59:07 -06002799 info->sechdrs[info->index.info].sh_flags &= ~(unsigned long)SHF_ALLOC;
2800 info->sechdrs[info->index.vers].sh_flags &= ~(unsigned long)SHF_ALLOC;
Rusty Russell8b5f61a2010-08-05 12:59:06 -06002801 return 0;
2802}
2803
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002804/*
2805 * Set up our basic convenience variables (pointers to section headers,
2806 * search for module section index etc), and do some basic section
2807 * verification.
2808 *
2809 * Return the temporary module pointer (we'll replace it with the final
2810 * one when we move the module sections around).
2811 */
Rusty Russell2f3238a2012-10-22 18:09:41 +10302812static struct module *setup_load_info(struct load_info *info, int flags)
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002813{
2814 unsigned int i;
Rusty Russell8b5f61a2010-08-05 12:59:06 -06002815 int err;
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002816 struct module *mod;
2817
2818 /* Set up the convenience variables */
2819 info->sechdrs = (void *)info->hdr + info->hdr->e_shoff;
Rusty Russell8b5f61a2010-08-05 12:59:06 -06002820 info->secstrings = (void *)info->hdr
2821 + info->sechdrs[info->hdr->e_shstrndx].sh_offset;
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002822
Rusty Russell2f3238a2012-10-22 18:09:41 +10302823 err = rewrite_section_headers(info, flags);
Rusty Russell8b5f61a2010-08-05 12:59:06 -06002824 if (err)
2825 return ERR_PTR(err);
2826
2827 /* Find internal symbols and strings. */
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002828 for (i = 1; i < info->hdr->e_shnum; i++) {
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002829 if (info->sechdrs[i].sh_type == SHT_SYMTAB) {
2830 info->index.sym = i;
2831 info->index.str = info->sechdrs[i].sh_link;
Rusty Russell8b5f61a2010-08-05 12:59:06 -06002832 info->strtab = (char *)info->hdr
2833 + info->sechdrs[info->index.str].sh_offset;
2834 break;
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002835 }
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002836 }
2837
Rusty Russell49668682010-08-05 12:59:10 -06002838 info->index.mod = find_sec(info, ".gnu.linkonce.this_module");
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002839 if (!info->index.mod) {
Andrew Mortonbddb12b2013-11-12 15:11:28 -08002840 pr_warn("No module found in object\n");
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002841 return ERR_PTR(-ENOEXEC);
2842 }
2843 /* This is temporary: point mod into copy of data. */
2844 mod = (void *)info->sechdrs[info->index.mod].sh_addr;
2845
2846 if (info->index.sym == 0) {
Andrew Mortonbddb12b2013-11-12 15:11:28 -08002847 pr_warn("%s: module has no symbols (stripped?)\n", mod->name);
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002848 return ERR_PTR(-ENOEXEC);
2849 }
2850
Rusty Russell49668682010-08-05 12:59:10 -06002851 info->index.pcpu = find_pcpusec(info);
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002852
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002853 /* Check module struct version now, before we try to use module. */
2854 if (!check_modstruct_version(info->sechdrs, info->index.vers, mod))
2855 return ERR_PTR(-ENOEXEC);
2856
2857 return mod;
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002858}
2859
Rusty Russell2f3238a2012-10-22 18:09:41 +10302860static int check_modinfo(struct module *mod, struct load_info *info, int flags)
Rusty Russell40dd2562010-08-05 12:59:03 -06002861{
Rusty Russell49668682010-08-05 12:59:10 -06002862 const char *modmagic = get_modinfo(info, "vermagic");
Rusty Russell40dd2562010-08-05 12:59:03 -06002863 int err;
2864
Rusty Russell2f3238a2012-10-22 18:09:41 +10302865 if (flags & MODULE_INIT_IGNORE_VERMAGIC)
2866 modmagic = NULL;
2867
Rusty Russell40dd2562010-08-05 12:59:03 -06002868 /* This is allowed: modprobe --force will invalidate it. */
2869 if (!modmagic) {
2870 err = try_to_force_load(mod, "bad vermagic");
2871 if (err)
2872 return err;
Rusty Russell49668682010-08-05 12:59:10 -06002873 } else if (!same_magic(modmagic, vermagic, info->index.vers)) {
Andrew Mortonbddb12b2013-11-12 15:11:28 -08002874 pr_err("%s: version magic '%s' should be '%s'\n",
Rusty Russell40dd2562010-08-05 12:59:03 -06002875 mod->name, modmagic, vermagic);
2876 return -ENOEXEC;
2877 }
2878
Ben Hutchings2449b8b2011-10-24 15:12:28 +02002879 if (!get_modinfo(info, "intree"))
Rusty Russell373d4d02013-01-21 17:17:39 +10302880 add_taint_module(mod, TAINT_OOT_MODULE, LOCKDEP_STILL_OK);
Ben Hutchings2449b8b2011-10-24 15:12:28 +02002881
Rusty Russell49668682010-08-05 12:59:10 -06002882 if (get_modinfo(info, "staging")) {
Rusty Russell373d4d02013-01-21 17:17:39 +10302883 add_taint_module(mod, TAINT_CRAP, LOCKDEP_STILL_OK);
Andrew Mortonbddb12b2013-11-12 15:11:28 -08002884 pr_warn("%s: module is from the staging directory, the quality "
2885 "is unknown, you have been warned.\n", mod->name);
Rusty Russell40dd2562010-08-05 12:59:03 -06002886 }
Rusty Russell22e268e2010-08-05 12:59:05 -06002887
2888 /* Set up license info based on the info section */
Rusty Russell49668682010-08-05 12:59:10 -06002889 set_license(mod, get_modinfo(info, "license"));
Rusty Russell22e268e2010-08-05 12:59:05 -06002890
Rusty Russell40dd2562010-08-05 12:59:03 -06002891 return 0;
2892}
2893
Frantisek Hrbataeb3057d2013-10-14 18:08:46 +10302894static int find_module_sections(struct module *mod, struct load_info *info)
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06002895{
Rusty Russell49668682010-08-05 12:59:10 -06002896 mod->kp = section_objs(info, "__param",
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06002897 sizeof(*mod->kp), &mod->num_kp);
Rusty Russell49668682010-08-05 12:59:10 -06002898 mod->syms = section_objs(info, "__ksymtab",
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06002899 sizeof(*mod->syms), &mod->num_syms);
Rusty Russell49668682010-08-05 12:59:10 -06002900 mod->crcs = section_addr(info, "__kcrctab");
2901 mod->gpl_syms = section_objs(info, "__ksymtab_gpl",
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06002902 sizeof(*mod->gpl_syms),
2903 &mod->num_gpl_syms);
Rusty Russell49668682010-08-05 12:59:10 -06002904 mod->gpl_crcs = section_addr(info, "__kcrctab_gpl");
2905 mod->gpl_future_syms = section_objs(info,
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06002906 "__ksymtab_gpl_future",
2907 sizeof(*mod->gpl_future_syms),
2908 &mod->num_gpl_future_syms);
Rusty Russell49668682010-08-05 12:59:10 -06002909 mod->gpl_future_crcs = section_addr(info, "__kcrctab_gpl_future");
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06002910
2911#ifdef CONFIG_UNUSED_SYMBOLS
Rusty Russell49668682010-08-05 12:59:10 -06002912 mod->unused_syms = section_objs(info, "__ksymtab_unused",
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06002913 sizeof(*mod->unused_syms),
2914 &mod->num_unused_syms);
Rusty Russell49668682010-08-05 12:59:10 -06002915 mod->unused_crcs = section_addr(info, "__kcrctab_unused");
2916 mod->unused_gpl_syms = section_objs(info, "__ksymtab_unused_gpl",
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06002917 sizeof(*mod->unused_gpl_syms),
2918 &mod->num_unused_gpl_syms);
Rusty Russell49668682010-08-05 12:59:10 -06002919 mod->unused_gpl_crcs = section_addr(info, "__kcrctab_unused_gpl");
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06002920#endif
2921#ifdef CONFIG_CONSTRUCTORS
Rusty Russell49668682010-08-05 12:59:10 -06002922 mod->ctors = section_objs(info, ".ctors",
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06002923 sizeof(*mod->ctors), &mod->num_ctors);
Frantisek Hrbataeb3057d2013-10-14 18:08:46 +10302924 if (!mod->ctors)
2925 mod->ctors = section_objs(info, ".init_array",
2926 sizeof(*mod->ctors), &mod->num_ctors);
2927 else if (find_sec(info, ".init_array")) {
2928 /*
2929 * This shouldn't happen with same compiler and binutils
2930 * building all parts of the module.
2931 */
Ionut Alexa6da0b562014-11-10 09:31:29 +10302932 pr_warn("%s: has both .ctors and .init_array.\n",
Frantisek Hrbataeb3057d2013-10-14 18:08:46 +10302933 mod->name);
2934 return -EINVAL;
2935 }
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06002936#endif
2937
2938#ifdef CONFIG_TRACEPOINTS
Mathieu Desnoyers65498642011-01-26 17:26:22 -05002939 mod->tracepoints_ptrs = section_objs(info, "__tracepoints_ptrs",
2940 sizeof(*mod->tracepoints_ptrs),
2941 &mod->num_tracepoints);
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06002942#endif
Jason Baronbf5438fc2010-09-17 11:09:00 -04002943#ifdef HAVE_JUMP_LABEL
2944 mod->jump_entries = section_objs(info, "__jump_table",
2945 sizeof(*mod->jump_entries),
2946 &mod->num_jump_entries);
2947#endif
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06002948#ifdef CONFIG_EVENT_TRACING
Rusty Russell49668682010-08-05 12:59:10 -06002949 mod->trace_events = section_objs(info, "_ftrace_events",
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06002950 sizeof(*mod->trace_events),
2951 &mod->num_trace_events);
Steven Rostedt (Red Hat)3673b8e2015-03-25 15:44:21 -04002952 mod->trace_enums = section_objs(info, "_ftrace_enum_map",
2953 sizeof(*mod->trace_enums),
2954 &mod->num_trace_enums);
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06002955#endif
Steven Rostedt13b9b6e2010-11-10 22:19:24 -05002956#ifdef CONFIG_TRACING
2957 mod->trace_bprintk_fmt_start = section_objs(info, "__trace_printk_fmt",
2958 sizeof(*mod->trace_bprintk_fmt_start),
2959 &mod->num_trace_bprintk_fmt);
Steven Rostedt13b9b6e2010-11-10 22:19:24 -05002960#endif
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06002961#ifdef CONFIG_FTRACE_MCOUNT_RECORD
2962 /* sechdrs[0].sh_size is always zero */
Rusty Russell49668682010-08-05 12:59:10 -06002963 mod->ftrace_callsites = section_objs(info, "__mcount_loc",
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06002964 sizeof(*mod->ftrace_callsites),
2965 &mod->num_ftrace_callsites);
2966#endif
Rusty Russell22e268e2010-08-05 12:59:05 -06002967
Rusty Russell811d66a2010-08-05 12:59:12 -06002968 mod->extable = section_objs(info, "__ex_table",
2969 sizeof(*mod->extable), &mod->num_exentries);
2970
Rusty Russell49668682010-08-05 12:59:10 -06002971 if (section_addr(info, "__obsparm"))
Andrew Mortonbddb12b2013-11-12 15:11:28 -08002972 pr_warn("%s: Ignoring obsolete parameters\n", mod->name);
Rusty Russell811d66a2010-08-05 12:59:12 -06002973
2974 info->debug = section_objs(info, "__verbose",
2975 sizeof(*info->debug), &info->num_debug);
Frantisek Hrbataeb3057d2013-10-14 18:08:46 +10302976
2977 return 0;
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06002978}
2979
Rusty Russell49668682010-08-05 12:59:10 -06002980static int move_module(struct module *mod, struct load_info *info)
Linus Torvalds65b8a9b2010-08-05 12:59:02 -06002981{
2982 int i;
2983 void *ptr;
2984
2985 /* Do the allocs. */
Peter Zijlstra4f6665462015-05-27 11:09:38 +09302986 ptr = module_alloc(mod->core_size);
Linus Torvalds65b8a9b2010-08-05 12:59:02 -06002987 /*
2988 * The pointer to this block is stored in the module structure
2989 * which is inside the block. Just mark it as not being a
2990 * leak.
2991 */
2992 kmemleak_not_leak(ptr);
2993 if (!ptr)
Rusty Russelld9131882010-08-05 12:59:08 -06002994 return -ENOMEM;
Linus Torvalds65b8a9b2010-08-05 12:59:02 -06002995
2996 memset(ptr, 0, mod->core_size);
2997 mod->module_core = ptr;
2998
Rusty Russell82fab442012-12-11 09:38:33 +10302999 if (mod->init_size) {
Peter Zijlstra4f6665462015-05-27 11:09:38 +09303000 ptr = module_alloc(mod->init_size);
Rusty Russell82fab442012-12-11 09:38:33 +10303001 /*
3002 * The pointer to this block is stored in the module structure
3003 * which is inside the block. This block doesn't need to be
3004 * scanned as it contains data and code that will be freed
3005 * after the module is initialized.
3006 */
3007 kmemleak_ignore(ptr);
3008 if (!ptr) {
Rusty Russellbe1f2212015-01-20 09:07:05 +10303009 module_memfree(mod->module_core);
Rusty Russell82fab442012-12-11 09:38:33 +10303010 return -ENOMEM;
3011 }
3012 memset(ptr, 0, mod->init_size);
3013 mod->module_init = ptr;
3014 } else
3015 mod->module_init = NULL;
Linus Torvalds65b8a9b2010-08-05 12:59:02 -06003016
3017 /* Transfer each section which specifies SHF_ALLOC */
Jim Cromie5e124162011-12-06 12:11:31 -07003018 pr_debug("final section addresses:\n");
Rusty Russell49668682010-08-05 12:59:10 -06003019 for (i = 0; i < info->hdr->e_shnum; i++) {
Linus Torvalds65b8a9b2010-08-05 12:59:02 -06003020 void *dest;
Rusty Russell49668682010-08-05 12:59:10 -06003021 Elf_Shdr *shdr = &info->sechdrs[i];
Linus Torvalds65b8a9b2010-08-05 12:59:02 -06003022
Rusty Russell49668682010-08-05 12:59:10 -06003023 if (!(shdr->sh_flags & SHF_ALLOC))
Linus Torvalds65b8a9b2010-08-05 12:59:02 -06003024 continue;
3025
Rusty Russell49668682010-08-05 12:59:10 -06003026 if (shdr->sh_entsize & INIT_OFFSET_MASK)
Linus Torvalds65b8a9b2010-08-05 12:59:02 -06003027 dest = mod->module_init
Rusty Russell49668682010-08-05 12:59:10 -06003028 + (shdr->sh_entsize & ~INIT_OFFSET_MASK);
Linus Torvalds65b8a9b2010-08-05 12:59:02 -06003029 else
Rusty Russell49668682010-08-05 12:59:10 -06003030 dest = mod->module_core + shdr->sh_entsize;
Linus Torvalds65b8a9b2010-08-05 12:59:02 -06003031
Rusty Russell49668682010-08-05 12:59:10 -06003032 if (shdr->sh_type != SHT_NOBITS)
3033 memcpy(dest, (void *)shdr->sh_addr, shdr->sh_size);
Linus Torvalds65b8a9b2010-08-05 12:59:02 -06003034 /* Update sh_addr to point to copy in image. */
Rusty Russell49668682010-08-05 12:59:10 -06003035 shdr->sh_addr = (unsigned long)dest;
Jim Cromie5e124162011-12-06 12:11:31 -07003036 pr_debug("\t0x%lx %s\n",
3037 (long)shdr->sh_addr, info->secstrings + shdr->sh_name);
Linus Torvalds65b8a9b2010-08-05 12:59:02 -06003038 }
Rusty Russelld9131882010-08-05 12:59:08 -06003039
3040 return 0;
Linus Torvalds65b8a9b2010-08-05 12:59:02 -06003041}
3042
Rusty Russell49668682010-08-05 12:59:10 -06003043static int check_module_license_and_versions(struct module *mod)
Rusty Russell22e268e2010-08-05 12:59:05 -06003044{
3045 /*
3046 * ndiswrapper is under GPL by itself, but loads proprietary modules.
3047 * Don't use add_taint_module(), as it would prevent ndiswrapper from
3048 * using GPL-only symbols it needs.
3049 */
3050 if (strcmp(mod->name, "ndiswrapper") == 0)
Rusty Russell373d4d02013-01-21 17:17:39 +10303051 add_taint(TAINT_PROPRIETARY_MODULE, LOCKDEP_NOW_UNRELIABLE);
Rusty Russell22e268e2010-08-05 12:59:05 -06003052
3053 /* driverloader was caught wrongly pretending to be under GPL */
3054 if (strcmp(mod->name, "driverloader") == 0)
Rusty Russell373d4d02013-01-21 17:17:39 +10303055 add_taint_module(mod, TAINT_PROPRIETARY_MODULE,
3056 LOCKDEP_NOW_UNRELIABLE);
Rusty Russell22e268e2010-08-05 12:59:05 -06003057
Matthew Garrettc99af372012-06-22 13:49:31 -04003058 /* lve claims to be GPL but upstream won't provide source */
3059 if (strcmp(mod->name, "lve") == 0)
Rusty Russell373d4d02013-01-21 17:17:39 +10303060 add_taint_module(mod, TAINT_PROPRIETARY_MODULE,
3061 LOCKDEP_NOW_UNRELIABLE);
Matthew Garrettc99af372012-06-22 13:49:31 -04003062
Rusty Russell22e268e2010-08-05 12:59:05 -06003063#ifdef CONFIG_MODVERSIONS
3064 if ((mod->num_syms && !mod->crcs)
3065 || (mod->num_gpl_syms && !mod->gpl_crcs)
3066 || (mod->num_gpl_future_syms && !mod->gpl_future_crcs)
3067#ifdef CONFIG_UNUSED_SYMBOLS
3068 || (mod->num_unused_syms && !mod->unused_crcs)
3069 || (mod->num_unused_gpl_syms && !mod->unused_gpl_crcs)
3070#endif
3071 ) {
3072 return try_to_force_load(mod,
3073 "no versions for exported symbols");
3074 }
3075#endif
3076 return 0;
3077}
3078
3079static void flush_module_icache(const struct module *mod)
3080{
3081 mm_segment_t old_fs;
3082
3083 /* flush the icache in correct context */
3084 old_fs = get_fs();
3085 set_fs(KERNEL_DS);
3086
3087 /*
3088 * Flush the instruction cache, since we've played with text.
3089 * Do it before processing of module parameters, so the module
3090 * can provide parameter accessor functions of its own.
3091 */
3092 if (mod->module_init)
3093 flush_icache_range((unsigned long)mod->module_init,
3094 (unsigned long)mod->module_init
3095 + mod->init_size);
3096 flush_icache_range((unsigned long)mod->module_core,
3097 (unsigned long)mod->module_core + mod->core_size);
3098
3099 set_fs(old_fs);
3100}
3101
Jonas Bonn74e08fc2011-06-30 21:22:11 +02003102int __weak module_frob_arch_sections(Elf_Ehdr *hdr,
3103 Elf_Shdr *sechdrs,
3104 char *secstrings,
3105 struct module *mod)
3106{
3107 return 0;
3108}
3109
Rusty Russell2f3238a2012-10-22 18:09:41 +10303110static struct module *layout_and_allocate(struct load_info *info, int flags)
Rusty Russelld9131882010-08-05 12:59:08 -06003111{
3112 /* Module within temporary copy. */
3113 struct module *mod;
3114 int err;
3115
Rusty Russell2f3238a2012-10-22 18:09:41 +10303116 mod = setup_load_info(info, flags);
Rusty Russelld9131882010-08-05 12:59:08 -06003117 if (IS_ERR(mod))
3118 return mod;
3119
Rusty Russell2f3238a2012-10-22 18:09:41 +10303120 err = check_modinfo(mod, info, flags);
Rusty Russelld9131882010-08-05 12:59:08 -06003121 if (err)
3122 return ERR_PTR(err);
3123
3124 /* Allow arches to frob section contents and sizes. */
Rusty Russell49668682010-08-05 12:59:10 -06003125 err = module_frob_arch_sections(info->hdr, info->sechdrs,
3126 info->secstrings, mod);
Rusty Russelld9131882010-08-05 12:59:08 -06003127 if (err < 0)
Rusty Russell8d8022e2013-07-03 10:06:28 +09303128 return ERR_PTR(err);
Rusty Russelld9131882010-08-05 12:59:08 -06003129
Rusty Russell8d8022e2013-07-03 10:06:28 +09303130 /* We will do a special allocation for per-cpu sections later. */
3131 info->sechdrs[info->index.pcpu].sh_flags &= ~(unsigned long)SHF_ALLOC;
Rusty Russelld9131882010-08-05 12:59:08 -06003132
3133 /* Determine total sizes, and put offsets in sh_entsize. For now
3134 this is done generically; there doesn't appear to be any
3135 special cases for the architectures. */
Rusty Russell49668682010-08-05 12:59:10 -06003136 layout_sections(mod, info);
Rusty Russell49668682010-08-05 12:59:10 -06003137 layout_symtab(mod, info);
Rusty Russelld9131882010-08-05 12:59:08 -06003138
3139 /* Allocate and move to the final place */
Rusty Russell49668682010-08-05 12:59:10 -06003140 err = move_module(mod, info);
Rusty Russelld9131882010-08-05 12:59:08 -06003141 if (err)
Rusty Russell8d8022e2013-07-03 10:06:28 +09303142 return ERR_PTR(err);
Rusty Russelld9131882010-08-05 12:59:08 -06003143
3144 /* Module has been copied to its final place now: return it. */
3145 mod = (void *)info->sechdrs[info->index.mod].sh_addr;
Rusty Russell49668682010-08-05 12:59:10 -06003146 kmemleak_load_module(mod, info);
Rusty Russelld9131882010-08-05 12:59:08 -06003147 return mod;
Rusty Russelld9131882010-08-05 12:59:08 -06003148}
3149
3150/* mod is no longer valid after this! */
3151static void module_deallocate(struct module *mod, struct load_info *info)
3152{
Rusty Russelld9131882010-08-05 12:59:08 -06003153 percpu_modfree(mod);
Rusty Russelld453cde2015-01-20 09:07:04 +10303154 module_arch_freeing_init(mod);
Rusty Russellbe1f2212015-01-20 09:07:05 +10303155 module_memfree(mod->module_init);
3156 module_memfree(mod->module_core);
Rusty Russelld9131882010-08-05 12:59:08 -06003157}
3158
Jonas Bonn74e08fc2011-06-30 21:22:11 +02003159int __weak module_finalize(const Elf_Ehdr *hdr,
3160 const Elf_Shdr *sechdrs,
3161 struct module *me)
3162{
3163 return 0;
3164}
3165
Rusty Russell811d66a2010-08-05 12:59:12 -06003166static int post_relocation(struct module *mod, const struct load_info *info)
3167{
Rusty Russell51f3d0f2010-08-05 12:59:13 -06003168 /* Sort exception table now relocations are done. */
Rusty Russell811d66a2010-08-05 12:59:12 -06003169 sort_extable(mod->extable, mod->extable + mod->num_exentries);
3170
3171 /* Copy relocated percpu area over. */
3172 percpu_modcopy(mod, (void *)info->sechdrs[info->index.pcpu].sh_addr,
3173 info->sechdrs[info->index.pcpu].sh_size);
3174
Rusty Russell51f3d0f2010-08-05 12:59:13 -06003175 /* Setup kallsyms-specific fields. */
Rusty Russell811d66a2010-08-05 12:59:12 -06003176 add_kallsyms(mod, info);
3177
3178 /* Arch-specific module finalizing. */
3179 return module_finalize(info->hdr, info->sechdrs, mod);
3180}
3181
Rusty Russell9bb9c3b2012-09-28 14:31:03 +09303182/* Is this module of this name done loading? No locks held. */
3183static bool finished_loading(const char *name)
3184{
3185 struct module *mod;
3186 bool ret;
3187
Peter Zijlstra9cc019b2015-02-11 15:01:13 +10303188 /*
3189 * The module_mutex should not be a heavily contended lock;
3190 * if we get the occasional sleep here, we'll go an extra iteration
3191 * in the wait_event_interruptible(), which is harmless.
3192 */
3193 sched_annotate_sleep();
Rusty Russell9bb9c3b2012-09-28 14:31:03 +09303194 mutex_lock(&module_mutex);
Mathias Krause4f6de4d2013-07-02 15:35:11 +09303195 mod = find_module_all(name, strlen(name), true);
Rusty Russell0d21b0e2013-01-12 11:38:44 +10303196 ret = !mod || mod->state == MODULE_STATE_LIVE
3197 || mod->state == MODULE_STATE_GOING;
Rusty Russell9bb9c3b2012-09-28 14:31:03 +09303198 mutex_unlock(&module_mutex);
3199
3200 return ret;
3201}
3202
Peter Oberparleiterb99b87f2009-06-17 16:28:03 -07003203/* Call module constructors. */
3204static void do_mod_ctors(struct module *mod)
3205{
3206#ifdef CONFIG_CONSTRUCTORS
3207 unsigned long i;
3208
3209 for (i = 0; i < mod->num_ctors; i++)
3210 mod->ctors[i]();
3211#endif
3212}
3213
Rusty Russellc7496372015-01-20 09:07:05 +10303214/* For freeing module_init on success, in case kallsyms traversing */
3215struct mod_initfree {
3216 struct rcu_head rcu;
3217 void *module_init;
3218};
3219
3220static void do_free_init(struct rcu_head *head)
3221{
3222 struct mod_initfree *m = container_of(head, struct mod_initfree, rcu);
3223 module_memfree(m->module_init);
3224 kfree(m);
3225}
3226
Jan Kiszkabe02a182015-02-17 13:46:50 -08003227/*
3228 * This is where the real work happens.
3229 *
3230 * Keep it uninlined to provide a reliable breakpoint target, e.g. for the gdb
3231 * helper command 'lx-symbols'.
3232 */
3233static noinline int do_init_module(struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003234{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003235 int ret = 0;
Rusty Russellc7496372015-01-20 09:07:05 +10303236 struct mod_initfree *freeinit;
3237
3238 freeinit = kmalloc(sizeof(*freeinit), GFP_KERNEL);
3239 if (!freeinit) {
3240 ret = -ENOMEM;
3241 goto fail;
3242 }
3243 freeinit->module_init = mod->module_init;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003244
Tejun Heo774a1222013-01-15 18:52:51 -08003245 /*
3246 * We want to find out whether @mod uses async during init. Clear
3247 * PF_USED_ASYNC. async_schedule*() will set it.
3248 */
3249 current->flags &= ~PF_USED_ASYNC;
3250
Peter Oberparleiterb99b87f2009-06-17 16:28:03 -07003251 do_mod_ctors(mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003252 /* Start the module */
3253 if (mod->init != NULL)
Arjan van de Ven59f94152008-07-30 12:49:02 -07003254 ret = do_one_initcall(mod->init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003255 if (ret < 0) {
Rusty Russellc7496372015-01-20 09:07:05 +10303256 goto fail_free_freeinit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003257 }
Alexey Dobriyane24e2e62008-03-10 11:43:53 -07003258 if (ret > 0) {
Andrew Mortonbddb12b2013-11-12 15:11:28 -08003259 pr_warn("%s: '%s'->init suspiciously returned %d, it should "
3260 "follow 0/-E convention\n"
3261 "%s: loading module anyway...\n",
3262 __func__, mod->name, ret, __func__);
Alexey Dobriyane24e2e62008-03-10 11:43:53 -07003263 dump_stack();
3264 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003265
Rusty Russell6f139092012-09-28 14:31:03 +09303266 /* Now it's a first class citizen! */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003267 mod->state = MODULE_STATE_LIVE;
Masami Hiramatsu0deddf432009-01-06 14:41:54 -08003268 blocking_notifier_call_chain(&module_notify_list,
3269 MODULE_STATE_LIVE, mod);
Rusty Russell6c5db222008-03-10 11:43:52 -07003270
Tejun Heo774a1222013-01-15 18:52:51 -08003271 /*
3272 * We need to finish all async code before the module init sequence
3273 * is done. This has potential to deadlock. For example, a newly
3274 * detected block device can trigger request_module() of the
3275 * default iosched from async probing task. Once userland helper
3276 * reaches here, async_synchronize_full() will wait on the async
3277 * task waiting on request_module() and deadlock.
3278 *
3279 * This deadlock is avoided by perfomring async_synchronize_full()
3280 * iff module init queued any async jobs. This isn't a full
3281 * solution as it will deadlock the same if module loading from
3282 * async jobs nests more than once; however, due to the various
3283 * constraints, this hack seems to be the best option for now.
3284 * Please refer to the following thread for details.
3285 *
3286 * http://thread.gmane.org/gmane.linux.kernel/1420814
3287 */
3288 if (current->flags & PF_USED_ASYNC)
3289 async_synchronize_full();
Linus Torvaldsd6de2c82009-04-10 12:17:41 -07003290
Rusty Russell6c5db222008-03-10 11:43:52 -07003291 mutex_lock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003292 /* Drop initial reference. */
3293 module_put(mod);
Rusty Russellad6561d2009-06-12 21:47:03 -06003294 trim_init_extable(mod);
Jan Beulich4a496222009-07-06 14:50:42 +01003295#ifdef CONFIG_KALLSYMS
3296 mod->num_symtab = mod->core_num_syms;
3297 mod->symtab = mod->core_symtab;
Jan Beulich554bdfe2009-07-06 14:51:44 +01003298 mod->strtab = mod->core_strtab;
Jan Beulich4a496222009-07-06 14:50:42 +01003299#endif
Peter Zijlstra93c2e102015-05-27 11:09:37 +09303300 mod_tree_remove_init(mod);
Jan Glauber01526ed2011-05-19 16:55:26 -06003301 unset_module_init_ro_nx(mod);
Rusty Russelld453cde2015-01-20 09:07:04 +10303302 module_arch_freeing_init(mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003303 mod->module_init = NULL;
3304 mod->init_size = 0;
Jan Glauber4d103802011-05-19 16:55:25 -06003305 mod->init_ro_size = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003306 mod->init_text_size = 0;
Rusty Russellc7496372015-01-20 09:07:05 +10303307 /*
3308 * We want to free module_init, but be aware that kallsyms may be
Peter Zijlstra0be964b2015-05-27 11:09:35 +09303309 * walking this with preempt disabled. In all the failure paths, we
3310 * call synchronize_sched(), but we don't want to slow down the success
3311 * path, so use actual RCU here.
Rusty Russellc7496372015-01-20 09:07:05 +10303312 */
Peter Zijlstra0be964b2015-05-27 11:09:35 +09303313 call_rcu_sched(&freeinit->rcu, do_free_init);
Ashutosh Naik6389a382006-03-23 03:00:46 -08003314 mutex_unlock(&module_mutex);
Rusty Russell6f139092012-09-28 14:31:03 +09303315 wake_up_all(&module_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003316
3317 return 0;
Rusty Russellc7496372015-01-20 09:07:05 +10303318
3319fail_free_freeinit:
3320 kfree(freeinit);
3321fail:
3322 /* Try to protect us from buggy refcounters. */
3323 mod->state = MODULE_STATE_GOING;
3324 synchronize_sched();
3325 module_put(mod);
3326 blocking_notifier_call_chain(&module_notify_list,
3327 MODULE_STATE_GOING, mod);
3328 free_module(mod);
3329 wake_up_all(&module_wq);
3330 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003331}
3332
Kees Cook34e11692012-10-16 07:31:07 +10303333static int may_init_module(void)
3334{
3335 if (!capable(CAP_SYS_MODULE) || modules_disabled)
3336 return -EPERM;
3337
3338 return 0;
3339}
3340
Rusty Russella3535c72013-01-21 17:18:59 +10303341/*
3342 * We try to place it in the list now to make sure it's unique before
3343 * we dedicate too many resources. In particular, temporary percpu
3344 * memory exhaustion.
3345 */
3346static int add_unformed_module(struct module *mod)
3347{
3348 int err;
3349 struct module *old;
3350
3351 mod->state = MODULE_STATE_UNFORMED;
3352
3353again:
3354 mutex_lock(&module_mutex);
Mathias Krause4f6de4d2013-07-02 15:35:11 +09303355 old = find_module_all(mod->name, strlen(mod->name), true);
3356 if (old != NULL) {
Rusty Russella3535c72013-01-21 17:18:59 +10303357 if (old->state == MODULE_STATE_COMING
3358 || old->state == MODULE_STATE_UNFORMED) {
3359 /* Wait in case it fails to load. */
3360 mutex_unlock(&module_mutex);
Peter Zijlstra9cc019b2015-02-11 15:01:13 +10303361 err = wait_event_interruptible(module_wq,
3362 finished_loading(mod->name));
Rusty Russella3535c72013-01-21 17:18:59 +10303363 if (err)
3364 goto out_unlocked;
3365 goto again;
3366 }
3367 err = -EEXIST;
3368 goto out;
3369 }
Peter Zijlstra4f6665462015-05-27 11:09:38 +09303370 mod_update_bounds(mod);
Rusty Russella3535c72013-01-21 17:18:59 +10303371 list_add_rcu(&mod->list, &modules);
Peter Zijlstra93c2e102015-05-27 11:09:37 +09303372 mod_tree_insert(mod);
Rusty Russella3535c72013-01-21 17:18:59 +10303373 err = 0;
3374
3375out:
3376 mutex_unlock(&module_mutex);
3377out_unlocked:
3378 return err;
3379}
3380
3381static int complete_formation(struct module *mod, struct load_info *info)
3382{
3383 int err;
3384
3385 mutex_lock(&module_mutex);
3386
3387 /* Find duplicate symbols (must be called under lock). */
3388 err = verify_export_symbols(mod);
3389 if (err < 0)
3390 goto out;
3391
3392 /* This relies on module_mutex for list integrity. */
3393 module_bug_finalize(info->hdr, info->sechdrs, mod);
3394
Rusty Russell49822232014-05-14 10:54:19 +09303395 /* Set RO and NX regions for core */
3396 set_section_ro_nx(mod->module_core,
3397 mod->core_text_size,
3398 mod->core_ro_size,
3399 mod->core_size);
3400
3401 /* Set RO and NX regions for init */
3402 set_section_ro_nx(mod->module_init,
3403 mod->init_text_size,
3404 mod->init_ro_size,
3405 mod->init_size);
3406
Rusty Russella3535c72013-01-21 17:18:59 +10303407 /* Mark state as coming so strong_try_module_get() ignores us,
3408 * but kallsyms etc. can see us. */
3409 mod->state = MODULE_STATE_COMING;
Rusty Russell49822232014-05-14 10:54:19 +09303410 mutex_unlock(&module_mutex);
3411
3412 blocking_notifier_call_chain(&module_notify_list,
3413 MODULE_STATE_COMING, mod);
3414 return 0;
Rusty Russella3535c72013-01-21 17:18:59 +10303415
3416out:
3417 mutex_unlock(&module_mutex);
3418 return err;
3419}
3420
Rusty Russell54041d82013-07-02 15:35:12 +09303421static int unknown_module_param_cb(char *param, char *val, const char *modname)
3422{
Ionut Alexa6da0b562014-11-10 09:31:29 +10303423 /* Check for magic 'dyndbg' arg */
Rusty Russell54041d82013-07-02 15:35:12 +09303424 int ret = ddebug_dyndbg_module_param_cb(param, val, modname);
Andrew Mortonbddb12b2013-11-12 15:11:28 -08003425 if (ret != 0)
3426 pr_warn("%s: unknown parameter '%s' ignored\n", modname, param);
Rusty Russell54041d82013-07-02 15:35:12 +09303427 return 0;
3428}
3429
Kees Cook34e11692012-10-16 07:31:07 +10303430/* Allocate and load the module: note that size of section 0 is always
3431 zero, and we rely on this for optional sections. */
Rusty Russell2f3238a2012-10-22 18:09:41 +10303432static int load_module(struct load_info *info, const char __user *uargs,
3433 int flags)
Kees Cook34e11692012-10-16 07:31:07 +10303434{
Rusty Russella3535c72013-01-21 17:18:59 +10303435 struct module *mod;
Kees Cook34e11692012-10-16 07:31:07 +10303436 long err;
Rusty Russell51e158c2014-04-28 11:34:33 +09303437 char *after_dashes;
Kees Cook34e11692012-10-16 07:31:07 +10303438
3439 err = module_sig_check(info);
3440 if (err)
3441 goto free_copy;
3442
3443 err = elf_header_check(info);
3444 if (err)
3445 goto free_copy;
3446
3447 /* Figure out module layout, and allocate all the memory. */
Rusty Russell2f3238a2012-10-22 18:09:41 +10303448 mod = layout_and_allocate(info, flags);
Kees Cook34e11692012-10-16 07:31:07 +10303449 if (IS_ERR(mod)) {
3450 err = PTR_ERR(mod);
3451 goto free_copy;
3452 }
3453
Rusty Russella3535c72013-01-21 17:18:59 +10303454 /* Reserve our place in the list. */
3455 err = add_unformed_module(mod);
3456 if (err)
Rusty Russell1fb93412013-01-12 13:27:34 +10303457 goto free_module;
Rusty Russell1fb93412013-01-12 13:27:34 +10303458
Kees Cook34e11692012-10-16 07:31:07 +10303459#ifdef CONFIG_MODULE_SIG
3460 mod->sig_ok = info->sig_ok;
Rusty Russell64748a22013-01-21 17:03:02 +10303461 if (!mod->sig_ok) {
Andrew Mortonbddb12b2013-11-12 15:11:28 -08003462 pr_notice_once("%s: module verification failed: signature "
Marcel Holtmannab92ebb2015-02-06 15:09:57 +10303463 "and/or required key missing - tainting "
Andrew Mortonbddb12b2013-11-12 15:11:28 -08003464 "kernel\n", mod->name);
Mathieu Desnoyers66cc69e2014-03-13 12:11:30 +10303465 add_taint_module(mod, TAINT_UNSIGNED_MODULE, LOCKDEP_STILL_OK);
Rusty Russell64748a22013-01-21 17:03:02 +10303466 }
Kees Cook34e11692012-10-16 07:31:07 +10303467#endif
3468
Rusty Russell8d8022e2013-07-03 10:06:28 +09303469 /* To avoid stressing percpu allocator, do this once we're unique. */
Rusty Russell9eb76d72013-07-03 10:06:29 +09303470 err = percpu_modalloc(mod, info);
Rusty Russell8d8022e2013-07-03 10:06:28 +09303471 if (err)
3472 goto unlink_mod;
3473
Kees Cook34e11692012-10-16 07:31:07 +10303474 /* Now module is in final location, initialize linked lists, etc. */
3475 err = module_unload_init(mod);
3476 if (err)
Rusty Russell1fb93412013-01-12 13:27:34 +10303477 goto unlink_mod;
Kees Cook34e11692012-10-16 07:31:07 +10303478
3479 /* Now we've got everything in the final locations, we can
3480 * find optional sections. */
Frantisek Hrbataeb3057d2013-10-14 18:08:46 +10303481 err = find_module_sections(mod, info);
3482 if (err)
3483 goto free_unload;
Kees Cook34e11692012-10-16 07:31:07 +10303484
3485 err = check_module_license_and_versions(mod);
3486 if (err)
3487 goto free_unload;
3488
3489 /* Set up MODINFO_ATTR fields */
3490 setup_modinfo(mod, info);
3491
3492 /* Fix up syms, so that st_value is a pointer to location. */
3493 err = simplify_symbols(mod, info);
3494 if (err < 0)
3495 goto free_modinfo;
3496
3497 err = apply_relocations(mod, info);
3498 if (err < 0)
3499 goto free_modinfo;
3500
3501 err = post_relocation(mod, info);
3502 if (err < 0)
3503 goto free_modinfo;
3504
3505 flush_module_icache(mod);
3506
3507 /* Now copy in args */
3508 mod->args = strndup_user(uargs, ~0UL >> 1);
3509 if (IS_ERR(mod->args)) {
3510 err = PTR_ERR(mod->args);
3511 goto free_arch_cleanup;
3512 }
3513
Kees Cook34e11692012-10-16 07:31:07 +10303514 dynamic_debug_setup(info->debug, info->num_debug);
3515
Steven Rostedt (Red Hat)a949ae52014-04-24 10:40:12 -04003516 /* Ftrace init must be called in the MODULE_STATE_UNFORMED state */
3517 ftrace_module_init(mod);
3518
Rusty Russella3535c72013-01-21 17:18:59 +10303519 /* Finally it's fully formed, ready to start executing. */
3520 err = complete_formation(mod, info);
3521 if (err)
Rusty Russell1fb93412013-01-12 13:27:34 +10303522 goto ddebug_cleanup;
Kees Cook34e11692012-10-16 07:31:07 +10303523
Kees Cook34e11692012-10-16 07:31:07 +10303524 /* Module is ready to execute: parsing args may do that. */
Rusty Russell51e158c2014-04-28 11:34:33 +09303525 after_dashes = parse_args(mod->name, mod->args, mod->kp, mod->num_kp,
3526 -32768, 32767, unknown_module_param_cb);
3527 if (IS_ERR(after_dashes)) {
3528 err = PTR_ERR(after_dashes);
Rusty Russell1fb93412013-01-12 13:27:34 +10303529 goto bug_cleanup;
Rusty Russell51e158c2014-04-28 11:34:33 +09303530 } else if (after_dashes) {
3531 pr_warn("%s: parameters '%s' after `--' ignored\n",
3532 mod->name, after_dashes);
3533 }
Kees Cook34e11692012-10-16 07:31:07 +10303534
3535 /* Link in to syfs. */
3536 err = mod_sysfs_setup(mod, info, mod->kp, mod->num_kp);
3537 if (err < 0)
Rusty Russell1fb93412013-01-12 13:27:34 +10303538 goto bug_cleanup;
Kees Cook34e11692012-10-16 07:31:07 +10303539
3540 /* Get rid of temporary copy. */
3541 free_copy(info);
3542
3543 /* Done! */
3544 trace_module_load(mod);
3545
3546 return do_init_module(mod);
3547
Rusty Russell1fb93412013-01-12 13:27:34 +10303548 bug_cleanup:
3549 /* module_bug_cleanup needs module_mutex protection */
Kees Cook34e11692012-10-16 07:31:07 +10303550 mutex_lock(&module_mutex);
Kees Cook34e11692012-10-16 07:31:07 +10303551 module_bug_cleanup(mod);
Linus Torvaldsee61abb2013-01-20 20:22:58 -08003552 mutex_unlock(&module_mutex);
Andy Lutomirskiff7e0052014-08-16 04:13:37 +09303553
3554 /* we can't deallocate the module until we clear memory protection */
3555 unset_module_init_ro_nx(mod);
3556 unset_module_core_ro_nx(mod);
3557
Rusty Russella3535c72013-01-21 17:18:59 +10303558 ddebug_cleanup:
Rusty Russell1fb93412013-01-12 13:27:34 +10303559 dynamic_debug_remove(info->debug);
Kees Cook34e11692012-10-16 07:31:07 +10303560 synchronize_sched();
3561 kfree(mod->args);
3562 free_arch_cleanup:
3563 module_arch_cleanup(mod);
3564 free_modinfo:
3565 free_modinfo(mod);
3566 free_unload:
3567 module_unload_free(mod);
Rusty Russell1fb93412013-01-12 13:27:34 +10303568 unlink_mod:
3569 mutex_lock(&module_mutex);
3570 /* Unlink carefully: kallsyms could be walking list. */
3571 list_del_rcu(&mod->list);
3572 wake_up_all(&module_wq);
Peter Zijlstra0be964b2015-05-27 11:09:35 +09303573 /* Wait for RCU-sched synchronizing before releasing mod->list. */
3574 synchronize_sched();
Rusty Russell1fb93412013-01-12 13:27:34 +10303575 mutex_unlock(&module_mutex);
Kees Cook34e11692012-10-16 07:31:07 +10303576 free_module:
Peter Zijlstra35a93932015-02-26 16:23:11 +01003577 /* Free lock-classes; relies on the preceding sync_rcu() */
3578 lockdep_free_key_range(mod->module_core, mod->core_size);
3579
Kees Cook34e11692012-10-16 07:31:07 +10303580 module_deallocate(mod, info);
3581 free_copy:
3582 free_copy(info);
3583 return err;
3584}
3585
3586SYSCALL_DEFINE3(init_module, void __user *, umod,
3587 unsigned long, len, const char __user *, uargs)
3588{
3589 int err;
3590 struct load_info info = { };
3591
3592 err = may_init_module();
3593 if (err)
3594 return err;
3595
3596 pr_debug("init_module: umod=%p, len=%lu, uargs=%p\n",
3597 umod, len, uargs);
3598
3599 err = copy_module_from_user(umod, len, &info);
3600 if (err)
3601 return err;
3602
Rusty Russell2f3238a2012-10-22 18:09:41 +10303603 return load_module(&info, uargs, 0);
Kees Cook34e11692012-10-16 07:31:07 +10303604}
3605
Rusty Russell2f3238a2012-10-22 18:09:41 +10303606SYSCALL_DEFINE3(finit_module, int, fd, const char __user *, uargs, int, flags)
Kees Cook34e11692012-10-16 07:31:07 +10303607{
3608 int err;
3609 struct load_info info = { };
3610
3611 err = may_init_module();
3612 if (err)
3613 return err;
3614
Rusty Russell2f3238a2012-10-22 18:09:41 +10303615 pr_debug("finit_module: fd=%d, uargs=%p, flags=%i\n", fd, uargs, flags);
3616
3617 if (flags & ~(MODULE_INIT_IGNORE_MODVERSIONS
3618 |MODULE_INIT_IGNORE_VERMAGIC))
3619 return -EINVAL;
Kees Cook34e11692012-10-16 07:31:07 +10303620
3621 err = copy_module_from_fd(fd, &info);
3622 if (err)
3623 return err;
3624
Rusty Russell2f3238a2012-10-22 18:09:41 +10303625 return load_module(&info, uargs, flags);
Kees Cook34e11692012-10-16 07:31:07 +10303626}
3627
Linus Torvalds1da177e2005-04-16 15:20:36 -07003628static inline int within(unsigned long addr, void *start, unsigned long size)
3629{
3630 return ((void *)addr >= start && (void *)addr < start + size);
3631}
3632
3633#ifdef CONFIG_KALLSYMS
3634/*
3635 * This ignores the intensely annoying "mapping symbols" found
3636 * in ARM ELF files: $a, $t and $d.
3637 */
3638static inline int is_arm_mapping_symbol(const char *str)
3639{
Russell King2e3a10a2014-07-27 07:29:01 +09303640 if (str[0] == '.' && str[1] == 'L')
3641 return true;
Kyle McMartin6c34f1f2014-09-16 22:37:18 +01003642 return str[0] == '$' && strchr("axtd", str[1])
Linus Torvalds1da177e2005-04-16 15:20:36 -07003643 && (str[2] == '\0' || str[2] == '.');
3644}
3645
3646static const char *get_ksymbol(struct module *mod,
3647 unsigned long addr,
3648 unsigned long *size,
3649 unsigned long *offset)
3650{
3651 unsigned int i, best = 0;
3652 unsigned long nextval;
3653
3654 /* At worse, next value is at end of module */
Masami Hiramatsua06f6212009-01-06 14:41:49 -08003655 if (within_module_init(addr, mod))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003656 nextval = (unsigned long)mod->module_init+mod->init_text_size;
Daniel Walker22a8bde2007-10-18 03:06:07 -07003657 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003658 nextval = (unsigned long)mod->module_core+mod->core_text_size;
3659
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003660 /* Scan for closest preceding symbol, and next symbol. (ELF
Daniel Walker22a8bde2007-10-18 03:06:07 -07003661 starts real symbols at 1). */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003662 for (i = 1; i < mod->num_symtab; i++) {
3663 if (mod->symtab[i].st_shndx == SHN_UNDEF)
3664 continue;
3665
3666 /* We ignore unnamed symbols: they're uninformative
3667 * and inserted at a whim. */
3668 if (mod->symtab[i].st_value <= addr
3669 && mod->symtab[i].st_value > mod->symtab[best].st_value
3670 && *(mod->strtab + mod->symtab[i].st_name) != '\0'
3671 && !is_arm_mapping_symbol(mod->strtab + mod->symtab[i].st_name))
3672 best = i;
3673 if (mod->symtab[i].st_value > addr
3674 && mod->symtab[i].st_value < nextval
3675 && *(mod->strtab + mod->symtab[i].st_name) != '\0'
3676 && !is_arm_mapping_symbol(mod->strtab + mod->symtab[i].st_name))
3677 nextval = mod->symtab[i].st_value;
3678 }
3679
3680 if (!best)
3681 return NULL;
3682
Alexey Dobriyanffb45122007-05-08 00:28:41 -07003683 if (size)
3684 *size = nextval - mod->symtab[best].st_value;
3685 if (offset)
3686 *offset = addr - mod->symtab[best].st_value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003687 return mod->strtab + mod->symtab[best].st_name;
3688}
3689
Rusty Russell6dd06c92008-01-29 17:13:22 -05003690/* For kallsyms to ask for address resolution. NULL means not found. Careful
3691 * not to lock to avoid deadlock on oopses, simply disable preemption. */
Andrew Morton92dfc9d2008-02-08 04:18:43 -08003692const char *module_address_lookup(unsigned long addr,
Rusty Russell6dd06c92008-01-29 17:13:22 -05003693 unsigned long *size,
3694 unsigned long *offset,
3695 char **modname,
3696 char *namebuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003697{
Rusty Russellcb2a5202008-01-14 00:55:03 -08003698 const char *ret = NULL;
Peter Zijlstrab7df4d12015-05-27 11:09:37 +09303699 struct module *mod;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003700
Rusty Russellcb2a5202008-01-14 00:55:03 -08003701 preempt_disable();
Peter Zijlstrab7df4d12015-05-27 11:09:37 +09303702 mod = __module_address(addr);
3703 if (mod) {
3704 if (modname)
3705 *modname = mod->name;
3706 ret = get_ksymbol(mod, addr, size, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003707 }
Rusty Russell6dd06c92008-01-29 17:13:22 -05003708 /* Make a copy in here where it's safe */
3709 if (ret) {
3710 strncpy(namebuf, ret, KSYM_NAME_LEN - 1);
3711 ret = namebuf;
3712 }
Rusty Russellcb2a5202008-01-14 00:55:03 -08003713 preempt_enable();
Peter Zijlstrab7df4d12015-05-27 11:09:37 +09303714
Andrew Morton92dfc9d2008-02-08 04:18:43 -08003715 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003716}
3717
Alexey Dobriyan9d65cb42007-05-08 00:28:43 -07003718int lookup_module_symbol_name(unsigned long addr, char *symname)
3719{
3720 struct module *mod;
3721
Rusty Russellcb2a5202008-01-14 00:55:03 -08003722 preempt_disable();
Andi Kleend72b3752008-08-30 10:09:00 +02003723 list_for_each_entry_rcu(mod, &modules, list) {
Rusty Russell0d21b0e2013-01-12 11:38:44 +10303724 if (mod->state == MODULE_STATE_UNFORMED)
3725 continue;
Petr Mladek9b20a352014-07-27 07:24:01 +09303726 if (within_module(addr, mod)) {
Alexey Dobriyan9d65cb42007-05-08 00:28:43 -07003727 const char *sym;
3728
3729 sym = get_ksymbol(mod, addr, NULL, NULL);
3730 if (!sym)
3731 goto out;
Tejun Heo9281ace2007-07-17 04:03:51 -07003732 strlcpy(symname, sym, KSYM_NAME_LEN);
Rusty Russellcb2a5202008-01-14 00:55:03 -08003733 preempt_enable();
Alexey Dobriyan9d65cb42007-05-08 00:28:43 -07003734 return 0;
3735 }
3736 }
3737out:
Rusty Russellcb2a5202008-01-14 00:55:03 -08003738 preempt_enable();
Alexey Dobriyan9d65cb42007-05-08 00:28:43 -07003739 return -ERANGE;
3740}
3741
Alexey Dobriyana5c43da2007-05-08 00:28:47 -07003742int lookup_module_symbol_attrs(unsigned long addr, unsigned long *size,
3743 unsigned long *offset, char *modname, char *name)
3744{
3745 struct module *mod;
3746
Rusty Russellcb2a5202008-01-14 00:55:03 -08003747 preempt_disable();
Andi Kleend72b3752008-08-30 10:09:00 +02003748 list_for_each_entry_rcu(mod, &modules, list) {
Rusty Russell0d21b0e2013-01-12 11:38:44 +10303749 if (mod->state == MODULE_STATE_UNFORMED)
3750 continue;
Petr Mladek9b20a352014-07-27 07:24:01 +09303751 if (within_module(addr, mod)) {
Alexey Dobriyana5c43da2007-05-08 00:28:47 -07003752 const char *sym;
3753
3754 sym = get_ksymbol(mod, addr, size, offset);
3755 if (!sym)
3756 goto out;
3757 if (modname)
Tejun Heo9281ace2007-07-17 04:03:51 -07003758 strlcpy(modname, mod->name, MODULE_NAME_LEN);
Alexey Dobriyana5c43da2007-05-08 00:28:47 -07003759 if (name)
Tejun Heo9281ace2007-07-17 04:03:51 -07003760 strlcpy(name, sym, KSYM_NAME_LEN);
Rusty Russellcb2a5202008-01-14 00:55:03 -08003761 preempt_enable();
Alexey Dobriyana5c43da2007-05-08 00:28:47 -07003762 return 0;
3763 }
3764 }
3765out:
Rusty Russellcb2a5202008-01-14 00:55:03 -08003766 preempt_enable();
Alexey Dobriyana5c43da2007-05-08 00:28:47 -07003767 return -ERANGE;
3768}
3769
Alexey Dobriyanea078902007-05-08 00:28:39 -07003770int module_get_kallsym(unsigned int symnum, unsigned long *value, char *type,
3771 char *name, char *module_name, int *exported)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003772{
3773 struct module *mod;
3774
Rusty Russellcb2a5202008-01-14 00:55:03 -08003775 preempt_disable();
Andi Kleend72b3752008-08-30 10:09:00 +02003776 list_for_each_entry_rcu(mod, &modules, list) {
Rusty Russell0d21b0e2013-01-12 11:38:44 +10303777 if (mod->state == MODULE_STATE_UNFORMED)
3778 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003779 if (symnum < mod->num_symtab) {
3780 *value = mod->symtab[symnum].st_value;
3781 *type = mod->symtab[symnum].st_info;
Andreas Gruenbacher098c5ee2006-07-14 00:24:04 -07003782 strlcpy(name, mod->strtab + mod->symtab[symnum].st_name,
Tejun Heo9281ace2007-07-17 04:03:51 -07003783 KSYM_NAME_LEN);
3784 strlcpy(module_name, mod->name, MODULE_NAME_LEN);
Tim Abbottca4787b2009-01-05 08:40:10 -06003785 *exported = is_exported(name, *value, mod);
Rusty Russellcb2a5202008-01-14 00:55:03 -08003786 preempt_enable();
Alexey Dobriyanea078902007-05-08 00:28:39 -07003787 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003788 }
3789 symnum -= mod->num_symtab;
3790 }
Rusty Russellcb2a5202008-01-14 00:55:03 -08003791 preempt_enable();
Alexey Dobriyanea078902007-05-08 00:28:39 -07003792 return -ERANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003793}
3794
3795static unsigned long mod_find_symname(struct module *mod, const char *name)
3796{
3797 unsigned int i;
3798
3799 for (i = 0; i < mod->num_symtab; i++)
Keith Owens54e8ce42006-02-03 03:03:53 -08003800 if (strcmp(name, mod->strtab+mod->symtab[i].st_name) == 0 &&
3801 mod->symtab[i].st_info != 'U')
Linus Torvalds1da177e2005-04-16 15:20:36 -07003802 return mod->symtab[i].st_value;
3803 return 0;
3804}
3805
3806/* Look for this name: can be of form module:name. */
3807unsigned long module_kallsyms_lookup_name(const char *name)
3808{
3809 struct module *mod;
3810 char *colon;
3811 unsigned long ret = 0;
3812
3813 /* Don't lock: we're in enough trouble already. */
Rusty Russellcb2a5202008-01-14 00:55:03 -08003814 preempt_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003815 if ((colon = strchr(name, ':')) != NULL) {
Mathias Krause4f6de4d2013-07-02 15:35:11 +09303816 if ((mod = find_module_all(name, colon - name, false)) != NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003817 ret = mod_find_symname(mod, colon+1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003818 } else {
Rusty Russell0d21b0e2013-01-12 11:38:44 +10303819 list_for_each_entry_rcu(mod, &modules, list) {
3820 if (mod->state == MODULE_STATE_UNFORMED)
3821 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003822 if ((ret = mod_find_symname(mod, name)) != 0)
3823 break;
Rusty Russell0d21b0e2013-01-12 11:38:44 +10303824 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003825 }
Rusty Russellcb2a5202008-01-14 00:55:03 -08003826 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003827 return ret;
3828}
Anders Kaseorg75a66612008-12-05 19:03:58 -05003829
3830int module_kallsyms_on_each_symbol(int (*fn)(void *, const char *,
3831 struct module *, unsigned long),
3832 void *data)
3833{
3834 struct module *mod;
3835 unsigned int i;
3836 int ret;
3837
Peter Zijlstra0be964b2015-05-27 11:09:35 +09303838 module_assert_mutex();
3839
Anders Kaseorg75a66612008-12-05 19:03:58 -05003840 list_for_each_entry(mod, &modules, list) {
Rusty Russell0d21b0e2013-01-12 11:38:44 +10303841 if (mod->state == MODULE_STATE_UNFORMED)
3842 continue;
Anders Kaseorg75a66612008-12-05 19:03:58 -05003843 for (i = 0; i < mod->num_symtab; i++) {
3844 ret = fn(data, mod->strtab + mod->symtab[i].st_name,
3845 mod, mod->symtab[i].st_value);
3846 if (ret != 0)
3847 return ret;
3848 }
3849 }
3850 return 0;
3851}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003852#endif /* CONFIG_KALLSYMS */
3853
Arjan van de Ven21aa9282008-01-25 21:08:33 +01003854static char *module_flags(struct module *mod, char *buf)
Florin Malitafa3ba2e82006-10-11 01:21:48 -07003855{
3856 int bx = 0;
3857
Rusty Russell0d21b0e2013-01-12 11:38:44 +10303858 BUG_ON(mod->state == MODULE_STATE_UNFORMED);
Arjan van de Ven21aa9282008-01-25 21:08:33 +01003859 if (mod->taints ||
3860 mod->state == MODULE_STATE_GOING ||
3861 mod->state == MODULE_STATE_COMING) {
Florin Malitafa3ba2e82006-10-11 01:21:48 -07003862 buf[bx++] = '(';
Kay Sieverscca3e702012-01-13 09:32:15 +10303863 bx += module_flags_taint(mod, buf + bx);
Arjan van de Ven21aa9282008-01-25 21:08:33 +01003864 /* Show a - for module-is-being-unloaded */
3865 if (mod->state == MODULE_STATE_GOING)
3866 buf[bx++] = '-';
3867 /* Show a + for module-is-being-loaded */
3868 if (mod->state == MODULE_STATE_COMING)
3869 buf[bx++] = '+';
Florin Malitafa3ba2e82006-10-11 01:21:48 -07003870 buf[bx++] = ')';
3871 }
3872 buf[bx] = '\0';
3873
3874 return buf;
3875}
3876
Alexey Dobriyan3b5d5c62008-10-06 13:19:27 +04003877#ifdef CONFIG_PROC_FS
3878/* Called by the /proc file system to return a list of modules. */
3879static void *m_start(struct seq_file *m, loff_t *pos)
3880{
3881 mutex_lock(&module_mutex);
3882 return seq_list_start(&modules, *pos);
3883}
3884
3885static void *m_next(struct seq_file *m, void *p, loff_t *pos)
3886{
3887 return seq_list_next(p, &modules, pos);
3888}
3889
3890static void m_stop(struct seq_file *m, void *p)
3891{
3892 mutex_unlock(&module_mutex);
3893}
3894
Linus Torvalds1da177e2005-04-16 15:20:36 -07003895static int m_show(struct seq_file *m, void *p)
3896{
3897 struct module *mod = list_entry(p, struct module, list);
Florin Malitafa3ba2e82006-10-11 01:21:48 -07003898 char buf[8];
3899
Rusty Russell0d21b0e2013-01-12 11:38:44 +10303900 /* We always ignore unformed modules. */
3901 if (mod->state == MODULE_STATE_UNFORMED)
3902 return 0;
3903
Denys Vlasenko2f0f2a32008-07-22 19:24:27 -05003904 seq_printf(m, "%s %u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003905 mod->name, mod->init_size + mod->core_size);
3906 print_unload_info(m, mod);
3907
3908 /* Informative for users. */
3909 seq_printf(m, " %s",
Ionut Alexa6da0b562014-11-10 09:31:29 +10303910 mod->state == MODULE_STATE_GOING ? "Unloading" :
3911 mod->state == MODULE_STATE_COMING ? "Loading" :
Linus Torvalds1da177e2005-04-16 15:20:36 -07003912 "Live");
3913 /* Used by oprofile and other similar tools. */
Kees Cook9f36e2c2011-03-22 16:34:22 -07003914 seq_printf(m, " 0x%pK", mod->module_core);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003915
Florin Malitafa3ba2e82006-10-11 01:21:48 -07003916 /* Taints info */
3917 if (mod->taints)
Arjan van de Ven21aa9282008-01-25 21:08:33 +01003918 seq_printf(m, " %s", module_flags(mod, buf));
Florin Malitafa3ba2e82006-10-11 01:21:48 -07003919
Ionut Alexa6da0b562014-11-10 09:31:29 +10303920 seq_puts(m, "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003921 return 0;
3922}
3923
3924/* Format: modulename size refcount deps address
3925
3926 Where refcount is a number or -, and deps is a comma-separated list
3927 of depends or -.
3928*/
Alexey Dobriyan3b5d5c62008-10-06 13:19:27 +04003929static const struct seq_operations modules_op = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003930 .start = m_start,
3931 .next = m_next,
3932 .stop = m_stop,
3933 .show = m_show
3934};
3935
Alexey Dobriyan3b5d5c62008-10-06 13:19:27 +04003936static int modules_open(struct inode *inode, struct file *file)
3937{
3938 return seq_open(file, &modules_op);
3939}
3940
3941static const struct file_operations proc_modules_operations = {
3942 .open = modules_open,
3943 .read = seq_read,
3944 .llseek = seq_lseek,
3945 .release = seq_release,
3946};
3947
3948static int __init proc_modules_init(void)
3949{
3950 proc_create("modules", 0, NULL, &proc_modules_operations);
3951 return 0;
3952}
3953module_init(proc_modules_init);
3954#endif
3955
Linus Torvalds1da177e2005-04-16 15:20:36 -07003956/* Given an address, look for it in the module exception tables. */
3957const struct exception_table_entry *search_module_extables(unsigned long addr)
3958{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003959 const struct exception_table_entry *e = NULL;
3960 struct module *mod;
3961
Rusty Russell24da1cb2007-07-15 23:41:46 -07003962 preempt_disable();
Andi Kleend72b3752008-08-30 10:09:00 +02003963 list_for_each_entry_rcu(mod, &modules, list) {
Rusty Russell0d21b0e2013-01-12 11:38:44 +10303964 if (mod->state == MODULE_STATE_UNFORMED)
3965 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003966 if (mod->num_exentries == 0)
3967 continue;
Daniel Walker22a8bde2007-10-18 03:06:07 -07003968
Linus Torvalds1da177e2005-04-16 15:20:36 -07003969 e = search_extable(mod->extable,
3970 mod->extable + mod->num_exentries - 1,
3971 addr);
3972 if (e)
3973 break;
3974 }
Rusty Russell24da1cb2007-07-15 23:41:46 -07003975 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003976
3977 /* Now, if we found one, we are running inside it now, hence
Daniel Walker22a8bde2007-10-18 03:06:07 -07003978 we cannot unload the module, hence no refcnt needed. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003979 return e;
3980}
3981
Ingo Molnar4d435f92006-07-03 00:24:24 -07003982/*
Rusty Russelle6104992009-03-31 13:05:31 -06003983 * is_module_address - is this address inside a module?
3984 * @addr: the address to check.
3985 *
3986 * See is_module_text_address() if you simply want to see if the address
3987 * is code (not data).
Ingo Molnar4d435f92006-07-03 00:24:24 -07003988 */
Rusty Russelle6104992009-03-31 13:05:31 -06003989bool is_module_address(unsigned long addr)
Ingo Molnar4d435f92006-07-03 00:24:24 -07003990{
Rusty Russelle6104992009-03-31 13:05:31 -06003991 bool ret;
Ingo Molnar4d435f92006-07-03 00:24:24 -07003992
Rusty Russell24da1cb2007-07-15 23:41:46 -07003993 preempt_disable();
Rusty Russelle6104992009-03-31 13:05:31 -06003994 ret = __module_address(addr) != NULL;
Rusty Russell24da1cb2007-07-15 23:41:46 -07003995 preempt_enable();
Ingo Molnar4d435f92006-07-03 00:24:24 -07003996
Rusty Russelle6104992009-03-31 13:05:31 -06003997 return ret;
Ingo Molnar4d435f92006-07-03 00:24:24 -07003998}
3999
Rusty Russelle6104992009-03-31 13:05:31 -06004000/*
4001 * __module_address - get the module which contains an address.
4002 * @addr: the address.
4003 *
4004 * Must be called with preempt disabled or module mutex held so that
4005 * module doesn't get freed during this.
4006 */
Linus Torvalds714f83d2009-04-05 11:04:19 -07004007struct module *__module_address(unsigned long addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004008{
4009 struct module *mod;
4010
Rusty Russell3a642e92008-07-22 19:24:28 -05004011 if (addr < module_addr_min || addr > module_addr_max)
4012 return NULL;
4013
Peter Zijlstra0be964b2015-05-27 11:09:35 +09304014 module_assert_mutex_or_preempt();
4015
Peter Zijlstra6c9692e2015-05-27 11:09:37 +09304016 mod = mod_find(addr);
Peter Zijlstra93c2e102015-05-27 11:09:37 +09304017 if (mod) {
4018 BUG_ON(!within_module(addr, mod));
Rusty Russell0d21b0e2013-01-12 11:38:44 +10304019 if (mod->state == MODULE_STATE_UNFORMED)
Peter Zijlstra93c2e102015-05-27 11:09:37 +09304020 mod = NULL;
Rusty Russell0d21b0e2013-01-12 11:38:44 +10304021 }
Peter Zijlstra93c2e102015-05-27 11:09:37 +09304022 return mod;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004023}
Tim Abbottc6b37802008-12-05 19:03:59 -05004024EXPORT_SYMBOL_GPL(__module_address);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004025
Rusty Russelle6104992009-03-31 13:05:31 -06004026/*
4027 * is_module_text_address - is this address inside module code?
4028 * @addr: the address to check.
4029 *
4030 * See is_module_address() if you simply want to see if the address is
4031 * anywhere in a module. See kernel_text_address() for testing if an
4032 * address corresponds to kernel or module code.
4033 */
4034bool is_module_text_address(unsigned long addr)
4035{
4036 bool ret;
4037
4038 preempt_disable();
4039 ret = __module_text_address(addr) != NULL;
4040 preempt_enable();
4041
4042 return ret;
4043}
4044
4045/*
4046 * __module_text_address - get the module whose code contains an address.
4047 * @addr: the address.
4048 *
4049 * Must be called with preempt disabled or module mutex held so that
4050 * module doesn't get freed during this.
4051 */
4052struct module *__module_text_address(unsigned long addr)
4053{
4054 struct module *mod = __module_address(addr);
4055 if (mod) {
4056 /* Make sure it's within the text section. */
4057 if (!within(addr, mod->module_init, mod->init_text_size)
4058 && !within(addr, mod->module_core, mod->core_text_size))
4059 mod = NULL;
4060 }
4061 return mod;
4062}
Tim Abbottc6b37802008-12-05 19:03:59 -05004063EXPORT_SYMBOL_GPL(__module_text_address);
Rusty Russelle6104992009-03-31 13:05:31 -06004064
Linus Torvalds1da177e2005-04-16 15:20:36 -07004065/* Don't grab lock, we're oopsing. */
4066void print_modules(void)
4067{
4068 struct module *mod;
Randy Dunlap2bc2d612006-10-02 02:17:02 -07004069 char buf[8];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004070
Linus Torvaldsb2311252009-06-16 11:07:14 -07004071 printk(KERN_DEFAULT "Modules linked in:");
Andi Kleend72b3752008-08-30 10:09:00 +02004072 /* Most callers should already have preempt disabled, but make sure */
4073 preempt_disable();
Rusty Russell0d21b0e2013-01-12 11:38:44 +10304074 list_for_each_entry_rcu(mod, &modules, list) {
4075 if (mod->state == MODULE_STATE_UNFORMED)
4076 continue;
Jiri Slaby27bba4d2014-02-03 11:13:13 +10304077 pr_cont(" %s%s", mod->name, module_flags(mod, buf));
Rusty Russell0d21b0e2013-01-12 11:38:44 +10304078 }
Andi Kleend72b3752008-08-30 10:09:00 +02004079 preempt_enable();
Arjan van de Vene14af7e2008-01-25 21:08:33 +01004080 if (last_unloaded_module[0])
Jiri Slaby27bba4d2014-02-03 11:13:13 +10304081 pr_cont(" [last unloaded: %s]", last_unloaded_module);
4082 pr_cont("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004083}
4084
Linus Torvalds1da177e2005-04-16 15:20:36 -07004085#ifdef CONFIG_MODVERSIONS
Rusty Russell8c8ef422009-03-31 13:05:34 -06004086/* Generate the signature for all relevant module structures here.
4087 * If these change, we don't want to try to parse the module. */
4088void module_layout(struct module *mod,
4089 struct modversion_info *ver,
4090 struct kernel_param *kp,
4091 struct kernel_symbol *ks,
Mathieu Desnoyers65498642011-01-26 17:26:22 -05004092 struct tracepoint * const *tp)
Rusty Russell8c8ef422009-03-31 13:05:34 -06004093{
4094}
4095EXPORT_SYMBOL(module_layout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004096#endif