blob: b0f92a3651403861530ee487ea7c6b987fe010ab [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>
Paul Gortmaker8a293be2016-07-23 14:01:45 -040020#include <linux/extable.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <linux/moduleloader.h>
Steven Rostedt (Red Hat)af658dc2015-04-29 14:36:05 -040022#include <linux/trace_events.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <linux/init.h>
Alexey Dobriyanae84e322007-05-08 00:28:38 -070024#include <linux/kallsyms.h>
Kees Cook34e11692012-10-16 07:31:07 +103025#include <linux/file.h>
Alexey Dobriyan3b5d5c62008-10-06 13:19:27 +040026#include <linux/fs.h>
Roland McGrath6d760132007-10-16 23:26:40 -070027#include <linux/sysfs.h>
Randy Dunlap9f158332005-09-13 01:25:16 -070028#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <linux/slab.h>
30#include <linux/vmalloc.h>
31#include <linux/elf.h>
Alexey Dobriyan3b5d5c62008-10-06 13:19:27 +040032#include <linux/proc_fs.h>
Kees Cook2e72d512012-10-16 07:32:07 +103033#include <linux/security.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include <linux/seq_file.h>
35#include <linux/syscalls.h>
36#include <linux/fcntl.h>
37#include <linux/rcupdate.h>
Randy.Dunlapc59ede72006-01-11 12:17:46 -080038#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include <linux/cpu.h>
40#include <linux/moduleparam.h>
41#include <linux/errno.h>
42#include <linux/err.h>
43#include <linux/vermagic.h>
44#include <linux/notifier.h>
Al Virof6a57032006-10-18 01:47:25 -040045#include <linux/sched.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070046#include <linux/device.h>
Matt Domschc988d2b2005-06-23 22:05:15 -070047#include <linux/string.h>
Arjan van de Ven97d1f152006-03-23 03:00:24 -080048#include <linux/mutex.h>
Andi Kleend72b3752008-08-30 10:09:00 +020049#include <linux/rculist.h>
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080050#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070051#include <asm/cacheflush.h>
Michael Ellerman563ec5c2017-07-06 15:35:58 -070052#include <linux/set_memory.h>
Bernd Schmidteb8cdec2009-09-21 17:03:57 -070053#include <asm/mmu_context.h>
Sam Ravnborgb817f6f2006-06-09 21:53:55 +020054#include <linux/license.h>
Christoph Lameter6d762392008-02-08 04:18:42 -080055#include <asm/sections.h>
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -040056#include <linux/tracepoint.h>
Steven Rostedt90d595f2008-08-14 15:45:09 -040057#include <linux/ftrace.h>
Jessica Yu7e545d62016-03-16 20:55:39 -040058#include <linux/livepatch.h>
Arjan van de Ven22a9d642009-01-07 08:45:46 -080059#include <linux/async.h>
Tejun Heofbf59bc2009-02-20 16:29:08 +090060#include <linux/percpu.h>
Catalin Marinas4f2294b2009-06-11 13:23:20 +010061#include <linux/kmemleak.h>
Jason Baronbf5438fc2010-09-17 11:09:00 -040062#include <linux/jump_label.h>
matthieu castet84e1c6b2010-11-16 22:35:16 +010063#include <linux/pfn.h>
Alessio Igor Bogani403ed272011-04-20 11:10:52 +020064#include <linux/bsearch.h>
Luis de Bethencourt9d5059c2016-08-02 14:03:47 -070065#include <linux/dynamic_debug.h>
Richard Guy Briggsca86cad2017-02-04 13:10:38 -050066#include <linux/audit.h>
Rusty Russell2f3238a2012-10-22 18:09:41 +103067#include <uapi/linux/module.h>
Rusty Russell106a4ee2012-09-26 10:09:40 +010068#include "module-internal.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
Li Zefan7ead8b82009-08-17 16:56:28 +080070#define CREATE_TRACE_POINTS
71#include <trace/events/module.h>
72
Linus Torvalds1da177e2005-04-16 15:20:36 -070073#ifndef ARCH_SHF_SMALL
74#define ARCH_SHF_SMALL 0
75#endif
76
matthieu castet84e1c6b2010-11-16 22:35:16 +010077/*
78 * Modules' sections will be aligned on page boundaries
79 * to ensure complete separation of code and data, but
Laura Abbott0f5bf6d2017-02-06 16:31:58 -080080 * only when CONFIG_STRICT_MODULE_RWX=y
matthieu castet84e1c6b2010-11-16 22:35:16 +010081 */
Laura Abbott0f5bf6d2017-02-06 16:31:58 -080082#ifdef CONFIG_STRICT_MODULE_RWX
matthieu castet84e1c6b2010-11-16 22:35:16 +010083# define debug_align(X) ALIGN(X, PAGE_SIZE)
84#else
85# define debug_align(X) (X)
86#endif
87
Linus Torvalds1da177e2005-04-16 15:20:36 -070088/* If this is set, the section belongs in the init part of the module */
89#define INIT_OFFSET_MASK (1UL << (BITS_PER_LONG-1))
90
Rusty Russell75676502010-06-05 11:17:36 -060091/*
92 * Mutex protects:
93 * 1) List of modules (also safely readable with preempt_disable),
94 * 2) module_use links,
95 * 3) module_addr_min/module_addr_max.
Masami Hiramatsue513cc12014-11-10 09:30:29 +103096 * (delete and add uses RCU list operations). */
Tim Abbottc6b37802008-12-05 19:03:59 -050097DEFINE_MUTEX(module_mutex);
98EXPORT_SYMBOL_GPL(module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070099static LIST_HEAD(modules);
Peter Zijlstra93c2e102015-05-27 11:09:37 +0930100
Peter Zijlstra6c9692e2015-05-27 11:09:37 +0930101#ifdef CONFIG_MODULES_TREE_LOOKUP
102
Peter Zijlstra93c2e102015-05-27 11:09:37 +0930103/*
104 * Use a latched RB-tree for __module_address(); this allows us to use
105 * RCU-sched lookups of the address from any context.
106 *
Peter Zijlstra6c9692e2015-05-27 11:09:37 +0930107 * This is conditional on PERF_EVENTS || TRACING because those can really hit
108 * __module_address() hard by doing a lot of stack unwinding; potentially from
109 * NMI context.
Peter Zijlstra93c2e102015-05-27 11:09:37 +0930110 */
111
112static __always_inline unsigned long __mod_tree_val(struct latch_tree_node *n)
113{
Rusty Russell7523e4d2015-11-26 09:44:08 +1030114 struct module_layout *layout = container_of(n, struct module_layout, mtn.node);
Peter Zijlstra93c2e102015-05-27 11:09:37 +0930115
Rusty Russell7523e4d2015-11-26 09:44:08 +1030116 return (unsigned long)layout->base;
Peter Zijlstra93c2e102015-05-27 11:09:37 +0930117}
118
119static __always_inline unsigned long __mod_tree_size(struct latch_tree_node *n)
120{
Rusty Russell7523e4d2015-11-26 09:44:08 +1030121 struct module_layout *layout = container_of(n, struct module_layout, mtn.node);
Peter Zijlstra93c2e102015-05-27 11:09:37 +0930122
Rusty Russell7523e4d2015-11-26 09:44:08 +1030123 return (unsigned long)layout->size;
Peter Zijlstra93c2e102015-05-27 11:09:37 +0930124}
125
126static __always_inline bool
127mod_tree_less(struct latch_tree_node *a, struct latch_tree_node *b)
128{
129 return __mod_tree_val(a) < __mod_tree_val(b);
130}
131
132static __always_inline int
133mod_tree_comp(void *key, struct latch_tree_node *n)
134{
135 unsigned long val = (unsigned long)key;
136 unsigned long start, end;
137
138 start = __mod_tree_val(n);
139 if (val < start)
140 return -1;
141
142 end = start + __mod_tree_size(n);
143 if (val >= end)
144 return 1;
145
146 return 0;
147}
148
149static const struct latch_tree_ops mod_tree_ops = {
150 .less = mod_tree_less,
151 .comp = mod_tree_comp,
152};
153
Peter Zijlstra4f6665462015-05-27 11:09:38 +0930154static struct mod_tree_root {
155 struct latch_tree_root root;
156 unsigned long addr_min;
157 unsigned long addr_max;
158} mod_tree __cacheline_aligned = {
159 .addr_min = -1UL,
160};
161
162#define module_addr_min mod_tree.addr_min
163#define module_addr_max mod_tree.addr_max
164
165static noinline void __mod_tree_insert(struct mod_tree_node *node)
166{
167 latch_tree_insert(&node->node, &mod_tree.root, &mod_tree_ops);
168}
169
170static void __mod_tree_remove(struct mod_tree_node *node)
171{
172 latch_tree_erase(&node->node, &mod_tree.root, &mod_tree_ops);
173}
Peter Zijlstra93c2e102015-05-27 11:09:37 +0930174
175/*
176 * These modifications: insert, remove_init and remove; are serialized by the
177 * module_mutex.
178 */
179static void mod_tree_insert(struct module *mod)
180{
Rusty Russell7523e4d2015-11-26 09:44:08 +1030181 mod->core_layout.mtn.mod = mod;
182 mod->init_layout.mtn.mod = mod;
Peter Zijlstra93c2e102015-05-27 11:09:37 +0930183
Rusty Russell7523e4d2015-11-26 09:44:08 +1030184 __mod_tree_insert(&mod->core_layout.mtn);
185 if (mod->init_layout.size)
186 __mod_tree_insert(&mod->init_layout.mtn);
Peter Zijlstra93c2e102015-05-27 11:09:37 +0930187}
188
189static void mod_tree_remove_init(struct module *mod)
190{
Rusty Russell7523e4d2015-11-26 09:44:08 +1030191 if (mod->init_layout.size)
192 __mod_tree_remove(&mod->init_layout.mtn);
Peter Zijlstra93c2e102015-05-27 11:09:37 +0930193}
194
195static void mod_tree_remove(struct module *mod)
196{
Rusty Russell7523e4d2015-11-26 09:44:08 +1030197 __mod_tree_remove(&mod->core_layout.mtn);
Peter Zijlstra93c2e102015-05-27 11:09:37 +0930198 mod_tree_remove_init(mod);
199}
200
Peter Zijlstra6c9692e2015-05-27 11:09:37 +0930201static struct module *mod_find(unsigned long addr)
Peter Zijlstra93c2e102015-05-27 11:09:37 +0930202{
203 struct latch_tree_node *ltn;
204
Peter Zijlstra4f6665462015-05-27 11:09:38 +0930205 ltn = latch_tree_find((void *)addr, &mod_tree.root, &mod_tree_ops);
Peter Zijlstra93c2e102015-05-27 11:09:37 +0930206 if (!ltn)
207 return NULL;
208
209 return container_of(ltn, struct mod_tree_node, node)->mod;
210}
211
Peter Zijlstra6c9692e2015-05-27 11:09:37 +0930212#else /* MODULES_TREE_LOOKUP */
213
Peter Zijlstra4f6665462015-05-27 11:09:38 +0930214static unsigned long module_addr_min = -1UL, module_addr_max = 0;
215
Peter Zijlstra6c9692e2015-05-27 11:09:37 +0930216static void mod_tree_insert(struct module *mod) { }
217static void mod_tree_remove_init(struct module *mod) { }
218static void mod_tree_remove(struct module *mod) { }
219
220static struct module *mod_find(unsigned long addr)
221{
222 struct module *mod;
223
224 list_for_each_entry_rcu(mod, &modules, list) {
225 if (within_module(addr, mod))
226 return mod;
227 }
228
229 return NULL;
230}
231
232#endif /* MODULES_TREE_LOOKUP */
233
Peter Zijlstra4f6665462015-05-27 11:09:38 +0930234/*
235 * Bounds of module text, for speeding up __module_address.
236 * Protected by module_mutex.
237 */
238static void __mod_update_bounds(void *base, unsigned int size)
239{
240 unsigned long min = (unsigned long)base;
241 unsigned long max = min + size;
242
243 if (min < module_addr_min)
244 module_addr_min = min;
245 if (max > module_addr_max)
246 module_addr_max = max;
247}
248
249static void mod_update_bounds(struct module *mod)
250{
Rusty Russell7523e4d2015-11-26 09:44:08 +1030251 __mod_update_bounds(mod->core_layout.base, mod->core_layout.size);
252 if (mod->init_layout.size)
253 __mod_update_bounds(mod->init_layout.base, mod->init_layout.size);
Peter Zijlstra4f6665462015-05-27 11:09:38 +0930254}
255
Jason Wessel67fc4e02010-05-20 21:04:21 -0500256#ifdef CONFIG_KGDB_KDB
257struct list_head *kdb_modules = &modules; /* kdb needs the list of modules */
258#endif /* CONFIG_KGDB_KDB */
259
Peter Zijlstra0be964b2015-05-27 11:09:35 +0930260static void module_assert_mutex(void)
Rusty Russell106a4ee2012-09-26 10:09:40 +0100261{
Peter Zijlstra0be964b2015-05-27 11:09:35 +0930262 lockdep_assert_held(&module_mutex);
Rusty Russell106a4ee2012-09-26 10:09:40 +0100263}
264
Peter Zijlstra0be964b2015-05-27 11:09:35 +0930265static void module_assert_mutex_or_preempt(void)
266{
267#ifdef CONFIG_LOCKDEP
268 if (unlikely(!debug_locks))
269 return;
Rusty Russell106a4ee2012-09-26 10:09:40 +0100270
Steven Rostedt95025142016-07-19 05:59:24 +0930271 WARN_ON_ONCE(!rcu_read_lock_sched_held() &&
Peter Zijlstra0be964b2015-05-27 11:09:35 +0930272 !lockdep_is_held(&module_mutex));
273#endif
274}
275
Luis R. Rodriguez6727bb92015-05-27 11:09:39 +0930276static bool sig_enforce = IS_ENABLED(CONFIG_MODULE_SIG_FORCE);
277#ifndef CONFIG_MODULE_SIG_FORCE
Rusty Russell106a4ee2012-09-26 10:09:40 +0100278module_param(sig_enforce, bool_enable_only, 0644);
279#endif /* !CONFIG_MODULE_SIG_FORCE */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280
Stephen Rothwell19e45292009-04-14 17:27:18 +1000281/* Block module loading/unloading? */
282int modules_disabled = 0;
Dave Young02608be2012-02-01 10:33:14 +0800283core_param(nomodule, modules_disabled, bint, 0);
Stephen Rothwell19e45292009-04-14 17:27:18 +1000284
Rusty Russellc9a3ba52008-01-29 17:13:18 -0500285/* Waiting for a module to finish initializing? */
286static DECLARE_WAIT_QUEUE_HEAD(module_wq);
287
Alan Sterne041c682006-03-27 01:16:30 -0800288static BLOCKING_NOTIFIER_HEAD(module_notify_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289
Ionut Alexa6da0b562014-11-10 09:31:29 +1030290int register_module_notifier(struct notifier_block *nb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291{
Alan Sterne041c682006-03-27 01:16:30 -0800292 return blocking_notifier_chain_register(&module_notify_list, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293}
294EXPORT_SYMBOL(register_module_notifier);
295
Ionut Alexa6da0b562014-11-10 09:31:29 +1030296int unregister_module_notifier(struct notifier_block *nb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297{
Alan Sterne041c682006-03-27 01:16:30 -0800298 return blocking_notifier_chain_unregister(&module_notify_list, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299}
300EXPORT_SYMBOL(unregister_module_notifier);
301
Rusty Russelleded41c2010-08-05 12:59:07 -0600302struct load_info {
303 Elf_Ehdr *hdr;
304 unsigned long len;
305 Elf_Shdr *sechdrs;
Rusty Russell6526c532010-08-05 12:59:10 -0600306 char *secstrings, *strtab;
Rusty Russelld9131882010-08-05 12:59:08 -0600307 unsigned long symoffs, stroffs;
Rusty Russell811d66a2010-08-05 12:59:12 -0600308 struct _ddebug *debug;
309 unsigned int num_debug;
Rusty Russell106a4ee2012-09-26 10:09:40 +0100310 bool sig_ok;
Rusty Russell82440622016-02-03 16:55:26 +1030311#ifdef CONFIG_KALLSYMS
312 unsigned long mod_kallsyms_init_off;
313#endif
Rusty Russelleded41c2010-08-05 12:59:07 -0600314 struct {
315 unsigned int sym, str, mod, vers, info, pcpu;
316 } index;
317};
318
Miroslav Benes71d9f502016-11-16 16:45:48 +0100319/*
320 * We require a truly strong try_module_get(): 0 means success.
321 * Otherwise an error is returned due to ongoing or failed
322 * initialization etc.
323 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324static inline int strong_try_module_get(struct module *mod)
325{
Rusty Russell0d21b0e2013-01-12 11:38:44 +1030326 BUG_ON(mod && mod->state == MODULE_STATE_UNFORMED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 if (mod && mod->state == MODULE_STATE_COMING)
Rusty Russellc9a3ba52008-01-29 17:13:18 -0500328 return -EBUSY;
329 if (try_module_get(mod))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 return 0;
Rusty Russellc9a3ba52008-01-29 17:13:18 -0500331 else
332 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333}
334
Rusty Russell373d4d02013-01-21 17:17:39 +1030335static inline void add_taint_module(struct module *mod, unsigned flag,
336 enum lockdep_ok lockdep_ok)
Florin Malitafa3ba2e82006-10-11 01:21:48 -0700337{
Rusty Russell373d4d02013-01-21 17:17:39 +1030338 add_taint(flag, lockdep_ok);
Petr Mladek7fd83292016-09-21 13:47:22 +0200339 set_bit(flag, &mod->taints);
Florin Malitafa3ba2e82006-10-11 01:21:48 -0700340}
341
Robert P. J. Day02a3e592007-05-09 07:26:28 +0200342/*
343 * A thread that wants to hold a reference to a module only while it
344 * is running can call this to safely exit. nfsd and lockd use this.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 */
Jiri Kosinabf262dc2016-04-12 05:02:09 +0930346void __noreturn __module_put_and_exit(struct module *mod, long code)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347{
348 module_put(mod);
349 do_exit(code);
350}
351EXPORT_SYMBOL(__module_put_and_exit);
Daniel Walker22a8bde2007-10-18 03:06:07 -0700352
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353/* Find a module section: 0 means not found. */
Rusty Russell49668682010-08-05 12:59:10 -0600354static unsigned int find_sec(const struct load_info *info, const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355{
356 unsigned int i;
357
Rusty Russell49668682010-08-05 12:59:10 -0600358 for (i = 1; i < info->hdr->e_shnum; i++) {
359 Elf_Shdr *shdr = &info->sechdrs[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 /* Alloc bit cleared means "ignore it." */
Rusty Russell49668682010-08-05 12:59:10 -0600361 if ((shdr->sh_flags & SHF_ALLOC)
362 && strcmp(info->secstrings + shdr->sh_name, name) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 return i;
Rusty Russell49668682010-08-05 12:59:10 -0600364 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 return 0;
366}
367
Rusty Russell5e458cc2008-10-22 10:00:13 -0500368/* Find a module section, or NULL. */
Rusty Russell49668682010-08-05 12:59:10 -0600369static void *section_addr(const struct load_info *info, const char *name)
Rusty Russell5e458cc2008-10-22 10:00:13 -0500370{
371 /* Section 0 has sh_addr 0. */
Rusty Russell49668682010-08-05 12:59:10 -0600372 return (void *)info->sechdrs[find_sec(info, name)].sh_addr;
Rusty Russell5e458cc2008-10-22 10:00:13 -0500373}
374
375/* Find a module section, or NULL. Fill in number of "objects" in section. */
Rusty Russell49668682010-08-05 12:59:10 -0600376static void *section_objs(const struct load_info *info,
Rusty Russell5e458cc2008-10-22 10:00:13 -0500377 const char *name,
378 size_t object_size,
379 unsigned int *num)
380{
Rusty Russell49668682010-08-05 12:59:10 -0600381 unsigned int sec = find_sec(info, name);
Rusty Russell5e458cc2008-10-22 10:00:13 -0500382
383 /* Section 0 has sh_addr 0 and sh_size 0. */
Rusty Russell49668682010-08-05 12:59:10 -0600384 *num = info->sechdrs[sec].sh_size / object_size;
385 return (void *)info->sechdrs[sec].sh_addr;
Rusty Russell5e458cc2008-10-22 10:00:13 -0500386}
387
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388/* Provided by the linker */
389extern const struct kernel_symbol __start___ksymtab[];
390extern const struct kernel_symbol __stop___ksymtab[];
391extern const struct kernel_symbol __start___ksymtab_gpl[];
392extern const struct kernel_symbol __stop___ksymtab_gpl[];
Greg Kroah-Hartman9f28bb72006-03-20 13:17:13 -0800393extern const struct kernel_symbol __start___ksymtab_gpl_future[];
394extern const struct kernel_symbol __stop___ksymtab_gpl_future[];
Ard Biesheuvel71810db2017-02-03 09:54:06 +0000395extern const s32 __start___kcrctab[];
396extern const s32 __start___kcrctab_gpl[];
397extern const s32 __start___kcrctab_gpl_future[];
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500398#ifdef CONFIG_UNUSED_SYMBOLS
399extern const struct kernel_symbol __start___ksymtab_unused[];
400extern const struct kernel_symbol __stop___ksymtab_unused[];
401extern const struct kernel_symbol __start___ksymtab_unused_gpl[];
402extern const struct kernel_symbol __stop___ksymtab_unused_gpl[];
Ard Biesheuvel71810db2017-02-03 09:54:06 +0000403extern const s32 __start___kcrctab_unused[];
404extern const s32 __start___kcrctab_unused_gpl[];
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500405#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406
407#ifndef CONFIG_MODVERSIONS
408#define symversion(base, idx) NULL
409#else
Andrew Mortonf83ca9f2006-03-28 01:56:20 -0800410#define symversion(base, idx) ((base != NULL) ? ((base) + (idx)) : NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411#endif
412
Rusty Russelldafd0942008-07-22 19:24:25 -0500413static bool each_symbol_in_section(const struct symsearch *arr,
414 unsigned int arrsize,
415 struct module *owner,
416 bool (*fn)(const struct symsearch *syms,
417 struct module *owner,
Rusty Russellde4d8d52011-04-19 21:49:58 +0200418 void *data),
Rusty Russelldafd0942008-07-22 19:24:25 -0500419 void *data)
Sam Ravnborg3fd68052006-02-08 21:16:45 +0100420{
Rusty Russellde4d8d52011-04-19 21:49:58 +0200421 unsigned int j;
Rusty Russelldafd0942008-07-22 19:24:25 -0500422
423 for (j = 0; j < arrsize; j++) {
Rusty Russellde4d8d52011-04-19 21:49:58 +0200424 if (fn(&arr[j], owner, data))
425 return true;
Rusty Russelldafd0942008-07-22 19:24:25 -0500426 }
427
428 return false;
Sam Ravnborg3fd68052006-02-08 21:16:45 +0100429}
430
Rusty Russelldafd0942008-07-22 19:24:25 -0500431/* Returns true as soon as fn returns true, otherwise false. */
Rusty Russellde4d8d52011-04-19 21:49:58 +0200432bool each_symbol_section(bool (*fn)(const struct symsearch *arr,
433 struct module *owner,
434 void *data),
435 void *data)
Arjan van de Venf71d20e2006-06-28 04:26:45 -0700436{
Rusty Russelldafd0942008-07-22 19:24:25 -0500437 struct module *mod;
Linus Torvalds44032e62010-08-05 12:59:05 -0600438 static const struct symsearch arr[] = {
Rusty Russelldafd0942008-07-22 19:24:25 -0500439 { __start___ksymtab, __stop___ksymtab, __start___kcrctab,
440 NOT_GPL_ONLY, false },
441 { __start___ksymtab_gpl, __stop___ksymtab_gpl,
442 __start___kcrctab_gpl,
443 GPL_ONLY, false },
444 { __start___ksymtab_gpl_future, __stop___ksymtab_gpl_future,
445 __start___kcrctab_gpl_future,
446 WILL_BE_GPL_ONLY, false },
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500447#ifdef CONFIG_UNUSED_SYMBOLS
Rusty Russelldafd0942008-07-22 19:24:25 -0500448 { __start___ksymtab_unused, __stop___ksymtab_unused,
449 __start___kcrctab_unused,
450 NOT_GPL_ONLY, true },
451 { __start___ksymtab_unused_gpl, __stop___ksymtab_unused_gpl,
452 __start___kcrctab_unused_gpl,
453 GPL_ONLY, true },
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500454#endif
Rusty Russelldafd0942008-07-22 19:24:25 -0500455 };
456
Peter Zijlstra0be964b2015-05-27 11:09:35 +0930457 module_assert_mutex_or_preempt();
458
Rusty Russelldafd0942008-07-22 19:24:25 -0500459 if (each_symbol_in_section(arr, ARRAY_SIZE(arr), NULL, fn, data))
460 return true;
461
Andi Kleend72b3752008-08-30 10:09:00 +0200462 list_for_each_entry_rcu(mod, &modules, list) {
Rusty Russelldafd0942008-07-22 19:24:25 -0500463 struct symsearch arr[] = {
464 { mod->syms, mod->syms + mod->num_syms, mod->crcs,
465 NOT_GPL_ONLY, false },
466 { mod->gpl_syms, mod->gpl_syms + mod->num_gpl_syms,
467 mod->gpl_crcs,
468 GPL_ONLY, false },
469 { mod->gpl_future_syms,
470 mod->gpl_future_syms + mod->num_gpl_future_syms,
471 mod->gpl_future_crcs,
472 WILL_BE_GPL_ONLY, false },
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500473#ifdef CONFIG_UNUSED_SYMBOLS
Rusty Russelldafd0942008-07-22 19:24:25 -0500474 { mod->unused_syms,
475 mod->unused_syms + mod->num_unused_syms,
476 mod->unused_crcs,
477 NOT_GPL_ONLY, true },
478 { mod->unused_gpl_syms,
479 mod->unused_gpl_syms + mod->num_unused_gpl_syms,
480 mod->unused_gpl_crcs,
481 GPL_ONLY, true },
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500482#endif
Rusty Russelldafd0942008-07-22 19:24:25 -0500483 };
484
Rusty Russell0d21b0e2013-01-12 11:38:44 +1030485 if (mod->state == MODULE_STATE_UNFORMED)
486 continue;
487
Rusty Russelldafd0942008-07-22 19:24:25 -0500488 if (each_symbol_in_section(arr, ARRAY_SIZE(arr), mod, fn, data))
489 return true;
490 }
491 return false;
Arjan van de Venf71d20e2006-06-28 04:26:45 -0700492}
Rusty Russellde4d8d52011-04-19 21:49:58 +0200493EXPORT_SYMBOL_GPL(each_symbol_section);
Arjan van de Venf71d20e2006-06-28 04:26:45 -0700494
Rusty Russelldafd0942008-07-22 19:24:25 -0500495struct find_symbol_arg {
496 /* Input */
497 const char *name;
498 bool gplok;
499 bool warn;
500
501 /* Output */
502 struct module *owner;
Ard Biesheuvel71810db2017-02-03 09:54:06 +0000503 const s32 *crc;
Tim Abbott414fd312008-12-05 19:03:56 -0500504 const struct kernel_symbol *sym;
Rusty Russelldafd0942008-07-22 19:24:25 -0500505};
506
Rusty Russellde4d8d52011-04-19 21:49:58 +0200507static bool check_symbol(const struct symsearch *syms,
508 struct module *owner,
509 unsigned int symnum, void *data)
Rusty Russellad9546c2008-05-01 21:14:59 -0500510{
Rusty Russelldafd0942008-07-22 19:24:25 -0500511 struct find_symbol_arg *fsa = data;
512
Rusty Russelldafd0942008-07-22 19:24:25 -0500513 if (!fsa->gplok) {
514 if (syms->licence == GPL_ONLY)
515 return false;
516 if (syms->licence == WILL_BE_GPL_ONLY && fsa->warn) {
Andrew Mortonbddb12b2013-11-12 15:11:28 -0800517 pr_warn("Symbol %s is being used by a non-GPL module, "
518 "which will not be allowed in the future\n",
519 fsa->name);
Rusty Russelldafd0942008-07-22 19:24:25 -0500520 }
521 }
522
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500523#ifdef CONFIG_UNUSED_SYMBOLS
Rusty Russelldafd0942008-07-22 19:24:25 -0500524 if (syms->unused && fsa->warn) {
Andrew Mortonbddb12b2013-11-12 15:11:28 -0800525 pr_warn("Symbol %s is marked as UNUSED, however this module is "
526 "using it.\n", fsa->name);
527 pr_warn("This symbol will go away in the future.\n");
Yannick Guerrini7b63c3a2015-03-24 12:31:40 +1030528 pr_warn("Please evaluate if this is the right api to use and "
529 "if it really is, submit a report to the linux kernel "
530 "mailing list together with submitting your code for "
Andrew Mortonbddb12b2013-11-12 15:11:28 -0800531 "inclusion.\n");
Rusty Russellad9546c2008-05-01 21:14:59 -0500532 }
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500533#endif
Rusty Russelldafd0942008-07-22 19:24:25 -0500534
535 fsa->owner = owner;
536 fsa->crc = symversion(syms->crcs, symnum);
Tim Abbott414fd312008-12-05 19:03:56 -0500537 fsa->sym = &syms->start[symnum];
Rusty Russellad9546c2008-05-01 21:14:59 -0500538 return true;
539}
540
Alessio Igor Bogani403ed272011-04-20 11:10:52 +0200541static int cmp_name(const void *va, const void *vb)
542{
543 const char *a;
544 const struct kernel_symbol *b;
545 a = va; b = vb;
546 return strcmp(a, b->name);
547}
548
Rusty Russellde4d8d52011-04-19 21:49:58 +0200549static bool find_symbol_in_section(const struct symsearch *syms,
550 struct module *owner,
551 void *data)
552{
553 struct find_symbol_arg *fsa = data;
Alessio Igor Bogani403ed272011-04-20 11:10:52 +0200554 struct kernel_symbol *sym;
Rusty Russellde4d8d52011-04-19 21:49:58 +0200555
Alessio Igor Bogani403ed272011-04-20 11:10:52 +0200556 sym = bsearch(fsa->name, syms->start, syms->stop - syms->start,
557 sizeof(struct kernel_symbol), cmp_name);
558
559 if (sym != NULL && check_symbol(syms, owner, sym - syms->start, data))
560 return true;
561
Rusty Russellde4d8d52011-04-19 21:49:58 +0200562 return false;
563}
564
Tim Abbott414fd312008-12-05 19:03:56 -0500565/* Find a symbol and return it, along with, (optional) crc and
Rusty Russell75676502010-06-05 11:17:36 -0600566 * (optional) module which owns it. Needs preempt disabled or module_mutex. */
Tim Abbottc6b37802008-12-05 19:03:59 -0500567const struct kernel_symbol *find_symbol(const char *name,
568 struct module **owner,
Ard Biesheuvel71810db2017-02-03 09:54:06 +0000569 const s32 **crc,
Tim Abbottc6b37802008-12-05 19:03:59 -0500570 bool gplok,
571 bool warn)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572{
Rusty Russelldafd0942008-07-22 19:24:25 -0500573 struct find_symbol_arg fsa;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574
Rusty Russelldafd0942008-07-22 19:24:25 -0500575 fsa.name = name;
576 fsa.gplok = gplok;
577 fsa.warn = warn;
578
Rusty Russellde4d8d52011-04-19 21:49:58 +0200579 if (each_symbol_section(find_symbol_in_section, &fsa)) {
Rusty Russellad9546c2008-05-01 21:14:59 -0500580 if (owner)
Rusty Russelldafd0942008-07-22 19:24:25 -0500581 *owner = fsa.owner;
582 if (crc)
583 *crc = fsa.crc;
Tim Abbott414fd312008-12-05 19:03:56 -0500584 return fsa.sym;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 }
Rusty Russellad9546c2008-05-01 21:14:59 -0500586
Jim Cromie5e124162011-12-06 12:11:31 -0700587 pr_debug("Failed to find symbol %s\n", name);
Tim Abbott414fd312008-12-05 19:03:56 -0500588 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589}
Tim Abbottc6b37802008-12-05 19:03:59 -0500590EXPORT_SYMBOL_GPL(find_symbol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591
Rusty Russellfe0d34d2015-07-29 05:52:14 +0930592/*
593 * Search for module by name: must hold module_mutex (or preempt disabled
594 * for read-only access).
595 */
Mathias Krause4f6de4d2013-07-02 15:35:11 +0930596static struct module *find_module_all(const char *name, size_t len,
Rusty Russell0d21b0e2013-01-12 11:38:44 +1030597 bool even_unformed)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598{
599 struct module *mod;
600
Rusty Russellfe0d34d2015-07-29 05:52:14 +0930601 module_assert_mutex_or_preempt();
Peter Zijlstra0be964b2015-05-27 11:09:35 +0930602
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 list_for_each_entry(mod, &modules, list) {
Rusty Russell0d21b0e2013-01-12 11:38:44 +1030604 if (!even_unformed && mod->state == MODULE_STATE_UNFORMED)
605 continue;
Mathias Krause4f6de4d2013-07-02 15:35:11 +0930606 if (strlen(mod->name) == len && !memcmp(mod->name, name, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 return mod;
608 }
609 return NULL;
610}
Rusty Russell0d21b0e2013-01-12 11:38:44 +1030611
612struct module *find_module(const char *name)
613{
Rusty Russellfe0d34d2015-07-29 05:52:14 +0930614 module_assert_mutex();
Mathias Krause4f6de4d2013-07-02 15:35:11 +0930615 return find_module_all(name, strlen(name), false);
Rusty Russell0d21b0e2013-01-12 11:38:44 +1030616}
Tim Abbottc6b37802008-12-05 19:03:59 -0500617EXPORT_SYMBOL_GPL(find_module);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618
619#ifdef CONFIG_SMP
Tejun Heofbf59bc2009-02-20 16:29:08 +0900620
Tejun Heo259354d2010-03-10 18:56:10 +0900621static inline void __percpu *mod_percpu(struct module *mod)
Tejun Heofbf59bc2009-02-20 16:29:08 +0900622{
Tejun Heo259354d2010-03-10 18:56:10 +0900623 return mod->percpu;
624}
Tejun Heofbf59bc2009-02-20 16:29:08 +0900625
Rusty Russell9eb76d72013-07-03 10:06:29 +0930626static int percpu_modalloc(struct module *mod, struct load_info *info)
Tejun Heo259354d2010-03-10 18:56:10 +0900627{
Rusty Russell9eb76d72013-07-03 10:06:29 +0930628 Elf_Shdr *pcpusec = &info->sechdrs[info->index.pcpu];
629 unsigned long align = pcpusec->sh_addralign;
630
631 if (!pcpusec->sh_size)
632 return 0;
633
Tejun Heofbf59bc2009-02-20 16:29:08 +0900634 if (align > PAGE_SIZE) {
Andrew Mortonbddb12b2013-11-12 15:11:28 -0800635 pr_warn("%s: per-cpu alignment %li > %li\n",
636 mod->name, align, PAGE_SIZE);
Tejun Heofbf59bc2009-02-20 16:29:08 +0900637 align = PAGE_SIZE;
638 }
639
Rusty Russell9eb76d72013-07-03 10:06:29 +0930640 mod->percpu = __alloc_reserved_percpu(pcpusec->sh_size, align);
Tejun Heo259354d2010-03-10 18:56:10 +0900641 if (!mod->percpu) {
Andrew Mortonbddb12b2013-11-12 15:11:28 -0800642 pr_warn("%s: Could not allocate %lu bytes percpu data\n",
643 mod->name, (unsigned long)pcpusec->sh_size);
Tejun Heo259354d2010-03-10 18:56:10 +0900644 return -ENOMEM;
645 }
Rusty Russell9eb76d72013-07-03 10:06:29 +0930646 mod->percpu_size = pcpusec->sh_size;
Tejun Heo259354d2010-03-10 18:56:10 +0900647 return 0;
Tejun Heofbf59bc2009-02-20 16:29:08 +0900648}
649
Tejun Heo259354d2010-03-10 18:56:10 +0900650static void percpu_modfree(struct module *mod)
Tejun Heofbf59bc2009-02-20 16:29:08 +0900651{
Tejun Heo259354d2010-03-10 18:56:10 +0900652 free_percpu(mod->percpu);
Tejun Heofbf59bc2009-02-20 16:29:08 +0900653}
654
Rusty Russell49668682010-08-05 12:59:10 -0600655static unsigned int find_pcpusec(struct load_info *info)
Tejun Heo6b588c12009-02-20 16:29:07 +0900656{
Rusty Russell49668682010-08-05 12:59:10 -0600657 return find_sec(info, ".data..percpu");
Tejun Heo6b588c12009-02-20 16:29:07 +0900658}
659
Tejun Heo259354d2010-03-10 18:56:10 +0900660static void percpu_modcopy(struct module *mod,
661 const void *from, unsigned long size)
Tejun Heo6b588c12009-02-20 16:29:07 +0900662{
663 int cpu;
664
665 for_each_possible_cpu(cpu)
Tejun Heo259354d2010-03-10 18:56:10 +0900666 memcpy(per_cpu_ptr(mod->percpu, cpu), from, size);
Tejun Heo6b588c12009-02-20 16:29:07 +0900667}
668
Thomas Gleixner383776f2017-02-27 15:37:36 +0100669bool __is_module_percpu_address(unsigned long addr, unsigned long *can_addr)
Tejun Heo10fad5e2010-03-10 18:57:54 +0900670{
671 struct module *mod;
672 unsigned int cpu;
673
674 preempt_disable();
675
676 list_for_each_entry_rcu(mod, &modules, list) {
Rusty Russell0d21b0e2013-01-12 11:38:44 +1030677 if (mod->state == MODULE_STATE_UNFORMED)
678 continue;
Tejun Heo10fad5e2010-03-10 18:57:54 +0900679 if (!mod->percpu_size)
680 continue;
681 for_each_possible_cpu(cpu) {
682 void *start = per_cpu_ptr(mod->percpu, cpu);
Thomas Gleixner383776f2017-02-27 15:37:36 +0100683 void *va = (void *)addr;
Tejun Heo10fad5e2010-03-10 18:57:54 +0900684
Thomas Gleixner383776f2017-02-27 15:37:36 +0100685 if (va >= start && va < start + mod->percpu_size) {
Peter Zijlstra8ce371f2017-03-20 12:26:55 +0100686 if (can_addr) {
Thomas Gleixner383776f2017-02-27 15:37:36 +0100687 *can_addr = (unsigned long) (va - start);
Peter Zijlstra8ce371f2017-03-20 12:26:55 +0100688 *can_addr += (unsigned long)
689 per_cpu_ptr(mod->percpu,
690 get_boot_cpu_id());
691 }
Tejun Heo10fad5e2010-03-10 18:57:54 +0900692 preempt_enable();
693 return true;
694 }
695 }
696 }
697
698 preempt_enable();
699 return false;
Daniel Walker22a8bde2007-10-18 03:06:07 -0700700}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701
Thomas Gleixner383776f2017-02-27 15:37:36 +0100702/**
703 * is_module_percpu_address - test whether address is from module static percpu
704 * @addr: address to test
705 *
706 * Test whether @addr belongs to module static percpu area.
707 *
708 * RETURNS:
709 * %true if @addr is from module static percpu area
710 */
711bool is_module_percpu_address(unsigned long addr)
712{
713 return __is_module_percpu_address(addr, NULL);
714}
715
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716#else /* ... !CONFIG_SMP */
Tejun Heo6b588c12009-02-20 16:29:07 +0900717
Tejun Heo259354d2010-03-10 18:56:10 +0900718static inline void __percpu *mod_percpu(struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719{
720 return NULL;
721}
Rusty Russell9eb76d72013-07-03 10:06:29 +0930722static int percpu_modalloc(struct module *mod, struct load_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723{
Rusty Russell9eb76d72013-07-03 10:06:29 +0930724 /* UP modules shouldn't have this section: ENOMEM isn't quite right */
725 if (info->sechdrs[info->index.pcpu].sh_size != 0)
726 return -ENOMEM;
727 return 0;
Tejun Heo259354d2010-03-10 18:56:10 +0900728}
729static inline void percpu_modfree(struct module *mod)
730{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731}
Rusty Russell49668682010-08-05 12:59:10 -0600732static unsigned int find_pcpusec(struct load_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733{
734 return 0;
735}
Tejun Heo259354d2010-03-10 18:56:10 +0900736static inline void percpu_modcopy(struct module *mod,
737 const void *from, unsigned long size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738{
739 /* pcpusec should be 0, and size of that section should be 0. */
740 BUG_ON(size != 0);
741}
Tejun Heo10fad5e2010-03-10 18:57:54 +0900742bool is_module_percpu_address(unsigned long addr)
743{
744 return false;
745}
Tejun Heo6b588c12009-02-20 16:29:07 +0900746
Thomas Gleixner383776f2017-02-27 15:37:36 +0100747bool __is_module_percpu_address(unsigned long addr, unsigned long *can_addr)
748{
749 return false;
750}
751
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752#endif /* CONFIG_SMP */
753
Matt Domschc988d2b2005-06-23 22:05:15 -0700754#define MODINFO_ATTR(field) \
755static void setup_modinfo_##field(struct module *mod, const char *s) \
756{ \
757 mod->field = kstrdup(s, GFP_KERNEL); \
758} \
759static ssize_t show_modinfo_##field(struct module_attribute *mattr, \
Kay Sievers4befb022011-07-24 22:06:04 +0930760 struct module_kobject *mk, char *buffer) \
Matt Domschc988d2b2005-06-23 22:05:15 -0700761{ \
Chen Gangcc56ded2013-08-20 15:34:21 +0930762 return scnprintf(buffer, PAGE_SIZE, "%s\n", mk->mod->field); \
Matt Domschc988d2b2005-06-23 22:05:15 -0700763} \
764static int modinfo_##field##_exists(struct module *mod) \
765{ \
766 return mod->field != NULL; \
767} \
768static void free_modinfo_##field(struct module *mod) \
769{ \
Daniel Walker22a8bde2007-10-18 03:06:07 -0700770 kfree(mod->field); \
771 mod->field = NULL; \
Matt Domschc988d2b2005-06-23 22:05:15 -0700772} \
773static struct module_attribute modinfo_##field = { \
Tejun Heo7b595752007-06-14 03:45:17 +0900774 .attr = { .name = __stringify(field), .mode = 0444 }, \
Matt Domschc988d2b2005-06-23 22:05:15 -0700775 .show = show_modinfo_##field, \
776 .setup = setup_modinfo_##field, \
777 .test = modinfo_##field##_exists, \
778 .free = free_modinfo_##field, \
779};
780
781MODINFO_ATTR(version);
782MODINFO_ATTR(srcversion);
783
Arjan van de Vene14af7e2008-01-25 21:08:33 +0100784static char last_unloaded_module[MODULE_NAME_LEN+1];
785
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -0800786#ifdef CONFIG_MODULE_UNLOAD
Steven Rostedteb0c5372010-03-29 14:25:18 -0400787
788EXPORT_TRACEPOINT_SYMBOL(module_get);
789
Masami Hiramatsue513cc12014-11-10 09:30:29 +1030790/* MODULE_REF_BASE is the base reference count by kmodule loader. */
791#define MODULE_REF_BASE 1
792
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793/* Init the unload section of the module. */
Rusty Russell9f85a4b2010-08-05 12:59:04 -0600794static int module_unload_init(struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795{
Masami Hiramatsue513cc12014-11-10 09:30:29 +1030796 /*
797 * Initialize reference counter to MODULE_REF_BASE.
798 * refcnt == 0 means module is going.
799 */
800 atomic_set(&mod->refcnt, MODULE_REF_BASE);
Rusty Russell9f85a4b2010-08-05 12:59:04 -0600801
Linus Torvalds2c02dfe2010-05-31 12:19:37 -0700802 INIT_LIST_HEAD(&mod->source_list);
803 INIT_LIST_HEAD(&mod->target_list);
Christoph Lametere1783a22010-01-05 15:34:50 +0900804
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 /* Hold reference count during initialization. */
Masami Hiramatsue513cc12014-11-10 09:30:29 +1030806 atomic_inc(&mod->refcnt);
Rusty Russell9f85a4b2010-08-05 12:59:04 -0600807
808 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809}
810
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811/* Does a already use b? */
812static int already_uses(struct module *a, struct module *b)
813{
814 struct module_use *use;
815
Linus Torvalds2c02dfe2010-05-31 12:19:37 -0700816 list_for_each_entry(use, &b->source_list, source_list) {
817 if (use->source == a) {
Jim Cromie5e124162011-12-06 12:11:31 -0700818 pr_debug("%s uses %s!\n", a->name, b->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 return 1;
820 }
821 }
Jim Cromie5e124162011-12-06 12:11:31 -0700822 pr_debug("%s does not use %s!\n", a->name, b->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 return 0;
824}
825
Linus Torvalds2c02dfe2010-05-31 12:19:37 -0700826/*
827 * Module a uses b
828 * - we add 'a' as a "source", 'b' as a "target" of module use
829 * - the module_use is added to the list of 'b' sources (so
830 * 'b' can walk the list to see who sourced them), and of 'a'
831 * targets (so 'a' can see what modules it targets).
832 */
833static int add_module_usage(struct module *a, struct module *b)
834{
Linus Torvalds2c02dfe2010-05-31 12:19:37 -0700835 struct module_use *use;
836
Jim Cromie5e124162011-12-06 12:11:31 -0700837 pr_debug("Allocating new usage for %s.\n", a->name);
Linus Torvalds2c02dfe2010-05-31 12:19:37 -0700838 use = kmalloc(sizeof(*use), GFP_ATOMIC);
839 if (!use) {
Andrew Mortonbddb12b2013-11-12 15:11:28 -0800840 pr_warn("%s: out of memory loading\n", a->name);
Linus Torvalds2c02dfe2010-05-31 12:19:37 -0700841 return -ENOMEM;
842 }
843
844 use->source = a;
845 use->target = b;
846 list_add(&use->source_list, &b->source_list);
847 list_add(&use->target_list, &a->target_list);
Linus Torvalds2c02dfe2010-05-31 12:19:37 -0700848 return 0;
849}
850
Rusty Russell75676502010-06-05 11:17:36 -0600851/* Module a uses b: caller needs module_mutex() */
Rusty Russell9bea7f22010-06-05 11:17:37 -0600852int ref_module(struct module *a, struct module *b)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853{
Rusty Russellc8e21ce2010-06-05 11:17:35 -0600854 int err;
Kay Sievers270a6c42007-01-18 13:26:15 +0100855
Rusty Russell9bea7f22010-06-05 11:17:37 -0600856 if (b == NULL || already_uses(a, b))
Linus Torvalds218ce732010-05-25 16:48:30 -0700857 return 0;
Linus Torvalds218ce732010-05-25 16:48:30 -0700858
Rusty Russell9bea7f22010-06-05 11:17:37 -0600859 /* If module isn't available, we fail. */
860 err = strong_try_module_get(b);
Rusty Russellc9a3ba52008-01-29 17:13:18 -0500861 if (err)
Rusty Russell9bea7f22010-06-05 11:17:37 -0600862 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863
Linus Torvalds2c02dfe2010-05-31 12:19:37 -0700864 err = add_module_usage(a, b);
865 if (err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 module_put(b);
Rusty Russell9bea7f22010-06-05 11:17:37 -0600867 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 }
Rusty Russell9bea7f22010-06-05 11:17:37 -0600869 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870}
Rusty Russell9bea7f22010-06-05 11:17:37 -0600871EXPORT_SYMBOL_GPL(ref_module);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872
873/* Clear the unload stuff of the module. */
874static void module_unload_free(struct module *mod)
875{
Linus Torvalds2c02dfe2010-05-31 12:19:37 -0700876 struct module_use *use, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877
Rusty Russell75676502010-06-05 11:17:36 -0600878 mutex_lock(&module_mutex);
Linus Torvalds2c02dfe2010-05-31 12:19:37 -0700879 list_for_each_entry_safe(use, tmp, &mod->target_list, target_list) {
880 struct module *i = use->target;
Jim Cromie5e124162011-12-06 12:11:31 -0700881 pr_debug("%s unusing %s\n", mod->name, i->name);
Linus Torvalds2c02dfe2010-05-31 12:19:37 -0700882 module_put(i);
883 list_del(&use->source_list);
884 list_del(&use->target_list);
885 kfree(use);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 }
Rusty Russell75676502010-06-05 11:17:36 -0600887 mutex_unlock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888}
889
890#ifdef CONFIG_MODULE_FORCE_UNLOAD
Akinobu Mitafb169792006-01-08 01:04:29 -0800891static inline int try_force_unload(unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892{
893 int ret = (flags & O_TRUNC);
894 if (ret)
Rusty Russell373d4d02013-01-21 17:17:39 +1030895 add_taint(TAINT_FORCED_RMMOD, LOCKDEP_NOW_UNRELIABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 return ret;
897}
898#else
Akinobu Mitafb169792006-01-08 01:04:29 -0800899static inline int try_force_unload(unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900{
901 return 0;
902}
903#endif /* CONFIG_MODULE_FORCE_UNLOAD */
904
Masami Hiramatsue513cc12014-11-10 09:30:29 +1030905/* Try to release refcount of module, 0 means success. */
906static int try_release_module_ref(struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907{
Masami Hiramatsue513cc12014-11-10 09:30:29 +1030908 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909
Masami Hiramatsue513cc12014-11-10 09:30:29 +1030910 /* Try to decrement refcnt which we set at loading */
911 ret = atomic_sub_return(MODULE_REF_BASE, &mod->refcnt);
912 BUG_ON(ret < 0);
913 if (ret)
914 /* Someone can put this right now, recover with checking */
915 ret = atomic_add_unless(&mod->refcnt, MODULE_REF_BASE, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916
Masami Hiramatsue513cc12014-11-10 09:30:29 +1030917 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918}
919
920static int try_stop_module(struct module *mod, int flags, int *forced)
921{
Masami Hiramatsue513cc12014-11-10 09:30:29 +1030922 /* If it's not unused, quit unless we're forcing. */
923 if (try_release_module_ref(mod) != 0) {
924 *forced = try_force_unload(flags);
925 if (!(*forced))
926 return -EWOULDBLOCK;
927 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928
Masami Hiramatsue513cc12014-11-10 09:30:29 +1030929 /* Mark it as dying. */
930 mod->state = MODULE_STATE_GOING;
931
932 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933}
934
Rusty Russelld5db1392015-01-22 11:13:14 +1030935/**
936 * module_refcount - return the refcount or -1 if unloading
937 *
938 * @mod: the module we're checking
939 *
940 * Returns:
941 * -1 if the module is in the process of unloading
942 * otherwise the number of references in the kernel to the module
943 */
944int module_refcount(struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945{
Rusty Russelld5db1392015-01-22 11:13:14 +1030946 return atomic_read(&mod->refcnt) - MODULE_REF_BASE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947}
948EXPORT_SYMBOL(module_refcount);
949
950/* This exists whether we can unload or not */
951static void free_module(struct module *mod);
952
Heiko Carstens17da2bd2009-01-14 14:14:10 +0100953SYSCALL_DEFINE2(delete_module, const char __user *, name_user,
954 unsigned int, flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955{
956 struct module *mod;
Greg Kroah-Hartmandfff0a02007-02-23 14:54:57 -0800957 char name[MODULE_NAME_LEN];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 int ret, forced = 0;
959
Kees Cook3d433212009-04-02 15:49:29 -0700960 if (!capable(CAP_SYS_MODULE) || modules_disabled)
Greg Kroah-Hartmandfff0a02007-02-23 14:54:57 -0800961 return -EPERM;
962
963 if (strncpy_from_user(name, name_user, MODULE_NAME_LEN-1) < 0)
964 return -EFAULT;
965 name[MODULE_NAME_LEN-1] = '\0';
966
Richard Guy Briggsf6276ac2017-05-02 10:16:04 -0400967 audit_log_kern_module(name);
968
Tejun Heo3fc1f1e2010-05-06 18:49:20 +0200969 if (mutex_lock_interruptible(&module_mutex) != 0)
970 return -EINTR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971
972 mod = find_module(name);
973 if (!mod) {
974 ret = -ENOENT;
975 goto out;
976 }
977
Linus Torvalds2c02dfe2010-05-31 12:19:37 -0700978 if (!list_empty(&mod->source_list)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 /* Other modules depend on us: get rid of them first. */
980 ret = -EWOULDBLOCK;
981 goto out;
982 }
983
984 /* Doing init or already dying? */
985 if (mod->state != MODULE_STATE_LIVE) {
Rusty Russell3f2b9c92013-09-17 05:48:51 +0930986 /* FIXME: if (force), slam module count damn the torpedoes */
Jim Cromie5e124162011-12-06 12:11:31 -0700987 pr_debug("%s already dying\n", mod->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 ret = -EBUSY;
989 goto out;
990 }
991
992 /* If it has an init func, it must have an exit func to unload */
Rusty Russellaf49d922007-10-16 23:26:27 -0700993 if (mod->init && !mod->exit) {
Akinobu Mitafb169792006-01-08 01:04:29 -0800994 forced = try_force_unload(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 if (!forced) {
996 /* This module can't be removed */
997 ret = -EBUSY;
998 goto out;
999 }
1000 }
1001
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 /* Stop the machine so refcounts can't move and disable module. */
1003 ret = try_stop_module(mod, flags, &forced);
1004 if (ret != 0)
1005 goto out;
1006
Peter Oberparleiterdf4b5652008-04-21 14:34:31 +02001007 mutex_unlock(&module_mutex);
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001008 /* Final destruction now no one is using it. */
Peter Oberparleiterdf4b5652008-04-21 14:34:31 +02001009 if (mod->exit != NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 mod->exit();
Peter Oberparleiterdf4b5652008-04-21 14:34:31 +02001011 blocking_notifier_call_chain(&module_notify_list,
1012 MODULE_STATE_GOING, mod);
Jessica Yu7e545d62016-03-16 20:55:39 -04001013 klp_module_going(mod);
Jessica Yu7dcd1822016-02-16 17:32:33 -05001014 ftrace_release_mod(mod);
1015
Arjan van de Ven22a9d642009-01-07 08:45:46 -08001016 async_synchronize_full();
Rusty Russell75676502010-06-05 11:17:36 -06001017
Arjan van de Vene14af7e2008-01-25 21:08:33 +01001018 /* Store the name of the last unloaded module for diagnostic purposes */
Rusty Russellefa53452008-01-29 17:13:20 -05001019 strlcpy(last_unloaded_module, mod->name, sizeof(last_unloaded_module));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020
Rusty Russell75676502010-06-05 11:17:36 -06001021 free_module(mod);
1022 return 0;
1023out:
Ashutosh Naik6389a382006-03-23 03:00:46 -08001024 mutex_unlock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 return ret;
1026}
1027
Jianjun Kongd1e99d72008-12-08 14:26:29 +08001028static inline void print_unload_info(struct seq_file *m, struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029{
1030 struct module_use *use;
1031 int printed_something = 0;
1032
Rusty Russelld5db1392015-01-22 11:13:14 +10301033 seq_printf(m, " %i ", module_refcount(mod));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034
Ionut Alexa6da0b562014-11-10 09:31:29 +10301035 /*
1036 * Always include a trailing , so userspace can differentiate
1037 * between this and the old multi-field proc format.
1038 */
Linus Torvalds2c02dfe2010-05-31 12:19:37 -07001039 list_for_each_entry(use, &mod->source_list, source_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 printed_something = 1;
Linus Torvalds2c02dfe2010-05-31 12:19:37 -07001041 seq_printf(m, "%s,", use->source->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 }
1043
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 if (mod->init != NULL && mod->exit == NULL) {
1045 printed_something = 1;
Ionut Alexa6da0b562014-11-10 09:31:29 +10301046 seq_puts(m, "[permanent],");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 }
1048
1049 if (!printed_something)
Ionut Alexa6da0b562014-11-10 09:31:29 +10301050 seq_puts(m, "-");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051}
1052
1053void __symbol_put(const char *symbol)
1054{
1055 struct module *owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056
Rusty Russell24da1cb2007-07-15 23:41:46 -07001057 preempt_disable();
Tim Abbott414fd312008-12-05 19:03:56 -05001058 if (!find_symbol(symbol, &owner, NULL, true, false))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 BUG();
1060 module_put(owner);
Rusty Russell24da1cb2007-07-15 23:41:46 -07001061 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062}
1063EXPORT_SYMBOL(__symbol_put);
1064
Rusty Russell7d1d16e2009-08-26 22:02:54 +09301065/* Note this assumes addr is a function, which it currently always is. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066void symbol_put_addr(void *addr)
1067{
Trent Piepho5e376612006-05-15 09:44:06 -07001068 struct module *modaddr;
Rusty Russell7d1d16e2009-08-26 22:02:54 +09301069 unsigned long a = (unsigned long)dereference_function_descriptor(addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070
Rusty Russell7d1d16e2009-08-26 22:02:54 +09301071 if (core_kernel_text(a))
Trent Piepho5e376612006-05-15 09:44:06 -07001072 return;
1073
Peter Zijlstra275d7d42015-08-20 10:34:59 +09301074 /*
1075 * Even though we hold a reference on the module; we still need to
1076 * disable preemption in order to safely traverse the data structure.
1077 */
1078 preempt_disable();
Rusty Russell7d1d16e2009-08-26 22:02:54 +09301079 modaddr = __module_text_address(a);
Rusty Russella6e6abd2009-03-31 13:05:31 -06001080 BUG_ON(!modaddr);
Trent Piepho5e376612006-05-15 09:44:06 -07001081 module_put(modaddr);
Peter Zijlstra275d7d42015-08-20 10:34:59 +09301082 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083}
1084EXPORT_SYMBOL_GPL(symbol_put_addr);
1085
1086static ssize_t show_refcnt(struct module_attribute *mattr,
Kay Sievers4befb022011-07-24 22:06:04 +09301087 struct module_kobject *mk, char *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088{
Rusty Russelld5db1392015-01-22 11:13:14 +10301089 return sprintf(buffer, "%i\n", module_refcount(mk->mod));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090}
1091
Kay Sieverscca3e702012-01-13 09:32:15 +10301092static struct module_attribute modinfo_refcnt =
1093 __ATTR(refcnt, 0444, show_refcnt, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094
Steven Rostedtd53799b2012-03-26 12:50:52 +10301095void __module_get(struct module *module)
1096{
1097 if (module) {
1098 preempt_disable();
Masami Hiramatsu2f35c412014-11-10 09:29:29 +10301099 atomic_inc(&module->refcnt);
Steven Rostedtd53799b2012-03-26 12:50:52 +10301100 trace_module_get(module, _RET_IP_);
1101 preempt_enable();
1102 }
1103}
1104EXPORT_SYMBOL(__module_get);
1105
1106bool try_module_get(struct module *module)
1107{
1108 bool ret = true;
1109
1110 if (module) {
1111 preempt_disable();
Masami Hiramatsue513cc12014-11-10 09:30:29 +10301112 /* Note: here, we can fail to get a reference */
1113 if (likely(module_is_live(module) &&
1114 atomic_inc_not_zero(&module->refcnt) != 0))
Steven Rostedtd53799b2012-03-26 12:50:52 +10301115 trace_module_get(module, _RET_IP_);
Masami Hiramatsue513cc12014-11-10 09:30:29 +10301116 else
Steven Rostedtd53799b2012-03-26 12:50:52 +10301117 ret = false;
1118
1119 preempt_enable();
1120 }
1121 return ret;
1122}
1123EXPORT_SYMBOL(try_module_get);
1124
Al Virof6a57032006-10-18 01:47:25 -04001125void module_put(struct module *module)
1126{
Masami Hiramatsue513cc12014-11-10 09:30:29 +10301127 int ret;
1128
Al Virof6a57032006-10-18 01:47:25 -04001129 if (module) {
Christoph Lametere1783a22010-01-05 15:34:50 +09001130 preempt_disable();
Masami Hiramatsue513cc12014-11-10 09:30:29 +10301131 ret = atomic_dec_if_positive(&module->refcnt);
1132 WARN_ON(ret < 0); /* Failed to put refcount */
Li Zefanae832d12010-03-24 10:57:43 +08001133 trace_module_put(module, _RET_IP_);
Christoph Lametere1783a22010-01-05 15:34:50 +09001134 preempt_enable();
Al Virof6a57032006-10-18 01:47:25 -04001135 }
1136}
1137EXPORT_SYMBOL(module_put);
1138
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139#else /* !CONFIG_MODULE_UNLOAD */
Jianjun Kongd1e99d72008-12-08 14:26:29 +08001140static inline void print_unload_info(struct seq_file *m, struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141{
1142 /* We don't know the usage count, or what modules are using. */
Ionut Alexa6da0b562014-11-10 09:31:29 +10301143 seq_puts(m, " - -");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144}
1145
1146static inline void module_unload_free(struct module *mod)
1147{
1148}
1149
Rusty Russell9bea7f22010-06-05 11:17:37 -06001150int ref_module(struct module *a, struct module *b)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151{
Rusty Russell9bea7f22010-06-05 11:17:37 -06001152 return strong_try_module_get(b);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153}
Rusty Russell9bea7f22010-06-05 11:17:37 -06001154EXPORT_SYMBOL_GPL(ref_module);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155
Rusty Russell9f85a4b2010-08-05 12:59:04 -06001156static inline int module_unload_init(struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157{
Rusty Russell9f85a4b2010-08-05 12:59:04 -06001158 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159}
1160#endif /* CONFIG_MODULE_UNLOAD */
1161
Kevin Winchester53999bf2012-01-15 19:32:55 -04001162static size_t module_flags_taint(struct module *mod, char *buf)
1163{
1164 size_t l = 0;
Petr Mladek7fd83292016-09-21 13:47:22 +02001165 int i;
Kevin Winchester53999bf2012-01-15 19:32:55 -04001166
Petr Mladek7fd83292016-09-21 13:47:22 +02001167 for (i = 0; i < TAINT_FLAGS_COUNT; i++) {
1168 if (taint_flags[i].module && test_bit(i, &mod->taints))
Larry Finger5eb7c0d2017-01-01 20:25:25 -06001169 buf[l++] = taint_flags[i].c_true;
Petr Mladek7fd83292016-09-21 13:47:22 +02001170 }
1171
Kevin Winchester53999bf2012-01-15 19:32:55 -04001172 return l;
1173}
1174
Kay Sievers1f717402006-11-24 12:15:25 +01001175static ssize_t show_initstate(struct module_attribute *mattr,
Kay Sievers4befb022011-07-24 22:06:04 +09301176 struct module_kobject *mk, char *buffer)
Kay Sievers1f717402006-11-24 12:15:25 +01001177{
1178 const char *state = "unknown";
1179
Kay Sievers4befb022011-07-24 22:06:04 +09301180 switch (mk->mod->state) {
Kay Sievers1f717402006-11-24 12:15:25 +01001181 case MODULE_STATE_LIVE:
1182 state = "live";
1183 break;
1184 case MODULE_STATE_COMING:
1185 state = "coming";
1186 break;
1187 case MODULE_STATE_GOING:
1188 state = "going";
1189 break;
Rusty Russell0d21b0e2013-01-12 11:38:44 +10301190 default:
1191 BUG();
Kay Sievers1f717402006-11-24 12:15:25 +01001192 }
1193 return sprintf(buffer, "%s\n", state);
1194}
1195
Kay Sieverscca3e702012-01-13 09:32:15 +10301196static struct module_attribute modinfo_initstate =
1197 __ATTR(initstate, 0444, show_initstate, NULL);
Kay Sievers1f717402006-11-24 12:15:25 +01001198
Kay Sievers88bfa322011-07-24 22:06:04 +09301199static ssize_t store_uevent(struct module_attribute *mattr,
1200 struct module_kobject *mk,
1201 const char *buffer, size_t count)
1202{
Peter Rajnohaf36776f2017-05-09 15:22:30 +02001203 kobject_synth_uevent(&mk->kobj, buffer, count);
Kay Sievers88bfa322011-07-24 22:06:04 +09301204 return count;
1205}
1206
Kay Sieverscca3e702012-01-13 09:32:15 +10301207struct module_attribute module_uevent =
1208 __ATTR(uevent, 0200, NULL, store_uevent);
1209
1210static ssize_t show_coresize(struct module_attribute *mattr,
1211 struct module_kobject *mk, char *buffer)
1212{
Rusty Russell7523e4d2015-11-26 09:44:08 +10301213 return sprintf(buffer, "%u\n", mk->mod->core_layout.size);
Kay Sieverscca3e702012-01-13 09:32:15 +10301214}
1215
1216static struct module_attribute modinfo_coresize =
1217 __ATTR(coresize, 0444, show_coresize, NULL);
1218
1219static ssize_t show_initsize(struct module_attribute *mattr,
1220 struct module_kobject *mk, char *buffer)
1221{
Rusty Russell7523e4d2015-11-26 09:44:08 +10301222 return sprintf(buffer, "%u\n", mk->mod->init_layout.size);
Kay Sieverscca3e702012-01-13 09:32:15 +10301223}
1224
1225static struct module_attribute modinfo_initsize =
1226 __ATTR(initsize, 0444, show_initsize, NULL);
1227
1228static ssize_t show_taint(struct module_attribute *mattr,
1229 struct module_kobject *mk, char *buffer)
1230{
1231 size_t l;
1232
1233 l = module_flags_taint(mk->mod, buffer);
1234 buffer[l++] = '\n';
1235 return l;
1236}
1237
1238static struct module_attribute modinfo_taint =
1239 __ATTR(taint, 0444, show_taint, NULL);
Kay Sievers88bfa322011-07-24 22:06:04 +09301240
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001241static struct module_attribute *modinfo_attrs[] = {
Kay Sieverscca3e702012-01-13 09:32:15 +10301242 &module_uevent,
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001243 &modinfo_version,
1244 &modinfo_srcversion,
Kay Sieverscca3e702012-01-13 09:32:15 +10301245 &modinfo_initstate,
1246 &modinfo_coresize,
1247 &modinfo_initsize,
1248 &modinfo_taint,
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001249#ifdef CONFIG_MODULE_UNLOAD
Kay Sieverscca3e702012-01-13 09:32:15 +10301250 &modinfo_refcnt,
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001251#endif
1252 NULL,
1253};
1254
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255static const char vermagic[] = VERMAGIC_STRING;
1256
Rusty Russellc6e665c2009-03-31 13:05:33 -06001257static int try_to_force_load(struct module *mod, const char *reason)
Linus Torvalds826e4502008-05-04 17:04:16 -07001258{
1259#ifdef CONFIG_MODULE_FORCE_LOAD
Andi Kleen25ddbb12008-10-15 22:01:41 -07001260 if (!test_taint(TAINT_FORCED_MODULE))
Andrew Mortonbddb12b2013-11-12 15:11:28 -08001261 pr_warn("%s: %s: kernel tainted.\n", mod->name, reason);
Rusty Russell373d4d02013-01-21 17:17:39 +10301262 add_taint_module(mod, TAINT_FORCED_MODULE, LOCKDEP_NOW_UNRELIABLE);
Linus Torvalds826e4502008-05-04 17:04:16 -07001263 return 0;
1264#else
1265 return -ENOEXEC;
1266#endif
1267}
1268
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269#ifdef CONFIG_MODVERSIONS
Ard Biesheuvel71810db2017-02-03 09:54:06 +00001270
1271static u32 resolve_rel_crc(const s32 *crc)
Rusty Russelld4703ae2009-12-15 16:28:32 -06001272{
Ard Biesheuvel71810db2017-02-03 09:54:06 +00001273 return *(u32 *)((void *)crc + *crc);
Rusty Russelld4703ae2009-12-15 16:28:32 -06001274}
1275
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276static int check_version(Elf_Shdr *sechdrs,
1277 unsigned int versindex,
1278 const char *symname,
Ionut Alexa6da0b562014-11-10 09:31:29 +10301279 struct module *mod,
Ard Biesheuvel71810db2017-02-03 09:54:06 +00001280 const s32 *crc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281{
1282 unsigned int i, num_versions;
1283 struct modversion_info *versions;
1284
1285 /* Exporting module didn't supply crcs? OK, we're already tainted. */
1286 if (!crc)
1287 return 1;
1288
Rusty Russella5dd6972008-05-09 16:24:21 +10001289 /* No versions at all? modprobe --force does this. */
1290 if (versindex == 0)
1291 return try_to_force_load(mod, symname) == 0;
1292
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 versions = (void *) sechdrs[versindex].sh_addr;
1294 num_versions = sechdrs[versindex].sh_size
1295 / sizeof(struct modversion_info);
1296
1297 for (i = 0; i < num_versions; i++) {
Ard Biesheuvel71810db2017-02-03 09:54:06 +00001298 u32 crcval;
1299
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300 if (strcmp(versions[i].name, symname) != 0)
1301 continue;
1302
Ard Biesheuvel71810db2017-02-03 09:54:06 +00001303 if (IS_ENABLED(CONFIG_MODULE_REL_CRCS))
1304 crcval = resolve_rel_crc(crc);
1305 else
1306 crcval = *crc;
1307 if (versions[i].crc == crcval)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308 return 1;
Ard Biesheuvel71810db2017-02-03 09:54:06 +00001309 pr_debug("Found checksum %X vs module %lX\n",
1310 crcval, versions[i].crc);
Linus Torvalds826e4502008-05-04 17:04:16 -07001311 goto bad_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312 }
Linus Torvalds826e4502008-05-04 17:04:16 -07001313
Linus Torvaldsfaaae2a2016-11-29 15:20:14 -08001314 /* Broken toolchain. Warn once, then let it go.. */
1315 pr_warn_once("%s: no symbol version for %s\n", mod->name, symname);
1316 return 1;
Linus Torvalds826e4502008-05-04 17:04:16 -07001317
1318bad_version:
Ionut Alexa6da0b562014-11-10 09:31:29 +10301319 pr_warn("%s: disagrees about version of symbol %s\n",
Linus Torvalds826e4502008-05-04 17:04:16 -07001320 mod->name, symname);
1321 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322}
1323
1324static inline int check_modstruct_version(Elf_Shdr *sechdrs,
1325 unsigned int versindex,
1326 struct module *mod)
1327{
Ard Biesheuvel71810db2017-02-03 09:54:06 +00001328 const s32 *crc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329
Peter Zijlstra926a59b2015-05-27 11:09:35 +09301330 /*
1331 * Since this should be found in kernel (which can't be removed), no
1332 * locking is necessary -- use preempt_disable() to placate lockdep.
1333 */
1334 preempt_disable();
Rusty Russellb92021b2013-03-15 15:04:17 +10301335 if (!find_symbol(VMLINUX_SYMBOL_STR(module_layout), NULL,
Peter Zijlstra926a59b2015-05-27 11:09:35 +09301336 &crc, true, false)) {
1337 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338 BUG();
Peter Zijlstra926a59b2015-05-27 11:09:35 +09301339 }
1340 preempt_enable();
James Hogana4b6a772013-03-18 19:38:56 +10301341 return check_version(sechdrs, versindex,
Ard Biesheuvel71810db2017-02-03 09:54:06 +00001342 VMLINUX_SYMBOL_STR(module_layout), mod, crc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343}
1344
Rusty Russell91e37a72008-05-09 16:25:28 +10001345/* First part is kernel version, which we ignore if module has crcs. */
1346static inline int same_magic(const char *amagic, const char *bmagic,
1347 bool has_crcs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348{
Rusty Russell91e37a72008-05-09 16:25:28 +10001349 if (has_crcs) {
1350 amagic += strcspn(amagic, " ");
1351 bmagic += strcspn(bmagic, " ");
1352 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353 return strcmp(amagic, bmagic) == 0;
1354}
1355#else
1356static inline int check_version(Elf_Shdr *sechdrs,
1357 unsigned int versindex,
1358 const char *symname,
Ionut Alexa6da0b562014-11-10 09:31:29 +10301359 struct module *mod,
Ard Biesheuvel71810db2017-02-03 09:54:06 +00001360 const s32 *crc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361{
1362 return 1;
1363}
1364
1365static inline int check_modstruct_version(Elf_Shdr *sechdrs,
1366 unsigned int versindex,
1367 struct module *mod)
1368{
1369 return 1;
1370}
1371
Rusty Russell91e37a72008-05-09 16:25:28 +10001372static inline int same_magic(const char *amagic, const char *bmagic,
1373 bool has_crcs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374{
1375 return strcmp(amagic, bmagic) == 0;
1376}
1377#endif /* CONFIG_MODVERSIONS */
1378
Rusty Russell75676502010-06-05 11:17:36 -06001379/* Resolve a symbol for this module. I.e. if we find one, record usage. */
Rusty Russell49668682010-08-05 12:59:10 -06001380static const struct kernel_symbol *resolve_symbol(struct module *mod,
1381 const struct load_info *info,
Tim Abbott414fd312008-12-05 19:03:56 -05001382 const char *name,
Rusty Russell9bea7f22010-06-05 11:17:37 -06001383 char ownername[])
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384{
1385 struct module *owner;
Tim Abbott414fd312008-12-05 19:03:56 -05001386 const struct kernel_symbol *sym;
Ard Biesheuvel71810db2017-02-03 09:54:06 +00001387 const s32 *crc;
Rusty Russell9bea7f22010-06-05 11:17:37 -06001388 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389
Peter Zijlstrad64810f2015-02-11 15:01:13 +10301390 /*
1391 * The module_mutex should not be a heavily contended lock;
1392 * if we get the occasional sleep here, we'll go an extra iteration
1393 * in the wait_event_interruptible(), which is harmless.
1394 */
1395 sched_annotate_sleep();
Rusty Russell75676502010-06-05 11:17:36 -06001396 mutex_lock(&module_mutex);
Tim Abbott414fd312008-12-05 19:03:56 -05001397 sym = find_symbol(name, &owner, &crc,
Andi Kleen25ddbb12008-10-15 22:01:41 -07001398 !(mod->taints & (1 << TAINT_PROPRIETARY_MODULE)), true);
Rusty Russell9bea7f22010-06-05 11:17:37 -06001399 if (!sym)
1400 goto unlock;
1401
Ard Biesheuvel71810db2017-02-03 09:54:06 +00001402 if (!check_version(info->sechdrs, info->index.vers, name, mod, crc)) {
Rusty Russell9bea7f22010-06-05 11:17:37 -06001403 sym = ERR_PTR(-EINVAL);
1404 goto getname;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405 }
Rusty Russell9bea7f22010-06-05 11:17:37 -06001406
1407 err = ref_module(mod, owner);
1408 if (err) {
1409 sym = ERR_PTR(err);
1410 goto getname;
1411 }
1412
1413getname:
1414 /* We must make copy under the lock if we failed to get ref. */
1415 strncpy(ownername, module_name(owner), MODULE_NAME_LEN);
1416unlock:
Rusty Russell75676502010-06-05 11:17:36 -06001417 mutex_unlock(&module_mutex);
Linus Torvalds218ce732010-05-25 16:48:30 -07001418 return sym;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419}
1420
Rusty Russell49668682010-08-05 12:59:10 -06001421static const struct kernel_symbol *
1422resolve_symbol_wait(struct module *mod,
1423 const struct load_info *info,
1424 const char *name)
Rusty Russell9bea7f22010-06-05 11:17:37 -06001425{
1426 const struct kernel_symbol *ksym;
Rusty Russell49668682010-08-05 12:59:10 -06001427 char owner[MODULE_NAME_LEN];
Rusty Russell9bea7f22010-06-05 11:17:37 -06001428
1429 if (wait_event_interruptible_timeout(module_wq,
Rusty Russell49668682010-08-05 12:59:10 -06001430 !IS_ERR(ksym = resolve_symbol(mod, info, name, owner))
1431 || PTR_ERR(ksym) != -EBUSY,
Rusty Russell9bea7f22010-06-05 11:17:37 -06001432 30 * HZ) <= 0) {
Andrew Mortonbddb12b2013-11-12 15:11:28 -08001433 pr_warn("%s: gave up waiting for init of module %s.\n",
1434 mod->name, owner);
Rusty Russell9bea7f22010-06-05 11:17:37 -06001435 }
1436 return ksym;
1437}
1438
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439/*
1440 * /sys/module/foo/sections stuff
1441 * J. Corbet <corbet@lwn.net>
1442 */
Rusty Russell8f6d0372010-08-05 12:59:09 -06001443#ifdef CONFIG_SYSFS
Ben Hutchings10b465a2009-12-19 14:43:01 +00001444
Rusty Russell8f6d0372010-08-05 12:59:09 -06001445#ifdef CONFIG_KALLSYMS
Ben Hutchings10b465a2009-12-19 14:43:01 +00001446static inline bool sect_empty(const Elf_Shdr *sect)
1447{
1448 return !(sect->sh_flags & SHF_ALLOC) || sect->sh_size == 0;
1449}
1450
Ionut Alexa6da0b562014-11-10 09:31:29 +10301451struct module_sect_attr {
Rusty Russella58730c2008-03-13 09:03:44 +00001452 struct module_attribute mattr;
1453 char *name;
1454 unsigned long address;
1455};
1456
Ionut Alexa6da0b562014-11-10 09:31:29 +10301457struct module_sect_attrs {
Rusty Russella58730c2008-03-13 09:03:44 +00001458 struct attribute_group grp;
1459 unsigned int nsections;
1460 struct module_sect_attr attrs[0];
1461};
1462
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463static ssize_t module_sect_show(struct module_attribute *mattr,
Kay Sievers4befb022011-07-24 22:06:04 +09301464 struct module_kobject *mk, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465{
1466 struct module_sect_attr *sattr =
1467 container_of(mattr, struct module_sect_attr, mattr);
Kees Cook9f36e2c2011-03-22 16:34:22 -07001468 return sprintf(buf, "0x%pK\n", (void *)sattr->address);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469}
1470
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001471static void free_sect_attrs(struct module_sect_attrs *sect_attrs)
1472{
Rusty Russella58730c2008-03-13 09:03:44 +00001473 unsigned int section;
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001474
1475 for (section = 0; section < sect_attrs->nsections; section++)
1476 kfree(sect_attrs->attrs[section].name);
1477 kfree(sect_attrs);
1478}
1479
Rusty Russell8f6d0372010-08-05 12:59:09 -06001480static void add_sect_attrs(struct module *mod, const struct load_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481{
1482 unsigned int nloaded = 0, i, size[2];
1483 struct module_sect_attrs *sect_attrs;
1484 struct module_sect_attr *sattr;
1485 struct attribute **gattr;
Daniel Walker22a8bde2007-10-18 03:06:07 -07001486
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487 /* Count loaded sections and allocate structures */
Rusty Russell8f6d0372010-08-05 12:59:09 -06001488 for (i = 0; i < info->hdr->e_shnum; i++)
1489 if (!sect_empty(&info->sechdrs[i]))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490 nloaded++;
1491 size[0] = ALIGN(sizeof(*sect_attrs)
1492 + nloaded * sizeof(sect_attrs->attrs[0]),
1493 sizeof(sect_attrs->grp.attrs[0]));
1494 size[1] = (nloaded + 1) * sizeof(sect_attrs->grp.attrs[0]);
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001495 sect_attrs = kzalloc(size[0] + size[1], GFP_KERNEL);
1496 if (sect_attrs == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497 return;
1498
1499 /* Setup section attributes. */
1500 sect_attrs->grp.name = "sections";
1501 sect_attrs->grp.attrs = (void *)sect_attrs + size[0];
1502
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001503 sect_attrs->nsections = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504 sattr = &sect_attrs->attrs[0];
1505 gattr = &sect_attrs->grp.attrs[0];
Rusty Russell8f6d0372010-08-05 12:59:09 -06001506 for (i = 0; i < info->hdr->e_shnum; i++) {
1507 Elf_Shdr *sec = &info->sechdrs[i];
1508 if (sect_empty(sec))
Helge Deller35dead42009-12-03 00:29:15 +01001509 continue;
Rusty Russell8f6d0372010-08-05 12:59:09 -06001510 sattr->address = sec->sh_addr;
1511 sattr->name = kstrdup(info->secstrings + sec->sh_name,
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001512 GFP_KERNEL);
1513 if (sattr->name == NULL)
1514 goto out;
1515 sect_attrs->nsections++;
Eric W. Biederman361795b2010-02-12 13:41:56 -08001516 sysfs_attr_init(&sattr->mattr.attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517 sattr->mattr.show = module_sect_show;
1518 sattr->mattr.store = NULL;
1519 sattr->mattr.attr.name = sattr->name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520 sattr->mattr.attr.mode = S_IRUGO;
1521 *(gattr++) = &(sattr++)->mattr.attr;
1522 }
1523 *gattr = NULL;
1524
1525 if (sysfs_create_group(&mod->mkobj.kobj, &sect_attrs->grp))
1526 goto out;
1527
1528 mod->sect_attrs = sect_attrs;
1529 return;
1530 out:
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001531 free_sect_attrs(sect_attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532}
1533
1534static void remove_sect_attrs(struct module *mod)
1535{
1536 if (mod->sect_attrs) {
1537 sysfs_remove_group(&mod->mkobj.kobj,
1538 &mod->sect_attrs->grp);
1539 /* We are positive that no one is using any sect attrs
1540 * at this point. Deallocate immediately. */
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001541 free_sect_attrs(mod->sect_attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542 mod->sect_attrs = NULL;
1543 }
1544}
1545
Roland McGrath6d760132007-10-16 23:26:40 -07001546/*
1547 * /sys/module/foo/notes/.section.name gives contents of SHT_NOTE sections.
1548 */
1549
1550struct module_notes_attrs {
1551 struct kobject *dir;
1552 unsigned int notes;
1553 struct bin_attribute attrs[0];
1554};
1555
Chris Wright2c3c8be2010-05-12 18:28:57 -07001556static ssize_t module_notes_read(struct file *filp, struct kobject *kobj,
Roland McGrath6d760132007-10-16 23:26:40 -07001557 struct bin_attribute *bin_attr,
1558 char *buf, loff_t pos, size_t count)
1559{
1560 /*
1561 * The caller checked the pos and count against our size.
1562 */
1563 memcpy(buf, bin_attr->private + pos, count);
1564 return count;
1565}
1566
1567static void free_notes_attrs(struct module_notes_attrs *notes_attrs,
1568 unsigned int i)
1569{
1570 if (notes_attrs->dir) {
1571 while (i-- > 0)
1572 sysfs_remove_bin_file(notes_attrs->dir,
1573 &notes_attrs->attrs[i]);
Alexey Dobriyane9432092008-09-23 23:51:11 +04001574 kobject_put(notes_attrs->dir);
Roland McGrath6d760132007-10-16 23:26:40 -07001575 }
1576 kfree(notes_attrs);
1577}
1578
Rusty Russell8f6d0372010-08-05 12:59:09 -06001579static void add_notes_attrs(struct module *mod, const struct load_info *info)
Roland McGrath6d760132007-10-16 23:26:40 -07001580{
1581 unsigned int notes, loaded, i;
1582 struct module_notes_attrs *notes_attrs;
1583 struct bin_attribute *nattr;
1584
Ingo Molnarea6bff32009-08-28 10:44:56 +02001585 /* failed to create section attributes, so can't create notes */
1586 if (!mod->sect_attrs)
1587 return;
1588
Roland McGrath6d760132007-10-16 23:26:40 -07001589 /* Count notes sections and allocate structures. */
1590 notes = 0;
Rusty Russell8f6d0372010-08-05 12:59:09 -06001591 for (i = 0; i < info->hdr->e_shnum; i++)
1592 if (!sect_empty(&info->sechdrs[i]) &&
1593 (info->sechdrs[i].sh_type == SHT_NOTE))
Roland McGrath6d760132007-10-16 23:26:40 -07001594 ++notes;
1595
1596 if (notes == 0)
1597 return;
1598
1599 notes_attrs = kzalloc(sizeof(*notes_attrs)
1600 + notes * sizeof(notes_attrs->attrs[0]),
1601 GFP_KERNEL);
1602 if (notes_attrs == NULL)
1603 return;
1604
1605 notes_attrs->notes = notes;
1606 nattr = &notes_attrs->attrs[0];
Rusty Russell8f6d0372010-08-05 12:59:09 -06001607 for (loaded = i = 0; i < info->hdr->e_shnum; ++i) {
1608 if (sect_empty(&info->sechdrs[i]))
Roland McGrath6d760132007-10-16 23:26:40 -07001609 continue;
Rusty Russell8f6d0372010-08-05 12:59:09 -06001610 if (info->sechdrs[i].sh_type == SHT_NOTE) {
Eric W. Biederman361795b2010-02-12 13:41:56 -08001611 sysfs_bin_attr_init(nattr);
Roland McGrath6d760132007-10-16 23:26:40 -07001612 nattr->attr.name = mod->sect_attrs->attrs[loaded].name;
1613 nattr->attr.mode = S_IRUGO;
Rusty Russell8f6d0372010-08-05 12:59:09 -06001614 nattr->size = info->sechdrs[i].sh_size;
1615 nattr->private = (void *) info->sechdrs[i].sh_addr;
Roland McGrath6d760132007-10-16 23:26:40 -07001616 nattr->read = module_notes_read;
1617 ++nattr;
1618 }
1619 ++loaded;
1620 }
1621
Greg Kroah-Hartman4ff6abf2007-11-05 22:24:43 -08001622 notes_attrs->dir = kobject_create_and_add("notes", &mod->mkobj.kobj);
Roland McGrath6d760132007-10-16 23:26:40 -07001623 if (!notes_attrs->dir)
1624 goto out;
1625
1626 for (i = 0; i < notes; ++i)
1627 if (sysfs_create_bin_file(notes_attrs->dir,
1628 &notes_attrs->attrs[i]))
1629 goto out;
1630
1631 mod->notes_attrs = notes_attrs;
1632 return;
1633
1634 out:
1635 free_notes_attrs(notes_attrs, i);
1636}
1637
1638static void remove_notes_attrs(struct module *mod)
1639{
1640 if (mod->notes_attrs)
1641 free_notes_attrs(mod->notes_attrs, mod->notes_attrs->notes);
1642}
1643
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644#else
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001645
Rusty Russell8f6d0372010-08-05 12:59:09 -06001646static inline void add_sect_attrs(struct module *mod,
1647 const struct load_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648{
1649}
1650
1651static inline void remove_sect_attrs(struct module *mod)
1652{
1653}
Roland McGrath6d760132007-10-16 23:26:40 -07001654
Rusty Russell8f6d0372010-08-05 12:59:09 -06001655static inline void add_notes_attrs(struct module *mod,
1656 const struct load_info *info)
Roland McGrath6d760132007-10-16 23:26:40 -07001657{
1658}
1659
1660static inline void remove_notes_attrs(struct module *mod)
1661{
1662}
Rusty Russell8f6d0372010-08-05 12:59:09 -06001663#endif /* CONFIG_KALLSYMS */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664
Rusty Russell80a3d1b2010-06-05 11:17:36 -06001665static void add_usage_links(struct module *mod)
1666{
1667#ifdef CONFIG_MODULE_UNLOAD
1668 struct module_use *use;
1669 int nowarn;
1670
Rusty Russell75676502010-06-05 11:17:36 -06001671 mutex_lock(&module_mutex);
Rusty Russell80a3d1b2010-06-05 11:17:36 -06001672 list_for_each_entry(use, &mod->target_list, target_list) {
1673 nowarn = sysfs_create_link(use->target->holders_dir,
1674 &mod->mkobj.kobj, mod->name);
1675 }
Rusty Russell75676502010-06-05 11:17:36 -06001676 mutex_unlock(&module_mutex);
Rusty Russell80a3d1b2010-06-05 11:17:36 -06001677#endif
1678}
1679
1680static void del_usage_links(struct module *mod)
1681{
1682#ifdef CONFIG_MODULE_UNLOAD
1683 struct module_use *use;
1684
Rusty Russell75676502010-06-05 11:17:36 -06001685 mutex_lock(&module_mutex);
Rusty Russell80a3d1b2010-06-05 11:17:36 -06001686 list_for_each_entry(use, &mod->target_list, target_list)
1687 sysfs_remove_link(use->target->holders_dir, mod->name);
Rusty Russell75676502010-06-05 11:17:36 -06001688 mutex_unlock(&module_mutex);
Rusty Russell80a3d1b2010-06-05 11:17:36 -06001689#endif
1690}
1691
Rusty Russell6407ebb22010-06-05 11:17:36 -06001692static int module_add_modinfo_attrs(struct module *mod)
Matt Domschc988d2b2005-06-23 22:05:15 -07001693{
1694 struct module_attribute *attr;
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001695 struct module_attribute *temp_attr;
Matt Domschc988d2b2005-06-23 22:05:15 -07001696 int error = 0;
1697 int i;
1698
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001699 mod->modinfo_attrs = kzalloc((sizeof(struct module_attribute) *
1700 (ARRAY_SIZE(modinfo_attrs) + 1)),
1701 GFP_KERNEL);
1702 if (!mod->modinfo_attrs)
1703 return -ENOMEM;
1704
1705 temp_attr = mod->modinfo_attrs;
Matt Domschc988d2b2005-06-23 22:05:15 -07001706 for (i = 0; (attr = modinfo_attrs[i]) && !error; i++) {
Rusty Russellc75b5902016-04-12 05:03:09 +09301707 if (!attr->test || attr->test(mod)) {
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001708 memcpy(temp_attr, attr, sizeof(*temp_attr));
Eric W. Biederman361795b2010-02-12 13:41:56 -08001709 sysfs_attr_init(&temp_attr->attr);
Ionut Alexa6da0b562014-11-10 09:31:29 +10301710 error = sysfs_create_file(&mod->mkobj.kobj,
1711 &temp_attr->attr);
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001712 ++temp_attr;
1713 }
Matt Domschc988d2b2005-06-23 22:05:15 -07001714 }
1715 return error;
1716}
1717
Rusty Russell6407ebb22010-06-05 11:17:36 -06001718static void module_remove_modinfo_attrs(struct module *mod)
Matt Domschc988d2b2005-06-23 22:05:15 -07001719{
1720 struct module_attribute *attr;
1721 int i;
1722
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001723 for (i = 0; (attr = &mod->modinfo_attrs[i]); i++) {
1724 /* pick a field to test for end of list */
1725 if (!attr->attr.name)
1726 break;
Ionut Alexa6da0b562014-11-10 09:31:29 +10301727 sysfs_remove_file(&mod->mkobj.kobj, &attr->attr);
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001728 if (attr->free)
1729 attr->free(mod);
Matt Domschc988d2b2005-06-23 22:05:15 -07001730 }
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001731 kfree(mod->modinfo_attrs);
Matt Domschc988d2b2005-06-23 22:05:15 -07001732}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733
Li Zhong942e4432013-09-03 16:33:57 +09301734static void mod_kobject_put(struct module *mod)
1735{
1736 DECLARE_COMPLETION_ONSTACK(c);
1737 mod->mkobj.kobj_completion = &c;
1738 kobject_put(&mod->mkobj.kobj);
1739 wait_for_completion(&c);
1740}
1741
Rusty Russell6407ebb22010-06-05 11:17:36 -06001742static int mod_sysfs_init(struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743{
1744 int err;
Greg Kroah-Hartman6494a932008-01-27 15:38:40 -08001745 struct kobject *kobj;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746
Greg Kroah-Hartman823bccf2007-04-13 13:15:19 -07001747 if (!module_sysfs_initialized) {
Andrew Mortonbddb12b2013-11-12 15:11:28 -08001748 pr_err("%s: module sysfs not initialized\n", mod->name);
Ed Swierk1cc5f712006-09-25 16:25:36 -07001749 err = -EINVAL;
1750 goto out;
1751 }
Greg Kroah-Hartman6494a932008-01-27 15:38:40 -08001752
1753 kobj = kset_find_obj(module_kset, mod->name);
1754 if (kobj) {
Andrew Mortonbddb12b2013-11-12 15:11:28 -08001755 pr_err("%s: module is already loaded\n", mod->name);
Greg Kroah-Hartman6494a932008-01-27 15:38:40 -08001756 kobject_put(kobj);
1757 err = -EINVAL;
1758 goto out;
1759 }
1760
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761 mod->mkobj.mod = mod;
Kay Sieverse17e0f52006-11-24 12:15:25 +01001762
Greg Kroah-Hartmanac3c8142007-12-17 23:05:35 -07001763 memset(&mod->mkobj.kobj, 0, sizeof(mod->mkobj.kobj));
1764 mod->mkobj.kobj.kset = module_kset;
1765 err = kobject_init_and_add(&mod->mkobj.kobj, &module_ktype, NULL,
1766 "%s", mod->name);
1767 if (err)
Li Zhong942e4432013-09-03 16:33:57 +09301768 mod_kobject_put(mod);
Kay Sievers270a6c42007-01-18 13:26:15 +01001769
Kay Sievers97c146ef2007-11-29 23:46:11 +01001770 /* delay uevent until full sysfs population */
Kay Sievers270a6c42007-01-18 13:26:15 +01001771out:
1772 return err;
1773}
1774
Rusty Russell6407ebb22010-06-05 11:17:36 -06001775static int mod_sysfs_setup(struct module *mod,
Rusty Russell8f6d0372010-08-05 12:59:09 -06001776 const struct load_info *info,
Kay Sievers270a6c42007-01-18 13:26:15 +01001777 struct kernel_param *kparam,
1778 unsigned int num_params)
1779{
1780 int err;
1781
Rusty Russell80a3d1b2010-06-05 11:17:36 -06001782 err = mod_sysfs_init(mod);
1783 if (err)
1784 goto out;
1785
Greg Kroah-Hartman4ff6abf2007-11-05 22:24:43 -08001786 mod->holders_dir = kobject_create_and_add("holders", &mod->mkobj.kobj);
Akinobu Mita240936e2007-04-26 00:12:09 -07001787 if (!mod->holders_dir) {
1788 err = -ENOMEM;
Kay Sievers270a6c42007-01-18 13:26:15 +01001789 goto out_unreg;
Akinobu Mita240936e2007-04-26 00:12:09 -07001790 }
Kay Sievers270a6c42007-01-18 13:26:15 +01001791
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792 err = module_param_sysfs_setup(mod, kparam, num_params);
1793 if (err)
Kay Sievers270a6c42007-01-18 13:26:15 +01001794 goto out_unreg_holders;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795
Matt Domschc988d2b2005-06-23 22:05:15 -07001796 err = module_add_modinfo_attrs(mod);
1797 if (err)
Kay Sieverse17e0f52006-11-24 12:15:25 +01001798 goto out_unreg_param;
Matt Domschc988d2b2005-06-23 22:05:15 -07001799
Rusty Russell80a3d1b2010-06-05 11:17:36 -06001800 add_usage_links(mod);
Rusty Russell8f6d0372010-08-05 12:59:09 -06001801 add_sect_attrs(mod, info);
1802 add_notes_attrs(mod, info);
Rusty Russell80a3d1b2010-06-05 11:17:36 -06001803
Kay Sieverse17e0f52006-11-24 12:15:25 +01001804 kobject_uevent(&mod->mkobj.kobj, KOBJ_ADD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805 return 0;
1806
Kay Sieverse17e0f52006-11-24 12:15:25 +01001807out_unreg_param:
1808 module_param_sysfs_remove(mod);
Kay Sievers270a6c42007-01-18 13:26:15 +01001809out_unreg_holders:
Greg Kroah-Hartman78a2d902007-12-20 08:13:05 -08001810 kobject_put(mod->holders_dir);
Kay Sievers270a6c42007-01-18 13:26:15 +01001811out_unreg:
Li Zhong942e4432013-09-03 16:33:57 +09301812 mod_kobject_put(mod);
Rusty Russell80a3d1b2010-06-05 11:17:36 -06001813out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814 return err;
1815}
Denis V. Lunev34e4e2f2008-05-20 13:59:48 +04001816
1817static void mod_sysfs_fini(struct module *mod)
1818{
Rusty Russell8f6d0372010-08-05 12:59:09 -06001819 remove_notes_attrs(mod);
1820 remove_sect_attrs(mod);
Li Zhong942e4432013-09-03 16:33:57 +09301821 mod_kobject_put(mod);
Denis V. Lunev34e4e2f2008-05-20 13:59:48 +04001822}
1823
Rusty Russellcf2fde72015-06-26 06:44:38 +09301824static void init_param_lock(struct module *mod)
1825{
1826 mutex_init(&mod->param_lock);
1827}
Rusty Russell8f6d0372010-08-05 12:59:09 -06001828#else /* !CONFIG_SYSFS */
Denis V. Lunev34e4e2f2008-05-20 13:59:48 +04001829
Rusty Russell8f6d0372010-08-05 12:59:09 -06001830static int mod_sysfs_setup(struct module *mod,
1831 const struct load_info *info,
Rusty Russell6407ebb22010-06-05 11:17:36 -06001832 struct kernel_param *kparam,
1833 unsigned int num_params)
1834{
1835 return 0;
1836}
1837
Denis V. Lunev34e4e2f2008-05-20 13:59:48 +04001838static void mod_sysfs_fini(struct module *mod)
1839{
1840}
1841
Rusty Russell36b03602010-08-05 12:59:09 -06001842static void module_remove_modinfo_attrs(struct module *mod)
1843{
1844}
1845
Rusty Russell80a3d1b2010-06-05 11:17:36 -06001846static void del_usage_links(struct module *mod)
1847{
1848}
1849
Rusty Russellcf2fde72015-06-26 06:44:38 +09301850static void init_param_lock(struct module *mod)
1851{
1852}
Denis V. Lunev34e4e2f2008-05-20 13:59:48 +04001853#endif /* CONFIG_SYSFS */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854
Rusty Russell36b03602010-08-05 12:59:09 -06001855static void mod_sysfs_teardown(struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856{
Rusty Russell80a3d1b2010-06-05 11:17:36 -06001857 del_usage_links(mod);
Matt Domschc988d2b2005-06-23 22:05:15 -07001858 module_remove_modinfo_attrs(mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859 module_param_sysfs_remove(mod);
Greg Kroah-Hartman78a2d902007-12-20 08:13:05 -08001860 kobject_put(mod->mkobj.drivers_dir);
1861 kobject_put(mod->holders_dir);
Denis V. Lunev34e4e2f2008-05-20 13:59:48 +04001862 mod_sysfs_fini(mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863}
1864
Laura Abbott0f5bf6d2017-02-06 16:31:58 -08001865#ifdef CONFIG_STRICT_MODULE_RWX
matthieu castet84e1c6b2010-11-16 22:35:16 +01001866/*
1867 * LKM RO/NX protection: protect module's text/ro-data
1868 * from modification and any data from execution.
Rusty Russell85c898d2015-11-26 09:45:08 +10301869 *
1870 * General layout of module is:
Jessica Yu444d13f2016-07-27 12:06:21 +09301871 * [text] [read-only-data] [ro-after-init] [writable data]
1872 * text_size -----^ ^ ^ ^
1873 * ro_size ------------------------| | |
1874 * ro_after_init_size -----------------------------| |
1875 * size -----------------------------------------------------------|
Rusty Russell85c898d2015-11-26 09:45:08 +10301876 *
1877 * These values are always page-aligned (as is base)
matthieu castet84e1c6b2010-11-16 22:35:16 +01001878 */
Rusty Russell85c898d2015-11-26 09:45:08 +10301879static void frob_text(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 set_memory((unsigned long)layout->base,
1885 layout->text_size >> PAGE_SHIFT);
matthieu castet84e1c6b2010-11-16 22:35:16 +01001886}
1887
Rusty Russell85c898d2015-11-26 09:45:08 +10301888static void frob_rodata(const struct module_layout *layout,
1889 int (*set_memory)(unsigned long start, int num_pages))
matthieu castet84e1c6b2010-11-16 22:35:16 +01001890{
Rusty Russell85c898d2015-11-26 09:45:08 +10301891 BUG_ON((unsigned long)layout->base & (PAGE_SIZE-1));
1892 BUG_ON((unsigned long)layout->text_size & (PAGE_SIZE-1));
1893 BUG_ON((unsigned long)layout->ro_size & (PAGE_SIZE-1));
1894 set_memory((unsigned long)layout->base + layout->text_size,
1895 (layout->ro_size - layout->text_size) >> PAGE_SHIFT);
matthieu castet84e1c6b2010-11-16 22:35:16 +01001896}
1897
Jessica Yu444d13f2016-07-27 12:06:21 +09301898static void frob_ro_after_init(const struct module_layout *layout,
1899 int (*set_memory)(unsigned long start, int num_pages))
1900{
1901 BUG_ON((unsigned long)layout->base & (PAGE_SIZE-1));
1902 BUG_ON((unsigned long)layout->ro_size & (PAGE_SIZE-1));
1903 BUG_ON((unsigned long)layout->ro_after_init_size & (PAGE_SIZE-1));
1904 set_memory((unsigned long)layout->base + layout->ro_size,
1905 (layout->ro_after_init_size - layout->ro_size) >> PAGE_SHIFT);
1906}
1907
Rusty Russell85c898d2015-11-26 09:45:08 +10301908static void frob_writable_data(const struct module_layout *layout,
1909 int (*set_memory)(unsigned long start, int num_pages))
matthieu castet84e1c6b2010-11-16 22:35:16 +01001910{
Rusty Russell85c898d2015-11-26 09:45:08 +10301911 BUG_ON((unsigned long)layout->base & (PAGE_SIZE-1));
Jessica Yu444d13f2016-07-27 12:06:21 +09301912 BUG_ON((unsigned long)layout->ro_after_init_size & (PAGE_SIZE-1));
Rusty Russell85c898d2015-11-26 09:45:08 +10301913 BUG_ON((unsigned long)layout->size & (PAGE_SIZE-1));
Jessica Yu444d13f2016-07-27 12:06:21 +09301914 set_memory((unsigned long)layout->base + layout->ro_after_init_size,
1915 (layout->size - layout->ro_after_init_size) >> PAGE_SHIFT);
Jan Glauber01526ed2011-05-19 16:55:26 -06001916}
1917
Rusty Russell85c898d2015-11-26 09:45:08 +10301918/* livepatching wants to disable read-only so it can frob module. */
1919void module_disable_ro(const struct module *mod)
Jan Glauber01526ed2011-05-19 16:55:26 -06001920{
AKASHI Takahiro39290b32016-11-14 15:15:05 +09001921 if (!rodata_enabled)
1922 return;
1923
Rusty Russell85c898d2015-11-26 09:45:08 +10301924 frob_text(&mod->core_layout, set_memory_rw);
1925 frob_rodata(&mod->core_layout, set_memory_rw);
Jessica Yu444d13f2016-07-27 12:06:21 +09301926 frob_ro_after_init(&mod->core_layout, set_memory_rw);
Rusty Russell85c898d2015-11-26 09:45:08 +10301927 frob_text(&mod->init_layout, set_memory_rw);
1928 frob_rodata(&mod->init_layout, set_memory_rw);
Josh Poimboeuf20ef10c2015-11-26 09:42:08 +10301929}
1930
Jessica Yu444d13f2016-07-27 12:06:21 +09301931void module_enable_ro(const struct module *mod, bool after_init)
Josh Poimboeuf20ef10c2015-11-26 09:42:08 +10301932{
AKASHI Takahiro39290b32016-11-14 15:15:05 +09001933 if (!rodata_enabled)
1934 return;
1935
Rusty Russell85c898d2015-11-26 09:45:08 +10301936 frob_text(&mod->core_layout, set_memory_ro);
1937 frob_rodata(&mod->core_layout, set_memory_ro);
1938 frob_text(&mod->init_layout, set_memory_ro);
1939 frob_rodata(&mod->init_layout, set_memory_ro);
Jessica Yu444d13f2016-07-27 12:06:21 +09301940
1941 if (after_init)
1942 frob_ro_after_init(&mod->core_layout, set_memory_ro);
Jan Glauber01526ed2011-05-19 16:55:26 -06001943}
1944
Rusty Russell85c898d2015-11-26 09:45:08 +10301945static void module_enable_nx(const struct module *mod)
Jan Glauber01526ed2011-05-19 16:55:26 -06001946{
Rusty Russell85c898d2015-11-26 09:45:08 +10301947 frob_rodata(&mod->core_layout, set_memory_nx);
Jessica Yu444d13f2016-07-27 12:06:21 +09301948 frob_ro_after_init(&mod->core_layout, set_memory_nx);
Rusty Russell85c898d2015-11-26 09:45:08 +10301949 frob_writable_data(&mod->core_layout, set_memory_nx);
1950 frob_rodata(&mod->init_layout, set_memory_nx);
1951 frob_writable_data(&mod->init_layout, set_memory_nx);
1952}
1953
1954static void module_disable_nx(const struct module *mod)
1955{
1956 frob_rodata(&mod->core_layout, set_memory_x);
Jessica Yu444d13f2016-07-27 12:06:21 +09301957 frob_ro_after_init(&mod->core_layout, set_memory_x);
Rusty Russell85c898d2015-11-26 09:45:08 +10301958 frob_writable_data(&mod->core_layout, set_memory_x);
1959 frob_rodata(&mod->init_layout, set_memory_x);
1960 frob_writable_data(&mod->init_layout, set_memory_x);
matthieu castet84e1c6b2010-11-16 22:35:16 +01001961}
1962
1963/* Iterate through all modules and set each module's text as RW */
Daniel J Blueman5d05c702011-03-08 22:01:47 +08001964void set_all_modules_text_rw(void)
matthieu castet84e1c6b2010-11-16 22:35:16 +01001965{
1966 struct module *mod;
1967
AKASHI Takahiro39290b32016-11-14 15:15:05 +09001968 if (!rodata_enabled)
1969 return;
1970
matthieu castet84e1c6b2010-11-16 22:35:16 +01001971 mutex_lock(&module_mutex);
1972 list_for_each_entry_rcu(mod, &modules, list) {
Rusty Russell0d21b0e2013-01-12 11:38:44 +10301973 if (mod->state == MODULE_STATE_UNFORMED)
1974 continue;
Rusty Russell85c898d2015-11-26 09:45:08 +10301975
1976 frob_text(&mod->core_layout, set_memory_rw);
1977 frob_text(&mod->init_layout, set_memory_rw);
matthieu castet84e1c6b2010-11-16 22:35:16 +01001978 }
1979 mutex_unlock(&module_mutex);
1980}
1981
1982/* Iterate through all modules and set each module's text as RO */
Daniel J Blueman5d05c702011-03-08 22:01:47 +08001983void set_all_modules_text_ro(void)
matthieu castet84e1c6b2010-11-16 22:35:16 +01001984{
1985 struct module *mod;
1986
AKASHI Takahiro39290b32016-11-14 15:15:05 +09001987 if (!rodata_enabled)
1988 return;
1989
matthieu castet84e1c6b2010-11-16 22:35:16 +01001990 mutex_lock(&module_mutex);
1991 list_for_each_entry_rcu(mod, &modules, list) {
Aaron Tomlin905dd707f2016-10-27 10:36:06 +01001992 /*
1993 * Ignore going modules since it's possible that ro
1994 * protection has already been disabled, otherwise we'll
1995 * run into protection faults at module deallocation.
1996 */
1997 if (mod->state == MODULE_STATE_UNFORMED ||
1998 mod->state == MODULE_STATE_GOING)
Rusty Russell0d21b0e2013-01-12 11:38:44 +10301999 continue;
Rusty Russell85c898d2015-11-26 09:45:08 +10302000
2001 frob_text(&mod->core_layout, set_memory_ro);
2002 frob_text(&mod->init_layout, set_memory_ro);
matthieu castet84e1c6b2010-11-16 22:35:16 +01002003 }
2004 mutex_unlock(&module_mutex);
2005}
Rusty Russell85c898d2015-11-26 09:45:08 +10302006
2007static void disable_ro_nx(const struct module_layout *layout)
2008{
AKASHI Takahiro39290b32016-11-14 15:15:05 +09002009 if (rodata_enabled) {
2010 frob_text(layout, set_memory_rw);
2011 frob_rodata(layout, set_memory_rw);
2012 frob_ro_after_init(layout, set_memory_rw);
2013 }
Rusty Russell85c898d2015-11-26 09:45:08 +10302014 frob_rodata(layout, set_memory_x);
Jessica Yu444d13f2016-07-27 12:06:21 +09302015 frob_ro_after_init(layout, set_memory_x);
Rusty Russell85c898d2015-11-26 09:45:08 +10302016 frob_writable_data(layout, set_memory_x);
2017}
2018
matthieu castet84e1c6b2010-11-16 22:35:16 +01002019#else
Rusty Russell85c898d2015-11-26 09:45:08 +10302020static void disable_ro_nx(const struct module_layout *layout) { }
2021static void module_enable_nx(const struct module *mod) { }
2022static void module_disable_nx(const struct module *mod) { }
matthieu castet84e1c6b2010-11-16 22:35:16 +01002023#endif
2024
Jessica Yu1ce15ef2016-03-22 20:03:16 -04002025#ifdef CONFIG_LIVEPATCH
2026/*
2027 * Persist Elf information about a module. Copy the Elf header,
2028 * section header table, section string table, and symtab section
2029 * index from info to mod->klp_info.
2030 */
2031static int copy_module_elf(struct module *mod, struct load_info *info)
2032{
2033 unsigned int size, symndx;
2034 int ret;
2035
2036 size = sizeof(*mod->klp_info);
2037 mod->klp_info = kmalloc(size, GFP_KERNEL);
2038 if (mod->klp_info == NULL)
2039 return -ENOMEM;
2040
2041 /* Elf header */
2042 size = sizeof(mod->klp_info->hdr);
2043 memcpy(&mod->klp_info->hdr, info->hdr, size);
2044
2045 /* Elf section header table */
2046 size = sizeof(*info->sechdrs) * info->hdr->e_shnum;
2047 mod->klp_info->sechdrs = kmalloc(size, GFP_KERNEL);
2048 if (mod->klp_info->sechdrs == NULL) {
2049 ret = -ENOMEM;
2050 goto free_info;
2051 }
2052 memcpy(mod->klp_info->sechdrs, info->sechdrs, size);
2053
2054 /* Elf section name string table */
2055 size = info->sechdrs[info->hdr->e_shstrndx].sh_size;
2056 mod->klp_info->secstrings = kmalloc(size, GFP_KERNEL);
2057 if (mod->klp_info->secstrings == NULL) {
2058 ret = -ENOMEM;
2059 goto free_sechdrs;
2060 }
2061 memcpy(mod->klp_info->secstrings, info->secstrings, size);
2062
2063 /* Elf symbol section index */
2064 symndx = info->index.sym;
2065 mod->klp_info->symndx = symndx;
2066
2067 /*
2068 * For livepatch modules, core_kallsyms.symtab is a complete
2069 * copy of the original symbol table. Adjust sh_addr to point
2070 * to core_kallsyms.symtab since the copy of the symtab in module
2071 * init memory is freed at the end of do_init_module().
2072 */
2073 mod->klp_info->sechdrs[symndx].sh_addr = \
2074 (unsigned long) mod->core_kallsyms.symtab;
2075
2076 return 0;
2077
2078free_sechdrs:
2079 kfree(mod->klp_info->sechdrs);
2080free_info:
2081 kfree(mod->klp_info);
2082 return ret;
2083}
2084
2085static void free_module_elf(struct module *mod)
2086{
2087 kfree(mod->klp_info->sechdrs);
2088 kfree(mod->klp_info->secstrings);
2089 kfree(mod->klp_info);
2090}
2091#else /* !CONFIG_LIVEPATCH */
2092static int copy_module_elf(struct module *mod, struct load_info *info)
2093{
2094 return 0;
2095}
2096
2097static void free_module_elf(struct module *mod)
2098{
2099}
2100#endif /* CONFIG_LIVEPATCH */
2101
Rusty Russellbe1f2212015-01-20 09:07:05 +10302102void __weak module_memfree(void *module_region)
Jonas Bonn74e08fc2011-06-30 21:22:11 +02002103{
2104 vfree(module_region);
2105}
2106
2107void __weak module_arch_cleanup(struct module *mod)
2108{
2109}
2110
Rusty Russelld453cde2015-01-20 09:07:04 +10302111void __weak module_arch_freeing_init(struct module *mod)
2112{
2113}
2114
Rusty Russell75676502010-06-05 11:17:36 -06002115/* Free a module, remove from lists, etc. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116static void free_module(struct module *mod)
2117{
Li Zefan7ead8b82009-08-17 16:56:28 +08002118 trace_module_free(mod);
2119
Rusty Russell36b03602010-08-05 12:59:09 -06002120 mod_sysfs_teardown(mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121
Rusty Russell944a1fa2013-04-17 13:20:03 +09302122 /* We leave it in list to prevent duplicate loads, but make sure
2123 * that noone uses it while it's being deconstructed. */
Prarit Bhargavad3051b42014-10-14 02:51:39 +10302124 mutex_lock(&module_mutex);
Rusty Russell944a1fa2013-04-17 13:20:03 +09302125 mod->state = MODULE_STATE_UNFORMED;
Prarit Bhargavad3051b42014-10-14 02:51:39 +10302126 mutex_unlock(&module_mutex);
Rusty Russell944a1fa2013-04-17 13:20:03 +09302127
Jason Baronb82bab4b2010-07-27 13:18:01 -07002128 /* Remove dynamic debug info */
2129 ddebug_remove_module(mod->name);
2130
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131 /* Arch-specific cleanup. */
2132 module_arch_cleanup(mod);
2133
2134 /* Module unload stuff */
2135 module_unload_free(mod);
2136
Rusty Russelle180a6b2009-03-31 13:05:29 -06002137 /* Free any allocated parameters. */
2138 destroy_params(mod->kp, mod->num_kp);
2139
Jessica Yu1ce15ef2016-03-22 20:03:16 -04002140 if (is_livepatch_module(mod))
2141 free_module_elf(mod);
2142
Rusty Russell944a1fa2013-04-17 13:20:03 +09302143 /* Now we can delete it from the lists */
2144 mutex_lock(&module_mutex);
Masami Hiramatsu461e34a2014-11-10 09:27:29 +10302145 /* Unlink carefully: kallsyms could be walking list. */
2146 list_del_rcu(&mod->list);
Peter Zijlstra93c2e102015-05-27 11:09:37 +09302147 mod_tree_remove(mod);
Masami Hiramatsu0286b5e2014-11-10 09:28:29 +10302148 /* Remove this module from bug list, this uses list_del_rcu */
Masami Hiramatsu461e34a2014-11-10 09:27:29 +10302149 module_bug_cleanup(mod);
Peter Zijlstra0be964b2015-05-27 11:09:35 +09302150 /* Wait for RCU-sched synchronizing before releasing mod->list and buglist. */
2151 synchronize_sched();
Rusty Russell944a1fa2013-04-17 13:20:03 +09302152 mutex_unlock(&module_mutex);
2153
Rusty Russell85c898d2015-11-26 09:45:08 +10302154 /* This may be empty, but that's OK */
2155 disable_ro_nx(&mod->init_layout);
Rusty Russelld453cde2015-01-20 09:07:04 +10302156 module_arch_freeing_init(mod);
Rusty Russell7523e4d2015-11-26 09:44:08 +10302157 module_memfree(mod->init_layout.base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002158 kfree(mod->args);
Tejun Heo259354d2010-03-10 18:56:10 +09002159 percpu_modfree(mod);
Rusty Russell9f85a4b2010-08-05 12:59:04 -06002160
Peter Zijlstra35a93932015-02-26 16:23:11 +01002161 /* Free lock-classes; relies on the preceding sync_rcu(). */
Rusty Russell7523e4d2015-11-26 09:44:08 +10302162 lockdep_free_key_range(mod->core_layout.base, mod->core_layout.size);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002163
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164 /* Finally, free the core (containing the module structure) */
Rusty Russell85c898d2015-11-26 09:45:08 +10302165 disable_ro_nx(&mod->core_layout);
Rusty Russell7523e4d2015-11-26 09:44:08 +10302166 module_memfree(mod->core_layout.base);
Bernd Schmidteb8cdec2009-09-21 17:03:57 -07002167
2168#ifdef CONFIG_MPU
2169 update_protections(current->mm);
2170#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171}
2172
2173void *__symbol_get(const char *symbol)
2174{
2175 struct module *owner;
Tim Abbott414fd312008-12-05 19:03:56 -05002176 const struct kernel_symbol *sym;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177
Rusty Russell24da1cb2007-07-15 23:41:46 -07002178 preempt_disable();
Tim Abbott414fd312008-12-05 19:03:56 -05002179 sym = find_symbol(symbol, &owner, NULL, true, true);
2180 if (sym && strong_try_module_get(owner))
2181 sym = NULL;
Rusty Russell24da1cb2007-07-15 23:41:46 -07002182 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183
Tim Abbott414fd312008-12-05 19:03:56 -05002184 return sym ? (void *)sym->value : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185}
2186EXPORT_SYMBOL_GPL(__symbol_get);
2187
Ashutosh Naikeea8b542006-01-08 01:04:25 -08002188/*
2189 * Ensure that an exported symbol [global namespace] does not already exist
Robert P. J. Day02a3e592007-05-09 07:26:28 +02002190 * in the kernel or in some other module's exported symbol table.
Rusty Russellbe593f42010-06-05 11:17:37 -06002191 *
2192 * You must hold the module_mutex.
Ashutosh Naikeea8b542006-01-08 01:04:25 -08002193 */
2194static int verify_export_symbols(struct module *mod)
2195{
Rusty Russellb2111042008-05-01 21:15:00 -05002196 unsigned int i;
Ashutosh Naikeea8b542006-01-08 01:04:25 -08002197 struct module *owner;
Rusty Russellb2111042008-05-01 21:15:00 -05002198 const struct kernel_symbol *s;
2199 struct {
2200 const struct kernel_symbol *sym;
2201 unsigned int num;
2202 } arr[] = {
2203 { mod->syms, mod->num_syms },
2204 { mod->gpl_syms, mod->num_gpl_syms },
2205 { mod->gpl_future_syms, mod->num_gpl_future_syms },
Denys Vlasenkof7f5b672008-07-22 19:24:26 -05002206#ifdef CONFIG_UNUSED_SYMBOLS
Rusty Russellb2111042008-05-01 21:15:00 -05002207 { mod->unused_syms, mod->num_unused_syms },
2208 { mod->unused_gpl_syms, mod->num_unused_gpl_syms },
Denys Vlasenkof7f5b672008-07-22 19:24:26 -05002209#endif
Rusty Russellb2111042008-05-01 21:15:00 -05002210 };
Ashutosh Naikeea8b542006-01-08 01:04:25 -08002211
Rusty Russellb2111042008-05-01 21:15:00 -05002212 for (i = 0; i < ARRAY_SIZE(arr); i++) {
2213 for (s = arr[i].sym; s < arr[i].sym + arr[i].num; s++) {
Rusty Russellbe593f42010-06-05 11:17:37 -06002214 if (find_symbol(s->name, &owner, NULL, true, false)) {
Andrew Mortonbddb12b2013-11-12 15:11:28 -08002215 pr_err("%s: exports duplicate symbol %s"
Rusty Russellb2111042008-05-01 21:15:00 -05002216 " (owned by %s)\n",
2217 mod->name, s->name, module_name(owner));
2218 return -ENOEXEC;
2219 }
Ashutosh Naikeea8b542006-01-08 01:04:25 -08002220 }
Rusty Russellb2111042008-05-01 21:15:00 -05002221 }
2222 return 0;
Ashutosh Naikeea8b542006-01-08 01:04:25 -08002223}
2224
Matti Linnanvuori9a4b9702007-11-08 08:37:38 -08002225/* Change all symbols so that st_value encodes the pointer directly. */
Rusty Russell49668682010-08-05 12:59:10 -06002226static int simplify_symbols(struct module *mod, const struct load_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002227{
Rusty Russell49668682010-08-05 12:59:10 -06002228 Elf_Shdr *symsec = &info->sechdrs[info->index.sym];
2229 Elf_Sym *sym = (void *)symsec->sh_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002230 unsigned long secbase;
Rusty Russell49668682010-08-05 12:59:10 -06002231 unsigned int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002232 int ret = 0;
Tim Abbott414fd312008-12-05 19:03:56 -05002233 const struct kernel_symbol *ksym;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002234
Rusty Russell49668682010-08-05 12:59:10 -06002235 for (i = 1; i < symsec->sh_size / sizeof(Elf_Sym); i++) {
2236 const char *name = info->strtab + sym[i].st_name;
2237
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238 switch (sym[i].st_shndx) {
2239 case SHN_COMMON:
Joe Mario80375982014-02-08 09:01:09 +01002240 /* Ignore common symbols */
2241 if (!strncmp(name, "__gnu_lto", 9))
2242 break;
2243
Linus Torvalds1da177e2005-04-16 15:20:36 -07002244 /* We compiled with -fno-common. These are not
2245 supposed to happen. */
Jim Cromie5e124162011-12-06 12:11:31 -07002246 pr_debug("Common symbol: %s\n", name);
Ionut Alexa6da0b562014-11-10 09:31:29 +10302247 pr_warn("%s: please compile with -fno-common\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002248 mod->name);
2249 ret = -ENOEXEC;
2250 break;
2251
2252 case SHN_ABS:
2253 /* Don't need to do anything */
Jim Cromie5e124162011-12-06 12:11:31 -07002254 pr_debug("Absolute symbol: 0x%08lx\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002255 (long)sym[i].st_value);
2256 break;
2257
Jessica Yu1ce15ef2016-03-22 20:03:16 -04002258 case SHN_LIVEPATCH:
2259 /* Livepatch symbols are resolved by livepatch */
2260 break;
2261
Linus Torvalds1da177e2005-04-16 15:20:36 -07002262 case SHN_UNDEF:
Rusty Russell49668682010-08-05 12:59:10 -06002263 ksym = resolve_symbol_wait(mod, info, name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002264 /* Ok if resolved. */
Rusty Russell9bea7f22010-06-05 11:17:37 -06002265 if (ksym && !IS_ERR(ksym)) {
Tim Abbott414fd312008-12-05 19:03:56 -05002266 sym[i].st_value = ksym->value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267 break;
Tim Abbott414fd312008-12-05 19:03:56 -05002268 }
2269
Linus Torvalds1da177e2005-04-16 15:20:36 -07002270 /* Ok if weak. */
Rusty Russell9bea7f22010-06-05 11:17:37 -06002271 if (!ksym && ELF_ST_BIND(sym[i].st_info) == STB_WEAK)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002272 break;
2273
Andrew Mortonbddb12b2013-11-12 15:11:28 -08002274 pr_warn("%s: Unknown symbol %s (err %li)\n",
2275 mod->name, name, PTR_ERR(ksym));
Rusty Russell9bea7f22010-06-05 11:17:37 -06002276 ret = PTR_ERR(ksym) ?: -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002277 break;
2278
2279 default:
2280 /* Divert to percpu allocation if a percpu var. */
Rusty Russell49668682010-08-05 12:59:10 -06002281 if (sym[i].st_shndx == info->index.pcpu)
Tejun Heo259354d2010-03-10 18:56:10 +09002282 secbase = (unsigned long)mod_percpu(mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002283 else
Rusty Russell49668682010-08-05 12:59:10 -06002284 secbase = info->sechdrs[sym[i].st_shndx].sh_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002285 sym[i].st_value += secbase;
2286 break;
2287 }
2288 }
2289
2290 return ret;
2291}
2292
Rusty Russell49668682010-08-05 12:59:10 -06002293static int apply_relocations(struct module *mod, const struct load_info *info)
Rusty Russell22e268e2010-08-05 12:59:05 -06002294{
2295 unsigned int i;
2296 int err = 0;
2297
2298 /* Now do relocations. */
Rusty Russell49668682010-08-05 12:59:10 -06002299 for (i = 1; i < info->hdr->e_shnum; i++) {
2300 unsigned int infosec = info->sechdrs[i].sh_info;
Rusty Russell22e268e2010-08-05 12:59:05 -06002301
2302 /* Not a valid relocation section? */
Rusty Russell49668682010-08-05 12:59:10 -06002303 if (infosec >= info->hdr->e_shnum)
Rusty Russell22e268e2010-08-05 12:59:05 -06002304 continue;
2305
2306 /* Don't bother with non-allocated sections */
Rusty Russell49668682010-08-05 12:59:10 -06002307 if (!(info->sechdrs[infosec].sh_flags & SHF_ALLOC))
Rusty Russell22e268e2010-08-05 12:59:05 -06002308 continue;
2309
Jessica Yu1ce15ef2016-03-22 20:03:16 -04002310 /* Livepatch relocation sections are applied by livepatch */
2311 if (info->sechdrs[i].sh_flags & SHF_RELA_LIVEPATCH)
2312 continue;
2313
Rusty Russell49668682010-08-05 12:59:10 -06002314 if (info->sechdrs[i].sh_type == SHT_REL)
2315 err = apply_relocate(info->sechdrs, info->strtab,
2316 info->index.sym, i, mod);
2317 else if (info->sechdrs[i].sh_type == SHT_RELA)
2318 err = apply_relocate_add(info->sechdrs, info->strtab,
2319 info->index.sym, i, mod);
Rusty Russell22e268e2010-08-05 12:59:05 -06002320 if (err < 0)
2321 break;
2322 }
2323 return err;
2324}
2325
Helge Deller088af9a2008-12-31 12:31:18 +01002326/* Additional bytes needed by arch in front of individual sections */
2327unsigned int __weak arch_mod_section_prepend(struct module *mod,
2328 unsigned int section)
2329{
2330 /* default implementation just returns zero */
2331 return 0;
2332}
2333
Linus Torvalds1da177e2005-04-16 15:20:36 -07002334/* Update size with this section: return offset. */
Helge Deller088af9a2008-12-31 12:31:18 +01002335static long get_offset(struct module *mod, unsigned int *size,
2336 Elf_Shdr *sechdr, unsigned int section)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002337{
2338 long ret;
2339
Helge Deller088af9a2008-12-31 12:31:18 +01002340 *size += arch_mod_section_prepend(mod, section);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002341 ret = ALIGN(*size, sechdr->sh_addralign ?: 1);
2342 *size = ret + sechdr->sh_size;
2343 return ret;
2344}
2345
2346/* Lay out the SHF_ALLOC sections in a way not dissimilar to how ld
2347 might -- code, read-only data, read-write data, small data. Tally
2348 sizes, and place the offsets into sh_entsize fields: high bit means it
2349 belongs in init. */
Rusty Russell49668682010-08-05 12:59:10 -06002350static void layout_sections(struct module *mod, struct load_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002351{
2352 static unsigned long const masks[][2] = {
2353 /* NOTE: all executable code must be the first section
2354 * in this array; otherwise modify the text_size
2355 * finder in the two loops below */
2356 { SHF_EXECINSTR | SHF_ALLOC, ARCH_SHF_SMALL },
2357 { SHF_ALLOC, SHF_WRITE | ARCH_SHF_SMALL },
Jessica Yu444d13f2016-07-27 12:06:21 +09302358 { SHF_RO_AFTER_INIT | SHF_ALLOC, ARCH_SHF_SMALL },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002359 { SHF_WRITE | SHF_ALLOC, ARCH_SHF_SMALL },
2360 { ARCH_SHF_SMALL | SHF_ALLOC, 0 }
2361 };
2362 unsigned int m, i;
2363
Rusty Russell49668682010-08-05 12:59:10 -06002364 for (i = 0; i < info->hdr->e_shnum; i++)
2365 info->sechdrs[i].sh_entsize = ~0UL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002366
Jim Cromie5e124162011-12-06 12:11:31 -07002367 pr_debug("Core section allocation order:\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002368 for (m = 0; m < ARRAY_SIZE(masks); ++m) {
Rusty Russell49668682010-08-05 12:59:10 -06002369 for (i = 0; i < info->hdr->e_shnum; ++i) {
2370 Elf_Shdr *s = &info->sechdrs[i];
2371 const char *sname = info->secstrings + s->sh_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002372
2373 if ((s->sh_flags & masks[m][0]) != masks[m][0]
2374 || (s->sh_flags & masks[m][1])
2375 || s->sh_entsize != ~0UL
Rusty Russell49668682010-08-05 12:59:10 -06002376 || strstarts(sname, ".init"))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002377 continue;
Rusty Russell7523e4d2015-11-26 09:44:08 +10302378 s->sh_entsize = get_offset(mod, &mod->core_layout.size, s, i);
Jim Cromie5e124162011-12-06 12:11:31 -07002379 pr_debug("\t%s\n", sname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002380 }
matthieu castet84e1c6b2010-11-16 22:35:16 +01002381 switch (m) {
2382 case 0: /* executable */
Rusty Russell7523e4d2015-11-26 09:44:08 +10302383 mod->core_layout.size = debug_align(mod->core_layout.size);
2384 mod->core_layout.text_size = mod->core_layout.size;
matthieu castet84e1c6b2010-11-16 22:35:16 +01002385 break;
2386 case 1: /* RO: text and ro-data */
Rusty Russell7523e4d2015-11-26 09:44:08 +10302387 mod->core_layout.size = debug_align(mod->core_layout.size);
2388 mod->core_layout.ro_size = mod->core_layout.size;
matthieu castet84e1c6b2010-11-16 22:35:16 +01002389 break;
Jessica Yu444d13f2016-07-27 12:06:21 +09302390 case 2: /* RO after init */
2391 mod->core_layout.size = debug_align(mod->core_layout.size);
2392 mod->core_layout.ro_after_init_size = mod->core_layout.size;
2393 break;
2394 case 4: /* whole core */
Rusty Russell7523e4d2015-11-26 09:44:08 +10302395 mod->core_layout.size = debug_align(mod->core_layout.size);
matthieu castet84e1c6b2010-11-16 22:35:16 +01002396 break;
2397 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002398 }
2399
Jim Cromie5e124162011-12-06 12:11:31 -07002400 pr_debug("Init section allocation order:\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002401 for (m = 0; m < ARRAY_SIZE(masks); ++m) {
Rusty Russell49668682010-08-05 12:59:10 -06002402 for (i = 0; i < info->hdr->e_shnum; ++i) {
2403 Elf_Shdr *s = &info->sechdrs[i];
2404 const char *sname = info->secstrings + s->sh_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002405
2406 if ((s->sh_flags & masks[m][0]) != masks[m][0]
2407 || (s->sh_flags & masks[m][1])
2408 || s->sh_entsize != ~0UL
Rusty Russell49668682010-08-05 12:59:10 -06002409 || !strstarts(sname, ".init"))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002410 continue;
Rusty Russell7523e4d2015-11-26 09:44:08 +10302411 s->sh_entsize = (get_offset(mod, &mod->init_layout.size, s, i)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002412 | INIT_OFFSET_MASK);
Jim Cromie5e124162011-12-06 12:11:31 -07002413 pr_debug("\t%s\n", sname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002414 }
matthieu castet84e1c6b2010-11-16 22:35:16 +01002415 switch (m) {
2416 case 0: /* executable */
Rusty Russell7523e4d2015-11-26 09:44:08 +10302417 mod->init_layout.size = debug_align(mod->init_layout.size);
2418 mod->init_layout.text_size = mod->init_layout.size;
matthieu castet84e1c6b2010-11-16 22:35:16 +01002419 break;
2420 case 1: /* RO: text and ro-data */
Rusty Russell7523e4d2015-11-26 09:44:08 +10302421 mod->init_layout.size = debug_align(mod->init_layout.size);
2422 mod->init_layout.ro_size = mod->init_layout.size;
matthieu castet84e1c6b2010-11-16 22:35:16 +01002423 break;
Jessica Yu444d13f2016-07-27 12:06:21 +09302424 case 2:
2425 /*
2426 * RO after init doesn't apply to init_layout (only
2427 * core_layout), so it just takes the value of ro_size.
2428 */
2429 mod->init_layout.ro_after_init_size = mod->init_layout.ro_size;
2430 break;
2431 case 4: /* whole init */
Rusty Russell7523e4d2015-11-26 09:44:08 +10302432 mod->init_layout.size = debug_align(mod->init_layout.size);
matthieu castet84e1c6b2010-11-16 22:35:16 +01002433 break;
2434 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002435 }
2436}
2437
Linus Torvalds1da177e2005-04-16 15:20:36 -07002438static void set_license(struct module *mod, const char *license)
2439{
2440 if (!license)
2441 license = "unspecified";
2442
Florin Malitafa3ba2e82006-10-11 01:21:48 -07002443 if (!license_is_gpl_compatible(license)) {
Andi Kleen25ddbb12008-10-15 22:01:41 -07002444 if (!test_taint(TAINT_PROPRIETARY_MODULE))
Andrew Mortonbddb12b2013-11-12 15:11:28 -08002445 pr_warn("%s: module license '%s' taints kernel.\n",
2446 mod->name, license);
Rusty Russell373d4d02013-01-21 17:17:39 +10302447 add_taint_module(mod, TAINT_PROPRIETARY_MODULE,
2448 LOCKDEP_NOW_UNRELIABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002449 }
2450}
2451
2452/* Parse tag=value strings from .modinfo section */
2453static char *next_string(char *string, unsigned long *secsize)
2454{
2455 /* Skip non-zero chars */
2456 while (string[0]) {
2457 string++;
2458 if ((*secsize)-- <= 1)
2459 return NULL;
2460 }
2461
2462 /* Skip any zero padding. */
2463 while (!string[0]) {
2464 string++;
2465 if ((*secsize)-- <= 1)
2466 return NULL;
2467 }
2468 return string;
2469}
2470
Rusty Russell49668682010-08-05 12:59:10 -06002471static char *get_modinfo(struct load_info *info, const char *tag)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002472{
2473 char *p;
2474 unsigned int taglen = strlen(tag);
Rusty Russell49668682010-08-05 12:59:10 -06002475 Elf_Shdr *infosec = &info->sechdrs[info->index.info];
2476 unsigned long size = infosec->sh_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002477
Rusty Russell49668682010-08-05 12:59:10 -06002478 for (p = (char *)infosec->sh_addr; p; p = next_string(p, &size)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002479 if (strncmp(p, tag, taglen) == 0 && p[taglen] == '=')
2480 return p + taglen + 1;
2481 }
2482 return NULL;
2483}
2484
Rusty Russell49668682010-08-05 12:59:10 -06002485static void setup_modinfo(struct module *mod, struct load_info *info)
Matt Domschc988d2b2005-06-23 22:05:15 -07002486{
2487 struct module_attribute *attr;
2488 int i;
2489
2490 for (i = 0; (attr = modinfo_attrs[i]); i++) {
2491 if (attr->setup)
Rusty Russell49668682010-08-05 12:59:10 -06002492 attr->setup(mod, get_modinfo(info, attr->attr.name));
Matt Domschc988d2b2005-06-23 22:05:15 -07002493 }
2494}
Matt Domschc988d2b2005-06-23 22:05:15 -07002495
Rusty Russella263f7762009-09-25 00:32:58 -06002496static void free_modinfo(struct module *mod)
2497{
2498 struct module_attribute *attr;
2499 int i;
2500
2501 for (i = 0; (attr = modinfo_attrs[i]); i++) {
2502 if (attr->free)
2503 attr->free(mod);
2504 }
2505}
2506
Linus Torvalds1da177e2005-04-16 15:20:36 -07002507#ifdef CONFIG_KALLSYMS
WANG Cong15bba372008-07-24 15:41:48 +01002508
2509/* lookup symbol in given range of kernel_symbols */
2510static const struct kernel_symbol *lookup_symbol(const char *name,
2511 const struct kernel_symbol *start,
2512 const struct kernel_symbol *stop)
2513{
Alessio Igor Bogani9d634872011-05-18 22:35:59 +02002514 return bsearch(name, start, stop - start,
2515 sizeof(struct kernel_symbol), cmp_name);
WANG Cong15bba372008-07-24 15:41:48 +01002516}
2517
Tim Abbottca4787b2009-01-05 08:40:10 -06002518static int is_exported(const char *name, unsigned long value,
2519 const struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002520{
Tim Abbottca4787b2009-01-05 08:40:10 -06002521 const struct kernel_symbol *ks;
2522 if (!mod)
2523 ks = lookup_symbol(name, __start___ksymtab, __stop___ksymtab);
Sam Ravnborg3fd68052006-02-08 21:16:45 +01002524 else
Tim Abbottca4787b2009-01-05 08:40:10 -06002525 ks = lookup_symbol(name, mod->syms, mod->syms + mod->num_syms);
2526 return ks != NULL && ks->value == value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002527}
2528
2529/* As per nm */
Rusty Russelleded41c2010-08-05 12:59:07 -06002530static char elf_type(const Elf_Sym *sym, const struct load_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002531{
Rusty Russelleded41c2010-08-05 12:59:07 -06002532 const Elf_Shdr *sechdrs = info->sechdrs;
2533
Linus Torvalds1da177e2005-04-16 15:20:36 -07002534 if (ELF_ST_BIND(sym->st_info) == STB_WEAK) {
2535 if (ELF_ST_TYPE(sym->st_info) == STT_OBJECT)
2536 return 'v';
2537 else
2538 return 'w';
2539 }
2540 if (sym->st_shndx == SHN_UNDEF)
2541 return 'U';
Miroslav Benese0224412015-11-26 13:18:06 +10302542 if (sym->st_shndx == SHN_ABS || sym->st_shndx == info->index.pcpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002543 return 'a';
2544 if (sym->st_shndx >= SHN_LORESERVE)
2545 return '?';
2546 if (sechdrs[sym->st_shndx].sh_flags & SHF_EXECINSTR)
2547 return 't';
2548 if (sechdrs[sym->st_shndx].sh_flags & SHF_ALLOC
2549 && sechdrs[sym->st_shndx].sh_type != SHT_NOBITS) {
2550 if (!(sechdrs[sym->st_shndx].sh_flags & SHF_WRITE))
2551 return 'r';
2552 else if (sechdrs[sym->st_shndx].sh_flags & ARCH_SHF_SMALL)
2553 return 'g';
2554 else
2555 return 'd';
2556 }
2557 if (sechdrs[sym->st_shndx].sh_type == SHT_NOBITS) {
2558 if (sechdrs[sym->st_shndx].sh_flags & ARCH_SHF_SMALL)
2559 return 's';
2560 else
2561 return 'b';
2562 }
Rusty Russelleded41c2010-08-05 12:59:07 -06002563 if (strstarts(info->secstrings + sechdrs[sym->st_shndx].sh_name,
2564 ".debug")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002565 return 'n';
Rusty Russelleded41c2010-08-05 12:59:07 -06002566 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002567 return '?';
2568}
2569
Jan Beulich4a496222009-07-06 14:50:42 +01002570static bool is_core_symbol(const Elf_Sym *src, const Elf_Shdr *sechdrs,
Miroslav Benese0224412015-11-26 13:18:06 +10302571 unsigned int shnum, unsigned int pcpundx)
Jan Beulich4a496222009-07-06 14:50:42 +01002572{
2573 const Elf_Shdr *sec;
2574
2575 if (src->st_shndx == SHN_UNDEF
2576 || src->st_shndx >= shnum
2577 || !src->st_name)
2578 return false;
2579
Miroslav Benese0224412015-11-26 13:18:06 +10302580#ifdef CONFIG_KALLSYMS_ALL
2581 if (src->st_shndx == pcpundx)
2582 return true;
2583#endif
2584
Jan Beulich4a496222009-07-06 14:50:42 +01002585 sec = sechdrs + src->st_shndx;
2586 if (!(sec->sh_flags & SHF_ALLOC)
2587#ifndef CONFIG_KALLSYMS_ALL
2588 || !(sec->sh_flags & SHF_EXECINSTR)
2589#endif
2590 || (sec->sh_entsize & INIT_OFFSET_MASK))
2591 return false;
2592
2593 return true;
2594}
2595
Kevin Cernekee48fd1182012-01-13 09:32:14 +10302596/*
2597 * We only allocate and copy the strings needed by the parts of symtab
2598 * we keep. This is simple, but has the effect of making multiple
2599 * copies of duplicates. We could be more sophisticated, see
2600 * linux-kernel thread starting with
2601 * <73defb5e4bca04a6431392cc341112b1@localhost>.
2602 */
Rusty Russell49668682010-08-05 12:59:10 -06002603static void layout_symtab(struct module *mod, struct load_info *info)
Jan Beulich4a496222009-07-06 14:50:42 +01002604{
Rusty Russell49668682010-08-05 12:59:10 -06002605 Elf_Shdr *symsect = info->sechdrs + info->index.sym;
2606 Elf_Shdr *strsect = info->sechdrs + info->index.str;
Jan Beulich4a496222009-07-06 14:50:42 +01002607 const Elf_Sym *src;
Satoru Takeuchi54523ec2012-12-05 12:29:04 +10302608 unsigned int i, nsrc, ndst, strtab_size = 0;
Jan Beulich4a496222009-07-06 14:50:42 +01002609
2610 /* Put symbol section at end of init part of module. */
2611 symsect->sh_flags |= SHF_ALLOC;
Rusty Russell7523e4d2015-11-26 09:44:08 +10302612 symsect->sh_entsize = get_offset(mod, &mod->init_layout.size, symsect,
Rusty Russell49668682010-08-05 12:59:10 -06002613 info->index.sym) | INIT_OFFSET_MASK;
Jim Cromie5e124162011-12-06 12:11:31 -07002614 pr_debug("\t%s\n", info->secstrings + symsect->sh_name);
Jan Beulich4a496222009-07-06 14:50:42 +01002615
Rusty Russell49668682010-08-05 12:59:10 -06002616 src = (void *)info->hdr + symsect->sh_offset;
Jan Beulich4a496222009-07-06 14:50:42 +01002617 nsrc = symsect->sh_size / sizeof(*src);
Kevin Cernekee70b1e9162011-11-12 19:08:55 -08002618
Kevin Cernekee48fd1182012-01-13 09:32:14 +10302619 /* Compute total space required for the core symbols' strtab. */
Rusty Russell59ef28b2012-10-25 10:49:25 +10302620 for (ndst = i = 0; i < nsrc; i++) {
Jessica Yu1ce15ef2016-03-22 20:03:16 -04002621 if (i == 0 || is_livepatch_module(mod) ||
Miroslav Benese0224412015-11-26 13:18:06 +10302622 is_core_symbol(src+i, info->sechdrs, info->hdr->e_shnum,
2623 info->index.pcpu)) {
Rusty Russell59ef28b2012-10-25 10:49:25 +10302624 strtab_size += strlen(&info->strtab[src[i].st_name])+1;
Kevin Cernekee48fd1182012-01-13 09:32:14 +10302625 ndst++;
Jan Beulich554bdfe2009-07-06 14:51:44 +01002626 }
Rusty Russell59ef28b2012-10-25 10:49:25 +10302627 }
Jan Beulich4a496222009-07-06 14:50:42 +01002628
2629 /* Append room for core symbols at end of core part. */
Rusty Russell7523e4d2015-11-26 09:44:08 +10302630 info->symoffs = ALIGN(mod->core_layout.size, symsect->sh_addralign ?: 1);
2631 info->stroffs = mod->core_layout.size = info->symoffs + ndst * sizeof(Elf_Sym);
2632 mod->core_layout.size += strtab_size;
2633 mod->core_layout.size = debug_align(mod->core_layout.size);
Jan Beulich4a496222009-07-06 14:50:42 +01002634
Jan Beulich554bdfe2009-07-06 14:51:44 +01002635 /* Put string table section at end of init part of module. */
2636 strsect->sh_flags |= SHF_ALLOC;
Rusty Russell7523e4d2015-11-26 09:44:08 +10302637 strsect->sh_entsize = get_offset(mod, &mod->init_layout.size, strsect,
Rusty Russell49668682010-08-05 12:59:10 -06002638 info->index.str) | INIT_OFFSET_MASK;
Jim Cromie5e124162011-12-06 12:11:31 -07002639 pr_debug("\t%s\n", info->secstrings + strsect->sh_name);
Rusty Russell82440622016-02-03 16:55:26 +10302640
2641 /* We'll tack temporary mod_kallsyms on the end. */
2642 mod->init_layout.size = ALIGN(mod->init_layout.size,
2643 __alignof__(struct mod_kallsyms));
2644 info->mod_kallsyms_init_off = mod->init_layout.size;
2645 mod->init_layout.size += sizeof(struct mod_kallsyms);
2646 mod->init_layout.size = debug_align(mod->init_layout.size);
Jan Beulich4a496222009-07-06 14:50:42 +01002647}
2648
Rusty Russell82440622016-02-03 16:55:26 +10302649/*
2650 * We use the full symtab and strtab which layout_symtab arranged to
2651 * be appended to the init section. Later we switch to the cut-down
2652 * core-only ones.
2653 */
Rusty Russell811d66a2010-08-05 12:59:12 -06002654static void add_kallsyms(struct module *mod, const struct load_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002655{
Jan Beulich4a496222009-07-06 14:50:42 +01002656 unsigned int i, ndst;
2657 const Elf_Sym *src;
2658 Elf_Sym *dst;
Jan Beulich554bdfe2009-07-06 14:51:44 +01002659 char *s;
Rusty Russelleded41c2010-08-05 12:59:07 -06002660 Elf_Shdr *symsec = &info->sechdrs[info->index.sym];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002661
Rusty Russell82440622016-02-03 16:55:26 +10302662 /* Set up to point into init section. */
2663 mod->kallsyms = mod->init_layout.base + info->mod_kallsyms_init_off;
2664
2665 mod->kallsyms->symtab = (void *)symsec->sh_addr;
2666 mod->kallsyms->num_symtab = symsec->sh_size / sizeof(Elf_Sym);
Rusty Russell511ca6a2010-08-05 12:59:08 -06002667 /* Make sure we get permanent strtab: don't use info->strtab. */
Rusty Russell82440622016-02-03 16:55:26 +10302668 mod->kallsyms->strtab = (void *)info->sechdrs[info->index.str].sh_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002669
2670 /* Set types up while we still have access to sections. */
Rusty Russell82440622016-02-03 16:55:26 +10302671 for (i = 0; i < mod->kallsyms->num_symtab; i++)
2672 mod->kallsyms->symtab[i].st_info
2673 = elf_type(&mod->kallsyms->symtab[i], info);
Jan Beulich4a496222009-07-06 14:50:42 +01002674
Rusty Russell82440622016-02-03 16:55:26 +10302675 /* Now populate the cut down core kallsyms for after init. */
2676 mod->core_kallsyms.symtab = dst = mod->core_layout.base + info->symoffs;
2677 mod->core_kallsyms.strtab = s = mod->core_layout.base + info->stroffs;
2678 src = mod->kallsyms->symtab;
2679 for (ndst = i = 0; i < mod->kallsyms->num_symtab; i++) {
Jessica Yu1ce15ef2016-03-22 20:03:16 -04002680 if (i == 0 || is_livepatch_module(mod) ||
Miroslav Benese0224412015-11-26 13:18:06 +10302681 is_core_symbol(src+i, info->sechdrs, info->hdr->e_shnum,
2682 info->index.pcpu)) {
Rusty Russell59ef28b2012-10-25 10:49:25 +10302683 dst[ndst] = src[i];
Rusty Russell82440622016-02-03 16:55:26 +10302684 dst[ndst++].st_name = s - mod->core_kallsyms.strtab;
2685 s += strlcpy(s, &mod->kallsyms->strtab[src[i].st_name],
Rusty Russell59ef28b2012-10-25 10:49:25 +10302686 KSYM_NAME_LEN) + 1;
2687 }
Jan Beulich4a496222009-07-06 14:50:42 +01002688 }
Rusty Russell82440622016-02-03 16:55:26 +10302689 mod->core_kallsyms.num_symtab = ndst;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002690}
2691#else
Rusty Russell49668682010-08-05 12:59:10 -06002692static inline void layout_symtab(struct module *mod, struct load_info *info)
Jan Beulich4a496222009-07-06 14:50:42 +01002693{
2694}
Paul Mundt3ae91c22009-10-01 15:43:54 -07002695
Michał Mirosławabbce902010-09-20 01:58:08 +02002696static void add_kallsyms(struct module *mod, const struct load_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002697{
2698}
2699#endif /* CONFIG_KALLSYMS */
2700
Jason Barone9d376f2009-02-05 11:51:38 -05002701static void dynamic_debug_setup(struct _ddebug *debug, unsigned int num)
Rusty Russell5e458cc2008-10-22 10:00:13 -05002702{
Rusty Russell811d66a2010-08-05 12:59:12 -06002703 if (!debug)
2704 return;
Jason Barone9d376f2009-02-05 11:51:38 -05002705#ifdef CONFIG_DYNAMIC_DEBUG
2706 if (ddebug_add_module(debug, num, debug->modname))
Andrew Mortonbddb12b2013-11-12 15:11:28 -08002707 pr_err("dynamic debug error adding module: %s\n",
2708 debug->modname);
Jason Barone9d376f2009-02-05 11:51:38 -05002709#endif
Rusty Russell5e458cc2008-10-22 10:00:13 -05002710}
Jason Baron346e15b2008-08-12 16:46:19 -04002711
Yehuda Sadehff49d742010-07-03 13:07:35 +10002712static void dynamic_debug_remove(struct _ddebug *debug)
2713{
2714 if (debug)
2715 ddebug_remove_module(debug->modname);
2716}
2717
Jonas Bonn74e08fc2011-06-30 21:22:11 +02002718void * __weak module_alloc(unsigned long size)
2719{
Rusty Russell82fab442012-12-11 09:38:33 +10302720 return vmalloc_exec(size);
Jonas Bonn74e08fc2011-06-30 21:22:11 +02002721}
2722
Catalin Marinas4f2294b2009-06-11 13:23:20 +01002723#ifdef CONFIG_DEBUG_KMEMLEAK
Rusty Russell49668682010-08-05 12:59:10 -06002724static void kmemleak_load_module(const struct module *mod,
2725 const struct load_info *info)
Catalin Marinas4f2294b2009-06-11 13:23:20 +01002726{
2727 unsigned int i;
2728
2729 /* only scan the sections containing data */
Catalin Marinasc017b4b2009-10-28 13:33:09 +00002730 kmemleak_scan_area(mod, sizeof(struct module), GFP_KERNEL);
Catalin Marinas4f2294b2009-06-11 13:23:20 +01002731
Rusty Russell49668682010-08-05 12:59:10 -06002732 for (i = 1; i < info->hdr->e_shnum; i++) {
Steven Rostedt06c94942013-05-15 20:33:01 +01002733 /* Scan all writable sections that's not executable */
2734 if (!(info->sechdrs[i].sh_flags & SHF_ALLOC) ||
2735 !(info->sechdrs[i].sh_flags & SHF_WRITE) ||
2736 (info->sechdrs[i].sh_flags & SHF_EXECINSTR))
Catalin Marinas4f2294b2009-06-11 13:23:20 +01002737 continue;
2738
Rusty Russell49668682010-08-05 12:59:10 -06002739 kmemleak_scan_area((void *)info->sechdrs[i].sh_addr,
2740 info->sechdrs[i].sh_size, GFP_KERNEL);
Catalin Marinas4f2294b2009-06-11 13:23:20 +01002741 }
2742}
2743#else
Rusty Russell49668682010-08-05 12:59:10 -06002744static inline void kmemleak_load_module(const struct module *mod,
2745 const struct load_info *info)
Catalin Marinas4f2294b2009-06-11 13:23:20 +01002746{
2747}
2748#endif
2749
Rusty Russell106a4ee2012-09-26 10:09:40 +01002750#ifdef CONFIG_MODULE_SIG
Ben Hutchingsbca014c2016-04-28 09:24:01 +09302751static int module_sig_check(struct load_info *info, int flags)
Rusty Russell106a4ee2012-09-26 10:09:40 +01002752{
2753 int err = -ENOKEY;
Kees Cook34e11692012-10-16 07:31:07 +10302754 const unsigned long markerlen = sizeof(MODULE_SIG_STRING) - 1;
2755 const void *mod = info->hdr;
Rusty Russell106a4ee2012-09-26 10:09:40 +01002756
Ben Hutchingsbca014c2016-04-28 09:24:01 +09302757 /*
2758 * Require flags == 0, as a module with version information
2759 * removed is no longer the module that was signed
2760 */
2761 if (flags == 0 &&
2762 info->len > markerlen &&
Kees Cook34e11692012-10-16 07:31:07 +10302763 memcmp(mod + info->len - markerlen, MODULE_SIG_STRING, markerlen) == 0) {
David Howellscaabe242012-10-20 01:19:29 +01002764 /* We truncate the module to discard the signature */
Kees Cook34e11692012-10-16 07:31:07 +10302765 info->len -= markerlen;
2766 err = mod_verify_sig(mod, &info->len);
Rusty Russell106a4ee2012-09-26 10:09:40 +01002767 }
2768
2769 if (!err) {
2770 info->sig_ok = true;
2771 return 0;
2772 }
2773
2774 /* Not having a signature is only an error if we're strict. */
2775 if (err == -ENOKEY && !sig_enforce)
2776 err = 0;
2777
2778 return err;
2779}
2780#else /* !CONFIG_MODULE_SIG */
Ben Hutchingsbca014c2016-04-28 09:24:01 +09302781static int module_sig_check(struct load_info *info, int flags)
Rusty Russell106a4ee2012-09-26 10:09:40 +01002782{
2783 return 0;
2784}
2785#endif /* !CONFIG_MODULE_SIG */
2786
Kees Cook34e11692012-10-16 07:31:07 +10302787/* Sanity checks against invalid binaries, wrong arch, weird elf version. */
2788static int elf_header_check(struct load_info *info)
Rusty Russell40dd2562010-08-05 12:59:03 -06002789{
Kees Cook34e11692012-10-16 07:31:07 +10302790 if (info->len < sizeof(*(info->hdr)))
Rusty Russell40dd2562010-08-05 12:59:03 -06002791 return -ENOEXEC;
2792
Kees Cook34e11692012-10-16 07:31:07 +10302793 if (memcmp(info->hdr->e_ident, ELFMAG, SELFMAG) != 0
2794 || info->hdr->e_type != ET_REL
2795 || !elf_check_arch(info->hdr)
2796 || info->hdr->e_shentsize != sizeof(Elf_Shdr))
2797 return -ENOEXEC;
2798
2799 if (info->hdr->e_shoff >= info->len
2800 || (info->hdr->e_shnum * sizeof(Elf_Shdr) >
2801 info->len - info->hdr->e_shoff))
2802 return -ENOEXEC;
2803
2804 return 0;
2805}
2806
Linus Torvalds3afe9f82015-04-07 10:33:49 -07002807#define COPY_CHUNK_SIZE (16*PAGE_SIZE)
2808
2809static int copy_chunked_from_user(void *dst, const void __user *usrc, unsigned long len)
2810{
2811 do {
2812 unsigned long n = min(len, COPY_CHUNK_SIZE);
2813
2814 if (copy_from_user(dst, usrc, n) != 0)
2815 return -EFAULT;
2816 cond_resched();
2817 dst += n;
2818 usrc += n;
2819 len -= n;
2820 } while (len);
2821 return 0;
2822}
2823
Jessica Yu1ce15ef2016-03-22 20:03:16 -04002824#ifdef CONFIG_LIVEPATCH
Josh Poimboeuf2992ef22016-08-25 10:04:45 -05002825static int check_modinfo_livepatch(struct module *mod, struct load_info *info)
Jessica Yu1ce15ef2016-03-22 20:03:16 -04002826{
Josh Poimboeuf2992ef22016-08-25 10:04:45 -05002827 if (get_modinfo(info, "livepatch")) {
2828 mod->klp = true;
2829 add_taint_module(mod, TAINT_LIVEPATCH, LOCKDEP_STILL_OK);
Joe Lawrence7598d162017-01-12 11:57:44 -05002830 pr_notice_once("%s: tainting kernel with TAINT_LIVEPATCH\n",
2831 mod->name);
Josh Poimboeuf2992ef22016-08-25 10:04:45 -05002832 }
Jessica Yu1ce15ef2016-03-22 20:03:16 -04002833
2834 return 0;
2835}
2836#else /* !CONFIG_LIVEPATCH */
Josh Poimboeuf2992ef22016-08-25 10:04:45 -05002837static int check_modinfo_livepatch(struct module *mod, struct load_info *info)
Jessica Yu1ce15ef2016-03-22 20:03:16 -04002838{
2839 if (get_modinfo(info, "livepatch")) {
2840 pr_err("%s: module is marked as livepatch module, but livepatch support is disabled",
2841 mod->name);
2842 return -ENOEXEC;
2843 }
2844
2845 return 0;
2846}
2847#endif /* CONFIG_LIVEPATCH */
2848
Kees Cook34e11692012-10-16 07:31:07 +10302849/* Sets info->hdr and info->len. */
2850static int copy_module_from_user(const void __user *umod, unsigned long len,
2851 struct load_info *info)
2852{
Kees Cook2e72d512012-10-16 07:32:07 +10302853 int err;
2854
Kees Cook34e11692012-10-16 07:31:07 +10302855 info->len = len;
2856 if (info->len < sizeof(*(info->hdr)))
Rusty Russell40dd2562010-08-05 12:59:03 -06002857 return -ENOEXEC;
2858
Mimi Zohara1db7422015-12-30 07:35:30 -05002859 err = security_kernel_read_file(NULL, READING_MODULE);
Kees Cook2e72d512012-10-16 07:32:07 +10302860 if (err)
2861 return err;
2862
Rusty Russell40dd2562010-08-05 12:59:03 -06002863 /* Suck in entire file: we'll want most of it. */
Kirill A. Shutemovcc9e6052015-03-24 12:31:40 +10302864 info->hdr = __vmalloc(info->len,
Michal Hocko19809c22017-05-08 15:57:44 -07002865 GFP_KERNEL | __GFP_NOWARN, PAGE_KERNEL);
Kees Cook34e11692012-10-16 07:31:07 +10302866 if (!info->hdr)
Rusty Russell40dd2562010-08-05 12:59:03 -06002867 return -ENOMEM;
2868
Linus Torvalds3afe9f82015-04-07 10:33:49 -07002869 if (copy_chunked_from_user(info->hdr, umod, info->len) != 0) {
Kees Cook34e11692012-10-16 07:31:07 +10302870 vfree(info->hdr);
2871 return -EFAULT;
Rusty Russell40dd2562010-08-05 12:59:03 -06002872 }
2873
Rusty Russell40dd2562010-08-05 12:59:03 -06002874 return 0;
Kees Cook34e11692012-10-16 07:31:07 +10302875}
Rusty Russell40dd2562010-08-05 12:59:03 -06002876
Rusty Russelld9131882010-08-05 12:59:08 -06002877static void free_copy(struct load_info *info)
2878{
Rusty Russelld9131882010-08-05 12:59:08 -06002879 vfree(info->hdr);
2880}
2881
Rusty Russell2f3238a2012-10-22 18:09:41 +10302882static int rewrite_section_headers(struct load_info *info, int flags)
Rusty Russell8b5f61a2010-08-05 12:59:06 -06002883{
2884 unsigned int i;
2885
2886 /* This should always be true, but let's be sure. */
2887 info->sechdrs[0].sh_addr = 0;
2888
2889 for (i = 1; i < info->hdr->e_shnum; i++) {
2890 Elf_Shdr *shdr = &info->sechdrs[i];
2891 if (shdr->sh_type != SHT_NOBITS
2892 && info->len < shdr->sh_offset + shdr->sh_size) {
Andrew Mortonbddb12b2013-11-12 15:11:28 -08002893 pr_err("Module len %lu truncated\n", info->len);
Rusty Russell8b5f61a2010-08-05 12:59:06 -06002894 return -ENOEXEC;
2895 }
2896
2897 /* Mark all sections sh_addr with their address in the
2898 temporary image. */
2899 shdr->sh_addr = (size_t)info->hdr + shdr->sh_offset;
2900
2901#ifndef CONFIG_MODULE_UNLOAD
2902 /* Don't load .exit sections */
2903 if (strstarts(info->secstrings+shdr->sh_name, ".exit"))
2904 shdr->sh_flags &= ~(unsigned long)SHF_ALLOC;
2905#endif
Rusty Russell8b5f61a2010-08-05 12:59:06 -06002906 }
Rusty Russelld6df72a2010-08-05 12:59:07 -06002907
2908 /* Track but don't keep modinfo and version sections. */
Rusty Russell2f3238a2012-10-22 18:09:41 +10302909 if (flags & MODULE_INIT_IGNORE_MODVERSIONS)
2910 info->index.vers = 0; /* Pretend no __versions section! */
2911 else
2912 info->index.vers = find_sec(info, "__versions");
Rusty Russell49668682010-08-05 12:59:10 -06002913 info->index.info = find_sec(info, ".modinfo");
Rusty Russelld6df72a2010-08-05 12:59:07 -06002914 info->sechdrs[info->index.info].sh_flags &= ~(unsigned long)SHF_ALLOC;
2915 info->sechdrs[info->index.vers].sh_flags &= ~(unsigned long)SHF_ALLOC;
Rusty Russell8b5f61a2010-08-05 12:59:06 -06002916 return 0;
2917}
2918
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002919/*
2920 * Set up our basic convenience variables (pointers to section headers,
2921 * search for module section index etc), and do some basic section
2922 * verification.
2923 *
2924 * Return the temporary module pointer (we'll replace it with the final
2925 * one when we move the module sections around).
2926 */
Rusty Russell2f3238a2012-10-22 18:09:41 +10302927static struct module *setup_load_info(struct load_info *info, int flags)
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002928{
2929 unsigned int i;
Rusty Russell8b5f61a2010-08-05 12:59:06 -06002930 int err;
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002931 struct module *mod;
2932
2933 /* Set up the convenience variables */
2934 info->sechdrs = (void *)info->hdr + info->hdr->e_shoff;
Rusty Russell8b5f61a2010-08-05 12:59:06 -06002935 info->secstrings = (void *)info->hdr
2936 + info->sechdrs[info->hdr->e_shstrndx].sh_offset;
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002937
Rusty Russell2f3238a2012-10-22 18:09:41 +10302938 err = rewrite_section_headers(info, flags);
Rusty Russell8b5f61a2010-08-05 12:59:06 -06002939 if (err)
2940 return ERR_PTR(err);
2941
2942 /* Find internal symbols and strings. */
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002943 for (i = 1; i < info->hdr->e_shnum; i++) {
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002944 if (info->sechdrs[i].sh_type == SHT_SYMTAB) {
2945 info->index.sym = i;
2946 info->index.str = info->sechdrs[i].sh_link;
Rusty Russell8b5f61a2010-08-05 12:59:06 -06002947 info->strtab = (char *)info->hdr
2948 + info->sechdrs[info->index.str].sh_offset;
2949 break;
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002950 }
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002951 }
2952
Rusty Russell49668682010-08-05 12:59:10 -06002953 info->index.mod = find_sec(info, ".gnu.linkonce.this_module");
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002954 if (!info->index.mod) {
Andrew Mortonbddb12b2013-11-12 15:11:28 -08002955 pr_warn("No module found in object\n");
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002956 return ERR_PTR(-ENOEXEC);
2957 }
2958 /* This is temporary: point mod into copy of data. */
2959 mod = (void *)info->sechdrs[info->index.mod].sh_addr;
2960
2961 if (info->index.sym == 0) {
Andrew Mortonbddb12b2013-11-12 15:11:28 -08002962 pr_warn("%s: module has no symbols (stripped?)\n", mod->name);
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002963 return ERR_PTR(-ENOEXEC);
2964 }
2965
Rusty Russell49668682010-08-05 12:59:10 -06002966 info->index.pcpu = find_pcpusec(info);
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002967
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002968 /* Check module struct version now, before we try to use module. */
2969 if (!check_modstruct_version(info->sechdrs, info->index.vers, mod))
2970 return ERR_PTR(-ENOEXEC);
2971
2972 return mod;
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002973}
2974
Rusty Russell2f3238a2012-10-22 18:09:41 +10302975static int check_modinfo(struct module *mod, struct load_info *info, int flags)
Rusty Russell40dd2562010-08-05 12:59:03 -06002976{
Rusty Russell49668682010-08-05 12:59:10 -06002977 const char *modmagic = get_modinfo(info, "vermagic");
Rusty Russell40dd2562010-08-05 12:59:03 -06002978 int err;
2979
Rusty Russell2f3238a2012-10-22 18:09:41 +10302980 if (flags & MODULE_INIT_IGNORE_VERMAGIC)
2981 modmagic = NULL;
2982
Rusty Russell40dd2562010-08-05 12:59:03 -06002983 /* This is allowed: modprobe --force will invalidate it. */
2984 if (!modmagic) {
2985 err = try_to_force_load(mod, "bad vermagic");
2986 if (err)
2987 return err;
Rusty Russell49668682010-08-05 12:59:10 -06002988 } else if (!same_magic(modmagic, vermagic, info->index.vers)) {
Andrew Mortonbddb12b2013-11-12 15:11:28 -08002989 pr_err("%s: version magic '%s' should be '%s'\n",
Rusty Russell40dd2562010-08-05 12:59:03 -06002990 mod->name, modmagic, vermagic);
2991 return -ENOEXEC;
2992 }
2993
Libor Pechacek3205c362016-04-13 11:06:12 +09302994 if (!get_modinfo(info, "intree")) {
2995 if (!test_taint(TAINT_OOT_MODULE))
2996 pr_warn("%s: loading out-of-tree module taints kernel.\n",
2997 mod->name);
Rusty Russell373d4d02013-01-21 17:17:39 +10302998 add_taint_module(mod, TAINT_OOT_MODULE, LOCKDEP_STILL_OK);
Libor Pechacek3205c362016-04-13 11:06:12 +09302999 }
Ben Hutchings2449b8b2011-10-24 15:12:28 +02003000
Rusty Russell49668682010-08-05 12:59:10 -06003001 if (get_modinfo(info, "staging")) {
Rusty Russell373d4d02013-01-21 17:17:39 +10303002 add_taint_module(mod, TAINT_CRAP, LOCKDEP_STILL_OK);
Andrew Mortonbddb12b2013-11-12 15:11:28 -08003003 pr_warn("%s: module is from the staging directory, the quality "
3004 "is unknown, you have been warned.\n", mod->name);
Rusty Russell40dd2562010-08-05 12:59:03 -06003005 }
Rusty Russell22e268e2010-08-05 12:59:05 -06003006
Josh Poimboeuf2992ef22016-08-25 10:04:45 -05003007 err = check_modinfo_livepatch(mod, info);
Jessica Yu1ce15ef2016-03-22 20:03:16 -04003008 if (err)
3009 return err;
3010
Rusty Russell22e268e2010-08-05 12:59:05 -06003011 /* Set up license info based on the info section */
Rusty Russell49668682010-08-05 12:59:10 -06003012 set_license(mod, get_modinfo(info, "license"));
Rusty Russell22e268e2010-08-05 12:59:05 -06003013
Rusty Russell40dd2562010-08-05 12:59:03 -06003014 return 0;
3015}
3016
Frantisek Hrbataeb3057d2013-10-14 18:08:46 +10303017static int find_module_sections(struct module *mod, struct load_info *info)
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06003018{
Rusty Russell49668682010-08-05 12:59:10 -06003019 mod->kp = section_objs(info, "__param",
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06003020 sizeof(*mod->kp), &mod->num_kp);
Rusty Russell49668682010-08-05 12:59:10 -06003021 mod->syms = section_objs(info, "__ksymtab",
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06003022 sizeof(*mod->syms), &mod->num_syms);
Rusty Russell49668682010-08-05 12:59:10 -06003023 mod->crcs = section_addr(info, "__kcrctab");
3024 mod->gpl_syms = section_objs(info, "__ksymtab_gpl",
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06003025 sizeof(*mod->gpl_syms),
3026 &mod->num_gpl_syms);
Rusty Russell49668682010-08-05 12:59:10 -06003027 mod->gpl_crcs = section_addr(info, "__kcrctab_gpl");
3028 mod->gpl_future_syms = section_objs(info,
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06003029 "__ksymtab_gpl_future",
3030 sizeof(*mod->gpl_future_syms),
3031 &mod->num_gpl_future_syms);
Rusty Russell49668682010-08-05 12:59:10 -06003032 mod->gpl_future_crcs = section_addr(info, "__kcrctab_gpl_future");
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06003033
3034#ifdef CONFIG_UNUSED_SYMBOLS
Rusty Russell49668682010-08-05 12:59:10 -06003035 mod->unused_syms = section_objs(info, "__ksymtab_unused",
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06003036 sizeof(*mod->unused_syms),
3037 &mod->num_unused_syms);
Rusty Russell49668682010-08-05 12:59:10 -06003038 mod->unused_crcs = section_addr(info, "__kcrctab_unused");
3039 mod->unused_gpl_syms = section_objs(info, "__ksymtab_unused_gpl",
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06003040 sizeof(*mod->unused_gpl_syms),
3041 &mod->num_unused_gpl_syms);
Rusty Russell49668682010-08-05 12:59:10 -06003042 mod->unused_gpl_crcs = section_addr(info, "__kcrctab_unused_gpl");
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06003043#endif
3044#ifdef CONFIG_CONSTRUCTORS
Rusty Russell49668682010-08-05 12:59:10 -06003045 mod->ctors = section_objs(info, ".ctors",
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06003046 sizeof(*mod->ctors), &mod->num_ctors);
Frantisek Hrbataeb3057d2013-10-14 18:08:46 +10303047 if (!mod->ctors)
3048 mod->ctors = section_objs(info, ".init_array",
3049 sizeof(*mod->ctors), &mod->num_ctors);
3050 else if (find_sec(info, ".init_array")) {
3051 /*
3052 * This shouldn't happen with same compiler and binutils
3053 * building all parts of the module.
3054 */
Ionut Alexa6da0b562014-11-10 09:31:29 +10303055 pr_warn("%s: has both .ctors and .init_array.\n",
Frantisek Hrbataeb3057d2013-10-14 18:08:46 +10303056 mod->name);
3057 return -EINVAL;
3058 }
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06003059#endif
3060
3061#ifdef CONFIG_TRACEPOINTS
Mathieu Desnoyers65498642011-01-26 17:26:22 -05003062 mod->tracepoints_ptrs = section_objs(info, "__tracepoints_ptrs",
3063 sizeof(*mod->tracepoints_ptrs),
3064 &mod->num_tracepoints);
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06003065#endif
Jason Baronbf5438fc2010-09-17 11:09:00 -04003066#ifdef HAVE_JUMP_LABEL
3067 mod->jump_entries = section_objs(info, "__jump_table",
3068 sizeof(*mod->jump_entries),
3069 &mod->num_jump_entries);
3070#endif
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06003071#ifdef CONFIG_EVENT_TRACING
Rusty Russell49668682010-08-05 12:59:10 -06003072 mod->trace_events = section_objs(info, "_ftrace_events",
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06003073 sizeof(*mod->trace_events),
3074 &mod->num_trace_events);
Jeremy Linton99be6472017-05-31 16:56:44 -05003075 mod->trace_evals = section_objs(info, "_ftrace_eval_map",
3076 sizeof(*mod->trace_evals),
3077 &mod->num_trace_evals);
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06003078#endif
Steven Rostedt13b9b6e2010-11-10 22:19:24 -05003079#ifdef CONFIG_TRACING
3080 mod->trace_bprintk_fmt_start = section_objs(info, "__trace_printk_fmt",
3081 sizeof(*mod->trace_bprintk_fmt_start),
3082 &mod->num_trace_bprintk_fmt);
Steven Rostedt13b9b6e2010-11-10 22:19:24 -05003083#endif
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06003084#ifdef CONFIG_FTRACE_MCOUNT_RECORD
3085 /* sechdrs[0].sh_size is always zero */
Rusty Russell49668682010-08-05 12:59:10 -06003086 mod->ftrace_callsites = section_objs(info, "__mcount_loc",
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06003087 sizeof(*mod->ftrace_callsites),
3088 &mod->num_ftrace_callsites);
3089#endif
Rusty Russell22e268e2010-08-05 12:59:05 -06003090
Rusty Russell811d66a2010-08-05 12:59:12 -06003091 mod->extable = section_objs(info, "__ex_table",
3092 sizeof(*mod->extable), &mod->num_exentries);
3093
Rusty Russell49668682010-08-05 12:59:10 -06003094 if (section_addr(info, "__obsparm"))
Andrew Mortonbddb12b2013-11-12 15:11:28 -08003095 pr_warn("%s: Ignoring obsolete parameters\n", mod->name);
Rusty Russell811d66a2010-08-05 12:59:12 -06003096
3097 info->debug = section_objs(info, "__verbose",
3098 sizeof(*info->debug), &info->num_debug);
Frantisek Hrbataeb3057d2013-10-14 18:08:46 +10303099
3100 return 0;
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06003101}
3102
Rusty Russell49668682010-08-05 12:59:10 -06003103static int move_module(struct module *mod, struct load_info *info)
Linus Torvalds65b8a9b2010-08-05 12:59:02 -06003104{
3105 int i;
3106 void *ptr;
3107
3108 /* Do the allocs. */
Rusty Russell7523e4d2015-11-26 09:44:08 +10303109 ptr = module_alloc(mod->core_layout.size);
Linus Torvalds65b8a9b2010-08-05 12:59:02 -06003110 /*
3111 * The pointer to this block is stored in the module structure
3112 * which is inside the block. Just mark it as not being a
3113 * leak.
3114 */
3115 kmemleak_not_leak(ptr);
3116 if (!ptr)
Rusty Russelld9131882010-08-05 12:59:08 -06003117 return -ENOMEM;
Linus Torvalds65b8a9b2010-08-05 12:59:02 -06003118
Rusty Russell7523e4d2015-11-26 09:44:08 +10303119 memset(ptr, 0, mod->core_layout.size);
3120 mod->core_layout.base = ptr;
Linus Torvalds65b8a9b2010-08-05 12:59:02 -06003121
Rusty Russell7523e4d2015-11-26 09:44:08 +10303122 if (mod->init_layout.size) {
3123 ptr = module_alloc(mod->init_layout.size);
Rusty Russell82fab442012-12-11 09:38:33 +10303124 /*
3125 * The pointer to this block is stored in the module structure
3126 * which is inside the block. This block doesn't need to be
3127 * scanned as it contains data and code that will be freed
3128 * after the module is initialized.
3129 */
3130 kmemleak_ignore(ptr);
3131 if (!ptr) {
Rusty Russell7523e4d2015-11-26 09:44:08 +10303132 module_memfree(mod->core_layout.base);
Rusty Russell82fab442012-12-11 09:38:33 +10303133 return -ENOMEM;
3134 }
Rusty Russell7523e4d2015-11-26 09:44:08 +10303135 memset(ptr, 0, mod->init_layout.size);
3136 mod->init_layout.base = ptr;
Rusty Russell82fab442012-12-11 09:38:33 +10303137 } else
Rusty Russell7523e4d2015-11-26 09:44:08 +10303138 mod->init_layout.base = NULL;
Linus Torvalds65b8a9b2010-08-05 12:59:02 -06003139
3140 /* Transfer each section which specifies SHF_ALLOC */
Jim Cromie5e124162011-12-06 12:11:31 -07003141 pr_debug("final section addresses:\n");
Rusty Russell49668682010-08-05 12:59:10 -06003142 for (i = 0; i < info->hdr->e_shnum; i++) {
Linus Torvalds65b8a9b2010-08-05 12:59:02 -06003143 void *dest;
Rusty Russell49668682010-08-05 12:59:10 -06003144 Elf_Shdr *shdr = &info->sechdrs[i];
Linus Torvalds65b8a9b2010-08-05 12:59:02 -06003145
Rusty Russell49668682010-08-05 12:59:10 -06003146 if (!(shdr->sh_flags & SHF_ALLOC))
Linus Torvalds65b8a9b2010-08-05 12:59:02 -06003147 continue;
3148
Rusty Russell49668682010-08-05 12:59:10 -06003149 if (shdr->sh_entsize & INIT_OFFSET_MASK)
Rusty Russell7523e4d2015-11-26 09:44:08 +10303150 dest = mod->init_layout.base
Rusty Russell49668682010-08-05 12:59:10 -06003151 + (shdr->sh_entsize & ~INIT_OFFSET_MASK);
Linus Torvalds65b8a9b2010-08-05 12:59:02 -06003152 else
Rusty Russell7523e4d2015-11-26 09:44:08 +10303153 dest = mod->core_layout.base + shdr->sh_entsize;
Linus Torvalds65b8a9b2010-08-05 12:59:02 -06003154
Rusty Russell49668682010-08-05 12:59:10 -06003155 if (shdr->sh_type != SHT_NOBITS)
3156 memcpy(dest, (void *)shdr->sh_addr, shdr->sh_size);
Linus Torvalds65b8a9b2010-08-05 12:59:02 -06003157 /* Update sh_addr to point to copy in image. */
Rusty Russell49668682010-08-05 12:59:10 -06003158 shdr->sh_addr = (unsigned long)dest;
Jim Cromie5e124162011-12-06 12:11:31 -07003159 pr_debug("\t0x%lx %s\n",
3160 (long)shdr->sh_addr, info->secstrings + shdr->sh_name);
Linus Torvalds65b8a9b2010-08-05 12:59:02 -06003161 }
Rusty Russelld9131882010-08-05 12:59:08 -06003162
3163 return 0;
Linus Torvalds65b8a9b2010-08-05 12:59:02 -06003164}
3165
Rusty Russell49668682010-08-05 12:59:10 -06003166static int check_module_license_and_versions(struct module *mod)
Rusty Russell22e268e2010-08-05 12:59:05 -06003167{
Libor Pechacek3205c362016-04-13 11:06:12 +09303168 int prev_taint = test_taint(TAINT_PROPRIETARY_MODULE);
3169
Rusty Russell22e268e2010-08-05 12:59:05 -06003170 /*
3171 * ndiswrapper is under GPL by itself, but loads proprietary modules.
3172 * Don't use add_taint_module(), as it would prevent ndiswrapper from
3173 * using GPL-only symbols it needs.
3174 */
3175 if (strcmp(mod->name, "ndiswrapper") == 0)
Rusty Russell373d4d02013-01-21 17:17:39 +10303176 add_taint(TAINT_PROPRIETARY_MODULE, LOCKDEP_NOW_UNRELIABLE);
Rusty Russell22e268e2010-08-05 12:59:05 -06003177
3178 /* driverloader was caught wrongly pretending to be under GPL */
3179 if (strcmp(mod->name, "driverloader") == 0)
Rusty Russell373d4d02013-01-21 17:17:39 +10303180 add_taint_module(mod, TAINT_PROPRIETARY_MODULE,
3181 LOCKDEP_NOW_UNRELIABLE);
Rusty Russell22e268e2010-08-05 12:59:05 -06003182
Matthew Garrettc99af372012-06-22 13:49:31 -04003183 /* lve claims to be GPL but upstream won't provide source */
3184 if (strcmp(mod->name, "lve") == 0)
Rusty Russell373d4d02013-01-21 17:17:39 +10303185 add_taint_module(mod, TAINT_PROPRIETARY_MODULE,
3186 LOCKDEP_NOW_UNRELIABLE);
Matthew Garrettc99af372012-06-22 13:49:31 -04003187
Libor Pechacek3205c362016-04-13 11:06:12 +09303188 if (!prev_taint && test_taint(TAINT_PROPRIETARY_MODULE))
3189 pr_warn("%s: module license taints kernel.\n", mod->name);
3190
Rusty Russell22e268e2010-08-05 12:59:05 -06003191#ifdef CONFIG_MODVERSIONS
3192 if ((mod->num_syms && !mod->crcs)
3193 || (mod->num_gpl_syms && !mod->gpl_crcs)
3194 || (mod->num_gpl_future_syms && !mod->gpl_future_crcs)
3195#ifdef CONFIG_UNUSED_SYMBOLS
3196 || (mod->num_unused_syms && !mod->unused_crcs)
3197 || (mod->num_unused_gpl_syms && !mod->unused_gpl_crcs)
3198#endif
3199 ) {
3200 return try_to_force_load(mod,
3201 "no versions for exported symbols");
3202 }
3203#endif
3204 return 0;
3205}
3206
3207static void flush_module_icache(const struct module *mod)
3208{
3209 mm_segment_t old_fs;
3210
3211 /* flush the icache in correct context */
3212 old_fs = get_fs();
3213 set_fs(KERNEL_DS);
3214
3215 /*
3216 * Flush the instruction cache, since we've played with text.
3217 * Do it before processing of module parameters, so the module
3218 * can provide parameter accessor functions of its own.
3219 */
Rusty Russell7523e4d2015-11-26 09:44:08 +10303220 if (mod->init_layout.base)
3221 flush_icache_range((unsigned long)mod->init_layout.base,
3222 (unsigned long)mod->init_layout.base
3223 + mod->init_layout.size);
3224 flush_icache_range((unsigned long)mod->core_layout.base,
3225 (unsigned long)mod->core_layout.base + mod->core_layout.size);
Rusty Russell22e268e2010-08-05 12:59:05 -06003226
3227 set_fs(old_fs);
3228}
3229
Jonas Bonn74e08fc2011-06-30 21:22:11 +02003230int __weak module_frob_arch_sections(Elf_Ehdr *hdr,
3231 Elf_Shdr *sechdrs,
3232 char *secstrings,
3233 struct module *mod)
3234{
3235 return 0;
3236}
3237
Prarit Bhargavabe7de5f2016-07-21 15:37:56 +09303238/* module_blacklist is a comma-separated list of module names */
3239static char *module_blacklist;
3240static bool blacklisted(char *module_name)
3241{
3242 const char *p;
3243 size_t len;
3244
3245 if (!module_blacklist)
3246 return false;
3247
3248 for (p = module_blacklist; *p; p += len) {
3249 len = strcspn(p, ",");
3250 if (strlen(module_name) == len && !memcmp(module_name, p, len))
3251 return true;
3252 if (p[len] == ',')
3253 len++;
3254 }
3255 return false;
3256}
3257core_param(module_blacklist, module_blacklist, charp, 0400);
3258
Rusty Russell2f3238a2012-10-22 18:09:41 +10303259static struct module *layout_and_allocate(struct load_info *info, int flags)
Rusty Russelld9131882010-08-05 12:59:08 -06003260{
3261 /* Module within temporary copy. */
3262 struct module *mod;
Jessica Yu444d13f2016-07-27 12:06:21 +09303263 unsigned int ndx;
Rusty Russelld9131882010-08-05 12:59:08 -06003264 int err;
3265
Rusty Russell2f3238a2012-10-22 18:09:41 +10303266 mod = setup_load_info(info, flags);
Rusty Russelld9131882010-08-05 12:59:08 -06003267 if (IS_ERR(mod))
3268 return mod;
3269
Prarit Bhargavabe7de5f2016-07-21 15:37:56 +09303270 if (blacklisted(mod->name))
3271 return ERR_PTR(-EPERM);
3272
Rusty Russell2f3238a2012-10-22 18:09:41 +10303273 err = check_modinfo(mod, info, flags);
Rusty Russelld9131882010-08-05 12:59:08 -06003274 if (err)
3275 return ERR_PTR(err);
3276
3277 /* Allow arches to frob section contents and sizes. */
Rusty Russell49668682010-08-05 12:59:10 -06003278 err = module_frob_arch_sections(info->hdr, info->sechdrs,
3279 info->secstrings, mod);
Rusty Russelld9131882010-08-05 12:59:08 -06003280 if (err < 0)
Rusty Russell8d8022e2013-07-03 10:06:28 +09303281 return ERR_PTR(err);
Rusty Russelld9131882010-08-05 12:59:08 -06003282
Rusty Russell8d8022e2013-07-03 10:06:28 +09303283 /* We will do a special allocation for per-cpu sections later. */
3284 info->sechdrs[info->index.pcpu].sh_flags &= ~(unsigned long)SHF_ALLOC;
Rusty Russelld9131882010-08-05 12:59:08 -06003285
Jessica Yu444d13f2016-07-27 12:06:21 +09303286 /*
3287 * Mark ro_after_init section with SHF_RO_AFTER_INIT so that
3288 * layout_sections() can put it in the right place.
3289 * Note: ro_after_init sections also have SHF_{WRITE,ALLOC} set.
3290 */
3291 ndx = find_sec(info, ".data..ro_after_init");
3292 if (ndx)
3293 info->sechdrs[ndx].sh_flags |= SHF_RO_AFTER_INIT;
3294
Rusty Russelld9131882010-08-05 12:59:08 -06003295 /* Determine total sizes, and put offsets in sh_entsize. For now
3296 this is done generically; there doesn't appear to be any
3297 special cases for the architectures. */
Rusty Russell49668682010-08-05 12:59:10 -06003298 layout_sections(mod, info);
Rusty Russell49668682010-08-05 12:59:10 -06003299 layout_symtab(mod, info);
Rusty Russelld9131882010-08-05 12:59:08 -06003300
3301 /* Allocate and move to the final place */
Rusty Russell49668682010-08-05 12:59:10 -06003302 err = move_module(mod, info);
Rusty Russelld9131882010-08-05 12:59:08 -06003303 if (err)
Rusty Russell8d8022e2013-07-03 10:06:28 +09303304 return ERR_PTR(err);
Rusty Russelld9131882010-08-05 12:59:08 -06003305
3306 /* Module has been copied to its final place now: return it. */
3307 mod = (void *)info->sechdrs[info->index.mod].sh_addr;
Rusty Russell49668682010-08-05 12:59:10 -06003308 kmemleak_load_module(mod, info);
Rusty Russelld9131882010-08-05 12:59:08 -06003309 return mod;
Rusty Russelld9131882010-08-05 12:59:08 -06003310}
3311
3312/* mod is no longer valid after this! */
3313static void module_deallocate(struct module *mod, struct load_info *info)
3314{
Rusty Russelld9131882010-08-05 12:59:08 -06003315 percpu_modfree(mod);
Rusty Russelld453cde2015-01-20 09:07:04 +10303316 module_arch_freeing_init(mod);
Rusty Russell7523e4d2015-11-26 09:44:08 +10303317 module_memfree(mod->init_layout.base);
3318 module_memfree(mod->core_layout.base);
Rusty Russelld9131882010-08-05 12:59:08 -06003319}
3320
Jonas Bonn74e08fc2011-06-30 21:22:11 +02003321int __weak module_finalize(const Elf_Ehdr *hdr,
3322 const Elf_Shdr *sechdrs,
3323 struct module *me)
3324{
3325 return 0;
3326}
3327
Rusty Russell811d66a2010-08-05 12:59:12 -06003328static int post_relocation(struct module *mod, const struct load_info *info)
3329{
Rusty Russell51f3d0f2010-08-05 12:59:13 -06003330 /* Sort exception table now relocations are done. */
Rusty Russell811d66a2010-08-05 12:59:12 -06003331 sort_extable(mod->extable, mod->extable + mod->num_exentries);
3332
3333 /* Copy relocated percpu area over. */
3334 percpu_modcopy(mod, (void *)info->sechdrs[info->index.pcpu].sh_addr,
3335 info->sechdrs[info->index.pcpu].sh_size);
3336
Rusty Russell51f3d0f2010-08-05 12:59:13 -06003337 /* Setup kallsyms-specific fields. */
Rusty Russell811d66a2010-08-05 12:59:12 -06003338 add_kallsyms(mod, info);
3339
3340 /* Arch-specific module finalizing. */
3341 return module_finalize(info->hdr, info->sechdrs, mod);
3342}
3343
Rusty Russell9bb9c3b2012-09-28 14:31:03 +09303344/* Is this module of this name done loading? No locks held. */
3345static bool finished_loading(const char *name)
3346{
3347 struct module *mod;
3348 bool ret;
3349
Peter Zijlstra9cc019b2015-02-11 15:01:13 +10303350 /*
3351 * The module_mutex should not be a heavily contended lock;
3352 * if we get the occasional sleep here, we'll go an extra iteration
3353 * in the wait_event_interruptible(), which is harmless.
3354 */
3355 sched_annotate_sleep();
Rusty Russell9bb9c3b2012-09-28 14:31:03 +09303356 mutex_lock(&module_mutex);
Mathias Krause4f6de4d2013-07-02 15:35:11 +09303357 mod = find_module_all(name, strlen(name), true);
Rusty Russell0d21b0e2013-01-12 11:38:44 +10303358 ret = !mod || mod->state == MODULE_STATE_LIVE
3359 || mod->state == MODULE_STATE_GOING;
Rusty Russell9bb9c3b2012-09-28 14:31:03 +09303360 mutex_unlock(&module_mutex);
3361
3362 return ret;
3363}
3364
Peter Oberparleiterb99b87f2009-06-17 16:28:03 -07003365/* Call module constructors. */
3366static void do_mod_ctors(struct module *mod)
3367{
3368#ifdef CONFIG_CONSTRUCTORS
3369 unsigned long i;
3370
3371 for (i = 0; i < mod->num_ctors; i++)
3372 mod->ctors[i]();
3373#endif
3374}
3375
Rusty Russellc7496372015-01-20 09:07:05 +10303376/* For freeing module_init on success, in case kallsyms traversing */
3377struct mod_initfree {
3378 struct rcu_head rcu;
3379 void *module_init;
3380};
3381
3382static void do_free_init(struct rcu_head *head)
3383{
3384 struct mod_initfree *m = container_of(head, struct mod_initfree, rcu);
3385 module_memfree(m->module_init);
3386 kfree(m);
3387}
3388
Jan Kiszkabe02a182015-02-17 13:46:50 -08003389/*
3390 * This is where the real work happens.
3391 *
3392 * Keep it uninlined to provide a reliable breakpoint target, e.g. for the gdb
3393 * helper command 'lx-symbols'.
3394 */
3395static noinline int do_init_module(struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003396{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003397 int ret = 0;
Rusty Russellc7496372015-01-20 09:07:05 +10303398 struct mod_initfree *freeinit;
3399
3400 freeinit = kmalloc(sizeof(*freeinit), GFP_KERNEL);
3401 if (!freeinit) {
3402 ret = -ENOMEM;
3403 goto fail;
3404 }
Rusty Russell7523e4d2015-11-26 09:44:08 +10303405 freeinit->module_init = mod->init_layout.base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003406
Tejun Heo774a1222013-01-15 18:52:51 -08003407 /*
3408 * We want to find out whether @mod uses async during init. Clear
3409 * PF_USED_ASYNC. async_schedule*() will set it.
3410 */
3411 current->flags &= ~PF_USED_ASYNC;
3412
Peter Oberparleiterb99b87f2009-06-17 16:28:03 -07003413 do_mod_ctors(mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003414 /* Start the module */
3415 if (mod->init != NULL)
Arjan van de Ven59f94152008-07-30 12:49:02 -07003416 ret = do_one_initcall(mod->init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003417 if (ret < 0) {
Rusty Russellc7496372015-01-20 09:07:05 +10303418 goto fail_free_freeinit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003419 }
Alexey Dobriyane24e2e62008-03-10 11:43:53 -07003420 if (ret > 0) {
Andrew Mortonbddb12b2013-11-12 15:11:28 -08003421 pr_warn("%s: '%s'->init suspiciously returned %d, it should "
3422 "follow 0/-E convention\n"
3423 "%s: loading module anyway...\n",
3424 __func__, mod->name, ret, __func__);
Alexey Dobriyane24e2e62008-03-10 11:43:53 -07003425 dump_stack();
3426 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003427
Rusty Russell6f139092012-09-28 14:31:03 +09303428 /* Now it's a first class citizen! */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003429 mod->state = MODULE_STATE_LIVE;
Masami Hiramatsu0deddf42009-01-06 14:41:54 -08003430 blocking_notifier_call_chain(&module_notify_list,
3431 MODULE_STATE_LIVE, mod);
Rusty Russell6c5db222008-03-10 11:43:52 -07003432
Tejun Heo774a1222013-01-15 18:52:51 -08003433 /*
3434 * We need to finish all async code before the module init sequence
3435 * is done. This has potential to deadlock. For example, a newly
3436 * detected block device can trigger request_module() of the
3437 * default iosched from async probing task. Once userland helper
3438 * reaches here, async_synchronize_full() will wait on the async
3439 * task waiting on request_module() and deadlock.
3440 *
3441 * This deadlock is avoided by perfomring async_synchronize_full()
3442 * iff module init queued any async jobs. This isn't a full
3443 * solution as it will deadlock the same if module loading from
3444 * async jobs nests more than once; however, due to the various
3445 * constraints, this hack seems to be the best option for now.
3446 * Please refer to the following thread for details.
3447 *
3448 * http://thread.gmane.org/gmane.linux.kernel/1420814
3449 */
Luis R. Rodriguezf2411da2015-03-30 16:20:05 -07003450 if (!mod->async_probe_requested && (current->flags & PF_USED_ASYNC))
Tejun Heo774a1222013-01-15 18:52:51 -08003451 async_synchronize_full();
Linus Torvaldsd6de2c82009-04-10 12:17:41 -07003452
Rusty Russell6c5db222008-03-10 11:43:52 -07003453 mutex_lock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003454 /* Drop initial reference. */
3455 module_put(mod);
Rusty Russellad6561d2009-06-12 21:47:03 -06003456 trim_init_extable(mod);
Jan Beulich4a496222009-07-06 14:50:42 +01003457#ifdef CONFIG_KALLSYMS
Rusty Russell82440622016-02-03 16:55:26 +10303458 /* Switch to core kallsyms now init is done: kallsyms may be walking! */
3459 rcu_assign_pointer(mod->kallsyms, &mod->core_kallsyms);
Jan Beulich4a496222009-07-06 14:50:42 +01003460#endif
Jessica Yu444d13f2016-07-27 12:06:21 +09303461 module_enable_ro(mod, true);
Peter Zijlstra93c2e102015-05-27 11:09:37 +09303462 mod_tree_remove_init(mod);
Rusty Russell85c898d2015-11-26 09:45:08 +10303463 disable_ro_nx(&mod->init_layout);
Rusty Russelld453cde2015-01-20 09:07:04 +10303464 module_arch_freeing_init(mod);
Rusty Russell7523e4d2015-11-26 09:44:08 +10303465 mod->init_layout.base = NULL;
3466 mod->init_layout.size = 0;
3467 mod->init_layout.ro_size = 0;
Jessica Yu444d13f2016-07-27 12:06:21 +09303468 mod->init_layout.ro_after_init_size = 0;
Rusty Russell7523e4d2015-11-26 09:44:08 +10303469 mod->init_layout.text_size = 0;
Rusty Russellc7496372015-01-20 09:07:05 +10303470 /*
3471 * We want to free module_init, but be aware that kallsyms may be
Peter Zijlstra0be964b2015-05-27 11:09:35 +09303472 * walking this with preempt disabled. In all the failure paths, we
3473 * call synchronize_sched(), but we don't want to slow down the success
3474 * path, so use actual RCU here.
Rusty Russellc7496372015-01-20 09:07:05 +10303475 */
Peter Zijlstra0be964b2015-05-27 11:09:35 +09303476 call_rcu_sched(&freeinit->rcu, do_free_init);
Ashutosh Naik6389a382006-03-23 03:00:46 -08003477 mutex_unlock(&module_mutex);
Rusty Russell6f139092012-09-28 14:31:03 +09303478 wake_up_all(&module_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003479
3480 return 0;
Rusty Russellc7496372015-01-20 09:07:05 +10303481
3482fail_free_freeinit:
3483 kfree(freeinit);
3484fail:
3485 /* Try to protect us from buggy refcounters. */
3486 mod->state = MODULE_STATE_GOING;
3487 synchronize_sched();
3488 module_put(mod);
3489 blocking_notifier_call_chain(&module_notify_list,
3490 MODULE_STATE_GOING, mod);
Jessica Yu7e545d62016-03-16 20:55:39 -04003491 klp_module_going(mod);
Jessica Yu7dcd1822016-02-16 17:32:33 -05003492 ftrace_release_mod(mod);
Rusty Russellc7496372015-01-20 09:07:05 +10303493 free_module(mod);
3494 wake_up_all(&module_wq);
3495 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003496}
3497
Kees Cook34e11692012-10-16 07:31:07 +10303498static int may_init_module(void)
3499{
3500 if (!capable(CAP_SYS_MODULE) || modules_disabled)
3501 return -EPERM;
3502
3503 return 0;
3504}
3505
Rusty Russella3535c72013-01-21 17:18:59 +10303506/*
3507 * We try to place it in the list now to make sure it's unique before
3508 * we dedicate too many resources. In particular, temporary percpu
3509 * memory exhaustion.
3510 */
3511static int add_unformed_module(struct module *mod)
3512{
3513 int err;
3514 struct module *old;
3515
3516 mod->state = MODULE_STATE_UNFORMED;
3517
3518again:
3519 mutex_lock(&module_mutex);
Mathias Krause4f6de4d2013-07-02 15:35:11 +09303520 old = find_module_all(mod->name, strlen(mod->name), true);
3521 if (old != NULL) {
Rusty Russella3535c72013-01-21 17:18:59 +10303522 if (old->state == MODULE_STATE_COMING
3523 || old->state == MODULE_STATE_UNFORMED) {
3524 /* Wait in case it fails to load. */
3525 mutex_unlock(&module_mutex);
Peter Zijlstra9cc019b2015-02-11 15:01:13 +10303526 err = wait_event_interruptible(module_wq,
3527 finished_loading(mod->name));
Rusty Russella3535c72013-01-21 17:18:59 +10303528 if (err)
3529 goto out_unlocked;
3530 goto again;
3531 }
3532 err = -EEXIST;
3533 goto out;
3534 }
Peter Zijlstra4f6665462015-05-27 11:09:38 +09303535 mod_update_bounds(mod);
Rusty Russella3535c72013-01-21 17:18:59 +10303536 list_add_rcu(&mod->list, &modules);
Peter Zijlstra93c2e102015-05-27 11:09:37 +09303537 mod_tree_insert(mod);
Rusty Russella3535c72013-01-21 17:18:59 +10303538 err = 0;
3539
3540out:
3541 mutex_unlock(&module_mutex);
3542out_unlocked:
3543 return err;
3544}
3545
3546static int complete_formation(struct module *mod, struct load_info *info)
3547{
3548 int err;
3549
3550 mutex_lock(&module_mutex);
3551
3552 /* Find duplicate symbols (must be called under lock). */
3553 err = verify_export_symbols(mod);
3554 if (err < 0)
3555 goto out;
3556
3557 /* This relies on module_mutex for list integrity. */
3558 module_bug_finalize(info->hdr, info->sechdrs, mod);
3559
Jessica Yu444d13f2016-07-27 12:06:21 +09303560 module_enable_ro(mod, false);
Rusty Russell85c898d2015-11-26 09:45:08 +10303561 module_enable_nx(mod);
Rusty Russell49822232014-05-14 10:54:19 +09303562
Rusty Russella3535c72013-01-21 17:18:59 +10303563 /* Mark state as coming so strong_try_module_get() ignores us,
3564 * but kallsyms etc. can see us. */
3565 mod->state = MODULE_STATE_COMING;
Rusty Russell49822232014-05-14 10:54:19 +09303566 mutex_unlock(&module_mutex);
3567
Rusty Russell49822232014-05-14 10:54:19 +09303568 return 0;
Rusty Russella3535c72013-01-21 17:18:59 +10303569
3570out:
3571 mutex_unlock(&module_mutex);
3572 return err;
3573}
3574
Jessica Yu4c973d12016-03-16 20:55:38 -04003575static int prepare_coming_module(struct module *mod)
3576{
Jessica Yu7e545d62016-03-16 20:55:39 -04003577 int err;
3578
Jessica Yu4c973d12016-03-16 20:55:38 -04003579 ftrace_module_enable(mod);
Jessica Yu7e545d62016-03-16 20:55:39 -04003580 err = klp_module_coming(mod);
3581 if (err)
3582 return err;
3583
Jessica Yu4c973d12016-03-16 20:55:38 -04003584 blocking_notifier_call_chain(&module_notify_list,
3585 MODULE_STATE_COMING, mod);
3586 return 0;
3587}
3588
Luis R. Rodriguezecc86172015-03-30 16:20:03 -07003589static int unknown_module_param_cb(char *param, char *val, const char *modname,
3590 void *arg)
Rusty Russell54041d82013-07-02 15:35:12 +09303591{
Luis R. Rodriguezf2411da2015-03-30 16:20:05 -07003592 struct module *mod = arg;
3593 int ret;
3594
3595 if (strcmp(param, "async_probe") == 0) {
3596 mod->async_probe_requested = true;
3597 return 0;
3598 }
3599
Ionut Alexa6da0b562014-11-10 09:31:29 +10303600 /* Check for magic 'dyndbg' arg */
Luis R. Rodriguezf2411da2015-03-30 16:20:05 -07003601 ret = ddebug_dyndbg_module_param_cb(param, val, modname);
Andrew Mortonbddb12b2013-11-12 15:11:28 -08003602 if (ret != 0)
3603 pr_warn("%s: unknown parameter '%s' ignored\n", modname, param);
Rusty Russell54041d82013-07-02 15:35:12 +09303604 return 0;
3605}
3606
Kees Cook34e11692012-10-16 07:31:07 +10303607/* Allocate and load the module: note that size of section 0 is always
3608 zero, and we rely on this for optional sections. */
Rusty Russell2f3238a2012-10-22 18:09:41 +10303609static int load_module(struct load_info *info, const char __user *uargs,
3610 int flags)
Kees Cook34e11692012-10-16 07:31:07 +10303611{
Rusty Russella3535c72013-01-21 17:18:59 +10303612 struct module *mod;
Kees Cook34e11692012-10-16 07:31:07 +10303613 long err;
Rusty Russell51e158c2014-04-28 11:34:33 +09303614 char *after_dashes;
Kees Cook34e11692012-10-16 07:31:07 +10303615
Ben Hutchingsbca014c2016-04-28 09:24:01 +09303616 err = module_sig_check(info, flags);
Kees Cook34e11692012-10-16 07:31:07 +10303617 if (err)
3618 goto free_copy;
3619
3620 err = elf_header_check(info);
3621 if (err)
3622 goto free_copy;
3623
3624 /* Figure out module layout, and allocate all the memory. */
Rusty Russell2f3238a2012-10-22 18:09:41 +10303625 mod = layout_and_allocate(info, flags);
Kees Cook34e11692012-10-16 07:31:07 +10303626 if (IS_ERR(mod)) {
3627 err = PTR_ERR(mod);
3628 goto free_copy;
3629 }
3630
Richard Guy Briggsca86cad2017-02-04 13:10:38 -05003631 audit_log_kern_module(mod->name);
3632
Rusty Russella3535c72013-01-21 17:18:59 +10303633 /* Reserve our place in the list. */
3634 err = add_unformed_module(mod);
3635 if (err)
Rusty Russell1fb93412013-01-12 13:27:34 +10303636 goto free_module;
Rusty Russell1fb93412013-01-12 13:27:34 +10303637
Kees Cook34e11692012-10-16 07:31:07 +10303638#ifdef CONFIG_MODULE_SIG
3639 mod->sig_ok = info->sig_ok;
Rusty Russell64748a22013-01-21 17:03:02 +10303640 if (!mod->sig_ok) {
Andrew Mortonbddb12b2013-11-12 15:11:28 -08003641 pr_notice_once("%s: module verification failed: signature "
Marcel Holtmannab92ebb2015-02-06 15:09:57 +10303642 "and/or required key missing - tainting "
Andrew Mortonbddb12b2013-11-12 15:11:28 -08003643 "kernel\n", mod->name);
Mathieu Desnoyers66cc69e2014-03-13 12:11:30 +10303644 add_taint_module(mod, TAINT_UNSIGNED_MODULE, LOCKDEP_STILL_OK);
Rusty Russell64748a22013-01-21 17:03:02 +10303645 }
Kees Cook34e11692012-10-16 07:31:07 +10303646#endif
3647
Rusty Russell8d8022e2013-07-03 10:06:28 +09303648 /* To avoid stressing percpu allocator, do this once we're unique. */
Rusty Russell9eb76d72013-07-03 10:06:29 +09303649 err = percpu_modalloc(mod, info);
Rusty Russell8d8022e2013-07-03 10:06:28 +09303650 if (err)
3651 goto unlink_mod;
3652
Kees Cook34e11692012-10-16 07:31:07 +10303653 /* Now module is in final location, initialize linked lists, etc. */
3654 err = module_unload_init(mod);
3655 if (err)
Rusty Russell1fb93412013-01-12 13:27:34 +10303656 goto unlink_mod;
Kees Cook34e11692012-10-16 07:31:07 +10303657
Rusty Russellcf2fde72015-06-26 06:44:38 +09303658 init_param_lock(mod);
Dan Streetmanb51d23e2015-06-17 06:18:52 +09303659
Kees Cook34e11692012-10-16 07:31:07 +10303660 /* Now we've got everything in the final locations, we can
3661 * find optional sections. */
Frantisek Hrbataeb3057d2013-10-14 18:08:46 +10303662 err = find_module_sections(mod, info);
3663 if (err)
3664 goto free_unload;
Kees Cook34e11692012-10-16 07:31:07 +10303665
3666 err = check_module_license_and_versions(mod);
3667 if (err)
3668 goto free_unload;
3669
3670 /* Set up MODINFO_ATTR fields */
3671 setup_modinfo(mod, info);
3672
3673 /* Fix up syms, so that st_value is a pointer to location. */
3674 err = simplify_symbols(mod, info);
3675 if (err < 0)
3676 goto free_modinfo;
3677
3678 err = apply_relocations(mod, info);
3679 if (err < 0)
3680 goto free_modinfo;
3681
3682 err = post_relocation(mod, info);
3683 if (err < 0)
3684 goto free_modinfo;
3685
3686 flush_module_icache(mod);
3687
3688 /* Now copy in args */
3689 mod->args = strndup_user(uargs, ~0UL >> 1);
3690 if (IS_ERR(mod->args)) {
3691 err = PTR_ERR(mod->args);
3692 goto free_arch_cleanup;
3693 }
3694
Kees Cook34e11692012-10-16 07:31:07 +10303695 dynamic_debug_setup(info->debug, info->num_debug);
3696
Steven Rostedt (Red Hat)a949ae52014-04-24 10:40:12 -04003697 /* Ftrace init must be called in the MODULE_STATE_UNFORMED state */
3698 ftrace_module_init(mod);
3699
Rusty Russella3535c72013-01-21 17:18:59 +10303700 /* Finally it's fully formed, ready to start executing. */
3701 err = complete_formation(mod, info);
3702 if (err)
Rusty Russell1fb93412013-01-12 13:27:34 +10303703 goto ddebug_cleanup;
Kees Cook34e11692012-10-16 07:31:07 +10303704
Jessica Yu4c973d12016-03-16 20:55:38 -04003705 err = prepare_coming_module(mod);
3706 if (err)
3707 goto bug_cleanup;
3708
Kees Cook34e11692012-10-16 07:31:07 +10303709 /* Module is ready to execute: parsing args may do that. */
Rusty Russell51e158c2014-04-28 11:34:33 +09303710 after_dashes = parse_args(mod->name, mod->args, mod->kp, mod->num_kp,
Luis R. Rodriguez4355efb2016-02-03 16:55:26 +10303711 -32768, 32767, mod,
Luis R. Rodriguezecc86172015-03-30 16:20:03 -07003712 unknown_module_param_cb);
Rusty Russell51e158c2014-04-28 11:34:33 +09303713 if (IS_ERR(after_dashes)) {
3714 err = PTR_ERR(after_dashes);
Jessica Yu4c973d12016-03-16 20:55:38 -04003715 goto coming_cleanup;
Rusty Russell51e158c2014-04-28 11:34:33 +09303716 } else if (after_dashes) {
3717 pr_warn("%s: parameters '%s' after `--' ignored\n",
3718 mod->name, after_dashes);
3719 }
Kees Cook34e11692012-10-16 07:31:07 +10303720
Richard Guy Briggsca86cad2017-02-04 13:10:38 -05003721 /* Link in to sysfs. */
Kees Cook34e11692012-10-16 07:31:07 +10303722 err = mod_sysfs_setup(mod, info, mod->kp, mod->num_kp);
3723 if (err < 0)
Jessica Yu4c973d12016-03-16 20:55:38 -04003724 goto coming_cleanup;
Kees Cook34e11692012-10-16 07:31:07 +10303725
Jessica Yu1ce15ef2016-03-22 20:03:16 -04003726 if (is_livepatch_module(mod)) {
3727 err = copy_module_elf(mod, info);
3728 if (err < 0)
3729 goto sysfs_cleanup;
3730 }
3731
Kees Cook34e11692012-10-16 07:31:07 +10303732 /* Get rid of temporary copy. */
3733 free_copy(info);
3734
3735 /* Done! */
3736 trace_module_load(mod);
3737
3738 return do_init_module(mod);
3739
Jessica Yu1ce15ef2016-03-22 20:03:16 -04003740 sysfs_cleanup:
3741 mod_sysfs_teardown(mod);
Jessica Yu4c973d12016-03-16 20:55:38 -04003742 coming_cleanup:
Aaron Tomlin885a78d2016-10-20 17:18:12 +01003743 mod->state = MODULE_STATE_GOING;
Luis R. Rodrigueza5544882017-02-10 14:06:22 -08003744 destroy_params(mod->kp, mod->num_kp);
Jessica Yu4c973d12016-03-16 20:55:38 -04003745 blocking_notifier_call_chain(&module_notify_list,
3746 MODULE_STATE_GOING, mod);
Jessica Yu7e545d62016-03-16 20:55:39 -04003747 klp_module_going(mod);
Rusty Russell1fb93412013-01-12 13:27:34 +10303748 bug_cleanup:
3749 /* module_bug_cleanup needs module_mutex protection */
Kees Cook34e11692012-10-16 07:31:07 +10303750 mutex_lock(&module_mutex);
Kees Cook34e11692012-10-16 07:31:07 +10303751 module_bug_cleanup(mod);
Linus Torvaldsee61abb2013-01-20 20:22:58 -08003752 mutex_unlock(&module_mutex);
Andy Lutomirskiff7e0052014-08-16 04:13:37 +09303753
3754 /* we can't deallocate the module until we clear memory protection */
Rusty Russell85c898d2015-11-26 09:45:08 +10303755 module_disable_ro(mod);
3756 module_disable_nx(mod);
Andy Lutomirskiff7e0052014-08-16 04:13:37 +09303757
Rusty Russella3535c72013-01-21 17:18:59 +10303758 ddebug_cleanup:
Rusty Russell1fb93412013-01-12 13:27:34 +10303759 dynamic_debug_remove(info->debug);
Kees Cook34e11692012-10-16 07:31:07 +10303760 synchronize_sched();
3761 kfree(mod->args);
3762 free_arch_cleanup:
3763 module_arch_cleanup(mod);
3764 free_modinfo:
3765 free_modinfo(mod);
3766 free_unload:
3767 module_unload_free(mod);
Rusty Russell1fb93412013-01-12 13:27:34 +10303768 unlink_mod:
3769 mutex_lock(&module_mutex);
3770 /* Unlink carefully: kallsyms could be walking list. */
3771 list_del_rcu(&mod->list);
Peter Zijlstra758556b2015-07-09 06:48:06 +09303772 mod_tree_remove(mod);
Rusty Russell1fb93412013-01-12 13:27:34 +10303773 wake_up_all(&module_wq);
Peter Zijlstra0be964b2015-05-27 11:09:35 +09303774 /* Wait for RCU-sched synchronizing before releasing mod->list. */
3775 synchronize_sched();
Rusty Russell1fb93412013-01-12 13:27:34 +10303776 mutex_unlock(&module_mutex);
Kees Cook34e11692012-10-16 07:31:07 +10303777 free_module:
Steven Rostedt (Red Hat)049fb9b2016-01-05 20:32:47 -05003778 /*
3779 * Ftrace needs to clean up what it initialized.
3780 * This does nothing if ftrace_module_init() wasn't called,
3781 * but it must be called outside of module_mutex.
3782 */
3783 ftrace_release_mod(mod);
Peter Zijlstra35a93932015-02-26 16:23:11 +01003784 /* Free lock-classes; relies on the preceding sync_rcu() */
Rusty Russell7523e4d2015-11-26 09:44:08 +10303785 lockdep_free_key_range(mod->core_layout.base, mod->core_layout.size);
Peter Zijlstra35a93932015-02-26 16:23:11 +01003786
Kees Cook34e11692012-10-16 07:31:07 +10303787 module_deallocate(mod, info);
3788 free_copy:
3789 free_copy(info);
3790 return err;
3791}
3792
3793SYSCALL_DEFINE3(init_module, void __user *, umod,
3794 unsigned long, len, const char __user *, uargs)
3795{
3796 int err;
3797 struct load_info info = { };
3798
3799 err = may_init_module();
3800 if (err)
3801 return err;
3802
3803 pr_debug("init_module: umod=%p, len=%lu, uargs=%p\n",
3804 umod, len, uargs);
3805
3806 err = copy_module_from_user(umod, len, &info);
3807 if (err)
3808 return err;
3809
Rusty Russell2f3238a2012-10-22 18:09:41 +10303810 return load_module(&info, uargs, 0);
Kees Cook34e11692012-10-16 07:31:07 +10303811}
3812
Rusty Russell2f3238a2012-10-22 18:09:41 +10303813SYSCALL_DEFINE3(finit_module, int, fd, const char __user *, uargs, int, flags)
Kees Cook34e11692012-10-16 07:31:07 +10303814{
Kees Cook34e11692012-10-16 07:31:07 +10303815 struct load_info info = { };
Mimi Zohara1db7422015-12-30 07:35:30 -05003816 loff_t size;
3817 void *hdr;
3818 int err;
Kees Cook34e11692012-10-16 07:31:07 +10303819
3820 err = may_init_module();
3821 if (err)
3822 return err;
3823
Rusty Russell2f3238a2012-10-22 18:09:41 +10303824 pr_debug("finit_module: fd=%d, uargs=%p, flags=%i\n", fd, uargs, flags);
3825
3826 if (flags & ~(MODULE_INIT_IGNORE_MODVERSIONS
3827 |MODULE_INIT_IGNORE_VERMAGIC))
3828 return -EINVAL;
Kees Cook34e11692012-10-16 07:31:07 +10303829
Mimi Zohara1db7422015-12-30 07:35:30 -05003830 err = kernel_read_file_from_fd(fd, &hdr, &size, INT_MAX,
3831 READING_MODULE);
Kees Cook34e11692012-10-16 07:31:07 +10303832 if (err)
3833 return err;
Mimi Zohara1db7422015-12-30 07:35:30 -05003834 info.hdr = hdr;
3835 info.len = size;
Kees Cook34e11692012-10-16 07:31:07 +10303836
Rusty Russell2f3238a2012-10-22 18:09:41 +10303837 return load_module(&info, uargs, flags);
Kees Cook34e11692012-10-16 07:31:07 +10303838}
3839
Linus Torvalds1da177e2005-04-16 15:20:36 -07003840static inline int within(unsigned long addr, void *start, unsigned long size)
3841{
3842 return ((void *)addr >= start && (void *)addr < start + size);
3843}
3844
3845#ifdef CONFIG_KALLSYMS
3846/*
3847 * This ignores the intensely annoying "mapping symbols" found
3848 * in ARM ELF files: $a, $t and $d.
3849 */
3850static inline int is_arm_mapping_symbol(const char *str)
3851{
Russell King2e3a10a2014-07-27 07:29:01 +09303852 if (str[0] == '.' && str[1] == 'L')
3853 return true;
Kyle McMartin6c34f1f2014-09-16 22:37:18 +01003854 return str[0] == '$' && strchr("axtd", str[1])
Linus Torvalds1da177e2005-04-16 15:20:36 -07003855 && (str[2] == '\0' || str[2] == '.');
3856}
3857
Rusty Russell82440622016-02-03 16:55:26 +10303858static const char *symname(struct mod_kallsyms *kallsyms, unsigned int symnum)
Rusty Russell2e7bac52016-02-03 16:55:26 +10303859{
Rusty Russell82440622016-02-03 16:55:26 +10303860 return kallsyms->strtab + kallsyms->symtab[symnum].st_name;
Rusty Russell2e7bac52016-02-03 16:55:26 +10303861}
3862
Linus Torvalds1da177e2005-04-16 15:20:36 -07003863static const char *get_ksymbol(struct module *mod,
3864 unsigned long addr,
3865 unsigned long *size,
3866 unsigned long *offset)
3867{
3868 unsigned int i, best = 0;
3869 unsigned long nextval;
Rusty Russell82440622016-02-03 16:55:26 +10303870 struct mod_kallsyms *kallsyms = rcu_dereference_sched(mod->kallsyms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003871
3872 /* At worse, next value is at end of module */
Masami Hiramatsua06f6212009-01-06 14:41:49 -08003873 if (within_module_init(addr, mod))
Rusty Russell7523e4d2015-11-26 09:44:08 +10303874 nextval = (unsigned long)mod->init_layout.base+mod->init_layout.text_size;
Daniel Walker22a8bde2007-10-18 03:06:07 -07003875 else
Rusty Russell7523e4d2015-11-26 09:44:08 +10303876 nextval = (unsigned long)mod->core_layout.base+mod->core_layout.text_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003877
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003878 /* Scan for closest preceding symbol, and next symbol. (ELF
Daniel Walker22a8bde2007-10-18 03:06:07 -07003879 starts real symbols at 1). */
Rusty Russell82440622016-02-03 16:55:26 +10303880 for (i = 1; i < kallsyms->num_symtab; i++) {
3881 if (kallsyms->symtab[i].st_shndx == SHN_UNDEF)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003882 continue;
3883
3884 /* We ignore unnamed symbols: they're uninformative
3885 * and inserted at a whim. */
Rusty Russell82440622016-02-03 16:55:26 +10303886 if (*symname(kallsyms, i) == '\0'
3887 || is_arm_mapping_symbol(symname(kallsyms, i)))
Rusty Russell2e7bac52016-02-03 16:55:26 +10303888 continue;
3889
Rusty Russell82440622016-02-03 16:55:26 +10303890 if (kallsyms->symtab[i].st_value <= addr
3891 && kallsyms->symtab[i].st_value > kallsyms->symtab[best].st_value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003892 best = i;
Rusty Russell82440622016-02-03 16:55:26 +10303893 if (kallsyms->symtab[i].st_value > addr
3894 && kallsyms->symtab[i].st_value < nextval)
3895 nextval = kallsyms->symtab[i].st_value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003896 }
3897
3898 if (!best)
3899 return NULL;
3900
Alexey Dobriyanffb45122007-05-08 00:28:41 -07003901 if (size)
Rusty Russell82440622016-02-03 16:55:26 +10303902 *size = nextval - kallsyms->symtab[best].st_value;
Alexey Dobriyanffb45122007-05-08 00:28:41 -07003903 if (offset)
Rusty Russell82440622016-02-03 16:55:26 +10303904 *offset = addr - kallsyms->symtab[best].st_value;
3905 return symname(kallsyms, best);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003906}
3907
Rusty Russell6dd06c92008-01-29 17:13:22 -05003908/* For kallsyms to ask for address resolution. NULL means not found. Careful
3909 * not to lock to avoid deadlock on oopses, simply disable preemption. */
Andrew Morton92dfc9d2008-02-08 04:18:43 -08003910const char *module_address_lookup(unsigned long addr,
Rusty Russell6dd06c92008-01-29 17:13:22 -05003911 unsigned long *size,
3912 unsigned long *offset,
3913 char **modname,
3914 char *namebuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003915{
Rusty Russellcb2a5202008-01-14 00:55:03 -08003916 const char *ret = NULL;
Peter Zijlstrab7df4d12015-05-27 11:09:37 +09303917 struct module *mod;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003918
Rusty Russellcb2a5202008-01-14 00:55:03 -08003919 preempt_disable();
Peter Zijlstrab7df4d12015-05-27 11:09:37 +09303920 mod = __module_address(addr);
3921 if (mod) {
3922 if (modname)
3923 *modname = mod->name;
3924 ret = get_ksymbol(mod, addr, size, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003925 }
Rusty Russell6dd06c92008-01-29 17:13:22 -05003926 /* Make a copy in here where it's safe */
3927 if (ret) {
3928 strncpy(namebuf, ret, KSYM_NAME_LEN - 1);
3929 ret = namebuf;
3930 }
Rusty Russellcb2a5202008-01-14 00:55:03 -08003931 preempt_enable();
Peter Zijlstrab7df4d12015-05-27 11:09:37 +09303932
Andrew Morton92dfc9d2008-02-08 04:18:43 -08003933 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003934}
3935
Alexey Dobriyan9d65cb42007-05-08 00:28:43 -07003936int lookup_module_symbol_name(unsigned long addr, char *symname)
3937{
3938 struct module *mod;
3939
Rusty Russellcb2a5202008-01-14 00:55:03 -08003940 preempt_disable();
Andi Kleend72b3752008-08-30 10:09:00 +02003941 list_for_each_entry_rcu(mod, &modules, list) {
Rusty Russell0d21b0e2013-01-12 11:38:44 +10303942 if (mod->state == MODULE_STATE_UNFORMED)
3943 continue;
Petr Mladek9b20a352014-07-27 07:24:01 +09303944 if (within_module(addr, mod)) {
Alexey Dobriyan9d65cb42007-05-08 00:28:43 -07003945 const char *sym;
3946
3947 sym = get_ksymbol(mod, addr, NULL, NULL);
3948 if (!sym)
3949 goto out;
Tejun Heo9281ace2007-07-17 04:03:51 -07003950 strlcpy(symname, sym, KSYM_NAME_LEN);
Rusty Russellcb2a5202008-01-14 00:55:03 -08003951 preempt_enable();
Alexey Dobriyan9d65cb42007-05-08 00:28:43 -07003952 return 0;
3953 }
3954 }
3955out:
Rusty Russellcb2a5202008-01-14 00:55:03 -08003956 preempt_enable();
Alexey Dobriyan9d65cb42007-05-08 00:28:43 -07003957 return -ERANGE;
3958}
3959
Alexey Dobriyana5c43da2007-05-08 00:28:47 -07003960int lookup_module_symbol_attrs(unsigned long addr, unsigned long *size,
3961 unsigned long *offset, char *modname, char *name)
3962{
3963 struct module *mod;
3964
Rusty Russellcb2a5202008-01-14 00:55:03 -08003965 preempt_disable();
Andi Kleend72b3752008-08-30 10:09:00 +02003966 list_for_each_entry_rcu(mod, &modules, list) {
Rusty Russell0d21b0e2013-01-12 11:38:44 +10303967 if (mod->state == MODULE_STATE_UNFORMED)
3968 continue;
Petr Mladek9b20a352014-07-27 07:24:01 +09303969 if (within_module(addr, mod)) {
Alexey Dobriyana5c43da2007-05-08 00:28:47 -07003970 const char *sym;
3971
3972 sym = get_ksymbol(mod, addr, size, offset);
3973 if (!sym)
3974 goto out;
3975 if (modname)
Tejun Heo9281ace2007-07-17 04:03:51 -07003976 strlcpy(modname, mod->name, MODULE_NAME_LEN);
Alexey Dobriyana5c43da2007-05-08 00:28:47 -07003977 if (name)
Tejun Heo9281ace2007-07-17 04:03:51 -07003978 strlcpy(name, sym, KSYM_NAME_LEN);
Rusty Russellcb2a5202008-01-14 00:55:03 -08003979 preempt_enable();
Alexey Dobriyana5c43da2007-05-08 00:28:47 -07003980 return 0;
3981 }
3982 }
3983out:
Rusty Russellcb2a5202008-01-14 00:55:03 -08003984 preempt_enable();
Alexey Dobriyana5c43da2007-05-08 00:28:47 -07003985 return -ERANGE;
3986}
3987
Alexey Dobriyanea078902007-05-08 00:28:39 -07003988int module_get_kallsym(unsigned int symnum, unsigned long *value, char *type,
3989 char *name, char *module_name, int *exported)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003990{
3991 struct module *mod;
3992
Rusty Russellcb2a5202008-01-14 00:55:03 -08003993 preempt_disable();
Andi Kleend72b3752008-08-30 10:09:00 +02003994 list_for_each_entry_rcu(mod, &modules, list) {
Rusty Russell82440622016-02-03 16:55:26 +10303995 struct mod_kallsyms *kallsyms;
3996
Rusty Russell0d21b0e2013-01-12 11:38:44 +10303997 if (mod->state == MODULE_STATE_UNFORMED)
3998 continue;
Rusty Russell82440622016-02-03 16:55:26 +10303999 kallsyms = rcu_dereference_sched(mod->kallsyms);
4000 if (symnum < kallsyms->num_symtab) {
4001 *value = kallsyms->symtab[symnum].st_value;
4002 *type = kallsyms->symtab[symnum].st_info;
4003 strlcpy(name, symname(kallsyms, symnum), KSYM_NAME_LEN);
Tejun Heo9281ace2007-07-17 04:03:51 -07004004 strlcpy(module_name, mod->name, MODULE_NAME_LEN);
Tim Abbottca4787b2009-01-05 08:40:10 -06004005 *exported = is_exported(name, *value, mod);
Rusty Russellcb2a5202008-01-14 00:55:03 -08004006 preempt_enable();
Alexey Dobriyanea078902007-05-08 00:28:39 -07004007 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004008 }
Rusty Russell82440622016-02-03 16:55:26 +10304009 symnum -= kallsyms->num_symtab;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004010 }
Rusty Russellcb2a5202008-01-14 00:55:03 -08004011 preempt_enable();
Alexey Dobriyanea078902007-05-08 00:28:39 -07004012 return -ERANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004013}
4014
4015static unsigned long mod_find_symname(struct module *mod, const char *name)
4016{
4017 unsigned int i;
Rusty Russell82440622016-02-03 16:55:26 +10304018 struct mod_kallsyms *kallsyms = rcu_dereference_sched(mod->kallsyms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004019
Rusty Russell82440622016-02-03 16:55:26 +10304020 for (i = 0; i < kallsyms->num_symtab; i++)
4021 if (strcmp(name, symname(kallsyms, i)) == 0 &&
4022 kallsyms->symtab[i].st_info != 'U')
4023 return kallsyms->symtab[i].st_value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004024 return 0;
4025}
4026
4027/* Look for this name: can be of form module:name. */
4028unsigned long module_kallsyms_lookup_name(const char *name)
4029{
4030 struct module *mod;
4031 char *colon;
4032 unsigned long ret = 0;
4033
4034 /* Don't lock: we're in enough trouble already. */
Rusty Russellcb2a5202008-01-14 00:55:03 -08004035 preempt_disable();
Naveen N. Rao17586182017-04-23 22:53:43 +05304036 if ((colon = strnchr(name, MODULE_NAME_LEN, ':')) != NULL) {
Mathias Krause4f6de4d2013-07-02 15:35:11 +09304037 if ((mod = find_module_all(name, colon - name, false)) != NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004038 ret = mod_find_symname(mod, colon+1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004039 } else {
Rusty Russell0d21b0e2013-01-12 11:38:44 +10304040 list_for_each_entry_rcu(mod, &modules, list) {
4041 if (mod->state == MODULE_STATE_UNFORMED)
4042 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004043 if ((ret = mod_find_symname(mod, name)) != 0)
4044 break;
Rusty Russell0d21b0e2013-01-12 11:38:44 +10304045 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004046 }
Rusty Russellcb2a5202008-01-14 00:55:03 -08004047 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004048 return ret;
4049}
Anders Kaseorg75a66612008-12-05 19:03:58 -05004050
4051int module_kallsyms_on_each_symbol(int (*fn)(void *, const char *,
4052 struct module *, unsigned long),
4053 void *data)
4054{
4055 struct module *mod;
4056 unsigned int i;
4057 int ret;
4058
Peter Zijlstra0be964b2015-05-27 11:09:35 +09304059 module_assert_mutex();
4060
Anders Kaseorg75a66612008-12-05 19:03:58 -05004061 list_for_each_entry(mod, &modules, list) {
Rusty Russell82440622016-02-03 16:55:26 +10304062 /* We hold module_mutex: no need for rcu_dereference_sched */
4063 struct mod_kallsyms *kallsyms = mod->kallsyms;
4064
Rusty Russell0d21b0e2013-01-12 11:38:44 +10304065 if (mod->state == MODULE_STATE_UNFORMED)
4066 continue;
Rusty Russell82440622016-02-03 16:55:26 +10304067 for (i = 0; i < kallsyms->num_symtab; i++) {
4068 ret = fn(data, symname(kallsyms, i),
4069 mod, kallsyms->symtab[i].st_value);
Anders Kaseorg75a66612008-12-05 19:03:58 -05004070 if (ret != 0)
4071 return ret;
4072 }
4073 }
4074 return 0;
4075}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004076#endif /* CONFIG_KALLSYMS */
4077
Petr Mladek7fd83292016-09-21 13:47:22 +02004078/* Maximum number of characters written by module_flags() */
4079#define MODULE_FLAGS_BUF_SIZE (TAINT_FLAGS_COUNT + 4)
4080
4081/* Keep in sync with MODULE_FLAGS_BUF_SIZE !!! */
Arjan van de Ven21aa9282008-01-25 21:08:33 +01004082static char *module_flags(struct module *mod, char *buf)
Florin Malitafa3ba2e82006-10-11 01:21:48 -07004083{
4084 int bx = 0;
4085
Rusty Russell0d21b0e2013-01-12 11:38:44 +10304086 BUG_ON(mod->state == MODULE_STATE_UNFORMED);
Arjan van de Ven21aa9282008-01-25 21:08:33 +01004087 if (mod->taints ||
4088 mod->state == MODULE_STATE_GOING ||
4089 mod->state == MODULE_STATE_COMING) {
Florin Malitafa3ba2e82006-10-11 01:21:48 -07004090 buf[bx++] = '(';
Kay Sieverscca3e702012-01-13 09:32:15 +10304091 bx += module_flags_taint(mod, buf + bx);
Arjan van de Ven21aa9282008-01-25 21:08:33 +01004092 /* Show a - for module-is-being-unloaded */
4093 if (mod->state == MODULE_STATE_GOING)
4094 buf[bx++] = '-';
4095 /* Show a + for module-is-being-loaded */
4096 if (mod->state == MODULE_STATE_COMING)
4097 buf[bx++] = '+';
Florin Malitafa3ba2e82006-10-11 01:21:48 -07004098 buf[bx++] = ')';
4099 }
4100 buf[bx] = '\0';
4101
4102 return buf;
4103}
4104
Alexey Dobriyan3b5d5c62008-10-06 13:19:27 +04004105#ifdef CONFIG_PROC_FS
4106/* Called by the /proc file system to return a list of modules. */
4107static void *m_start(struct seq_file *m, loff_t *pos)
4108{
4109 mutex_lock(&module_mutex);
4110 return seq_list_start(&modules, *pos);
4111}
4112
4113static void *m_next(struct seq_file *m, void *p, loff_t *pos)
4114{
4115 return seq_list_next(p, &modules, pos);
4116}
4117
4118static void m_stop(struct seq_file *m, void *p)
4119{
4120 mutex_unlock(&module_mutex);
4121}
4122
Linus Torvalds1da177e2005-04-16 15:20:36 -07004123static int m_show(struct seq_file *m, void *p)
4124{
4125 struct module *mod = list_entry(p, struct module, list);
Petr Mladek7fd83292016-09-21 13:47:22 +02004126 char buf[MODULE_FLAGS_BUF_SIZE];
Florin Malitafa3ba2e82006-10-11 01:21:48 -07004127
Rusty Russell0d21b0e2013-01-12 11:38:44 +10304128 /* We always ignore unformed modules. */
4129 if (mod->state == MODULE_STATE_UNFORMED)
4130 return 0;
4131
Denys Vlasenko2f0f2a32008-07-22 19:24:27 -05004132 seq_printf(m, "%s %u",
Rusty Russell7523e4d2015-11-26 09:44:08 +10304133 mod->name, mod->init_layout.size + mod->core_layout.size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004134 print_unload_info(m, mod);
4135
4136 /* Informative for users. */
4137 seq_printf(m, " %s",
Ionut Alexa6da0b562014-11-10 09:31:29 +10304138 mod->state == MODULE_STATE_GOING ? "Unloading" :
4139 mod->state == MODULE_STATE_COMING ? "Loading" :
Linus Torvalds1da177e2005-04-16 15:20:36 -07004140 "Live");
4141 /* Used by oprofile and other similar tools. */
Rusty Russell7523e4d2015-11-26 09:44:08 +10304142 seq_printf(m, " 0x%pK", mod->core_layout.base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004143
Florin Malitafa3ba2e82006-10-11 01:21:48 -07004144 /* Taints info */
4145 if (mod->taints)
Arjan van de Ven21aa9282008-01-25 21:08:33 +01004146 seq_printf(m, " %s", module_flags(mod, buf));
Florin Malitafa3ba2e82006-10-11 01:21:48 -07004147
Ionut Alexa6da0b562014-11-10 09:31:29 +10304148 seq_puts(m, "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004149 return 0;
4150}
4151
4152/* Format: modulename size refcount deps address
4153
4154 Where refcount is a number or -, and deps is a comma-separated list
4155 of depends or -.
4156*/
Alexey Dobriyan3b5d5c62008-10-06 13:19:27 +04004157static const struct seq_operations modules_op = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004158 .start = m_start,
4159 .next = m_next,
4160 .stop = m_stop,
4161 .show = m_show
4162};
4163
Alexey Dobriyan3b5d5c62008-10-06 13:19:27 +04004164static int modules_open(struct inode *inode, struct file *file)
4165{
4166 return seq_open(file, &modules_op);
4167}
4168
4169static const struct file_operations proc_modules_operations = {
4170 .open = modules_open,
4171 .read = seq_read,
4172 .llseek = seq_lseek,
4173 .release = seq_release,
4174};
4175
4176static int __init proc_modules_init(void)
4177{
4178 proc_create("modules", 0, NULL, &proc_modules_operations);
4179 return 0;
4180}
4181module_init(proc_modules_init);
4182#endif
4183
Linus Torvalds1da177e2005-04-16 15:20:36 -07004184/* Given an address, look for it in the module exception tables. */
4185const struct exception_table_entry *search_module_extables(unsigned long addr)
4186{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004187 const struct exception_table_entry *e = NULL;
4188 struct module *mod;
4189
Rusty Russell24da1cb2007-07-15 23:41:46 -07004190 preempt_disable();
Peter Zijlstra5ff22642017-02-08 15:48:01 +01004191 mod = __module_address(addr);
4192 if (!mod)
4193 goto out;
Daniel Walker22a8bde2007-10-18 03:06:07 -07004194
Peter Zijlstra5ff22642017-02-08 15:48:01 +01004195 if (!mod->num_exentries)
4196 goto out;
4197
4198 e = search_extable(mod->extable,
Thomas Meyera94c33d2017-07-10 15:51:58 -07004199 mod->num_exentries,
Peter Zijlstra5ff22642017-02-08 15:48:01 +01004200 addr);
4201out:
Rusty Russell24da1cb2007-07-15 23:41:46 -07004202 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004203
Peter Zijlstra5ff22642017-02-08 15:48:01 +01004204 /*
4205 * Now, if we found one, we are running inside it now, hence
4206 * we cannot unload the module, hence no refcnt needed.
4207 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004208 return e;
4209}
4210
Ingo Molnar4d435f92006-07-03 00:24:24 -07004211/*
Rusty Russelle6104992009-03-31 13:05:31 -06004212 * is_module_address - is this address inside a module?
4213 * @addr: the address to check.
4214 *
4215 * See is_module_text_address() if you simply want to see if the address
4216 * is code (not data).
Ingo Molnar4d435f92006-07-03 00:24:24 -07004217 */
Rusty Russelle6104992009-03-31 13:05:31 -06004218bool is_module_address(unsigned long addr)
Ingo Molnar4d435f92006-07-03 00:24:24 -07004219{
Rusty Russelle6104992009-03-31 13:05:31 -06004220 bool ret;
Ingo Molnar4d435f92006-07-03 00:24:24 -07004221
Rusty Russell24da1cb2007-07-15 23:41:46 -07004222 preempt_disable();
Rusty Russelle6104992009-03-31 13:05:31 -06004223 ret = __module_address(addr) != NULL;
Rusty Russell24da1cb2007-07-15 23:41:46 -07004224 preempt_enable();
Ingo Molnar4d435f92006-07-03 00:24:24 -07004225
Rusty Russelle6104992009-03-31 13:05:31 -06004226 return ret;
Ingo Molnar4d435f92006-07-03 00:24:24 -07004227}
4228
Rusty Russelle6104992009-03-31 13:05:31 -06004229/*
4230 * __module_address - get the module which contains an address.
4231 * @addr: the address.
4232 *
4233 * Must be called with preempt disabled or module mutex held so that
4234 * module doesn't get freed during this.
4235 */
Linus Torvalds714f83d2009-04-05 11:04:19 -07004236struct module *__module_address(unsigned long addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004237{
4238 struct module *mod;
4239
Rusty Russell3a642e92008-07-22 19:24:28 -05004240 if (addr < module_addr_min || addr > module_addr_max)
4241 return NULL;
4242
Peter Zijlstra0be964b2015-05-27 11:09:35 +09304243 module_assert_mutex_or_preempt();
4244
Peter Zijlstra6c9692e2015-05-27 11:09:37 +09304245 mod = mod_find(addr);
Peter Zijlstra93c2e102015-05-27 11:09:37 +09304246 if (mod) {
4247 BUG_ON(!within_module(addr, mod));
Rusty Russell0d21b0e2013-01-12 11:38:44 +10304248 if (mod->state == MODULE_STATE_UNFORMED)
Peter Zijlstra93c2e102015-05-27 11:09:37 +09304249 mod = NULL;
Rusty Russell0d21b0e2013-01-12 11:38:44 +10304250 }
Peter Zijlstra93c2e102015-05-27 11:09:37 +09304251 return mod;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004252}
Tim Abbottc6b37802008-12-05 19:03:59 -05004253EXPORT_SYMBOL_GPL(__module_address);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004254
Rusty Russelle6104992009-03-31 13:05:31 -06004255/*
4256 * is_module_text_address - is this address inside module code?
4257 * @addr: the address to check.
4258 *
4259 * See is_module_address() if you simply want to see if the address is
4260 * anywhere in a module. See kernel_text_address() for testing if an
4261 * address corresponds to kernel or module code.
4262 */
4263bool is_module_text_address(unsigned long addr)
4264{
4265 bool ret;
4266
4267 preempt_disable();
4268 ret = __module_text_address(addr) != NULL;
4269 preempt_enable();
4270
4271 return ret;
4272}
4273
4274/*
4275 * __module_text_address - get the module whose code contains an address.
4276 * @addr: the address.
4277 *
4278 * Must be called with preempt disabled or module mutex held so that
4279 * module doesn't get freed during this.
4280 */
4281struct module *__module_text_address(unsigned long addr)
4282{
4283 struct module *mod = __module_address(addr);
4284 if (mod) {
4285 /* Make sure it's within the text section. */
Rusty Russell7523e4d2015-11-26 09:44:08 +10304286 if (!within(addr, mod->init_layout.base, mod->init_layout.text_size)
4287 && !within(addr, mod->core_layout.base, mod->core_layout.text_size))
Rusty Russelle6104992009-03-31 13:05:31 -06004288 mod = NULL;
4289 }
4290 return mod;
4291}
Tim Abbottc6b37802008-12-05 19:03:59 -05004292EXPORT_SYMBOL_GPL(__module_text_address);
Rusty Russelle6104992009-03-31 13:05:31 -06004293
Linus Torvalds1da177e2005-04-16 15:20:36 -07004294/* Don't grab lock, we're oopsing. */
4295void print_modules(void)
4296{
4297 struct module *mod;
Petr Mladek7fd83292016-09-21 13:47:22 +02004298 char buf[MODULE_FLAGS_BUF_SIZE];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004299
Linus Torvaldsb2311252009-06-16 11:07:14 -07004300 printk(KERN_DEFAULT "Modules linked in:");
Andi Kleend72b3752008-08-30 10:09:00 +02004301 /* Most callers should already have preempt disabled, but make sure */
4302 preempt_disable();
Rusty Russell0d21b0e2013-01-12 11:38:44 +10304303 list_for_each_entry_rcu(mod, &modules, list) {
4304 if (mod->state == MODULE_STATE_UNFORMED)
4305 continue;
Jiri Slaby27bba4d2014-02-03 11:13:13 +10304306 pr_cont(" %s%s", mod->name, module_flags(mod, buf));
Rusty Russell0d21b0e2013-01-12 11:38:44 +10304307 }
Andi Kleend72b3752008-08-30 10:09:00 +02004308 preempt_enable();
Arjan van de Vene14af7e2008-01-25 21:08:33 +01004309 if (last_unloaded_module[0])
Jiri Slaby27bba4d2014-02-03 11:13:13 +10304310 pr_cont(" [last unloaded: %s]", last_unloaded_module);
4311 pr_cont("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004312}
4313
Linus Torvalds1da177e2005-04-16 15:20:36 -07004314#ifdef CONFIG_MODVERSIONS
Rusty Russell8c8ef422009-03-31 13:05:34 -06004315/* Generate the signature for all relevant module structures here.
4316 * If these change, we don't want to try to parse the module. */
4317void module_layout(struct module *mod,
4318 struct modversion_info *ver,
4319 struct kernel_param *kp,
4320 struct kernel_symbol *ks,
Mathieu Desnoyers65498642011-01-26 17:26:22 -05004321 struct tracepoint * const *tp)
Rusty Russell8c8ef422009-03-31 13:05:34 -06004322{
4323}
4324EXPORT_SYMBOL(module_layout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004325#endif