blob: 5432dbedf8cf87bb85a53ba4276951914d9e6f2a [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 Rostedt (Red Hat)af658dc2015-04-29 14:36:05 -040021#include <linux/trace_events.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>
Jessica Yu7e545d62016-03-16 20:55:39 -040056#include <linux/livepatch.h>
Arjan van de Ven22a9d642009-01-07 08:45:46 -080057#include <linux/async.h>
Tejun Heofbf59bc2009-02-20 16:29:08 +090058#include <linux/percpu.h>
Catalin Marinas4f2294b2009-06-11 13:23:20 +010059#include <linux/kmemleak.h>
Jason Baronbf5438fc2010-09-17 11:09:00 -040060#include <linux/jump_label.h>
matthieu castet84e1c6b2010-11-16 22:35:16 +010061#include <linux/pfn.h>
Alessio Igor Bogani403ed272011-04-20 11:10:52 +020062#include <linux/bsearch.h>
Luis de Bethencourt9d5059c2016-08-02 14:03:47 -070063#include <linux/dynamic_debug.h>
Richard Guy Briggsca86cad2017-02-04 13:10:38 -050064#include <linux/audit.h>
Rusty Russell2f3238a2012-10-22 18:09:41 +103065#include <uapi/linux/module.h>
Rusty Russell106a4ee2012-09-26 10:09:40 +010066#include "module-internal.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070067
Li Zefan7ead8b82009-08-17 16:56:28 +080068#define CREATE_TRACE_POINTS
69#include <trace/events/module.h>
70
Linus Torvalds1da177e2005-04-16 15:20:36 -070071#ifndef ARCH_SHF_SMALL
72#define ARCH_SHF_SMALL 0
73#endif
74
matthieu castet84e1c6b2010-11-16 22:35:16 +010075/*
76 * Modules' sections will be aligned on page boundaries
77 * to ensure complete separation of code and data, but
78 * only when CONFIG_DEBUG_SET_MODULE_RONX=y
79 */
80#ifdef CONFIG_DEBUG_SET_MODULE_RONX
81# define debug_align(X) ALIGN(X, PAGE_SIZE)
82#else
83# define debug_align(X) (X)
84#endif
85
Linus Torvalds1da177e2005-04-16 15:20:36 -070086/* If this is set, the section belongs in the init part of the module */
87#define INIT_OFFSET_MASK (1UL << (BITS_PER_LONG-1))
88
Rusty Russell75676502010-06-05 11:17:36 -060089/*
90 * Mutex protects:
91 * 1) List of modules (also safely readable with preempt_disable),
92 * 2) module_use links,
93 * 3) module_addr_min/module_addr_max.
Masami Hiramatsue513cc12014-11-10 09:30:29 +103094 * (delete and add uses RCU list operations). */
Tim Abbottc6b37802008-12-05 19:03:59 -050095DEFINE_MUTEX(module_mutex);
96EXPORT_SYMBOL_GPL(module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070097static LIST_HEAD(modules);
Peter Zijlstra93c2e102015-05-27 11:09:37 +093098
Peter Zijlstra6c9692e2015-05-27 11:09:37 +093099#ifdef CONFIG_MODULES_TREE_LOOKUP
100
Peter Zijlstra93c2e102015-05-27 11:09:37 +0930101/*
102 * Use a latched RB-tree for __module_address(); this allows us to use
103 * RCU-sched lookups of the address from any context.
104 *
Peter Zijlstra6c9692e2015-05-27 11:09:37 +0930105 * This is conditional on PERF_EVENTS || TRACING because those can really hit
106 * __module_address() hard by doing a lot of stack unwinding; potentially from
107 * NMI context.
Peter Zijlstra93c2e102015-05-27 11:09:37 +0930108 */
109
110static __always_inline unsigned long __mod_tree_val(struct latch_tree_node *n)
111{
Rusty Russell7523e4d2015-11-26 09:44:08 +1030112 struct module_layout *layout = container_of(n, struct module_layout, mtn.node);
Peter Zijlstra93c2e102015-05-27 11:09:37 +0930113
Rusty Russell7523e4d2015-11-26 09:44:08 +1030114 return (unsigned long)layout->base;
Peter Zijlstra93c2e102015-05-27 11:09:37 +0930115}
116
117static __always_inline unsigned long __mod_tree_size(struct latch_tree_node *n)
118{
Rusty Russell7523e4d2015-11-26 09:44:08 +1030119 struct module_layout *layout = container_of(n, struct module_layout, mtn.node);
Peter Zijlstra93c2e102015-05-27 11:09:37 +0930120
Rusty Russell7523e4d2015-11-26 09:44:08 +1030121 return (unsigned long)layout->size;
Peter Zijlstra93c2e102015-05-27 11:09:37 +0930122}
123
124static __always_inline bool
125mod_tree_less(struct latch_tree_node *a, struct latch_tree_node *b)
126{
127 return __mod_tree_val(a) < __mod_tree_val(b);
128}
129
130static __always_inline int
131mod_tree_comp(void *key, struct latch_tree_node *n)
132{
133 unsigned long val = (unsigned long)key;
134 unsigned long start, end;
135
136 start = __mod_tree_val(n);
137 if (val < start)
138 return -1;
139
140 end = start + __mod_tree_size(n);
141 if (val >= end)
142 return 1;
143
144 return 0;
145}
146
147static const struct latch_tree_ops mod_tree_ops = {
148 .less = mod_tree_less,
149 .comp = mod_tree_comp,
150};
151
Peter Zijlstra4f6665462015-05-27 11:09:38 +0930152static struct mod_tree_root {
153 struct latch_tree_root root;
154 unsigned long addr_min;
155 unsigned long addr_max;
156} mod_tree __cacheline_aligned = {
157 .addr_min = -1UL,
158};
159
160#define module_addr_min mod_tree.addr_min
161#define module_addr_max mod_tree.addr_max
162
163static noinline void __mod_tree_insert(struct mod_tree_node *node)
164{
165 latch_tree_insert(&node->node, &mod_tree.root, &mod_tree_ops);
166}
167
168static void __mod_tree_remove(struct mod_tree_node *node)
169{
170 latch_tree_erase(&node->node, &mod_tree.root, &mod_tree_ops);
171}
Peter Zijlstra93c2e102015-05-27 11:09:37 +0930172
173/*
174 * These modifications: insert, remove_init and remove; are serialized by the
175 * module_mutex.
176 */
177static void mod_tree_insert(struct module *mod)
178{
Rusty Russell7523e4d2015-11-26 09:44:08 +1030179 mod->core_layout.mtn.mod = mod;
180 mod->init_layout.mtn.mod = mod;
Peter Zijlstra93c2e102015-05-27 11:09:37 +0930181
Rusty Russell7523e4d2015-11-26 09:44:08 +1030182 __mod_tree_insert(&mod->core_layout.mtn);
183 if (mod->init_layout.size)
184 __mod_tree_insert(&mod->init_layout.mtn);
Peter Zijlstra93c2e102015-05-27 11:09:37 +0930185}
186
187static void mod_tree_remove_init(struct module *mod)
188{
Rusty Russell7523e4d2015-11-26 09:44:08 +1030189 if (mod->init_layout.size)
190 __mod_tree_remove(&mod->init_layout.mtn);
Peter Zijlstra93c2e102015-05-27 11:09:37 +0930191}
192
193static void mod_tree_remove(struct module *mod)
194{
Rusty Russell7523e4d2015-11-26 09:44:08 +1030195 __mod_tree_remove(&mod->core_layout.mtn);
Peter Zijlstra93c2e102015-05-27 11:09:37 +0930196 mod_tree_remove_init(mod);
197}
198
Peter Zijlstra6c9692e2015-05-27 11:09:37 +0930199static struct module *mod_find(unsigned long addr)
Peter Zijlstra93c2e102015-05-27 11:09:37 +0930200{
201 struct latch_tree_node *ltn;
202
Peter Zijlstra4f6665462015-05-27 11:09:38 +0930203 ltn = latch_tree_find((void *)addr, &mod_tree.root, &mod_tree_ops);
Peter Zijlstra93c2e102015-05-27 11:09:37 +0930204 if (!ltn)
205 return NULL;
206
207 return container_of(ltn, struct mod_tree_node, node)->mod;
208}
209
Peter Zijlstra6c9692e2015-05-27 11:09:37 +0930210#else /* MODULES_TREE_LOOKUP */
211
Peter Zijlstra4f6665462015-05-27 11:09:38 +0930212static unsigned long module_addr_min = -1UL, module_addr_max = 0;
213
Peter Zijlstra6c9692e2015-05-27 11:09:37 +0930214static void mod_tree_insert(struct module *mod) { }
215static void mod_tree_remove_init(struct module *mod) { }
216static void mod_tree_remove(struct module *mod) { }
217
218static struct module *mod_find(unsigned long addr)
219{
220 struct module *mod;
221
222 list_for_each_entry_rcu(mod, &modules, list) {
223 if (within_module(addr, mod))
224 return mod;
225 }
226
227 return NULL;
228}
229
230#endif /* MODULES_TREE_LOOKUP */
231
Peter Zijlstra4f6665462015-05-27 11:09:38 +0930232/*
233 * Bounds of module text, for speeding up __module_address.
234 * Protected by module_mutex.
235 */
236static void __mod_update_bounds(void *base, unsigned int size)
237{
238 unsigned long min = (unsigned long)base;
239 unsigned long max = min + size;
240
241 if (min < module_addr_min)
242 module_addr_min = min;
243 if (max > module_addr_max)
244 module_addr_max = max;
245}
246
247static void mod_update_bounds(struct module *mod)
248{
Rusty Russell7523e4d2015-11-26 09:44:08 +1030249 __mod_update_bounds(mod->core_layout.base, mod->core_layout.size);
250 if (mod->init_layout.size)
251 __mod_update_bounds(mod->init_layout.base, mod->init_layout.size);
Peter Zijlstra4f6665462015-05-27 11:09:38 +0930252}
253
Jason Wessel67fc4e02010-05-20 21:04:21 -0500254#ifdef CONFIG_KGDB_KDB
255struct list_head *kdb_modules = &modules; /* kdb needs the list of modules */
256#endif /* CONFIG_KGDB_KDB */
257
Peter Zijlstra0be964b2015-05-27 11:09:35 +0930258static void module_assert_mutex(void)
Rusty Russell106a4ee2012-09-26 10:09:40 +0100259{
Peter Zijlstra0be964b2015-05-27 11:09:35 +0930260 lockdep_assert_held(&module_mutex);
Rusty Russell106a4ee2012-09-26 10:09:40 +0100261}
262
Peter Zijlstra0be964b2015-05-27 11:09:35 +0930263static void module_assert_mutex_or_preempt(void)
264{
265#ifdef CONFIG_LOCKDEP
266 if (unlikely(!debug_locks))
267 return;
Rusty Russell106a4ee2012-09-26 10:09:40 +0100268
Steven Rostedt95025142016-07-19 05:59:24 +0930269 WARN_ON_ONCE(!rcu_read_lock_sched_held() &&
Peter Zijlstra0be964b2015-05-27 11:09:35 +0930270 !lockdep_is_held(&module_mutex));
271#endif
272}
273
Luis R. Rodriguez6727bb92015-05-27 11:09:39 +0930274static bool sig_enforce = IS_ENABLED(CONFIG_MODULE_SIG_FORCE);
275#ifndef CONFIG_MODULE_SIG_FORCE
Rusty Russell106a4ee2012-09-26 10:09:40 +0100276module_param(sig_enforce, bool_enable_only, 0644);
277#endif /* !CONFIG_MODULE_SIG_FORCE */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278
Stephen Rothwell19e45292009-04-14 17:27:18 +1000279/* Block module loading/unloading? */
280int modules_disabled = 0;
Dave Young02608be2012-02-01 10:33:14 +0800281core_param(nomodule, modules_disabled, bint, 0);
Stephen Rothwell19e45292009-04-14 17:27:18 +1000282
Rusty Russellc9a3ba52008-01-29 17:13:18 -0500283/* Waiting for a module to finish initializing? */
284static DECLARE_WAIT_QUEUE_HEAD(module_wq);
285
Alan Sterne041c682006-03-27 01:16:30 -0800286static BLOCKING_NOTIFIER_HEAD(module_notify_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287
Ionut Alexa6da0b562014-11-10 09:31:29 +1030288int register_module_notifier(struct notifier_block *nb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289{
Alan Sterne041c682006-03-27 01:16:30 -0800290 return blocking_notifier_chain_register(&module_notify_list, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291}
292EXPORT_SYMBOL(register_module_notifier);
293
Ionut Alexa6da0b562014-11-10 09:31:29 +1030294int unregister_module_notifier(struct notifier_block *nb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295{
Alan Sterne041c682006-03-27 01:16:30 -0800296 return blocking_notifier_chain_unregister(&module_notify_list, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297}
298EXPORT_SYMBOL(unregister_module_notifier);
299
Rusty Russelleded41c2010-08-05 12:59:07 -0600300struct load_info {
301 Elf_Ehdr *hdr;
302 unsigned long len;
303 Elf_Shdr *sechdrs;
Rusty Russell6526c532010-08-05 12:59:10 -0600304 char *secstrings, *strtab;
Rusty Russelld9131882010-08-05 12:59:08 -0600305 unsigned long symoffs, stroffs;
Rusty Russell811d66a2010-08-05 12:59:12 -0600306 struct _ddebug *debug;
307 unsigned int num_debug;
Rusty Russell106a4ee2012-09-26 10:09:40 +0100308 bool sig_ok;
Rusty Russell82440622016-02-03 16:55:26 +1030309#ifdef CONFIG_KALLSYMS
310 unsigned long mod_kallsyms_init_off;
311#endif
Rusty Russelleded41c2010-08-05 12:59:07 -0600312 struct {
313 unsigned int sym, str, mod, vers, info, pcpu;
314 } index;
315};
316
Matti Linnanvuori9a4b9702007-11-08 08:37:38 -0800317/* We require a truly strong try_module_get(): 0 means failure due to
318 ongoing or failed initialization etc. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319static inline int strong_try_module_get(struct module *mod)
320{
Rusty Russell0d21b0e2013-01-12 11:38:44 +1030321 BUG_ON(mod && mod->state == MODULE_STATE_UNFORMED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 if (mod && mod->state == MODULE_STATE_COMING)
Rusty Russellc9a3ba52008-01-29 17:13:18 -0500323 return -EBUSY;
324 if (try_module_get(mod))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 return 0;
Rusty Russellc9a3ba52008-01-29 17:13:18 -0500326 else
327 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328}
329
Rusty Russell373d4d02013-01-21 17:17:39 +1030330static inline void add_taint_module(struct module *mod, unsigned flag,
331 enum lockdep_ok lockdep_ok)
Florin Malitafa3ba2e82006-10-11 01:21:48 -0700332{
Rusty Russell373d4d02013-01-21 17:17:39 +1030333 add_taint(flag, lockdep_ok);
Andi Kleen25ddbb12008-10-15 22:01:41 -0700334 mod->taints |= (1U << flag);
Florin Malitafa3ba2e82006-10-11 01:21:48 -0700335}
336
Robert P. J. Day02a3e592007-05-09 07:26:28 +0200337/*
338 * A thread that wants to hold a reference to a module only while it
339 * is running can call this to safely exit. nfsd and lockd use this.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 */
Jiri Kosinabf262dc2016-04-12 05:02:09 +0930341void __noreturn __module_put_and_exit(struct module *mod, long code)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342{
343 module_put(mod);
344 do_exit(code);
345}
346EXPORT_SYMBOL(__module_put_and_exit);
Daniel Walker22a8bde2007-10-18 03:06:07 -0700347
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348/* Find a module section: 0 means not found. */
Rusty Russell49668682010-08-05 12:59:10 -0600349static unsigned int find_sec(const struct load_info *info, const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350{
351 unsigned int i;
352
Rusty Russell49668682010-08-05 12:59:10 -0600353 for (i = 1; i < info->hdr->e_shnum; i++) {
354 Elf_Shdr *shdr = &info->sechdrs[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 /* Alloc bit cleared means "ignore it." */
Rusty Russell49668682010-08-05 12:59:10 -0600356 if ((shdr->sh_flags & SHF_ALLOC)
357 && strcmp(info->secstrings + shdr->sh_name, name) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 return i;
Rusty Russell49668682010-08-05 12:59:10 -0600359 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 return 0;
361}
362
Rusty Russell5e458cc2008-10-22 10:00:13 -0500363/* Find a module section, or NULL. */
Rusty Russell49668682010-08-05 12:59:10 -0600364static void *section_addr(const struct load_info *info, const char *name)
Rusty Russell5e458cc2008-10-22 10:00:13 -0500365{
366 /* Section 0 has sh_addr 0. */
Rusty Russell49668682010-08-05 12:59:10 -0600367 return (void *)info->sechdrs[find_sec(info, name)].sh_addr;
Rusty Russell5e458cc2008-10-22 10:00:13 -0500368}
369
370/* Find a module section, or NULL. Fill in number of "objects" in section. */
Rusty Russell49668682010-08-05 12:59:10 -0600371static void *section_objs(const struct load_info *info,
Rusty Russell5e458cc2008-10-22 10:00:13 -0500372 const char *name,
373 size_t object_size,
374 unsigned int *num)
375{
Rusty Russell49668682010-08-05 12:59:10 -0600376 unsigned int sec = find_sec(info, name);
Rusty Russell5e458cc2008-10-22 10:00:13 -0500377
378 /* Section 0 has sh_addr 0 and sh_size 0. */
Rusty Russell49668682010-08-05 12:59:10 -0600379 *num = info->sechdrs[sec].sh_size / object_size;
380 return (void *)info->sechdrs[sec].sh_addr;
Rusty Russell5e458cc2008-10-22 10:00:13 -0500381}
382
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383/* Provided by the linker */
384extern const struct kernel_symbol __start___ksymtab[];
385extern const struct kernel_symbol __stop___ksymtab[];
386extern const struct kernel_symbol __start___ksymtab_gpl[];
387extern const struct kernel_symbol __stop___ksymtab_gpl[];
Greg Kroah-Hartman9f28bb72006-03-20 13:17:13 -0800388extern const struct kernel_symbol __start___ksymtab_gpl_future[];
389extern const struct kernel_symbol __stop___ksymtab_gpl_future[];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390extern const unsigned long __start___kcrctab[];
391extern const unsigned long __start___kcrctab_gpl[];
Greg Kroah-Hartman9f28bb72006-03-20 13:17:13 -0800392extern const unsigned long __start___kcrctab_gpl_future[];
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500393#ifdef CONFIG_UNUSED_SYMBOLS
394extern const struct kernel_symbol __start___ksymtab_unused[];
395extern const struct kernel_symbol __stop___ksymtab_unused[];
396extern const struct kernel_symbol __start___ksymtab_unused_gpl[];
397extern const struct kernel_symbol __stop___ksymtab_unused_gpl[];
Arjan van de Venf71d20e2006-06-28 04:26:45 -0700398extern const unsigned long __start___kcrctab_unused[];
399extern const unsigned long __start___kcrctab_unused_gpl[];
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500400#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401
402#ifndef CONFIG_MODVERSIONS
403#define symversion(base, idx) NULL
404#else
Andrew Mortonf83ca9f2006-03-28 01:56:20 -0800405#define symversion(base, idx) ((base != NULL) ? ((base) + (idx)) : NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406#endif
407
Rusty Russelldafd0942008-07-22 19:24:25 -0500408static bool each_symbol_in_section(const struct symsearch *arr,
409 unsigned int arrsize,
410 struct module *owner,
411 bool (*fn)(const struct symsearch *syms,
412 struct module *owner,
Rusty Russellde4d8d52011-04-19 21:49:58 +0200413 void *data),
Rusty Russelldafd0942008-07-22 19:24:25 -0500414 void *data)
Sam Ravnborg3fd68052006-02-08 21:16:45 +0100415{
Rusty Russellde4d8d52011-04-19 21:49:58 +0200416 unsigned int j;
Rusty Russelldafd0942008-07-22 19:24:25 -0500417
418 for (j = 0; j < arrsize; j++) {
Rusty Russellde4d8d52011-04-19 21:49:58 +0200419 if (fn(&arr[j], owner, data))
420 return true;
Rusty Russelldafd0942008-07-22 19:24:25 -0500421 }
422
423 return false;
Sam Ravnborg3fd68052006-02-08 21:16:45 +0100424}
425
Rusty Russelldafd0942008-07-22 19:24:25 -0500426/* Returns true as soon as fn returns true, otherwise false. */
Rusty Russellde4d8d52011-04-19 21:49:58 +0200427bool each_symbol_section(bool (*fn)(const struct symsearch *arr,
428 struct module *owner,
429 void *data),
430 void *data)
Arjan van de Venf71d20e2006-06-28 04:26:45 -0700431{
Rusty Russelldafd0942008-07-22 19:24:25 -0500432 struct module *mod;
Linus Torvalds44032e62010-08-05 12:59:05 -0600433 static const struct symsearch arr[] = {
Rusty Russelldafd0942008-07-22 19:24:25 -0500434 { __start___ksymtab, __stop___ksymtab, __start___kcrctab,
435 NOT_GPL_ONLY, false },
436 { __start___ksymtab_gpl, __stop___ksymtab_gpl,
437 __start___kcrctab_gpl,
438 GPL_ONLY, false },
439 { __start___ksymtab_gpl_future, __stop___ksymtab_gpl_future,
440 __start___kcrctab_gpl_future,
441 WILL_BE_GPL_ONLY, false },
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500442#ifdef CONFIG_UNUSED_SYMBOLS
Rusty Russelldafd0942008-07-22 19:24:25 -0500443 { __start___ksymtab_unused, __stop___ksymtab_unused,
444 __start___kcrctab_unused,
445 NOT_GPL_ONLY, true },
446 { __start___ksymtab_unused_gpl, __stop___ksymtab_unused_gpl,
447 __start___kcrctab_unused_gpl,
448 GPL_ONLY, true },
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500449#endif
Rusty Russelldafd0942008-07-22 19:24:25 -0500450 };
451
Peter Zijlstra0be964b2015-05-27 11:09:35 +0930452 module_assert_mutex_or_preempt();
453
Rusty Russelldafd0942008-07-22 19:24:25 -0500454 if (each_symbol_in_section(arr, ARRAY_SIZE(arr), NULL, fn, data))
455 return true;
456
Andi Kleend72b3752008-08-30 10:09:00 +0200457 list_for_each_entry_rcu(mod, &modules, list) {
Rusty Russelldafd0942008-07-22 19:24:25 -0500458 struct symsearch arr[] = {
459 { mod->syms, mod->syms + mod->num_syms, mod->crcs,
460 NOT_GPL_ONLY, false },
461 { mod->gpl_syms, mod->gpl_syms + mod->num_gpl_syms,
462 mod->gpl_crcs,
463 GPL_ONLY, false },
464 { mod->gpl_future_syms,
465 mod->gpl_future_syms + mod->num_gpl_future_syms,
466 mod->gpl_future_crcs,
467 WILL_BE_GPL_ONLY, false },
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500468#ifdef CONFIG_UNUSED_SYMBOLS
Rusty Russelldafd0942008-07-22 19:24:25 -0500469 { mod->unused_syms,
470 mod->unused_syms + mod->num_unused_syms,
471 mod->unused_crcs,
472 NOT_GPL_ONLY, true },
473 { mod->unused_gpl_syms,
474 mod->unused_gpl_syms + mod->num_unused_gpl_syms,
475 mod->unused_gpl_crcs,
476 GPL_ONLY, true },
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500477#endif
Rusty Russelldafd0942008-07-22 19:24:25 -0500478 };
479
Rusty Russell0d21b0e2013-01-12 11:38:44 +1030480 if (mod->state == MODULE_STATE_UNFORMED)
481 continue;
482
Rusty Russelldafd0942008-07-22 19:24:25 -0500483 if (each_symbol_in_section(arr, ARRAY_SIZE(arr), mod, fn, data))
484 return true;
485 }
486 return false;
Arjan van de Venf71d20e2006-06-28 04:26:45 -0700487}
Rusty Russellde4d8d52011-04-19 21:49:58 +0200488EXPORT_SYMBOL_GPL(each_symbol_section);
Arjan van de Venf71d20e2006-06-28 04:26:45 -0700489
Rusty Russelldafd0942008-07-22 19:24:25 -0500490struct find_symbol_arg {
491 /* Input */
492 const char *name;
493 bool gplok;
494 bool warn;
495
496 /* Output */
497 struct module *owner;
498 const unsigned long *crc;
Tim Abbott414fd312008-12-05 19:03:56 -0500499 const struct kernel_symbol *sym;
Rusty Russelldafd0942008-07-22 19:24:25 -0500500};
501
Rusty Russellde4d8d52011-04-19 21:49:58 +0200502static bool check_symbol(const struct symsearch *syms,
503 struct module *owner,
504 unsigned int symnum, void *data)
Rusty Russellad9546c2008-05-01 21:14:59 -0500505{
Rusty Russelldafd0942008-07-22 19:24:25 -0500506 struct find_symbol_arg *fsa = data;
507
Rusty Russelldafd0942008-07-22 19:24:25 -0500508 if (!fsa->gplok) {
509 if (syms->licence == GPL_ONLY)
510 return false;
511 if (syms->licence == WILL_BE_GPL_ONLY && fsa->warn) {
Andrew Mortonbddb12b2013-11-12 15:11:28 -0800512 pr_warn("Symbol %s is being used by a non-GPL module, "
513 "which will not be allowed in the future\n",
514 fsa->name);
Rusty Russelldafd0942008-07-22 19:24:25 -0500515 }
516 }
517
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500518#ifdef CONFIG_UNUSED_SYMBOLS
Rusty Russelldafd0942008-07-22 19:24:25 -0500519 if (syms->unused && fsa->warn) {
Andrew Mortonbddb12b2013-11-12 15:11:28 -0800520 pr_warn("Symbol %s is marked as UNUSED, however this module is "
521 "using it.\n", fsa->name);
522 pr_warn("This symbol will go away in the future.\n");
Yannick Guerrini7b63c3a2015-03-24 12:31:40 +1030523 pr_warn("Please evaluate if this is the right api to use and "
524 "if it really is, submit a report to the linux kernel "
525 "mailing list together with submitting your code for "
Andrew Mortonbddb12b2013-11-12 15:11:28 -0800526 "inclusion.\n");
Rusty Russellad9546c2008-05-01 21:14:59 -0500527 }
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500528#endif
Rusty Russelldafd0942008-07-22 19:24:25 -0500529
530 fsa->owner = owner;
531 fsa->crc = symversion(syms->crcs, symnum);
Tim Abbott414fd312008-12-05 19:03:56 -0500532 fsa->sym = &syms->start[symnum];
Rusty Russellad9546c2008-05-01 21:14:59 -0500533 return true;
534}
535
Alessio Igor Bogani403ed272011-04-20 11:10:52 +0200536static int cmp_name(const void *va, const void *vb)
537{
538 const char *a;
539 const struct kernel_symbol *b;
540 a = va; b = vb;
541 return strcmp(a, b->name);
542}
543
Rusty Russellde4d8d52011-04-19 21:49:58 +0200544static bool find_symbol_in_section(const struct symsearch *syms,
545 struct module *owner,
546 void *data)
547{
548 struct find_symbol_arg *fsa = data;
Alessio Igor Bogani403ed272011-04-20 11:10:52 +0200549 struct kernel_symbol *sym;
Rusty Russellde4d8d52011-04-19 21:49:58 +0200550
Alessio Igor Bogani403ed272011-04-20 11:10:52 +0200551 sym = bsearch(fsa->name, syms->start, syms->stop - syms->start,
552 sizeof(struct kernel_symbol), cmp_name);
553
554 if (sym != NULL && check_symbol(syms, owner, sym - syms->start, data))
555 return true;
556
Rusty Russellde4d8d52011-04-19 21:49:58 +0200557 return false;
558}
559
Tim Abbott414fd312008-12-05 19:03:56 -0500560/* Find a symbol and return it, along with, (optional) crc and
Rusty Russell75676502010-06-05 11:17:36 -0600561 * (optional) module which owns it. Needs preempt disabled or module_mutex. */
Tim Abbottc6b37802008-12-05 19:03:59 -0500562const struct kernel_symbol *find_symbol(const char *name,
563 struct module **owner,
564 const unsigned long **crc,
565 bool gplok,
566 bool warn)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567{
Rusty Russelldafd0942008-07-22 19:24:25 -0500568 struct find_symbol_arg fsa;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569
Rusty Russelldafd0942008-07-22 19:24:25 -0500570 fsa.name = name;
571 fsa.gplok = gplok;
572 fsa.warn = warn;
573
Rusty Russellde4d8d52011-04-19 21:49:58 +0200574 if (each_symbol_section(find_symbol_in_section, &fsa)) {
Rusty Russellad9546c2008-05-01 21:14:59 -0500575 if (owner)
Rusty Russelldafd0942008-07-22 19:24:25 -0500576 *owner = fsa.owner;
577 if (crc)
578 *crc = fsa.crc;
Tim Abbott414fd312008-12-05 19:03:56 -0500579 return fsa.sym;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 }
Rusty Russellad9546c2008-05-01 21:14:59 -0500581
Jim Cromie5e124162011-12-06 12:11:31 -0700582 pr_debug("Failed to find symbol %s\n", name);
Tim Abbott414fd312008-12-05 19:03:56 -0500583 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584}
Tim Abbottc6b37802008-12-05 19:03:59 -0500585EXPORT_SYMBOL_GPL(find_symbol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586
Rusty Russellfe0d34d2015-07-29 05:52:14 +0930587/*
588 * Search for module by name: must hold module_mutex (or preempt disabled
589 * for read-only access).
590 */
Mathias Krause4f6de4d52013-07-02 15:35:11 +0930591static struct module *find_module_all(const char *name, size_t len,
Rusty Russell0d21b0e2013-01-12 11:38:44 +1030592 bool even_unformed)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593{
594 struct module *mod;
595
Rusty Russellfe0d34d2015-07-29 05:52:14 +0930596 module_assert_mutex_or_preempt();
Peter Zijlstra0be964b2015-05-27 11:09:35 +0930597
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 list_for_each_entry(mod, &modules, list) {
Rusty Russell0d21b0e2013-01-12 11:38:44 +1030599 if (!even_unformed && mod->state == MODULE_STATE_UNFORMED)
600 continue;
Mathias Krause4f6de4d52013-07-02 15:35:11 +0930601 if (strlen(mod->name) == len && !memcmp(mod->name, name, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 return mod;
603 }
604 return NULL;
605}
Rusty Russell0d21b0e2013-01-12 11:38:44 +1030606
607struct module *find_module(const char *name)
608{
Rusty Russellfe0d34d2015-07-29 05:52:14 +0930609 module_assert_mutex();
Mathias Krause4f6de4d52013-07-02 15:35:11 +0930610 return find_module_all(name, strlen(name), false);
Rusty Russell0d21b0e2013-01-12 11:38:44 +1030611}
Tim Abbottc6b37802008-12-05 19:03:59 -0500612EXPORT_SYMBOL_GPL(find_module);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613
614#ifdef CONFIG_SMP
Tejun Heofbf59bc2009-02-20 16:29:08 +0900615
Tejun Heo259354d2010-03-10 18:56:10 +0900616static inline void __percpu *mod_percpu(struct module *mod)
Tejun Heofbf59bc2009-02-20 16:29:08 +0900617{
Tejun Heo259354d2010-03-10 18:56:10 +0900618 return mod->percpu;
619}
Tejun Heofbf59bc2009-02-20 16:29:08 +0900620
Rusty Russell9eb76d72013-07-03 10:06:29 +0930621static int percpu_modalloc(struct module *mod, struct load_info *info)
Tejun Heo259354d2010-03-10 18:56:10 +0900622{
Rusty Russell9eb76d72013-07-03 10:06:29 +0930623 Elf_Shdr *pcpusec = &info->sechdrs[info->index.pcpu];
624 unsigned long align = pcpusec->sh_addralign;
625
626 if (!pcpusec->sh_size)
627 return 0;
628
Tejun Heofbf59bc2009-02-20 16:29:08 +0900629 if (align > PAGE_SIZE) {
Andrew Mortonbddb12b2013-11-12 15:11:28 -0800630 pr_warn("%s: per-cpu alignment %li > %li\n",
631 mod->name, align, PAGE_SIZE);
Tejun Heofbf59bc2009-02-20 16:29:08 +0900632 align = PAGE_SIZE;
633 }
634
Rusty Russell9eb76d72013-07-03 10:06:29 +0930635 mod->percpu = __alloc_reserved_percpu(pcpusec->sh_size, align);
Tejun Heo259354d2010-03-10 18:56:10 +0900636 if (!mod->percpu) {
Andrew Mortonbddb12b2013-11-12 15:11:28 -0800637 pr_warn("%s: Could not allocate %lu bytes percpu data\n",
638 mod->name, (unsigned long)pcpusec->sh_size);
Tejun Heo259354d2010-03-10 18:56:10 +0900639 return -ENOMEM;
640 }
Rusty Russell9eb76d72013-07-03 10:06:29 +0930641 mod->percpu_size = pcpusec->sh_size;
Tejun Heo259354d2010-03-10 18:56:10 +0900642 return 0;
Tejun Heofbf59bc2009-02-20 16:29:08 +0900643}
644
Tejun Heo259354d2010-03-10 18:56:10 +0900645static void percpu_modfree(struct module *mod)
Tejun Heofbf59bc2009-02-20 16:29:08 +0900646{
Tejun Heo259354d2010-03-10 18:56:10 +0900647 free_percpu(mod->percpu);
Tejun Heofbf59bc2009-02-20 16:29:08 +0900648}
649
Rusty Russell49668682010-08-05 12:59:10 -0600650static unsigned int find_pcpusec(struct load_info *info)
Tejun Heo6b588c12009-02-20 16:29:07 +0900651{
Rusty Russell49668682010-08-05 12:59:10 -0600652 return find_sec(info, ".data..percpu");
Tejun Heo6b588c12009-02-20 16:29:07 +0900653}
654
Tejun Heo259354d2010-03-10 18:56:10 +0900655static void percpu_modcopy(struct module *mod,
656 const void *from, unsigned long size)
Tejun Heo6b588c12009-02-20 16:29:07 +0900657{
658 int cpu;
659
660 for_each_possible_cpu(cpu)
Tejun Heo259354d2010-03-10 18:56:10 +0900661 memcpy(per_cpu_ptr(mod->percpu, cpu), from, size);
Tejun Heo6b588c12009-02-20 16:29:07 +0900662}
663
Tejun Heo10fad5e2010-03-10 18:57:54 +0900664/**
665 * is_module_percpu_address - test whether address is from module static percpu
666 * @addr: address to test
667 *
668 * Test whether @addr belongs to module static percpu area.
669 *
670 * RETURNS:
671 * %true if @addr is from module static percpu area
672 */
673bool is_module_percpu_address(unsigned long addr)
674{
675 struct module *mod;
676 unsigned int cpu;
677
678 preempt_disable();
679
680 list_for_each_entry_rcu(mod, &modules, list) {
Rusty Russell0d21b0e2013-01-12 11:38:44 +1030681 if (mod->state == MODULE_STATE_UNFORMED)
682 continue;
Tejun Heo10fad5e2010-03-10 18:57:54 +0900683 if (!mod->percpu_size)
684 continue;
685 for_each_possible_cpu(cpu) {
686 void *start = per_cpu_ptr(mod->percpu, cpu);
687
688 if ((void *)addr >= start &&
689 (void *)addr < start + mod->percpu_size) {
690 preempt_enable();
691 return true;
692 }
693 }
694 }
695
696 preempt_enable();
697 return false;
Daniel Walker22a8bde2007-10-18 03:06:07 -0700698}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699
700#else /* ... !CONFIG_SMP */
Tejun Heo6b588c12009-02-20 16:29:07 +0900701
Tejun Heo259354d2010-03-10 18:56:10 +0900702static inline void __percpu *mod_percpu(struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703{
704 return NULL;
705}
Rusty Russell9eb76d72013-07-03 10:06:29 +0930706static int percpu_modalloc(struct module *mod, struct load_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707{
Rusty Russell9eb76d72013-07-03 10:06:29 +0930708 /* UP modules shouldn't have this section: ENOMEM isn't quite right */
709 if (info->sechdrs[info->index.pcpu].sh_size != 0)
710 return -ENOMEM;
711 return 0;
Tejun Heo259354d2010-03-10 18:56:10 +0900712}
713static inline void percpu_modfree(struct module *mod)
714{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715}
Rusty Russell49668682010-08-05 12:59:10 -0600716static unsigned int find_pcpusec(struct load_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717{
718 return 0;
719}
Tejun Heo259354d2010-03-10 18:56:10 +0900720static inline void percpu_modcopy(struct module *mod,
721 const void *from, unsigned long size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722{
723 /* pcpusec should be 0, and size of that section should be 0. */
724 BUG_ON(size != 0);
725}
Tejun Heo10fad5e2010-03-10 18:57:54 +0900726bool is_module_percpu_address(unsigned long addr)
727{
728 return false;
729}
Tejun Heo6b588c12009-02-20 16:29:07 +0900730
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731#endif /* CONFIG_SMP */
732
Matt Domschc988d2b2005-06-23 22:05:15 -0700733#define MODINFO_ATTR(field) \
734static void setup_modinfo_##field(struct module *mod, const char *s) \
735{ \
736 mod->field = kstrdup(s, GFP_KERNEL); \
737} \
738static ssize_t show_modinfo_##field(struct module_attribute *mattr, \
Kay Sievers4befb022011-07-24 22:06:04 +0930739 struct module_kobject *mk, char *buffer) \
Matt Domschc988d2b2005-06-23 22:05:15 -0700740{ \
Chen Gangcc56ded2013-08-20 15:34:21 +0930741 return scnprintf(buffer, PAGE_SIZE, "%s\n", mk->mod->field); \
Matt Domschc988d2b2005-06-23 22:05:15 -0700742} \
743static int modinfo_##field##_exists(struct module *mod) \
744{ \
745 return mod->field != NULL; \
746} \
747static void free_modinfo_##field(struct module *mod) \
748{ \
Daniel Walker22a8bde2007-10-18 03:06:07 -0700749 kfree(mod->field); \
750 mod->field = NULL; \
Matt Domschc988d2b2005-06-23 22:05:15 -0700751} \
752static struct module_attribute modinfo_##field = { \
Tejun Heo7b595752007-06-14 03:45:17 +0900753 .attr = { .name = __stringify(field), .mode = 0444 }, \
Matt Domschc988d2b2005-06-23 22:05:15 -0700754 .show = show_modinfo_##field, \
755 .setup = setup_modinfo_##field, \
756 .test = modinfo_##field##_exists, \
757 .free = free_modinfo_##field, \
758};
759
760MODINFO_ATTR(version);
761MODINFO_ATTR(srcversion);
762
Arjan van de Vene14af7e2008-01-25 21:08:33 +0100763static char last_unloaded_module[MODULE_NAME_LEN+1];
764
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -0800765#ifdef CONFIG_MODULE_UNLOAD
Steven Rostedteb0c5372010-03-29 14:25:18 -0400766
767EXPORT_TRACEPOINT_SYMBOL(module_get);
768
Masami Hiramatsue513cc12014-11-10 09:30:29 +1030769/* MODULE_REF_BASE is the base reference count by kmodule loader. */
770#define MODULE_REF_BASE 1
771
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772/* Init the unload section of the module. */
Rusty Russell9f85a4b2010-08-05 12:59:04 -0600773static int module_unload_init(struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774{
Masami Hiramatsue513cc12014-11-10 09:30:29 +1030775 /*
776 * Initialize reference counter to MODULE_REF_BASE.
777 * refcnt == 0 means module is going.
778 */
779 atomic_set(&mod->refcnt, MODULE_REF_BASE);
Rusty Russell9f85a4b2010-08-05 12:59:04 -0600780
Linus Torvalds2c02dfe2010-05-31 12:19:37 -0700781 INIT_LIST_HEAD(&mod->source_list);
782 INIT_LIST_HEAD(&mod->target_list);
Christoph Lametere1783a22010-01-05 15:34:50 +0900783
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 /* Hold reference count during initialization. */
Masami Hiramatsue513cc12014-11-10 09:30:29 +1030785 atomic_inc(&mod->refcnt);
Rusty Russell9f85a4b2010-08-05 12:59:04 -0600786
787 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788}
789
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790/* Does a already use b? */
791static int already_uses(struct module *a, struct module *b)
792{
793 struct module_use *use;
794
Linus Torvalds2c02dfe2010-05-31 12:19:37 -0700795 list_for_each_entry(use, &b->source_list, source_list) {
796 if (use->source == a) {
Jim Cromie5e124162011-12-06 12:11:31 -0700797 pr_debug("%s uses %s!\n", a->name, b->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 return 1;
799 }
800 }
Jim Cromie5e124162011-12-06 12:11:31 -0700801 pr_debug("%s does not use %s!\n", a->name, b->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 return 0;
803}
804
Linus Torvalds2c02dfe2010-05-31 12:19:37 -0700805/*
806 * Module a uses b
807 * - we add 'a' as a "source", 'b' as a "target" of module use
808 * - the module_use is added to the list of 'b' sources (so
809 * 'b' can walk the list to see who sourced them), and of 'a'
810 * targets (so 'a' can see what modules it targets).
811 */
812static int add_module_usage(struct module *a, struct module *b)
813{
Linus Torvalds2c02dfe2010-05-31 12:19:37 -0700814 struct module_use *use;
815
Jim Cromie5e124162011-12-06 12:11:31 -0700816 pr_debug("Allocating new usage for %s.\n", a->name);
Linus Torvalds2c02dfe2010-05-31 12:19:37 -0700817 use = kmalloc(sizeof(*use), GFP_ATOMIC);
818 if (!use) {
Andrew Mortonbddb12b2013-11-12 15:11:28 -0800819 pr_warn("%s: out of memory loading\n", a->name);
Linus Torvalds2c02dfe2010-05-31 12:19:37 -0700820 return -ENOMEM;
821 }
822
823 use->source = a;
824 use->target = b;
825 list_add(&use->source_list, &b->source_list);
826 list_add(&use->target_list, &a->target_list);
Linus Torvalds2c02dfe2010-05-31 12:19:37 -0700827 return 0;
828}
829
Rusty Russell75676502010-06-05 11:17:36 -0600830/* Module a uses b: caller needs module_mutex() */
Rusty Russell9bea7f22010-06-05 11:17:37 -0600831int ref_module(struct module *a, struct module *b)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832{
Rusty Russellc8e21ce2010-06-05 11:17:35 -0600833 int err;
Kay Sievers270a6c42007-01-18 13:26:15 +0100834
Rusty Russell9bea7f22010-06-05 11:17:37 -0600835 if (b == NULL || already_uses(a, b))
Linus Torvalds218ce732010-05-25 16:48:30 -0700836 return 0;
Linus Torvalds218ce732010-05-25 16:48:30 -0700837
Rusty Russell9bea7f22010-06-05 11:17:37 -0600838 /* If module isn't available, we fail. */
839 err = strong_try_module_get(b);
Rusty Russellc9a3ba52008-01-29 17:13:18 -0500840 if (err)
Rusty Russell9bea7f22010-06-05 11:17:37 -0600841 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842
Linus Torvalds2c02dfe2010-05-31 12:19:37 -0700843 err = add_module_usage(a, b);
844 if (err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 module_put(b);
Rusty Russell9bea7f22010-06-05 11:17:37 -0600846 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 }
Rusty Russell9bea7f22010-06-05 11:17:37 -0600848 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849}
Rusty Russell9bea7f22010-06-05 11:17:37 -0600850EXPORT_SYMBOL_GPL(ref_module);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851
852/* Clear the unload stuff of the module. */
853static void module_unload_free(struct module *mod)
854{
Linus Torvalds2c02dfe2010-05-31 12:19:37 -0700855 struct module_use *use, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856
Rusty Russell75676502010-06-05 11:17:36 -0600857 mutex_lock(&module_mutex);
Linus Torvalds2c02dfe2010-05-31 12:19:37 -0700858 list_for_each_entry_safe(use, tmp, &mod->target_list, target_list) {
859 struct module *i = use->target;
Jim Cromie5e124162011-12-06 12:11:31 -0700860 pr_debug("%s unusing %s\n", mod->name, i->name);
Linus Torvalds2c02dfe2010-05-31 12:19:37 -0700861 module_put(i);
862 list_del(&use->source_list);
863 list_del(&use->target_list);
864 kfree(use);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 }
Rusty Russell75676502010-06-05 11:17:36 -0600866 mutex_unlock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867}
868
869#ifdef CONFIG_MODULE_FORCE_UNLOAD
Akinobu Mitafb169792006-01-08 01:04:29 -0800870static inline int try_force_unload(unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871{
872 int ret = (flags & O_TRUNC);
873 if (ret)
Rusty Russell373d4d02013-01-21 17:17:39 +1030874 add_taint(TAINT_FORCED_RMMOD, LOCKDEP_NOW_UNRELIABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 return ret;
876}
877#else
Akinobu Mitafb169792006-01-08 01:04:29 -0800878static inline int try_force_unload(unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879{
880 return 0;
881}
882#endif /* CONFIG_MODULE_FORCE_UNLOAD */
883
Masami Hiramatsue513cc12014-11-10 09:30:29 +1030884/* Try to release refcount of module, 0 means success. */
885static int try_release_module_ref(struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886{
Masami Hiramatsue513cc12014-11-10 09:30:29 +1030887 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888
Masami Hiramatsue513cc12014-11-10 09:30:29 +1030889 /* Try to decrement refcnt which we set at loading */
890 ret = atomic_sub_return(MODULE_REF_BASE, &mod->refcnt);
891 BUG_ON(ret < 0);
892 if (ret)
893 /* Someone can put this right now, recover with checking */
894 ret = atomic_add_unless(&mod->refcnt, MODULE_REF_BASE, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895
Masami Hiramatsue513cc12014-11-10 09:30:29 +1030896 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897}
898
899static int try_stop_module(struct module *mod, int flags, int *forced)
900{
Masami Hiramatsue513cc12014-11-10 09:30:29 +1030901 /* If it's not unused, quit unless we're forcing. */
902 if (try_release_module_ref(mod) != 0) {
903 *forced = try_force_unload(flags);
904 if (!(*forced))
905 return -EWOULDBLOCK;
906 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907
Masami Hiramatsue513cc12014-11-10 09:30:29 +1030908 /* Mark it as dying. */
909 mod->state = MODULE_STATE_GOING;
910
911 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912}
913
Rusty Russelld5db1392015-01-22 11:13:14 +1030914/**
915 * module_refcount - return the refcount or -1 if unloading
916 *
917 * @mod: the module we're checking
918 *
919 * Returns:
920 * -1 if the module is in the process of unloading
921 * otherwise the number of references in the kernel to the module
922 */
923int module_refcount(struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924{
Rusty Russelld5db1392015-01-22 11:13:14 +1030925 return atomic_read(&mod->refcnt) - MODULE_REF_BASE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926}
927EXPORT_SYMBOL(module_refcount);
928
929/* This exists whether we can unload or not */
930static void free_module(struct module *mod);
931
Heiko Carstens17da2bd2009-01-14 14:14:10 +0100932SYSCALL_DEFINE2(delete_module, const char __user *, name_user,
933 unsigned int, flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934{
935 struct module *mod;
Greg Kroah-Hartmandfff0a02007-02-23 14:54:57 -0800936 char name[MODULE_NAME_LEN];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 int ret, forced = 0;
938
Kees Cook3d433212009-04-02 15:49:29 -0700939 if (!capable(CAP_SYS_MODULE) || modules_disabled)
Greg Kroah-Hartmandfff0a02007-02-23 14:54:57 -0800940 return -EPERM;
941
942 if (strncpy_from_user(name, name_user, MODULE_NAME_LEN-1) < 0)
943 return -EFAULT;
944 name[MODULE_NAME_LEN-1] = '\0';
945
Tejun Heo3fc1f1e2010-05-06 18:49:20 +0200946 if (mutex_lock_interruptible(&module_mutex) != 0)
947 return -EINTR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948
949 mod = find_module(name);
950 if (!mod) {
951 ret = -ENOENT;
952 goto out;
953 }
954
Linus Torvalds2c02dfe2010-05-31 12:19:37 -0700955 if (!list_empty(&mod->source_list)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 /* Other modules depend on us: get rid of them first. */
957 ret = -EWOULDBLOCK;
958 goto out;
959 }
960
961 /* Doing init or already dying? */
962 if (mod->state != MODULE_STATE_LIVE) {
Rusty Russell3f2b9c92013-09-17 05:48:51 +0930963 /* FIXME: if (force), slam module count damn the torpedoes */
Jim Cromie5e124162011-12-06 12:11:31 -0700964 pr_debug("%s already dying\n", mod->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 ret = -EBUSY;
966 goto out;
967 }
968
969 /* If it has an init func, it must have an exit func to unload */
Rusty Russellaf49d922007-10-16 23:26:27 -0700970 if (mod->init && !mod->exit) {
Akinobu Mitafb169792006-01-08 01:04:29 -0800971 forced = try_force_unload(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 if (!forced) {
973 /* This module can't be removed */
974 ret = -EBUSY;
975 goto out;
976 }
977 }
978
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 /* Stop the machine so refcounts can't move and disable module. */
980 ret = try_stop_module(mod, flags, &forced);
981 if (ret != 0)
982 goto out;
983
Peter Oberparleiterdf4b5652008-04-21 14:34:31 +0200984 mutex_unlock(&module_mutex);
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300985 /* Final destruction now no one is using it. */
Peter Oberparleiterdf4b5652008-04-21 14:34:31 +0200986 if (mod->exit != NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 mod->exit();
Peter Oberparleiterdf4b5652008-04-21 14:34:31 +0200988 blocking_notifier_call_chain(&module_notify_list,
989 MODULE_STATE_GOING, mod);
Jessica Yu7e545d62016-03-16 20:55:39 -0400990 klp_module_going(mod);
Jessica Yu7dcd1822016-02-16 17:32:33 -0500991 ftrace_release_mod(mod);
992
Arjan van de Ven22a9d642009-01-07 08:45:46 -0800993 async_synchronize_full();
Rusty Russell75676502010-06-05 11:17:36 -0600994
Arjan van de Vene14af7e2008-01-25 21:08:33 +0100995 /* Store the name of the last unloaded module for diagnostic purposes */
Rusty Russellefa53452008-01-29 17:13:20 -0500996 strlcpy(last_unloaded_module, mod->name, sizeof(last_unloaded_module));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997
Rusty Russell75676502010-06-05 11:17:36 -0600998 free_module(mod);
999 return 0;
1000out:
Ashutosh Naik6389a382006-03-23 03:00:46 -08001001 mutex_unlock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 return ret;
1003}
1004
Jianjun Kongd1e99d72008-12-08 14:26:29 +08001005static inline void print_unload_info(struct seq_file *m, struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006{
1007 struct module_use *use;
1008 int printed_something = 0;
1009
Rusty Russelld5db1392015-01-22 11:13:14 +10301010 seq_printf(m, " %i ", module_refcount(mod));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011
Ionut Alexa6da0b562014-11-10 09:31:29 +10301012 /*
1013 * Always include a trailing , so userspace can differentiate
1014 * between this and the old multi-field proc format.
1015 */
Linus Torvalds2c02dfe2010-05-31 12:19:37 -07001016 list_for_each_entry(use, &mod->source_list, source_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 printed_something = 1;
Linus Torvalds2c02dfe2010-05-31 12:19:37 -07001018 seq_printf(m, "%s,", use->source->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 }
1020
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 if (mod->init != NULL && mod->exit == NULL) {
1022 printed_something = 1;
Ionut Alexa6da0b562014-11-10 09:31:29 +10301023 seq_puts(m, "[permanent],");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 }
1025
1026 if (!printed_something)
Ionut Alexa6da0b562014-11-10 09:31:29 +10301027 seq_puts(m, "-");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028}
1029
1030void __symbol_put(const char *symbol)
1031{
1032 struct module *owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033
Rusty Russell24da1cb2007-07-15 23:41:46 -07001034 preempt_disable();
Tim Abbott414fd312008-12-05 19:03:56 -05001035 if (!find_symbol(symbol, &owner, NULL, true, false))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 BUG();
1037 module_put(owner);
Rusty Russell24da1cb2007-07-15 23:41:46 -07001038 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039}
1040EXPORT_SYMBOL(__symbol_put);
1041
Rusty Russell7d1d16e2009-08-26 22:02:54 +09301042/* Note this assumes addr is a function, which it currently always is. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043void symbol_put_addr(void *addr)
1044{
Trent Piepho5e376612006-05-15 09:44:06 -07001045 struct module *modaddr;
Rusty Russell7d1d16e2009-08-26 22:02:54 +09301046 unsigned long a = (unsigned long)dereference_function_descriptor(addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047
Rusty Russell7d1d16e2009-08-26 22:02:54 +09301048 if (core_kernel_text(a))
Trent Piepho5e376612006-05-15 09:44:06 -07001049 return;
1050
Peter Zijlstra275d7d42015-08-20 10:34:59 +09301051 /*
1052 * Even though we hold a reference on the module; we still need to
1053 * disable preemption in order to safely traverse the data structure.
1054 */
1055 preempt_disable();
Rusty Russell7d1d16e2009-08-26 22:02:54 +09301056 modaddr = __module_text_address(a);
Rusty Russella6e6abd2009-03-31 13:05:31 -06001057 BUG_ON(!modaddr);
Trent Piepho5e376612006-05-15 09:44:06 -07001058 module_put(modaddr);
Peter Zijlstra275d7d42015-08-20 10:34:59 +09301059 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060}
1061EXPORT_SYMBOL_GPL(symbol_put_addr);
1062
1063static ssize_t show_refcnt(struct module_attribute *mattr,
Kay Sievers4befb022011-07-24 22:06:04 +09301064 struct module_kobject *mk, char *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065{
Rusty Russelld5db1392015-01-22 11:13:14 +10301066 return sprintf(buffer, "%i\n", module_refcount(mk->mod));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067}
1068
Kay Sieverscca3e702012-01-13 09:32:15 +10301069static struct module_attribute modinfo_refcnt =
1070 __ATTR(refcnt, 0444, show_refcnt, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071
Steven Rostedtd53799b2012-03-26 12:50:52 +10301072void __module_get(struct module *module)
1073{
1074 if (module) {
1075 preempt_disable();
Masami Hiramatsu2f35c412014-11-10 09:29:29 +10301076 atomic_inc(&module->refcnt);
Steven Rostedtd53799b2012-03-26 12:50:52 +10301077 trace_module_get(module, _RET_IP_);
1078 preempt_enable();
1079 }
1080}
1081EXPORT_SYMBOL(__module_get);
1082
1083bool try_module_get(struct module *module)
1084{
1085 bool ret = true;
1086
1087 if (module) {
1088 preempt_disable();
Masami Hiramatsue513cc12014-11-10 09:30:29 +10301089 /* Note: here, we can fail to get a reference */
1090 if (likely(module_is_live(module) &&
1091 atomic_inc_not_zero(&module->refcnt) != 0))
Steven Rostedtd53799b2012-03-26 12:50:52 +10301092 trace_module_get(module, _RET_IP_);
Masami Hiramatsue513cc12014-11-10 09:30:29 +10301093 else
Steven Rostedtd53799b2012-03-26 12:50:52 +10301094 ret = false;
1095
1096 preempt_enable();
1097 }
1098 return ret;
1099}
1100EXPORT_SYMBOL(try_module_get);
1101
Al Virof6a57032006-10-18 01:47:25 -04001102void module_put(struct module *module)
1103{
Masami Hiramatsue513cc12014-11-10 09:30:29 +10301104 int ret;
1105
Al Virof6a57032006-10-18 01:47:25 -04001106 if (module) {
Christoph Lametere1783a22010-01-05 15:34:50 +09001107 preempt_disable();
Masami Hiramatsue513cc12014-11-10 09:30:29 +10301108 ret = atomic_dec_if_positive(&module->refcnt);
1109 WARN_ON(ret < 0); /* Failed to put refcount */
Li Zefanae832d12010-03-24 10:57:43 +08001110 trace_module_put(module, _RET_IP_);
Christoph Lametere1783a22010-01-05 15:34:50 +09001111 preempt_enable();
Al Virof6a57032006-10-18 01:47:25 -04001112 }
1113}
1114EXPORT_SYMBOL(module_put);
1115
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116#else /* !CONFIG_MODULE_UNLOAD */
Jianjun Kongd1e99d72008-12-08 14:26:29 +08001117static inline void print_unload_info(struct seq_file *m, struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118{
1119 /* We don't know the usage count, or what modules are using. */
Ionut Alexa6da0b562014-11-10 09:31:29 +10301120 seq_puts(m, " - -");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121}
1122
1123static inline void module_unload_free(struct module *mod)
1124{
1125}
1126
Rusty Russell9bea7f22010-06-05 11:17:37 -06001127int ref_module(struct module *a, struct module *b)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128{
Rusty Russell9bea7f22010-06-05 11:17:37 -06001129 return strong_try_module_get(b);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130}
Rusty Russell9bea7f22010-06-05 11:17:37 -06001131EXPORT_SYMBOL_GPL(ref_module);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132
Rusty Russell9f85a4b2010-08-05 12:59:04 -06001133static inline int module_unload_init(struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134{
Rusty Russell9f85a4b2010-08-05 12:59:04 -06001135 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136}
1137#endif /* CONFIG_MODULE_UNLOAD */
1138
Kevin Winchester53999bf2012-01-15 19:32:55 -04001139static size_t module_flags_taint(struct module *mod, char *buf)
1140{
1141 size_t l = 0;
1142
1143 if (mod->taints & (1 << TAINT_PROPRIETARY_MODULE))
1144 buf[l++] = 'P';
1145 if (mod->taints & (1 << TAINT_OOT_MODULE))
1146 buf[l++] = 'O';
1147 if (mod->taints & (1 << TAINT_FORCED_MODULE))
1148 buf[l++] = 'F';
1149 if (mod->taints & (1 << TAINT_CRAP))
1150 buf[l++] = 'C';
Mathieu Desnoyers66cc69e2014-03-13 12:11:30 +10301151 if (mod->taints & (1 << TAINT_UNSIGNED_MODULE))
Rusty Russell57673c22014-03-31 14:39:57 +10301152 buf[l++] = 'E';
Kevin Winchester53999bf2012-01-15 19:32:55 -04001153 /*
1154 * TAINT_FORCED_RMMOD: could be added.
Dave Jones8c904872014-02-26 10:49:49 -05001155 * TAINT_CPU_OUT_OF_SPEC, TAINT_MACHINE_CHECK, TAINT_BAD_PAGE don't
Kevin Winchester53999bf2012-01-15 19:32:55 -04001156 * apply to modules.
1157 */
1158 return l;
1159}
1160
Kay Sievers1f717402006-11-24 12:15:25 +01001161static ssize_t show_initstate(struct module_attribute *mattr,
Kay Sievers4befb022011-07-24 22:06:04 +09301162 struct module_kobject *mk, char *buffer)
Kay Sievers1f717402006-11-24 12:15:25 +01001163{
1164 const char *state = "unknown";
1165
Kay Sievers4befb022011-07-24 22:06:04 +09301166 switch (mk->mod->state) {
Kay Sievers1f717402006-11-24 12:15:25 +01001167 case MODULE_STATE_LIVE:
1168 state = "live";
1169 break;
1170 case MODULE_STATE_COMING:
1171 state = "coming";
1172 break;
1173 case MODULE_STATE_GOING:
1174 state = "going";
1175 break;
Rusty Russell0d21b0e2013-01-12 11:38:44 +10301176 default:
1177 BUG();
Kay Sievers1f717402006-11-24 12:15:25 +01001178 }
1179 return sprintf(buffer, "%s\n", state);
1180}
1181
Kay Sieverscca3e702012-01-13 09:32:15 +10301182static struct module_attribute modinfo_initstate =
1183 __ATTR(initstate, 0444, show_initstate, NULL);
Kay Sievers1f717402006-11-24 12:15:25 +01001184
Kay Sievers88bfa322011-07-24 22:06:04 +09301185static ssize_t store_uevent(struct module_attribute *mattr,
1186 struct module_kobject *mk,
1187 const char *buffer, size_t count)
1188{
1189 enum kobject_action action;
1190
1191 if (kobject_action_type(buffer, count, &action) == 0)
1192 kobject_uevent(&mk->kobj, action);
1193 return count;
1194}
1195
Kay Sieverscca3e702012-01-13 09:32:15 +10301196struct module_attribute module_uevent =
1197 __ATTR(uevent, 0200, NULL, store_uevent);
1198
1199static ssize_t show_coresize(struct module_attribute *mattr,
1200 struct module_kobject *mk, char *buffer)
1201{
Rusty Russell7523e4d2015-11-26 09:44:08 +10301202 return sprintf(buffer, "%u\n", mk->mod->core_layout.size);
Kay Sieverscca3e702012-01-13 09:32:15 +10301203}
1204
1205static struct module_attribute modinfo_coresize =
1206 __ATTR(coresize, 0444, show_coresize, NULL);
1207
1208static ssize_t show_initsize(struct module_attribute *mattr,
1209 struct module_kobject *mk, char *buffer)
1210{
Rusty Russell7523e4d2015-11-26 09:44:08 +10301211 return sprintf(buffer, "%u\n", mk->mod->init_layout.size);
Kay Sieverscca3e702012-01-13 09:32:15 +10301212}
1213
1214static struct module_attribute modinfo_initsize =
1215 __ATTR(initsize, 0444, show_initsize, NULL);
1216
1217static ssize_t show_taint(struct module_attribute *mattr,
1218 struct module_kobject *mk, char *buffer)
1219{
1220 size_t l;
1221
1222 l = module_flags_taint(mk->mod, buffer);
1223 buffer[l++] = '\n';
1224 return l;
1225}
1226
1227static struct module_attribute modinfo_taint =
1228 __ATTR(taint, 0444, show_taint, NULL);
Kay Sievers88bfa322011-07-24 22:06:04 +09301229
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001230static struct module_attribute *modinfo_attrs[] = {
Kay Sieverscca3e702012-01-13 09:32:15 +10301231 &module_uevent,
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001232 &modinfo_version,
1233 &modinfo_srcversion,
Kay Sieverscca3e702012-01-13 09:32:15 +10301234 &modinfo_initstate,
1235 &modinfo_coresize,
1236 &modinfo_initsize,
1237 &modinfo_taint,
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001238#ifdef CONFIG_MODULE_UNLOAD
Kay Sieverscca3e702012-01-13 09:32:15 +10301239 &modinfo_refcnt,
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001240#endif
1241 NULL,
1242};
1243
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244static const char vermagic[] = VERMAGIC_STRING;
1245
Rusty Russellc6e665c2009-03-31 13:05:33 -06001246static int try_to_force_load(struct module *mod, const char *reason)
Linus Torvalds826e4502008-05-04 17:04:16 -07001247{
1248#ifdef CONFIG_MODULE_FORCE_LOAD
Andi Kleen25ddbb12008-10-15 22:01:41 -07001249 if (!test_taint(TAINT_FORCED_MODULE))
Andrew Mortonbddb12b2013-11-12 15:11:28 -08001250 pr_warn("%s: %s: kernel tainted.\n", mod->name, reason);
Rusty Russell373d4d02013-01-21 17:17:39 +10301251 add_taint_module(mod, TAINT_FORCED_MODULE, LOCKDEP_NOW_UNRELIABLE);
Linus Torvalds826e4502008-05-04 17:04:16 -07001252 return 0;
1253#else
1254 return -ENOEXEC;
1255#endif
1256}
1257
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258#ifdef CONFIG_MODVERSIONS
Rusty Russelld4703ae2009-12-15 16:28:32 -06001259/* If the arch applies (non-zero) relocations to kernel kcrctab, unapply it. */
1260static unsigned long maybe_relocated(unsigned long crc,
1261 const struct module *crc_owner)
1262{
1263#ifdef ARCH_RELOCATES_KCRCTAB
1264 if (crc_owner == NULL)
1265 return crc - (unsigned long)reloc_start;
1266#endif
1267 return crc;
1268}
1269
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270static int check_version(Elf_Shdr *sechdrs,
1271 unsigned int versindex,
1272 const char *symname,
Ionut Alexa6da0b562014-11-10 09:31:29 +10301273 struct module *mod,
Rusty Russelld4703ae2009-12-15 16:28:32 -06001274 const unsigned long *crc,
1275 const struct module *crc_owner)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276{
1277 unsigned int i, num_versions;
1278 struct modversion_info *versions;
1279
1280 /* Exporting module didn't supply crcs? OK, we're already tainted. */
1281 if (!crc)
1282 return 1;
1283
Rusty Russella5dd6972008-05-09 16:24:21 +10001284 /* No versions at all? modprobe --force does this. */
1285 if (versindex == 0)
1286 return try_to_force_load(mod, symname) == 0;
1287
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288 versions = (void *) sechdrs[versindex].sh_addr;
1289 num_versions = sechdrs[versindex].sh_size
1290 / sizeof(struct modversion_info);
1291
1292 for (i = 0; i < num_versions; i++) {
1293 if (strcmp(versions[i].name, symname) != 0)
1294 continue;
1295
Rusty Russelld4703ae2009-12-15 16:28:32 -06001296 if (versions[i].crc == maybe_relocated(*crc, crc_owner))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 return 1;
Jim Cromie5e124162011-12-06 12:11:31 -07001298 pr_debug("Found checksum %lX vs module %lX\n",
Rusty Russelld4703ae2009-12-15 16:28:32 -06001299 maybe_relocated(*crc, crc_owner), versions[i].crc);
Linus Torvalds826e4502008-05-04 17:04:16 -07001300 goto bad_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301 }
Linus Torvalds826e4502008-05-04 17:04:16 -07001302
Andrew Mortonbddb12b2013-11-12 15:11:28 -08001303 pr_warn("%s: no symbol version for %s\n", mod->name, symname);
Rusty Russella5dd6972008-05-09 16:24:21 +10001304 return 0;
Linus Torvalds826e4502008-05-04 17:04:16 -07001305
1306bad_version:
Ionut Alexa6da0b562014-11-10 09:31:29 +10301307 pr_warn("%s: disagrees about version of symbol %s\n",
Linus Torvalds826e4502008-05-04 17:04:16 -07001308 mod->name, symname);
1309 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310}
1311
1312static inline int check_modstruct_version(Elf_Shdr *sechdrs,
1313 unsigned int versindex,
1314 struct module *mod)
1315{
1316 const unsigned long *crc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317
Peter Zijlstra926a59b2015-05-27 11:09:35 +09301318 /*
1319 * Since this should be found in kernel (which can't be removed), no
1320 * locking is necessary -- use preempt_disable() to placate lockdep.
1321 */
1322 preempt_disable();
Rusty Russellb92021b2013-03-15 15:04:17 +10301323 if (!find_symbol(VMLINUX_SYMBOL_STR(module_layout), NULL,
Peter Zijlstra926a59b2015-05-27 11:09:35 +09301324 &crc, true, false)) {
1325 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326 BUG();
Peter Zijlstra926a59b2015-05-27 11:09:35 +09301327 }
1328 preempt_enable();
James Hogana4b6a772013-03-18 19:38:56 +10301329 return check_version(sechdrs, versindex,
1330 VMLINUX_SYMBOL_STR(module_layout), mod, crc,
Rusty Russelld4703ae2009-12-15 16:28:32 -06001331 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332}
1333
Rusty Russell91e37a72008-05-09 16:25:28 +10001334/* First part is kernel version, which we ignore if module has crcs. */
1335static inline int same_magic(const char *amagic, const char *bmagic,
1336 bool has_crcs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337{
Rusty Russell91e37a72008-05-09 16:25:28 +10001338 if (has_crcs) {
1339 amagic += strcspn(amagic, " ");
1340 bmagic += strcspn(bmagic, " ");
1341 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342 return strcmp(amagic, bmagic) == 0;
1343}
1344#else
1345static inline int check_version(Elf_Shdr *sechdrs,
1346 unsigned int versindex,
1347 const char *symname,
Ionut Alexa6da0b562014-11-10 09:31:29 +10301348 struct module *mod,
Rusty Russelld4703ae2009-12-15 16:28:32 -06001349 const unsigned long *crc,
1350 const struct module *crc_owner)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351{
1352 return 1;
1353}
1354
1355static inline int check_modstruct_version(Elf_Shdr *sechdrs,
1356 unsigned int versindex,
1357 struct module *mod)
1358{
1359 return 1;
1360}
1361
Rusty Russell91e37a72008-05-09 16:25:28 +10001362static inline int same_magic(const char *amagic, const char *bmagic,
1363 bool has_crcs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364{
1365 return strcmp(amagic, bmagic) == 0;
1366}
1367#endif /* CONFIG_MODVERSIONS */
1368
Rusty Russell75676502010-06-05 11:17:36 -06001369/* Resolve a symbol for this module. I.e. if we find one, record usage. */
Rusty Russell49668682010-08-05 12:59:10 -06001370static const struct kernel_symbol *resolve_symbol(struct module *mod,
1371 const struct load_info *info,
Tim Abbott414fd312008-12-05 19:03:56 -05001372 const char *name,
Rusty Russell9bea7f22010-06-05 11:17:37 -06001373 char ownername[])
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374{
1375 struct module *owner;
Tim Abbott414fd312008-12-05 19:03:56 -05001376 const struct kernel_symbol *sym;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377 const unsigned long *crc;
Rusty Russell9bea7f22010-06-05 11:17:37 -06001378 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379
Peter Zijlstrad64810f2015-02-11 15:01:13 +10301380 /*
1381 * The module_mutex should not be a heavily contended lock;
1382 * if we get the occasional sleep here, we'll go an extra iteration
1383 * in the wait_event_interruptible(), which is harmless.
1384 */
1385 sched_annotate_sleep();
Rusty Russell75676502010-06-05 11:17:36 -06001386 mutex_lock(&module_mutex);
Tim Abbott414fd312008-12-05 19:03:56 -05001387 sym = find_symbol(name, &owner, &crc,
Andi Kleen25ddbb12008-10-15 22:01:41 -07001388 !(mod->taints & (1 << TAINT_PROPRIETARY_MODULE)), true);
Rusty Russell9bea7f22010-06-05 11:17:37 -06001389 if (!sym)
1390 goto unlock;
1391
Rusty Russell49668682010-08-05 12:59:10 -06001392 if (!check_version(info->sechdrs, info->index.vers, name, mod, crc,
1393 owner)) {
Rusty Russell9bea7f22010-06-05 11:17:37 -06001394 sym = ERR_PTR(-EINVAL);
1395 goto getname;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396 }
Rusty Russell9bea7f22010-06-05 11:17:37 -06001397
1398 err = ref_module(mod, owner);
1399 if (err) {
1400 sym = ERR_PTR(err);
1401 goto getname;
1402 }
1403
1404getname:
1405 /* We must make copy under the lock if we failed to get ref. */
1406 strncpy(ownername, module_name(owner), MODULE_NAME_LEN);
1407unlock:
Rusty Russell75676502010-06-05 11:17:36 -06001408 mutex_unlock(&module_mutex);
Linus Torvalds218ce732010-05-25 16:48:30 -07001409 return sym;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410}
1411
Rusty Russell49668682010-08-05 12:59:10 -06001412static const struct kernel_symbol *
1413resolve_symbol_wait(struct module *mod,
1414 const struct load_info *info,
1415 const char *name)
Rusty Russell9bea7f22010-06-05 11:17:37 -06001416{
1417 const struct kernel_symbol *ksym;
Rusty Russell49668682010-08-05 12:59:10 -06001418 char owner[MODULE_NAME_LEN];
Rusty Russell9bea7f22010-06-05 11:17:37 -06001419
1420 if (wait_event_interruptible_timeout(module_wq,
Rusty Russell49668682010-08-05 12:59:10 -06001421 !IS_ERR(ksym = resolve_symbol(mod, info, name, owner))
1422 || PTR_ERR(ksym) != -EBUSY,
Rusty Russell9bea7f22010-06-05 11:17:37 -06001423 30 * HZ) <= 0) {
Andrew Mortonbddb12b2013-11-12 15:11:28 -08001424 pr_warn("%s: gave up waiting for init of module %s.\n",
1425 mod->name, owner);
Rusty Russell9bea7f22010-06-05 11:17:37 -06001426 }
1427 return ksym;
1428}
1429
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430/*
1431 * /sys/module/foo/sections stuff
1432 * J. Corbet <corbet@lwn.net>
1433 */
Rusty Russell8f6d0372010-08-05 12:59:09 -06001434#ifdef CONFIG_SYSFS
Ben Hutchings10b465a2009-12-19 14:43:01 +00001435
Rusty Russell8f6d0372010-08-05 12:59:09 -06001436#ifdef CONFIG_KALLSYMS
Ben Hutchings10b465a2009-12-19 14:43:01 +00001437static inline bool sect_empty(const Elf_Shdr *sect)
1438{
1439 return !(sect->sh_flags & SHF_ALLOC) || sect->sh_size == 0;
1440}
1441
Ionut Alexa6da0b562014-11-10 09:31:29 +10301442struct module_sect_attr {
Rusty Russella58730c2008-03-13 09:03:44 +00001443 struct module_attribute mattr;
1444 char *name;
1445 unsigned long address;
1446};
1447
Ionut Alexa6da0b562014-11-10 09:31:29 +10301448struct module_sect_attrs {
Rusty Russella58730c2008-03-13 09:03:44 +00001449 struct attribute_group grp;
1450 unsigned int nsections;
1451 struct module_sect_attr attrs[0];
1452};
1453
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454static ssize_t module_sect_show(struct module_attribute *mattr,
Kay Sievers4befb022011-07-24 22:06:04 +09301455 struct module_kobject *mk, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456{
1457 struct module_sect_attr *sattr =
1458 container_of(mattr, struct module_sect_attr, mattr);
Kees Cook9f36e2c2011-03-22 16:34:22 -07001459 return sprintf(buf, "0x%pK\n", (void *)sattr->address);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460}
1461
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001462static void free_sect_attrs(struct module_sect_attrs *sect_attrs)
1463{
Rusty Russella58730c2008-03-13 09:03:44 +00001464 unsigned int section;
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001465
1466 for (section = 0; section < sect_attrs->nsections; section++)
1467 kfree(sect_attrs->attrs[section].name);
1468 kfree(sect_attrs);
1469}
1470
Rusty Russell8f6d0372010-08-05 12:59:09 -06001471static void add_sect_attrs(struct module *mod, const struct load_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472{
1473 unsigned int nloaded = 0, i, size[2];
1474 struct module_sect_attrs *sect_attrs;
1475 struct module_sect_attr *sattr;
1476 struct attribute **gattr;
Daniel Walker22a8bde2007-10-18 03:06:07 -07001477
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478 /* Count loaded sections and allocate structures */
Rusty Russell8f6d0372010-08-05 12:59:09 -06001479 for (i = 0; i < info->hdr->e_shnum; i++)
1480 if (!sect_empty(&info->sechdrs[i]))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481 nloaded++;
1482 size[0] = ALIGN(sizeof(*sect_attrs)
1483 + nloaded * sizeof(sect_attrs->attrs[0]),
1484 sizeof(sect_attrs->grp.attrs[0]));
1485 size[1] = (nloaded + 1) * sizeof(sect_attrs->grp.attrs[0]);
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001486 sect_attrs = kzalloc(size[0] + size[1], GFP_KERNEL);
1487 if (sect_attrs == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488 return;
1489
1490 /* Setup section attributes. */
1491 sect_attrs->grp.name = "sections";
1492 sect_attrs->grp.attrs = (void *)sect_attrs + size[0];
1493
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001494 sect_attrs->nsections = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495 sattr = &sect_attrs->attrs[0];
1496 gattr = &sect_attrs->grp.attrs[0];
Rusty Russell8f6d0372010-08-05 12:59:09 -06001497 for (i = 0; i < info->hdr->e_shnum; i++) {
1498 Elf_Shdr *sec = &info->sechdrs[i];
1499 if (sect_empty(sec))
Helge Deller35dead42009-12-03 00:29:15 +01001500 continue;
Rusty Russell8f6d0372010-08-05 12:59:09 -06001501 sattr->address = sec->sh_addr;
1502 sattr->name = kstrdup(info->secstrings + sec->sh_name,
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001503 GFP_KERNEL);
1504 if (sattr->name == NULL)
1505 goto out;
1506 sect_attrs->nsections++;
Eric W. Biederman361795b2010-02-12 13:41:56 -08001507 sysfs_attr_init(&sattr->mattr.attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508 sattr->mattr.show = module_sect_show;
1509 sattr->mattr.store = NULL;
1510 sattr->mattr.attr.name = sattr->name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511 sattr->mattr.attr.mode = S_IRUGO;
1512 *(gattr++) = &(sattr++)->mattr.attr;
1513 }
1514 *gattr = NULL;
1515
1516 if (sysfs_create_group(&mod->mkobj.kobj, &sect_attrs->grp))
1517 goto out;
1518
1519 mod->sect_attrs = sect_attrs;
1520 return;
1521 out:
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001522 free_sect_attrs(sect_attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523}
1524
1525static void remove_sect_attrs(struct module *mod)
1526{
1527 if (mod->sect_attrs) {
1528 sysfs_remove_group(&mod->mkobj.kobj,
1529 &mod->sect_attrs->grp);
1530 /* We are positive that no one is using any sect attrs
1531 * at this point. Deallocate immediately. */
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001532 free_sect_attrs(mod->sect_attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533 mod->sect_attrs = NULL;
1534 }
1535}
1536
Roland McGrath6d760132007-10-16 23:26:40 -07001537/*
1538 * /sys/module/foo/notes/.section.name gives contents of SHT_NOTE sections.
1539 */
1540
1541struct module_notes_attrs {
1542 struct kobject *dir;
1543 unsigned int notes;
1544 struct bin_attribute attrs[0];
1545};
1546
Chris Wright2c3c8be2010-05-12 18:28:57 -07001547static ssize_t module_notes_read(struct file *filp, struct kobject *kobj,
Roland McGrath6d760132007-10-16 23:26:40 -07001548 struct bin_attribute *bin_attr,
1549 char *buf, loff_t pos, size_t count)
1550{
1551 /*
1552 * The caller checked the pos and count against our size.
1553 */
1554 memcpy(buf, bin_attr->private + pos, count);
1555 return count;
1556}
1557
1558static void free_notes_attrs(struct module_notes_attrs *notes_attrs,
1559 unsigned int i)
1560{
1561 if (notes_attrs->dir) {
1562 while (i-- > 0)
1563 sysfs_remove_bin_file(notes_attrs->dir,
1564 &notes_attrs->attrs[i]);
Alexey Dobriyane9432092008-09-23 23:51:11 +04001565 kobject_put(notes_attrs->dir);
Roland McGrath6d760132007-10-16 23:26:40 -07001566 }
1567 kfree(notes_attrs);
1568}
1569
Rusty Russell8f6d0372010-08-05 12:59:09 -06001570static void add_notes_attrs(struct module *mod, const struct load_info *info)
Roland McGrath6d760132007-10-16 23:26:40 -07001571{
1572 unsigned int notes, loaded, i;
1573 struct module_notes_attrs *notes_attrs;
1574 struct bin_attribute *nattr;
1575
Ingo Molnarea6bff32009-08-28 10:44:56 +02001576 /* failed to create section attributes, so can't create notes */
1577 if (!mod->sect_attrs)
1578 return;
1579
Roland McGrath6d760132007-10-16 23:26:40 -07001580 /* Count notes sections and allocate structures. */
1581 notes = 0;
Rusty Russell8f6d0372010-08-05 12:59:09 -06001582 for (i = 0; i < info->hdr->e_shnum; i++)
1583 if (!sect_empty(&info->sechdrs[i]) &&
1584 (info->sechdrs[i].sh_type == SHT_NOTE))
Roland McGrath6d760132007-10-16 23:26:40 -07001585 ++notes;
1586
1587 if (notes == 0)
1588 return;
1589
1590 notes_attrs = kzalloc(sizeof(*notes_attrs)
1591 + notes * sizeof(notes_attrs->attrs[0]),
1592 GFP_KERNEL);
1593 if (notes_attrs == NULL)
1594 return;
1595
1596 notes_attrs->notes = notes;
1597 nattr = &notes_attrs->attrs[0];
Rusty Russell8f6d0372010-08-05 12:59:09 -06001598 for (loaded = i = 0; i < info->hdr->e_shnum; ++i) {
1599 if (sect_empty(&info->sechdrs[i]))
Roland McGrath6d760132007-10-16 23:26:40 -07001600 continue;
Rusty Russell8f6d0372010-08-05 12:59:09 -06001601 if (info->sechdrs[i].sh_type == SHT_NOTE) {
Eric W. Biederman361795b2010-02-12 13:41:56 -08001602 sysfs_bin_attr_init(nattr);
Roland McGrath6d760132007-10-16 23:26:40 -07001603 nattr->attr.name = mod->sect_attrs->attrs[loaded].name;
1604 nattr->attr.mode = S_IRUGO;
Rusty Russell8f6d0372010-08-05 12:59:09 -06001605 nattr->size = info->sechdrs[i].sh_size;
1606 nattr->private = (void *) info->sechdrs[i].sh_addr;
Roland McGrath6d760132007-10-16 23:26:40 -07001607 nattr->read = module_notes_read;
1608 ++nattr;
1609 }
1610 ++loaded;
1611 }
1612
Greg Kroah-Hartman4ff6abf2007-11-05 22:24:43 -08001613 notes_attrs->dir = kobject_create_and_add("notes", &mod->mkobj.kobj);
Roland McGrath6d760132007-10-16 23:26:40 -07001614 if (!notes_attrs->dir)
1615 goto out;
1616
1617 for (i = 0; i < notes; ++i)
1618 if (sysfs_create_bin_file(notes_attrs->dir,
1619 &notes_attrs->attrs[i]))
1620 goto out;
1621
1622 mod->notes_attrs = notes_attrs;
1623 return;
1624
1625 out:
1626 free_notes_attrs(notes_attrs, i);
1627}
1628
1629static void remove_notes_attrs(struct module *mod)
1630{
1631 if (mod->notes_attrs)
1632 free_notes_attrs(mod->notes_attrs, mod->notes_attrs->notes);
1633}
1634
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635#else
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001636
Rusty Russell8f6d0372010-08-05 12:59:09 -06001637static inline void add_sect_attrs(struct module *mod,
1638 const struct load_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639{
1640}
1641
1642static inline void remove_sect_attrs(struct module *mod)
1643{
1644}
Roland McGrath6d760132007-10-16 23:26:40 -07001645
Rusty Russell8f6d0372010-08-05 12:59:09 -06001646static inline void add_notes_attrs(struct module *mod,
1647 const struct load_info *info)
Roland McGrath6d760132007-10-16 23:26:40 -07001648{
1649}
1650
1651static inline void remove_notes_attrs(struct module *mod)
1652{
1653}
Rusty Russell8f6d0372010-08-05 12:59:09 -06001654#endif /* CONFIG_KALLSYMS */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655
Rusty Russell80a3d1b2010-06-05 11:17:36 -06001656static void add_usage_links(struct module *mod)
1657{
1658#ifdef CONFIG_MODULE_UNLOAD
1659 struct module_use *use;
1660 int nowarn;
1661
Rusty Russell75676502010-06-05 11:17:36 -06001662 mutex_lock(&module_mutex);
Rusty Russell80a3d1b2010-06-05 11:17:36 -06001663 list_for_each_entry(use, &mod->target_list, target_list) {
1664 nowarn = sysfs_create_link(use->target->holders_dir,
1665 &mod->mkobj.kobj, mod->name);
1666 }
Rusty Russell75676502010-06-05 11:17:36 -06001667 mutex_unlock(&module_mutex);
Rusty Russell80a3d1b2010-06-05 11:17:36 -06001668#endif
1669}
1670
1671static void del_usage_links(struct module *mod)
1672{
1673#ifdef CONFIG_MODULE_UNLOAD
1674 struct module_use *use;
1675
Rusty Russell75676502010-06-05 11:17:36 -06001676 mutex_lock(&module_mutex);
Rusty Russell80a3d1b2010-06-05 11:17:36 -06001677 list_for_each_entry(use, &mod->target_list, target_list)
1678 sysfs_remove_link(use->target->holders_dir, mod->name);
Rusty Russell75676502010-06-05 11:17:36 -06001679 mutex_unlock(&module_mutex);
Rusty Russell80a3d1b2010-06-05 11:17:36 -06001680#endif
1681}
1682
Rusty Russell6407ebb22010-06-05 11:17:36 -06001683static int module_add_modinfo_attrs(struct module *mod)
Matt Domschc988d2b2005-06-23 22:05:15 -07001684{
1685 struct module_attribute *attr;
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001686 struct module_attribute *temp_attr;
Matt Domschc988d2b2005-06-23 22:05:15 -07001687 int error = 0;
1688 int i;
1689
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001690 mod->modinfo_attrs = kzalloc((sizeof(struct module_attribute) *
1691 (ARRAY_SIZE(modinfo_attrs) + 1)),
1692 GFP_KERNEL);
1693 if (!mod->modinfo_attrs)
1694 return -ENOMEM;
1695
1696 temp_attr = mod->modinfo_attrs;
Matt Domschc988d2b2005-06-23 22:05:15 -07001697 for (i = 0; (attr = modinfo_attrs[i]) && !error; i++) {
Rusty Russellc75b5902016-04-12 05:03:09 +09301698 if (!attr->test || attr->test(mod)) {
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001699 memcpy(temp_attr, attr, sizeof(*temp_attr));
Eric W. Biederman361795b2010-02-12 13:41:56 -08001700 sysfs_attr_init(&temp_attr->attr);
Ionut Alexa6da0b562014-11-10 09:31:29 +10301701 error = sysfs_create_file(&mod->mkobj.kobj,
1702 &temp_attr->attr);
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001703 ++temp_attr;
1704 }
Matt Domschc988d2b2005-06-23 22:05:15 -07001705 }
1706 return error;
1707}
1708
Rusty Russell6407ebb22010-06-05 11:17:36 -06001709static void module_remove_modinfo_attrs(struct module *mod)
Matt Domschc988d2b2005-06-23 22:05:15 -07001710{
1711 struct module_attribute *attr;
1712 int i;
1713
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001714 for (i = 0; (attr = &mod->modinfo_attrs[i]); i++) {
1715 /* pick a field to test for end of list */
1716 if (!attr->attr.name)
1717 break;
Ionut Alexa6da0b562014-11-10 09:31:29 +10301718 sysfs_remove_file(&mod->mkobj.kobj, &attr->attr);
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001719 if (attr->free)
1720 attr->free(mod);
Matt Domschc988d2b2005-06-23 22:05:15 -07001721 }
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001722 kfree(mod->modinfo_attrs);
Matt Domschc988d2b2005-06-23 22:05:15 -07001723}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724
Li Zhong942e4432013-09-03 16:33:57 +09301725static void mod_kobject_put(struct module *mod)
1726{
1727 DECLARE_COMPLETION_ONSTACK(c);
1728 mod->mkobj.kobj_completion = &c;
1729 kobject_put(&mod->mkobj.kobj);
1730 wait_for_completion(&c);
1731}
1732
Rusty Russell6407ebb22010-06-05 11:17:36 -06001733static int mod_sysfs_init(struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734{
1735 int err;
Greg Kroah-Hartman6494a932008-01-27 15:38:40 -08001736 struct kobject *kobj;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737
Greg Kroah-Hartman823bccf2007-04-13 13:15:19 -07001738 if (!module_sysfs_initialized) {
Andrew Mortonbddb12b2013-11-12 15:11:28 -08001739 pr_err("%s: module sysfs not initialized\n", mod->name);
Ed Swierk1cc5f712006-09-25 16:25:36 -07001740 err = -EINVAL;
1741 goto out;
1742 }
Greg Kroah-Hartman6494a932008-01-27 15:38:40 -08001743
1744 kobj = kset_find_obj(module_kset, mod->name);
1745 if (kobj) {
Andrew Mortonbddb12b2013-11-12 15:11:28 -08001746 pr_err("%s: module is already loaded\n", mod->name);
Greg Kroah-Hartman6494a932008-01-27 15:38:40 -08001747 kobject_put(kobj);
1748 err = -EINVAL;
1749 goto out;
1750 }
1751
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752 mod->mkobj.mod = mod;
Kay Sieverse17e0f52006-11-24 12:15:25 +01001753
Greg Kroah-Hartmanac3c8142007-12-17 23:05:35 -07001754 memset(&mod->mkobj.kobj, 0, sizeof(mod->mkobj.kobj));
1755 mod->mkobj.kobj.kset = module_kset;
1756 err = kobject_init_and_add(&mod->mkobj.kobj, &module_ktype, NULL,
1757 "%s", mod->name);
1758 if (err)
Li Zhong942e4432013-09-03 16:33:57 +09301759 mod_kobject_put(mod);
Kay Sievers270a6c42007-01-18 13:26:15 +01001760
Kay Sievers97c146ef2007-11-29 23:46:11 +01001761 /* delay uevent until full sysfs population */
Kay Sievers270a6c42007-01-18 13:26:15 +01001762out:
1763 return err;
1764}
1765
Rusty Russell6407ebb22010-06-05 11:17:36 -06001766static int mod_sysfs_setup(struct module *mod,
Rusty Russell8f6d0372010-08-05 12:59:09 -06001767 const struct load_info *info,
Kay Sievers270a6c42007-01-18 13:26:15 +01001768 struct kernel_param *kparam,
1769 unsigned int num_params)
1770{
1771 int err;
1772
Rusty Russell80a3d1b2010-06-05 11:17:36 -06001773 err = mod_sysfs_init(mod);
1774 if (err)
1775 goto out;
1776
Greg Kroah-Hartman4ff6abf2007-11-05 22:24:43 -08001777 mod->holders_dir = kobject_create_and_add("holders", &mod->mkobj.kobj);
Akinobu Mita240936e2007-04-26 00:12:09 -07001778 if (!mod->holders_dir) {
1779 err = -ENOMEM;
Kay Sievers270a6c42007-01-18 13:26:15 +01001780 goto out_unreg;
Akinobu Mita240936e2007-04-26 00:12:09 -07001781 }
Kay Sievers270a6c42007-01-18 13:26:15 +01001782
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783 err = module_param_sysfs_setup(mod, kparam, num_params);
1784 if (err)
Kay Sievers270a6c42007-01-18 13:26:15 +01001785 goto out_unreg_holders;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786
Matt Domschc988d2b2005-06-23 22:05:15 -07001787 err = module_add_modinfo_attrs(mod);
1788 if (err)
Kay Sieverse17e0f52006-11-24 12:15:25 +01001789 goto out_unreg_param;
Matt Domschc988d2b2005-06-23 22:05:15 -07001790
Rusty Russell80a3d1b2010-06-05 11:17:36 -06001791 add_usage_links(mod);
Rusty Russell8f6d0372010-08-05 12:59:09 -06001792 add_sect_attrs(mod, info);
1793 add_notes_attrs(mod, info);
Rusty Russell80a3d1b2010-06-05 11:17:36 -06001794
Kay Sieverse17e0f52006-11-24 12:15:25 +01001795 kobject_uevent(&mod->mkobj.kobj, KOBJ_ADD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796 return 0;
1797
Kay Sieverse17e0f52006-11-24 12:15:25 +01001798out_unreg_param:
1799 module_param_sysfs_remove(mod);
Kay Sievers270a6c42007-01-18 13:26:15 +01001800out_unreg_holders:
Greg Kroah-Hartman78a2d902007-12-20 08:13:05 -08001801 kobject_put(mod->holders_dir);
Kay Sievers270a6c42007-01-18 13:26:15 +01001802out_unreg:
Li Zhong942e4432013-09-03 16:33:57 +09301803 mod_kobject_put(mod);
Rusty Russell80a3d1b2010-06-05 11:17:36 -06001804out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805 return err;
1806}
Denis V. Lunev34e4e2f2008-05-20 13:59:48 +04001807
1808static void mod_sysfs_fini(struct module *mod)
1809{
Rusty Russell8f6d0372010-08-05 12:59:09 -06001810 remove_notes_attrs(mod);
1811 remove_sect_attrs(mod);
Li Zhong942e4432013-09-03 16:33:57 +09301812 mod_kobject_put(mod);
Denis V. Lunev34e4e2f2008-05-20 13:59:48 +04001813}
1814
Rusty Russellcf2fde72015-06-26 06:44:38 +09301815static void init_param_lock(struct module *mod)
1816{
1817 mutex_init(&mod->param_lock);
1818}
Rusty Russell8f6d0372010-08-05 12:59:09 -06001819#else /* !CONFIG_SYSFS */
Denis V. Lunev34e4e2f2008-05-20 13:59:48 +04001820
Rusty Russell8f6d0372010-08-05 12:59:09 -06001821static int mod_sysfs_setup(struct module *mod,
1822 const struct load_info *info,
Rusty Russell6407ebb22010-06-05 11:17:36 -06001823 struct kernel_param *kparam,
1824 unsigned int num_params)
1825{
1826 return 0;
1827}
1828
Denis V. Lunev34e4e2f2008-05-20 13:59:48 +04001829static void mod_sysfs_fini(struct module *mod)
1830{
1831}
1832
Rusty Russell36b03602010-08-05 12:59:09 -06001833static void module_remove_modinfo_attrs(struct module *mod)
1834{
1835}
1836
Rusty Russell80a3d1b2010-06-05 11:17:36 -06001837static void del_usage_links(struct module *mod)
1838{
1839}
1840
Rusty Russellcf2fde72015-06-26 06:44:38 +09301841static void init_param_lock(struct module *mod)
1842{
1843}
Denis V. Lunev34e4e2f2008-05-20 13:59:48 +04001844#endif /* CONFIG_SYSFS */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845
Rusty Russell36b03602010-08-05 12:59:09 -06001846static void mod_sysfs_teardown(struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847{
Rusty Russell80a3d1b2010-06-05 11:17:36 -06001848 del_usage_links(mod);
Matt Domschc988d2b2005-06-23 22:05:15 -07001849 module_remove_modinfo_attrs(mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850 module_param_sysfs_remove(mod);
Greg Kroah-Hartman78a2d902007-12-20 08:13:05 -08001851 kobject_put(mod->mkobj.drivers_dir);
1852 kobject_put(mod->holders_dir);
Denis V. Lunev34e4e2f2008-05-20 13:59:48 +04001853 mod_sysfs_fini(mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854}
1855
matthieu castet84e1c6b2010-11-16 22:35:16 +01001856#ifdef CONFIG_DEBUG_SET_MODULE_RONX
1857/*
1858 * LKM RO/NX protection: protect module's text/ro-data
1859 * from modification and any data from execution.
Rusty Russell85c898d2015-11-26 09:45:08 +10301860 *
1861 * General layout of module is:
Jessica Yu444d13f2016-07-27 12:06:21 +09301862 * [text] [read-only-data] [ro-after-init] [writable data]
1863 * text_size -----^ ^ ^ ^
1864 * ro_size ------------------------| | |
1865 * ro_after_init_size -----------------------------| |
1866 * size -----------------------------------------------------------|
Rusty Russell85c898d2015-11-26 09:45:08 +10301867 *
1868 * These values are always page-aligned (as is base)
matthieu castet84e1c6b2010-11-16 22:35:16 +01001869 */
Rusty Russell85c898d2015-11-26 09:45:08 +10301870static void frob_text(const struct module_layout *layout,
1871 int (*set_memory)(unsigned long start, int num_pages))
matthieu castet84e1c6b2010-11-16 22:35:16 +01001872{
Rusty Russell85c898d2015-11-26 09:45:08 +10301873 BUG_ON((unsigned long)layout->base & (PAGE_SIZE-1));
1874 BUG_ON((unsigned long)layout->text_size & (PAGE_SIZE-1));
1875 set_memory((unsigned long)layout->base,
1876 layout->text_size >> PAGE_SHIFT);
matthieu castet84e1c6b2010-11-16 22:35:16 +01001877}
1878
Rusty Russell85c898d2015-11-26 09:45:08 +10301879static void frob_rodata(const struct module_layout *layout,
1880 int (*set_memory)(unsigned long start, int num_pages))
matthieu castet84e1c6b2010-11-16 22:35:16 +01001881{
Rusty Russell85c898d2015-11-26 09:45:08 +10301882 BUG_ON((unsigned long)layout->base & (PAGE_SIZE-1));
1883 BUG_ON((unsigned long)layout->text_size & (PAGE_SIZE-1));
1884 BUG_ON((unsigned long)layout->ro_size & (PAGE_SIZE-1));
1885 set_memory((unsigned long)layout->base + layout->text_size,
1886 (layout->ro_size - layout->text_size) >> PAGE_SHIFT);
matthieu castet84e1c6b2010-11-16 22:35:16 +01001887}
1888
Jessica Yu444d13f2016-07-27 12:06:21 +09301889static void frob_ro_after_init(const struct module_layout *layout,
1890 int (*set_memory)(unsigned long start, int num_pages))
1891{
1892 BUG_ON((unsigned long)layout->base & (PAGE_SIZE-1));
1893 BUG_ON((unsigned long)layout->ro_size & (PAGE_SIZE-1));
1894 BUG_ON((unsigned long)layout->ro_after_init_size & (PAGE_SIZE-1));
1895 set_memory((unsigned long)layout->base + layout->ro_size,
1896 (layout->ro_after_init_size - layout->ro_size) >> PAGE_SHIFT);
1897}
1898
Rusty Russell85c898d2015-11-26 09:45:08 +10301899static void frob_writable_data(const struct module_layout *layout,
1900 int (*set_memory)(unsigned long start, int num_pages))
matthieu castet84e1c6b2010-11-16 22:35:16 +01001901{
Rusty Russell85c898d2015-11-26 09:45:08 +10301902 BUG_ON((unsigned long)layout->base & (PAGE_SIZE-1));
Jessica Yu444d13f2016-07-27 12:06:21 +09301903 BUG_ON((unsigned long)layout->ro_after_init_size & (PAGE_SIZE-1));
Rusty Russell85c898d2015-11-26 09:45:08 +10301904 BUG_ON((unsigned long)layout->size & (PAGE_SIZE-1));
Jessica Yu444d13f2016-07-27 12:06:21 +09301905 set_memory((unsigned long)layout->base + layout->ro_after_init_size,
1906 (layout->size - layout->ro_after_init_size) >> PAGE_SHIFT);
Jan Glauber01526ed2011-05-19 16:55:26 -06001907}
1908
Rusty Russell85c898d2015-11-26 09:45:08 +10301909/* livepatching wants to disable read-only so it can frob module. */
1910void module_disable_ro(const struct module *mod)
Jan Glauber01526ed2011-05-19 16:55:26 -06001911{
Rusty Russell85c898d2015-11-26 09:45:08 +10301912 frob_text(&mod->core_layout, set_memory_rw);
1913 frob_rodata(&mod->core_layout, set_memory_rw);
Jessica Yu444d13f2016-07-27 12:06:21 +09301914 frob_ro_after_init(&mod->core_layout, set_memory_rw);
Rusty Russell85c898d2015-11-26 09:45:08 +10301915 frob_text(&mod->init_layout, set_memory_rw);
1916 frob_rodata(&mod->init_layout, set_memory_rw);
Josh Poimboeuf20ef10c2015-11-26 09:42:08 +10301917}
1918
Jessica Yu444d13f2016-07-27 12:06:21 +09301919void module_enable_ro(const struct module *mod, bool after_init)
Josh Poimboeuf20ef10c2015-11-26 09:42:08 +10301920{
Rusty Russell85c898d2015-11-26 09:45:08 +10301921 frob_text(&mod->core_layout, set_memory_ro);
1922 frob_rodata(&mod->core_layout, set_memory_ro);
1923 frob_text(&mod->init_layout, set_memory_ro);
1924 frob_rodata(&mod->init_layout, set_memory_ro);
Jessica Yu444d13f2016-07-27 12:06:21 +09301925
1926 if (after_init)
1927 frob_ro_after_init(&mod->core_layout, set_memory_ro);
Jan Glauber01526ed2011-05-19 16:55:26 -06001928}
1929
Rusty Russell85c898d2015-11-26 09:45:08 +10301930static void module_enable_nx(const struct module *mod)
Jan Glauber01526ed2011-05-19 16:55:26 -06001931{
Rusty Russell85c898d2015-11-26 09:45:08 +10301932 frob_rodata(&mod->core_layout, set_memory_nx);
Jessica Yu444d13f2016-07-27 12:06:21 +09301933 frob_ro_after_init(&mod->core_layout, set_memory_nx);
Rusty Russell85c898d2015-11-26 09:45:08 +10301934 frob_writable_data(&mod->core_layout, set_memory_nx);
1935 frob_rodata(&mod->init_layout, set_memory_nx);
1936 frob_writable_data(&mod->init_layout, set_memory_nx);
1937}
1938
1939static void module_disable_nx(const struct module *mod)
1940{
1941 frob_rodata(&mod->core_layout, set_memory_x);
Jessica Yu444d13f2016-07-27 12:06:21 +09301942 frob_ro_after_init(&mod->core_layout, set_memory_x);
Rusty Russell85c898d2015-11-26 09:45:08 +10301943 frob_writable_data(&mod->core_layout, set_memory_x);
1944 frob_rodata(&mod->init_layout, set_memory_x);
1945 frob_writable_data(&mod->init_layout, set_memory_x);
matthieu castet84e1c6b2010-11-16 22:35:16 +01001946}
1947
1948/* Iterate through all modules and set each module's text as RW */
Daniel J Blueman5d05c702011-03-08 22:01:47 +08001949void set_all_modules_text_rw(void)
matthieu castet84e1c6b2010-11-16 22:35:16 +01001950{
1951 struct module *mod;
1952
1953 mutex_lock(&module_mutex);
1954 list_for_each_entry_rcu(mod, &modules, list) {
Rusty Russell0d21b0e2013-01-12 11:38:44 +10301955 if (mod->state == MODULE_STATE_UNFORMED)
1956 continue;
Rusty Russell85c898d2015-11-26 09:45:08 +10301957
1958 frob_text(&mod->core_layout, set_memory_rw);
1959 frob_text(&mod->init_layout, set_memory_rw);
matthieu castet84e1c6b2010-11-16 22:35:16 +01001960 }
1961 mutex_unlock(&module_mutex);
1962}
1963
1964/* Iterate through all modules and set each module's text as RO */
Daniel J Blueman5d05c702011-03-08 22:01:47 +08001965void set_all_modules_text_ro(void)
matthieu castet84e1c6b2010-11-16 22:35:16 +01001966{
1967 struct module *mod;
1968
1969 mutex_lock(&module_mutex);
1970 list_for_each_entry_rcu(mod, &modules, list) {
Rusty Russell0d21b0e2013-01-12 11:38:44 +10301971 if (mod->state == MODULE_STATE_UNFORMED)
1972 continue;
Rusty Russell85c898d2015-11-26 09:45:08 +10301973
1974 frob_text(&mod->core_layout, set_memory_ro);
1975 frob_text(&mod->init_layout, set_memory_ro);
matthieu castet84e1c6b2010-11-16 22:35:16 +01001976 }
1977 mutex_unlock(&module_mutex);
1978}
Rusty Russell85c898d2015-11-26 09:45:08 +10301979
1980static void disable_ro_nx(const struct module_layout *layout)
1981{
1982 frob_text(layout, set_memory_rw);
1983 frob_rodata(layout, set_memory_rw);
1984 frob_rodata(layout, set_memory_x);
Jessica Yu444d13f2016-07-27 12:06:21 +09301985 frob_ro_after_init(layout, set_memory_rw);
1986 frob_ro_after_init(layout, set_memory_x);
Rusty Russell85c898d2015-11-26 09:45:08 +10301987 frob_writable_data(layout, set_memory_x);
1988}
1989
matthieu castet84e1c6b2010-11-16 22:35:16 +01001990#else
Rusty Russell85c898d2015-11-26 09:45:08 +10301991static void disable_ro_nx(const struct module_layout *layout) { }
1992static void module_enable_nx(const struct module *mod) { }
1993static void module_disable_nx(const struct module *mod) { }
matthieu castet84e1c6b2010-11-16 22:35:16 +01001994#endif
1995
Jessica Yu1ce15ef2016-03-22 20:03:16 -04001996#ifdef CONFIG_LIVEPATCH
1997/*
1998 * Persist Elf information about a module. Copy the Elf header,
1999 * section header table, section string table, and symtab section
2000 * index from info to mod->klp_info.
2001 */
2002static int copy_module_elf(struct module *mod, struct load_info *info)
2003{
2004 unsigned int size, symndx;
2005 int ret;
2006
2007 size = sizeof(*mod->klp_info);
2008 mod->klp_info = kmalloc(size, GFP_KERNEL);
2009 if (mod->klp_info == NULL)
2010 return -ENOMEM;
2011
2012 /* Elf header */
2013 size = sizeof(mod->klp_info->hdr);
2014 memcpy(&mod->klp_info->hdr, info->hdr, size);
2015
2016 /* Elf section header table */
2017 size = sizeof(*info->sechdrs) * info->hdr->e_shnum;
2018 mod->klp_info->sechdrs = kmalloc(size, GFP_KERNEL);
2019 if (mod->klp_info->sechdrs == NULL) {
2020 ret = -ENOMEM;
2021 goto free_info;
2022 }
2023 memcpy(mod->klp_info->sechdrs, info->sechdrs, size);
2024
2025 /* Elf section name string table */
2026 size = info->sechdrs[info->hdr->e_shstrndx].sh_size;
2027 mod->klp_info->secstrings = kmalloc(size, GFP_KERNEL);
2028 if (mod->klp_info->secstrings == NULL) {
2029 ret = -ENOMEM;
2030 goto free_sechdrs;
2031 }
2032 memcpy(mod->klp_info->secstrings, info->secstrings, size);
2033
2034 /* Elf symbol section index */
2035 symndx = info->index.sym;
2036 mod->klp_info->symndx = symndx;
2037
2038 /*
2039 * For livepatch modules, core_kallsyms.symtab is a complete
2040 * copy of the original symbol table. Adjust sh_addr to point
2041 * to core_kallsyms.symtab since the copy of the symtab in module
2042 * init memory is freed at the end of do_init_module().
2043 */
2044 mod->klp_info->sechdrs[symndx].sh_addr = \
2045 (unsigned long) mod->core_kallsyms.symtab;
2046
2047 return 0;
2048
2049free_sechdrs:
2050 kfree(mod->klp_info->sechdrs);
2051free_info:
2052 kfree(mod->klp_info);
2053 return ret;
2054}
2055
2056static void free_module_elf(struct module *mod)
2057{
2058 kfree(mod->klp_info->sechdrs);
2059 kfree(mod->klp_info->secstrings);
2060 kfree(mod->klp_info);
2061}
2062#else /* !CONFIG_LIVEPATCH */
2063static int copy_module_elf(struct module *mod, struct load_info *info)
2064{
2065 return 0;
2066}
2067
2068static void free_module_elf(struct module *mod)
2069{
2070}
2071#endif /* CONFIG_LIVEPATCH */
2072
Rusty Russellbe1f2212015-01-20 09:07:05 +10302073void __weak module_memfree(void *module_region)
Jonas Bonn74e08fc2011-06-30 21:22:11 +02002074{
2075 vfree(module_region);
2076}
2077
2078void __weak module_arch_cleanup(struct module *mod)
2079{
2080}
2081
Rusty Russelld453cde2015-01-20 09:07:04 +10302082void __weak module_arch_freeing_init(struct module *mod)
2083{
2084}
2085
Rusty Russell75676502010-06-05 11:17:36 -06002086/* Free a module, remove from lists, etc. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087static void free_module(struct module *mod)
2088{
Li Zefan7ead8b82009-08-17 16:56:28 +08002089 trace_module_free(mod);
2090
Rusty Russell36b03602010-08-05 12:59:09 -06002091 mod_sysfs_teardown(mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092
Rusty Russell944a1fa2013-04-17 13:20:03 +09302093 /* We leave it in list to prevent duplicate loads, but make sure
2094 * that noone uses it while it's being deconstructed. */
Prarit Bhargavad3051b42014-10-14 02:51:39 +10302095 mutex_lock(&module_mutex);
Rusty Russell944a1fa2013-04-17 13:20:03 +09302096 mod->state = MODULE_STATE_UNFORMED;
Prarit Bhargavad3051b42014-10-14 02:51:39 +10302097 mutex_unlock(&module_mutex);
Rusty Russell944a1fa2013-04-17 13:20:03 +09302098
Jason Baronb82bab4b2010-07-27 13:18:01 -07002099 /* Remove dynamic debug info */
2100 ddebug_remove_module(mod->name);
2101
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102 /* Arch-specific cleanup. */
2103 module_arch_cleanup(mod);
2104
2105 /* Module unload stuff */
2106 module_unload_free(mod);
2107
Rusty Russelle180a6b2009-03-31 13:05:29 -06002108 /* Free any allocated parameters. */
2109 destroy_params(mod->kp, mod->num_kp);
2110
Jessica Yu1ce15ef2016-03-22 20:03:16 -04002111 if (is_livepatch_module(mod))
2112 free_module_elf(mod);
2113
Rusty Russell944a1fa2013-04-17 13:20:03 +09302114 /* Now we can delete it from the lists */
2115 mutex_lock(&module_mutex);
Masami Hiramatsu461e34a2014-11-10 09:27:29 +10302116 /* Unlink carefully: kallsyms could be walking list. */
2117 list_del_rcu(&mod->list);
Peter Zijlstra93c2e102015-05-27 11:09:37 +09302118 mod_tree_remove(mod);
Masami Hiramatsu0286b5e2014-11-10 09:28:29 +10302119 /* Remove this module from bug list, this uses list_del_rcu */
Masami Hiramatsu461e34a2014-11-10 09:27:29 +10302120 module_bug_cleanup(mod);
Peter Zijlstra0be964b2015-05-27 11:09:35 +09302121 /* Wait for RCU-sched synchronizing before releasing mod->list and buglist. */
2122 synchronize_sched();
Rusty Russell944a1fa2013-04-17 13:20:03 +09302123 mutex_unlock(&module_mutex);
2124
Rusty Russell85c898d2015-11-26 09:45:08 +10302125 /* This may be empty, but that's OK */
2126 disable_ro_nx(&mod->init_layout);
Rusty Russelld453cde2015-01-20 09:07:04 +10302127 module_arch_freeing_init(mod);
Rusty Russell7523e4d2015-11-26 09:44:08 +10302128 module_memfree(mod->init_layout.base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129 kfree(mod->args);
Tejun Heo259354d2010-03-10 18:56:10 +09002130 percpu_modfree(mod);
Rusty Russell9f85a4b2010-08-05 12:59:04 -06002131
Peter Zijlstra35a93932015-02-26 16:23:11 +01002132 /* Free lock-classes; relies on the preceding sync_rcu(). */
Rusty Russell7523e4d2015-11-26 09:44:08 +10302133 lockdep_free_key_range(mod->core_layout.base, mod->core_layout.size);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002134
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135 /* Finally, free the core (containing the module structure) */
Rusty Russell85c898d2015-11-26 09:45:08 +10302136 disable_ro_nx(&mod->core_layout);
Rusty Russell7523e4d2015-11-26 09:44:08 +10302137 module_memfree(mod->core_layout.base);
Bernd Schmidteb8cdec2009-09-21 17:03:57 -07002138
2139#ifdef CONFIG_MPU
2140 update_protections(current->mm);
2141#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142}
2143
2144void *__symbol_get(const char *symbol)
2145{
2146 struct module *owner;
Tim Abbott414fd312008-12-05 19:03:56 -05002147 const struct kernel_symbol *sym;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148
Rusty Russell24da1cb2007-07-15 23:41:46 -07002149 preempt_disable();
Tim Abbott414fd312008-12-05 19:03:56 -05002150 sym = find_symbol(symbol, &owner, NULL, true, true);
2151 if (sym && strong_try_module_get(owner))
2152 sym = NULL;
Rusty Russell24da1cb2007-07-15 23:41:46 -07002153 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154
Tim Abbott414fd312008-12-05 19:03:56 -05002155 return sym ? (void *)sym->value : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156}
2157EXPORT_SYMBOL_GPL(__symbol_get);
2158
Ashutosh Naikeea8b542006-01-08 01:04:25 -08002159/*
2160 * Ensure that an exported symbol [global namespace] does not already exist
Robert P. J. Day02a3e592007-05-09 07:26:28 +02002161 * in the kernel or in some other module's exported symbol table.
Rusty Russellbe593f42010-06-05 11:17:37 -06002162 *
2163 * You must hold the module_mutex.
Ashutosh Naikeea8b542006-01-08 01:04:25 -08002164 */
2165static int verify_export_symbols(struct module *mod)
2166{
Rusty Russellb2111042008-05-01 21:15:00 -05002167 unsigned int i;
Ashutosh Naikeea8b542006-01-08 01:04:25 -08002168 struct module *owner;
Rusty Russellb2111042008-05-01 21:15:00 -05002169 const struct kernel_symbol *s;
2170 struct {
2171 const struct kernel_symbol *sym;
2172 unsigned int num;
2173 } arr[] = {
2174 { mod->syms, mod->num_syms },
2175 { mod->gpl_syms, mod->num_gpl_syms },
2176 { mod->gpl_future_syms, mod->num_gpl_future_syms },
Denys Vlasenkof7f5b672008-07-22 19:24:26 -05002177#ifdef CONFIG_UNUSED_SYMBOLS
Rusty Russellb2111042008-05-01 21:15:00 -05002178 { mod->unused_syms, mod->num_unused_syms },
2179 { mod->unused_gpl_syms, mod->num_unused_gpl_syms },
Denys Vlasenkof7f5b672008-07-22 19:24:26 -05002180#endif
Rusty Russellb2111042008-05-01 21:15:00 -05002181 };
Ashutosh Naikeea8b542006-01-08 01:04:25 -08002182
Rusty Russellb2111042008-05-01 21:15:00 -05002183 for (i = 0; i < ARRAY_SIZE(arr); i++) {
2184 for (s = arr[i].sym; s < arr[i].sym + arr[i].num; s++) {
Rusty Russellbe593f42010-06-05 11:17:37 -06002185 if (find_symbol(s->name, &owner, NULL, true, false)) {
Andrew Mortonbddb12b2013-11-12 15:11:28 -08002186 pr_err("%s: exports duplicate symbol %s"
Rusty Russellb2111042008-05-01 21:15:00 -05002187 " (owned by %s)\n",
2188 mod->name, s->name, module_name(owner));
2189 return -ENOEXEC;
2190 }
Ashutosh Naikeea8b542006-01-08 01:04:25 -08002191 }
Rusty Russellb2111042008-05-01 21:15:00 -05002192 }
2193 return 0;
Ashutosh Naikeea8b542006-01-08 01:04:25 -08002194}
2195
Matti Linnanvuori9a4b9702007-11-08 08:37:38 -08002196/* Change all symbols so that st_value encodes the pointer directly. */
Rusty Russell49668682010-08-05 12:59:10 -06002197static int simplify_symbols(struct module *mod, const struct load_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198{
Rusty Russell49668682010-08-05 12:59:10 -06002199 Elf_Shdr *symsec = &info->sechdrs[info->index.sym];
2200 Elf_Sym *sym = (void *)symsec->sh_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201 unsigned long secbase;
Rusty Russell49668682010-08-05 12:59:10 -06002202 unsigned int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203 int ret = 0;
Tim Abbott414fd312008-12-05 19:03:56 -05002204 const struct kernel_symbol *ksym;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002205
Rusty Russell49668682010-08-05 12:59:10 -06002206 for (i = 1; i < symsec->sh_size / sizeof(Elf_Sym); i++) {
2207 const char *name = info->strtab + sym[i].st_name;
2208
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209 switch (sym[i].st_shndx) {
2210 case SHN_COMMON:
Joe Mario80375982014-02-08 09:01:09 +01002211 /* Ignore common symbols */
2212 if (!strncmp(name, "__gnu_lto", 9))
2213 break;
2214
Linus Torvalds1da177e2005-04-16 15:20:36 -07002215 /* We compiled with -fno-common. These are not
2216 supposed to happen. */
Jim Cromie5e124162011-12-06 12:11:31 -07002217 pr_debug("Common symbol: %s\n", name);
Ionut Alexa6da0b562014-11-10 09:31:29 +10302218 pr_warn("%s: please compile with -fno-common\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002219 mod->name);
2220 ret = -ENOEXEC;
2221 break;
2222
2223 case SHN_ABS:
2224 /* Don't need to do anything */
Jim Cromie5e124162011-12-06 12:11:31 -07002225 pr_debug("Absolute symbol: 0x%08lx\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002226 (long)sym[i].st_value);
2227 break;
2228
Jessica Yu1ce15ef2016-03-22 20:03:16 -04002229 case SHN_LIVEPATCH:
2230 /* Livepatch symbols are resolved by livepatch */
2231 break;
2232
Linus Torvalds1da177e2005-04-16 15:20:36 -07002233 case SHN_UNDEF:
Rusty Russell49668682010-08-05 12:59:10 -06002234 ksym = resolve_symbol_wait(mod, info, name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002235 /* Ok if resolved. */
Rusty Russell9bea7f22010-06-05 11:17:37 -06002236 if (ksym && !IS_ERR(ksym)) {
Tim Abbott414fd312008-12-05 19:03:56 -05002237 sym[i].st_value = ksym->value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238 break;
Tim Abbott414fd312008-12-05 19:03:56 -05002239 }
2240
Linus Torvalds1da177e2005-04-16 15:20:36 -07002241 /* Ok if weak. */
Rusty Russell9bea7f22010-06-05 11:17:37 -06002242 if (!ksym && ELF_ST_BIND(sym[i].st_info) == STB_WEAK)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002243 break;
2244
Andrew Mortonbddb12b2013-11-12 15:11:28 -08002245 pr_warn("%s: Unknown symbol %s (err %li)\n",
2246 mod->name, name, PTR_ERR(ksym));
Rusty Russell9bea7f22010-06-05 11:17:37 -06002247 ret = PTR_ERR(ksym) ?: -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002248 break;
2249
2250 default:
2251 /* Divert to percpu allocation if a percpu var. */
Rusty Russell49668682010-08-05 12:59:10 -06002252 if (sym[i].st_shndx == info->index.pcpu)
Tejun Heo259354d2010-03-10 18:56:10 +09002253 secbase = (unsigned long)mod_percpu(mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002254 else
Rusty Russell49668682010-08-05 12:59:10 -06002255 secbase = info->sechdrs[sym[i].st_shndx].sh_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256 sym[i].st_value += secbase;
2257 break;
2258 }
2259 }
2260
2261 return ret;
2262}
2263
Rusty Russell49668682010-08-05 12:59:10 -06002264static int apply_relocations(struct module *mod, const struct load_info *info)
Rusty Russell22e268e2010-08-05 12:59:05 -06002265{
2266 unsigned int i;
2267 int err = 0;
2268
2269 /* Now do relocations. */
Rusty Russell49668682010-08-05 12:59:10 -06002270 for (i = 1; i < info->hdr->e_shnum; i++) {
2271 unsigned int infosec = info->sechdrs[i].sh_info;
Rusty Russell22e268e2010-08-05 12:59:05 -06002272
2273 /* Not a valid relocation section? */
Rusty Russell49668682010-08-05 12:59:10 -06002274 if (infosec >= info->hdr->e_shnum)
Rusty Russell22e268e2010-08-05 12:59:05 -06002275 continue;
2276
2277 /* Don't bother with non-allocated sections */
Rusty Russell49668682010-08-05 12:59:10 -06002278 if (!(info->sechdrs[infosec].sh_flags & SHF_ALLOC))
Rusty Russell22e268e2010-08-05 12:59:05 -06002279 continue;
2280
Jessica Yu1ce15ef2016-03-22 20:03:16 -04002281 /* Livepatch relocation sections are applied by livepatch */
2282 if (info->sechdrs[i].sh_flags & SHF_RELA_LIVEPATCH)
2283 continue;
2284
Rusty Russell49668682010-08-05 12:59:10 -06002285 if (info->sechdrs[i].sh_type == SHT_REL)
2286 err = apply_relocate(info->sechdrs, info->strtab,
2287 info->index.sym, i, mod);
2288 else if (info->sechdrs[i].sh_type == SHT_RELA)
2289 err = apply_relocate_add(info->sechdrs, info->strtab,
2290 info->index.sym, i, mod);
Rusty Russell22e268e2010-08-05 12:59:05 -06002291 if (err < 0)
2292 break;
2293 }
2294 return err;
2295}
2296
Helge Deller088af9a2008-12-31 12:31:18 +01002297/* Additional bytes needed by arch in front of individual sections */
2298unsigned int __weak arch_mod_section_prepend(struct module *mod,
2299 unsigned int section)
2300{
2301 /* default implementation just returns zero */
2302 return 0;
2303}
2304
Linus Torvalds1da177e2005-04-16 15:20:36 -07002305/* Update size with this section: return offset. */
Helge Deller088af9a2008-12-31 12:31:18 +01002306static long get_offset(struct module *mod, unsigned int *size,
2307 Elf_Shdr *sechdr, unsigned int section)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308{
2309 long ret;
2310
Helge Deller088af9a2008-12-31 12:31:18 +01002311 *size += arch_mod_section_prepend(mod, section);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312 ret = ALIGN(*size, sechdr->sh_addralign ?: 1);
2313 *size = ret + sechdr->sh_size;
2314 return ret;
2315}
2316
2317/* Lay out the SHF_ALLOC sections in a way not dissimilar to how ld
2318 might -- code, read-only data, read-write data, small data. Tally
2319 sizes, and place the offsets into sh_entsize fields: high bit means it
2320 belongs in init. */
Rusty Russell49668682010-08-05 12:59:10 -06002321static void layout_sections(struct module *mod, struct load_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002322{
2323 static unsigned long const masks[][2] = {
2324 /* NOTE: all executable code must be the first section
2325 * in this array; otherwise modify the text_size
2326 * finder in the two loops below */
2327 { SHF_EXECINSTR | SHF_ALLOC, ARCH_SHF_SMALL },
2328 { SHF_ALLOC, SHF_WRITE | ARCH_SHF_SMALL },
Jessica Yu444d13f2016-07-27 12:06:21 +09302329 { SHF_RO_AFTER_INIT | SHF_ALLOC, ARCH_SHF_SMALL },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002330 { SHF_WRITE | SHF_ALLOC, ARCH_SHF_SMALL },
2331 { ARCH_SHF_SMALL | SHF_ALLOC, 0 }
2332 };
2333 unsigned int m, i;
2334
Rusty Russell49668682010-08-05 12:59:10 -06002335 for (i = 0; i < info->hdr->e_shnum; i++)
2336 info->sechdrs[i].sh_entsize = ~0UL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002337
Jim Cromie5e124162011-12-06 12:11:31 -07002338 pr_debug("Core section allocation order:\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002339 for (m = 0; m < ARRAY_SIZE(masks); ++m) {
Rusty Russell49668682010-08-05 12:59:10 -06002340 for (i = 0; i < info->hdr->e_shnum; ++i) {
2341 Elf_Shdr *s = &info->sechdrs[i];
2342 const char *sname = info->secstrings + s->sh_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002343
2344 if ((s->sh_flags & masks[m][0]) != masks[m][0]
2345 || (s->sh_flags & masks[m][1])
2346 || s->sh_entsize != ~0UL
Rusty Russell49668682010-08-05 12:59:10 -06002347 || strstarts(sname, ".init"))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002348 continue;
Rusty Russell7523e4d2015-11-26 09:44:08 +10302349 s->sh_entsize = get_offset(mod, &mod->core_layout.size, s, i);
Jim Cromie5e124162011-12-06 12:11:31 -07002350 pr_debug("\t%s\n", sname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002351 }
matthieu castet84e1c6b2010-11-16 22:35:16 +01002352 switch (m) {
2353 case 0: /* executable */
Rusty Russell7523e4d2015-11-26 09:44:08 +10302354 mod->core_layout.size = debug_align(mod->core_layout.size);
2355 mod->core_layout.text_size = mod->core_layout.size;
matthieu castet84e1c6b2010-11-16 22:35:16 +01002356 break;
2357 case 1: /* RO: text and ro-data */
Rusty Russell7523e4d2015-11-26 09:44:08 +10302358 mod->core_layout.size = debug_align(mod->core_layout.size);
2359 mod->core_layout.ro_size = mod->core_layout.size;
matthieu castet84e1c6b2010-11-16 22:35:16 +01002360 break;
Jessica Yu444d13f2016-07-27 12:06:21 +09302361 case 2: /* RO after init */
2362 mod->core_layout.size = debug_align(mod->core_layout.size);
2363 mod->core_layout.ro_after_init_size = mod->core_layout.size;
2364 break;
2365 case 4: /* whole core */
Rusty Russell7523e4d2015-11-26 09:44:08 +10302366 mod->core_layout.size = debug_align(mod->core_layout.size);
matthieu castet84e1c6b2010-11-16 22:35:16 +01002367 break;
2368 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002369 }
2370
Jim Cromie5e124162011-12-06 12:11:31 -07002371 pr_debug("Init section allocation order:\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002372 for (m = 0; m < ARRAY_SIZE(masks); ++m) {
Rusty Russell49668682010-08-05 12:59:10 -06002373 for (i = 0; i < info->hdr->e_shnum; ++i) {
2374 Elf_Shdr *s = &info->sechdrs[i];
2375 const char *sname = info->secstrings + s->sh_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002376
2377 if ((s->sh_flags & masks[m][0]) != masks[m][0]
2378 || (s->sh_flags & masks[m][1])
2379 || s->sh_entsize != ~0UL
Rusty Russell49668682010-08-05 12:59:10 -06002380 || !strstarts(sname, ".init"))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002381 continue;
Rusty Russell7523e4d2015-11-26 09:44:08 +10302382 s->sh_entsize = (get_offset(mod, &mod->init_layout.size, s, i)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002383 | INIT_OFFSET_MASK);
Jim Cromie5e124162011-12-06 12:11:31 -07002384 pr_debug("\t%s\n", sname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002385 }
matthieu castet84e1c6b2010-11-16 22:35:16 +01002386 switch (m) {
2387 case 0: /* executable */
Rusty Russell7523e4d2015-11-26 09:44:08 +10302388 mod->init_layout.size = debug_align(mod->init_layout.size);
2389 mod->init_layout.text_size = mod->init_layout.size;
matthieu castet84e1c6b2010-11-16 22:35:16 +01002390 break;
2391 case 1: /* RO: text and ro-data */
Rusty Russell7523e4d2015-11-26 09:44:08 +10302392 mod->init_layout.size = debug_align(mod->init_layout.size);
2393 mod->init_layout.ro_size = mod->init_layout.size;
matthieu castet84e1c6b2010-11-16 22:35:16 +01002394 break;
Jessica Yu444d13f2016-07-27 12:06:21 +09302395 case 2:
2396 /*
2397 * RO after init doesn't apply to init_layout (only
2398 * core_layout), so it just takes the value of ro_size.
2399 */
2400 mod->init_layout.ro_after_init_size = mod->init_layout.ro_size;
2401 break;
2402 case 4: /* whole init */
Rusty Russell7523e4d2015-11-26 09:44:08 +10302403 mod->init_layout.size = debug_align(mod->init_layout.size);
matthieu castet84e1c6b2010-11-16 22:35:16 +01002404 break;
2405 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002406 }
2407}
2408
Linus Torvalds1da177e2005-04-16 15:20:36 -07002409static void set_license(struct module *mod, const char *license)
2410{
2411 if (!license)
2412 license = "unspecified";
2413
Florin Malitafa3ba2e82006-10-11 01:21:48 -07002414 if (!license_is_gpl_compatible(license)) {
Andi Kleen25ddbb12008-10-15 22:01:41 -07002415 if (!test_taint(TAINT_PROPRIETARY_MODULE))
Andrew Mortonbddb12b2013-11-12 15:11:28 -08002416 pr_warn("%s: module license '%s' taints kernel.\n",
2417 mod->name, license);
Rusty Russell373d4d02013-01-21 17:17:39 +10302418 add_taint_module(mod, TAINT_PROPRIETARY_MODULE,
2419 LOCKDEP_NOW_UNRELIABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002420 }
2421}
2422
2423/* Parse tag=value strings from .modinfo section */
2424static char *next_string(char *string, unsigned long *secsize)
2425{
2426 /* Skip non-zero chars */
2427 while (string[0]) {
2428 string++;
2429 if ((*secsize)-- <= 1)
2430 return NULL;
2431 }
2432
2433 /* Skip any zero padding. */
2434 while (!string[0]) {
2435 string++;
2436 if ((*secsize)-- <= 1)
2437 return NULL;
2438 }
2439 return string;
2440}
2441
Rusty Russell49668682010-08-05 12:59:10 -06002442static char *get_modinfo(struct load_info *info, const char *tag)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002443{
2444 char *p;
2445 unsigned int taglen = strlen(tag);
Rusty Russell49668682010-08-05 12:59:10 -06002446 Elf_Shdr *infosec = &info->sechdrs[info->index.info];
2447 unsigned long size = infosec->sh_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002448
Rusty Russell49668682010-08-05 12:59:10 -06002449 for (p = (char *)infosec->sh_addr; p; p = next_string(p, &size)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002450 if (strncmp(p, tag, taglen) == 0 && p[taglen] == '=')
2451 return p + taglen + 1;
2452 }
2453 return NULL;
2454}
2455
Rusty Russell49668682010-08-05 12:59:10 -06002456static void setup_modinfo(struct module *mod, struct load_info *info)
Matt Domschc988d2b2005-06-23 22:05:15 -07002457{
2458 struct module_attribute *attr;
2459 int i;
2460
2461 for (i = 0; (attr = modinfo_attrs[i]); i++) {
2462 if (attr->setup)
Rusty Russell49668682010-08-05 12:59:10 -06002463 attr->setup(mod, get_modinfo(info, attr->attr.name));
Matt Domschc988d2b2005-06-23 22:05:15 -07002464 }
2465}
Matt Domschc988d2b2005-06-23 22:05:15 -07002466
Rusty Russella263f7762009-09-25 00:32:58 -06002467static void free_modinfo(struct module *mod)
2468{
2469 struct module_attribute *attr;
2470 int i;
2471
2472 for (i = 0; (attr = modinfo_attrs[i]); i++) {
2473 if (attr->free)
2474 attr->free(mod);
2475 }
2476}
2477
Linus Torvalds1da177e2005-04-16 15:20:36 -07002478#ifdef CONFIG_KALLSYMS
WANG Cong15bba372008-07-24 15:41:48 +01002479
2480/* lookup symbol in given range of kernel_symbols */
2481static const struct kernel_symbol *lookup_symbol(const char *name,
2482 const struct kernel_symbol *start,
2483 const struct kernel_symbol *stop)
2484{
Alessio Igor Bogani9d634872011-05-18 22:35:59 +02002485 return bsearch(name, start, stop - start,
2486 sizeof(struct kernel_symbol), cmp_name);
WANG Cong15bba372008-07-24 15:41:48 +01002487}
2488
Tim Abbottca4787b2009-01-05 08:40:10 -06002489static int is_exported(const char *name, unsigned long value,
2490 const struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002491{
Tim Abbottca4787b2009-01-05 08:40:10 -06002492 const struct kernel_symbol *ks;
2493 if (!mod)
2494 ks = lookup_symbol(name, __start___ksymtab, __stop___ksymtab);
Sam Ravnborg3fd68052006-02-08 21:16:45 +01002495 else
Tim Abbottca4787b2009-01-05 08:40:10 -06002496 ks = lookup_symbol(name, mod->syms, mod->syms + mod->num_syms);
2497 return ks != NULL && ks->value == value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002498}
2499
2500/* As per nm */
Rusty Russelleded41c2010-08-05 12:59:07 -06002501static char elf_type(const Elf_Sym *sym, const struct load_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002502{
Rusty Russelleded41c2010-08-05 12:59:07 -06002503 const Elf_Shdr *sechdrs = info->sechdrs;
2504
Linus Torvalds1da177e2005-04-16 15:20:36 -07002505 if (ELF_ST_BIND(sym->st_info) == STB_WEAK) {
2506 if (ELF_ST_TYPE(sym->st_info) == STT_OBJECT)
2507 return 'v';
2508 else
2509 return 'w';
2510 }
2511 if (sym->st_shndx == SHN_UNDEF)
2512 return 'U';
Miroslav Benese0224412015-11-26 13:18:06 +10302513 if (sym->st_shndx == SHN_ABS || sym->st_shndx == info->index.pcpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002514 return 'a';
2515 if (sym->st_shndx >= SHN_LORESERVE)
2516 return '?';
2517 if (sechdrs[sym->st_shndx].sh_flags & SHF_EXECINSTR)
2518 return 't';
2519 if (sechdrs[sym->st_shndx].sh_flags & SHF_ALLOC
2520 && sechdrs[sym->st_shndx].sh_type != SHT_NOBITS) {
2521 if (!(sechdrs[sym->st_shndx].sh_flags & SHF_WRITE))
2522 return 'r';
2523 else if (sechdrs[sym->st_shndx].sh_flags & ARCH_SHF_SMALL)
2524 return 'g';
2525 else
2526 return 'd';
2527 }
2528 if (sechdrs[sym->st_shndx].sh_type == SHT_NOBITS) {
2529 if (sechdrs[sym->st_shndx].sh_flags & ARCH_SHF_SMALL)
2530 return 's';
2531 else
2532 return 'b';
2533 }
Rusty Russelleded41c2010-08-05 12:59:07 -06002534 if (strstarts(info->secstrings + sechdrs[sym->st_shndx].sh_name,
2535 ".debug")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002536 return 'n';
Rusty Russelleded41c2010-08-05 12:59:07 -06002537 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002538 return '?';
2539}
2540
Jan Beulich4a496222009-07-06 14:50:42 +01002541static bool is_core_symbol(const Elf_Sym *src, const Elf_Shdr *sechdrs,
Miroslav Benese0224412015-11-26 13:18:06 +10302542 unsigned int shnum, unsigned int pcpundx)
Jan Beulich4a496222009-07-06 14:50:42 +01002543{
2544 const Elf_Shdr *sec;
2545
2546 if (src->st_shndx == SHN_UNDEF
2547 || src->st_shndx >= shnum
2548 || !src->st_name)
2549 return false;
2550
Miroslav Benese0224412015-11-26 13:18:06 +10302551#ifdef CONFIG_KALLSYMS_ALL
2552 if (src->st_shndx == pcpundx)
2553 return true;
2554#endif
2555
Jan Beulich4a496222009-07-06 14:50:42 +01002556 sec = sechdrs + src->st_shndx;
2557 if (!(sec->sh_flags & SHF_ALLOC)
2558#ifndef CONFIG_KALLSYMS_ALL
2559 || !(sec->sh_flags & SHF_EXECINSTR)
2560#endif
2561 || (sec->sh_entsize & INIT_OFFSET_MASK))
2562 return false;
2563
2564 return true;
2565}
2566
Kevin Cernekee48fd1182012-01-13 09:32:14 +10302567/*
2568 * We only allocate and copy the strings needed by the parts of symtab
2569 * we keep. This is simple, but has the effect of making multiple
2570 * copies of duplicates. We could be more sophisticated, see
2571 * linux-kernel thread starting with
2572 * <73defb5e4bca04a6431392cc341112b1@localhost>.
2573 */
Rusty Russell49668682010-08-05 12:59:10 -06002574static void layout_symtab(struct module *mod, struct load_info *info)
Jan Beulich4a496222009-07-06 14:50:42 +01002575{
Rusty Russell49668682010-08-05 12:59:10 -06002576 Elf_Shdr *symsect = info->sechdrs + info->index.sym;
2577 Elf_Shdr *strsect = info->sechdrs + info->index.str;
Jan Beulich4a496222009-07-06 14:50:42 +01002578 const Elf_Sym *src;
Satoru Takeuchi54523ec2012-12-05 12:29:04 +10302579 unsigned int i, nsrc, ndst, strtab_size = 0;
Jan Beulich4a496222009-07-06 14:50:42 +01002580
2581 /* Put symbol section at end of init part of module. */
2582 symsect->sh_flags |= SHF_ALLOC;
Rusty Russell7523e4d2015-11-26 09:44:08 +10302583 symsect->sh_entsize = get_offset(mod, &mod->init_layout.size, symsect,
Rusty Russell49668682010-08-05 12:59:10 -06002584 info->index.sym) | INIT_OFFSET_MASK;
Jim Cromie5e124162011-12-06 12:11:31 -07002585 pr_debug("\t%s\n", info->secstrings + symsect->sh_name);
Jan Beulich4a496222009-07-06 14:50:42 +01002586
Rusty Russell49668682010-08-05 12:59:10 -06002587 src = (void *)info->hdr + symsect->sh_offset;
Jan Beulich4a496222009-07-06 14:50:42 +01002588 nsrc = symsect->sh_size / sizeof(*src);
Kevin Cernekee70b1e9162011-11-12 19:08:55 -08002589
Kevin Cernekee48fd1182012-01-13 09:32:14 +10302590 /* Compute total space required for the core symbols' strtab. */
Rusty Russell59ef28b2012-10-25 10:49:25 +10302591 for (ndst = i = 0; i < nsrc; i++) {
Jessica Yu1ce15ef2016-03-22 20:03:16 -04002592 if (i == 0 || is_livepatch_module(mod) ||
Miroslav Benese0224412015-11-26 13:18:06 +10302593 is_core_symbol(src+i, info->sechdrs, info->hdr->e_shnum,
2594 info->index.pcpu)) {
Rusty Russell59ef28b2012-10-25 10:49:25 +10302595 strtab_size += strlen(&info->strtab[src[i].st_name])+1;
Kevin Cernekee48fd1182012-01-13 09:32:14 +10302596 ndst++;
Jan Beulich554bdfe2009-07-06 14:51:44 +01002597 }
Rusty Russell59ef28b2012-10-25 10:49:25 +10302598 }
Jan Beulich4a496222009-07-06 14:50:42 +01002599
2600 /* Append room for core symbols at end of core part. */
Rusty Russell7523e4d2015-11-26 09:44:08 +10302601 info->symoffs = ALIGN(mod->core_layout.size, symsect->sh_addralign ?: 1);
2602 info->stroffs = mod->core_layout.size = info->symoffs + ndst * sizeof(Elf_Sym);
2603 mod->core_layout.size += strtab_size;
2604 mod->core_layout.size = debug_align(mod->core_layout.size);
Jan Beulich4a496222009-07-06 14:50:42 +01002605
Jan Beulich554bdfe2009-07-06 14:51:44 +01002606 /* Put string table section at end of init part of module. */
2607 strsect->sh_flags |= SHF_ALLOC;
Rusty Russell7523e4d2015-11-26 09:44:08 +10302608 strsect->sh_entsize = get_offset(mod, &mod->init_layout.size, strsect,
Rusty Russell49668682010-08-05 12:59:10 -06002609 info->index.str) | INIT_OFFSET_MASK;
Jim Cromie5e124162011-12-06 12:11:31 -07002610 pr_debug("\t%s\n", info->secstrings + strsect->sh_name);
Rusty Russell82440622016-02-03 16:55:26 +10302611
2612 /* We'll tack temporary mod_kallsyms on the end. */
2613 mod->init_layout.size = ALIGN(mod->init_layout.size,
2614 __alignof__(struct mod_kallsyms));
2615 info->mod_kallsyms_init_off = mod->init_layout.size;
2616 mod->init_layout.size += sizeof(struct mod_kallsyms);
2617 mod->init_layout.size = debug_align(mod->init_layout.size);
Jan Beulich4a496222009-07-06 14:50:42 +01002618}
2619
Rusty Russell82440622016-02-03 16:55:26 +10302620/*
2621 * We use the full symtab and strtab which layout_symtab arranged to
2622 * be appended to the init section. Later we switch to the cut-down
2623 * core-only ones.
2624 */
Rusty Russell811d66a2010-08-05 12:59:12 -06002625static void add_kallsyms(struct module *mod, const struct load_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002626{
Jan Beulich4a496222009-07-06 14:50:42 +01002627 unsigned int i, ndst;
2628 const Elf_Sym *src;
2629 Elf_Sym *dst;
Jan Beulich554bdfe2009-07-06 14:51:44 +01002630 char *s;
Rusty Russelleded41c2010-08-05 12:59:07 -06002631 Elf_Shdr *symsec = &info->sechdrs[info->index.sym];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002632
Rusty Russell82440622016-02-03 16:55:26 +10302633 /* Set up to point into init section. */
2634 mod->kallsyms = mod->init_layout.base + info->mod_kallsyms_init_off;
2635
2636 mod->kallsyms->symtab = (void *)symsec->sh_addr;
2637 mod->kallsyms->num_symtab = symsec->sh_size / sizeof(Elf_Sym);
Rusty Russell511ca6a2010-08-05 12:59:08 -06002638 /* Make sure we get permanent strtab: don't use info->strtab. */
Rusty Russell82440622016-02-03 16:55:26 +10302639 mod->kallsyms->strtab = (void *)info->sechdrs[info->index.str].sh_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002640
2641 /* Set types up while we still have access to sections. */
Rusty Russell82440622016-02-03 16:55:26 +10302642 for (i = 0; i < mod->kallsyms->num_symtab; i++)
2643 mod->kallsyms->symtab[i].st_info
2644 = elf_type(&mod->kallsyms->symtab[i], info);
Jan Beulich4a496222009-07-06 14:50:42 +01002645
Rusty Russell82440622016-02-03 16:55:26 +10302646 /* Now populate the cut down core kallsyms for after init. */
2647 mod->core_kallsyms.symtab = dst = mod->core_layout.base + info->symoffs;
2648 mod->core_kallsyms.strtab = s = mod->core_layout.base + info->stroffs;
2649 src = mod->kallsyms->symtab;
2650 for (ndst = i = 0; i < mod->kallsyms->num_symtab; i++) {
Jessica Yu1ce15ef2016-03-22 20:03:16 -04002651 if (i == 0 || is_livepatch_module(mod) ||
Miroslav Benese0224412015-11-26 13:18:06 +10302652 is_core_symbol(src+i, info->sechdrs, info->hdr->e_shnum,
2653 info->index.pcpu)) {
Rusty Russell59ef28b2012-10-25 10:49:25 +10302654 dst[ndst] = src[i];
Rusty Russell82440622016-02-03 16:55:26 +10302655 dst[ndst++].st_name = s - mod->core_kallsyms.strtab;
2656 s += strlcpy(s, &mod->kallsyms->strtab[src[i].st_name],
Rusty Russell59ef28b2012-10-25 10:49:25 +10302657 KSYM_NAME_LEN) + 1;
2658 }
Jan Beulich4a496222009-07-06 14:50:42 +01002659 }
Rusty Russell82440622016-02-03 16:55:26 +10302660 mod->core_kallsyms.num_symtab = ndst;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002661}
2662#else
Rusty Russell49668682010-08-05 12:59:10 -06002663static inline void layout_symtab(struct module *mod, struct load_info *info)
Jan Beulich4a496222009-07-06 14:50:42 +01002664{
2665}
Paul Mundt3ae91c22009-10-01 15:43:54 -07002666
Michał Mirosławabbce902010-09-20 01:58:08 +02002667static void add_kallsyms(struct module *mod, const struct load_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002668{
2669}
2670#endif /* CONFIG_KALLSYMS */
2671
Jason Barone9d376f2009-02-05 11:51:38 -05002672static void dynamic_debug_setup(struct _ddebug *debug, unsigned int num)
Rusty Russell5e458cc2008-10-22 10:00:13 -05002673{
Rusty Russell811d66a2010-08-05 12:59:12 -06002674 if (!debug)
2675 return;
Jason Barone9d376f2009-02-05 11:51:38 -05002676#ifdef CONFIG_DYNAMIC_DEBUG
2677 if (ddebug_add_module(debug, num, debug->modname))
Andrew Mortonbddb12b2013-11-12 15:11:28 -08002678 pr_err("dynamic debug error adding module: %s\n",
2679 debug->modname);
Jason Barone9d376f2009-02-05 11:51:38 -05002680#endif
Rusty Russell5e458cc2008-10-22 10:00:13 -05002681}
Jason Baron346e15b2008-08-12 16:46:19 -04002682
Yehuda Sadehff49d742010-07-03 13:07:35 +10002683static void dynamic_debug_remove(struct _ddebug *debug)
2684{
2685 if (debug)
2686 ddebug_remove_module(debug->modname);
2687}
2688
Jonas Bonn74e08fc2011-06-30 21:22:11 +02002689void * __weak module_alloc(unsigned long size)
2690{
Rusty Russell82fab442012-12-11 09:38:33 +10302691 return vmalloc_exec(size);
Jonas Bonn74e08fc2011-06-30 21:22:11 +02002692}
2693
Catalin Marinas4f2294b2009-06-11 13:23:20 +01002694#ifdef CONFIG_DEBUG_KMEMLEAK
Rusty Russell49668682010-08-05 12:59:10 -06002695static void kmemleak_load_module(const struct module *mod,
2696 const struct load_info *info)
Catalin Marinas4f2294b2009-06-11 13:23:20 +01002697{
2698 unsigned int i;
2699
2700 /* only scan the sections containing data */
Catalin Marinasc017b4be32009-10-28 13:33:09 +00002701 kmemleak_scan_area(mod, sizeof(struct module), GFP_KERNEL);
Catalin Marinas4f2294b2009-06-11 13:23:20 +01002702
Rusty Russell49668682010-08-05 12:59:10 -06002703 for (i = 1; i < info->hdr->e_shnum; i++) {
Steven Rostedt06c94942013-05-15 20:33:01 +01002704 /* Scan all writable sections that's not executable */
2705 if (!(info->sechdrs[i].sh_flags & SHF_ALLOC) ||
2706 !(info->sechdrs[i].sh_flags & SHF_WRITE) ||
2707 (info->sechdrs[i].sh_flags & SHF_EXECINSTR))
Catalin Marinas4f2294b2009-06-11 13:23:20 +01002708 continue;
2709
Rusty Russell49668682010-08-05 12:59:10 -06002710 kmemleak_scan_area((void *)info->sechdrs[i].sh_addr,
2711 info->sechdrs[i].sh_size, GFP_KERNEL);
Catalin Marinas4f2294b2009-06-11 13:23:20 +01002712 }
2713}
2714#else
Rusty Russell49668682010-08-05 12:59:10 -06002715static inline void kmemleak_load_module(const struct module *mod,
2716 const struct load_info *info)
Catalin Marinas4f2294b2009-06-11 13:23:20 +01002717{
2718}
2719#endif
2720
Rusty Russell106a4ee2012-09-26 10:09:40 +01002721#ifdef CONFIG_MODULE_SIG
Ben Hutchingsbca014c2016-04-28 09:24:01 +09302722static int module_sig_check(struct load_info *info, int flags)
Rusty Russell106a4ee2012-09-26 10:09:40 +01002723{
2724 int err = -ENOKEY;
Kees Cook34e11692012-10-16 07:31:07 +10302725 const unsigned long markerlen = sizeof(MODULE_SIG_STRING) - 1;
2726 const void *mod = info->hdr;
Rusty Russell106a4ee2012-09-26 10:09:40 +01002727
Ben Hutchingsbca014c2016-04-28 09:24:01 +09302728 /*
2729 * Require flags == 0, as a module with version information
2730 * removed is no longer the module that was signed
2731 */
2732 if (flags == 0 &&
2733 info->len > markerlen &&
Kees Cook34e11692012-10-16 07:31:07 +10302734 memcmp(mod + info->len - markerlen, MODULE_SIG_STRING, markerlen) == 0) {
David Howellscaabe242012-10-20 01:19:29 +01002735 /* We truncate the module to discard the signature */
Kees Cook34e11692012-10-16 07:31:07 +10302736 info->len -= markerlen;
2737 err = mod_verify_sig(mod, &info->len);
Rusty Russell106a4ee2012-09-26 10:09:40 +01002738 }
2739
2740 if (!err) {
2741 info->sig_ok = true;
2742 return 0;
2743 }
2744
2745 /* Not having a signature is only an error if we're strict. */
2746 if (err == -ENOKEY && !sig_enforce)
2747 err = 0;
2748
2749 return err;
2750}
2751#else /* !CONFIG_MODULE_SIG */
Ben Hutchingsbca014c2016-04-28 09:24:01 +09302752static int module_sig_check(struct load_info *info, int flags)
Rusty Russell106a4ee2012-09-26 10:09:40 +01002753{
2754 return 0;
2755}
2756#endif /* !CONFIG_MODULE_SIG */
2757
Kees Cook34e11692012-10-16 07:31:07 +10302758/* Sanity checks against invalid binaries, wrong arch, weird elf version. */
2759static int elf_header_check(struct load_info *info)
Rusty Russell40dd2562010-08-05 12:59:03 -06002760{
Kees Cook34e11692012-10-16 07:31:07 +10302761 if (info->len < sizeof(*(info->hdr)))
Rusty Russell40dd2562010-08-05 12:59:03 -06002762 return -ENOEXEC;
2763
Kees Cook34e11692012-10-16 07:31:07 +10302764 if (memcmp(info->hdr->e_ident, ELFMAG, SELFMAG) != 0
2765 || info->hdr->e_type != ET_REL
2766 || !elf_check_arch(info->hdr)
2767 || info->hdr->e_shentsize != sizeof(Elf_Shdr))
2768 return -ENOEXEC;
2769
2770 if (info->hdr->e_shoff >= info->len
2771 || (info->hdr->e_shnum * sizeof(Elf_Shdr) >
2772 info->len - info->hdr->e_shoff))
2773 return -ENOEXEC;
2774
2775 return 0;
2776}
2777
Linus Torvalds3afe9f82015-04-07 10:33:49 -07002778#define COPY_CHUNK_SIZE (16*PAGE_SIZE)
2779
2780static int copy_chunked_from_user(void *dst, const void __user *usrc, unsigned long len)
2781{
2782 do {
2783 unsigned long n = min(len, COPY_CHUNK_SIZE);
2784
2785 if (copy_from_user(dst, usrc, n) != 0)
2786 return -EFAULT;
2787 cond_resched();
2788 dst += n;
2789 usrc += n;
2790 len -= n;
2791 } while (len);
2792 return 0;
2793}
2794
Jessica Yu1ce15ef2016-03-22 20:03:16 -04002795#ifdef CONFIG_LIVEPATCH
2796static int find_livepatch_modinfo(struct module *mod, struct load_info *info)
2797{
2798 mod->klp = get_modinfo(info, "livepatch") ? true : false;
2799
2800 return 0;
2801}
2802#else /* !CONFIG_LIVEPATCH */
2803static int find_livepatch_modinfo(struct module *mod, struct load_info *info)
2804{
2805 if (get_modinfo(info, "livepatch")) {
2806 pr_err("%s: module is marked as livepatch module, but livepatch support is disabled",
2807 mod->name);
2808 return -ENOEXEC;
2809 }
2810
2811 return 0;
2812}
2813#endif /* CONFIG_LIVEPATCH */
2814
Kees Cook34e11692012-10-16 07:31:07 +10302815/* Sets info->hdr and info->len. */
2816static int copy_module_from_user(const void __user *umod, unsigned long len,
2817 struct load_info *info)
2818{
Kees Cook2e72d512012-10-16 07:32:07 +10302819 int err;
2820
Kees Cook34e11692012-10-16 07:31:07 +10302821 info->len = len;
2822 if (info->len < sizeof(*(info->hdr)))
Rusty Russell40dd2562010-08-05 12:59:03 -06002823 return -ENOEXEC;
2824
Mimi Zohara1db7422015-12-30 07:35:30 -05002825 err = security_kernel_read_file(NULL, READING_MODULE);
Kees Cook2e72d512012-10-16 07:32:07 +10302826 if (err)
2827 return err;
2828
Rusty Russell40dd2562010-08-05 12:59:03 -06002829 /* Suck in entire file: we'll want most of it. */
Kirill A. Shutemovcc9e6052015-03-24 12:31:40 +10302830 info->hdr = __vmalloc(info->len,
2831 GFP_KERNEL | __GFP_HIGHMEM | __GFP_NOWARN, PAGE_KERNEL);
Kees Cook34e11692012-10-16 07:31:07 +10302832 if (!info->hdr)
Rusty Russell40dd2562010-08-05 12:59:03 -06002833 return -ENOMEM;
2834
Linus Torvalds3afe9f82015-04-07 10:33:49 -07002835 if (copy_chunked_from_user(info->hdr, umod, info->len) != 0) {
Kees Cook34e11692012-10-16 07:31:07 +10302836 vfree(info->hdr);
2837 return -EFAULT;
Rusty Russell40dd2562010-08-05 12:59:03 -06002838 }
2839
Rusty Russell40dd2562010-08-05 12:59:03 -06002840 return 0;
Kees Cook34e11692012-10-16 07:31:07 +10302841}
Rusty Russell40dd2562010-08-05 12:59:03 -06002842
Rusty Russelld9131882010-08-05 12:59:08 -06002843static void free_copy(struct load_info *info)
2844{
Rusty Russelld9131882010-08-05 12:59:08 -06002845 vfree(info->hdr);
2846}
2847
Rusty Russell2f3238a2012-10-22 18:09:41 +10302848static int rewrite_section_headers(struct load_info *info, int flags)
Rusty Russell8b5f61a2010-08-05 12:59:06 -06002849{
2850 unsigned int i;
2851
2852 /* This should always be true, but let's be sure. */
2853 info->sechdrs[0].sh_addr = 0;
2854
2855 for (i = 1; i < info->hdr->e_shnum; i++) {
2856 Elf_Shdr *shdr = &info->sechdrs[i];
2857 if (shdr->sh_type != SHT_NOBITS
2858 && info->len < shdr->sh_offset + shdr->sh_size) {
Andrew Mortonbddb12b2013-11-12 15:11:28 -08002859 pr_err("Module len %lu truncated\n", info->len);
Rusty Russell8b5f61a2010-08-05 12:59:06 -06002860 return -ENOEXEC;
2861 }
2862
2863 /* Mark all sections sh_addr with their address in the
2864 temporary image. */
2865 shdr->sh_addr = (size_t)info->hdr + shdr->sh_offset;
2866
2867#ifndef CONFIG_MODULE_UNLOAD
2868 /* Don't load .exit sections */
2869 if (strstarts(info->secstrings+shdr->sh_name, ".exit"))
2870 shdr->sh_flags &= ~(unsigned long)SHF_ALLOC;
2871#endif
Rusty Russell8b5f61a2010-08-05 12:59:06 -06002872 }
Rusty Russelld6df72a2010-08-05 12:59:07 -06002873
2874 /* Track but don't keep modinfo and version sections. */
Rusty Russell2f3238a2012-10-22 18:09:41 +10302875 if (flags & MODULE_INIT_IGNORE_MODVERSIONS)
2876 info->index.vers = 0; /* Pretend no __versions section! */
2877 else
2878 info->index.vers = find_sec(info, "__versions");
Rusty Russell49668682010-08-05 12:59:10 -06002879 info->index.info = find_sec(info, ".modinfo");
Rusty Russelld6df72a2010-08-05 12:59:07 -06002880 info->sechdrs[info->index.info].sh_flags &= ~(unsigned long)SHF_ALLOC;
2881 info->sechdrs[info->index.vers].sh_flags &= ~(unsigned long)SHF_ALLOC;
Rusty Russell8b5f61a2010-08-05 12:59:06 -06002882 return 0;
2883}
2884
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002885/*
2886 * Set up our basic convenience variables (pointers to section headers,
2887 * search for module section index etc), and do some basic section
2888 * verification.
2889 *
2890 * Return the temporary module pointer (we'll replace it with the final
2891 * one when we move the module sections around).
2892 */
Rusty Russell2f3238a2012-10-22 18:09:41 +10302893static struct module *setup_load_info(struct load_info *info, int flags)
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002894{
2895 unsigned int i;
Rusty Russell8b5f61a2010-08-05 12:59:06 -06002896 int err;
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002897 struct module *mod;
2898
2899 /* Set up the convenience variables */
2900 info->sechdrs = (void *)info->hdr + info->hdr->e_shoff;
Rusty Russell8b5f61a2010-08-05 12:59:06 -06002901 info->secstrings = (void *)info->hdr
2902 + info->sechdrs[info->hdr->e_shstrndx].sh_offset;
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002903
Rusty Russell2f3238a2012-10-22 18:09:41 +10302904 err = rewrite_section_headers(info, flags);
Rusty Russell8b5f61a2010-08-05 12:59:06 -06002905 if (err)
2906 return ERR_PTR(err);
2907
2908 /* Find internal symbols and strings. */
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002909 for (i = 1; i < info->hdr->e_shnum; i++) {
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002910 if (info->sechdrs[i].sh_type == SHT_SYMTAB) {
2911 info->index.sym = i;
2912 info->index.str = info->sechdrs[i].sh_link;
Rusty Russell8b5f61a2010-08-05 12:59:06 -06002913 info->strtab = (char *)info->hdr
2914 + info->sechdrs[info->index.str].sh_offset;
2915 break;
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002916 }
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002917 }
2918
Rusty Russell49668682010-08-05 12:59:10 -06002919 info->index.mod = find_sec(info, ".gnu.linkonce.this_module");
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002920 if (!info->index.mod) {
Andrew Mortonbddb12b2013-11-12 15:11:28 -08002921 pr_warn("No module found in object\n");
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002922 return ERR_PTR(-ENOEXEC);
2923 }
2924 /* This is temporary: point mod into copy of data. */
2925 mod = (void *)info->sechdrs[info->index.mod].sh_addr;
2926
2927 if (info->index.sym == 0) {
Andrew Mortonbddb12b2013-11-12 15:11:28 -08002928 pr_warn("%s: module has no symbols (stripped?)\n", mod->name);
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002929 return ERR_PTR(-ENOEXEC);
2930 }
2931
Rusty Russell49668682010-08-05 12:59:10 -06002932 info->index.pcpu = find_pcpusec(info);
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002933
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002934 /* Check module struct version now, before we try to use module. */
2935 if (!check_modstruct_version(info->sechdrs, info->index.vers, mod))
2936 return ERR_PTR(-ENOEXEC);
2937
2938 return mod;
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002939}
2940
Rusty Russell2f3238a2012-10-22 18:09:41 +10302941static int check_modinfo(struct module *mod, struct load_info *info, int flags)
Rusty Russell40dd2562010-08-05 12:59:03 -06002942{
Rusty Russell49668682010-08-05 12:59:10 -06002943 const char *modmagic = get_modinfo(info, "vermagic");
Rusty Russell40dd2562010-08-05 12:59:03 -06002944 int err;
2945
Rusty Russell2f3238a2012-10-22 18:09:41 +10302946 if (flags & MODULE_INIT_IGNORE_VERMAGIC)
2947 modmagic = NULL;
2948
Rusty Russell40dd2562010-08-05 12:59:03 -06002949 /* This is allowed: modprobe --force will invalidate it. */
2950 if (!modmagic) {
2951 err = try_to_force_load(mod, "bad vermagic");
2952 if (err)
2953 return err;
Rusty Russell49668682010-08-05 12:59:10 -06002954 } else if (!same_magic(modmagic, vermagic, info->index.vers)) {
Andrew Mortonbddb12b2013-11-12 15:11:28 -08002955 pr_err("%s: version magic '%s' should be '%s'\n",
Rusty Russell40dd2562010-08-05 12:59:03 -06002956 mod->name, modmagic, vermagic);
2957 return -ENOEXEC;
2958 }
2959
Libor Pechacek3205c362016-04-13 11:06:12 +09302960 if (!get_modinfo(info, "intree")) {
2961 if (!test_taint(TAINT_OOT_MODULE))
2962 pr_warn("%s: loading out-of-tree module taints kernel.\n",
2963 mod->name);
Rusty Russell373d4d02013-01-21 17:17:39 +10302964 add_taint_module(mod, TAINT_OOT_MODULE, LOCKDEP_STILL_OK);
Libor Pechacek3205c362016-04-13 11:06:12 +09302965 }
Ben Hutchings2449b8b2011-10-24 15:12:28 +02002966
Rusty Russell49668682010-08-05 12:59:10 -06002967 if (get_modinfo(info, "staging")) {
Rusty Russell373d4d02013-01-21 17:17:39 +10302968 add_taint_module(mod, TAINT_CRAP, LOCKDEP_STILL_OK);
Andrew Mortonbddb12b2013-11-12 15:11:28 -08002969 pr_warn("%s: module is from the staging directory, the quality "
2970 "is unknown, you have been warned.\n", mod->name);
Rusty Russell40dd2562010-08-05 12:59:03 -06002971 }
Rusty Russell22e268e2010-08-05 12:59:05 -06002972
Jessica Yu1ce15ef2016-03-22 20:03:16 -04002973 err = find_livepatch_modinfo(mod, info);
2974 if (err)
2975 return err;
2976
Rusty Russell22e268e2010-08-05 12:59:05 -06002977 /* Set up license info based on the info section */
Rusty Russell49668682010-08-05 12:59:10 -06002978 set_license(mod, get_modinfo(info, "license"));
Rusty Russell22e268e2010-08-05 12:59:05 -06002979
Rusty Russell40dd2562010-08-05 12:59:03 -06002980 return 0;
2981}
2982
Frantisek Hrbataeb3057d2013-10-14 18:08:46 +10302983static int find_module_sections(struct module *mod, struct load_info *info)
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06002984{
Rusty Russell49668682010-08-05 12:59:10 -06002985 mod->kp = section_objs(info, "__param",
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06002986 sizeof(*mod->kp), &mod->num_kp);
Rusty Russell49668682010-08-05 12:59:10 -06002987 mod->syms = section_objs(info, "__ksymtab",
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06002988 sizeof(*mod->syms), &mod->num_syms);
Rusty Russell49668682010-08-05 12:59:10 -06002989 mod->crcs = section_addr(info, "__kcrctab");
2990 mod->gpl_syms = section_objs(info, "__ksymtab_gpl",
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06002991 sizeof(*mod->gpl_syms),
2992 &mod->num_gpl_syms);
Rusty Russell49668682010-08-05 12:59:10 -06002993 mod->gpl_crcs = section_addr(info, "__kcrctab_gpl");
2994 mod->gpl_future_syms = section_objs(info,
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06002995 "__ksymtab_gpl_future",
2996 sizeof(*mod->gpl_future_syms),
2997 &mod->num_gpl_future_syms);
Rusty Russell49668682010-08-05 12:59:10 -06002998 mod->gpl_future_crcs = section_addr(info, "__kcrctab_gpl_future");
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06002999
3000#ifdef CONFIG_UNUSED_SYMBOLS
Rusty Russell49668682010-08-05 12:59:10 -06003001 mod->unused_syms = section_objs(info, "__ksymtab_unused",
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06003002 sizeof(*mod->unused_syms),
3003 &mod->num_unused_syms);
Rusty Russell49668682010-08-05 12:59:10 -06003004 mod->unused_crcs = section_addr(info, "__kcrctab_unused");
3005 mod->unused_gpl_syms = section_objs(info, "__ksymtab_unused_gpl",
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06003006 sizeof(*mod->unused_gpl_syms),
3007 &mod->num_unused_gpl_syms);
Rusty Russell49668682010-08-05 12:59:10 -06003008 mod->unused_gpl_crcs = section_addr(info, "__kcrctab_unused_gpl");
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06003009#endif
3010#ifdef CONFIG_CONSTRUCTORS
Rusty Russell49668682010-08-05 12:59:10 -06003011 mod->ctors = section_objs(info, ".ctors",
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06003012 sizeof(*mod->ctors), &mod->num_ctors);
Frantisek Hrbataeb3057d2013-10-14 18:08:46 +10303013 if (!mod->ctors)
3014 mod->ctors = section_objs(info, ".init_array",
3015 sizeof(*mod->ctors), &mod->num_ctors);
3016 else if (find_sec(info, ".init_array")) {
3017 /*
3018 * This shouldn't happen with same compiler and binutils
3019 * building all parts of the module.
3020 */
Ionut Alexa6da0b562014-11-10 09:31:29 +10303021 pr_warn("%s: has both .ctors and .init_array.\n",
Frantisek Hrbataeb3057d2013-10-14 18:08:46 +10303022 mod->name);
3023 return -EINVAL;
3024 }
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06003025#endif
3026
3027#ifdef CONFIG_TRACEPOINTS
Mathieu Desnoyers65498642011-01-26 17:26:22 -05003028 mod->tracepoints_ptrs = section_objs(info, "__tracepoints_ptrs",
3029 sizeof(*mod->tracepoints_ptrs),
3030 &mod->num_tracepoints);
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06003031#endif
Jason Baronbf5438fc2010-09-17 11:09:00 -04003032#ifdef HAVE_JUMP_LABEL
3033 mod->jump_entries = section_objs(info, "__jump_table",
3034 sizeof(*mod->jump_entries),
3035 &mod->num_jump_entries);
3036#endif
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06003037#ifdef CONFIG_EVENT_TRACING
Rusty Russell49668682010-08-05 12:59:10 -06003038 mod->trace_events = section_objs(info, "_ftrace_events",
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06003039 sizeof(*mod->trace_events),
3040 &mod->num_trace_events);
Steven Rostedt (Red Hat)3673b8e2015-03-25 15:44:21 -04003041 mod->trace_enums = section_objs(info, "_ftrace_enum_map",
3042 sizeof(*mod->trace_enums),
3043 &mod->num_trace_enums);
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06003044#endif
Steven Rostedt13b9b6e2010-11-10 22:19:24 -05003045#ifdef CONFIG_TRACING
3046 mod->trace_bprintk_fmt_start = section_objs(info, "__trace_printk_fmt",
3047 sizeof(*mod->trace_bprintk_fmt_start),
3048 &mod->num_trace_bprintk_fmt);
Steven Rostedt13b9b6e2010-11-10 22:19:24 -05003049#endif
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06003050#ifdef CONFIG_FTRACE_MCOUNT_RECORD
3051 /* sechdrs[0].sh_size is always zero */
Rusty Russell49668682010-08-05 12:59:10 -06003052 mod->ftrace_callsites = section_objs(info, "__mcount_loc",
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06003053 sizeof(*mod->ftrace_callsites),
3054 &mod->num_ftrace_callsites);
3055#endif
Rusty Russell22e268e2010-08-05 12:59:05 -06003056
Rusty Russell811d66a2010-08-05 12:59:12 -06003057 mod->extable = section_objs(info, "__ex_table",
3058 sizeof(*mod->extable), &mod->num_exentries);
3059
Rusty Russell49668682010-08-05 12:59:10 -06003060 if (section_addr(info, "__obsparm"))
Andrew Mortonbddb12b2013-11-12 15:11:28 -08003061 pr_warn("%s: Ignoring obsolete parameters\n", mod->name);
Rusty Russell811d66a2010-08-05 12:59:12 -06003062
3063 info->debug = section_objs(info, "__verbose",
3064 sizeof(*info->debug), &info->num_debug);
Frantisek Hrbataeb3057d2013-10-14 18:08:46 +10303065
3066 return 0;
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06003067}
3068
Rusty Russell49668682010-08-05 12:59:10 -06003069static int move_module(struct module *mod, struct load_info *info)
Linus Torvalds65b8a9b2010-08-05 12:59:02 -06003070{
3071 int i;
3072 void *ptr;
3073
3074 /* Do the allocs. */
Rusty Russell7523e4d2015-11-26 09:44:08 +10303075 ptr = module_alloc(mod->core_layout.size);
Linus Torvalds65b8a9b2010-08-05 12:59:02 -06003076 /*
3077 * The pointer to this block is stored in the module structure
3078 * which is inside the block. Just mark it as not being a
3079 * leak.
3080 */
3081 kmemleak_not_leak(ptr);
3082 if (!ptr)
Rusty Russelld9131882010-08-05 12:59:08 -06003083 return -ENOMEM;
Linus Torvalds65b8a9b2010-08-05 12:59:02 -06003084
Rusty Russell7523e4d2015-11-26 09:44:08 +10303085 memset(ptr, 0, mod->core_layout.size);
3086 mod->core_layout.base = ptr;
Linus Torvalds65b8a9b2010-08-05 12:59:02 -06003087
Rusty Russell7523e4d2015-11-26 09:44:08 +10303088 if (mod->init_layout.size) {
3089 ptr = module_alloc(mod->init_layout.size);
Rusty Russell82fab442012-12-11 09:38:33 +10303090 /*
3091 * The pointer to this block is stored in the module structure
3092 * which is inside the block. This block doesn't need to be
3093 * scanned as it contains data and code that will be freed
3094 * after the module is initialized.
3095 */
3096 kmemleak_ignore(ptr);
3097 if (!ptr) {
Rusty Russell7523e4d2015-11-26 09:44:08 +10303098 module_memfree(mod->core_layout.base);
Rusty Russell82fab442012-12-11 09:38:33 +10303099 return -ENOMEM;
3100 }
Rusty Russell7523e4d2015-11-26 09:44:08 +10303101 memset(ptr, 0, mod->init_layout.size);
3102 mod->init_layout.base = ptr;
Rusty Russell82fab442012-12-11 09:38:33 +10303103 } else
Rusty Russell7523e4d2015-11-26 09:44:08 +10303104 mod->init_layout.base = NULL;
Linus Torvalds65b8a9b2010-08-05 12:59:02 -06003105
3106 /* Transfer each section which specifies SHF_ALLOC */
Jim Cromie5e124162011-12-06 12:11:31 -07003107 pr_debug("final section addresses:\n");
Rusty Russell49668682010-08-05 12:59:10 -06003108 for (i = 0; i < info->hdr->e_shnum; i++) {
Linus Torvalds65b8a9b2010-08-05 12:59:02 -06003109 void *dest;
Rusty Russell49668682010-08-05 12:59:10 -06003110 Elf_Shdr *shdr = &info->sechdrs[i];
Linus Torvalds65b8a9b2010-08-05 12:59:02 -06003111
Rusty Russell49668682010-08-05 12:59:10 -06003112 if (!(shdr->sh_flags & SHF_ALLOC))
Linus Torvalds65b8a9b2010-08-05 12:59:02 -06003113 continue;
3114
Rusty Russell49668682010-08-05 12:59:10 -06003115 if (shdr->sh_entsize & INIT_OFFSET_MASK)
Rusty Russell7523e4d2015-11-26 09:44:08 +10303116 dest = mod->init_layout.base
Rusty Russell49668682010-08-05 12:59:10 -06003117 + (shdr->sh_entsize & ~INIT_OFFSET_MASK);
Linus Torvalds65b8a9b2010-08-05 12:59:02 -06003118 else
Rusty Russell7523e4d2015-11-26 09:44:08 +10303119 dest = mod->core_layout.base + shdr->sh_entsize;
Linus Torvalds65b8a9b2010-08-05 12:59:02 -06003120
Rusty Russell49668682010-08-05 12:59:10 -06003121 if (shdr->sh_type != SHT_NOBITS)
3122 memcpy(dest, (void *)shdr->sh_addr, shdr->sh_size);
Linus Torvalds65b8a9b2010-08-05 12:59:02 -06003123 /* Update sh_addr to point to copy in image. */
Rusty Russell49668682010-08-05 12:59:10 -06003124 shdr->sh_addr = (unsigned long)dest;
Jim Cromie5e124162011-12-06 12:11:31 -07003125 pr_debug("\t0x%lx %s\n",
3126 (long)shdr->sh_addr, info->secstrings + shdr->sh_name);
Linus Torvalds65b8a9b2010-08-05 12:59:02 -06003127 }
Rusty Russelld9131882010-08-05 12:59:08 -06003128
3129 return 0;
Linus Torvalds65b8a9b2010-08-05 12:59:02 -06003130}
3131
Rusty Russell49668682010-08-05 12:59:10 -06003132static int check_module_license_and_versions(struct module *mod)
Rusty Russell22e268e2010-08-05 12:59:05 -06003133{
Libor Pechacek3205c362016-04-13 11:06:12 +09303134 int prev_taint = test_taint(TAINT_PROPRIETARY_MODULE);
3135
Rusty Russell22e268e2010-08-05 12:59:05 -06003136 /*
3137 * ndiswrapper is under GPL by itself, but loads proprietary modules.
3138 * Don't use add_taint_module(), as it would prevent ndiswrapper from
3139 * using GPL-only symbols it needs.
3140 */
3141 if (strcmp(mod->name, "ndiswrapper") == 0)
Rusty Russell373d4d02013-01-21 17:17:39 +10303142 add_taint(TAINT_PROPRIETARY_MODULE, LOCKDEP_NOW_UNRELIABLE);
Rusty Russell22e268e2010-08-05 12:59:05 -06003143
3144 /* driverloader was caught wrongly pretending to be under GPL */
3145 if (strcmp(mod->name, "driverloader") == 0)
Rusty Russell373d4d02013-01-21 17:17:39 +10303146 add_taint_module(mod, TAINT_PROPRIETARY_MODULE,
3147 LOCKDEP_NOW_UNRELIABLE);
Rusty Russell22e268e2010-08-05 12:59:05 -06003148
Matthew Garrettc99af372012-06-22 13:49:31 -04003149 /* lve claims to be GPL but upstream won't provide source */
3150 if (strcmp(mod->name, "lve") == 0)
Rusty Russell373d4d02013-01-21 17:17:39 +10303151 add_taint_module(mod, TAINT_PROPRIETARY_MODULE,
3152 LOCKDEP_NOW_UNRELIABLE);
Matthew Garrettc99af372012-06-22 13:49:31 -04003153
Libor Pechacek3205c362016-04-13 11:06:12 +09303154 if (!prev_taint && test_taint(TAINT_PROPRIETARY_MODULE))
3155 pr_warn("%s: module license taints kernel.\n", mod->name);
3156
Rusty Russell22e268e2010-08-05 12:59:05 -06003157#ifdef CONFIG_MODVERSIONS
3158 if ((mod->num_syms && !mod->crcs)
3159 || (mod->num_gpl_syms && !mod->gpl_crcs)
3160 || (mod->num_gpl_future_syms && !mod->gpl_future_crcs)
3161#ifdef CONFIG_UNUSED_SYMBOLS
3162 || (mod->num_unused_syms && !mod->unused_crcs)
3163 || (mod->num_unused_gpl_syms && !mod->unused_gpl_crcs)
3164#endif
3165 ) {
3166 return try_to_force_load(mod,
3167 "no versions for exported symbols");
3168 }
3169#endif
3170 return 0;
3171}
3172
3173static void flush_module_icache(const struct module *mod)
3174{
3175 mm_segment_t old_fs;
3176
3177 /* flush the icache in correct context */
3178 old_fs = get_fs();
3179 set_fs(KERNEL_DS);
3180
3181 /*
3182 * Flush the instruction cache, since we've played with text.
3183 * Do it before processing of module parameters, so the module
3184 * can provide parameter accessor functions of its own.
3185 */
Rusty Russell7523e4d2015-11-26 09:44:08 +10303186 if (mod->init_layout.base)
3187 flush_icache_range((unsigned long)mod->init_layout.base,
3188 (unsigned long)mod->init_layout.base
3189 + mod->init_layout.size);
3190 flush_icache_range((unsigned long)mod->core_layout.base,
3191 (unsigned long)mod->core_layout.base + mod->core_layout.size);
Rusty Russell22e268e2010-08-05 12:59:05 -06003192
3193 set_fs(old_fs);
3194}
3195
Jonas Bonn74e08fc2011-06-30 21:22:11 +02003196int __weak module_frob_arch_sections(Elf_Ehdr *hdr,
3197 Elf_Shdr *sechdrs,
3198 char *secstrings,
3199 struct module *mod)
3200{
3201 return 0;
3202}
3203
Prarit Bhargavabe7de5f2016-07-21 15:37:56 +09303204/* module_blacklist is a comma-separated list of module names */
3205static char *module_blacklist;
3206static bool blacklisted(char *module_name)
3207{
3208 const char *p;
3209 size_t len;
3210
3211 if (!module_blacklist)
3212 return false;
3213
3214 for (p = module_blacklist; *p; p += len) {
3215 len = strcspn(p, ",");
3216 if (strlen(module_name) == len && !memcmp(module_name, p, len))
3217 return true;
3218 if (p[len] == ',')
3219 len++;
3220 }
3221 return false;
3222}
3223core_param(module_blacklist, module_blacklist, charp, 0400);
3224
Rusty Russell2f3238a2012-10-22 18:09:41 +10303225static struct module *layout_and_allocate(struct load_info *info, int flags)
Rusty Russelld9131882010-08-05 12:59:08 -06003226{
3227 /* Module within temporary copy. */
3228 struct module *mod;
Jessica Yu444d13f2016-07-27 12:06:21 +09303229 unsigned int ndx;
Rusty Russelld9131882010-08-05 12:59:08 -06003230 int err;
3231
Rusty Russell2f3238a2012-10-22 18:09:41 +10303232 mod = setup_load_info(info, flags);
Rusty Russelld9131882010-08-05 12:59:08 -06003233 if (IS_ERR(mod))
3234 return mod;
3235
Prarit Bhargavabe7de5f2016-07-21 15:37:56 +09303236 if (blacklisted(mod->name))
3237 return ERR_PTR(-EPERM);
3238
Rusty Russell2f3238a2012-10-22 18:09:41 +10303239 err = check_modinfo(mod, info, flags);
Rusty Russelld9131882010-08-05 12:59:08 -06003240 if (err)
3241 return ERR_PTR(err);
3242
3243 /* Allow arches to frob section contents and sizes. */
Rusty Russell49668682010-08-05 12:59:10 -06003244 err = module_frob_arch_sections(info->hdr, info->sechdrs,
3245 info->secstrings, mod);
Rusty Russelld9131882010-08-05 12:59:08 -06003246 if (err < 0)
Rusty Russell8d8022e2013-07-03 10:06:28 +09303247 return ERR_PTR(err);
Rusty Russelld9131882010-08-05 12:59:08 -06003248
Rusty Russell8d8022e2013-07-03 10:06:28 +09303249 /* We will do a special allocation for per-cpu sections later. */
3250 info->sechdrs[info->index.pcpu].sh_flags &= ~(unsigned long)SHF_ALLOC;
Rusty Russelld9131882010-08-05 12:59:08 -06003251
Jessica Yu444d13f2016-07-27 12:06:21 +09303252 /*
3253 * Mark ro_after_init section with SHF_RO_AFTER_INIT so that
3254 * layout_sections() can put it in the right place.
3255 * Note: ro_after_init sections also have SHF_{WRITE,ALLOC} set.
3256 */
3257 ndx = find_sec(info, ".data..ro_after_init");
3258 if (ndx)
3259 info->sechdrs[ndx].sh_flags |= SHF_RO_AFTER_INIT;
3260
Rusty Russelld9131882010-08-05 12:59:08 -06003261 /* Determine total sizes, and put offsets in sh_entsize. For now
3262 this is done generically; there doesn't appear to be any
3263 special cases for the architectures. */
Rusty Russell49668682010-08-05 12:59:10 -06003264 layout_sections(mod, info);
Rusty Russell49668682010-08-05 12:59:10 -06003265 layout_symtab(mod, info);
Rusty Russelld9131882010-08-05 12:59:08 -06003266
3267 /* Allocate and move to the final place */
Rusty Russell49668682010-08-05 12:59:10 -06003268 err = move_module(mod, info);
Rusty Russelld9131882010-08-05 12:59:08 -06003269 if (err)
Rusty Russell8d8022e2013-07-03 10:06:28 +09303270 return ERR_PTR(err);
Rusty Russelld9131882010-08-05 12:59:08 -06003271
3272 /* Module has been copied to its final place now: return it. */
3273 mod = (void *)info->sechdrs[info->index.mod].sh_addr;
Rusty Russell49668682010-08-05 12:59:10 -06003274 kmemleak_load_module(mod, info);
Rusty Russelld9131882010-08-05 12:59:08 -06003275 return mod;
Rusty Russelld9131882010-08-05 12:59:08 -06003276}
3277
3278/* mod is no longer valid after this! */
3279static void module_deallocate(struct module *mod, struct load_info *info)
3280{
Rusty Russelld9131882010-08-05 12:59:08 -06003281 percpu_modfree(mod);
Rusty Russelld453cde2015-01-20 09:07:04 +10303282 module_arch_freeing_init(mod);
Rusty Russell7523e4d2015-11-26 09:44:08 +10303283 module_memfree(mod->init_layout.base);
3284 module_memfree(mod->core_layout.base);
Rusty Russelld9131882010-08-05 12:59:08 -06003285}
3286
Jonas Bonn74e08fc2011-06-30 21:22:11 +02003287int __weak module_finalize(const Elf_Ehdr *hdr,
3288 const Elf_Shdr *sechdrs,
3289 struct module *me)
3290{
3291 return 0;
3292}
3293
Rusty Russell811d66a2010-08-05 12:59:12 -06003294static int post_relocation(struct module *mod, const struct load_info *info)
3295{
Rusty Russell51f3d0f2010-08-05 12:59:13 -06003296 /* Sort exception table now relocations are done. */
Rusty Russell811d66a2010-08-05 12:59:12 -06003297 sort_extable(mod->extable, mod->extable + mod->num_exentries);
3298
3299 /* Copy relocated percpu area over. */
3300 percpu_modcopy(mod, (void *)info->sechdrs[info->index.pcpu].sh_addr,
3301 info->sechdrs[info->index.pcpu].sh_size);
3302
Rusty Russell51f3d0f2010-08-05 12:59:13 -06003303 /* Setup kallsyms-specific fields. */
Rusty Russell811d66a2010-08-05 12:59:12 -06003304 add_kallsyms(mod, info);
3305
3306 /* Arch-specific module finalizing. */
3307 return module_finalize(info->hdr, info->sechdrs, mod);
3308}
3309
Rusty Russell9bb9c3b2012-09-28 14:31:03 +09303310/* Is this module of this name done loading? No locks held. */
3311static bool finished_loading(const char *name)
3312{
3313 struct module *mod;
3314 bool ret;
3315
Peter Zijlstra9cc019b2015-02-11 15:01:13 +10303316 /*
3317 * The module_mutex should not be a heavily contended lock;
3318 * if we get the occasional sleep here, we'll go an extra iteration
3319 * in the wait_event_interruptible(), which is harmless.
3320 */
3321 sched_annotate_sleep();
Rusty Russell9bb9c3b2012-09-28 14:31:03 +09303322 mutex_lock(&module_mutex);
Mathias Krause4f6de4d52013-07-02 15:35:11 +09303323 mod = find_module_all(name, strlen(name), true);
Rusty Russell0d21b0e2013-01-12 11:38:44 +10303324 ret = !mod || mod->state == MODULE_STATE_LIVE
3325 || mod->state == MODULE_STATE_GOING;
Rusty Russell9bb9c3b2012-09-28 14:31:03 +09303326 mutex_unlock(&module_mutex);
3327
3328 return ret;
3329}
3330
Peter Oberparleiterb99b87f2009-06-17 16:28:03 -07003331/* Call module constructors. */
3332static void do_mod_ctors(struct module *mod)
3333{
3334#ifdef CONFIG_CONSTRUCTORS
3335 unsigned long i;
3336
3337 for (i = 0; i < mod->num_ctors; i++)
3338 mod->ctors[i]();
3339#endif
3340}
3341
Rusty Russellc7496372015-01-20 09:07:05 +10303342/* For freeing module_init on success, in case kallsyms traversing */
3343struct mod_initfree {
3344 struct rcu_head rcu;
3345 void *module_init;
3346};
3347
3348static void do_free_init(struct rcu_head *head)
3349{
3350 struct mod_initfree *m = container_of(head, struct mod_initfree, rcu);
3351 module_memfree(m->module_init);
3352 kfree(m);
3353}
3354
Jan Kiszkabe02a182015-02-17 13:46:50 -08003355/*
3356 * This is where the real work happens.
3357 *
3358 * Keep it uninlined to provide a reliable breakpoint target, e.g. for the gdb
3359 * helper command 'lx-symbols'.
3360 */
3361static noinline int do_init_module(struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003362{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003363 int ret = 0;
Rusty Russellc7496372015-01-20 09:07:05 +10303364 struct mod_initfree *freeinit;
3365
3366 freeinit = kmalloc(sizeof(*freeinit), GFP_KERNEL);
3367 if (!freeinit) {
3368 ret = -ENOMEM;
3369 goto fail;
3370 }
Rusty Russell7523e4d2015-11-26 09:44:08 +10303371 freeinit->module_init = mod->init_layout.base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003372
Tejun Heo774a1222013-01-15 18:52:51 -08003373 /*
3374 * We want to find out whether @mod uses async during init. Clear
3375 * PF_USED_ASYNC. async_schedule*() will set it.
3376 */
3377 current->flags &= ~PF_USED_ASYNC;
3378
Peter Oberparleiterb99b87f2009-06-17 16:28:03 -07003379 do_mod_ctors(mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003380 /* Start the module */
3381 if (mod->init != NULL)
Arjan van de Ven59f94152008-07-30 12:49:02 -07003382 ret = do_one_initcall(mod->init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003383 if (ret < 0) {
Rusty Russellc7496372015-01-20 09:07:05 +10303384 goto fail_free_freeinit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003385 }
Alexey Dobriyane24e2e62008-03-10 11:43:53 -07003386 if (ret > 0) {
Andrew Mortonbddb12b2013-11-12 15:11:28 -08003387 pr_warn("%s: '%s'->init suspiciously returned %d, it should "
3388 "follow 0/-E convention\n"
3389 "%s: loading module anyway...\n",
3390 __func__, mod->name, ret, __func__);
Alexey Dobriyane24e2e62008-03-10 11:43:53 -07003391 dump_stack();
3392 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003393
Rusty Russell6f139092012-09-28 14:31:03 +09303394 /* Now it's a first class citizen! */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003395 mod->state = MODULE_STATE_LIVE;
Masami Hiramatsu0deddf42009-01-06 14:41:54 -08003396 blocking_notifier_call_chain(&module_notify_list,
3397 MODULE_STATE_LIVE, mod);
Rusty Russell6c5db222008-03-10 11:43:52 -07003398
Tejun Heo774a1222013-01-15 18:52:51 -08003399 /*
3400 * We need to finish all async code before the module init sequence
3401 * is done. This has potential to deadlock. For example, a newly
3402 * detected block device can trigger request_module() of the
3403 * default iosched from async probing task. Once userland helper
3404 * reaches here, async_synchronize_full() will wait on the async
3405 * task waiting on request_module() and deadlock.
3406 *
3407 * This deadlock is avoided by perfomring async_synchronize_full()
3408 * iff module init queued any async jobs. This isn't a full
3409 * solution as it will deadlock the same if module loading from
3410 * async jobs nests more than once; however, due to the various
3411 * constraints, this hack seems to be the best option for now.
3412 * Please refer to the following thread for details.
3413 *
3414 * http://thread.gmane.org/gmane.linux.kernel/1420814
3415 */
Luis R. Rodriguezf2411da2015-03-30 16:20:05 -07003416 if (!mod->async_probe_requested && (current->flags & PF_USED_ASYNC))
Tejun Heo774a1222013-01-15 18:52:51 -08003417 async_synchronize_full();
Linus Torvaldsd6de2c82009-04-10 12:17:41 -07003418
Rusty Russell6c5db222008-03-10 11:43:52 -07003419 mutex_lock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003420 /* Drop initial reference. */
3421 module_put(mod);
Rusty Russellad6561d2009-06-12 21:47:03 -06003422 trim_init_extable(mod);
Jan Beulich4a496222009-07-06 14:50:42 +01003423#ifdef CONFIG_KALLSYMS
Rusty Russell82440622016-02-03 16:55:26 +10303424 /* Switch to core kallsyms now init is done: kallsyms may be walking! */
3425 rcu_assign_pointer(mod->kallsyms, &mod->core_kallsyms);
Jan Beulich4a496222009-07-06 14:50:42 +01003426#endif
Jessica Yu444d13f2016-07-27 12:06:21 +09303427 module_enable_ro(mod, true);
Peter Zijlstra93c2e102015-05-27 11:09:37 +09303428 mod_tree_remove_init(mod);
Rusty Russell85c898d2015-11-26 09:45:08 +10303429 disable_ro_nx(&mod->init_layout);
Rusty Russelld453cde2015-01-20 09:07:04 +10303430 module_arch_freeing_init(mod);
Rusty Russell7523e4d2015-11-26 09:44:08 +10303431 mod->init_layout.base = NULL;
3432 mod->init_layout.size = 0;
3433 mod->init_layout.ro_size = 0;
Jessica Yu444d13f2016-07-27 12:06:21 +09303434 mod->init_layout.ro_after_init_size = 0;
Rusty Russell7523e4d2015-11-26 09:44:08 +10303435 mod->init_layout.text_size = 0;
Rusty Russellc7496372015-01-20 09:07:05 +10303436 /*
3437 * We want to free module_init, but be aware that kallsyms may be
Peter Zijlstra0be964b2015-05-27 11:09:35 +09303438 * walking this with preempt disabled. In all the failure paths, we
3439 * call synchronize_sched(), but we don't want to slow down the success
3440 * path, so use actual RCU here.
Rusty Russellc7496372015-01-20 09:07:05 +10303441 */
Peter Zijlstra0be964b2015-05-27 11:09:35 +09303442 call_rcu_sched(&freeinit->rcu, do_free_init);
Ashutosh Naik6389a382006-03-23 03:00:46 -08003443 mutex_unlock(&module_mutex);
Rusty Russell6f139092012-09-28 14:31:03 +09303444 wake_up_all(&module_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003445
3446 return 0;
Rusty Russellc7496372015-01-20 09:07:05 +10303447
3448fail_free_freeinit:
3449 kfree(freeinit);
3450fail:
3451 /* Try to protect us from buggy refcounters. */
3452 mod->state = MODULE_STATE_GOING;
3453 synchronize_sched();
3454 module_put(mod);
3455 blocking_notifier_call_chain(&module_notify_list,
3456 MODULE_STATE_GOING, mod);
Jessica Yu7e545d62016-03-16 20:55:39 -04003457 klp_module_going(mod);
Jessica Yu7dcd1822016-02-16 17:32:33 -05003458 ftrace_release_mod(mod);
Rusty Russellc7496372015-01-20 09:07:05 +10303459 free_module(mod);
3460 wake_up_all(&module_wq);
3461 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003462}
3463
Kees Cook34e11692012-10-16 07:31:07 +10303464static int may_init_module(void)
3465{
3466 if (!capable(CAP_SYS_MODULE) || modules_disabled)
3467 return -EPERM;
3468
3469 return 0;
3470}
3471
Rusty Russella3535c72013-01-21 17:18:59 +10303472/*
3473 * We try to place it in the list now to make sure it's unique before
3474 * we dedicate too many resources. In particular, temporary percpu
3475 * memory exhaustion.
3476 */
3477static int add_unformed_module(struct module *mod)
3478{
3479 int err;
3480 struct module *old;
3481
3482 mod->state = MODULE_STATE_UNFORMED;
3483
3484again:
3485 mutex_lock(&module_mutex);
Mathias Krause4f6de4d52013-07-02 15:35:11 +09303486 old = find_module_all(mod->name, strlen(mod->name), true);
3487 if (old != NULL) {
Rusty Russella3535c72013-01-21 17:18:59 +10303488 if (old->state == MODULE_STATE_COMING
3489 || old->state == MODULE_STATE_UNFORMED) {
3490 /* Wait in case it fails to load. */
3491 mutex_unlock(&module_mutex);
Peter Zijlstra9cc019b2015-02-11 15:01:13 +10303492 err = wait_event_interruptible(module_wq,
3493 finished_loading(mod->name));
Rusty Russella3535c72013-01-21 17:18:59 +10303494 if (err)
3495 goto out_unlocked;
3496 goto again;
3497 }
3498 err = -EEXIST;
3499 goto out;
3500 }
Peter Zijlstra4f6665462015-05-27 11:09:38 +09303501 mod_update_bounds(mod);
Rusty Russella3535c72013-01-21 17:18:59 +10303502 list_add_rcu(&mod->list, &modules);
Peter Zijlstra93c2e102015-05-27 11:09:37 +09303503 mod_tree_insert(mod);
Rusty Russella3535c72013-01-21 17:18:59 +10303504 err = 0;
3505
3506out:
3507 mutex_unlock(&module_mutex);
3508out_unlocked:
3509 return err;
3510}
3511
3512static int complete_formation(struct module *mod, struct load_info *info)
3513{
3514 int err;
3515
3516 mutex_lock(&module_mutex);
3517
3518 /* Find duplicate symbols (must be called under lock). */
3519 err = verify_export_symbols(mod);
3520 if (err < 0)
3521 goto out;
3522
3523 /* This relies on module_mutex for list integrity. */
3524 module_bug_finalize(info->hdr, info->sechdrs, mod);
3525
Jessica Yu444d13f2016-07-27 12:06:21 +09303526 module_enable_ro(mod, false);
Rusty Russell85c898d2015-11-26 09:45:08 +10303527 module_enable_nx(mod);
Rusty Russell49822232014-05-14 10:54:19 +09303528
Rusty Russella3535c72013-01-21 17:18:59 +10303529 /* Mark state as coming so strong_try_module_get() ignores us,
3530 * but kallsyms etc. can see us. */
3531 mod->state = MODULE_STATE_COMING;
Rusty Russell49822232014-05-14 10:54:19 +09303532 mutex_unlock(&module_mutex);
3533
Rusty Russell49822232014-05-14 10:54:19 +09303534 return 0;
Rusty Russella3535c72013-01-21 17:18:59 +10303535
3536out:
3537 mutex_unlock(&module_mutex);
3538 return err;
3539}
3540
Jessica Yu4c973d12016-03-16 20:55:38 -04003541static int prepare_coming_module(struct module *mod)
3542{
Jessica Yu7e545d62016-03-16 20:55:39 -04003543 int err;
3544
Jessica Yu4c973d12016-03-16 20:55:38 -04003545 ftrace_module_enable(mod);
Jessica Yu7e545d62016-03-16 20:55:39 -04003546 err = klp_module_coming(mod);
3547 if (err)
3548 return err;
3549
Jessica Yu4c973d12016-03-16 20:55:38 -04003550 blocking_notifier_call_chain(&module_notify_list,
3551 MODULE_STATE_COMING, mod);
3552 return 0;
3553}
3554
Luis R. Rodriguezecc86172015-03-30 16:20:03 -07003555static int unknown_module_param_cb(char *param, char *val, const char *modname,
3556 void *arg)
Rusty Russell54041d82013-07-02 15:35:12 +09303557{
Luis R. Rodriguezf2411da2015-03-30 16:20:05 -07003558 struct module *mod = arg;
3559 int ret;
3560
3561 if (strcmp(param, "async_probe") == 0) {
3562 mod->async_probe_requested = true;
3563 return 0;
3564 }
3565
Ionut Alexa6da0b562014-11-10 09:31:29 +10303566 /* Check for magic 'dyndbg' arg */
Luis R. Rodriguezf2411da2015-03-30 16:20:05 -07003567 ret = ddebug_dyndbg_module_param_cb(param, val, modname);
Andrew Mortonbddb12b2013-11-12 15:11:28 -08003568 if (ret != 0)
3569 pr_warn("%s: unknown parameter '%s' ignored\n", modname, param);
Rusty Russell54041d82013-07-02 15:35:12 +09303570 return 0;
3571}
3572
Kees Cook34e11692012-10-16 07:31:07 +10303573/* Allocate and load the module: note that size of section 0 is always
3574 zero, and we rely on this for optional sections. */
Rusty Russell2f3238a2012-10-22 18:09:41 +10303575static int load_module(struct load_info *info, const char __user *uargs,
3576 int flags)
Kees Cook34e11692012-10-16 07:31:07 +10303577{
Rusty Russella3535c72013-01-21 17:18:59 +10303578 struct module *mod;
Kees Cook34e11692012-10-16 07:31:07 +10303579 long err;
Rusty Russell51e158c2014-04-28 11:34:33 +09303580 char *after_dashes;
Kees Cook34e11692012-10-16 07:31:07 +10303581
Ben Hutchingsbca014c2016-04-28 09:24:01 +09303582 err = module_sig_check(info, flags);
Kees Cook34e11692012-10-16 07:31:07 +10303583 if (err)
3584 goto free_copy;
3585
3586 err = elf_header_check(info);
3587 if (err)
3588 goto free_copy;
3589
3590 /* Figure out module layout, and allocate all the memory. */
Rusty Russell2f3238a2012-10-22 18:09:41 +10303591 mod = layout_and_allocate(info, flags);
Kees Cook34e11692012-10-16 07:31:07 +10303592 if (IS_ERR(mod)) {
3593 err = PTR_ERR(mod);
3594 goto free_copy;
3595 }
3596
Richard Guy Briggsca86cad2017-02-04 13:10:38 -05003597 audit_log_kern_module(mod->name);
3598
Rusty Russella3535c72013-01-21 17:18:59 +10303599 /* Reserve our place in the list. */
3600 err = add_unformed_module(mod);
3601 if (err)
Rusty Russell1fb93412013-01-12 13:27:34 +10303602 goto free_module;
Rusty Russell1fb93412013-01-12 13:27:34 +10303603
Kees Cook34e11692012-10-16 07:31:07 +10303604#ifdef CONFIG_MODULE_SIG
3605 mod->sig_ok = info->sig_ok;
Rusty Russell64748a22013-01-21 17:03:02 +10303606 if (!mod->sig_ok) {
Andrew Mortonbddb12b2013-11-12 15:11:28 -08003607 pr_notice_once("%s: module verification failed: signature "
Marcel Holtmannab92ebb2015-02-06 15:09:57 +10303608 "and/or required key missing - tainting "
Andrew Mortonbddb12b2013-11-12 15:11:28 -08003609 "kernel\n", mod->name);
Mathieu Desnoyers66cc69e2014-03-13 12:11:30 +10303610 add_taint_module(mod, TAINT_UNSIGNED_MODULE, LOCKDEP_STILL_OK);
Rusty Russell64748a22013-01-21 17:03:02 +10303611 }
Kees Cook34e11692012-10-16 07:31:07 +10303612#endif
3613
Rusty Russell8d8022e2013-07-03 10:06:28 +09303614 /* To avoid stressing percpu allocator, do this once we're unique. */
Rusty Russell9eb76d72013-07-03 10:06:29 +09303615 err = percpu_modalloc(mod, info);
Rusty Russell8d8022e2013-07-03 10:06:28 +09303616 if (err)
3617 goto unlink_mod;
3618
Kees Cook34e11692012-10-16 07:31:07 +10303619 /* Now module is in final location, initialize linked lists, etc. */
3620 err = module_unload_init(mod);
3621 if (err)
Rusty Russell1fb93412013-01-12 13:27:34 +10303622 goto unlink_mod;
Kees Cook34e11692012-10-16 07:31:07 +10303623
Rusty Russellcf2fde72015-06-26 06:44:38 +09303624 init_param_lock(mod);
Dan Streetmanb51d23e2015-06-17 06:18:52 +09303625
Kees Cook34e11692012-10-16 07:31:07 +10303626 /* Now we've got everything in the final locations, we can
3627 * find optional sections. */
Frantisek Hrbataeb3057d2013-10-14 18:08:46 +10303628 err = find_module_sections(mod, info);
3629 if (err)
3630 goto free_unload;
Kees Cook34e11692012-10-16 07:31:07 +10303631
3632 err = check_module_license_and_versions(mod);
3633 if (err)
3634 goto free_unload;
3635
3636 /* Set up MODINFO_ATTR fields */
3637 setup_modinfo(mod, info);
3638
3639 /* Fix up syms, so that st_value is a pointer to location. */
3640 err = simplify_symbols(mod, info);
3641 if (err < 0)
3642 goto free_modinfo;
3643
3644 err = apply_relocations(mod, info);
3645 if (err < 0)
3646 goto free_modinfo;
3647
3648 err = post_relocation(mod, info);
3649 if (err < 0)
3650 goto free_modinfo;
3651
3652 flush_module_icache(mod);
3653
3654 /* Now copy in args */
3655 mod->args = strndup_user(uargs, ~0UL >> 1);
3656 if (IS_ERR(mod->args)) {
3657 err = PTR_ERR(mod->args);
3658 goto free_arch_cleanup;
3659 }
3660
Kees Cook34e11692012-10-16 07:31:07 +10303661 dynamic_debug_setup(info->debug, info->num_debug);
3662
Steven Rostedt (Red Hat)a949ae52014-04-24 10:40:12 -04003663 /* Ftrace init must be called in the MODULE_STATE_UNFORMED state */
3664 ftrace_module_init(mod);
3665
Rusty Russella3535c72013-01-21 17:18:59 +10303666 /* Finally it's fully formed, ready to start executing. */
3667 err = complete_formation(mod, info);
3668 if (err)
Rusty Russell1fb93412013-01-12 13:27:34 +10303669 goto ddebug_cleanup;
Kees Cook34e11692012-10-16 07:31:07 +10303670
Jessica Yu4c973d12016-03-16 20:55:38 -04003671 err = prepare_coming_module(mod);
3672 if (err)
3673 goto bug_cleanup;
3674
Kees Cook34e11692012-10-16 07:31:07 +10303675 /* Module is ready to execute: parsing args may do that. */
Rusty Russell51e158c2014-04-28 11:34:33 +09303676 after_dashes = parse_args(mod->name, mod->args, mod->kp, mod->num_kp,
Luis R. Rodriguez4355efb2016-02-03 16:55:26 +10303677 -32768, 32767, mod,
Luis R. Rodriguezecc86172015-03-30 16:20:03 -07003678 unknown_module_param_cb);
Rusty Russell51e158c2014-04-28 11:34:33 +09303679 if (IS_ERR(after_dashes)) {
3680 err = PTR_ERR(after_dashes);
Jessica Yu4c973d12016-03-16 20:55:38 -04003681 goto coming_cleanup;
Rusty Russell51e158c2014-04-28 11:34:33 +09303682 } else if (after_dashes) {
3683 pr_warn("%s: parameters '%s' after `--' ignored\n",
3684 mod->name, after_dashes);
3685 }
Kees Cook34e11692012-10-16 07:31:07 +10303686
Richard Guy Briggsca86cad2017-02-04 13:10:38 -05003687 /* Link in to sysfs. */
Kees Cook34e11692012-10-16 07:31:07 +10303688 err = mod_sysfs_setup(mod, info, mod->kp, mod->num_kp);
3689 if (err < 0)
Jessica Yu4c973d12016-03-16 20:55:38 -04003690 goto coming_cleanup;
Kees Cook34e11692012-10-16 07:31:07 +10303691
Jessica Yu1ce15ef2016-03-22 20:03:16 -04003692 if (is_livepatch_module(mod)) {
3693 err = copy_module_elf(mod, info);
3694 if (err < 0)
3695 goto sysfs_cleanup;
3696 }
3697
Kees Cook34e11692012-10-16 07:31:07 +10303698 /* Get rid of temporary copy. */
3699 free_copy(info);
3700
3701 /* Done! */
3702 trace_module_load(mod);
3703
3704 return do_init_module(mod);
3705
Jessica Yu1ce15ef2016-03-22 20:03:16 -04003706 sysfs_cleanup:
3707 mod_sysfs_teardown(mod);
Jessica Yu4c973d12016-03-16 20:55:38 -04003708 coming_cleanup:
3709 blocking_notifier_call_chain(&module_notify_list,
3710 MODULE_STATE_GOING, mod);
Jessica Yu7e545d62016-03-16 20:55:39 -04003711 klp_module_going(mod);
Rusty Russell1fb93412013-01-12 13:27:34 +10303712 bug_cleanup:
3713 /* module_bug_cleanup needs module_mutex protection */
Kees Cook34e11692012-10-16 07:31:07 +10303714 mutex_lock(&module_mutex);
Kees Cook34e11692012-10-16 07:31:07 +10303715 module_bug_cleanup(mod);
Linus Torvaldsee61abb2013-01-20 20:22:58 -08003716 mutex_unlock(&module_mutex);
Andy Lutomirskiff7e0052014-08-16 04:13:37 +09303717
3718 /* we can't deallocate the module until we clear memory protection */
Rusty Russell85c898d2015-11-26 09:45:08 +10303719 module_disable_ro(mod);
3720 module_disable_nx(mod);
Andy Lutomirskiff7e0052014-08-16 04:13:37 +09303721
Rusty Russella3535c72013-01-21 17:18:59 +10303722 ddebug_cleanup:
Rusty Russell1fb93412013-01-12 13:27:34 +10303723 dynamic_debug_remove(info->debug);
Kees Cook34e11692012-10-16 07:31:07 +10303724 synchronize_sched();
3725 kfree(mod->args);
3726 free_arch_cleanup:
3727 module_arch_cleanup(mod);
3728 free_modinfo:
3729 free_modinfo(mod);
3730 free_unload:
3731 module_unload_free(mod);
Rusty Russell1fb93412013-01-12 13:27:34 +10303732 unlink_mod:
3733 mutex_lock(&module_mutex);
3734 /* Unlink carefully: kallsyms could be walking list. */
3735 list_del_rcu(&mod->list);
Peter Zijlstra758556b2015-07-09 06:48:06 +09303736 mod_tree_remove(mod);
Rusty Russell1fb93412013-01-12 13:27:34 +10303737 wake_up_all(&module_wq);
Peter Zijlstra0be964b2015-05-27 11:09:35 +09303738 /* Wait for RCU-sched synchronizing before releasing mod->list. */
3739 synchronize_sched();
Rusty Russell1fb93412013-01-12 13:27:34 +10303740 mutex_unlock(&module_mutex);
Kees Cook34e11692012-10-16 07:31:07 +10303741 free_module:
Steven Rostedt (Red Hat)049fb9b2016-01-05 20:32:47 -05003742 /*
3743 * Ftrace needs to clean up what it initialized.
3744 * This does nothing if ftrace_module_init() wasn't called,
3745 * but it must be called outside of module_mutex.
3746 */
3747 ftrace_release_mod(mod);
Peter Zijlstra35a93932015-02-26 16:23:11 +01003748 /* Free lock-classes; relies on the preceding sync_rcu() */
Rusty Russell7523e4d2015-11-26 09:44:08 +10303749 lockdep_free_key_range(mod->core_layout.base, mod->core_layout.size);
Peter Zijlstra35a93932015-02-26 16:23:11 +01003750
Kees Cook34e11692012-10-16 07:31:07 +10303751 module_deallocate(mod, info);
3752 free_copy:
3753 free_copy(info);
3754 return err;
3755}
3756
3757SYSCALL_DEFINE3(init_module, void __user *, umod,
3758 unsigned long, len, const char __user *, uargs)
3759{
3760 int err;
3761 struct load_info info = { };
3762
3763 err = may_init_module();
3764 if (err)
3765 return err;
3766
3767 pr_debug("init_module: umod=%p, len=%lu, uargs=%p\n",
3768 umod, len, uargs);
3769
3770 err = copy_module_from_user(umod, len, &info);
3771 if (err)
3772 return err;
3773
Rusty Russell2f3238a2012-10-22 18:09:41 +10303774 return load_module(&info, uargs, 0);
Kees Cook34e11692012-10-16 07:31:07 +10303775}
3776
Rusty Russell2f3238a2012-10-22 18:09:41 +10303777SYSCALL_DEFINE3(finit_module, int, fd, const char __user *, uargs, int, flags)
Kees Cook34e11692012-10-16 07:31:07 +10303778{
Kees Cook34e11692012-10-16 07:31:07 +10303779 struct load_info info = { };
Mimi Zohara1db7422015-12-30 07:35:30 -05003780 loff_t size;
3781 void *hdr;
3782 int err;
Kees Cook34e11692012-10-16 07:31:07 +10303783
3784 err = may_init_module();
3785 if (err)
3786 return err;
3787
Rusty Russell2f3238a2012-10-22 18:09:41 +10303788 pr_debug("finit_module: fd=%d, uargs=%p, flags=%i\n", fd, uargs, flags);
3789
3790 if (flags & ~(MODULE_INIT_IGNORE_MODVERSIONS
3791 |MODULE_INIT_IGNORE_VERMAGIC))
3792 return -EINVAL;
Kees Cook34e11692012-10-16 07:31:07 +10303793
Mimi Zohara1db7422015-12-30 07:35:30 -05003794 err = kernel_read_file_from_fd(fd, &hdr, &size, INT_MAX,
3795 READING_MODULE);
Kees Cook34e11692012-10-16 07:31:07 +10303796 if (err)
3797 return err;
Mimi Zohara1db7422015-12-30 07:35:30 -05003798 info.hdr = hdr;
3799 info.len = size;
Kees Cook34e11692012-10-16 07:31:07 +10303800
Rusty Russell2f3238a2012-10-22 18:09:41 +10303801 return load_module(&info, uargs, flags);
Kees Cook34e11692012-10-16 07:31:07 +10303802}
3803
Linus Torvalds1da177e2005-04-16 15:20:36 -07003804static inline int within(unsigned long addr, void *start, unsigned long size)
3805{
3806 return ((void *)addr >= start && (void *)addr < start + size);
3807}
3808
3809#ifdef CONFIG_KALLSYMS
3810/*
3811 * This ignores the intensely annoying "mapping symbols" found
3812 * in ARM ELF files: $a, $t and $d.
3813 */
3814static inline int is_arm_mapping_symbol(const char *str)
3815{
Russell King2e3a10a2014-07-27 07:29:01 +09303816 if (str[0] == '.' && str[1] == 'L')
3817 return true;
Kyle McMartin6c34f1f2014-09-16 22:37:18 +01003818 return str[0] == '$' && strchr("axtd", str[1])
Linus Torvalds1da177e2005-04-16 15:20:36 -07003819 && (str[2] == '\0' || str[2] == '.');
3820}
3821
Rusty Russell82440622016-02-03 16:55:26 +10303822static const char *symname(struct mod_kallsyms *kallsyms, unsigned int symnum)
Rusty Russell2e7bac52016-02-03 16:55:26 +10303823{
Rusty Russell82440622016-02-03 16:55:26 +10303824 return kallsyms->strtab + kallsyms->symtab[symnum].st_name;
Rusty Russell2e7bac52016-02-03 16:55:26 +10303825}
3826
Linus Torvalds1da177e2005-04-16 15:20:36 -07003827static const char *get_ksymbol(struct module *mod,
3828 unsigned long addr,
3829 unsigned long *size,
3830 unsigned long *offset)
3831{
3832 unsigned int i, best = 0;
3833 unsigned long nextval;
Rusty Russell82440622016-02-03 16:55:26 +10303834 struct mod_kallsyms *kallsyms = rcu_dereference_sched(mod->kallsyms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003835
3836 /* At worse, next value is at end of module */
Masami Hiramatsua06f6212009-01-06 14:41:49 -08003837 if (within_module_init(addr, mod))
Rusty Russell7523e4d2015-11-26 09:44:08 +10303838 nextval = (unsigned long)mod->init_layout.base+mod->init_layout.text_size;
Daniel Walker22a8bde2007-10-18 03:06:07 -07003839 else
Rusty Russell7523e4d2015-11-26 09:44:08 +10303840 nextval = (unsigned long)mod->core_layout.base+mod->core_layout.text_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003841
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003842 /* Scan for closest preceding symbol, and next symbol. (ELF
Daniel Walker22a8bde2007-10-18 03:06:07 -07003843 starts real symbols at 1). */
Rusty Russell82440622016-02-03 16:55:26 +10303844 for (i = 1; i < kallsyms->num_symtab; i++) {
3845 if (kallsyms->symtab[i].st_shndx == SHN_UNDEF)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003846 continue;
3847
3848 /* We ignore unnamed symbols: they're uninformative
3849 * and inserted at a whim. */
Rusty Russell82440622016-02-03 16:55:26 +10303850 if (*symname(kallsyms, i) == '\0'
3851 || is_arm_mapping_symbol(symname(kallsyms, i)))
Rusty Russell2e7bac52016-02-03 16:55:26 +10303852 continue;
3853
Rusty Russell82440622016-02-03 16:55:26 +10303854 if (kallsyms->symtab[i].st_value <= addr
3855 && kallsyms->symtab[i].st_value > kallsyms->symtab[best].st_value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003856 best = i;
Rusty Russell82440622016-02-03 16:55:26 +10303857 if (kallsyms->symtab[i].st_value > addr
3858 && kallsyms->symtab[i].st_value < nextval)
3859 nextval = kallsyms->symtab[i].st_value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003860 }
3861
3862 if (!best)
3863 return NULL;
3864
Alexey Dobriyanffb45122007-05-08 00:28:41 -07003865 if (size)
Rusty Russell82440622016-02-03 16:55:26 +10303866 *size = nextval - kallsyms->symtab[best].st_value;
Alexey Dobriyanffb45122007-05-08 00:28:41 -07003867 if (offset)
Rusty Russell82440622016-02-03 16:55:26 +10303868 *offset = addr - kallsyms->symtab[best].st_value;
3869 return symname(kallsyms, best);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003870}
3871
Rusty Russell6dd06c92008-01-29 17:13:22 -05003872/* For kallsyms to ask for address resolution. NULL means not found. Careful
3873 * not to lock to avoid deadlock on oopses, simply disable preemption. */
Andrew Morton92dfc9d2008-02-08 04:18:43 -08003874const char *module_address_lookup(unsigned long addr,
Rusty Russell6dd06c92008-01-29 17:13:22 -05003875 unsigned long *size,
3876 unsigned long *offset,
3877 char **modname,
3878 char *namebuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003879{
Rusty Russellcb2a5202008-01-14 00:55:03 -08003880 const char *ret = NULL;
Peter Zijlstrab7df4d12015-05-27 11:09:37 +09303881 struct module *mod;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003882
Rusty Russellcb2a5202008-01-14 00:55:03 -08003883 preempt_disable();
Peter Zijlstrab7df4d12015-05-27 11:09:37 +09303884 mod = __module_address(addr);
3885 if (mod) {
3886 if (modname)
3887 *modname = mod->name;
3888 ret = get_ksymbol(mod, addr, size, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003889 }
Rusty Russell6dd06c92008-01-29 17:13:22 -05003890 /* Make a copy in here where it's safe */
3891 if (ret) {
3892 strncpy(namebuf, ret, KSYM_NAME_LEN - 1);
3893 ret = namebuf;
3894 }
Rusty Russellcb2a5202008-01-14 00:55:03 -08003895 preempt_enable();
Peter Zijlstrab7df4d12015-05-27 11:09:37 +09303896
Andrew Morton92dfc9d2008-02-08 04:18:43 -08003897 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003898}
3899
Alexey Dobriyan9d65cb42007-05-08 00:28:43 -07003900int lookup_module_symbol_name(unsigned long addr, char *symname)
3901{
3902 struct module *mod;
3903
Rusty Russellcb2a5202008-01-14 00:55:03 -08003904 preempt_disable();
Andi Kleend72b3752008-08-30 10:09:00 +02003905 list_for_each_entry_rcu(mod, &modules, list) {
Rusty Russell0d21b0e2013-01-12 11:38:44 +10303906 if (mod->state == MODULE_STATE_UNFORMED)
3907 continue;
Petr Mladek9b20a352014-07-27 07:24:01 +09303908 if (within_module(addr, mod)) {
Alexey Dobriyan9d65cb42007-05-08 00:28:43 -07003909 const char *sym;
3910
3911 sym = get_ksymbol(mod, addr, NULL, NULL);
3912 if (!sym)
3913 goto out;
Tejun Heo9281ace2007-07-17 04:03:51 -07003914 strlcpy(symname, sym, KSYM_NAME_LEN);
Rusty Russellcb2a5202008-01-14 00:55:03 -08003915 preempt_enable();
Alexey Dobriyan9d65cb42007-05-08 00:28:43 -07003916 return 0;
3917 }
3918 }
3919out:
Rusty Russellcb2a5202008-01-14 00:55:03 -08003920 preempt_enable();
Alexey Dobriyan9d65cb42007-05-08 00:28:43 -07003921 return -ERANGE;
3922}
3923
Alexey Dobriyana5c43da2007-05-08 00:28:47 -07003924int lookup_module_symbol_attrs(unsigned long addr, unsigned long *size,
3925 unsigned long *offset, char *modname, char *name)
3926{
3927 struct module *mod;
3928
Rusty Russellcb2a5202008-01-14 00:55:03 -08003929 preempt_disable();
Andi Kleend72b3752008-08-30 10:09:00 +02003930 list_for_each_entry_rcu(mod, &modules, list) {
Rusty Russell0d21b0e2013-01-12 11:38:44 +10303931 if (mod->state == MODULE_STATE_UNFORMED)
3932 continue;
Petr Mladek9b20a352014-07-27 07:24:01 +09303933 if (within_module(addr, mod)) {
Alexey Dobriyana5c43da2007-05-08 00:28:47 -07003934 const char *sym;
3935
3936 sym = get_ksymbol(mod, addr, size, offset);
3937 if (!sym)
3938 goto out;
3939 if (modname)
Tejun Heo9281ace2007-07-17 04:03:51 -07003940 strlcpy(modname, mod->name, MODULE_NAME_LEN);
Alexey Dobriyana5c43da2007-05-08 00:28:47 -07003941 if (name)
Tejun Heo9281ace2007-07-17 04:03:51 -07003942 strlcpy(name, sym, KSYM_NAME_LEN);
Rusty Russellcb2a5202008-01-14 00:55:03 -08003943 preempt_enable();
Alexey Dobriyana5c43da2007-05-08 00:28:47 -07003944 return 0;
3945 }
3946 }
3947out:
Rusty Russellcb2a5202008-01-14 00:55:03 -08003948 preempt_enable();
Alexey Dobriyana5c43da2007-05-08 00:28:47 -07003949 return -ERANGE;
3950}
3951
Alexey Dobriyanea078902007-05-08 00:28:39 -07003952int module_get_kallsym(unsigned int symnum, unsigned long *value, char *type,
3953 char *name, char *module_name, int *exported)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003954{
3955 struct module *mod;
3956
Rusty Russellcb2a5202008-01-14 00:55:03 -08003957 preempt_disable();
Andi Kleend72b3752008-08-30 10:09:00 +02003958 list_for_each_entry_rcu(mod, &modules, list) {
Rusty Russell82440622016-02-03 16:55:26 +10303959 struct mod_kallsyms *kallsyms;
3960
Rusty Russell0d21b0e2013-01-12 11:38:44 +10303961 if (mod->state == MODULE_STATE_UNFORMED)
3962 continue;
Rusty Russell82440622016-02-03 16:55:26 +10303963 kallsyms = rcu_dereference_sched(mod->kallsyms);
3964 if (symnum < kallsyms->num_symtab) {
3965 *value = kallsyms->symtab[symnum].st_value;
3966 *type = kallsyms->symtab[symnum].st_info;
3967 strlcpy(name, symname(kallsyms, symnum), KSYM_NAME_LEN);
Tejun Heo9281ace2007-07-17 04:03:51 -07003968 strlcpy(module_name, mod->name, MODULE_NAME_LEN);
Tim Abbottca4787b2009-01-05 08:40:10 -06003969 *exported = is_exported(name, *value, mod);
Rusty Russellcb2a5202008-01-14 00:55:03 -08003970 preempt_enable();
Alexey Dobriyanea078902007-05-08 00:28:39 -07003971 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003972 }
Rusty Russell82440622016-02-03 16:55:26 +10303973 symnum -= kallsyms->num_symtab;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003974 }
Rusty Russellcb2a5202008-01-14 00:55:03 -08003975 preempt_enable();
Alexey Dobriyanea078902007-05-08 00:28:39 -07003976 return -ERANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003977}
3978
3979static unsigned long mod_find_symname(struct module *mod, const char *name)
3980{
3981 unsigned int i;
Rusty Russell82440622016-02-03 16:55:26 +10303982 struct mod_kallsyms *kallsyms = rcu_dereference_sched(mod->kallsyms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003983
Rusty Russell82440622016-02-03 16:55:26 +10303984 for (i = 0; i < kallsyms->num_symtab; i++)
3985 if (strcmp(name, symname(kallsyms, i)) == 0 &&
3986 kallsyms->symtab[i].st_info != 'U')
3987 return kallsyms->symtab[i].st_value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003988 return 0;
3989}
3990
3991/* Look for this name: can be of form module:name. */
3992unsigned long module_kallsyms_lookup_name(const char *name)
3993{
3994 struct module *mod;
3995 char *colon;
3996 unsigned long ret = 0;
3997
3998 /* Don't lock: we're in enough trouble already. */
Rusty Russellcb2a5202008-01-14 00:55:03 -08003999 preempt_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004000 if ((colon = strchr(name, ':')) != NULL) {
Mathias Krause4f6de4d52013-07-02 15:35:11 +09304001 if ((mod = find_module_all(name, colon - name, false)) != NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004002 ret = mod_find_symname(mod, colon+1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004003 } else {
Rusty Russell0d21b0e2013-01-12 11:38:44 +10304004 list_for_each_entry_rcu(mod, &modules, list) {
4005 if (mod->state == MODULE_STATE_UNFORMED)
4006 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004007 if ((ret = mod_find_symname(mod, name)) != 0)
4008 break;
Rusty Russell0d21b0e2013-01-12 11:38:44 +10304009 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004010 }
Rusty Russellcb2a5202008-01-14 00:55:03 -08004011 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004012 return ret;
4013}
Anders Kaseorg75a66612008-12-05 19:03:58 -05004014
4015int module_kallsyms_on_each_symbol(int (*fn)(void *, const char *,
4016 struct module *, unsigned long),
4017 void *data)
4018{
4019 struct module *mod;
4020 unsigned int i;
4021 int ret;
4022
Peter Zijlstra0be964b2015-05-27 11:09:35 +09304023 module_assert_mutex();
4024
Anders Kaseorg75a66612008-12-05 19:03:58 -05004025 list_for_each_entry(mod, &modules, list) {
Rusty Russell82440622016-02-03 16:55:26 +10304026 /* We hold module_mutex: no need for rcu_dereference_sched */
4027 struct mod_kallsyms *kallsyms = mod->kallsyms;
4028
Rusty Russell0d21b0e2013-01-12 11:38:44 +10304029 if (mod->state == MODULE_STATE_UNFORMED)
4030 continue;
Rusty Russell82440622016-02-03 16:55:26 +10304031 for (i = 0; i < kallsyms->num_symtab; i++) {
4032 ret = fn(data, symname(kallsyms, i),
4033 mod, kallsyms->symtab[i].st_value);
Anders Kaseorg75a66612008-12-05 19:03:58 -05004034 if (ret != 0)
4035 return ret;
4036 }
4037 }
4038 return 0;
4039}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004040#endif /* CONFIG_KALLSYMS */
4041
Arjan van de Ven21aa9282008-01-25 21:08:33 +01004042static char *module_flags(struct module *mod, char *buf)
Florin Malitafa3ba2e82006-10-11 01:21:48 -07004043{
4044 int bx = 0;
4045
Rusty Russell0d21b0e2013-01-12 11:38:44 +10304046 BUG_ON(mod->state == MODULE_STATE_UNFORMED);
Arjan van de Ven21aa9282008-01-25 21:08:33 +01004047 if (mod->taints ||
4048 mod->state == MODULE_STATE_GOING ||
4049 mod->state == MODULE_STATE_COMING) {
Florin Malitafa3ba2e82006-10-11 01:21:48 -07004050 buf[bx++] = '(';
Kay Sieverscca3e702012-01-13 09:32:15 +10304051 bx += module_flags_taint(mod, buf + bx);
Arjan van de Ven21aa9282008-01-25 21:08:33 +01004052 /* Show a - for module-is-being-unloaded */
4053 if (mod->state == MODULE_STATE_GOING)
4054 buf[bx++] = '-';
4055 /* Show a + for module-is-being-loaded */
4056 if (mod->state == MODULE_STATE_COMING)
4057 buf[bx++] = '+';
Florin Malitafa3ba2e82006-10-11 01:21:48 -07004058 buf[bx++] = ')';
4059 }
4060 buf[bx] = '\0';
4061
4062 return buf;
4063}
4064
Alexey Dobriyan3b5d5c62008-10-06 13:19:27 +04004065#ifdef CONFIG_PROC_FS
4066/* Called by the /proc file system to return a list of modules. */
4067static void *m_start(struct seq_file *m, loff_t *pos)
4068{
4069 mutex_lock(&module_mutex);
4070 return seq_list_start(&modules, *pos);
4071}
4072
4073static void *m_next(struct seq_file *m, void *p, loff_t *pos)
4074{
4075 return seq_list_next(p, &modules, pos);
4076}
4077
4078static void m_stop(struct seq_file *m, void *p)
4079{
4080 mutex_unlock(&module_mutex);
4081}
4082
Linus Torvalds1da177e2005-04-16 15:20:36 -07004083static int m_show(struct seq_file *m, void *p)
4084{
4085 struct module *mod = list_entry(p, struct module, list);
Florin Malitafa3ba2e82006-10-11 01:21:48 -07004086 char buf[8];
4087
Rusty Russell0d21b0e2013-01-12 11:38:44 +10304088 /* We always ignore unformed modules. */
4089 if (mod->state == MODULE_STATE_UNFORMED)
4090 return 0;
4091
Denys Vlasenko2f0f2a32008-07-22 19:24:27 -05004092 seq_printf(m, "%s %u",
Rusty Russell7523e4d2015-11-26 09:44:08 +10304093 mod->name, mod->init_layout.size + mod->core_layout.size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004094 print_unload_info(m, mod);
4095
4096 /* Informative for users. */
4097 seq_printf(m, " %s",
Ionut Alexa6da0b562014-11-10 09:31:29 +10304098 mod->state == MODULE_STATE_GOING ? "Unloading" :
4099 mod->state == MODULE_STATE_COMING ? "Loading" :
Linus Torvalds1da177e2005-04-16 15:20:36 -07004100 "Live");
4101 /* Used by oprofile and other similar tools. */
Rusty Russell7523e4d2015-11-26 09:44:08 +10304102 seq_printf(m, " 0x%pK", mod->core_layout.base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004103
Florin Malitafa3ba2e82006-10-11 01:21:48 -07004104 /* Taints info */
4105 if (mod->taints)
Arjan van de Ven21aa9282008-01-25 21:08:33 +01004106 seq_printf(m, " %s", module_flags(mod, buf));
Florin Malitafa3ba2e82006-10-11 01:21:48 -07004107
Ionut Alexa6da0b562014-11-10 09:31:29 +10304108 seq_puts(m, "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004109 return 0;
4110}
4111
4112/* Format: modulename size refcount deps address
4113
4114 Where refcount is a number or -, and deps is a comma-separated list
4115 of depends or -.
4116*/
Alexey Dobriyan3b5d5c62008-10-06 13:19:27 +04004117static const struct seq_operations modules_op = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004118 .start = m_start,
4119 .next = m_next,
4120 .stop = m_stop,
4121 .show = m_show
4122};
4123
Alexey Dobriyan3b5d5c62008-10-06 13:19:27 +04004124static int modules_open(struct inode *inode, struct file *file)
4125{
4126 return seq_open(file, &modules_op);
4127}
4128
4129static const struct file_operations proc_modules_operations = {
4130 .open = modules_open,
4131 .read = seq_read,
4132 .llseek = seq_lseek,
4133 .release = seq_release,
4134};
4135
4136static int __init proc_modules_init(void)
4137{
4138 proc_create("modules", 0, NULL, &proc_modules_operations);
4139 return 0;
4140}
4141module_init(proc_modules_init);
4142#endif
4143
Linus Torvalds1da177e2005-04-16 15:20:36 -07004144/* Given an address, look for it in the module exception tables. */
4145const struct exception_table_entry *search_module_extables(unsigned long addr)
4146{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004147 const struct exception_table_entry *e = NULL;
4148 struct module *mod;
4149
Rusty Russell24da1cb2007-07-15 23:41:46 -07004150 preempt_disable();
Andi Kleend72b3752008-08-30 10:09:00 +02004151 list_for_each_entry_rcu(mod, &modules, list) {
Rusty Russell0d21b0e2013-01-12 11:38:44 +10304152 if (mod->state == MODULE_STATE_UNFORMED)
4153 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004154 if (mod->num_exentries == 0)
4155 continue;
Daniel Walker22a8bde2007-10-18 03:06:07 -07004156
Linus Torvalds1da177e2005-04-16 15:20:36 -07004157 e = search_extable(mod->extable,
4158 mod->extable + mod->num_exentries - 1,
4159 addr);
4160 if (e)
4161 break;
4162 }
Rusty Russell24da1cb2007-07-15 23:41:46 -07004163 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004164
4165 /* Now, if we found one, we are running inside it now, hence
Daniel Walker22a8bde2007-10-18 03:06:07 -07004166 we cannot unload the module, hence no refcnt needed. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004167 return e;
4168}
4169
Ingo Molnar4d435f92006-07-03 00:24:24 -07004170/*
Rusty Russelle6104992009-03-31 13:05:31 -06004171 * is_module_address - is this address inside a module?
4172 * @addr: the address to check.
4173 *
4174 * See is_module_text_address() if you simply want to see if the address
4175 * is code (not data).
Ingo Molnar4d435f92006-07-03 00:24:24 -07004176 */
Rusty Russelle6104992009-03-31 13:05:31 -06004177bool is_module_address(unsigned long addr)
Ingo Molnar4d435f92006-07-03 00:24:24 -07004178{
Rusty Russelle6104992009-03-31 13:05:31 -06004179 bool ret;
Ingo Molnar4d435f92006-07-03 00:24:24 -07004180
Rusty Russell24da1cb2007-07-15 23:41:46 -07004181 preempt_disable();
Rusty Russelle6104992009-03-31 13:05:31 -06004182 ret = __module_address(addr) != NULL;
Rusty Russell24da1cb2007-07-15 23:41:46 -07004183 preempt_enable();
Ingo Molnar4d435f92006-07-03 00:24:24 -07004184
Rusty Russelle6104992009-03-31 13:05:31 -06004185 return ret;
Ingo Molnar4d435f92006-07-03 00:24:24 -07004186}
4187
Rusty Russelle6104992009-03-31 13:05:31 -06004188/*
4189 * __module_address - get the module which contains an address.
4190 * @addr: the address.
4191 *
4192 * Must be called with preempt disabled or module mutex held so that
4193 * module doesn't get freed during this.
4194 */
Linus Torvalds714f83d2009-04-05 11:04:19 -07004195struct module *__module_address(unsigned long addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004196{
4197 struct module *mod;
4198
Rusty Russell3a642e92008-07-22 19:24:28 -05004199 if (addr < module_addr_min || addr > module_addr_max)
4200 return NULL;
4201
Peter Zijlstra0be964b2015-05-27 11:09:35 +09304202 module_assert_mutex_or_preempt();
4203
Peter Zijlstra6c9692e2015-05-27 11:09:37 +09304204 mod = mod_find(addr);
Peter Zijlstra93c2e102015-05-27 11:09:37 +09304205 if (mod) {
4206 BUG_ON(!within_module(addr, mod));
Rusty Russell0d21b0e2013-01-12 11:38:44 +10304207 if (mod->state == MODULE_STATE_UNFORMED)
Peter Zijlstra93c2e102015-05-27 11:09:37 +09304208 mod = NULL;
Rusty Russell0d21b0e2013-01-12 11:38:44 +10304209 }
Peter Zijlstra93c2e102015-05-27 11:09:37 +09304210 return mod;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004211}
Tim Abbottc6b37802008-12-05 19:03:59 -05004212EXPORT_SYMBOL_GPL(__module_address);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004213
Rusty Russelle6104992009-03-31 13:05:31 -06004214/*
4215 * is_module_text_address - is this address inside module code?
4216 * @addr: the address to check.
4217 *
4218 * See is_module_address() if you simply want to see if the address is
4219 * anywhere in a module. See kernel_text_address() for testing if an
4220 * address corresponds to kernel or module code.
4221 */
4222bool is_module_text_address(unsigned long addr)
4223{
4224 bool ret;
4225
4226 preempt_disable();
4227 ret = __module_text_address(addr) != NULL;
4228 preempt_enable();
4229
4230 return ret;
4231}
4232
4233/*
4234 * __module_text_address - get the module whose code contains an address.
4235 * @addr: the address.
4236 *
4237 * Must be called with preempt disabled or module mutex held so that
4238 * module doesn't get freed during this.
4239 */
4240struct module *__module_text_address(unsigned long addr)
4241{
4242 struct module *mod = __module_address(addr);
4243 if (mod) {
4244 /* Make sure it's within the text section. */
Rusty Russell7523e4d2015-11-26 09:44:08 +10304245 if (!within(addr, mod->init_layout.base, mod->init_layout.text_size)
4246 && !within(addr, mod->core_layout.base, mod->core_layout.text_size))
Rusty Russelle6104992009-03-31 13:05:31 -06004247 mod = NULL;
4248 }
4249 return mod;
4250}
Tim Abbottc6b37802008-12-05 19:03:59 -05004251EXPORT_SYMBOL_GPL(__module_text_address);
Rusty Russelle6104992009-03-31 13:05:31 -06004252
Linus Torvalds1da177e2005-04-16 15:20:36 -07004253/* Don't grab lock, we're oopsing. */
4254void print_modules(void)
4255{
4256 struct module *mod;
Randy Dunlap2bc2d612006-10-02 02:17:02 -07004257 char buf[8];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004258
Linus Torvaldsb2311252009-06-16 11:07:14 -07004259 printk(KERN_DEFAULT "Modules linked in:");
Andi Kleend72b3752008-08-30 10:09:00 +02004260 /* Most callers should already have preempt disabled, but make sure */
4261 preempt_disable();
Rusty Russell0d21b0e2013-01-12 11:38:44 +10304262 list_for_each_entry_rcu(mod, &modules, list) {
4263 if (mod->state == MODULE_STATE_UNFORMED)
4264 continue;
Jiri Slaby27bba4d2014-02-03 11:13:13 +10304265 pr_cont(" %s%s", mod->name, module_flags(mod, buf));
Rusty Russell0d21b0e2013-01-12 11:38:44 +10304266 }
Andi Kleend72b3752008-08-30 10:09:00 +02004267 preempt_enable();
Arjan van de Vene14af7e2008-01-25 21:08:33 +01004268 if (last_unloaded_module[0])
Jiri Slaby27bba4d2014-02-03 11:13:13 +10304269 pr_cont(" [last unloaded: %s]", last_unloaded_module);
4270 pr_cont("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004271}
4272
Linus Torvalds1da177e2005-04-16 15:20:36 -07004273#ifdef CONFIG_MODVERSIONS
Rusty Russell8c8ef422009-03-31 13:05:34 -06004274/* Generate the signature for all relevant module structures here.
4275 * If these change, we don't want to try to parse the module. */
4276void module_layout(struct module *mod,
4277 struct modversion_info *ver,
4278 struct kernel_param *kp,
4279 struct kernel_symbol *ks,
Mathieu Desnoyers65498642011-01-26 17:26:22 -05004280 struct tracepoint * const *tp)
Rusty Russell8c8ef422009-03-31 13:05:34 -06004281{
4282}
4283EXPORT_SYMBOL(module_layout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004284#endif