blob: ca4509b13400c55c4a306868d45ff1341657c621 [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>
Laura Abbottbbca07c2017-05-08 15:58:29 -070052#ifdef CONFIG_STRICT_MODULE_RWX
53#include <asm/set_memory.h>
54#endif
Bernd Schmidteb8cdec2009-09-21 17:03:57 -070055#include <asm/mmu_context.h>
Sam Ravnborgb817f6f2006-06-09 21:53:55 +020056#include <linux/license.h>
Christoph Lameter6d762392008-02-08 04:18:42 -080057#include <asm/sections.h>
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -040058#include <linux/tracepoint.h>
Steven Rostedt90d595f2008-08-14 15:45:09 -040059#include <linux/ftrace.h>
Jessica Yu7e545d62016-03-16 20:55:39 -040060#include <linux/livepatch.h>
Arjan van de Ven22a9d642009-01-07 08:45:46 -080061#include <linux/async.h>
Tejun Heofbf59bc2009-02-20 16:29:08 +090062#include <linux/percpu.h>
Catalin Marinas4f2294b2009-06-11 13:23:20 +010063#include <linux/kmemleak.h>
Jason Baronbf5438fc2010-09-17 11:09:00 -040064#include <linux/jump_label.h>
matthieu castet84e1c6b2010-11-16 22:35:16 +010065#include <linux/pfn.h>
Alessio Igor Bogani403ed272011-04-20 11:10:52 +020066#include <linux/bsearch.h>
Luis de Bethencourt9d5059c2016-08-02 14:03:47 -070067#include <linux/dynamic_debug.h>
Richard Guy Briggsca86cad2017-02-04 13:10:38 -050068#include <linux/audit.h>
Rusty Russell2f3238a2012-10-22 18:09:41 +103069#include <uapi/linux/module.h>
Rusty Russell106a4ee2012-09-26 10:09:40 +010070#include "module-internal.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
Li Zefan7ead8b82009-08-17 16:56:28 +080072#define CREATE_TRACE_POINTS
73#include <trace/events/module.h>
74
Linus Torvalds1da177e2005-04-16 15:20:36 -070075#ifndef ARCH_SHF_SMALL
76#define ARCH_SHF_SMALL 0
77#endif
78
matthieu castet84e1c6b2010-11-16 22:35:16 +010079/*
80 * Modules' sections will be aligned on page boundaries
81 * to ensure complete separation of code and data, but
Laura Abbott0f5bf6d2017-02-06 16:31:58 -080082 * only when CONFIG_STRICT_MODULE_RWX=y
matthieu castet84e1c6b2010-11-16 22:35:16 +010083 */
Laura Abbott0f5bf6d2017-02-06 16:31:58 -080084#ifdef CONFIG_STRICT_MODULE_RWX
matthieu castet84e1c6b2010-11-16 22:35:16 +010085# define debug_align(X) ALIGN(X, PAGE_SIZE)
86#else
87# define debug_align(X) (X)
88#endif
89
Linus Torvalds1da177e2005-04-16 15:20:36 -070090/* If this is set, the section belongs in the init part of the module */
91#define INIT_OFFSET_MASK (1UL << (BITS_PER_LONG-1))
92
Rusty Russell75676502010-06-05 11:17:36 -060093/*
94 * Mutex protects:
95 * 1) List of modules (also safely readable with preempt_disable),
96 * 2) module_use links,
97 * 3) module_addr_min/module_addr_max.
Masami Hiramatsue513cc12014-11-10 09:30:29 +103098 * (delete and add uses RCU list operations). */
Tim Abbottc6b37802008-12-05 19:03:59 -050099DEFINE_MUTEX(module_mutex);
100EXPORT_SYMBOL_GPL(module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101static LIST_HEAD(modules);
Peter Zijlstra93c2e102015-05-27 11:09:37 +0930102
Peter Zijlstra6c9692e2015-05-27 11:09:37 +0930103#ifdef CONFIG_MODULES_TREE_LOOKUP
104
Peter Zijlstra93c2e102015-05-27 11:09:37 +0930105/*
106 * Use a latched RB-tree for __module_address(); this allows us to use
107 * RCU-sched lookups of the address from any context.
108 *
Peter Zijlstra6c9692e2015-05-27 11:09:37 +0930109 * This is conditional on PERF_EVENTS || TRACING because those can really hit
110 * __module_address() hard by doing a lot of stack unwinding; potentially from
111 * NMI context.
Peter Zijlstra93c2e102015-05-27 11:09:37 +0930112 */
113
114static __always_inline unsigned long __mod_tree_val(struct latch_tree_node *n)
115{
Rusty Russell7523e4d2015-11-26 09:44:08 +1030116 struct module_layout *layout = container_of(n, struct module_layout, mtn.node);
Peter Zijlstra93c2e102015-05-27 11:09:37 +0930117
Rusty Russell7523e4d2015-11-26 09:44:08 +1030118 return (unsigned long)layout->base;
Peter Zijlstra93c2e102015-05-27 11:09:37 +0930119}
120
121static __always_inline unsigned long __mod_tree_size(struct latch_tree_node *n)
122{
Rusty Russell7523e4d2015-11-26 09:44:08 +1030123 struct module_layout *layout = container_of(n, struct module_layout, mtn.node);
Peter Zijlstra93c2e102015-05-27 11:09:37 +0930124
Rusty Russell7523e4d2015-11-26 09:44:08 +1030125 return (unsigned long)layout->size;
Peter Zijlstra93c2e102015-05-27 11:09:37 +0930126}
127
128static __always_inline bool
129mod_tree_less(struct latch_tree_node *a, struct latch_tree_node *b)
130{
131 return __mod_tree_val(a) < __mod_tree_val(b);
132}
133
134static __always_inline int
135mod_tree_comp(void *key, struct latch_tree_node *n)
136{
137 unsigned long val = (unsigned long)key;
138 unsigned long start, end;
139
140 start = __mod_tree_val(n);
141 if (val < start)
142 return -1;
143
144 end = start + __mod_tree_size(n);
145 if (val >= end)
146 return 1;
147
148 return 0;
149}
150
151static const struct latch_tree_ops mod_tree_ops = {
152 .less = mod_tree_less,
153 .comp = mod_tree_comp,
154};
155
Peter Zijlstra4f6665462015-05-27 11:09:38 +0930156static struct mod_tree_root {
157 struct latch_tree_root root;
158 unsigned long addr_min;
159 unsigned long addr_max;
160} mod_tree __cacheline_aligned = {
161 .addr_min = -1UL,
162};
163
164#define module_addr_min mod_tree.addr_min
165#define module_addr_max mod_tree.addr_max
166
167static noinline void __mod_tree_insert(struct mod_tree_node *node)
168{
169 latch_tree_insert(&node->node, &mod_tree.root, &mod_tree_ops);
170}
171
172static void __mod_tree_remove(struct mod_tree_node *node)
173{
174 latch_tree_erase(&node->node, &mod_tree.root, &mod_tree_ops);
175}
Peter Zijlstra93c2e102015-05-27 11:09:37 +0930176
177/*
178 * These modifications: insert, remove_init and remove; are serialized by the
179 * module_mutex.
180 */
181static void mod_tree_insert(struct module *mod)
182{
Rusty Russell7523e4d2015-11-26 09:44:08 +1030183 mod->core_layout.mtn.mod = mod;
184 mod->init_layout.mtn.mod = mod;
Peter Zijlstra93c2e102015-05-27 11:09:37 +0930185
Rusty Russell7523e4d2015-11-26 09:44:08 +1030186 __mod_tree_insert(&mod->core_layout.mtn);
187 if (mod->init_layout.size)
188 __mod_tree_insert(&mod->init_layout.mtn);
Peter Zijlstra93c2e102015-05-27 11:09:37 +0930189}
190
191static void mod_tree_remove_init(struct module *mod)
192{
Rusty Russell7523e4d2015-11-26 09:44:08 +1030193 if (mod->init_layout.size)
194 __mod_tree_remove(&mod->init_layout.mtn);
Peter Zijlstra93c2e102015-05-27 11:09:37 +0930195}
196
197static void mod_tree_remove(struct module *mod)
198{
Rusty Russell7523e4d2015-11-26 09:44:08 +1030199 __mod_tree_remove(&mod->core_layout.mtn);
Peter Zijlstra93c2e102015-05-27 11:09:37 +0930200 mod_tree_remove_init(mod);
201}
202
Peter Zijlstra6c9692e2015-05-27 11:09:37 +0930203static struct module *mod_find(unsigned long addr)
Peter Zijlstra93c2e102015-05-27 11:09:37 +0930204{
205 struct latch_tree_node *ltn;
206
Peter Zijlstra4f6665462015-05-27 11:09:38 +0930207 ltn = latch_tree_find((void *)addr, &mod_tree.root, &mod_tree_ops);
Peter Zijlstra93c2e102015-05-27 11:09:37 +0930208 if (!ltn)
209 return NULL;
210
211 return container_of(ltn, struct mod_tree_node, node)->mod;
212}
213
Peter Zijlstra6c9692e2015-05-27 11:09:37 +0930214#else /* MODULES_TREE_LOOKUP */
215
Peter Zijlstra4f6665462015-05-27 11:09:38 +0930216static unsigned long module_addr_min = -1UL, module_addr_max = 0;
217
Peter Zijlstra6c9692e2015-05-27 11:09:37 +0930218static void mod_tree_insert(struct module *mod) { }
219static void mod_tree_remove_init(struct module *mod) { }
220static void mod_tree_remove(struct module *mod) { }
221
222static struct module *mod_find(unsigned long addr)
223{
224 struct module *mod;
225
226 list_for_each_entry_rcu(mod, &modules, list) {
227 if (within_module(addr, mod))
228 return mod;
229 }
230
231 return NULL;
232}
233
234#endif /* MODULES_TREE_LOOKUP */
235
Peter Zijlstra4f6665462015-05-27 11:09:38 +0930236/*
237 * Bounds of module text, for speeding up __module_address.
238 * Protected by module_mutex.
239 */
240static void __mod_update_bounds(void *base, unsigned int size)
241{
242 unsigned long min = (unsigned long)base;
243 unsigned long max = min + size;
244
245 if (min < module_addr_min)
246 module_addr_min = min;
247 if (max > module_addr_max)
248 module_addr_max = max;
249}
250
251static void mod_update_bounds(struct module *mod)
252{
Rusty Russell7523e4d2015-11-26 09:44:08 +1030253 __mod_update_bounds(mod->core_layout.base, mod->core_layout.size);
254 if (mod->init_layout.size)
255 __mod_update_bounds(mod->init_layout.base, mod->init_layout.size);
Peter Zijlstra4f6665462015-05-27 11:09:38 +0930256}
257
Jason Wessel67fc4e02010-05-20 21:04:21 -0500258#ifdef CONFIG_KGDB_KDB
259struct list_head *kdb_modules = &modules; /* kdb needs the list of modules */
260#endif /* CONFIG_KGDB_KDB */
261
Peter Zijlstra0be964b2015-05-27 11:09:35 +0930262static void module_assert_mutex(void)
Rusty Russell106a4ee2012-09-26 10:09:40 +0100263{
Peter Zijlstra0be964b2015-05-27 11:09:35 +0930264 lockdep_assert_held(&module_mutex);
Rusty Russell106a4ee2012-09-26 10:09:40 +0100265}
266
Peter Zijlstra0be964b2015-05-27 11:09:35 +0930267static void module_assert_mutex_or_preempt(void)
268{
269#ifdef CONFIG_LOCKDEP
270 if (unlikely(!debug_locks))
271 return;
Rusty Russell106a4ee2012-09-26 10:09:40 +0100272
Steven Rostedt95025142016-07-19 05:59:24 +0930273 WARN_ON_ONCE(!rcu_read_lock_sched_held() &&
Peter Zijlstra0be964b2015-05-27 11:09:35 +0930274 !lockdep_is_held(&module_mutex));
275#endif
276}
277
Luis R. Rodriguez6727bb92015-05-27 11:09:39 +0930278static bool sig_enforce = IS_ENABLED(CONFIG_MODULE_SIG_FORCE);
279#ifndef CONFIG_MODULE_SIG_FORCE
Rusty Russell106a4ee2012-09-26 10:09:40 +0100280module_param(sig_enforce, bool_enable_only, 0644);
281#endif /* !CONFIG_MODULE_SIG_FORCE */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282
Stephen Rothwell19e45292009-04-14 17:27:18 +1000283/* Block module loading/unloading? */
284int modules_disabled = 0;
Dave Young02608be2012-02-01 10:33:14 +0800285core_param(nomodule, modules_disabled, bint, 0);
Stephen Rothwell19e45292009-04-14 17:27:18 +1000286
Rusty Russellc9a3ba52008-01-29 17:13:18 -0500287/* Waiting for a module to finish initializing? */
288static DECLARE_WAIT_QUEUE_HEAD(module_wq);
289
Alan Sterne041c682006-03-27 01:16:30 -0800290static BLOCKING_NOTIFIER_HEAD(module_notify_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291
Ionut Alexa6da0b562014-11-10 09:31:29 +1030292int register_module_notifier(struct notifier_block *nb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293{
Alan Sterne041c682006-03-27 01:16:30 -0800294 return blocking_notifier_chain_register(&module_notify_list, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295}
296EXPORT_SYMBOL(register_module_notifier);
297
Ionut Alexa6da0b562014-11-10 09:31:29 +1030298int unregister_module_notifier(struct notifier_block *nb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299{
Alan Sterne041c682006-03-27 01:16:30 -0800300 return blocking_notifier_chain_unregister(&module_notify_list, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301}
302EXPORT_SYMBOL(unregister_module_notifier);
303
Rusty Russelleded41c2010-08-05 12:59:07 -0600304struct load_info {
305 Elf_Ehdr *hdr;
306 unsigned long len;
307 Elf_Shdr *sechdrs;
Rusty Russell6526c532010-08-05 12:59:10 -0600308 char *secstrings, *strtab;
Rusty Russelld9131882010-08-05 12:59:08 -0600309 unsigned long symoffs, stroffs;
Rusty Russell811d66a2010-08-05 12:59:12 -0600310 struct _ddebug *debug;
311 unsigned int num_debug;
Rusty Russell106a4ee2012-09-26 10:09:40 +0100312 bool sig_ok;
Rusty Russell82440622016-02-03 16:55:26 +1030313#ifdef CONFIG_KALLSYMS
314 unsigned long mod_kallsyms_init_off;
315#endif
Rusty Russelleded41c2010-08-05 12:59:07 -0600316 struct {
317 unsigned int sym, str, mod, vers, info, pcpu;
318 } index;
319};
320
Miroslav Benes71d9f502016-11-16 16:45:48 +0100321/*
322 * We require a truly strong try_module_get(): 0 means success.
323 * Otherwise an error is returned due to ongoing or failed
324 * initialization etc.
325 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326static inline int strong_try_module_get(struct module *mod)
327{
Rusty Russell0d21b0e2013-01-12 11:38:44 +1030328 BUG_ON(mod && mod->state == MODULE_STATE_UNFORMED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 if (mod && mod->state == MODULE_STATE_COMING)
Rusty Russellc9a3ba52008-01-29 17:13:18 -0500330 return -EBUSY;
331 if (try_module_get(mod))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 return 0;
Rusty Russellc9a3ba52008-01-29 17:13:18 -0500333 else
334 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335}
336
Rusty Russell373d4d02013-01-21 17:17:39 +1030337static inline void add_taint_module(struct module *mod, unsigned flag,
338 enum lockdep_ok lockdep_ok)
Florin Malitafa3ba2e82006-10-11 01:21:48 -0700339{
Rusty Russell373d4d02013-01-21 17:17:39 +1030340 add_taint(flag, lockdep_ok);
Petr Mladek7fd83292016-09-21 13:47:22 +0200341 set_bit(flag, &mod->taints);
Florin Malitafa3ba2e82006-10-11 01:21:48 -0700342}
343
Robert P. J. Day02a3e592007-05-09 07:26:28 +0200344/*
345 * A thread that wants to hold a reference to a module only while it
346 * is running can call this to safely exit. nfsd and lockd use this.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 */
Jiri Kosinabf262dc2016-04-12 05:02:09 +0930348void __noreturn __module_put_and_exit(struct module *mod, long code)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349{
350 module_put(mod);
351 do_exit(code);
352}
353EXPORT_SYMBOL(__module_put_and_exit);
Daniel Walker22a8bde2007-10-18 03:06:07 -0700354
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355/* Find a module section: 0 means not found. */
Rusty Russell49668682010-08-05 12:59:10 -0600356static unsigned int find_sec(const struct load_info *info, const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357{
358 unsigned int i;
359
Rusty Russell49668682010-08-05 12:59:10 -0600360 for (i = 1; i < info->hdr->e_shnum; i++) {
361 Elf_Shdr *shdr = &info->sechdrs[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 /* Alloc bit cleared means "ignore it." */
Rusty Russell49668682010-08-05 12:59:10 -0600363 if ((shdr->sh_flags & SHF_ALLOC)
364 && strcmp(info->secstrings + shdr->sh_name, name) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 return i;
Rusty Russell49668682010-08-05 12:59:10 -0600366 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 return 0;
368}
369
Rusty Russell5e458cc2008-10-22 10:00:13 -0500370/* Find a module section, or NULL. */
Rusty Russell49668682010-08-05 12:59:10 -0600371static void *section_addr(const struct load_info *info, const char *name)
Rusty Russell5e458cc2008-10-22 10:00:13 -0500372{
373 /* Section 0 has sh_addr 0. */
Rusty Russell49668682010-08-05 12:59:10 -0600374 return (void *)info->sechdrs[find_sec(info, name)].sh_addr;
Rusty Russell5e458cc2008-10-22 10:00:13 -0500375}
376
377/* Find a module section, or NULL. Fill in number of "objects" in section. */
Rusty Russell49668682010-08-05 12:59:10 -0600378static void *section_objs(const struct load_info *info,
Rusty Russell5e458cc2008-10-22 10:00:13 -0500379 const char *name,
380 size_t object_size,
381 unsigned int *num)
382{
Rusty Russell49668682010-08-05 12:59:10 -0600383 unsigned int sec = find_sec(info, name);
Rusty Russell5e458cc2008-10-22 10:00:13 -0500384
385 /* Section 0 has sh_addr 0 and sh_size 0. */
Rusty Russell49668682010-08-05 12:59:10 -0600386 *num = info->sechdrs[sec].sh_size / object_size;
387 return (void *)info->sechdrs[sec].sh_addr;
Rusty Russell5e458cc2008-10-22 10:00:13 -0500388}
389
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390/* Provided by the linker */
391extern const struct kernel_symbol __start___ksymtab[];
392extern const struct kernel_symbol __stop___ksymtab[];
393extern const struct kernel_symbol __start___ksymtab_gpl[];
394extern const struct kernel_symbol __stop___ksymtab_gpl[];
Greg Kroah-Hartman9f28bb72006-03-20 13:17:13 -0800395extern const struct kernel_symbol __start___ksymtab_gpl_future[];
396extern const struct kernel_symbol __stop___ksymtab_gpl_future[];
Ard Biesheuvel71810db2017-02-03 09:54:06 +0000397extern const s32 __start___kcrctab[];
398extern const s32 __start___kcrctab_gpl[];
399extern const s32 __start___kcrctab_gpl_future[];
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500400#ifdef CONFIG_UNUSED_SYMBOLS
401extern const struct kernel_symbol __start___ksymtab_unused[];
402extern const struct kernel_symbol __stop___ksymtab_unused[];
403extern const struct kernel_symbol __start___ksymtab_unused_gpl[];
404extern const struct kernel_symbol __stop___ksymtab_unused_gpl[];
Ard Biesheuvel71810db2017-02-03 09:54:06 +0000405extern const s32 __start___kcrctab_unused[];
406extern const s32 __start___kcrctab_unused_gpl[];
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500407#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408
409#ifndef CONFIG_MODVERSIONS
410#define symversion(base, idx) NULL
411#else
Andrew Mortonf83ca9f2006-03-28 01:56:20 -0800412#define symversion(base, idx) ((base != NULL) ? ((base) + (idx)) : NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413#endif
414
Rusty Russelldafd0942008-07-22 19:24:25 -0500415static bool each_symbol_in_section(const struct symsearch *arr,
416 unsigned int arrsize,
417 struct module *owner,
418 bool (*fn)(const struct symsearch *syms,
419 struct module *owner,
Rusty Russellde4d8d52011-04-19 21:49:58 +0200420 void *data),
Rusty Russelldafd0942008-07-22 19:24:25 -0500421 void *data)
Sam Ravnborg3fd68052006-02-08 21:16:45 +0100422{
Rusty Russellde4d8d52011-04-19 21:49:58 +0200423 unsigned int j;
Rusty Russelldafd0942008-07-22 19:24:25 -0500424
425 for (j = 0; j < arrsize; j++) {
Rusty Russellde4d8d52011-04-19 21:49:58 +0200426 if (fn(&arr[j], owner, data))
427 return true;
Rusty Russelldafd0942008-07-22 19:24:25 -0500428 }
429
430 return false;
Sam Ravnborg3fd68052006-02-08 21:16:45 +0100431}
432
Rusty Russelldafd0942008-07-22 19:24:25 -0500433/* Returns true as soon as fn returns true, otherwise false. */
Rusty Russellde4d8d52011-04-19 21:49:58 +0200434bool each_symbol_section(bool (*fn)(const struct symsearch *arr,
435 struct module *owner,
436 void *data),
437 void *data)
Arjan van de Venf71d20e2006-06-28 04:26:45 -0700438{
Rusty Russelldafd0942008-07-22 19:24:25 -0500439 struct module *mod;
Linus Torvalds44032e62010-08-05 12:59:05 -0600440 static const struct symsearch arr[] = {
Rusty Russelldafd0942008-07-22 19:24:25 -0500441 { __start___ksymtab, __stop___ksymtab, __start___kcrctab,
442 NOT_GPL_ONLY, false },
443 { __start___ksymtab_gpl, __stop___ksymtab_gpl,
444 __start___kcrctab_gpl,
445 GPL_ONLY, false },
446 { __start___ksymtab_gpl_future, __stop___ksymtab_gpl_future,
447 __start___kcrctab_gpl_future,
448 WILL_BE_GPL_ONLY, false },
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500449#ifdef CONFIG_UNUSED_SYMBOLS
Rusty Russelldafd0942008-07-22 19:24:25 -0500450 { __start___ksymtab_unused, __stop___ksymtab_unused,
451 __start___kcrctab_unused,
452 NOT_GPL_ONLY, true },
453 { __start___ksymtab_unused_gpl, __stop___ksymtab_unused_gpl,
454 __start___kcrctab_unused_gpl,
455 GPL_ONLY, true },
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500456#endif
Rusty Russelldafd0942008-07-22 19:24:25 -0500457 };
458
Peter Zijlstra0be964b2015-05-27 11:09:35 +0930459 module_assert_mutex_or_preempt();
460
Rusty Russelldafd0942008-07-22 19:24:25 -0500461 if (each_symbol_in_section(arr, ARRAY_SIZE(arr), NULL, fn, data))
462 return true;
463
Andi Kleend72b3752008-08-30 10:09:00 +0200464 list_for_each_entry_rcu(mod, &modules, list) {
Rusty Russelldafd0942008-07-22 19:24:25 -0500465 struct symsearch arr[] = {
466 { mod->syms, mod->syms + mod->num_syms, mod->crcs,
467 NOT_GPL_ONLY, false },
468 { mod->gpl_syms, mod->gpl_syms + mod->num_gpl_syms,
469 mod->gpl_crcs,
470 GPL_ONLY, false },
471 { mod->gpl_future_syms,
472 mod->gpl_future_syms + mod->num_gpl_future_syms,
473 mod->gpl_future_crcs,
474 WILL_BE_GPL_ONLY, false },
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500475#ifdef CONFIG_UNUSED_SYMBOLS
Rusty Russelldafd0942008-07-22 19:24:25 -0500476 { mod->unused_syms,
477 mod->unused_syms + mod->num_unused_syms,
478 mod->unused_crcs,
479 NOT_GPL_ONLY, true },
480 { mod->unused_gpl_syms,
481 mod->unused_gpl_syms + mod->num_unused_gpl_syms,
482 mod->unused_gpl_crcs,
483 GPL_ONLY, true },
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500484#endif
Rusty Russelldafd0942008-07-22 19:24:25 -0500485 };
486
Rusty Russell0d21b0e2013-01-12 11:38:44 +1030487 if (mod->state == MODULE_STATE_UNFORMED)
488 continue;
489
Rusty Russelldafd0942008-07-22 19:24:25 -0500490 if (each_symbol_in_section(arr, ARRAY_SIZE(arr), mod, fn, data))
491 return true;
492 }
493 return false;
Arjan van de Venf71d20e2006-06-28 04:26:45 -0700494}
Rusty Russellde4d8d52011-04-19 21:49:58 +0200495EXPORT_SYMBOL_GPL(each_symbol_section);
Arjan van de Venf71d20e2006-06-28 04:26:45 -0700496
Rusty Russelldafd0942008-07-22 19:24:25 -0500497struct find_symbol_arg {
498 /* Input */
499 const char *name;
500 bool gplok;
501 bool warn;
502
503 /* Output */
504 struct module *owner;
Ard Biesheuvel71810db2017-02-03 09:54:06 +0000505 const s32 *crc;
Tim Abbott414fd312008-12-05 19:03:56 -0500506 const struct kernel_symbol *sym;
Rusty Russelldafd0942008-07-22 19:24:25 -0500507};
508
Rusty Russellde4d8d52011-04-19 21:49:58 +0200509static bool check_symbol(const struct symsearch *syms,
510 struct module *owner,
511 unsigned int symnum, void *data)
Rusty Russellad9546c2008-05-01 21:14:59 -0500512{
Rusty Russelldafd0942008-07-22 19:24:25 -0500513 struct find_symbol_arg *fsa = data;
514
Rusty Russelldafd0942008-07-22 19:24:25 -0500515 if (!fsa->gplok) {
516 if (syms->licence == GPL_ONLY)
517 return false;
518 if (syms->licence == WILL_BE_GPL_ONLY && fsa->warn) {
Andrew Mortonbddb12b2013-11-12 15:11:28 -0800519 pr_warn("Symbol %s is being used by a non-GPL module, "
520 "which will not be allowed in the future\n",
521 fsa->name);
Rusty Russelldafd0942008-07-22 19:24:25 -0500522 }
523 }
524
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500525#ifdef CONFIG_UNUSED_SYMBOLS
Rusty Russelldafd0942008-07-22 19:24:25 -0500526 if (syms->unused && fsa->warn) {
Andrew Mortonbddb12b2013-11-12 15:11:28 -0800527 pr_warn("Symbol %s is marked as UNUSED, however this module is "
528 "using it.\n", fsa->name);
529 pr_warn("This symbol will go away in the future.\n");
Yannick Guerrini7b63c3a2015-03-24 12:31:40 +1030530 pr_warn("Please evaluate if this is the right api to use and "
531 "if it really is, submit a report to the linux kernel "
532 "mailing list together with submitting your code for "
Andrew Mortonbddb12b2013-11-12 15:11:28 -0800533 "inclusion.\n");
Rusty Russellad9546c2008-05-01 21:14:59 -0500534 }
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500535#endif
Rusty Russelldafd0942008-07-22 19:24:25 -0500536
537 fsa->owner = owner;
538 fsa->crc = symversion(syms->crcs, symnum);
Tim Abbott414fd312008-12-05 19:03:56 -0500539 fsa->sym = &syms->start[symnum];
Rusty Russellad9546c2008-05-01 21:14:59 -0500540 return true;
541}
542
Alessio Igor Bogani403ed272011-04-20 11:10:52 +0200543static int cmp_name(const void *va, const void *vb)
544{
545 const char *a;
546 const struct kernel_symbol *b;
547 a = va; b = vb;
548 return strcmp(a, b->name);
549}
550
Rusty Russellde4d8d52011-04-19 21:49:58 +0200551static bool find_symbol_in_section(const struct symsearch *syms,
552 struct module *owner,
553 void *data)
554{
555 struct find_symbol_arg *fsa = data;
Alessio Igor Bogani403ed272011-04-20 11:10:52 +0200556 struct kernel_symbol *sym;
Rusty Russellde4d8d52011-04-19 21:49:58 +0200557
Alessio Igor Bogani403ed272011-04-20 11:10:52 +0200558 sym = bsearch(fsa->name, syms->start, syms->stop - syms->start,
559 sizeof(struct kernel_symbol), cmp_name);
560
561 if (sym != NULL && check_symbol(syms, owner, sym - syms->start, data))
562 return true;
563
Rusty Russellde4d8d52011-04-19 21:49:58 +0200564 return false;
565}
566
Tim Abbott414fd312008-12-05 19:03:56 -0500567/* Find a symbol and return it, along with, (optional) crc and
Rusty Russell75676502010-06-05 11:17:36 -0600568 * (optional) module which owns it. Needs preempt disabled or module_mutex. */
Tim Abbottc6b37802008-12-05 19:03:59 -0500569const struct kernel_symbol *find_symbol(const char *name,
570 struct module **owner,
Ard Biesheuvel71810db2017-02-03 09:54:06 +0000571 const s32 **crc,
Tim Abbottc6b37802008-12-05 19:03:59 -0500572 bool gplok,
573 bool warn)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574{
Rusty Russelldafd0942008-07-22 19:24:25 -0500575 struct find_symbol_arg fsa;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576
Rusty Russelldafd0942008-07-22 19:24:25 -0500577 fsa.name = name;
578 fsa.gplok = gplok;
579 fsa.warn = warn;
580
Rusty Russellde4d8d52011-04-19 21:49:58 +0200581 if (each_symbol_section(find_symbol_in_section, &fsa)) {
Rusty Russellad9546c2008-05-01 21:14:59 -0500582 if (owner)
Rusty Russelldafd0942008-07-22 19:24:25 -0500583 *owner = fsa.owner;
584 if (crc)
585 *crc = fsa.crc;
Tim Abbott414fd312008-12-05 19:03:56 -0500586 return fsa.sym;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 }
Rusty Russellad9546c2008-05-01 21:14:59 -0500588
Jim Cromie5e124162011-12-06 12:11:31 -0700589 pr_debug("Failed to find symbol %s\n", name);
Tim Abbott414fd312008-12-05 19:03:56 -0500590 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591}
Tim Abbottc6b37802008-12-05 19:03:59 -0500592EXPORT_SYMBOL_GPL(find_symbol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593
Rusty Russellfe0d34d2015-07-29 05:52:14 +0930594/*
595 * Search for module by name: must hold module_mutex (or preempt disabled
596 * for read-only access).
597 */
Mathias Krause4f6de4d52013-07-02 15:35:11 +0930598static struct module *find_module_all(const char *name, size_t len,
Rusty Russell0d21b0e2013-01-12 11:38:44 +1030599 bool even_unformed)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600{
601 struct module *mod;
602
Rusty Russellfe0d34d2015-07-29 05:52:14 +0930603 module_assert_mutex_or_preempt();
Peter Zijlstra0be964b2015-05-27 11:09:35 +0930604
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 list_for_each_entry(mod, &modules, list) {
Rusty Russell0d21b0e2013-01-12 11:38:44 +1030606 if (!even_unformed && mod->state == MODULE_STATE_UNFORMED)
607 continue;
Mathias Krause4f6de4d52013-07-02 15:35:11 +0930608 if (strlen(mod->name) == len && !memcmp(mod->name, name, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 return mod;
610 }
611 return NULL;
612}
Rusty Russell0d21b0e2013-01-12 11:38:44 +1030613
614struct module *find_module(const char *name)
615{
Rusty Russellfe0d34d2015-07-29 05:52:14 +0930616 module_assert_mutex();
Mathias Krause4f6de4d52013-07-02 15:35:11 +0930617 return find_module_all(name, strlen(name), false);
Rusty Russell0d21b0e2013-01-12 11:38:44 +1030618}
Tim Abbottc6b37802008-12-05 19:03:59 -0500619EXPORT_SYMBOL_GPL(find_module);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620
621#ifdef CONFIG_SMP
Tejun Heofbf59bc2009-02-20 16:29:08 +0900622
Tejun Heo259354d2010-03-10 18:56:10 +0900623static inline void __percpu *mod_percpu(struct module *mod)
Tejun Heofbf59bc2009-02-20 16:29:08 +0900624{
Tejun Heo259354d2010-03-10 18:56:10 +0900625 return mod->percpu;
626}
Tejun Heofbf59bc2009-02-20 16:29:08 +0900627
Rusty Russell9eb76d72013-07-03 10:06:29 +0930628static int percpu_modalloc(struct module *mod, struct load_info *info)
Tejun Heo259354d2010-03-10 18:56:10 +0900629{
Rusty Russell9eb76d72013-07-03 10:06:29 +0930630 Elf_Shdr *pcpusec = &info->sechdrs[info->index.pcpu];
631 unsigned long align = pcpusec->sh_addralign;
632
633 if (!pcpusec->sh_size)
634 return 0;
635
Tejun Heofbf59bc2009-02-20 16:29:08 +0900636 if (align > PAGE_SIZE) {
Andrew Mortonbddb12b2013-11-12 15:11:28 -0800637 pr_warn("%s: per-cpu alignment %li > %li\n",
638 mod->name, align, PAGE_SIZE);
Tejun Heofbf59bc2009-02-20 16:29:08 +0900639 align = PAGE_SIZE;
640 }
641
Rusty Russell9eb76d72013-07-03 10:06:29 +0930642 mod->percpu = __alloc_reserved_percpu(pcpusec->sh_size, align);
Tejun Heo259354d2010-03-10 18:56:10 +0900643 if (!mod->percpu) {
Andrew Mortonbddb12b2013-11-12 15:11:28 -0800644 pr_warn("%s: Could not allocate %lu bytes percpu data\n",
645 mod->name, (unsigned long)pcpusec->sh_size);
Tejun Heo259354d2010-03-10 18:56:10 +0900646 return -ENOMEM;
647 }
Rusty Russell9eb76d72013-07-03 10:06:29 +0930648 mod->percpu_size = pcpusec->sh_size;
Tejun Heo259354d2010-03-10 18:56:10 +0900649 return 0;
Tejun Heofbf59bc2009-02-20 16:29:08 +0900650}
651
Tejun Heo259354d2010-03-10 18:56:10 +0900652static void percpu_modfree(struct module *mod)
Tejun Heofbf59bc2009-02-20 16:29:08 +0900653{
Tejun Heo259354d2010-03-10 18:56:10 +0900654 free_percpu(mod->percpu);
Tejun Heofbf59bc2009-02-20 16:29:08 +0900655}
656
Rusty Russell49668682010-08-05 12:59:10 -0600657static unsigned int find_pcpusec(struct load_info *info)
Tejun Heo6b588c12009-02-20 16:29:07 +0900658{
Rusty Russell49668682010-08-05 12:59:10 -0600659 return find_sec(info, ".data..percpu");
Tejun Heo6b588c12009-02-20 16:29:07 +0900660}
661
Tejun Heo259354d2010-03-10 18:56:10 +0900662static void percpu_modcopy(struct module *mod,
663 const void *from, unsigned long size)
Tejun Heo6b588c12009-02-20 16:29:07 +0900664{
665 int cpu;
666
667 for_each_possible_cpu(cpu)
Tejun Heo259354d2010-03-10 18:56:10 +0900668 memcpy(per_cpu_ptr(mod->percpu, cpu), from, size);
Tejun Heo6b588c12009-02-20 16:29:07 +0900669}
670
Thomas Gleixner383776f2017-02-27 15:37:36 +0100671bool __is_module_percpu_address(unsigned long addr, unsigned long *can_addr)
Tejun Heo10fad5e2010-03-10 18:57:54 +0900672{
673 struct module *mod;
674 unsigned int cpu;
675
676 preempt_disable();
677
678 list_for_each_entry_rcu(mod, &modules, list) {
Rusty Russell0d21b0e2013-01-12 11:38:44 +1030679 if (mod->state == MODULE_STATE_UNFORMED)
680 continue;
Tejun Heo10fad5e2010-03-10 18:57:54 +0900681 if (!mod->percpu_size)
682 continue;
683 for_each_possible_cpu(cpu) {
684 void *start = per_cpu_ptr(mod->percpu, cpu);
Thomas Gleixner383776f2017-02-27 15:37:36 +0100685 void *va = (void *)addr;
Tejun Heo10fad5e2010-03-10 18:57:54 +0900686
Thomas Gleixner383776f2017-02-27 15:37:36 +0100687 if (va >= start && va < start + mod->percpu_size) {
Peter Zijlstra8ce371f2017-03-20 12:26:55 +0100688 if (can_addr) {
Thomas Gleixner383776f2017-02-27 15:37:36 +0100689 *can_addr = (unsigned long) (va - start);
Peter Zijlstra8ce371f2017-03-20 12:26:55 +0100690 *can_addr += (unsigned long)
691 per_cpu_ptr(mod->percpu,
692 get_boot_cpu_id());
693 }
Tejun Heo10fad5e2010-03-10 18:57:54 +0900694 preempt_enable();
695 return true;
696 }
697 }
698 }
699
700 preempt_enable();
701 return false;
Daniel Walker22a8bde2007-10-18 03:06:07 -0700702}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703
Thomas Gleixner383776f2017-02-27 15:37:36 +0100704/**
705 * is_module_percpu_address - test whether address is from module static percpu
706 * @addr: address to test
707 *
708 * Test whether @addr belongs to module static percpu area.
709 *
710 * RETURNS:
711 * %true if @addr is from module static percpu area
712 */
713bool is_module_percpu_address(unsigned long addr)
714{
715 return __is_module_percpu_address(addr, NULL);
716}
717
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718#else /* ... !CONFIG_SMP */
Tejun Heo6b588c12009-02-20 16:29:07 +0900719
Tejun Heo259354d2010-03-10 18:56:10 +0900720static inline void __percpu *mod_percpu(struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721{
722 return NULL;
723}
Rusty Russell9eb76d72013-07-03 10:06:29 +0930724static int percpu_modalloc(struct module *mod, struct load_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725{
Rusty Russell9eb76d72013-07-03 10:06:29 +0930726 /* UP modules shouldn't have this section: ENOMEM isn't quite right */
727 if (info->sechdrs[info->index.pcpu].sh_size != 0)
728 return -ENOMEM;
729 return 0;
Tejun Heo259354d2010-03-10 18:56:10 +0900730}
731static inline void percpu_modfree(struct module *mod)
732{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733}
Rusty Russell49668682010-08-05 12:59:10 -0600734static unsigned int find_pcpusec(struct load_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735{
736 return 0;
737}
Tejun Heo259354d2010-03-10 18:56:10 +0900738static inline void percpu_modcopy(struct module *mod,
739 const void *from, unsigned long size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740{
741 /* pcpusec should be 0, and size of that section should be 0. */
742 BUG_ON(size != 0);
743}
Tejun Heo10fad5e2010-03-10 18:57:54 +0900744bool is_module_percpu_address(unsigned long addr)
745{
746 return false;
747}
Tejun Heo6b588c12009-02-20 16:29:07 +0900748
Thomas Gleixner383776f2017-02-27 15:37:36 +0100749bool __is_module_percpu_address(unsigned long addr, unsigned long *can_addr)
750{
751 return false;
752}
753
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754#endif /* CONFIG_SMP */
755
Matt Domschc988d2b2005-06-23 22:05:15 -0700756#define MODINFO_ATTR(field) \
757static void setup_modinfo_##field(struct module *mod, const char *s) \
758{ \
759 mod->field = kstrdup(s, GFP_KERNEL); \
760} \
761static ssize_t show_modinfo_##field(struct module_attribute *mattr, \
Kay Sievers4befb022011-07-24 22:06:04 +0930762 struct module_kobject *mk, char *buffer) \
Matt Domschc988d2b2005-06-23 22:05:15 -0700763{ \
Chen Gangcc56ded2013-08-20 15:34:21 +0930764 return scnprintf(buffer, PAGE_SIZE, "%s\n", mk->mod->field); \
Matt Domschc988d2b2005-06-23 22:05:15 -0700765} \
766static int modinfo_##field##_exists(struct module *mod) \
767{ \
768 return mod->field != NULL; \
769} \
770static void free_modinfo_##field(struct module *mod) \
771{ \
Daniel Walker22a8bde2007-10-18 03:06:07 -0700772 kfree(mod->field); \
773 mod->field = NULL; \
Matt Domschc988d2b2005-06-23 22:05:15 -0700774} \
775static struct module_attribute modinfo_##field = { \
Tejun Heo7b595752007-06-14 03:45:17 +0900776 .attr = { .name = __stringify(field), .mode = 0444 }, \
Matt Domschc988d2b2005-06-23 22:05:15 -0700777 .show = show_modinfo_##field, \
778 .setup = setup_modinfo_##field, \
779 .test = modinfo_##field##_exists, \
780 .free = free_modinfo_##field, \
781};
782
783MODINFO_ATTR(version);
784MODINFO_ATTR(srcversion);
785
Arjan van de Vene14af7e2008-01-25 21:08:33 +0100786static char last_unloaded_module[MODULE_NAME_LEN+1];
787
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -0800788#ifdef CONFIG_MODULE_UNLOAD
Steven Rostedteb0c5372010-03-29 14:25:18 -0400789
790EXPORT_TRACEPOINT_SYMBOL(module_get);
791
Masami Hiramatsue513cc12014-11-10 09:30:29 +1030792/* MODULE_REF_BASE is the base reference count by kmodule loader. */
793#define MODULE_REF_BASE 1
794
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795/* Init the unload section of the module. */
Rusty Russell9f85a4b2010-08-05 12:59:04 -0600796static int module_unload_init(struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797{
Masami Hiramatsue513cc12014-11-10 09:30:29 +1030798 /*
799 * Initialize reference counter to MODULE_REF_BASE.
800 * refcnt == 0 means module is going.
801 */
802 atomic_set(&mod->refcnt, MODULE_REF_BASE);
Rusty Russell9f85a4b2010-08-05 12:59:04 -0600803
Linus Torvalds2c02dfe2010-05-31 12:19:37 -0700804 INIT_LIST_HEAD(&mod->source_list);
805 INIT_LIST_HEAD(&mod->target_list);
Christoph Lametere1783a22010-01-05 15:34:50 +0900806
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 /* Hold reference count during initialization. */
Masami Hiramatsue513cc12014-11-10 09:30:29 +1030808 atomic_inc(&mod->refcnt);
Rusty Russell9f85a4b2010-08-05 12:59:04 -0600809
810 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811}
812
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813/* Does a already use b? */
814static int already_uses(struct module *a, struct module *b)
815{
816 struct module_use *use;
817
Linus Torvalds2c02dfe2010-05-31 12:19:37 -0700818 list_for_each_entry(use, &b->source_list, source_list) {
819 if (use->source == a) {
Jim Cromie5e124162011-12-06 12:11:31 -0700820 pr_debug("%s uses %s!\n", a->name, b->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 return 1;
822 }
823 }
Jim Cromie5e124162011-12-06 12:11:31 -0700824 pr_debug("%s does not use %s!\n", a->name, b->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 return 0;
826}
827
Linus Torvalds2c02dfe2010-05-31 12:19:37 -0700828/*
829 * Module a uses b
830 * - we add 'a' as a "source", 'b' as a "target" of module use
831 * - the module_use is added to the list of 'b' sources (so
832 * 'b' can walk the list to see who sourced them), and of 'a'
833 * targets (so 'a' can see what modules it targets).
834 */
835static int add_module_usage(struct module *a, struct module *b)
836{
Linus Torvalds2c02dfe2010-05-31 12:19:37 -0700837 struct module_use *use;
838
Jim Cromie5e124162011-12-06 12:11:31 -0700839 pr_debug("Allocating new usage for %s.\n", a->name);
Linus Torvalds2c02dfe2010-05-31 12:19:37 -0700840 use = kmalloc(sizeof(*use), GFP_ATOMIC);
841 if (!use) {
Andrew Mortonbddb12b2013-11-12 15:11:28 -0800842 pr_warn("%s: out of memory loading\n", a->name);
Linus Torvalds2c02dfe2010-05-31 12:19:37 -0700843 return -ENOMEM;
844 }
845
846 use->source = a;
847 use->target = b;
848 list_add(&use->source_list, &b->source_list);
849 list_add(&use->target_list, &a->target_list);
Linus Torvalds2c02dfe2010-05-31 12:19:37 -0700850 return 0;
851}
852
Rusty Russell75676502010-06-05 11:17:36 -0600853/* Module a uses b: caller needs module_mutex() */
Rusty Russell9bea7f22010-06-05 11:17:37 -0600854int ref_module(struct module *a, struct module *b)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855{
Rusty Russellc8e21ce2010-06-05 11:17:35 -0600856 int err;
Kay Sievers270a6c42007-01-18 13:26:15 +0100857
Rusty Russell9bea7f22010-06-05 11:17:37 -0600858 if (b == NULL || already_uses(a, b))
Linus Torvalds218ce732010-05-25 16:48:30 -0700859 return 0;
Linus Torvalds218ce732010-05-25 16:48:30 -0700860
Rusty Russell9bea7f22010-06-05 11:17:37 -0600861 /* If module isn't available, we fail. */
862 err = strong_try_module_get(b);
Rusty Russellc9a3ba52008-01-29 17:13:18 -0500863 if (err)
Rusty Russell9bea7f22010-06-05 11:17:37 -0600864 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865
Linus Torvalds2c02dfe2010-05-31 12:19:37 -0700866 err = add_module_usage(a, b);
867 if (err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 module_put(b);
Rusty Russell9bea7f22010-06-05 11:17:37 -0600869 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 }
Rusty Russell9bea7f22010-06-05 11:17:37 -0600871 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872}
Rusty Russell9bea7f22010-06-05 11:17:37 -0600873EXPORT_SYMBOL_GPL(ref_module);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874
875/* Clear the unload stuff of the module. */
876static void module_unload_free(struct module *mod)
877{
Linus Torvalds2c02dfe2010-05-31 12:19:37 -0700878 struct module_use *use, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879
Rusty Russell75676502010-06-05 11:17:36 -0600880 mutex_lock(&module_mutex);
Linus Torvalds2c02dfe2010-05-31 12:19:37 -0700881 list_for_each_entry_safe(use, tmp, &mod->target_list, target_list) {
882 struct module *i = use->target;
Jim Cromie5e124162011-12-06 12:11:31 -0700883 pr_debug("%s unusing %s\n", mod->name, i->name);
Linus Torvalds2c02dfe2010-05-31 12:19:37 -0700884 module_put(i);
885 list_del(&use->source_list);
886 list_del(&use->target_list);
887 kfree(use);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 }
Rusty Russell75676502010-06-05 11:17:36 -0600889 mutex_unlock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890}
891
892#ifdef CONFIG_MODULE_FORCE_UNLOAD
Akinobu Mitafb169792006-01-08 01:04:29 -0800893static inline int try_force_unload(unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894{
895 int ret = (flags & O_TRUNC);
896 if (ret)
Rusty Russell373d4d02013-01-21 17:17:39 +1030897 add_taint(TAINT_FORCED_RMMOD, LOCKDEP_NOW_UNRELIABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 return ret;
899}
900#else
Akinobu Mitafb169792006-01-08 01:04:29 -0800901static inline int try_force_unload(unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902{
903 return 0;
904}
905#endif /* CONFIG_MODULE_FORCE_UNLOAD */
906
Masami Hiramatsue513cc12014-11-10 09:30:29 +1030907/* Try to release refcount of module, 0 means success. */
908static int try_release_module_ref(struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909{
Masami Hiramatsue513cc12014-11-10 09:30:29 +1030910 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911
Masami Hiramatsue513cc12014-11-10 09:30:29 +1030912 /* Try to decrement refcnt which we set at loading */
913 ret = atomic_sub_return(MODULE_REF_BASE, &mod->refcnt);
914 BUG_ON(ret < 0);
915 if (ret)
916 /* Someone can put this right now, recover with checking */
917 ret = atomic_add_unless(&mod->refcnt, MODULE_REF_BASE, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918
Masami Hiramatsue513cc12014-11-10 09:30:29 +1030919 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920}
921
922static int try_stop_module(struct module *mod, int flags, int *forced)
923{
Masami Hiramatsue513cc12014-11-10 09:30:29 +1030924 /* If it's not unused, quit unless we're forcing. */
925 if (try_release_module_ref(mod) != 0) {
926 *forced = try_force_unload(flags);
927 if (!(*forced))
928 return -EWOULDBLOCK;
929 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930
Masami Hiramatsue513cc12014-11-10 09:30:29 +1030931 /* Mark it as dying. */
932 mod->state = MODULE_STATE_GOING;
933
934 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935}
936
Rusty Russelld5db1392015-01-22 11:13:14 +1030937/**
938 * module_refcount - return the refcount or -1 if unloading
939 *
940 * @mod: the module we're checking
941 *
942 * Returns:
943 * -1 if the module is in the process of unloading
944 * otherwise the number of references in the kernel to the module
945 */
946int module_refcount(struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947{
Rusty Russelld5db1392015-01-22 11:13:14 +1030948 return atomic_read(&mod->refcnt) - MODULE_REF_BASE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949}
950EXPORT_SYMBOL(module_refcount);
951
952/* This exists whether we can unload or not */
953static void free_module(struct module *mod);
954
Heiko Carstens17da2bd2009-01-14 14:14:10 +0100955SYSCALL_DEFINE2(delete_module, const char __user *, name_user,
956 unsigned int, flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957{
958 struct module *mod;
Greg Kroah-Hartmandfff0a02007-02-23 14:54:57 -0800959 char name[MODULE_NAME_LEN];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 int ret, forced = 0;
961
Kees Cook3d433212009-04-02 15:49:29 -0700962 if (!capable(CAP_SYS_MODULE) || modules_disabled)
Greg Kroah-Hartmandfff0a02007-02-23 14:54:57 -0800963 return -EPERM;
964
965 if (strncpy_from_user(name, name_user, MODULE_NAME_LEN-1) < 0)
966 return -EFAULT;
967 name[MODULE_NAME_LEN-1] = '\0';
968
Richard Guy Briggsf6276ac2017-05-02 10:16:04 -0400969 audit_log_kern_module(name);
970
Tejun Heo3fc1f1e2010-05-06 18:49:20 +0200971 if (mutex_lock_interruptible(&module_mutex) != 0)
972 return -EINTR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973
974 mod = find_module(name);
975 if (!mod) {
976 ret = -ENOENT;
977 goto out;
978 }
979
Linus Torvalds2c02dfe2010-05-31 12:19:37 -0700980 if (!list_empty(&mod->source_list)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 /* Other modules depend on us: get rid of them first. */
982 ret = -EWOULDBLOCK;
983 goto out;
984 }
985
986 /* Doing init or already dying? */
987 if (mod->state != MODULE_STATE_LIVE) {
Rusty Russell3f2b9c92013-09-17 05:48:51 +0930988 /* FIXME: if (force), slam module count damn the torpedoes */
Jim Cromie5e124162011-12-06 12:11:31 -0700989 pr_debug("%s already dying\n", mod->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 ret = -EBUSY;
991 goto out;
992 }
993
994 /* If it has an init func, it must have an exit func to unload */
Rusty Russellaf49d922007-10-16 23:26:27 -0700995 if (mod->init && !mod->exit) {
Akinobu Mitafb169792006-01-08 01:04:29 -0800996 forced = try_force_unload(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 if (!forced) {
998 /* This module can't be removed */
999 ret = -EBUSY;
1000 goto out;
1001 }
1002 }
1003
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 /* Stop the machine so refcounts can't move and disable module. */
1005 ret = try_stop_module(mod, flags, &forced);
1006 if (ret != 0)
1007 goto out;
1008
Peter Oberparleiterdf4b5652008-04-21 14:34:31 +02001009 mutex_unlock(&module_mutex);
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001010 /* Final destruction now no one is using it. */
Peter Oberparleiterdf4b5652008-04-21 14:34:31 +02001011 if (mod->exit != NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 mod->exit();
Peter Oberparleiterdf4b5652008-04-21 14:34:31 +02001013 blocking_notifier_call_chain(&module_notify_list,
1014 MODULE_STATE_GOING, mod);
Jessica Yu7e545d62016-03-16 20:55:39 -04001015 klp_module_going(mod);
Jessica Yu7dcd1822016-02-16 17:32:33 -05001016 ftrace_release_mod(mod);
1017
Arjan van de Ven22a9d642009-01-07 08:45:46 -08001018 async_synchronize_full();
Rusty Russell75676502010-06-05 11:17:36 -06001019
Arjan van de Vene14af7e2008-01-25 21:08:33 +01001020 /* Store the name of the last unloaded module for diagnostic purposes */
Rusty Russellefa53452008-01-29 17:13:20 -05001021 strlcpy(last_unloaded_module, mod->name, sizeof(last_unloaded_module));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022
Rusty Russell75676502010-06-05 11:17:36 -06001023 free_module(mod);
1024 return 0;
1025out:
Ashutosh Naik6389a382006-03-23 03:00:46 -08001026 mutex_unlock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 return ret;
1028}
1029
Jianjun Kongd1e99d72008-12-08 14:26:29 +08001030static inline void print_unload_info(struct seq_file *m, struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031{
1032 struct module_use *use;
1033 int printed_something = 0;
1034
Rusty Russelld5db1392015-01-22 11:13:14 +10301035 seq_printf(m, " %i ", module_refcount(mod));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036
Ionut Alexa6da0b562014-11-10 09:31:29 +10301037 /*
1038 * Always include a trailing , so userspace can differentiate
1039 * between this and the old multi-field proc format.
1040 */
Linus Torvalds2c02dfe2010-05-31 12:19:37 -07001041 list_for_each_entry(use, &mod->source_list, source_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 printed_something = 1;
Linus Torvalds2c02dfe2010-05-31 12:19:37 -07001043 seq_printf(m, "%s,", use->source->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 }
1045
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 if (mod->init != NULL && mod->exit == NULL) {
1047 printed_something = 1;
Ionut Alexa6da0b562014-11-10 09:31:29 +10301048 seq_puts(m, "[permanent],");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 }
1050
1051 if (!printed_something)
Ionut Alexa6da0b562014-11-10 09:31:29 +10301052 seq_puts(m, "-");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053}
1054
1055void __symbol_put(const char *symbol)
1056{
1057 struct module *owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058
Rusty Russell24da1cb2007-07-15 23:41:46 -07001059 preempt_disable();
Tim Abbott414fd312008-12-05 19:03:56 -05001060 if (!find_symbol(symbol, &owner, NULL, true, false))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 BUG();
1062 module_put(owner);
Rusty Russell24da1cb2007-07-15 23:41:46 -07001063 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064}
1065EXPORT_SYMBOL(__symbol_put);
1066
Rusty Russell7d1d16e2009-08-26 22:02:54 +09301067/* Note this assumes addr is a function, which it currently always is. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068void symbol_put_addr(void *addr)
1069{
Trent Piepho5e376612006-05-15 09:44:06 -07001070 struct module *modaddr;
Rusty Russell7d1d16e2009-08-26 22:02:54 +09301071 unsigned long a = (unsigned long)dereference_function_descriptor(addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072
Rusty Russell7d1d16e2009-08-26 22:02:54 +09301073 if (core_kernel_text(a))
Trent Piepho5e376612006-05-15 09:44:06 -07001074 return;
1075
Peter Zijlstra275d7d42015-08-20 10:34:59 +09301076 /*
1077 * Even though we hold a reference on the module; we still need to
1078 * disable preemption in order to safely traverse the data structure.
1079 */
1080 preempt_disable();
Rusty Russell7d1d16e2009-08-26 22:02:54 +09301081 modaddr = __module_text_address(a);
Rusty Russella6e6abd2009-03-31 13:05:31 -06001082 BUG_ON(!modaddr);
Trent Piepho5e376612006-05-15 09:44:06 -07001083 module_put(modaddr);
Peter Zijlstra275d7d42015-08-20 10:34:59 +09301084 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085}
1086EXPORT_SYMBOL_GPL(symbol_put_addr);
1087
1088static ssize_t show_refcnt(struct module_attribute *mattr,
Kay Sievers4befb022011-07-24 22:06:04 +09301089 struct module_kobject *mk, char *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090{
Rusty Russelld5db1392015-01-22 11:13:14 +10301091 return sprintf(buffer, "%i\n", module_refcount(mk->mod));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092}
1093
Kay Sieverscca3e702012-01-13 09:32:15 +10301094static struct module_attribute modinfo_refcnt =
1095 __ATTR(refcnt, 0444, show_refcnt, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096
Steven Rostedtd53799b2012-03-26 12:50:52 +10301097void __module_get(struct module *module)
1098{
1099 if (module) {
1100 preempt_disable();
Masami Hiramatsu2f35c412014-11-10 09:29:29 +10301101 atomic_inc(&module->refcnt);
Steven Rostedtd53799b2012-03-26 12:50:52 +10301102 trace_module_get(module, _RET_IP_);
1103 preempt_enable();
1104 }
1105}
1106EXPORT_SYMBOL(__module_get);
1107
1108bool try_module_get(struct module *module)
1109{
1110 bool ret = true;
1111
1112 if (module) {
1113 preempt_disable();
Masami Hiramatsue513cc12014-11-10 09:30:29 +10301114 /* Note: here, we can fail to get a reference */
1115 if (likely(module_is_live(module) &&
1116 atomic_inc_not_zero(&module->refcnt) != 0))
Steven Rostedtd53799b2012-03-26 12:50:52 +10301117 trace_module_get(module, _RET_IP_);
Masami Hiramatsue513cc12014-11-10 09:30:29 +10301118 else
Steven Rostedtd53799b2012-03-26 12:50:52 +10301119 ret = false;
1120
1121 preempt_enable();
1122 }
1123 return ret;
1124}
1125EXPORT_SYMBOL(try_module_get);
1126
Al Virof6a57032006-10-18 01:47:25 -04001127void module_put(struct module *module)
1128{
Masami Hiramatsue513cc12014-11-10 09:30:29 +10301129 int ret;
1130
Al Virof6a57032006-10-18 01:47:25 -04001131 if (module) {
Christoph Lametere1783a22010-01-05 15:34:50 +09001132 preempt_disable();
Masami Hiramatsue513cc12014-11-10 09:30:29 +10301133 ret = atomic_dec_if_positive(&module->refcnt);
1134 WARN_ON(ret < 0); /* Failed to put refcount */
Li Zefanae832d12010-03-24 10:57:43 +08001135 trace_module_put(module, _RET_IP_);
Christoph Lametere1783a22010-01-05 15:34:50 +09001136 preempt_enable();
Al Virof6a57032006-10-18 01:47:25 -04001137 }
1138}
1139EXPORT_SYMBOL(module_put);
1140
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141#else /* !CONFIG_MODULE_UNLOAD */
Jianjun Kongd1e99d72008-12-08 14:26:29 +08001142static inline void print_unload_info(struct seq_file *m, struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143{
1144 /* We don't know the usage count, or what modules are using. */
Ionut Alexa6da0b562014-11-10 09:31:29 +10301145 seq_puts(m, " - -");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146}
1147
1148static inline void module_unload_free(struct module *mod)
1149{
1150}
1151
Rusty Russell9bea7f22010-06-05 11:17:37 -06001152int ref_module(struct module *a, struct module *b)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153{
Rusty Russell9bea7f22010-06-05 11:17:37 -06001154 return strong_try_module_get(b);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155}
Rusty Russell9bea7f22010-06-05 11:17:37 -06001156EXPORT_SYMBOL_GPL(ref_module);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157
Rusty Russell9f85a4b2010-08-05 12:59:04 -06001158static inline int module_unload_init(struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159{
Rusty Russell9f85a4b2010-08-05 12:59:04 -06001160 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161}
1162#endif /* CONFIG_MODULE_UNLOAD */
1163
Kevin Winchester53999bf2012-01-15 19:32:55 -04001164static size_t module_flags_taint(struct module *mod, char *buf)
1165{
1166 size_t l = 0;
Petr Mladek7fd83292016-09-21 13:47:22 +02001167 int i;
Kevin Winchester53999bf2012-01-15 19:32:55 -04001168
Petr Mladek7fd83292016-09-21 13:47:22 +02001169 for (i = 0; i < TAINT_FLAGS_COUNT; i++) {
1170 if (taint_flags[i].module && test_bit(i, &mod->taints))
Larry Finger5eb7c0d2017-01-01 20:25:25 -06001171 buf[l++] = taint_flags[i].c_true;
Petr Mladek7fd83292016-09-21 13:47:22 +02001172 }
1173
Kevin Winchester53999bf2012-01-15 19:32:55 -04001174 return l;
1175}
1176
Kay Sievers1f717402006-11-24 12:15:25 +01001177static ssize_t show_initstate(struct module_attribute *mattr,
Kay Sievers4befb022011-07-24 22:06:04 +09301178 struct module_kobject *mk, char *buffer)
Kay Sievers1f717402006-11-24 12:15:25 +01001179{
1180 const char *state = "unknown";
1181
Kay Sievers4befb022011-07-24 22:06:04 +09301182 switch (mk->mod->state) {
Kay Sievers1f717402006-11-24 12:15:25 +01001183 case MODULE_STATE_LIVE:
1184 state = "live";
1185 break;
1186 case MODULE_STATE_COMING:
1187 state = "coming";
1188 break;
1189 case MODULE_STATE_GOING:
1190 state = "going";
1191 break;
Rusty Russell0d21b0e2013-01-12 11:38:44 +10301192 default:
1193 BUG();
Kay Sievers1f717402006-11-24 12:15:25 +01001194 }
1195 return sprintf(buffer, "%s\n", state);
1196}
1197
Kay Sieverscca3e702012-01-13 09:32:15 +10301198static struct module_attribute modinfo_initstate =
1199 __ATTR(initstate, 0444, show_initstate, NULL);
Kay Sievers1f717402006-11-24 12:15:25 +01001200
Kay Sievers88bfa322011-07-24 22:06:04 +09301201static ssize_t store_uevent(struct module_attribute *mattr,
1202 struct module_kobject *mk,
1203 const char *buffer, size_t count)
1204{
1205 enum kobject_action action;
1206
1207 if (kobject_action_type(buffer, count, &action) == 0)
1208 kobject_uevent(&mk->kobj, action);
1209 return count;
1210}
1211
Kay Sieverscca3e702012-01-13 09:32:15 +10301212struct module_attribute module_uevent =
1213 __ATTR(uevent, 0200, NULL, store_uevent);
1214
1215static ssize_t show_coresize(struct module_attribute *mattr,
1216 struct module_kobject *mk, char *buffer)
1217{
Rusty Russell7523e4d2015-11-26 09:44:08 +10301218 return sprintf(buffer, "%u\n", mk->mod->core_layout.size);
Kay Sieverscca3e702012-01-13 09:32:15 +10301219}
1220
1221static struct module_attribute modinfo_coresize =
1222 __ATTR(coresize, 0444, show_coresize, NULL);
1223
1224static ssize_t show_initsize(struct module_attribute *mattr,
1225 struct module_kobject *mk, char *buffer)
1226{
Rusty Russell7523e4d2015-11-26 09:44:08 +10301227 return sprintf(buffer, "%u\n", mk->mod->init_layout.size);
Kay Sieverscca3e702012-01-13 09:32:15 +10301228}
1229
1230static struct module_attribute modinfo_initsize =
1231 __ATTR(initsize, 0444, show_initsize, NULL);
1232
1233static ssize_t show_taint(struct module_attribute *mattr,
1234 struct module_kobject *mk, char *buffer)
1235{
1236 size_t l;
1237
1238 l = module_flags_taint(mk->mod, buffer);
1239 buffer[l++] = '\n';
1240 return l;
1241}
1242
1243static struct module_attribute modinfo_taint =
1244 __ATTR(taint, 0444, show_taint, NULL);
Kay Sievers88bfa322011-07-24 22:06:04 +09301245
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001246static struct module_attribute *modinfo_attrs[] = {
Kay Sieverscca3e702012-01-13 09:32:15 +10301247 &module_uevent,
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001248 &modinfo_version,
1249 &modinfo_srcversion,
Kay Sieverscca3e702012-01-13 09:32:15 +10301250 &modinfo_initstate,
1251 &modinfo_coresize,
1252 &modinfo_initsize,
1253 &modinfo_taint,
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001254#ifdef CONFIG_MODULE_UNLOAD
Kay Sieverscca3e702012-01-13 09:32:15 +10301255 &modinfo_refcnt,
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001256#endif
1257 NULL,
1258};
1259
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260static const char vermagic[] = VERMAGIC_STRING;
1261
Rusty Russellc6e665c2009-03-31 13:05:33 -06001262static int try_to_force_load(struct module *mod, const char *reason)
Linus Torvalds826e4502008-05-04 17:04:16 -07001263{
1264#ifdef CONFIG_MODULE_FORCE_LOAD
Andi Kleen25ddbb12008-10-15 22:01:41 -07001265 if (!test_taint(TAINT_FORCED_MODULE))
Andrew Mortonbddb12b2013-11-12 15:11:28 -08001266 pr_warn("%s: %s: kernel tainted.\n", mod->name, reason);
Rusty Russell373d4d02013-01-21 17:17:39 +10301267 add_taint_module(mod, TAINT_FORCED_MODULE, LOCKDEP_NOW_UNRELIABLE);
Linus Torvalds826e4502008-05-04 17:04:16 -07001268 return 0;
1269#else
1270 return -ENOEXEC;
1271#endif
1272}
1273
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274#ifdef CONFIG_MODVERSIONS
Ard Biesheuvel71810db2017-02-03 09:54:06 +00001275
1276static u32 resolve_rel_crc(const s32 *crc)
Rusty Russelld4703ae2009-12-15 16:28:32 -06001277{
Ard Biesheuvel71810db2017-02-03 09:54:06 +00001278 return *(u32 *)((void *)crc + *crc);
Rusty Russelld4703ae2009-12-15 16:28:32 -06001279}
1280
Kees Cook49019422017-04-21 15:35:26 -07001281static int check_version(const struct load_info *info,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 const char *symname,
Ionut Alexa6da0b562014-11-10 09:31:29 +10301283 struct module *mod,
Ard Biesheuvel71810db2017-02-03 09:54:06 +00001284 const s32 *crc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285{
Kees Cook49019422017-04-21 15:35:26 -07001286 Elf_Shdr *sechdrs = info->sechdrs;
1287 unsigned int versindex = info->index.vers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288 unsigned int i, num_versions;
1289 struct modversion_info *versions;
1290
1291 /* Exporting module didn't supply crcs? OK, we're already tainted. */
1292 if (!crc)
1293 return 1;
1294
Rusty Russella5dd6972008-05-09 16:24:21 +10001295 /* No versions at all? modprobe --force does this. */
1296 if (versindex == 0)
1297 return try_to_force_load(mod, symname) == 0;
1298
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299 versions = (void *) sechdrs[versindex].sh_addr;
1300 num_versions = sechdrs[versindex].sh_size
1301 / sizeof(struct modversion_info);
1302
1303 for (i = 0; i < num_versions; i++) {
Ard Biesheuvel71810db2017-02-03 09:54:06 +00001304 u32 crcval;
1305
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306 if (strcmp(versions[i].name, symname) != 0)
1307 continue;
1308
Ard Biesheuvel71810db2017-02-03 09:54:06 +00001309 if (IS_ENABLED(CONFIG_MODULE_REL_CRCS))
1310 crcval = resolve_rel_crc(crc);
1311 else
1312 crcval = *crc;
1313 if (versions[i].crc == crcval)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314 return 1;
Ard Biesheuvel71810db2017-02-03 09:54:06 +00001315 pr_debug("Found checksum %X vs module %lX\n",
1316 crcval, versions[i].crc);
Linus Torvalds826e4502008-05-04 17:04:16 -07001317 goto bad_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318 }
Linus Torvalds826e4502008-05-04 17:04:16 -07001319
Linus Torvaldsfaaae2a2016-11-29 15:20:14 -08001320 /* Broken toolchain. Warn once, then let it go.. */
1321 pr_warn_once("%s: no symbol version for %s\n", mod->name, symname);
1322 return 1;
Linus Torvalds826e4502008-05-04 17:04:16 -07001323
1324bad_version:
Ionut Alexa6da0b562014-11-10 09:31:29 +10301325 pr_warn("%s: disagrees about version of symbol %s\n",
Linus Torvalds826e4502008-05-04 17:04:16 -07001326 mod->name, symname);
1327 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328}
1329
Kees Cook49019422017-04-21 15:35:26 -07001330static inline int check_modstruct_version(const struct load_info *info,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331 struct module *mod)
1332{
Ard Biesheuvel71810db2017-02-03 09:54:06 +00001333 const s32 *crc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334
Peter Zijlstra926a59b2015-05-27 11:09:35 +09301335 /*
1336 * Since this should be found in kernel (which can't be removed), no
1337 * locking is necessary -- use preempt_disable() to placate lockdep.
1338 */
1339 preempt_disable();
Rusty Russellb92021b2013-03-15 15:04:17 +10301340 if (!find_symbol(VMLINUX_SYMBOL_STR(module_layout), NULL,
Peter Zijlstra926a59b2015-05-27 11:09:35 +09301341 &crc, true, false)) {
1342 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343 BUG();
Peter Zijlstra926a59b2015-05-27 11:09:35 +09301344 }
1345 preempt_enable();
Kees Cook49019422017-04-21 15:35:26 -07001346 return check_version(info, VMLINUX_SYMBOL_STR(module_layout),
1347 mod, crc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348}
1349
Rusty Russell91e37a72008-05-09 16:25:28 +10001350/* First part is kernel version, which we ignore if module has crcs. */
1351static inline int same_magic(const char *amagic, const char *bmagic,
1352 bool has_crcs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353{
Rusty Russell91e37a72008-05-09 16:25:28 +10001354 if (has_crcs) {
1355 amagic += strcspn(amagic, " ");
1356 bmagic += strcspn(bmagic, " ");
1357 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358 return strcmp(amagic, bmagic) == 0;
1359}
1360#else
Kees Cook49019422017-04-21 15:35:26 -07001361static inline int check_version(const struct load_info *info,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362 const char *symname,
Ionut Alexa6da0b562014-11-10 09:31:29 +10301363 struct module *mod,
Ard Biesheuvel71810db2017-02-03 09:54:06 +00001364 const s32 *crc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365{
1366 return 1;
1367}
1368
Kees Cook49019422017-04-21 15:35:26 -07001369static inline int check_modstruct_version(const struct load_info *info,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370 struct module *mod)
1371{
1372 return 1;
1373}
1374
Rusty Russell91e37a72008-05-09 16:25:28 +10001375static inline int same_magic(const char *amagic, const char *bmagic,
1376 bool has_crcs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377{
1378 return strcmp(amagic, bmagic) == 0;
1379}
1380#endif /* CONFIG_MODVERSIONS */
1381
Rusty Russell75676502010-06-05 11:17:36 -06001382/* Resolve a symbol for this module. I.e. if we find one, record usage. */
Rusty Russell49668682010-08-05 12:59:10 -06001383static const struct kernel_symbol *resolve_symbol(struct module *mod,
1384 const struct load_info *info,
Tim Abbott414fd312008-12-05 19:03:56 -05001385 const char *name,
Rusty Russell9bea7f22010-06-05 11:17:37 -06001386 char ownername[])
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387{
1388 struct module *owner;
Tim Abbott414fd312008-12-05 19:03:56 -05001389 const struct kernel_symbol *sym;
Ard Biesheuvel71810db2017-02-03 09:54:06 +00001390 const s32 *crc;
Rusty Russell9bea7f22010-06-05 11:17:37 -06001391 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392
Peter Zijlstrad64810f2015-02-11 15:01:13 +10301393 /*
1394 * The module_mutex should not be a heavily contended lock;
1395 * if we get the occasional sleep here, we'll go an extra iteration
1396 * in the wait_event_interruptible(), which is harmless.
1397 */
1398 sched_annotate_sleep();
Rusty Russell75676502010-06-05 11:17:36 -06001399 mutex_lock(&module_mutex);
Tim Abbott414fd312008-12-05 19:03:56 -05001400 sym = find_symbol(name, &owner, &crc,
Andi Kleen25ddbb12008-10-15 22:01:41 -07001401 !(mod->taints & (1 << TAINT_PROPRIETARY_MODULE)), true);
Rusty Russell9bea7f22010-06-05 11:17:37 -06001402 if (!sym)
1403 goto unlock;
1404
Kees Cook49019422017-04-21 15:35:26 -07001405 if (!check_version(info, name, mod, crc)) {
Rusty Russell9bea7f22010-06-05 11:17:37 -06001406 sym = ERR_PTR(-EINVAL);
1407 goto getname;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408 }
Rusty Russell9bea7f22010-06-05 11:17:37 -06001409
1410 err = ref_module(mod, owner);
1411 if (err) {
1412 sym = ERR_PTR(err);
1413 goto getname;
1414 }
1415
1416getname:
1417 /* We must make copy under the lock if we failed to get ref. */
1418 strncpy(ownername, module_name(owner), MODULE_NAME_LEN);
1419unlock:
Rusty Russell75676502010-06-05 11:17:36 -06001420 mutex_unlock(&module_mutex);
Linus Torvalds218ce732010-05-25 16:48:30 -07001421 return sym;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422}
1423
Rusty Russell49668682010-08-05 12:59:10 -06001424static const struct kernel_symbol *
1425resolve_symbol_wait(struct module *mod,
1426 const struct load_info *info,
1427 const char *name)
Rusty Russell9bea7f22010-06-05 11:17:37 -06001428{
1429 const struct kernel_symbol *ksym;
Rusty Russell49668682010-08-05 12:59:10 -06001430 char owner[MODULE_NAME_LEN];
Rusty Russell9bea7f22010-06-05 11:17:37 -06001431
1432 if (wait_event_interruptible_timeout(module_wq,
Rusty Russell49668682010-08-05 12:59:10 -06001433 !IS_ERR(ksym = resolve_symbol(mod, info, name, owner))
1434 || PTR_ERR(ksym) != -EBUSY,
Rusty Russell9bea7f22010-06-05 11:17:37 -06001435 30 * HZ) <= 0) {
Andrew Mortonbddb12b2013-11-12 15:11:28 -08001436 pr_warn("%s: gave up waiting for init of module %s.\n",
1437 mod->name, owner);
Rusty Russell9bea7f22010-06-05 11:17:37 -06001438 }
1439 return ksym;
1440}
1441
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442/*
1443 * /sys/module/foo/sections stuff
1444 * J. Corbet <corbet@lwn.net>
1445 */
Rusty Russell8f6d0372010-08-05 12:59:09 -06001446#ifdef CONFIG_SYSFS
Ben Hutchings10b465a2009-12-19 14:43:01 +00001447
Rusty Russell8f6d0372010-08-05 12:59:09 -06001448#ifdef CONFIG_KALLSYMS
Ben Hutchings10b465a2009-12-19 14:43:01 +00001449static inline bool sect_empty(const Elf_Shdr *sect)
1450{
1451 return !(sect->sh_flags & SHF_ALLOC) || sect->sh_size == 0;
1452}
1453
Ionut Alexa6da0b562014-11-10 09:31:29 +10301454struct module_sect_attr {
Rusty Russella58730c2008-03-13 09:03:44 +00001455 struct module_attribute mattr;
1456 char *name;
1457 unsigned long address;
1458};
1459
Ionut Alexa6da0b562014-11-10 09:31:29 +10301460struct module_sect_attrs {
Rusty Russella58730c2008-03-13 09:03:44 +00001461 struct attribute_group grp;
1462 unsigned int nsections;
1463 struct module_sect_attr attrs[0];
1464};
1465
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466static ssize_t module_sect_show(struct module_attribute *mattr,
Kay Sievers4befb022011-07-24 22:06:04 +09301467 struct module_kobject *mk, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468{
1469 struct module_sect_attr *sattr =
1470 container_of(mattr, struct module_sect_attr, mattr);
Kees Cook9f36e2c2011-03-22 16:34:22 -07001471 return sprintf(buf, "0x%pK\n", (void *)sattr->address);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472}
1473
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001474static void free_sect_attrs(struct module_sect_attrs *sect_attrs)
1475{
Rusty Russella58730c2008-03-13 09:03:44 +00001476 unsigned int section;
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001477
1478 for (section = 0; section < sect_attrs->nsections; section++)
1479 kfree(sect_attrs->attrs[section].name);
1480 kfree(sect_attrs);
1481}
1482
Rusty Russell8f6d0372010-08-05 12:59:09 -06001483static void add_sect_attrs(struct module *mod, const struct load_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484{
1485 unsigned int nloaded = 0, i, size[2];
1486 struct module_sect_attrs *sect_attrs;
1487 struct module_sect_attr *sattr;
1488 struct attribute **gattr;
Daniel Walker22a8bde2007-10-18 03:06:07 -07001489
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490 /* Count loaded sections and allocate structures */
Rusty Russell8f6d0372010-08-05 12:59:09 -06001491 for (i = 0; i < info->hdr->e_shnum; i++)
1492 if (!sect_empty(&info->sechdrs[i]))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493 nloaded++;
1494 size[0] = ALIGN(sizeof(*sect_attrs)
1495 + nloaded * sizeof(sect_attrs->attrs[0]),
1496 sizeof(sect_attrs->grp.attrs[0]));
1497 size[1] = (nloaded + 1) * sizeof(sect_attrs->grp.attrs[0]);
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001498 sect_attrs = kzalloc(size[0] + size[1], GFP_KERNEL);
1499 if (sect_attrs == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500 return;
1501
1502 /* Setup section attributes. */
1503 sect_attrs->grp.name = "sections";
1504 sect_attrs->grp.attrs = (void *)sect_attrs + size[0];
1505
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001506 sect_attrs->nsections = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507 sattr = &sect_attrs->attrs[0];
1508 gattr = &sect_attrs->grp.attrs[0];
Rusty Russell8f6d0372010-08-05 12:59:09 -06001509 for (i = 0; i < info->hdr->e_shnum; i++) {
1510 Elf_Shdr *sec = &info->sechdrs[i];
1511 if (sect_empty(sec))
Helge Deller35dead42009-12-03 00:29:15 +01001512 continue;
Rusty Russell8f6d0372010-08-05 12:59:09 -06001513 sattr->address = sec->sh_addr;
1514 sattr->name = kstrdup(info->secstrings + sec->sh_name,
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001515 GFP_KERNEL);
1516 if (sattr->name == NULL)
1517 goto out;
1518 sect_attrs->nsections++;
Eric W. Biederman361795b2010-02-12 13:41:56 -08001519 sysfs_attr_init(&sattr->mattr.attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520 sattr->mattr.show = module_sect_show;
1521 sattr->mattr.store = NULL;
1522 sattr->mattr.attr.name = sattr->name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523 sattr->mattr.attr.mode = S_IRUGO;
1524 *(gattr++) = &(sattr++)->mattr.attr;
1525 }
1526 *gattr = NULL;
1527
1528 if (sysfs_create_group(&mod->mkobj.kobj, &sect_attrs->grp))
1529 goto out;
1530
1531 mod->sect_attrs = sect_attrs;
1532 return;
1533 out:
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001534 free_sect_attrs(sect_attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535}
1536
1537static void remove_sect_attrs(struct module *mod)
1538{
1539 if (mod->sect_attrs) {
1540 sysfs_remove_group(&mod->mkobj.kobj,
1541 &mod->sect_attrs->grp);
1542 /* We are positive that no one is using any sect attrs
1543 * at this point. Deallocate immediately. */
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001544 free_sect_attrs(mod->sect_attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545 mod->sect_attrs = NULL;
1546 }
1547}
1548
Roland McGrath6d760132007-10-16 23:26:40 -07001549/*
1550 * /sys/module/foo/notes/.section.name gives contents of SHT_NOTE sections.
1551 */
1552
1553struct module_notes_attrs {
1554 struct kobject *dir;
1555 unsigned int notes;
1556 struct bin_attribute attrs[0];
1557};
1558
Chris Wright2c3c8be2010-05-12 18:28:57 -07001559static ssize_t module_notes_read(struct file *filp, struct kobject *kobj,
Roland McGrath6d760132007-10-16 23:26:40 -07001560 struct bin_attribute *bin_attr,
1561 char *buf, loff_t pos, size_t count)
1562{
1563 /*
1564 * The caller checked the pos and count against our size.
1565 */
1566 memcpy(buf, bin_attr->private + pos, count);
1567 return count;
1568}
1569
1570static void free_notes_attrs(struct module_notes_attrs *notes_attrs,
1571 unsigned int i)
1572{
1573 if (notes_attrs->dir) {
1574 while (i-- > 0)
1575 sysfs_remove_bin_file(notes_attrs->dir,
1576 &notes_attrs->attrs[i]);
Alexey Dobriyane9432092008-09-23 23:51:11 +04001577 kobject_put(notes_attrs->dir);
Roland McGrath6d760132007-10-16 23:26:40 -07001578 }
1579 kfree(notes_attrs);
1580}
1581
Rusty Russell8f6d0372010-08-05 12:59:09 -06001582static void add_notes_attrs(struct module *mod, const struct load_info *info)
Roland McGrath6d760132007-10-16 23:26:40 -07001583{
1584 unsigned int notes, loaded, i;
1585 struct module_notes_attrs *notes_attrs;
1586 struct bin_attribute *nattr;
1587
Ingo Molnarea6bff32009-08-28 10:44:56 +02001588 /* failed to create section attributes, so can't create notes */
1589 if (!mod->sect_attrs)
1590 return;
1591
Roland McGrath6d760132007-10-16 23:26:40 -07001592 /* Count notes sections and allocate structures. */
1593 notes = 0;
Rusty Russell8f6d0372010-08-05 12:59:09 -06001594 for (i = 0; i < info->hdr->e_shnum; i++)
1595 if (!sect_empty(&info->sechdrs[i]) &&
1596 (info->sechdrs[i].sh_type == SHT_NOTE))
Roland McGrath6d760132007-10-16 23:26:40 -07001597 ++notes;
1598
1599 if (notes == 0)
1600 return;
1601
1602 notes_attrs = kzalloc(sizeof(*notes_attrs)
1603 + notes * sizeof(notes_attrs->attrs[0]),
1604 GFP_KERNEL);
1605 if (notes_attrs == NULL)
1606 return;
1607
1608 notes_attrs->notes = notes;
1609 nattr = &notes_attrs->attrs[0];
Rusty Russell8f6d0372010-08-05 12:59:09 -06001610 for (loaded = i = 0; i < info->hdr->e_shnum; ++i) {
1611 if (sect_empty(&info->sechdrs[i]))
Roland McGrath6d760132007-10-16 23:26:40 -07001612 continue;
Rusty Russell8f6d0372010-08-05 12:59:09 -06001613 if (info->sechdrs[i].sh_type == SHT_NOTE) {
Eric W. Biederman361795b2010-02-12 13:41:56 -08001614 sysfs_bin_attr_init(nattr);
Roland McGrath6d760132007-10-16 23:26:40 -07001615 nattr->attr.name = mod->sect_attrs->attrs[loaded].name;
1616 nattr->attr.mode = S_IRUGO;
Rusty Russell8f6d0372010-08-05 12:59:09 -06001617 nattr->size = info->sechdrs[i].sh_size;
1618 nattr->private = (void *) info->sechdrs[i].sh_addr;
Roland McGrath6d760132007-10-16 23:26:40 -07001619 nattr->read = module_notes_read;
1620 ++nattr;
1621 }
1622 ++loaded;
1623 }
1624
Greg Kroah-Hartman4ff6abf2007-11-05 22:24:43 -08001625 notes_attrs->dir = kobject_create_and_add("notes", &mod->mkobj.kobj);
Roland McGrath6d760132007-10-16 23:26:40 -07001626 if (!notes_attrs->dir)
1627 goto out;
1628
1629 for (i = 0; i < notes; ++i)
1630 if (sysfs_create_bin_file(notes_attrs->dir,
1631 &notes_attrs->attrs[i]))
1632 goto out;
1633
1634 mod->notes_attrs = notes_attrs;
1635 return;
1636
1637 out:
1638 free_notes_attrs(notes_attrs, i);
1639}
1640
1641static void remove_notes_attrs(struct module *mod)
1642{
1643 if (mod->notes_attrs)
1644 free_notes_attrs(mod->notes_attrs, mod->notes_attrs->notes);
1645}
1646
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647#else
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001648
Rusty Russell8f6d0372010-08-05 12:59:09 -06001649static inline void add_sect_attrs(struct module *mod,
1650 const struct load_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651{
1652}
1653
1654static inline void remove_sect_attrs(struct module *mod)
1655{
1656}
Roland McGrath6d760132007-10-16 23:26:40 -07001657
Rusty Russell8f6d0372010-08-05 12:59:09 -06001658static inline void add_notes_attrs(struct module *mod,
1659 const struct load_info *info)
Roland McGrath6d760132007-10-16 23:26:40 -07001660{
1661}
1662
1663static inline void remove_notes_attrs(struct module *mod)
1664{
1665}
Rusty Russell8f6d0372010-08-05 12:59:09 -06001666#endif /* CONFIG_KALLSYMS */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667
Rusty Russell80a3d1b2010-06-05 11:17:36 -06001668static void add_usage_links(struct module *mod)
1669{
1670#ifdef CONFIG_MODULE_UNLOAD
1671 struct module_use *use;
1672 int nowarn;
1673
Rusty Russell75676502010-06-05 11:17:36 -06001674 mutex_lock(&module_mutex);
Rusty Russell80a3d1b2010-06-05 11:17:36 -06001675 list_for_each_entry(use, &mod->target_list, target_list) {
1676 nowarn = sysfs_create_link(use->target->holders_dir,
1677 &mod->mkobj.kobj, mod->name);
1678 }
Rusty Russell75676502010-06-05 11:17:36 -06001679 mutex_unlock(&module_mutex);
Rusty Russell80a3d1b2010-06-05 11:17:36 -06001680#endif
1681}
1682
1683static void del_usage_links(struct module *mod)
1684{
1685#ifdef CONFIG_MODULE_UNLOAD
1686 struct module_use *use;
1687
Rusty Russell75676502010-06-05 11:17:36 -06001688 mutex_lock(&module_mutex);
Rusty Russell80a3d1b2010-06-05 11:17:36 -06001689 list_for_each_entry(use, &mod->target_list, target_list)
1690 sysfs_remove_link(use->target->holders_dir, mod->name);
Rusty Russell75676502010-06-05 11:17:36 -06001691 mutex_unlock(&module_mutex);
Rusty Russell80a3d1b2010-06-05 11:17:36 -06001692#endif
1693}
1694
Rusty Russell6407ebb22010-06-05 11:17:36 -06001695static int module_add_modinfo_attrs(struct module *mod)
Matt Domschc988d2b2005-06-23 22:05:15 -07001696{
1697 struct module_attribute *attr;
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001698 struct module_attribute *temp_attr;
Matt Domschc988d2b2005-06-23 22:05:15 -07001699 int error = 0;
1700 int i;
1701
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001702 mod->modinfo_attrs = kzalloc((sizeof(struct module_attribute) *
1703 (ARRAY_SIZE(modinfo_attrs) + 1)),
1704 GFP_KERNEL);
1705 if (!mod->modinfo_attrs)
1706 return -ENOMEM;
1707
1708 temp_attr = mod->modinfo_attrs;
Matt Domschc988d2b2005-06-23 22:05:15 -07001709 for (i = 0; (attr = modinfo_attrs[i]) && !error; i++) {
Rusty Russellc75b5902016-04-12 05:03:09 +09301710 if (!attr->test || attr->test(mod)) {
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001711 memcpy(temp_attr, attr, sizeof(*temp_attr));
Eric W. Biederman361795b2010-02-12 13:41:56 -08001712 sysfs_attr_init(&temp_attr->attr);
Ionut Alexa6da0b562014-11-10 09:31:29 +10301713 error = sysfs_create_file(&mod->mkobj.kobj,
1714 &temp_attr->attr);
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001715 ++temp_attr;
1716 }
Matt Domschc988d2b2005-06-23 22:05:15 -07001717 }
1718 return error;
1719}
1720
Rusty Russell6407ebb22010-06-05 11:17:36 -06001721static void module_remove_modinfo_attrs(struct module *mod)
Matt Domschc988d2b2005-06-23 22:05:15 -07001722{
1723 struct module_attribute *attr;
1724 int i;
1725
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001726 for (i = 0; (attr = &mod->modinfo_attrs[i]); i++) {
1727 /* pick a field to test for end of list */
1728 if (!attr->attr.name)
1729 break;
Ionut Alexa6da0b562014-11-10 09:31:29 +10301730 sysfs_remove_file(&mod->mkobj.kobj, &attr->attr);
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001731 if (attr->free)
1732 attr->free(mod);
Matt Domschc988d2b2005-06-23 22:05:15 -07001733 }
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001734 kfree(mod->modinfo_attrs);
Matt Domschc988d2b2005-06-23 22:05:15 -07001735}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736
Li Zhong942e4432013-09-03 16:33:57 +09301737static void mod_kobject_put(struct module *mod)
1738{
1739 DECLARE_COMPLETION_ONSTACK(c);
1740 mod->mkobj.kobj_completion = &c;
1741 kobject_put(&mod->mkobj.kobj);
1742 wait_for_completion(&c);
1743}
1744
Rusty Russell6407ebb22010-06-05 11:17:36 -06001745static int mod_sysfs_init(struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746{
1747 int err;
Greg Kroah-Hartman6494a932008-01-27 15:38:40 -08001748 struct kobject *kobj;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749
Greg Kroah-Hartman823bccf2007-04-13 13:15:19 -07001750 if (!module_sysfs_initialized) {
Andrew Mortonbddb12b2013-11-12 15:11:28 -08001751 pr_err("%s: module sysfs not initialized\n", mod->name);
Ed Swierk1cc5f712006-09-25 16:25:36 -07001752 err = -EINVAL;
1753 goto out;
1754 }
Greg Kroah-Hartman6494a932008-01-27 15:38:40 -08001755
1756 kobj = kset_find_obj(module_kset, mod->name);
1757 if (kobj) {
Andrew Mortonbddb12b2013-11-12 15:11:28 -08001758 pr_err("%s: module is already loaded\n", mod->name);
Greg Kroah-Hartman6494a932008-01-27 15:38:40 -08001759 kobject_put(kobj);
1760 err = -EINVAL;
1761 goto out;
1762 }
1763
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764 mod->mkobj.mod = mod;
Kay Sieverse17e0f52006-11-24 12:15:25 +01001765
Greg Kroah-Hartmanac3c8142007-12-17 23:05:35 -07001766 memset(&mod->mkobj.kobj, 0, sizeof(mod->mkobj.kobj));
1767 mod->mkobj.kobj.kset = module_kset;
1768 err = kobject_init_and_add(&mod->mkobj.kobj, &module_ktype, NULL,
1769 "%s", mod->name);
1770 if (err)
Li Zhong942e4432013-09-03 16:33:57 +09301771 mod_kobject_put(mod);
Kay Sievers270a6c42007-01-18 13:26:15 +01001772
Kay Sievers97c146ef2007-11-29 23:46:11 +01001773 /* delay uevent until full sysfs population */
Kay Sievers270a6c42007-01-18 13:26:15 +01001774out:
1775 return err;
1776}
1777
Rusty Russell6407ebb22010-06-05 11:17:36 -06001778static int mod_sysfs_setup(struct module *mod,
Rusty Russell8f6d0372010-08-05 12:59:09 -06001779 const struct load_info *info,
Kay Sievers270a6c42007-01-18 13:26:15 +01001780 struct kernel_param *kparam,
1781 unsigned int num_params)
1782{
1783 int err;
1784
Rusty Russell80a3d1b2010-06-05 11:17:36 -06001785 err = mod_sysfs_init(mod);
1786 if (err)
1787 goto out;
1788
Greg Kroah-Hartman4ff6abf2007-11-05 22:24:43 -08001789 mod->holders_dir = kobject_create_and_add("holders", &mod->mkobj.kobj);
Akinobu Mita240936e2007-04-26 00:12:09 -07001790 if (!mod->holders_dir) {
1791 err = -ENOMEM;
Kay Sievers270a6c42007-01-18 13:26:15 +01001792 goto out_unreg;
Akinobu Mita240936e2007-04-26 00:12:09 -07001793 }
Kay Sievers270a6c42007-01-18 13:26:15 +01001794
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795 err = module_param_sysfs_setup(mod, kparam, num_params);
1796 if (err)
Kay Sievers270a6c42007-01-18 13:26:15 +01001797 goto out_unreg_holders;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798
Matt Domschc988d2b2005-06-23 22:05:15 -07001799 err = module_add_modinfo_attrs(mod);
1800 if (err)
Kay Sieverse17e0f52006-11-24 12:15:25 +01001801 goto out_unreg_param;
Matt Domschc988d2b2005-06-23 22:05:15 -07001802
Rusty Russell80a3d1b2010-06-05 11:17:36 -06001803 add_usage_links(mod);
Rusty Russell8f6d0372010-08-05 12:59:09 -06001804 add_sect_attrs(mod, info);
1805 add_notes_attrs(mod, info);
Rusty Russell80a3d1b2010-06-05 11:17:36 -06001806
Kay Sieverse17e0f52006-11-24 12:15:25 +01001807 kobject_uevent(&mod->mkobj.kobj, KOBJ_ADD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808 return 0;
1809
Kay Sieverse17e0f52006-11-24 12:15:25 +01001810out_unreg_param:
1811 module_param_sysfs_remove(mod);
Kay Sievers270a6c42007-01-18 13:26:15 +01001812out_unreg_holders:
Greg Kroah-Hartman78a2d902007-12-20 08:13:05 -08001813 kobject_put(mod->holders_dir);
Kay Sievers270a6c42007-01-18 13:26:15 +01001814out_unreg:
Li Zhong942e4432013-09-03 16:33:57 +09301815 mod_kobject_put(mod);
Rusty Russell80a3d1b2010-06-05 11:17:36 -06001816out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817 return err;
1818}
Denis V. Lunev34e4e2f2008-05-20 13:59:48 +04001819
1820static void mod_sysfs_fini(struct module *mod)
1821{
Rusty Russell8f6d0372010-08-05 12:59:09 -06001822 remove_notes_attrs(mod);
1823 remove_sect_attrs(mod);
Li Zhong942e4432013-09-03 16:33:57 +09301824 mod_kobject_put(mod);
Denis V. Lunev34e4e2f2008-05-20 13:59:48 +04001825}
1826
Rusty Russellcf2fde72015-06-26 06:44:38 +09301827static void init_param_lock(struct module *mod)
1828{
1829 mutex_init(&mod->param_lock);
1830}
Rusty Russell8f6d0372010-08-05 12:59:09 -06001831#else /* !CONFIG_SYSFS */
Denis V. Lunev34e4e2f2008-05-20 13:59:48 +04001832
Rusty Russell8f6d0372010-08-05 12:59:09 -06001833static int mod_sysfs_setup(struct module *mod,
1834 const struct load_info *info,
Rusty Russell6407ebb22010-06-05 11:17:36 -06001835 struct kernel_param *kparam,
1836 unsigned int num_params)
1837{
1838 return 0;
1839}
1840
Denis V. Lunev34e4e2f2008-05-20 13:59:48 +04001841static void mod_sysfs_fini(struct module *mod)
1842{
1843}
1844
Rusty Russell36b03602010-08-05 12:59:09 -06001845static void module_remove_modinfo_attrs(struct module *mod)
1846{
1847}
1848
Rusty Russell80a3d1b2010-06-05 11:17:36 -06001849static void del_usage_links(struct module *mod)
1850{
1851}
1852
Rusty Russellcf2fde72015-06-26 06:44:38 +09301853static void init_param_lock(struct module *mod)
1854{
1855}
Denis V. Lunev34e4e2f2008-05-20 13:59:48 +04001856#endif /* CONFIG_SYSFS */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857
Rusty Russell36b03602010-08-05 12:59:09 -06001858static void mod_sysfs_teardown(struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859{
Rusty Russell80a3d1b2010-06-05 11:17:36 -06001860 del_usage_links(mod);
Matt Domschc988d2b2005-06-23 22:05:15 -07001861 module_remove_modinfo_attrs(mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862 module_param_sysfs_remove(mod);
Greg Kroah-Hartman78a2d902007-12-20 08:13:05 -08001863 kobject_put(mod->mkobj.drivers_dir);
1864 kobject_put(mod->holders_dir);
Denis V. Lunev34e4e2f2008-05-20 13:59:48 +04001865 mod_sysfs_fini(mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866}
1867
Laura Abbott0f5bf6d2017-02-06 16:31:58 -08001868#ifdef CONFIG_STRICT_MODULE_RWX
matthieu castet84e1c6b2010-11-16 22:35:16 +01001869/*
1870 * LKM RO/NX protection: protect module's text/ro-data
1871 * from modification and any data from execution.
Rusty Russell85c898d2015-11-26 09:45:08 +10301872 *
1873 * General layout of module is:
Jessica Yu444d13f2016-07-27 12:06:21 +09301874 * [text] [read-only-data] [ro-after-init] [writable data]
1875 * text_size -----^ ^ ^ ^
1876 * ro_size ------------------------| | |
1877 * ro_after_init_size -----------------------------| |
1878 * size -----------------------------------------------------------|
Rusty Russell85c898d2015-11-26 09:45:08 +10301879 *
1880 * These values are always page-aligned (as is base)
matthieu castet84e1c6b2010-11-16 22:35:16 +01001881 */
Rusty Russell85c898d2015-11-26 09:45:08 +10301882static void frob_text(const struct module_layout *layout,
1883 int (*set_memory)(unsigned long start, int num_pages))
matthieu castet84e1c6b2010-11-16 22:35:16 +01001884{
Rusty Russell85c898d2015-11-26 09:45:08 +10301885 BUG_ON((unsigned long)layout->base & (PAGE_SIZE-1));
1886 BUG_ON((unsigned long)layout->text_size & (PAGE_SIZE-1));
1887 set_memory((unsigned long)layout->base,
1888 layout->text_size >> PAGE_SHIFT);
matthieu castet84e1c6b2010-11-16 22:35:16 +01001889}
1890
Rusty Russell85c898d2015-11-26 09:45:08 +10301891static void frob_rodata(const struct module_layout *layout,
1892 int (*set_memory)(unsigned long start, int num_pages))
matthieu castet84e1c6b2010-11-16 22:35:16 +01001893{
Rusty Russell85c898d2015-11-26 09:45:08 +10301894 BUG_ON((unsigned long)layout->base & (PAGE_SIZE-1));
1895 BUG_ON((unsigned long)layout->text_size & (PAGE_SIZE-1));
1896 BUG_ON((unsigned long)layout->ro_size & (PAGE_SIZE-1));
1897 set_memory((unsigned long)layout->base + layout->text_size,
1898 (layout->ro_size - layout->text_size) >> PAGE_SHIFT);
matthieu castet84e1c6b2010-11-16 22:35:16 +01001899}
1900
Jessica Yu444d13f2016-07-27 12:06:21 +09301901static void frob_ro_after_init(const struct module_layout *layout,
1902 int (*set_memory)(unsigned long start, int num_pages))
1903{
1904 BUG_ON((unsigned long)layout->base & (PAGE_SIZE-1));
1905 BUG_ON((unsigned long)layout->ro_size & (PAGE_SIZE-1));
1906 BUG_ON((unsigned long)layout->ro_after_init_size & (PAGE_SIZE-1));
1907 set_memory((unsigned long)layout->base + layout->ro_size,
1908 (layout->ro_after_init_size - layout->ro_size) >> PAGE_SHIFT);
1909}
1910
Rusty Russell85c898d2015-11-26 09:45:08 +10301911static void frob_writable_data(const struct module_layout *layout,
1912 int (*set_memory)(unsigned long start, int num_pages))
matthieu castet84e1c6b2010-11-16 22:35:16 +01001913{
Rusty Russell85c898d2015-11-26 09:45:08 +10301914 BUG_ON((unsigned long)layout->base & (PAGE_SIZE-1));
Jessica Yu444d13f2016-07-27 12:06:21 +09301915 BUG_ON((unsigned long)layout->ro_after_init_size & (PAGE_SIZE-1));
Rusty Russell85c898d2015-11-26 09:45:08 +10301916 BUG_ON((unsigned long)layout->size & (PAGE_SIZE-1));
Jessica Yu444d13f2016-07-27 12:06:21 +09301917 set_memory((unsigned long)layout->base + layout->ro_after_init_size,
1918 (layout->size - layout->ro_after_init_size) >> PAGE_SHIFT);
Jan Glauber01526ed2011-05-19 16:55:26 -06001919}
1920
Rusty Russell85c898d2015-11-26 09:45:08 +10301921/* livepatching wants to disable read-only so it can frob module. */
1922void module_disable_ro(const struct module *mod)
Jan Glauber01526ed2011-05-19 16:55:26 -06001923{
AKASHI Takahiro39290b32016-11-14 15:15:05 +09001924 if (!rodata_enabled)
1925 return;
1926
Rusty Russell85c898d2015-11-26 09:45:08 +10301927 frob_text(&mod->core_layout, set_memory_rw);
1928 frob_rodata(&mod->core_layout, set_memory_rw);
Jessica Yu444d13f2016-07-27 12:06:21 +09301929 frob_ro_after_init(&mod->core_layout, set_memory_rw);
Rusty Russell85c898d2015-11-26 09:45:08 +10301930 frob_text(&mod->init_layout, set_memory_rw);
1931 frob_rodata(&mod->init_layout, set_memory_rw);
Josh Poimboeuf20ef10c2015-11-26 09:42:08 +10301932}
1933
Jessica Yu444d13f2016-07-27 12:06:21 +09301934void module_enable_ro(const struct module *mod, bool after_init)
Josh Poimboeuf20ef10c2015-11-26 09:42:08 +10301935{
AKASHI Takahiro39290b32016-11-14 15:15:05 +09001936 if (!rodata_enabled)
1937 return;
1938
Rusty Russell85c898d2015-11-26 09:45:08 +10301939 frob_text(&mod->core_layout, set_memory_ro);
1940 frob_rodata(&mod->core_layout, set_memory_ro);
1941 frob_text(&mod->init_layout, set_memory_ro);
1942 frob_rodata(&mod->init_layout, set_memory_ro);
Jessica Yu444d13f2016-07-27 12:06:21 +09301943
1944 if (after_init)
1945 frob_ro_after_init(&mod->core_layout, set_memory_ro);
Jan Glauber01526ed2011-05-19 16:55:26 -06001946}
1947
Rusty Russell85c898d2015-11-26 09:45:08 +10301948static void module_enable_nx(const struct module *mod)
Jan Glauber01526ed2011-05-19 16:55:26 -06001949{
Rusty Russell85c898d2015-11-26 09:45:08 +10301950 frob_rodata(&mod->core_layout, set_memory_nx);
Jessica Yu444d13f2016-07-27 12:06:21 +09301951 frob_ro_after_init(&mod->core_layout, set_memory_nx);
Rusty Russell85c898d2015-11-26 09:45:08 +10301952 frob_writable_data(&mod->core_layout, set_memory_nx);
1953 frob_rodata(&mod->init_layout, set_memory_nx);
1954 frob_writable_data(&mod->init_layout, set_memory_nx);
1955}
1956
1957static void module_disable_nx(const struct module *mod)
1958{
1959 frob_rodata(&mod->core_layout, set_memory_x);
Jessica Yu444d13f2016-07-27 12:06:21 +09301960 frob_ro_after_init(&mod->core_layout, set_memory_x);
Rusty Russell85c898d2015-11-26 09:45:08 +10301961 frob_writable_data(&mod->core_layout, set_memory_x);
1962 frob_rodata(&mod->init_layout, set_memory_x);
1963 frob_writable_data(&mod->init_layout, set_memory_x);
matthieu castet84e1c6b2010-11-16 22:35:16 +01001964}
1965
1966/* Iterate through all modules and set each module's text as RW */
Daniel J Blueman5d05c702011-03-08 22:01:47 +08001967void set_all_modules_text_rw(void)
matthieu castet84e1c6b2010-11-16 22:35:16 +01001968{
1969 struct module *mod;
1970
AKASHI Takahiro39290b32016-11-14 15:15:05 +09001971 if (!rodata_enabled)
1972 return;
1973
matthieu castet84e1c6b2010-11-16 22:35:16 +01001974 mutex_lock(&module_mutex);
1975 list_for_each_entry_rcu(mod, &modules, list) {
Rusty Russell0d21b0e2013-01-12 11:38:44 +10301976 if (mod->state == MODULE_STATE_UNFORMED)
1977 continue;
Rusty Russell85c898d2015-11-26 09:45:08 +10301978
1979 frob_text(&mod->core_layout, set_memory_rw);
1980 frob_text(&mod->init_layout, set_memory_rw);
matthieu castet84e1c6b2010-11-16 22:35:16 +01001981 }
1982 mutex_unlock(&module_mutex);
1983}
1984
1985/* Iterate through all modules and set each module's text as RO */
Daniel J Blueman5d05c702011-03-08 22:01:47 +08001986void set_all_modules_text_ro(void)
matthieu castet84e1c6b2010-11-16 22:35:16 +01001987{
1988 struct module *mod;
1989
AKASHI Takahiro39290b32016-11-14 15:15:05 +09001990 if (!rodata_enabled)
1991 return;
1992
matthieu castet84e1c6b2010-11-16 22:35:16 +01001993 mutex_lock(&module_mutex);
1994 list_for_each_entry_rcu(mod, &modules, list) {
Aaron Tomlin905dd707f2016-10-27 10:36:06 +01001995 /*
1996 * Ignore going modules since it's possible that ro
1997 * protection has already been disabled, otherwise we'll
1998 * run into protection faults at module deallocation.
1999 */
2000 if (mod->state == MODULE_STATE_UNFORMED ||
2001 mod->state == MODULE_STATE_GOING)
Rusty Russell0d21b0e2013-01-12 11:38:44 +10302002 continue;
Rusty Russell85c898d2015-11-26 09:45:08 +10302003
2004 frob_text(&mod->core_layout, set_memory_ro);
2005 frob_text(&mod->init_layout, set_memory_ro);
matthieu castet84e1c6b2010-11-16 22:35:16 +01002006 }
2007 mutex_unlock(&module_mutex);
2008}
Rusty Russell85c898d2015-11-26 09:45:08 +10302009
2010static void disable_ro_nx(const struct module_layout *layout)
2011{
AKASHI Takahiro39290b32016-11-14 15:15:05 +09002012 if (rodata_enabled) {
2013 frob_text(layout, set_memory_rw);
2014 frob_rodata(layout, set_memory_rw);
2015 frob_ro_after_init(layout, set_memory_rw);
2016 }
Rusty Russell85c898d2015-11-26 09:45:08 +10302017 frob_rodata(layout, set_memory_x);
Jessica Yu444d13f2016-07-27 12:06:21 +09302018 frob_ro_after_init(layout, set_memory_x);
Rusty Russell85c898d2015-11-26 09:45:08 +10302019 frob_writable_data(layout, set_memory_x);
2020}
2021
matthieu castet84e1c6b2010-11-16 22:35:16 +01002022#else
Rusty Russell85c898d2015-11-26 09:45:08 +10302023static void disable_ro_nx(const struct module_layout *layout) { }
2024static void module_enable_nx(const struct module *mod) { }
2025static void module_disable_nx(const struct module *mod) { }
matthieu castet84e1c6b2010-11-16 22:35:16 +01002026#endif
2027
Jessica Yu1ce15ef2016-03-22 20:03:16 -04002028#ifdef CONFIG_LIVEPATCH
2029/*
2030 * Persist Elf information about a module. Copy the Elf header,
2031 * section header table, section string table, and symtab section
2032 * index from info to mod->klp_info.
2033 */
2034static int copy_module_elf(struct module *mod, struct load_info *info)
2035{
2036 unsigned int size, symndx;
2037 int ret;
2038
2039 size = sizeof(*mod->klp_info);
2040 mod->klp_info = kmalloc(size, GFP_KERNEL);
2041 if (mod->klp_info == NULL)
2042 return -ENOMEM;
2043
2044 /* Elf header */
2045 size = sizeof(mod->klp_info->hdr);
2046 memcpy(&mod->klp_info->hdr, info->hdr, size);
2047
2048 /* Elf section header table */
2049 size = sizeof(*info->sechdrs) * info->hdr->e_shnum;
2050 mod->klp_info->sechdrs = kmalloc(size, GFP_KERNEL);
2051 if (mod->klp_info->sechdrs == NULL) {
2052 ret = -ENOMEM;
2053 goto free_info;
2054 }
2055 memcpy(mod->klp_info->sechdrs, info->sechdrs, size);
2056
2057 /* Elf section name string table */
2058 size = info->sechdrs[info->hdr->e_shstrndx].sh_size;
2059 mod->klp_info->secstrings = kmalloc(size, GFP_KERNEL);
2060 if (mod->klp_info->secstrings == NULL) {
2061 ret = -ENOMEM;
2062 goto free_sechdrs;
2063 }
2064 memcpy(mod->klp_info->secstrings, info->secstrings, size);
2065
2066 /* Elf symbol section index */
2067 symndx = info->index.sym;
2068 mod->klp_info->symndx = symndx;
2069
2070 /*
2071 * For livepatch modules, core_kallsyms.symtab is a complete
2072 * copy of the original symbol table. Adjust sh_addr to point
2073 * to core_kallsyms.symtab since the copy of the symtab in module
2074 * init memory is freed at the end of do_init_module().
2075 */
2076 mod->klp_info->sechdrs[symndx].sh_addr = \
2077 (unsigned long) mod->core_kallsyms.symtab;
2078
2079 return 0;
2080
2081free_sechdrs:
2082 kfree(mod->klp_info->sechdrs);
2083free_info:
2084 kfree(mod->klp_info);
2085 return ret;
2086}
2087
2088static void free_module_elf(struct module *mod)
2089{
2090 kfree(mod->klp_info->sechdrs);
2091 kfree(mod->klp_info->secstrings);
2092 kfree(mod->klp_info);
2093}
2094#else /* !CONFIG_LIVEPATCH */
2095static int copy_module_elf(struct module *mod, struct load_info *info)
2096{
2097 return 0;
2098}
2099
2100static void free_module_elf(struct module *mod)
2101{
2102}
2103#endif /* CONFIG_LIVEPATCH */
2104
Rusty Russellbe1f2212015-01-20 09:07:05 +10302105void __weak module_memfree(void *module_region)
Jonas Bonn74e08fc2011-06-30 21:22:11 +02002106{
2107 vfree(module_region);
2108}
2109
2110void __weak module_arch_cleanup(struct module *mod)
2111{
2112}
2113
Rusty Russelld453cde2015-01-20 09:07:04 +10302114void __weak module_arch_freeing_init(struct module *mod)
2115{
2116}
2117
Rusty Russell75676502010-06-05 11:17:36 -06002118/* Free a module, remove from lists, etc. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119static void free_module(struct module *mod)
2120{
Li Zefan7ead8b82009-08-17 16:56:28 +08002121 trace_module_free(mod);
2122
Rusty Russell36b03602010-08-05 12:59:09 -06002123 mod_sysfs_teardown(mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124
Rusty Russell944a1fa2013-04-17 13:20:03 +09302125 /* We leave it in list to prevent duplicate loads, but make sure
2126 * that noone uses it while it's being deconstructed. */
Prarit Bhargavad3051b42014-10-14 02:51:39 +10302127 mutex_lock(&module_mutex);
Rusty Russell944a1fa2013-04-17 13:20:03 +09302128 mod->state = MODULE_STATE_UNFORMED;
Prarit Bhargavad3051b42014-10-14 02:51:39 +10302129 mutex_unlock(&module_mutex);
Rusty Russell944a1fa2013-04-17 13:20:03 +09302130
Jason Baronb82bab4b2010-07-27 13:18:01 -07002131 /* Remove dynamic debug info */
2132 ddebug_remove_module(mod->name);
2133
Linus Torvalds1da177e2005-04-16 15:20:36 -07002134 /* Arch-specific cleanup. */
2135 module_arch_cleanup(mod);
2136
2137 /* Module unload stuff */
2138 module_unload_free(mod);
2139
Rusty Russelle180a6b2009-03-31 13:05:29 -06002140 /* Free any allocated parameters. */
2141 destroy_params(mod->kp, mod->num_kp);
2142
Jessica Yu1ce15ef2016-03-22 20:03:16 -04002143 if (is_livepatch_module(mod))
2144 free_module_elf(mod);
2145
Rusty Russell944a1fa2013-04-17 13:20:03 +09302146 /* Now we can delete it from the lists */
2147 mutex_lock(&module_mutex);
Masami Hiramatsu461e34a2014-11-10 09:27:29 +10302148 /* Unlink carefully: kallsyms could be walking list. */
2149 list_del_rcu(&mod->list);
Peter Zijlstra93c2e102015-05-27 11:09:37 +09302150 mod_tree_remove(mod);
Masami Hiramatsu0286b5e2014-11-10 09:28:29 +10302151 /* Remove this module from bug list, this uses list_del_rcu */
Masami Hiramatsu461e34a2014-11-10 09:27:29 +10302152 module_bug_cleanup(mod);
Peter Zijlstra0be964b2015-05-27 11:09:35 +09302153 /* Wait for RCU-sched synchronizing before releasing mod->list and buglist. */
2154 synchronize_sched();
Rusty Russell944a1fa2013-04-17 13:20:03 +09302155 mutex_unlock(&module_mutex);
2156
Rusty Russell85c898d2015-11-26 09:45:08 +10302157 /* This may be empty, but that's OK */
2158 disable_ro_nx(&mod->init_layout);
Rusty Russelld453cde2015-01-20 09:07:04 +10302159 module_arch_freeing_init(mod);
Rusty Russell7523e4d2015-11-26 09:44:08 +10302160 module_memfree(mod->init_layout.base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161 kfree(mod->args);
Tejun Heo259354d2010-03-10 18:56:10 +09002162 percpu_modfree(mod);
Rusty Russell9f85a4b2010-08-05 12:59:04 -06002163
Peter Zijlstra35a93932015-02-26 16:23:11 +01002164 /* Free lock-classes; relies on the preceding sync_rcu(). */
Rusty Russell7523e4d2015-11-26 09:44:08 +10302165 lockdep_free_key_range(mod->core_layout.base, mod->core_layout.size);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002166
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167 /* Finally, free the core (containing the module structure) */
Rusty Russell85c898d2015-11-26 09:45:08 +10302168 disable_ro_nx(&mod->core_layout);
Rusty Russell7523e4d2015-11-26 09:44:08 +10302169 module_memfree(mod->core_layout.base);
Bernd Schmidteb8cdec2009-09-21 17:03:57 -07002170
2171#ifdef CONFIG_MPU
2172 update_protections(current->mm);
2173#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174}
2175
2176void *__symbol_get(const char *symbol)
2177{
2178 struct module *owner;
Tim Abbott414fd312008-12-05 19:03:56 -05002179 const struct kernel_symbol *sym;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180
Rusty Russell24da1cb2007-07-15 23:41:46 -07002181 preempt_disable();
Tim Abbott414fd312008-12-05 19:03:56 -05002182 sym = find_symbol(symbol, &owner, NULL, true, true);
2183 if (sym && strong_try_module_get(owner))
2184 sym = NULL;
Rusty Russell24da1cb2007-07-15 23:41:46 -07002185 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186
Tim Abbott414fd312008-12-05 19:03:56 -05002187 return sym ? (void *)sym->value : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188}
2189EXPORT_SYMBOL_GPL(__symbol_get);
2190
Ashutosh Naikeea8b542006-01-08 01:04:25 -08002191/*
2192 * Ensure that an exported symbol [global namespace] does not already exist
Robert P. J. Day02a3e592007-05-09 07:26:28 +02002193 * in the kernel or in some other module's exported symbol table.
Rusty Russellbe593f42010-06-05 11:17:37 -06002194 *
2195 * You must hold the module_mutex.
Ashutosh Naikeea8b542006-01-08 01:04:25 -08002196 */
2197static int verify_export_symbols(struct module *mod)
2198{
Rusty Russellb2111042008-05-01 21:15:00 -05002199 unsigned int i;
Ashutosh Naikeea8b542006-01-08 01:04:25 -08002200 struct module *owner;
Rusty Russellb2111042008-05-01 21:15:00 -05002201 const struct kernel_symbol *s;
2202 struct {
2203 const struct kernel_symbol *sym;
2204 unsigned int num;
2205 } arr[] = {
2206 { mod->syms, mod->num_syms },
2207 { mod->gpl_syms, mod->num_gpl_syms },
2208 { mod->gpl_future_syms, mod->num_gpl_future_syms },
Denys Vlasenkof7f5b672008-07-22 19:24:26 -05002209#ifdef CONFIG_UNUSED_SYMBOLS
Rusty Russellb2111042008-05-01 21:15:00 -05002210 { mod->unused_syms, mod->num_unused_syms },
2211 { mod->unused_gpl_syms, mod->num_unused_gpl_syms },
Denys Vlasenkof7f5b672008-07-22 19:24:26 -05002212#endif
Rusty Russellb2111042008-05-01 21:15:00 -05002213 };
Ashutosh Naikeea8b542006-01-08 01:04:25 -08002214
Rusty Russellb2111042008-05-01 21:15:00 -05002215 for (i = 0; i < ARRAY_SIZE(arr); i++) {
2216 for (s = arr[i].sym; s < arr[i].sym + arr[i].num; s++) {
Rusty Russellbe593f42010-06-05 11:17:37 -06002217 if (find_symbol(s->name, &owner, NULL, true, false)) {
Andrew Mortonbddb12b2013-11-12 15:11:28 -08002218 pr_err("%s: exports duplicate symbol %s"
Rusty Russellb2111042008-05-01 21:15:00 -05002219 " (owned by %s)\n",
2220 mod->name, s->name, module_name(owner));
2221 return -ENOEXEC;
2222 }
Ashutosh Naikeea8b542006-01-08 01:04:25 -08002223 }
Rusty Russellb2111042008-05-01 21:15:00 -05002224 }
2225 return 0;
Ashutosh Naikeea8b542006-01-08 01:04:25 -08002226}
2227
Matti Linnanvuori9a4b9702007-11-08 08:37:38 -08002228/* Change all symbols so that st_value encodes the pointer directly. */
Rusty Russell49668682010-08-05 12:59:10 -06002229static int simplify_symbols(struct module *mod, const struct load_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002230{
Rusty Russell49668682010-08-05 12:59:10 -06002231 Elf_Shdr *symsec = &info->sechdrs[info->index.sym];
2232 Elf_Sym *sym = (void *)symsec->sh_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002233 unsigned long secbase;
Rusty Russell49668682010-08-05 12:59:10 -06002234 unsigned int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002235 int ret = 0;
Tim Abbott414fd312008-12-05 19:03:56 -05002236 const struct kernel_symbol *ksym;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002237
Rusty Russell49668682010-08-05 12:59:10 -06002238 for (i = 1; i < symsec->sh_size / sizeof(Elf_Sym); i++) {
2239 const char *name = info->strtab + sym[i].st_name;
2240
Linus Torvalds1da177e2005-04-16 15:20:36 -07002241 switch (sym[i].st_shndx) {
2242 case SHN_COMMON:
Joe Mario80375982014-02-08 09:01:09 +01002243 /* Ignore common symbols */
2244 if (!strncmp(name, "__gnu_lto", 9))
2245 break;
2246
Linus Torvalds1da177e2005-04-16 15:20:36 -07002247 /* We compiled with -fno-common. These are not
2248 supposed to happen. */
Jim Cromie5e124162011-12-06 12:11:31 -07002249 pr_debug("Common symbol: %s\n", name);
Ionut Alexa6da0b562014-11-10 09:31:29 +10302250 pr_warn("%s: please compile with -fno-common\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002251 mod->name);
2252 ret = -ENOEXEC;
2253 break;
2254
2255 case SHN_ABS:
2256 /* Don't need to do anything */
Jim Cromie5e124162011-12-06 12:11:31 -07002257 pr_debug("Absolute symbol: 0x%08lx\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258 (long)sym[i].st_value);
2259 break;
2260
Jessica Yu1ce15ef2016-03-22 20:03:16 -04002261 case SHN_LIVEPATCH:
2262 /* Livepatch symbols are resolved by livepatch */
2263 break;
2264
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265 case SHN_UNDEF:
Rusty Russell49668682010-08-05 12:59:10 -06002266 ksym = resolve_symbol_wait(mod, info, name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267 /* Ok if resolved. */
Rusty Russell9bea7f22010-06-05 11:17:37 -06002268 if (ksym && !IS_ERR(ksym)) {
Tim Abbott414fd312008-12-05 19:03:56 -05002269 sym[i].st_value = ksym->value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002270 break;
Tim Abbott414fd312008-12-05 19:03:56 -05002271 }
2272
Linus Torvalds1da177e2005-04-16 15:20:36 -07002273 /* Ok if weak. */
Rusty Russell9bea7f22010-06-05 11:17:37 -06002274 if (!ksym && ELF_ST_BIND(sym[i].st_info) == STB_WEAK)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002275 break;
2276
Andrew Mortonbddb12b2013-11-12 15:11:28 -08002277 pr_warn("%s: Unknown symbol %s (err %li)\n",
2278 mod->name, name, PTR_ERR(ksym));
Rusty Russell9bea7f22010-06-05 11:17:37 -06002279 ret = PTR_ERR(ksym) ?: -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002280 break;
2281
2282 default:
2283 /* Divert to percpu allocation if a percpu var. */
Rusty Russell49668682010-08-05 12:59:10 -06002284 if (sym[i].st_shndx == info->index.pcpu)
Tejun Heo259354d2010-03-10 18:56:10 +09002285 secbase = (unsigned long)mod_percpu(mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002286 else
Rusty Russell49668682010-08-05 12:59:10 -06002287 secbase = info->sechdrs[sym[i].st_shndx].sh_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002288 sym[i].st_value += secbase;
2289 break;
2290 }
2291 }
2292
2293 return ret;
2294}
2295
Rusty Russell49668682010-08-05 12:59:10 -06002296static int apply_relocations(struct module *mod, const struct load_info *info)
Rusty Russell22e268e2010-08-05 12:59:05 -06002297{
2298 unsigned int i;
2299 int err = 0;
2300
2301 /* Now do relocations. */
Rusty Russell49668682010-08-05 12:59:10 -06002302 for (i = 1; i < info->hdr->e_shnum; i++) {
2303 unsigned int infosec = info->sechdrs[i].sh_info;
Rusty Russell22e268e2010-08-05 12:59:05 -06002304
2305 /* Not a valid relocation section? */
Rusty Russell49668682010-08-05 12:59:10 -06002306 if (infosec >= info->hdr->e_shnum)
Rusty Russell22e268e2010-08-05 12:59:05 -06002307 continue;
2308
2309 /* Don't bother with non-allocated sections */
Rusty Russell49668682010-08-05 12:59:10 -06002310 if (!(info->sechdrs[infosec].sh_flags & SHF_ALLOC))
Rusty Russell22e268e2010-08-05 12:59:05 -06002311 continue;
2312
Jessica Yu1ce15ef2016-03-22 20:03:16 -04002313 /* Livepatch relocation sections are applied by livepatch */
2314 if (info->sechdrs[i].sh_flags & SHF_RELA_LIVEPATCH)
2315 continue;
2316
Rusty Russell49668682010-08-05 12:59:10 -06002317 if (info->sechdrs[i].sh_type == SHT_REL)
2318 err = apply_relocate(info->sechdrs, info->strtab,
2319 info->index.sym, i, mod);
2320 else if (info->sechdrs[i].sh_type == SHT_RELA)
2321 err = apply_relocate_add(info->sechdrs, info->strtab,
2322 info->index.sym, i, mod);
Rusty Russell22e268e2010-08-05 12:59:05 -06002323 if (err < 0)
2324 break;
2325 }
2326 return err;
2327}
2328
Helge Deller088af9a2008-12-31 12:31:18 +01002329/* Additional bytes needed by arch in front of individual sections */
2330unsigned int __weak arch_mod_section_prepend(struct module *mod,
2331 unsigned int section)
2332{
2333 /* default implementation just returns zero */
2334 return 0;
2335}
2336
Linus Torvalds1da177e2005-04-16 15:20:36 -07002337/* Update size with this section: return offset. */
Helge Deller088af9a2008-12-31 12:31:18 +01002338static long get_offset(struct module *mod, unsigned int *size,
2339 Elf_Shdr *sechdr, unsigned int section)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002340{
2341 long ret;
2342
Helge Deller088af9a2008-12-31 12:31:18 +01002343 *size += arch_mod_section_prepend(mod, section);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002344 ret = ALIGN(*size, sechdr->sh_addralign ?: 1);
2345 *size = ret + sechdr->sh_size;
2346 return ret;
2347}
2348
2349/* Lay out the SHF_ALLOC sections in a way not dissimilar to how ld
2350 might -- code, read-only data, read-write data, small data. Tally
2351 sizes, and place the offsets into sh_entsize fields: high bit means it
2352 belongs in init. */
Rusty Russell49668682010-08-05 12:59:10 -06002353static void layout_sections(struct module *mod, struct load_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002354{
2355 static unsigned long const masks[][2] = {
2356 /* NOTE: all executable code must be the first section
2357 * in this array; otherwise modify the text_size
2358 * finder in the two loops below */
2359 { SHF_EXECINSTR | SHF_ALLOC, ARCH_SHF_SMALL },
2360 { SHF_ALLOC, SHF_WRITE | ARCH_SHF_SMALL },
Jessica Yu444d13f2016-07-27 12:06:21 +09302361 { SHF_RO_AFTER_INIT | SHF_ALLOC, ARCH_SHF_SMALL },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002362 { SHF_WRITE | SHF_ALLOC, ARCH_SHF_SMALL },
2363 { ARCH_SHF_SMALL | SHF_ALLOC, 0 }
2364 };
2365 unsigned int m, i;
2366
Rusty Russell49668682010-08-05 12:59:10 -06002367 for (i = 0; i < info->hdr->e_shnum; i++)
2368 info->sechdrs[i].sh_entsize = ~0UL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002369
Jim Cromie5e124162011-12-06 12:11:31 -07002370 pr_debug("Core section allocation order:\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002371 for (m = 0; m < ARRAY_SIZE(masks); ++m) {
Rusty Russell49668682010-08-05 12:59:10 -06002372 for (i = 0; i < info->hdr->e_shnum; ++i) {
2373 Elf_Shdr *s = &info->sechdrs[i];
2374 const char *sname = info->secstrings + s->sh_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002375
2376 if ((s->sh_flags & masks[m][0]) != masks[m][0]
2377 || (s->sh_flags & masks[m][1])
2378 || s->sh_entsize != ~0UL
Rusty Russell49668682010-08-05 12:59:10 -06002379 || strstarts(sname, ".init"))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002380 continue;
Rusty Russell7523e4d2015-11-26 09:44:08 +10302381 s->sh_entsize = get_offset(mod, &mod->core_layout.size, s, i);
Jim Cromie5e124162011-12-06 12:11:31 -07002382 pr_debug("\t%s\n", sname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002383 }
matthieu castet84e1c6b2010-11-16 22:35:16 +01002384 switch (m) {
2385 case 0: /* executable */
Rusty Russell7523e4d2015-11-26 09:44:08 +10302386 mod->core_layout.size = debug_align(mod->core_layout.size);
2387 mod->core_layout.text_size = mod->core_layout.size;
matthieu castet84e1c6b2010-11-16 22:35:16 +01002388 break;
2389 case 1: /* RO: text and ro-data */
Rusty Russell7523e4d2015-11-26 09:44:08 +10302390 mod->core_layout.size = debug_align(mod->core_layout.size);
2391 mod->core_layout.ro_size = mod->core_layout.size;
matthieu castet84e1c6b2010-11-16 22:35:16 +01002392 break;
Jessica Yu444d13f2016-07-27 12:06:21 +09302393 case 2: /* RO after init */
2394 mod->core_layout.size = debug_align(mod->core_layout.size);
2395 mod->core_layout.ro_after_init_size = mod->core_layout.size;
2396 break;
2397 case 4: /* whole core */
Rusty Russell7523e4d2015-11-26 09:44:08 +10302398 mod->core_layout.size = debug_align(mod->core_layout.size);
matthieu castet84e1c6b2010-11-16 22:35:16 +01002399 break;
2400 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002401 }
2402
Jim Cromie5e124162011-12-06 12:11:31 -07002403 pr_debug("Init section allocation order:\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002404 for (m = 0; m < ARRAY_SIZE(masks); ++m) {
Rusty Russell49668682010-08-05 12:59:10 -06002405 for (i = 0; i < info->hdr->e_shnum; ++i) {
2406 Elf_Shdr *s = &info->sechdrs[i];
2407 const char *sname = info->secstrings + s->sh_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002408
2409 if ((s->sh_flags & masks[m][0]) != masks[m][0]
2410 || (s->sh_flags & masks[m][1])
2411 || s->sh_entsize != ~0UL
Rusty Russell49668682010-08-05 12:59:10 -06002412 || !strstarts(sname, ".init"))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002413 continue;
Rusty Russell7523e4d2015-11-26 09:44:08 +10302414 s->sh_entsize = (get_offset(mod, &mod->init_layout.size, s, i)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002415 | INIT_OFFSET_MASK);
Jim Cromie5e124162011-12-06 12:11:31 -07002416 pr_debug("\t%s\n", sname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002417 }
matthieu castet84e1c6b2010-11-16 22:35:16 +01002418 switch (m) {
2419 case 0: /* executable */
Rusty Russell7523e4d2015-11-26 09:44:08 +10302420 mod->init_layout.size = debug_align(mod->init_layout.size);
2421 mod->init_layout.text_size = mod->init_layout.size;
matthieu castet84e1c6b2010-11-16 22:35:16 +01002422 break;
2423 case 1: /* RO: text and ro-data */
Rusty Russell7523e4d2015-11-26 09:44:08 +10302424 mod->init_layout.size = debug_align(mod->init_layout.size);
2425 mod->init_layout.ro_size = mod->init_layout.size;
matthieu castet84e1c6b2010-11-16 22:35:16 +01002426 break;
Jessica Yu444d13f2016-07-27 12:06:21 +09302427 case 2:
2428 /*
2429 * RO after init doesn't apply to init_layout (only
2430 * core_layout), so it just takes the value of ro_size.
2431 */
2432 mod->init_layout.ro_after_init_size = mod->init_layout.ro_size;
2433 break;
2434 case 4: /* whole init */
Rusty Russell7523e4d2015-11-26 09:44:08 +10302435 mod->init_layout.size = debug_align(mod->init_layout.size);
matthieu castet84e1c6b2010-11-16 22:35:16 +01002436 break;
2437 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002438 }
2439}
2440
Linus Torvalds1da177e2005-04-16 15:20:36 -07002441static void set_license(struct module *mod, const char *license)
2442{
2443 if (!license)
2444 license = "unspecified";
2445
Florin Malitafa3ba2e82006-10-11 01:21:48 -07002446 if (!license_is_gpl_compatible(license)) {
Andi Kleen25ddbb12008-10-15 22:01:41 -07002447 if (!test_taint(TAINT_PROPRIETARY_MODULE))
Andrew Mortonbddb12b2013-11-12 15:11:28 -08002448 pr_warn("%s: module license '%s' taints kernel.\n",
2449 mod->name, license);
Rusty Russell373d4d02013-01-21 17:17:39 +10302450 add_taint_module(mod, TAINT_PROPRIETARY_MODULE,
2451 LOCKDEP_NOW_UNRELIABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002452 }
2453}
2454
2455/* Parse tag=value strings from .modinfo section */
2456static char *next_string(char *string, unsigned long *secsize)
2457{
2458 /* Skip non-zero chars */
2459 while (string[0]) {
2460 string++;
2461 if ((*secsize)-- <= 1)
2462 return NULL;
2463 }
2464
2465 /* Skip any zero padding. */
2466 while (!string[0]) {
2467 string++;
2468 if ((*secsize)-- <= 1)
2469 return NULL;
2470 }
2471 return string;
2472}
2473
Rusty Russell49668682010-08-05 12:59:10 -06002474static char *get_modinfo(struct load_info *info, const char *tag)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002475{
2476 char *p;
2477 unsigned int taglen = strlen(tag);
Rusty Russell49668682010-08-05 12:59:10 -06002478 Elf_Shdr *infosec = &info->sechdrs[info->index.info];
2479 unsigned long size = infosec->sh_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002480
Rusty Russell49668682010-08-05 12:59:10 -06002481 for (p = (char *)infosec->sh_addr; p; p = next_string(p, &size)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002482 if (strncmp(p, tag, taglen) == 0 && p[taglen] == '=')
2483 return p + taglen + 1;
2484 }
2485 return NULL;
2486}
2487
Rusty Russell49668682010-08-05 12:59:10 -06002488static void setup_modinfo(struct module *mod, struct load_info *info)
Matt Domschc988d2b2005-06-23 22:05:15 -07002489{
2490 struct module_attribute *attr;
2491 int i;
2492
2493 for (i = 0; (attr = modinfo_attrs[i]); i++) {
2494 if (attr->setup)
Rusty Russell49668682010-08-05 12:59:10 -06002495 attr->setup(mod, get_modinfo(info, attr->attr.name));
Matt Domschc988d2b2005-06-23 22:05:15 -07002496 }
2497}
Matt Domschc988d2b2005-06-23 22:05:15 -07002498
Rusty Russella263f7762009-09-25 00:32:58 -06002499static void free_modinfo(struct module *mod)
2500{
2501 struct module_attribute *attr;
2502 int i;
2503
2504 for (i = 0; (attr = modinfo_attrs[i]); i++) {
2505 if (attr->free)
2506 attr->free(mod);
2507 }
2508}
2509
Linus Torvalds1da177e2005-04-16 15:20:36 -07002510#ifdef CONFIG_KALLSYMS
WANG Cong15bba372008-07-24 15:41:48 +01002511
2512/* lookup symbol in given range of kernel_symbols */
2513static const struct kernel_symbol *lookup_symbol(const char *name,
2514 const struct kernel_symbol *start,
2515 const struct kernel_symbol *stop)
2516{
Alessio Igor Bogani9d634872011-05-18 22:35:59 +02002517 return bsearch(name, start, stop - start,
2518 sizeof(struct kernel_symbol), cmp_name);
WANG Cong15bba372008-07-24 15:41:48 +01002519}
2520
Tim Abbottca4787b2009-01-05 08:40:10 -06002521static int is_exported(const char *name, unsigned long value,
2522 const struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002523{
Tim Abbottca4787b2009-01-05 08:40:10 -06002524 const struct kernel_symbol *ks;
2525 if (!mod)
2526 ks = lookup_symbol(name, __start___ksymtab, __stop___ksymtab);
Sam Ravnborg3fd68052006-02-08 21:16:45 +01002527 else
Tim Abbottca4787b2009-01-05 08:40:10 -06002528 ks = lookup_symbol(name, mod->syms, mod->syms + mod->num_syms);
2529 return ks != NULL && ks->value == value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002530}
2531
2532/* As per nm */
Rusty Russelleded41c2010-08-05 12:59:07 -06002533static char elf_type(const Elf_Sym *sym, const struct load_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002534{
Rusty Russelleded41c2010-08-05 12:59:07 -06002535 const Elf_Shdr *sechdrs = info->sechdrs;
2536
Linus Torvalds1da177e2005-04-16 15:20:36 -07002537 if (ELF_ST_BIND(sym->st_info) == STB_WEAK) {
2538 if (ELF_ST_TYPE(sym->st_info) == STT_OBJECT)
2539 return 'v';
2540 else
2541 return 'w';
2542 }
2543 if (sym->st_shndx == SHN_UNDEF)
2544 return 'U';
Miroslav Benese0224412015-11-26 13:18:06 +10302545 if (sym->st_shndx == SHN_ABS || sym->st_shndx == info->index.pcpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002546 return 'a';
2547 if (sym->st_shndx >= SHN_LORESERVE)
2548 return '?';
2549 if (sechdrs[sym->st_shndx].sh_flags & SHF_EXECINSTR)
2550 return 't';
2551 if (sechdrs[sym->st_shndx].sh_flags & SHF_ALLOC
2552 && sechdrs[sym->st_shndx].sh_type != SHT_NOBITS) {
2553 if (!(sechdrs[sym->st_shndx].sh_flags & SHF_WRITE))
2554 return 'r';
2555 else if (sechdrs[sym->st_shndx].sh_flags & ARCH_SHF_SMALL)
2556 return 'g';
2557 else
2558 return 'd';
2559 }
2560 if (sechdrs[sym->st_shndx].sh_type == SHT_NOBITS) {
2561 if (sechdrs[sym->st_shndx].sh_flags & ARCH_SHF_SMALL)
2562 return 's';
2563 else
2564 return 'b';
2565 }
Rusty Russelleded41c2010-08-05 12:59:07 -06002566 if (strstarts(info->secstrings + sechdrs[sym->st_shndx].sh_name,
2567 ".debug")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002568 return 'n';
Rusty Russelleded41c2010-08-05 12:59:07 -06002569 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002570 return '?';
2571}
2572
Jan Beulich4a496222009-07-06 14:50:42 +01002573static bool is_core_symbol(const Elf_Sym *src, const Elf_Shdr *sechdrs,
Miroslav Benese0224412015-11-26 13:18:06 +10302574 unsigned int shnum, unsigned int pcpundx)
Jan Beulich4a496222009-07-06 14:50:42 +01002575{
2576 const Elf_Shdr *sec;
2577
2578 if (src->st_shndx == SHN_UNDEF
2579 || src->st_shndx >= shnum
2580 || !src->st_name)
2581 return false;
2582
Miroslav Benese0224412015-11-26 13:18:06 +10302583#ifdef CONFIG_KALLSYMS_ALL
2584 if (src->st_shndx == pcpundx)
2585 return true;
2586#endif
2587
Jan Beulich4a496222009-07-06 14:50:42 +01002588 sec = sechdrs + src->st_shndx;
2589 if (!(sec->sh_flags & SHF_ALLOC)
2590#ifndef CONFIG_KALLSYMS_ALL
2591 || !(sec->sh_flags & SHF_EXECINSTR)
2592#endif
2593 || (sec->sh_entsize & INIT_OFFSET_MASK))
2594 return false;
2595
2596 return true;
2597}
2598
Kevin Cernekee48fd1182012-01-13 09:32:14 +10302599/*
2600 * We only allocate and copy the strings needed by the parts of symtab
2601 * we keep. This is simple, but has the effect of making multiple
2602 * copies of duplicates. We could be more sophisticated, see
2603 * linux-kernel thread starting with
2604 * <73defb5e4bca04a6431392cc341112b1@localhost>.
2605 */
Rusty Russell49668682010-08-05 12:59:10 -06002606static void layout_symtab(struct module *mod, struct load_info *info)
Jan Beulich4a496222009-07-06 14:50:42 +01002607{
Rusty Russell49668682010-08-05 12:59:10 -06002608 Elf_Shdr *symsect = info->sechdrs + info->index.sym;
2609 Elf_Shdr *strsect = info->sechdrs + info->index.str;
Jan Beulich4a496222009-07-06 14:50:42 +01002610 const Elf_Sym *src;
Satoru Takeuchi54523ec2012-12-05 12:29:04 +10302611 unsigned int i, nsrc, ndst, strtab_size = 0;
Jan Beulich4a496222009-07-06 14:50:42 +01002612
2613 /* Put symbol section at end of init part of module. */
2614 symsect->sh_flags |= SHF_ALLOC;
Rusty Russell7523e4d2015-11-26 09:44:08 +10302615 symsect->sh_entsize = get_offset(mod, &mod->init_layout.size, symsect,
Rusty Russell49668682010-08-05 12:59:10 -06002616 info->index.sym) | INIT_OFFSET_MASK;
Jim Cromie5e124162011-12-06 12:11:31 -07002617 pr_debug("\t%s\n", info->secstrings + symsect->sh_name);
Jan Beulich4a496222009-07-06 14:50:42 +01002618
Rusty Russell49668682010-08-05 12:59:10 -06002619 src = (void *)info->hdr + symsect->sh_offset;
Jan Beulich4a496222009-07-06 14:50:42 +01002620 nsrc = symsect->sh_size / sizeof(*src);
Kevin Cernekee70b1e9162011-11-12 19:08:55 -08002621
Kevin Cernekee48fd1182012-01-13 09:32:14 +10302622 /* Compute total space required for the core symbols' strtab. */
Rusty Russell59ef28b2012-10-25 10:49:25 +10302623 for (ndst = i = 0; i < nsrc; i++) {
Jessica Yu1ce15ef2016-03-22 20:03:16 -04002624 if (i == 0 || is_livepatch_module(mod) ||
Miroslav Benese0224412015-11-26 13:18:06 +10302625 is_core_symbol(src+i, info->sechdrs, info->hdr->e_shnum,
2626 info->index.pcpu)) {
Rusty Russell59ef28b2012-10-25 10:49:25 +10302627 strtab_size += strlen(&info->strtab[src[i].st_name])+1;
Kevin Cernekee48fd1182012-01-13 09:32:14 +10302628 ndst++;
Jan Beulich554bdfe2009-07-06 14:51:44 +01002629 }
Rusty Russell59ef28b2012-10-25 10:49:25 +10302630 }
Jan Beulich4a496222009-07-06 14:50:42 +01002631
2632 /* Append room for core symbols at end of core part. */
Rusty Russell7523e4d2015-11-26 09:44:08 +10302633 info->symoffs = ALIGN(mod->core_layout.size, symsect->sh_addralign ?: 1);
2634 info->stroffs = mod->core_layout.size = info->symoffs + ndst * sizeof(Elf_Sym);
2635 mod->core_layout.size += strtab_size;
2636 mod->core_layout.size = debug_align(mod->core_layout.size);
Jan Beulich4a496222009-07-06 14:50:42 +01002637
Jan Beulich554bdfe2009-07-06 14:51:44 +01002638 /* Put string table section at end of init part of module. */
2639 strsect->sh_flags |= SHF_ALLOC;
Rusty Russell7523e4d2015-11-26 09:44:08 +10302640 strsect->sh_entsize = get_offset(mod, &mod->init_layout.size, strsect,
Rusty Russell49668682010-08-05 12:59:10 -06002641 info->index.str) | INIT_OFFSET_MASK;
Jim Cromie5e124162011-12-06 12:11:31 -07002642 pr_debug("\t%s\n", info->secstrings + strsect->sh_name);
Rusty Russell82440622016-02-03 16:55:26 +10302643
2644 /* We'll tack temporary mod_kallsyms on the end. */
2645 mod->init_layout.size = ALIGN(mod->init_layout.size,
2646 __alignof__(struct mod_kallsyms));
2647 info->mod_kallsyms_init_off = mod->init_layout.size;
2648 mod->init_layout.size += sizeof(struct mod_kallsyms);
2649 mod->init_layout.size = debug_align(mod->init_layout.size);
Jan Beulich4a496222009-07-06 14:50:42 +01002650}
2651
Rusty Russell82440622016-02-03 16:55:26 +10302652/*
2653 * We use the full symtab and strtab which layout_symtab arranged to
2654 * be appended to the init section. Later we switch to the cut-down
2655 * core-only ones.
2656 */
Rusty Russell811d66a2010-08-05 12:59:12 -06002657static void add_kallsyms(struct module *mod, const struct load_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002658{
Jan Beulich4a496222009-07-06 14:50:42 +01002659 unsigned int i, ndst;
2660 const Elf_Sym *src;
2661 Elf_Sym *dst;
Jan Beulich554bdfe2009-07-06 14:51:44 +01002662 char *s;
Rusty Russelleded41c2010-08-05 12:59:07 -06002663 Elf_Shdr *symsec = &info->sechdrs[info->index.sym];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002664
Rusty Russell82440622016-02-03 16:55:26 +10302665 /* Set up to point into init section. */
2666 mod->kallsyms = mod->init_layout.base + info->mod_kallsyms_init_off;
2667
2668 mod->kallsyms->symtab = (void *)symsec->sh_addr;
2669 mod->kallsyms->num_symtab = symsec->sh_size / sizeof(Elf_Sym);
Rusty Russell511ca6a2010-08-05 12:59:08 -06002670 /* Make sure we get permanent strtab: don't use info->strtab. */
Rusty Russell82440622016-02-03 16:55:26 +10302671 mod->kallsyms->strtab = (void *)info->sechdrs[info->index.str].sh_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002672
2673 /* Set types up while we still have access to sections. */
Rusty Russell82440622016-02-03 16:55:26 +10302674 for (i = 0; i < mod->kallsyms->num_symtab; i++)
2675 mod->kallsyms->symtab[i].st_info
2676 = elf_type(&mod->kallsyms->symtab[i], info);
Jan Beulich4a496222009-07-06 14:50:42 +01002677
Rusty Russell82440622016-02-03 16:55:26 +10302678 /* Now populate the cut down core kallsyms for after init. */
2679 mod->core_kallsyms.symtab = dst = mod->core_layout.base + info->symoffs;
2680 mod->core_kallsyms.strtab = s = mod->core_layout.base + info->stroffs;
2681 src = mod->kallsyms->symtab;
2682 for (ndst = i = 0; i < mod->kallsyms->num_symtab; i++) {
Jessica Yu1ce15ef2016-03-22 20:03:16 -04002683 if (i == 0 || is_livepatch_module(mod) ||
Miroslav Benese0224412015-11-26 13:18:06 +10302684 is_core_symbol(src+i, info->sechdrs, info->hdr->e_shnum,
2685 info->index.pcpu)) {
Rusty Russell59ef28b2012-10-25 10:49:25 +10302686 dst[ndst] = src[i];
Rusty Russell82440622016-02-03 16:55:26 +10302687 dst[ndst++].st_name = s - mod->core_kallsyms.strtab;
2688 s += strlcpy(s, &mod->kallsyms->strtab[src[i].st_name],
Rusty Russell59ef28b2012-10-25 10:49:25 +10302689 KSYM_NAME_LEN) + 1;
2690 }
Jan Beulich4a496222009-07-06 14:50:42 +01002691 }
Rusty Russell82440622016-02-03 16:55:26 +10302692 mod->core_kallsyms.num_symtab = ndst;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002693}
2694#else
Rusty Russell49668682010-08-05 12:59:10 -06002695static inline void layout_symtab(struct module *mod, struct load_info *info)
Jan Beulich4a496222009-07-06 14:50:42 +01002696{
2697}
Paul Mundt3ae91c22009-10-01 15:43:54 -07002698
Michał Mirosławabbce902010-09-20 01:58:08 +02002699static void add_kallsyms(struct module *mod, const struct load_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002700{
2701}
2702#endif /* CONFIG_KALLSYMS */
2703
Jason Barone9d376f2009-02-05 11:51:38 -05002704static void dynamic_debug_setup(struct _ddebug *debug, unsigned int num)
Rusty Russell5e458cc2008-10-22 10:00:13 -05002705{
Rusty Russell811d66a2010-08-05 12:59:12 -06002706 if (!debug)
2707 return;
Jason Barone9d376f2009-02-05 11:51:38 -05002708#ifdef CONFIG_DYNAMIC_DEBUG
2709 if (ddebug_add_module(debug, num, debug->modname))
Andrew Mortonbddb12b2013-11-12 15:11:28 -08002710 pr_err("dynamic debug error adding module: %s\n",
2711 debug->modname);
Jason Barone9d376f2009-02-05 11:51:38 -05002712#endif
Rusty Russell5e458cc2008-10-22 10:00:13 -05002713}
Jason Baron346e15b2008-08-12 16:46:19 -04002714
Yehuda Sadehff49d742010-07-03 13:07:35 +10002715static void dynamic_debug_remove(struct _ddebug *debug)
2716{
2717 if (debug)
2718 ddebug_remove_module(debug->modname);
2719}
2720
Jonas Bonn74e08fc2011-06-30 21:22:11 +02002721void * __weak module_alloc(unsigned long size)
2722{
Rusty Russell82fab442012-12-11 09:38:33 +10302723 return vmalloc_exec(size);
Jonas Bonn74e08fc2011-06-30 21:22:11 +02002724}
2725
Catalin Marinas4f2294b2009-06-11 13:23:20 +01002726#ifdef CONFIG_DEBUG_KMEMLEAK
Rusty Russell49668682010-08-05 12:59:10 -06002727static void kmemleak_load_module(const struct module *mod,
2728 const struct load_info *info)
Catalin Marinas4f2294b2009-06-11 13:23:20 +01002729{
2730 unsigned int i;
2731
2732 /* only scan the sections containing data */
Catalin Marinasc017b4be32009-10-28 13:33:09 +00002733 kmemleak_scan_area(mod, sizeof(struct module), GFP_KERNEL);
Catalin Marinas4f2294b2009-06-11 13:23:20 +01002734
Rusty Russell49668682010-08-05 12:59:10 -06002735 for (i = 1; i < info->hdr->e_shnum; i++) {
Steven Rostedt06c94942013-05-15 20:33:01 +01002736 /* Scan all writable sections that's not executable */
2737 if (!(info->sechdrs[i].sh_flags & SHF_ALLOC) ||
2738 !(info->sechdrs[i].sh_flags & SHF_WRITE) ||
2739 (info->sechdrs[i].sh_flags & SHF_EXECINSTR))
Catalin Marinas4f2294b2009-06-11 13:23:20 +01002740 continue;
2741
Rusty Russell49668682010-08-05 12:59:10 -06002742 kmemleak_scan_area((void *)info->sechdrs[i].sh_addr,
2743 info->sechdrs[i].sh_size, GFP_KERNEL);
Catalin Marinas4f2294b2009-06-11 13:23:20 +01002744 }
2745}
2746#else
Rusty Russell49668682010-08-05 12:59:10 -06002747static inline void kmemleak_load_module(const struct module *mod,
2748 const struct load_info *info)
Catalin Marinas4f2294b2009-06-11 13:23:20 +01002749{
2750}
2751#endif
2752
Rusty Russell106a4ee2012-09-26 10:09:40 +01002753#ifdef CONFIG_MODULE_SIG
Ben Hutchingsbca014c2016-04-28 09:24:01 +09302754static int module_sig_check(struct load_info *info, int flags)
Rusty Russell106a4ee2012-09-26 10:09:40 +01002755{
2756 int err = -ENOKEY;
Kees Cook34e11692012-10-16 07:31:07 +10302757 const unsigned long markerlen = sizeof(MODULE_SIG_STRING) - 1;
2758 const void *mod = info->hdr;
Rusty Russell106a4ee2012-09-26 10:09:40 +01002759
Ben Hutchingsbca014c2016-04-28 09:24:01 +09302760 /*
2761 * Require flags == 0, as a module with version information
2762 * removed is no longer the module that was signed
2763 */
2764 if (flags == 0 &&
2765 info->len > markerlen &&
Kees Cook34e11692012-10-16 07:31:07 +10302766 memcmp(mod + info->len - markerlen, MODULE_SIG_STRING, markerlen) == 0) {
David Howellscaabe242012-10-20 01:19:29 +01002767 /* We truncate the module to discard the signature */
Kees Cook34e11692012-10-16 07:31:07 +10302768 info->len -= markerlen;
2769 err = mod_verify_sig(mod, &info->len);
Rusty Russell106a4ee2012-09-26 10:09:40 +01002770 }
2771
2772 if (!err) {
2773 info->sig_ok = true;
2774 return 0;
2775 }
2776
2777 /* Not having a signature is only an error if we're strict. */
2778 if (err == -ENOKEY && !sig_enforce)
2779 err = 0;
2780
2781 return err;
2782}
2783#else /* !CONFIG_MODULE_SIG */
Ben Hutchingsbca014c2016-04-28 09:24:01 +09302784static int module_sig_check(struct load_info *info, int flags)
Rusty Russell106a4ee2012-09-26 10:09:40 +01002785{
2786 return 0;
2787}
2788#endif /* !CONFIG_MODULE_SIG */
2789
Kees Cook34e11692012-10-16 07:31:07 +10302790/* Sanity checks against invalid binaries, wrong arch, weird elf version. */
2791static int elf_header_check(struct load_info *info)
Rusty Russell40dd2562010-08-05 12:59:03 -06002792{
Kees Cook34e11692012-10-16 07:31:07 +10302793 if (info->len < sizeof(*(info->hdr)))
Rusty Russell40dd2562010-08-05 12:59:03 -06002794 return -ENOEXEC;
2795
Kees Cook34e11692012-10-16 07:31:07 +10302796 if (memcmp(info->hdr->e_ident, ELFMAG, SELFMAG) != 0
2797 || info->hdr->e_type != ET_REL
2798 || !elf_check_arch(info->hdr)
2799 || info->hdr->e_shentsize != sizeof(Elf_Shdr))
2800 return -ENOEXEC;
2801
2802 if (info->hdr->e_shoff >= info->len
2803 || (info->hdr->e_shnum * sizeof(Elf_Shdr) >
2804 info->len - info->hdr->e_shoff))
2805 return -ENOEXEC;
2806
2807 return 0;
2808}
2809
Linus Torvalds3afe9f82015-04-07 10:33:49 -07002810#define COPY_CHUNK_SIZE (16*PAGE_SIZE)
2811
2812static int copy_chunked_from_user(void *dst, const void __user *usrc, unsigned long len)
2813{
2814 do {
2815 unsigned long n = min(len, COPY_CHUNK_SIZE);
2816
2817 if (copy_from_user(dst, usrc, n) != 0)
2818 return -EFAULT;
2819 cond_resched();
2820 dst += n;
2821 usrc += n;
2822 len -= n;
2823 } while (len);
2824 return 0;
2825}
2826
Jessica Yu1ce15ef2016-03-22 20:03:16 -04002827#ifdef CONFIG_LIVEPATCH
Josh Poimboeuf2992ef22016-08-25 10:04:45 -05002828static int check_modinfo_livepatch(struct module *mod, struct load_info *info)
Jessica Yu1ce15ef2016-03-22 20:03:16 -04002829{
Josh Poimboeuf2992ef22016-08-25 10:04:45 -05002830 if (get_modinfo(info, "livepatch")) {
2831 mod->klp = true;
2832 add_taint_module(mod, TAINT_LIVEPATCH, LOCKDEP_STILL_OK);
Joe Lawrence7598d162017-01-12 11:57:44 -05002833 pr_notice_once("%s: tainting kernel with TAINT_LIVEPATCH\n",
2834 mod->name);
Josh Poimboeuf2992ef22016-08-25 10:04:45 -05002835 }
Jessica Yu1ce15ef2016-03-22 20:03:16 -04002836
2837 return 0;
2838}
2839#else /* !CONFIG_LIVEPATCH */
Josh Poimboeuf2992ef22016-08-25 10:04:45 -05002840static int check_modinfo_livepatch(struct module *mod, struct load_info *info)
Jessica Yu1ce15ef2016-03-22 20:03:16 -04002841{
2842 if (get_modinfo(info, "livepatch")) {
2843 pr_err("%s: module is marked as livepatch module, but livepatch support is disabled",
2844 mod->name);
2845 return -ENOEXEC;
2846 }
2847
2848 return 0;
2849}
2850#endif /* CONFIG_LIVEPATCH */
2851
Kees Cook34e11692012-10-16 07:31:07 +10302852/* Sets info->hdr and info->len. */
2853static int copy_module_from_user(const void __user *umod, unsigned long len,
2854 struct load_info *info)
2855{
Kees Cook2e72d512012-10-16 07:32:07 +10302856 int err;
2857
Kees Cook34e11692012-10-16 07:31:07 +10302858 info->len = len;
2859 if (info->len < sizeof(*(info->hdr)))
Rusty Russell40dd2562010-08-05 12:59:03 -06002860 return -ENOEXEC;
2861
Mimi Zohara1db7422015-12-30 07:35:30 -05002862 err = security_kernel_read_file(NULL, READING_MODULE);
Kees Cook2e72d512012-10-16 07:32:07 +10302863 if (err)
2864 return err;
2865
Rusty Russell40dd2562010-08-05 12:59:03 -06002866 /* Suck in entire file: we'll want most of it. */
Kirill A. Shutemovcc9e6052015-03-24 12:31:40 +10302867 info->hdr = __vmalloc(info->len,
Michal Hocko19809c22017-05-08 15:57:44 -07002868 GFP_KERNEL | __GFP_NOWARN, PAGE_KERNEL);
Kees Cook34e11692012-10-16 07:31:07 +10302869 if (!info->hdr)
Rusty Russell40dd2562010-08-05 12:59:03 -06002870 return -ENOMEM;
2871
Linus Torvalds3afe9f82015-04-07 10:33:49 -07002872 if (copy_chunked_from_user(info->hdr, umod, info->len) != 0) {
Kees Cook34e11692012-10-16 07:31:07 +10302873 vfree(info->hdr);
2874 return -EFAULT;
Rusty Russell40dd2562010-08-05 12:59:03 -06002875 }
2876
Rusty Russell40dd2562010-08-05 12:59:03 -06002877 return 0;
Kees Cook34e11692012-10-16 07:31:07 +10302878}
Rusty Russell40dd2562010-08-05 12:59:03 -06002879
Rusty Russelld9131882010-08-05 12:59:08 -06002880static void free_copy(struct load_info *info)
2881{
Rusty Russelld9131882010-08-05 12:59:08 -06002882 vfree(info->hdr);
2883}
2884
Rusty Russell2f3238a2012-10-22 18:09:41 +10302885static int rewrite_section_headers(struct load_info *info, int flags)
Rusty Russell8b5f61a2010-08-05 12:59:06 -06002886{
2887 unsigned int i;
2888
2889 /* This should always be true, but let's be sure. */
2890 info->sechdrs[0].sh_addr = 0;
2891
2892 for (i = 1; i < info->hdr->e_shnum; i++) {
2893 Elf_Shdr *shdr = &info->sechdrs[i];
2894 if (shdr->sh_type != SHT_NOBITS
2895 && info->len < shdr->sh_offset + shdr->sh_size) {
Andrew Mortonbddb12b2013-11-12 15:11:28 -08002896 pr_err("Module len %lu truncated\n", info->len);
Rusty Russell8b5f61a2010-08-05 12:59:06 -06002897 return -ENOEXEC;
2898 }
2899
2900 /* Mark all sections sh_addr with their address in the
2901 temporary image. */
2902 shdr->sh_addr = (size_t)info->hdr + shdr->sh_offset;
2903
2904#ifndef CONFIG_MODULE_UNLOAD
2905 /* Don't load .exit sections */
2906 if (strstarts(info->secstrings+shdr->sh_name, ".exit"))
2907 shdr->sh_flags &= ~(unsigned long)SHF_ALLOC;
2908#endif
Rusty Russell8b5f61a2010-08-05 12:59:06 -06002909 }
Rusty Russelld6df72a2010-08-05 12:59:07 -06002910
2911 /* Track but don't keep modinfo and version sections. */
Rusty Russell2f3238a2012-10-22 18:09:41 +10302912 if (flags & MODULE_INIT_IGNORE_MODVERSIONS)
2913 info->index.vers = 0; /* Pretend no __versions section! */
2914 else
2915 info->index.vers = find_sec(info, "__versions");
Rusty Russell49668682010-08-05 12:59:10 -06002916 info->index.info = find_sec(info, ".modinfo");
Rusty Russelld6df72a2010-08-05 12:59:07 -06002917 info->sechdrs[info->index.info].sh_flags &= ~(unsigned long)SHF_ALLOC;
2918 info->sechdrs[info->index.vers].sh_flags &= ~(unsigned long)SHF_ALLOC;
Rusty Russell8b5f61a2010-08-05 12:59:06 -06002919 return 0;
2920}
2921
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002922/*
2923 * Set up our basic convenience variables (pointers to section headers,
2924 * search for module section index etc), and do some basic section
2925 * verification.
2926 *
2927 * Return the temporary module pointer (we'll replace it with the final
2928 * one when we move the module sections around).
2929 */
Rusty Russell2f3238a2012-10-22 18:09:41 +10302930static struct module *setup_load_info(struct load_info *info, int flags)
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002931{
2932 unsigned int i;
Rusty Russell8b5f61a2010-08-05 12:59:06 -06002933 int err;
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002934 struct module *mod;
2935
2936 /* Set up the convenience variables */
2937 info->sechdrs = (void *)info->hdr + info->hdr->e_shoff;
Rusty Russell8b5f61a2010-08-05 12:59:06 -06002938 info->secstrings = (void *)info->hdr
2939 + info->sechdrs[info->hdr->e_shstrndx].sh_offset;
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002940
Rusty Russell2f3238a2012-10-22 18:09:41 +10302941 err = rewrite_section_headers(info, flags);
Rusty Russell8b5f61a2010-08-05 12:59:06 -06002942 if (err)
2943 return ERR_PTR(err);
2944
2945 /* Find internal symbols and strings. */
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002946 for (i = 1; i < info->hdr->e_shnum; i++) {
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002947 if (info->sechdrs[i].sh_type == SHT_SYMTAB) {
2948 info->index.sym = i;
2949 info->index.str = info->sechdrs[i].sh_link;
Rusty Russell8b5f61a2010-08-05 12:59:06 -06002950 info->strtab = (char *)info->hdr
2951 + info->sechdrs[info->index.str].sh_offset;
2952 break;
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002953 }
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002954 }
2955
Rusty Russell49668682010-08-05 12:59:10 -06002956 info->index.mod = find_sec(info, ".gnu.linkonce.this_module");
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002957 if (!info->index.mod) {
Andrew Mortonbddb12b2013-11-12 15:11:28 -08002958 pr_warn("No module found in object\n");
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002959 return ERR_PTR(-ENOEXEC);
2960 }
2961 /* This is temporary: point mod into copy of data. */
2962 mod = (void *)info->sechdrs[info->index.mod].sh_addr;
2963
2964 if (info->index.sym == 0) {
Andrew Mortonbddb12b2013-11-12 15:11:28 -08002965 pr_warn("%s: module has no symbols (stripped?)\n", mod->name);
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002966 return ERR_PTR(-ENOEXEC);
2967 }
2968
Rusty Russell49668682010-08-05 12:59:10 -06002969 info->index.pcpu = find_pcpusec(info);
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002970
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002971 /* Check module struct version now, before we try to use module. */
Kees Cook49019422017-04-21 15:35:26 -07002972 if (!check_modstruct_version(info, mod))
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002973 return ERR_PTR(-ENOEXEC);
2974
2975 return mod;
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002976}
2977
Rusty Russell2f3238a2012-10-22 18:09:41 +10302978static int check_modinfo(struct module *mod, struct load_info *info, int flags)
Rusty Russell40dd2562010-08-05 12:59:03 -06002979{
Rusty Russell49668682010-08-05 12:59:10 -06002980 const char *modmagic = get_modinfo(info, "vermagic");
Rusty Russell40dd2562010-08-05 12:59:03 -06002981 int err;
2982
Rusty Russell2f3238a2012-10-22 18:09:41 +10302983 if (flags & MODULE_INIT_IGNORE_VERMAGIC)
2984 modmagic = NULL;
2985
Rusty Russell40dd2562010-08-05 12:59:03 -06002986 /* This is allowed: modprobe --force will invalidate it. */
2987 if (!modmagic) {
2988 err = try_to_force_load(mod, "bad vermagic");
2989 if (err)
2990 return err;
Rusty Russell49668682010-08-05 12:59:10 -06002991 } else if (!same_magic(modmagic, vermagic, info->index.vers)) {
Andrew Mortonbddb12b2013-11-12 15:11:28 -08002992 pr_err("%s: version magic '%s' should be '%s'\n",
Rusty Russell40dd2562010-08-05 12:59:03 -06002993 mod->name, modmagic, vermagic);
2994 return -ENOEXEC;
2995 }
2996
Libor Pechacek3205c362016-04-13 11:06:12 +09302997 if (!get_modinfo(info, "intree")) {
2998 if (!test_taint(TAINT_OOT_MODULE))
2999 pr_warn("%s: loading out-of-tree module taints kernel.\n",
3000 mod->name);
Rusty Russell373d4d02013-01-21 17:17:39 +10303001 add_taint_module(mod, TAINT_OOT_MODULE, LOCKDEP_STILL_OK);
Libor Pechacek3205c362016-04-13 11:06:12 +09303002 }
Ben Hutchings2449b8b2011-10-24 15:12:28 +02003003
Rusty Russell49668682010-08-05 12:59:10 -06003004 if (get_modinfo(info, "staging")) {
Rusty Russell373d4d02013-01-21 17:17:39 +10303005 add_taint_module(mod, TAINT_CRAP, LOCKDEP_STILL_OK);
Andrew Mortonbddb12b2013-11-12 15:11:28 -08003006 pr_warn("%s: module is from the staging directory, the quality "
3007 "is unknown, you have been warned.\n", mod->name);
Rusty Russell40dd2562010-08-05 12:59:03 -06003008 }
Rusty Russell22e268e2010-08-05 12:59:05 -06003009
Josh Poimboeuf2992ef22016-08-25 10:04:45 -05003010 err = check_modinfo_livepatch(mod, info);
Jessica Yu1ce15ef2016-03-22 20:03:16 -04003011 if (err)
3012 return err;
3013
Rusty Russell22e268e2010-08-05 12:59:05 -06003014 /* Set up license info based on the info section */
Rusty Russell49668682010-08-05 12:59:10 -06003015 set_license(mod, get_modinfo(info, "license"));
Rusty Russell22e268e2010-08-05 12:59:05 -06003016
Rusty Russell40dd2562010-08-05 12:59:03 -06003017 return 0;
3018}
3019
Frantisek Hrbataeb3057d2013-10-14 18:08:46 +10303020static int find_module_sections(struct module *mod, struct load_info *info)
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06003021{
Rusty Russell49668682010-08-05 12:59:10 -06003022 mod->kp = section_objs(info, "__param",
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06003023 sizeof(*mod->kp), &mod->num_kp);
Rusty Russell49668682010-08-05 12:59:10 -06003024 mod->syms = section_objs(info, "__ksymtab",
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06003025 sizeof(*mod->syms), &mod->num_syms);
Rusty Russell49668682010-08-05 12:59:10 -06003026 mod->crcs = section_addr(info, "__kcrctab");
3027 mod->gpl_syms = section_objs(info, "__ksymtab_gpl",
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06003028 sizeof(*mod->gpl_syms),
3029 &mod->num_gpl_syms);
Rusty Russell49668682010-08-05 12:59:10 -06003030 mod->gpl_crcs = section_addr(info, "__kcrctab_gpl");
3031 mod->gpl_future_syms = section_objs(info,
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06003032 "__ksymtab_gpl_future",
3033 sizeof(*mod->gpl_future_syms),
3034 &mod->num_gpl_future_syms);
Rusty Russell49668682010-08-05 12:59:10 -06003035 mod->gpl_future_crcs = section_addr(info, "__kcrctab_gpl_future");
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06003036
3037#ifdef CONFIG_UNUSED_SYMBOLS
Rusty Russell49668682010-08-05 12:59:10 -06003038 mod->unused_syms = section_objs(info, "__ksymtab_unused",
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06003039 sizeof(*mod->unused_syms),
3040 &mod->num_unused_syms);
Rusty Russell49668682010-08-05 12:59:10 -06003041 mod->unused_crcs = section_addr(info, "__kcrctab_unused");
3042 mod->unused_gpl_syms = section_objs(info, "__ksymtab_unused_gpl",
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06003043 sizeof(*mod->unused_gpl_syms),
3044 &mod->num_unused_gpl_syms);
Rusty Russell49668682010-08-05 12:59:10 -06003045 mod->unused_gpl_crcs = section_addr(info, "__kcrctab_unused_gpl");
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06003046#endif
3047#ifdef CONFIG_CONSTRUCTORS
Rusty Russell49668682010-08-05 12:59:10 -06003048 mod->ctors = section_objs(info, ".ctors",
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06003049 sizeof(*mod->ctors), &mod->num_ctors);
Frantisek Hrbataeb3057d2013-10-14 18:08:46 +10303050 if (!mod->ctors)
3051 mod->ctors = section_objs(info, ".init_array",
3052 sizeof(*mod->ctors), &mod->num_ctors);
3053 else if (find_sec(info, ".init_array")) {
3054 /*
3055 * This shouldn't happen with same compiler and binutils
3056 * building all parts of the module.
3057 */
Ionut Alexa6da0b562014-11-10 09:31:29 +10303058 pr_warn("%s: has both .ctors and .init_array.\n",
Frantisek Hrbataeb3057d2013-10-14 18:08:46 +10303059 mod->name);
3060 return -EINVAL;
3061 }
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06003062#endif
3063
3064#ifdef CONFIG_TRACEPOINTS
Mathieu Desnoyers65498642011-01-26 17:26:22 -05003065 mod->tracepoints_ptrs = section_objs(info, "__tracepoints_ptrs",
3066 sizeof(*mod->tracepoints_ptrs),
3067 &mod->num_tracepoints);
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06003068#endif
Jason Baronbf5438fc2010-09-17 11:09:00 -04003069#ifdef HAVE_JUMP_LABEL
3070 mod->jump_entries = section_objs(info, "__jump_table",
3071 sizeof(*mod->jump_entries),
3072 &mod->num_jump_entries);
3073#endif
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06003074#ifdef CONFIG_EVENT_TRACING
Rusty Russell49668682010-08-05 12:59:10 -06003075 mod->trace_events = section_objs(info, "_ftrace_events",
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06003076 sizeof(*mod->trace_events),
3077 &mod->num_trace_events);
Steven Rostedt (Red Hat)3673b8e2015-03-25 15:44:21 -04003078 mod->trace_enums = section_objs(info, "_ftrace_enum_map",
3079 sizeof(*mod->trace_enums),
3080 &mod->num_trace_enums);
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06003081#endif
Steven Rostedt13b9b6e2010-11-10 22:19:24 -05003082#ifdef CONFIG_TRACING
3083 mod->trace_bprintk_fmt_start = section_objs(info, "__trace_printk_fmt",
3084 sizeof(*mod->trace_bprintk_fmt_start),
3085 &mod->num_trace_bprintk_fmt);
Steven Rostedt13b9b6e2010-11-10 22:19:24 -05003086#endif
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06003087#ifdef CONFIG_FTRACE_MCOUNT_RECORD
3088 /* sechdrs[0].sh_size is always zero */
Rusty Russell49668682010-08-05 12:59:10 -06003089 mod->ftrace_callsites = section_objs(info, "__mcount_loc",
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06003090 sizeof(*mod->ftrace_callsites),
3091 &mod->num_ftrace_callsites);
3092#endif
Rusty Russell22e268e2010-08-05 12:59:05 -06003093
Rusty Russell811d66a2010-08-05 12:59:12 -06003094 mod->extable = section_objs(info, "__ex_table",
3095 sizeof(*mod->extable), &mod->num_exentries);
3096
Rusty Russell49668682010-08-05 12:59:10 -06003097 if (section_addr(info, "__obsparm"))
Andrew Mortonbddb12b2013-11-12 15:11:28 -08003098 pr_warn("%s: Ignoring obsolete parameters\n", mod->name);
Rusty Russell811d66a2010-08-05 12:59:12 -06003099
3100 info->debug = section_objs(info, "__verbose",
3101 sizeof(*info->debug), &info->num_debug);
Frantisek Hrbataeb3057d2013-10-14 18:08:46 +10303102
3103 return 0;
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06003104}
3105
Rusty Russell49668682010-08-05 12:59:10 -06003106static int move_module(struct module *mod, struct load_info *info)
Linus Torvalds65b8a9b2010-08-05 12:59:02 -06003107{
3108 int i;
3109 void *ptr;
3110
3111 /* Do the allocs. */
Rusty Russell7523e4d2015-11-26 09:44:08 +10303112 ptr = module_alloc(mod->core_layout.size);
Linus Torvalds65b8a9b2010-08-05 12:59:02 -06003113 /*
3114 * The pointer to this block is stored in the module structure
3115 * which is inside the block. Just mark it as not being a
3116 * leak.
3117 */
3118 kmemleak_not_leak(ptr);
3119 if (!ptr)
Rusty Russelld9131882010-08-05 12:59:08 -06003120 return -ENOMEM;
Linus Torvalds65b8a9b2010-08-05 12:59:02 -06003121
Rusty Russell7523e4d2015-11-26 09:44:08 +10303122 memset(ptr, 0, mod->core_layout.size);
3123 mod->core_layout.base = ptr;
Linus Torvalds65b8a9b2010-08-05 12:59:02 -06003124
Rusty Russell7523e4d2015-11-26 09:44:08 +10303125 if (mod->init_layout.size) {
3126 ptr = module_alloc(mod->init_layout.size);
Rusty Russell82fab442012-12-11 09:38:33 +10303127 /*
3128 * The pointer to this block is stored in the module structure
3129 * which is inside the block. This block doesn't need to be
3130 * scanned as it contains data and code that will be freed
3131 * after the module is initialized.
3132 */
3133 kmemleak_ignore(ptr);
3134 if (!ptr) {
Rusty Russell7523e4d2015-11-26 09:44:08 +10303135 module_memfree(mod->core_layout.base);
Rusty Russell82fab442012-12-11 09:38:33 +10303136 return -ENOMEM;
3137 }
Rusty Russell7523e4d2015-11-26 09:44:08 +10303138 memset(ptr, 0, mod->init_layout.size);
3139 mod->init_layout.base = ptr;
Rusty Russell82fab442012-12-11 09:38:33 +10303140 } else
Rusty Russell7523e4d2015-11-26 09:44:08 +10303141 mod->init_layout.base = NULL;
Linus Torvalds65b8a9b2010-08-05 12:59:02 -06003142
3143 /* Transfer each section which specifies SHF_ALLOC */
Jim Cromie5e124162011-12-06 12:11:31 -07003144 pr_debug("final section addresses:\n");
Rusty Russell49668682010-08-05 12:59:10 -06003145 for (i = 0; i < info->hdr->e_shnum; i++) {
Linus Torvalds65b8a9b2010-08-05 12:59:02 -06003146 void *dest;
Rusty Russell49668682010-08-05 12:59:10 -06003147 Elf_Shdr *shdr = &info->sechdrs[i];
Linus Torvalds65b8a9b2010-08-05 12:59:02 -06003148
Rusty Russell49668682010-08-05 12:59:10 -06003149 if (!(shdr->sh_flags & SHF_ALLOC))
Linus Torvalds65b8a9b2010-08-05 12:59:02 -06003150 continue;
3151
Rusty Russell49668682010-08-05 12:59:10 -06003152 if (shdr->sh_entsize & INIT_OFFSET_MASK)
Rusty Russell7523e4d2015-11-26 09:44:08 +10303153 dest = mod->init_layout.base
Rusty Russell49668682010-08-05 12:59:10 -06003154 + (shdr->sh_entsize & ~INIT_OFFSET_MASK);
Linus Torvalds65b8a9b2010-08-05 12:59:02 -06003155 else
Rusty Russell7523e4d2015-11-26 09:44:08 +10303156 dest = mod->core_layout.base + shdr->sh_entsize;
Linus Torvalds65b8a9b2010-08-05 12:59:02 -06003157
Rusty Russell49668682010-08-05 12:59:10 -06003158 if (shdr->sh_type != SHT_NOBITS)
3159 memcpy(dest, (void *)shdr->sh_addr, shdr->sh_size);
Linus Torvalds65b8a9b2010-08-05 12:59:02 -06003160 /* Update sh_addr to point to copy in image. */
Rusty Russell49668682010-08-05 12:59:10 -06003161 shdr->sh_addr = (unsigned long)dest;
Jim Cromie5e124162011-12-06 12:11:31 -07003162 pr_debug("\t0x%lx %s\n",
3163 (long)shdr->sh_addr, info->secstrings + shdr->sh_name);
Linus Torvalds65b8a9b2010-08-05 12:59:02 -06003164 }
Rusty Russelld9131882010-08-05 12:59:08 -06003165
3166 return 0;
Linus Torvalds65b8a9b2010-08-05 12:59:02 -06003167}
3168
Rusty Russell49668682010-08-05 12:59:10 -06003169static int check_module_license_and_versions(struct module *mod)
Rusty Russell22e268e2010-08-05 12:59:05 -06003170{
Libor Pechacek3205c362016-04-13 11:06:12 +09303171 int prev_taint = test_taint(TAINT_PROPRIETARY_MODULE);
3172
Rusty Russell22e268e2010-08-05 12:59:05 -06003173 /*
3174 * ndiswrapper is under GPL by itself, but loads proprietary modules.
3175 * Don't use add_taint_module(), as it would prevent ndiswrapper from
3176 * using GPL-only symbols it needs.
3177 */
3178 if (strcmp(mod->name, "ndiswrapper") == 0)
Rusty Russell373d4d02013-01-21 17:17:39 +10303179 add_taint(TAINT_PROPRIETARY_MODULE, LOCKDEP_NOW_UNRELIABLE);
Rusty Russell22e268e2010-08-05 12:59:05 -06003180
3181 /* driverloader was caught wrongly pretending to be under GPL */
3182 if (strcmp(mod->name, "driverloader") == 0)
Rusty Russell373d4d02013-01-21 17:17:39 +10303183 add_taint_module(mod, TAINT_PROPRIETARY_MODULE,
3184 LOCKDEP_NOW_UNRELIABLE);
Rusty Russell22e268e2010-08-05 12:59:05 -06003185
Matthew Garrettc99af372012-06-22 13:49:31 -04003186 /* lve claims to be GPL but upstream won't provide source */
3187 if (strcmp(mod->name, "lve") == 0)
Rusty Russell373d4d02013-01-21 17:17:39 +10303188 add_taint_module(mod, TAINT_PROPRIETARY_MODULE,
3189 LOCKDEP_NOW_UNRELIABLE);
Matthew Garrettc99af372012-06-22 13:49:31 -04003190
Libor Pechacek3205c362016-04-13 11:06:12 +09303191 if (!prev_taint && test_taint(TAINT_PROPRIETARY_MODULE))
3192 pr_warn("%s: module license taints kernel.\n", mod->name);
3193
Rusty Russell22e268e2010-08-05 12:59:05 -06003194#ifdef CONFIG_MODVERSIONS
3195 if ((mod->num_syms && !mod->crcs)
3196 || (mod->num_gpl_syms && !mod->gpl_crcs)
3197 || (mod->num_gpl_future_syms && !mod->gpl_future_crcs)
3198#ifdef CONFIG_UNUSED_SYMBOLS
3199 || (mod->num_unused_syms && !mod->unused_crcs)
3200 || (mod->num_unused_gpl_syms && !mod->unused_gpl_crcs)
3201#endif
3202 ) {
3203 return try_to_force_load(mod,
3204 "no versions for exported symbols");
3205 }
3206#endif
3207 return 0;
3208}
3209
3210static void flush_module_icache(const struct module *mod)
3211{
3212 mm_segment_t old_fs;
3213
3214 /* flush the icache in correct context */
3215 old_fs = get_fs();
3216 set_fs(KERNEL_DS);
3217
3218 /*
3219 * Flush the instruction cache, since we've played with text.
3220 * Do it before processing of module parameters, so the module
3221 * can provide parameter accessor functions of its own.
3222 */
Rusty Russell7523e4d2015-11-26 09:44:08 +10303223 if (mod->init_layout.base)
3224 flush_icache_range((unsigned long)mod->init_layout.base,
3225 (unsigned long)mod->init_layout.base
3226 + mod->init_layout.size);
3227 flush_icache_range((unsigned long)mod->core_layout.base,
3228 (unsigned long)mod->core_layout.base + mod->core_layout.size);
Rusty Russell22e268e2010-08-05 12:59:05 -06003229
3230 set_fs(old_fs);
3231}
3232
Jonas Bonn74e08fc2011-06-30 21:22:11 +02003233int __weak module_frob_arch_sections(Elf_Ehdr *hdr,
3234 Elf_Shdr *sechdrs,
3235 char *secstrings,
3236 struct module *mod)
3237{
3238 return 0;
3239}
3240
Prarit Bhargavabe7de5f2016-07-21 15:37:56 +09303241/* module_blacklist is a comma-separated list of module names */
3242static char *module_blacklist;
3243static bool blacklisted(char *module_name)
3244{
3245 const char *p;
3246 size_t len;
3247
3248 if (!module_blacklist)
3249 return false;
3250
3251 for (p = module_blacklist; *p; p += len) {
3252 len = strcspn(p, ",");
3253 if (strlen(module_name) == len && !memcmp(module_name, p, len))
3254 return true;
3255 if (p[len] == ',')
3256 len++;
3257 }
3258 return false;
3259}
3260core_param(module_blacklist, module_blacklist, charp, 0400);
3261
Rusty Russell2f3238a2012-10-22 18:09:41 +10303262static struct module *layout_and_allocate(struct load_info *info, int flags)
Rusty Russelld9131882010-08-05 12:59:08 -06003263{
3264 /* Module within temporary copy. */
3265 struct module *mod;
Jessica Yu444d13f2016-07-27 12:06:21 +09303266 unsigned int ndx;
Rusty Russelld9131882010-08-05 12:59:08 -06003267 int err;
3268
Rusty Russell2f3238a2012-10-22 18:09:41 +10303269 mod = setup_load_info(info, flags);
Rusty Russelld9131882010-08-05 12:59:08 -06003270 if (IS_ERR(mod))
3271 return mod;
3272
Prarit Bhargavabe7de5f2016-07-21 15:37:56 +09303273 if (blacklisted(mod->name))
3274 return ERR_PTR(-EPERM);
3275
Rusty Russell2f3238a2012-10-22 18:09:41 +10303276 err = check_modinfo(mod, info, flags);
Rusty Russelld9131882010-08-05 12:59:08 -06003277 if (err)
3278 return ERR_PTR(err);
3279
3280 /* Allow arches to frob section contents and sizes. */
Rusty Russell49668682010-08-05 12:59:10 -06003281 err = module_frob_arch_sections(info->hdr, info->sechdrs,
3282 info->secstrings, mod);
Rusty Russelld9131882010-08-05 12:59:08 -06003283 if (err < 0)
Rusty Russell8d8022e2013-07-03 10:06:28 +09303284 return ERR_PTR(err);
Rusty Russelld9131882010-08-05 12:59:08 -06003285
Rusty Russell8d8022e2013-07-03 10:06:28 +09303286 /* We will do a special allocation for per-cpu sections later. */
3287 info->sechdrs[info->index.pcpu].sh_flags &= ~(unsigned long)SHF_ALLOC;
Rusty Russelld9131882010-08-05 12:59:08 -06003288
Jessica Yu444d13f2016-07-27 12:06:21 +09303289 /*
3290 * Mark ro_after_init section with SHF_RO_AFTER_INIT so that
3291 * layout_sections() can put it in the right place.
3292 * Note: ro_after_init sections also have SHF_{WRITE,ALLOC} set.
3293 */
3294 ndx = find_sec(info, ".data..ro_after_init");
3295 if (ndx)
3296 info->sechdrs[ndx].sh_flags |= SHF_RO_AFTER_INIT;
3297
Rusty Russelld9131882010-08-05 12:59:08 -06003298 /* Determine total sizes, and put offsets in sh_entsize. For now
3299 this is done generically; there doesn't appear to be any
3300 special cases for the architectures. */
Rusty Russell49668682010-08-05 12:59:10 -06003301 layout_sections(mod, info);
Rusty Russell49668682010-08-05 12:59:10 -06003302 layout_symtab(mod, info);
Rusty Russelld9131882010-08-05 12:59:08 -06003303
3304 /* Allocate and move to the final place */
Rusty Russell49668682010-08-05 12:59:10 -06003305 err = move_module(mod, info);
Rusty Russelld9131882010-08-05 12:59:08 -06003306 if (err)
Rusty Russell8d8022e2013-07-03 10:06:28 +09303307 return ERR_PTR(err);
Rusty Russelld9131882010-08-05 12:59:08 -06003308
3309 /* Module has been copied to its final place now: return it. */
3310 mod = (void *)info->sechdrs[info->index.mod].sh_addr;
Rusty Russell49668682010-08-05 12:59:10 -06003311 kmemleak_load_module(mod, info);
Rusty Russelld9131882010-08-05 12:59:08 -06003312 return mod;
Rusty Russelld9131882010-08-05 12:59:08 -06003313}
3314
3315/* mod is no longer valid after this! */
3316static void module_deallocate(struct module *mod, struct load_info *info)
3317{
Rusty Russelld9131882010-08-05 12:59:08 -06003318 percpu_modfree(mod);
Rusty Russelld453cde2015-01-20 09:07:04 +10303319 module_arch_freeing_init(mod);
Rusty Russell7523e4d2015-11-26 09:44:08 +10303320 module_memfree(mod->init_layout.base);
3321 module_memfree(mod->core_layout.base);
Rusty Russelld9131882010-08-05 12:59:08 -06003322}
3323
Jonas Bonn74e08fc2011-06-30 21:22:11 +02003324int __weak module_finalize(const Elf_Ehdr *hdr,
3325 const Elf_Shdr *sechdrs,
3326 struct module *me)
3327{
3328 return 0;
3329}
3330
Rusty Russell811d66a2010-08-05 12:59:12 -06003331static int post_relocation(struct module *mod, const struct load_info *info)
3332{
Rusty Russell51f3d0f2010-08-05 12:59:13 -06003333 /* Sort exception table now relocations are done. */
Rusty Russell811d66a2010-08-05 12:59:12 -06003334 sort_extable(mod->extable, mod->extable + mod->num_exentries);
3335
3336 /* Copy relocated percpu area over. */
3337 percpu_modcopy(mod, (void *)info->sechdrs[info->index.pcpu].sh_addr,
3338 info->sechdrs[info->index.pcpu].sh_size);
3339
Rusty Russell51f3d0f2010-08-05 12:59:13 -06003340 /* Setup kallsyms-specific fields. */
Rusty Russell811d66a2010-08-05 12:59:12 -06003341 add_kallsyms(mod, info);
3342
3343 /* Arch-specific module finalizing. */
3344 return module_finalize(info->hdr, info->sechdrs, mod);
3345}
3346
Rusty Russell9bb9c3b2012-09-28 14:31:03 +09303347/* Is this module of this name done loading? No locks held. */
3348static bool finished_loading(const char *name)
3349{
3350 struct module *mod;
3351 bool ret;
3352
Peter Zijlstra9cc019b2015-02-11 15:01:13 +10303353 /*
3354 * The module_mutex should not be a heavily contended lock;
3355 * if we get the occasional sleep here, we'll go an extra iteration
3356 * in the wait_event_interruptible(), which is harmless.
3357 */
3358 sched_annotate_sleep();
Rusty Russell9bb9c3b2012-09-28 14:31:03 +09303359 mutex_lock(&module_mutex);
Mathias Krause4f6de4d52013-07-02 15:35:11 +09303360 mod = find_module_all(name, strlen(name), true);
Rusty Russell0d21b0e2013-01-12 11:38:44 +10303361 ret = !mod || mod->state == MODULE_STATE_LIVE
3362 || mod->state == MODULE_STATE_GOING;
Rusty Russell9bb9c3b2012-09-28 14:31:03 +09303363 mutex_unlock(&module_mutex);
3364
3365 return ret;
3366}
3367
Peter Oberparleiterb99b87f2009-06-17 16:28:03 -07003368/* Call module constructors. */
3369static void do_mod_ctors(struct module *mod)
3370{
3371#ifdef CONFIG_CONSTRUCTORS
3372 unsigned long i;
3373
3374 for (i = 0; i < mod->num_ctors; i++)
3375 mod->ctors[i]();
3376#endif
3377}
3378
Rusty Russellc7496372015-01-20 09:07:05 +10303379/* For freeing module_init on success, in case kallsyms traversing */
3380struct mod_initfree {
3381 struct rcu_head rcu;
3382 void *module_init;
3383};
3384
3385static void do_free_init(struct rcu_head *head)
3386{
3387 struct mod_initfree *m = container_of(head, struct mod_initfree, rcu);
3388 module_memfree(m->module_init);
3389 kfree(m);
3390}
3391
Jan Kiszkabe02a182015-02-17 13:46:50 -08003392/*
3393 * This is where the real work happens.
3394 *
3395 * Keep it uninlined to provide a reliable breakpoint target, e.g. for the gdb
3396 * helper command 'lx-symbols'.
3397 */
3398static noinline int do_init_module(struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003399{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003400 int ret = 0;
Rusty Russellc7496372015-01-20 09:07:05 +10303401 struct mod_initfree *freeinit;
3402
3403 freeinit = kmalloc(sizeof(*freeinit), GFP_KERNEL);
3404 if (!freeinit) {
3405 ret = -ENOMEM;
3406 goto fail;
3407 }
Rusty Russell7523e4d2015-11-26 09:44:08 +10303408 freeinit->module_init = mod->init_layout.base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003409
Tejun Heo774a1222013-01-15 18:52:51 -08003410 /*
3411 * We want to find out whether @mod uses async during init. Clear
3412 * PF_USED_ASYNC. async_schedule*() will set it.
3413 */
3414 current->flags &= ~PF_USED_ASYNC;
3415
Peter Oberparleiterb99b87f2009-06-17 16:28:03 -07003416 do_mod_ctors(mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003417 /* Start the module */
3418 if (mod->init != NULL)
Arjan van de Ven59f94152008-07-30 12:49:02 -07003419 ret = do_one_initcall(mod->init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003420 if (ret < 0) {
Rusty Russellc7496372015-01-20 09:07:05 +10303421 goto fail_free_freeinit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003422 }
Alexey Dobriyane24e2e62008-03-10 11:43:53 -07003423 if (ret > 0) {
Andrew Mortonbddb12b2013-11-12 15:11:28 -08003424 pr_warn("%s: '%s'->init suspiciously returned %d, it should "
3425 "follow 0/-E convention\n"
3426 "%s: loading module anyway...\n",
3427 __func__, mod->name, ret, __func__);
Alexey Dobriyane24e2e62008-03-10 11:43:53 -07003428 dump_stack();
3429 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003430
Rusty Russell6f139092012-09-28 14:31:03 +09303431 /* Now it's a first class citizen! */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003432 mod->state = MODULE_STATE_LIVE;
Masami Hiramatsu0deddf42009-01-06 14:41:54 -08003433 blocking_notifier_call_chain(&module_notify_list,
3434 MODULE_STATE_LIVE, mod);
Rusty Russell6c5db222008-03-10 11:43:52 -07003435
Tejun Heo774a1222013-01-15 18:52:51 -08003436 /*
3437 * We need to finish all async code before the module init sequence
3438 * is done. This has potential to deadlock. For example, a newly
3439 * detected block device can trigger request_module() of the
3440 * default iosched from async probing task. Once userland helper
3441 * reaches here, async_synchronize_full() will wait on the async
3442 * task waiting on request_module() and deadlock.
3443 *
3444 * This deadlock is avoided by perfomring async_synchronize_full()
3445 * iff module init queued any async jobs. This isn't a full
3446 * solution as it will deadlock the same if module loading from
3447 * async jobs nests more than once; however, due to the various
3448 * constraints, this hack seems to be the best option for now.
3449 * Please refer to the following thread for details.
3450 *
3451 * http://thread.gmane.org/gmane.linux.kernel/1420814
3452 */
Luis R. Rodriguezf2411da2015-03-30 16:20:05 -07003453 if (!mod->async_probe_requested && (current->flags & PF_USED_ASYNC))
Tejun Heo774a1222013-01-15 18:52:51 -08003454 async_synchronize_full();
Linus Torvaldsd6de2c82009-04-10 12:17:41 -07003455
Rusty Russell6c5db222008-03-10 11:43:52 -07003456 mutex_lock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003457 /* Drop initial reference. */
3458 module_put(mod);
Rusty Russellad6561d2009-06-12 21:47:03 -06003459 trim_init_extable(mod);
Jan Beulich4a496222009-07-06 14:50:42 +01003460#ifdef CONFIG_KALLSYMS
Rusty Russell82440622016-02-03 16:55:26 +10303461 /* Switch to core kallsyms now init is done: kallsyms may be walking! */
3462 rcu_assign_pointer(mod->kallsyms, &mod->core_kallsyms);
Jan Beulich4a496222009-07-06 14:50:42 +01003463#endif
Jessica Yu444d13f2016-07-27 12:06:21 +09303464 module_enable_ro(mod, true);
Peter Zijlstra93c2e102015-05-27 11:09:37 +09303465 mod_tree_remove_init(mod);
Rusty Russell85c898d2015-11-26 09:45:08 +10303466 disable_ro_nx(&mod->init_layout);
Rusty Russelld453cde2015-01-20 09:07:04 +10303467 module_arch_freeing_init(mod);
Rusty Russell7523e4d2015-11-26 09:44:08 +10303468 mod->init_layout.base = NULL;
3469 mod->init_layout.size = 0;
3470 mod->init_layout.ro_size = 0;
Jessica Yu444d13f2016-07-27 12:06:21 +09303471 mod->init_layout.ro_after_init_size = 0;
Rusty Russell7523e4d2015-11-26 09:44:08 +10303472 mod->init_layout.text_size = 0;
Rusty Russellc7496372015-01-20 09:07:05 +10303473 /*
3474 * We want to free module_init, but be aware that kallsyms may be
Peter Zijlstra0be964b2015-05-27 11:09:35 +09303475 * walking this with preempt disabled. In all the failure paths, we
3476 * call synchronize_sched(), but we don't want to slow down the success
3477 * path, so use actual RCU here.
Rusty Russellc7496372015-01-20 09:07:05 +10303478 */
Peter Zijlstra0be964b2015-05-27 11:09:35 +09303479 call_rcu_sched(&freeinit->rcu, do_free_init);
Ashutosh Naik6389a382006-03-23 03:00:46 -08003480 mutex_unlock(&module_mutex);
Rusty Russell6f139092012-09-28 14:31:03 +09303481 wake_up_all(&module_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003482
3483 return 0;
Rusty Russellc7496372015-01-20 09:07:05 +10303484
3485fail_free_freeinit:
3486 kfree(freeinit);
3487fail:
3488 /* Try to protect us from buggy refcounters. */
3489 mod->state = MODULE_STATE_GOING;
3490 synchronize_sched();
3491 module_put(mod);
3492 blocking_notifier_call_chain(&module_notify_list,
3493 MODULE_STATE_GOING, mod);
Jessica Yu7e545d62016-03-16 20:55:39 -04003494 klp_module_going(mod);
Jessica Yu7dcd1822016-02-16 17:32:33 -05003495 ftrace_release_mod(mod);
Rusty Russellc7496372015-01-20 09:07:05 +10303496 free_module(mod);
3497 wake_up_all(&module_wq);
3498 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003499}
3500
Kees Cook34e11692012-10-16 07:31:07 +10303501static int may_init_module(void)
3502{
3503 if (!capable(CAP_SYS_MODULE) || modules_disabled)
3504 return -EPERM;
3505
3506 return 0;
3507}
3508
Rusty Russella3535c72013-01-21 17:18:59 +10303509/*
3510 * We try to place it in the list now to make sure it's unique before
3511 * we dedicate too many resources. In particular, temporary percpu
3512 * memory exhaustion.
3513 */
3514static int add_unformed_module(struct module *mod)
3515{
3516 int err;
3517 struct module *old;
3518
3519 mod->state = MODULE_STATE_UNFORMED;
3520
3521again:
3522 mutex_lock(&module_mutex);
Mathias Krause4f6de4d52013-07-02 15:35:11 +09303523 old = find_module_all(mod->name, strlen(mod->name), true);
3524 if (old != NULL) {
Rusty Russella3535c72013-01-21 17:18:59 +10303525 if (old->state == MODULE_STATE_COMING
3526 || old->state == MODULE_STATE_UNFORMED) {
3527 /* Wait in case it fails to load. */
3528 mutex_unlock(&module_mutex);
Peter Zijlstra9cc019b2015-02-11 15:01:13 +10303529 err = wait_event_interruptible(module_wq,
3530 finished_loading(mod->name));
Rusty Russella3535c72013-01-21 17:18:59 +10303531 if (err)
3532 goto out_unlocked;
3533 goto again;
3534 }
3535 err = -EEXIST;
3536 goto out;
3537 }
Peter Zijlstra4f6665462015-05-27 11:09:38 +09303538 mod_update_bounds(mod);
Rusty Russella3535c72013-01-21 17:18:59 +10303539 list_add_rcu(&mod->list, &modules);
Peter Zijlstra93c2e102015-05-27 11:09:37 +09303540 mod_tree_insert(mod);
Rusty Russella3535c72013-01-21 17:18:59 +10303541 err = 0;
3542
3543out:
3544 mutex_unlock(&module_mutex);
3545out_unlocked:
3546 return err;
3547}
3548
3549static int complete_formation(struct module *mod, struct load_info *info)
3550{
3551 int err;
3552
3553 mutex_lock(&module_mutex);
3554
3555 /* Find duplicate symbols (must be called under lock). */
3556 err = verify_export_symbols(mod);
3557 if (err < 0)
3558 goto out;
3559
3560 /* This relies on module_mutex for list integrity. */
3561 module_bug_finalize(info->hdr, info->sechdrs, mod);
3562
Jessica Yu444d13f2016-07-27 12:06:21 +09303563 module_enable_ro(mod, false);
Rusty Russell85c898d2015-11-26 09:45:08 +10303564 module_enable_nx(mod);
Rusty Russell49822232014-05-14 10:54:19 +09303565
Rusty Russella3535c72013-01-21 17:18:59 +10303566 /* Mark state as coming so strong_try_module_get() ignores us,
3567 * but kallsyms etc. can see us. */
3568 mod->state = MODULE_STATE_COMING;
Rusty Russell49822232014-05-14 10:54:19 +09303569 mutex_unlock(&module_mutex);
3570
Rusty Russell49822232014-05-14 10:54:19 +09303571 return 0;
Rusty Russella3535c72013-01-21 17:18:59 +10303572
3573out:
3574 mutex_unlock(&module_mutex);
3575 return err;
3576}
3577
Jessica Yu4c973d12016-03-16 20:55:38 -04003578static int prepare_coming_module(struct module *mod)
3579{
Jessica Yu7e545d62016-03-16 20:55:39 -04003580 int err;
3581
Jessica Yu4c973d12016-03-16 20:55:38 -04003582 ftrace_module_enable(mod);
Jessica Yu7e545d62016-03-16 20:55:39 -04003583 err = klp_module_coming(mod);
3584 if (err)
3585 return err;
3586
Jessica Yu4c973d12016-03-16 20:55:38 -04003587 blocking_notifier_call_chain(&module_notify_list,
3588 MODULE_STATE_COMING, mod);
3589 return 0;
3590}
3591
Luis R. Rodriguezecc86172015-03-30 16:20:03 -07003592static int unknown_module_param_cb(char *param, char *val, const char *modname,
3593 void *arg)
Rusty Russell54041d82013-07-02 15:35:12 +09303594{
Luis R. Rodriguezf2411da2015-03-30 16:20:05 -07003595 struct module *mod = arg;
3596 int ret;
3597
3598 if (strcmp(param, "async_probe") == 0) {
3599 mod->async_probe_requested = true;
3600 return 0;
3601 }
3602
Ionut Alexa6da0b562014-11-10 09:31:29 +10303603 /* Check for magic 'dyndbg' arg */
Luis R. Rodriguezf2411da2015-03-30 16:20:05 -07003604 ret = ddebug_dyndbg_module_param_cb(param, val, modname);
Andrew Mortonbddb12b2013-11-12 15:11:28 -08003605 if (ret != 0)
3606 pr_warn("%s: unknown parameter '%s' ignored\n", modname, param);
Rusty Russell54041d82013-07-02 15:35:12 +09303607 return 0;
3608}
3609
Kees Cook34e11692012-10-16 07:31:07 +10303610/* Allocate and load the module: note that size of section 0 is always
3611 zero, and we rely on this for optional sections. */
Rusty Russell2f3238a2012-10-22 18:09:41 +10303612static int load_module(struct load_info *info, const char __user *uargs,
3613 int flags)
Kees Cook34e11692012-10-16 07:31:07 +10303614{
Rusty Russella3535c72013-01-21 17:18:59 +10303615 struct module *mod;
Kees Cook34e11692012-10-16 07:31:07 +10303616 long err;
Rusty Russell51e158c2014-04-28 11:34:33 +09303617 char *after_dashes;
Kees Cook34e11692012-10-16 07:31:07 +10303618
Ben Hutchingsbca014c2016-04-28 09:24:01 +09303619 err = module_sig_check(info, flags);
Kees Cook34e11692012-10-16 07:31:07 +10303620 if (err)
3621 goto free_copy;
3622
3623 err = elf_header_check(info);
3624 if (err)
3625 goto free_copy;
3626
3627 /* Figure out module layout, and allocate all the memory. */
Rusty Russell2f3238a2012-10-22 18:09:41 +10303628 mod = layout_and_allocate(info, flags);
Kees Cook34e11692012-10-16 07:31:07 +10303629 if (IS_ERR(mod)) {
3630 err = PTR_ERR(mod);
3631 goto free_copy;
3632 }
3633
Richard Guy Briggsca86cad2017-02-04 13:10:38 -05003634 audit_log_kern_module(mod->name);
3635
Rusty Russella3535c72013-01-21 17:18:59 +10303636 /* Reserve our place in the list. */
3637 err = add_unformed_module(mod);
3638 if (err)
Rusty Russell1fb93412013-01-12 13:27:34 +10303639 goto free_module;
Rusty Russell1fb93412013-01-12 13:27:34 +10303640
Kees Cook34e11692012-10-16 07:31:07 +10303641#ifdef CONFIG_MODULE_SIG
3642 mod->sig_ok = info->sig_ok;
Rusty Russell64748a22013-01-21 17:03:02 +10303643 if (!mod->sig_ok) {
Andrew Mortonbddb12b2013-11-12 15:11:28 -08003644 pr_notice_once("%s: module verification failed: signature "
Marcel Holtmannab92ebb2015-02-06 15:09:57 +10303645 "and/or required key missing - tainting "
Andrew Mortonbddb12b2013-11-12 15:11:28 -08003646 "kernel\n", mod->name);
Mathieu Desnoyers66cc69e2014-03-13 12:11:30 +10303647 add_taint_module(mod, TAINT_UNSIGNED_MODULE, LOCKDEP_STILL_OK);
Rusty Russell64748a22013-01-21 17:03:02 +10303648 }
Kees Cook34e11692012-10-16 07:31:07 +10303649#endif
3650
Rusty Russell8d8022e2013-07-03 10:06:28 +09303651 /* To avoid stressing percpu allocator, do this once we're unique. */
Rusty Russell9eb76d72013-07-03 10:06:29 +09303652 err = percpu_modalloc(mod, info);
Rusty Russell8d8022e2013-07-03 10:06:28 +09303653 if (err)
3654 goto unlink_mod;
3655
Kees Cook34e11692012-10-16 07:31:07 +10303656 /* Now module is in final location, initialize linked lists, etc. */
3657 err = module_unload_init(mod);
3658 if (err)
Rusty Russell1fb93412013-01-12 13:27:34 +10303659 goto unlink_mod;
Kees Cook34e11692012-10-16 07:31:07 +10303660
Rusty Russellcf2fde72015-06-26 06:44:38 +09303661 init_param_lock(mod);
Dan Streetmanb51d23e2015-06-17 06:18:52 +09303662
Kees Cook34e11692012-10-16 07:31:07 +10303663 /* Now we've got everything in the final locations, we can
3664 * find optional sections. */
Frantisek Hrbataeb3057d2013-10-14 18:08:46 +10303665 err = find_module_sections(mod, info);
3666 if (err)
3667 goto free_unload;
Kees Cook34e11692012-10-16 07:31:07 +10303668
3669 err = check_module_license_and_versions(mod);
3670 if (err)
3671 goto free_unload;
3672
3673 /* Set up MODINFO_ATTR fields */
3674 setup_modinfo(mod, info);
3675
3676 /* Fix up syms, so that st_value is a pointer to location. */
3677 err = simplify_symbols(mod, info);
3678 if (err < 0)
3679 goto free_modinfo;
3680
3681 err = apply_relocations(mod, info);
3682 if (err < 0)
3683 goto free_modinfo;
3684
3685 err = post_relocation(mod, info);
3686 if (err < 0)
3687 goto free_modinfo;
3688
3689 flush_module_icache(mod);
3690
3691 /* Now copy in args */
3692 mod->args = strndup_user(uargs, ~0UL >> 1);
3693 if (IS_ERR(mod->args)) {
3694 err = PTR_ERR(mod->args);
3695 goto free_arch_cleanup;
3696 }
3697
Kees Cook34e11692012-10-16 07:31:07 +10303698 dynamic_debug_setup(info->debug, info->num_debug);
3699
Steven Rostedt (Red Hat)a949ae52014-04-24 10:40:12 -04003700 /* Ftrace init must be called in the MODULE_STATE_UNFORMED state */
3701 ftrace_module_init(mod);
3702
Rusty Russella3535c72013-01-21 17:18:59 +10303703 /* Finally it's fully formed, ready to start executing. */
3704 err = complete_formation(mod, info);
3705 if (err)
Rusty Russell1fb93412013-01-12 13:27:34 +10303706 goto ddebug_cleanup;
Kees Cook34e11692012-10-16 07:31:07 +10303707
Jessica Yu4c973d12016-03-16 20:55:38 -04003708 err = prepare_coming_module(mod);
3709 if (err)
3710 goto bug_cleanup;
3711
Kees Cook34e11692012-10-16 07:31:07 +10303712 /* Module is ready to execute: parsing args may do that. */
Rusty Russell51e158c2014-04-28 11:34:33 +09303713 after_dashes = parse_args(mod->name, mod->args, mod->kp, mod->num_kp,
Luis R. Rodriguez4355efb2016-02-03 16:55:26 +10303714 -32768, 32767, mod,
Luis R. Rodriguezecc86172015-03-30 16:20:03 -07003715 unknown_module_param_cb);
Rusty Russell51e158c2014-04-28 11:34:33 +09303716 if (IS_ERR(after_dashes)) {
3717 err = PTR_ERR(after_dashes);
Jessica Yu4c973d12016-03-16 20:55:38 -04003718 goto coming_cleanup;
Rusty Russell51e158c2014-04-28 11:34:33 +09303719 } else if (after_dashes) {
3720 pr_warn("%s: parameters '%s' after `--' ignored\n",
3721 mod->name, after_dashes);
3722 }
Kees Cook34e11692012-10-16 07:31:07 +10303723
Richard Guy Briggsca86cad2017-02-04 13:10:38 -05003724 /* Link in to sysfs. */
Kees Cook34e11692012-10-16 07:31:07 +10303725 err = mod_sysfs_setup(mod, info, mod->kp, mod->num_kp);
3726 if (err < 0)
Jessica Yu4c973d12016-03-16 20:55:38 -04003727 goto coming_cleanup;
Kees Cook34e11692012-10-16 07:31:07 +10303728
Jessica Yu1ce15ef2016-03-22 20:03:16 -04003729 if (is_livepatch_module(mod)) {
3730 err = copy_module_elf(mod, info);
3731 if (err < 0)
3732 goto sysfs_cleanup;
3733 }
3734
Kees Cook34e11692012-10-16 07:31:07 +10303735 /* Get rid of temporary copy. */
3736 free_copy(info);
3737
3738 /* Done! */
3739 trace_module_load(mod);
3740
3741 return do_init_module(mod);
3742
Jessica Yu1ce15ef2016-03-22 20:03:16 -04003743 sysfs_cleanup:
3744 mod_sysfs_teardown(mod);
Jessica Yu4c973d12016-03-16 20:55:38 -04003745 coming_cleanup:
Aaron Tomlin885a78d2016-10-20 17:18:12 +01003746 mod->state = MODULE_STATE_GOING;
Luis R. Rodrigueza5544882017-02-10 14:06:22 -08003747 destroy_params(mod->kp, mod->num_kp);
Jessica Yu4c973d12016-03-16 20:55:38 -04003748 blocking_notifier_call_chain(&module_notify_list,
3749 MODULE_STATE_GOING, mod);
Jessica Yu7e545d62016-03-16 20:55:39 -04003750 klp_module_going(mod);
Rusty Russell1fb93412013-01-12 13:27:34 +10303751 bug_cleanup:
3752 /* module_bug_cleanup needs module_mutex protection */
Kees Cook34e11692012-10-16 07:31:07 +10303753 mutex_lock(&module_mutex);
Kees Cook34e11692012-10-16 07:31:07 +10303754 module_bug_cleanup(mod);
Linus Torvaldsee61abb2013-01-20 20:22:58 -08003755 mutex_unlock(&module_mutex);
Andy Lutomirskiff7e0052014-08-16 04:13:37 +09303756
3757 /* we can't deallocate the module until we clear memory protection */
Rusty Russell85c898d2015-11-26 09:45:08 +10303758 module_disable_ro(mod);
3759 module_disable_nx(mod);
Andy Lutomirskiff7e0052014-08-16 04:13:37 +09303760
Rusty Russella3535c72013-01-21 17:18:59 +10303761 ddebug_cleanup:
Rusty Russell1fb93412013-01-12 13:27:34 +10303762 dynamic_debug_remove(info->debug);
Kees Cook34e11692012-10-16 07:31:07 +10303763 synchronize_sched();
3764 kfree(mod->args);
3765 free_arch_cleanup:
3766 module_arch_cleanup(mod);
3767 free_modinfo:
3768 free_modinfo(mod);
3769 free_unload:
3770 module_unload_free(mod);
Rusty Russell1fb93412013-01-12 13:27:34 +10303771 unlink_mod:
3772 mutex_lock(&module_mutex);
3773 /* Unlink carefully: kallsyms could be walking list. */
3774 list_del_rcu(&mod->list);
Peter Zijlstra758556b2015-07-09 06:48:06 +09303775 mod_tree_remove(mod);
Rusty Russell1fb93412013-01-12 13:27:34 +10303776 wake_up_all(&module_wq);
Peter Zijlstra0be964b2015-05-27 11:09:35 +09303777 /* Wait for RCU-sched synchronizing before releasing mod->list. */
3778 synchronize_sched();
Rusty Russell1fb93412013-01-12 13:27:34 +10303779 mutex_unlock(&module_mutex);
Kees Cook34e11692012-10-16 07:31:07 +10303780 free_module:
Steven Rostedt (Red Hat)049fb9b2016-01-05 20:32:47 -05003781 /*
3782 * Ftrace needs to clean up what it initialized.
3783 * This does nothing if ftrace_module_init() wasn't called,
3784 * but it must be called outside of module_mutex.
3785 */
3786 ftrace_release_mod(mod);
Peter Zijlstra35a93932015-02-26 16:23:11 +01003787 /* Free lock-classes; relies on the preceding sync_rcu() */
Rusty Russell7523e4d2015-11-26 09:44:08 +10303788 lockdep_free_key_range(mod->core_layout.base, mod->core_layout.size);
Peter Zijlstra35a93932015-02-26 16:23:11 +01003789
Kees Cook34e11692012-10-16 07:31:07 +10303790 module_deallocate(mod, info);
3791 free_copy:
3792 free_copy(info);
3793 return err;
3794}
3795
3796SYSCALL_DEFINE3(init_module, void __user *, umod,
3797 unsigned long, len, const char __user *, uargs)
3798{
3799 int err;
3800 struct load_info info = { };
3801
3802 err = may_init_module();
3803 if (err)
3804 return err;
3805
3806 pr_debug("init_module: umod=%p, len=%lu, uargs=%p\n",
3807 umod, len, uargs);
3808
3809 err = copy_module_from_user(umod, len, &info);
3810 if (err)
3811 return err;
3812
Rusty Russell2f3238a2012-10-22 18:09:41 +10303813 return load_module(&info, uargs, 0);
Kees Cook34e11692012-10-16 07:31:07 +10303814}
3815
Rusty Russell2f3238a2012-10-22 18:09:41 +10303816SYSCALL_DEFINE3(finit_module, int, fd, const char __user *, uargs, int, flags)
Kees Cook34e11692012-10-16 07:31:07 +10303817{
Kees Cook34e11692012-10-16 07:31:07 +10303818 struct load_info info = { };
Mimi Zohara1db7422015-12-30 07:35:30 -05003819 loff_t size;
3820 void *hdr;
3821 int err;
Kees Cook34e11692012-10-16 07:31:07 +10303822
3823 err = may_init_module();
3824 if (err)
3825 return err;
3826
Rusty Russell2f3238a2012-10-22 18:09:41 +10303827 pr_debug("finit_module: fd=%d, uargs=%p, flags=%i\n", fd, uargs, flags);
3828
3829 if (flags & ~(MODULE_INIT_IGNORE_MODVERSIONS
3830 |MODULE_INIT_IGNORE_VERMAGIC))
3831 return -EINVAL;
Kees Cook34e11692012-10-16 07:31:07 +10303832
Mimi Zohara1db7422015-12-30 07:35:30 -05003833 err = kernel_read_file_from_fd(fd, &hdr, &size, INT_MAX,
3834 READING_MODULE);
Kees Cook34e11692012-10-16 07:31:07 +10303835 if (err)
3836 return err;
Mimi Zohara1db7422015-12-30 07:35:30 -05003837 info.hdr = hdr;
3838 info.len = size;
Kees Cook34e11692012-10-16 07:31:07 +10303839
Rusty Russell2f3238a2012-10-22 18:09:41 +10303840 return load_module(&info, uargs, flags);
Kees Cook34e11692012-10-16 07:31:07 +10303841}
3842
Linus Torvalds1da177e2005-04-16 15:20:36 -07003843static inline int within(unsigned long addr, void *start, unsigned long size)
3844{
3845 return ((void *)addr >= start && (void *)addr < start + size);
3846}
3847
3848#ifdef CONFIG_KALLSYMS
3849/*
3850 * This ignores the intensely annoying "mapping symbols" found
3851 * in ARM ELF files: $a, $t and $d.
3852 */
3853static inline int is_arm_mapping_symbol(const char *str)
3854{
Russell King2e3a10a2014-07-27 07:29:01 +09303855 if (str[0] == '.' && str[1] == 'L')
3856 return true;
Kyle McMartin6c34f1f2014-09-16 22:37:18 +01003857 return str[0] == '$' && strchr("axtd", str[1])
Linus Torvalds1da177e2005-04-16 15:20:36 -07003858 && (str[2] == '\0' || str[2] == '.');
3859}
3860
Rusty Russell82440622016-02-03 16:55:26 +10303861static const char *symname(struct mod_kallsyms *kallsyms, unsigned int symnum)
Rusty Russell2e7bac52016-02-03 16:55:26 +10303862{
Rusty Russell82440622016-02-03 16:55:26 +10303863 return kallsyms->strtab + kallsyms->symtab[symnum].st_name;
Rusty Russell2e7bac52016-02-03 16:55:26 +10303864}
3865
Linus Torvalds1da177e2005-04-16 15:20:36 -07003866static const char *get_ksymbol(struct module *mod,
3867 unsigned long addr,
3868 unsigned long *size,
3869 unsigned long *offset)
3870{
3871 unsigned int i, best = 0;
3872 unsigned long nextval;
Rusty Russell82440622016-02-03 16:55:26 +10303873 struct mod_kallsyms *kallsyms = rcu_dereference_sched(mod->kallsyms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003874
3875 /* At worse, next value is at end of module */
Masami Hiramatsua06f6212009-01-06 14:41:49 -08003876 if (within_module_init(addr, mod))
Rusty Russell7523e4d2015-11-26 09:44:08 +10303877 nextval = (unsigned long)mod->init_layout.base+mod->init_layout.text_size;
Daniel Walker22a8bde2007-10-18 03:06:07 -07003878 else
Rusty Russell7523e4d2015-11-26 09:44:08 +10303879 nextval = (unsigned long)mod->core_layout.base+mod->core_layout.text_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003880
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003881 /* Scan for closest preceding symbol, and next symbol. (ELF
Daniel Walker22a8bde2007-10-18 03:06:07 -07003882 starts real symbols at 1). */
Rusty Russell82440622016-02-03 16:55:26 +10303883 for (i = 1; i < kallsyms->num_symtab; i++) {
3884 if (kallsyms->symtab[i].st_shndx == SHN_UNDEF)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003885 continue;
3886
3887 /* We ignore unnamed symbols: they're uninformative
3888 * and inserted at a whim. */
Rusty Russell82440622016-02-03 16:55:26 +10303889 if (*symname(kallsyms, i) == '\0'
3890 || is_arm_mapping_symbol(symname(kallsyms, i)))
Rusty Russell2e7bac52016-02-03 16:55:26 +10303891 continue;
3892
Rusty Russell82440622016-02-03 16:55:26 +10303893 if (kallsyms->symtab[i].st_value <= addr
3894 && kallsyms->symtab[i].st_value > kallsyms->symtab[best].st_value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003895 best = i;
Rusty Russell82440622016-02-03 16:55:26 +10303896 if (kallsyms->symtab[i].st_value > addr
3897 && kallsyms->symtab[i].st_value < nextval)
3898 nextval = kallsyms->symtab[i].st_value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003899 }
3900
3901 if (!best)
3902 return NULL;
3903
Alexey Dobriyanffb45122007-05-08 00:28:41 -07003904 if (size)
Rusty Russell82440622016-02-03 16:55:26 +10303905 *size = nextval - kallsyms->symtab[best].st_value;
Alexey Dobriyanffb45122007-05-08 00:28:41 -07003906 if (offset)
Rusty Russell82440622016-02-03 16:55:26 +10303907 *offset = addr - kallsyms->symtab[best].st_value;
3908 return symname(kallsyms, best);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003909}
3910
Rusty Russell6dd06c92008-01-29 17:13:22 -05003911/* For kallsyms to ask for address resolution. NULL means not found. Careful
3912 * not to lock to avoid deadlock on oopses, simply disable preemption. */
Andrew Morton92dfc9d2008-02-08 04:18:43 -08003913const char *module_address_lookup(unsigned long addr,
Rusty Russell6dd06c92008-01-29 17:13:22 -05003914 unsigned long *size,
3915 unsigned long *offset,
3916 char **modname,
3917 char *namebuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003918{
Rusty Russellcb2a5202008-01-14 00:55:03 -08003919 const char *ret = NULL;
Peter Zijlstrab7df4d12015-05-27 11:09:37 +09303920 struct module *mod;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003921
Rusty Russellcb2a5202008-01-14 00:55:03 -08003922 preempt_disable();
Peter Zijlstrab7df4d12015-05-27 11:09:37 +09303923 mod = __module_address(addr);
3924 if (mod) {
3925 if (modname)
3926 *modname = mod->name;
3927 ret = get_ksymbol(mod, addr, size, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003928 }
Rusty Russell6dd06c92008-01-29 17:13:22 -05003929 /* Make a copy in here where it's safe */
3930 if (ret) {
3931 strncpy(namebuf, ret, KSYM_NAME_LEN - 1);
3932 ret = namebuf;
3933 }
Rusty Russellcb2a5202008-01-14 00:55:03 -08003934 preempt_enable();
Peter Zijlstrab7df4d12015-05-27 11:09:37 +09303935
Andrew Morton92dfc9d2008-02-08 04:18:43 -08003936 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003937}
3938
Alexey Dobriyan9d65cb42007-05-08 00:28:43 -07003939int lookup_module_symbol_name(unsigned long addr, char *symname)
3940{
3941 struct module *mod;
3942
Rusty Russellcb2a5202008-01-14 00:55:03 -08003943 preempt_disable();
Andi Kleend72b3752008-08-30 10:09:00 +02003944 list_for_each_entry_rcu(mod, &modules, list) {
Rusty Russell0d21b0e2013-01-12 11:38:44 +10303945 if (mod->state == MODULE_STATE_UNFORMED)
3946 continue;
Petr Mladek9b20a352014-07-27 07:24:01 +09303947 if (within_module(addr, mod)) {
Alexey Dobriyan9d65cb42007-05-08 00:28:43 -07003948 const char *sym;
3949
3950 sym = get_ksymbol(mod, addr, NULL, NULL);
3951 if (!sym)
3952 goto out;
Tejun Heo9281ace2007-07-17 04:03:51 -07003953 strlcpy(symname, sym, KSYM_NAME_LEN);
Rusty Russellcb2a5202008-01-14 00:55:03 -08003954 preempt_enable();
Alexey Dobriyan9d65cb42007-05-08 00:28:43 -07003955 return 0;
3956 }
3957 }
3958out:
Rusty Russellcb2a5202008-01-14 00:55:03 -08003959 preempt_enable();
Alexey Dobriyan9d65cb42007-05-08 00:28:43 -07003960 return -ERANGE;
3961}
3962
Alexey Dobriyana5c43da2007-05-08 00:28:47 -07003963int lookup_module_symbol_attrs(unsigned long addr, unsigned long *size,
3964 unsigned long *offset, char *modname, char *name)
3965{
3966 struct module *mod;
3967
Rusty Russellcb2a5202008-01-14 00:55:03 -08003968 preempt_disable();
Andi Kleend72b3752008-08-30 10:09:00 +02003969 list_for_each_entry_rcu(mod, &modules, list) {
Rusty Russell0d21b0e2013-01-12 11:38:44 +10303970 if (mod->state == MODULE_STATE_UNFORMED)
3971 continue;
Petr Mladek9b20a352014-07-27 07:24:01 +09303972 if (within_module(addr, mod)) {
Alexey Dobriyana5c43da2007-05-08 00:28:47 -07003973 const char *sym;
3974
3975 sym = get_ksymbol(mod, addr, size, offset);
3976 if (!sym)
3977 goto out;
3978 if (modname)
Tejun Heo9281ace2007-07-17 04:03:51 -07003979 strlcpy(modname, mod->name, MODULE_NAME_LEN);
Alexey Dobriyana5c43da2007-05-08 00:28:47 -07003980 if (name)
Tejun Heo9281ace2007-07-17 04:03:51 -07003981 strlcpy(name, sym, KSYM_NAME_LEN);
Rusty Russellcb2a5202008-01-14 00:55:03 -08003982 preempt_enable();
Alexey Dobriyana5c43da2007-05-08 00:28:47 -07003983 return 0;
3984 }
3985 }
3986out:
Rusty Russellcb2a5202008-01-14 00:55:03 -08003987 preempt_enable();
Alexey Dobriyana5c43da2007-05-08 00:28:47 -07003988 return -ERANGE;
3989}
3990
Alexey Dobriyanea078902007-05-08 00:28:39 -07003991int module_get_kallsym(unsigned int symnum, unsigned long *value, char *type,
3992 char *name, char *module_name, int *exported)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003993{
3994 struct module *mod;
3995
Rusty Russellcb2a5202008-01-14 00:55:03 -08003996 preempt_disable();
Andi Kleend72b3752008-08-30 10:09:00 +02003997 list_for_each_entry_rcu(mod, &modules, list) {
Rusty Russell82440622016-02-03 16:55:26 +10303998 struct mod_kallsyms *kallsyms;
3999
Rusty Russell0d21b0e2013-01-12 11:38:44 +10304000 if (mod->state == MODULE_STATE_UNFORMED)
4001 continue;
Rusty Russell82440622016-02-03 16:55:26 +10304002 kallsyms = rcu_dereference_sched(mod->kallsyms);
4003 if (symnum < kallsyms->num_symtab) {
4004 *value = kallsyms->symtab[symnum].st_value;
4005 *type = kallsyms->symtab[symnum].st_info;
4006 strlcpy(name, symname(kallsyms, symnum), KSYM_NAME_LEN);
Tejun Heo9281ace2007-07-17 04:03:51 -07004007 strlcpy(module_name, mod->name, MODULE_NAME_LEN);
Tim Abbottca4787b2009-01-05 08:40:10 -06004008 *exported = is_exported(name, *value, mod);
Rusty Russellcb2a5202008-01-14 00:55:03 -08004009 preempt_enable();
Alexey Dobriyanea078902007-05-08 00:28:39 -07004010 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004011 }
Rusty Russell82440622016-02-03 16:55:26 +10304012 symnum -= kallsyms->num_symtab;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004013 }
Rusty Russellcb2a5202008-01-14 00:55:03 -08004014 preempt_enable();
Alexey Dobriyanea078902007-05-08 00:28:39 -07004015 return -ERANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004016}
4017
4018static unsigned long mod_find_symname(struct module *mod, const char *name)
4019{
4020 unsigned int i;
Rusty Russell82440622016-02-03 16:55:26 +10304021 struct mod_kallsyms *kallsyms = rcu_dereference_sched(mod->kallsyms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004022
Rusty Russell82440622016-02-03 16:55:26 +10304023 for (i = 0; i < kallsyms->num_symtab; i++)
4024 if (strcmp(name, symname(kallsyms, i)) == 0 &&
4025 kallsyms->symtab[i].st_info != 'U')
4026 return kallsyms->symtab[i].st_value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004027 return 0;
4028}
4029
4030/* Look for this name: can be of form module:name. */
4031unsigned long module_kallsyms_lookup_name(const char *name)
4032{
4033 struct module *mod;
4034 char *colon;
4035 unsigned long ret = 0;
4036
4037 /* Don't lock: we're in enough trouble already. */
Rusty Russellcb2a5202008-01-14 00:55:03 -08004038 preempt_disable();
Naveen N. Rao17586182017-04-23 22:53:43 +05304039 if ((colon = strnchr(name, MODULE_NAME_LEN, ':')) != NULL) {
Mathias Krause4f6de4d52013-07-02 15:35:11 +09304040 if ((mod = find_module_all(name, colon - name, false)) != NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004041 ret = mod_find_symname(mod, colon+1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004042 } else {
Rusty Russell0d21b0e2013-01-12 11:38:44 +10304043 list_for_each_entry_rcu(mod, &modules, list) {
4044 if (mod->state == MODULE_STATE_UNFORMED)
4045 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004046 if ((ret = mod_find_symname(mod, name)) != 0)
4047 break;
Rusty Russell0d21b0e2013-01-12 11:38:44 +10304048 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004049 }
Rusty Russellcb2a5202008-01-14 00:55:03 -08004050 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004051 return ret;
4052}
Anders Kaseorg75a66612008-12-05 19:03:58 -05004053
4054int module_kallsyms_on_each_symbol(int (*fn)(void *, const char *,
4055 struct module *, unsigned long),
4056 void *data)
4057{
4058 struct module *mod;
4059 unsigned int i;
4060 int ret;
4061
Peter Zijlstra0be964b2015-05-27 11:09:35 +09304062 module_assert_mutex();
4063
Anders Kaseorg75a66612008-12-05 19:03:58 -05004064 list_for_each_entry(mod, &modules, list) {
Rusty Russell82440622016-02-03 16:55:26 +10304065 /* We hold module_mutex: no need for rcu_dereference_sched */
4066 struct mod_kallsyms *kallsyms = mod->kallsyms;
4067
Rusty Russell0d21b0e2013-01-12 11:38:44 +10304068 if (mod->state == MODULE_STATE_UNFORMED)
4069 continue;
Rusty Russell82440622016-02-03 16:55:26 +10304070 for (i = 0; i < kallsyms->num_symtab; i++) {
4071 ret = fn(data, symname(kallsyms, i),
4072 mod, kallsyms->symtab[i].st_value);
Anders Kaseorg75a66612008-12-05 19:03:58 -05004073 if (ret != 0)
4074 return ret;
4075 }
4076 }
4077 return 0;
4078}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004079#endif /* CONFIG_KALLSYMS */
4080
Petr Mladek7fd83292016-09-21 13:47:22 +02004081/* Maximum number of characters written by module_flags() */
4082#define MODULE_FLAGS_BUF_SIZE (TAINT_FLAGS_COUNT + 4)
4083
4084/* Keep in sync with MODULE_FLAGS_BUF_SIZE !!! */
Arjan van de Ven21aa9282008-01-25 21:08:33 +01004085static char *module_flags(struct module *mod, char *buf)
Florin Malitafa3ba2e82006-10-11 01:21:48 -07004086{
4087 int bx = 0;
4088
Rusty Russell0d21b0e2013-01-12 11:38:44 +10304089 BUG_ON(mod->state == MODULE_STATE_UNFORMED);
Arjan van de Ven21aa9282008-01-25 21:08:33 +01004090 if (mod->taints ||
4091 mod->state == MODULE_STATE_GOING ||
4092 mod->state == MODULE_STATE_COMING) {
Florin Malitafa3ba2e82006-10-11 01:21:48 -07004093 buf[bx++] = '(';
Kay Sieverscca3e702012-01-13 09:32:15 +10304094 bx += module_flags_taint(mod, buf + bx);
Arjan van de Ven21aa9282008-01-25 21:08:33 +01004095 /* Show a - for module-is-being-unloaded */
4096 if (mod->state == MODULE_STATE_GOING)
4097 buf[bx++] = '-';
4098 /* Show a + for module-is-being-loaded */
4099 if (mod->state == MODULE_STATE_COMING)
4100 buf[bx++] = '+';
Florin Malitafa3ba2e82006-10-11 01:21:48 -07004101 buf[bx++] = ')';
4102 }
4103 buf[bx] = '\0';
4104
4105 return buf;
4106}
4107
Alexey Dobriyan3b5d5c62008-10-06 13:19:27 +04004108#ifdef CONFIG_PROC_FS
4109/* Called by the /proc file system to return a list of modules. */
4110static void *m_start(struct seq_file *m, loff_t *pos)
4111{
4112 mutex_lock(&module_mutex);
4113 return seq_list_start(&modules, *pos);
4114}
4115
4116static void *m_next(struct seq_file *m, void *p, loff_t *pos)
4117{
4118 return seq_list_next(p, &modules, pos);
4119}
4120
4121static void m_stop(struct seq_file *m, void *p)
4122{
4123 mutex_unlock(&module_mutex);
4124}
4125
Linus Torvalds1da177e2005-04-16 15:20:36 -07004126static int m_show(struct seq_file *m, void *p)
4127{
4128 struct module *mod = list_entry(p, struct module, list);
Petr Mladek7fd83292016-09-21 13:47:22 +02004129 char buf[MODULE_FLAGS_BUF_SIZE];
Florin Malitafa3ba2e82006-10-11 01:21:48 -07004130
Rusty Russell0d21b0e2013-01-12 11:38:44 +10304131 /* We always ignore unformed modules. */
4132 if (mod->state == MODULE_STATE_UNFORMED)
4133 return 0;
4134
Denys Vlasenko2f0f2a32008-07-22 19:24:27 -05004135 seq_printf(m, "%s %u",
Rusty Russell7523e4d2015-11-26 09:44:08 +10304136 mod->name, mod->init_layout.size + mod->core_layout.size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004137 print_unload_info(m, mod);
4138
4139 /* Informative for users. */
4140 seq_printf(m, " %s",
Ionut Alexa6da0b562014-11-10 09:31:29 +10304141 mod->state == MODULE_STATE_GOING ? "Unloading" :
4142 mod->state == MODULE_STATE_COMING ? "Loading" :
Linus Torvalds1da177e2005-04-16 15:20:36 -07004143 "Live");
4144 /* Used by oprofile and other similar tools. */
Rusty Russell7523e4d2015-11-26 09:44:08 +10304145 seq_printf(m, " 0x%pK", mod->core_layout.base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004146
Florin Malitafa3ba2e82006-10-11 01:21:48 -07004147 /* Taints info */
4148 if (mod->taints)
Arjan van de Ven21aa9282008-01-25 21:08:33 +01004149 seq_printf(m, " %s", module_flags(mod, buf));
Florin Malitafa3ba2e82006-10-11 01:21:48 -07004150
Ionut Alexa6da0b562014-11-10 09:31:29 +10304151 seq_puts(m, "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004152 return 0;
4153}
4154
4155/* Format: modulename size refcount deps address
4156
4157 Where refcount is a number or -, and deps is a comma-separated list
4158 of depends or -.
4159*/
Alexey Dobriyan3b5d5c62008-10-06 13:19:27 +04004160static const struct seq_operations modules_op = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004161 .start = m_start,
4162 .next = m_next,
4163 .stop = m_stop,
4164 .show = m_show
4165};
4166
Alexey Dobriyan3b5d5c62008-10-06 13:19:27 +04004167static int modules_open(struct inode *inode, struct file *file)
4168{
4169 return seq_open(file, &modules_op);
4170}
4171
4172static const struct file_operations proc_modules_operations = {
4173 .open = modules_open,
4174 .read = seq_read,
4175 .llseek = seq_lseek,
4176 .release = seq_release,
4177};
4178
4179static int __init proc_modules_init(void)
4180{
4181 proc_create("modules", 0, NULL, &proc_modules_operations);
4182 return 0;
4183}
4184module_init(proc_modules_init);
4185#endif
4186
Linus Torvalds1da177e2005-04-16 15:20:36 -07004187/* Given an address, look for it in the module exception tables. */
4188const struct exception_table_entry *search_module_extables(unsigned long addr)
4189{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004190 const struct exception_table_entry *e = NULL;
4191 struct module *mod;
4192
Rusty Russell24da1cb2007-07-15 23:41:46 -07004193 preempt_disable();
Peter Zijlstra5ff22642017-02-08 15:48:01 +01004194 mod = __module_address(addr);
4195 if (!mod)
4196 goto out;
Daniel Walker22a8bde2007-10-18 03:06:07 -07004197
Peter Zijlstra5ff22642017-02-08 15:48:01 +01004198 if (!mod->num_exentries)
4199 goto out;
4200
4201 e = search_extable(mod->extable,
4202 mod->extable + mod->num_exentries - 1,
4203 addr);
4204out:
Rusty Russell24da1cb2007-07-15 23:41:46 -07004205 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004206
Peter Zijlstra5ff22642017-02-08 15:48:01 +01004207 /*
4208 * Now, if we found one, we are running inside it now, hence
4209 * we cannot unload the module, hence no refcnt needed.
4210 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004211 return e;
4212}
4213
Ingo Molnar4d435f92006-07-03 00:24:24 -07004214/*
Rusty Russelle6104992009-03-31 13:05:31 -06004215 * is_module_address - is this address inside a module?
4216 * @addr: the address to check.
4217 *
4218 * See is_module_text_address() if you simply want to see if the address
4219 * is code (not data).
Ingo Molnar4d435f92006-07-03 00:24:24 -07004220 */
Rusty Russelle6104992009-03-31 13:05:31 -06004221bool is_module_address(unsigned long addr)
Ingo Molnar4d435f92006-07-03 00:24:24 -07004222{
Rusty Russelle6104992009-03-31 13:05:31 -06004223 bool ret;
Ingo Molnar4d435f92006-07-03 00:24:24 -07004224
Rusty Russell24da1cb2007-07-15 23:41:46 -07004225 preempt_disable();
Rusty Russelle6104992009-03-31 13:05:31 -06004226 ret = __module_address(addr) != NULL;
Rusty Russell24da1cb2007-07-15 23:41:46 -07004227 preempt_enable();
Ingo Molnar4d435f92006-07-03 00:24:24 -07004228
Rusty Russelle6104992009-03-31 13:05:31 -06004229 return ret;
Ingo Molnar4d435f92006-07-03 00:24:24 -07004230}
4231
Rusty Russelle6104992009-03-31 13:05:31 -06004232/*
4233 * __module_address - get the module which contains an address.
4234 * @addr: the address.
4235 *
4236 * Must be called with preempt disabled or module mutex held so that
4237 * module doesn't get freed during this.
4238 */
Linus Torvalds714f83d2009-04-05 11:04:19 -07004239struct module *__module_address(unsigned long addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004240{
4241 struct module *mod;
4242
Rusty Russell3a642e92008-07-22 19:24:28 -05004243 if (addr < module_addr_min || addr > module_addr_max)
4244 return NULL;
4245
Peter Zijlstra0be964b2015-05-27 11:09:35 +09304246 module_assert_mutex_or_preempt();
4247
Peter Zijlstra6c9692e2015-05-27 11:09:37 +09304248 mod = mod_find(addr);
Peter Zijlstra93c2e102015-05-27 11:09:37 +09304249 if (mod) {
4250 BUG_ON(!within_module(addr, mod));
Rusty Russell0d21b0e2013-01-12 11:38:44 +10304251 if (mod->state == MODULE_STATE_UNFORMED)
Peter Zijlstra93c2e102015-05-27 11:09:37 +09304252 mod = NULL;
Rusty Russell0d21b0e2013-01-12 11:38:44 +10304253 }
Peter Zijlstra93c2e102015-05-27 11:09:37 +09304254 return mod;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004255}
Tim Abbottc6b37802008-12-05 19:03:59 -05004256EXPORT_SYMBOL_GPL(__module_address);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004257
Rusty Russelle6104992009-03-31 13:05:31 -06004258/*
4259 * is_module_text_address - is this address inside module code?
4260 * @addr: the address to check.
4261 *
4262 * See is_module_address() if you simply want to see if the address is
4263 * anywhere in a module. See kernel_text_address() for testing if an
4264 * address corresponds to kernel or module code.
4265 */
4266bool is_module_text_address(unsigned long addr)
4267{
4268 bool ret;
4269
4270 preempt_disable();
4271 ret = __module_text_address(addr) != NULL;
4272 preempt_enable();
4273
4274 return ret;
4275}
4276
4277/*
4278 * __module_text_address - get the module whose code contains an address.
4279 * @addr: the address.
4280 *
4281 * Must be called with preempt disabled or module mutex held so that
4282 * module doesn't get freed during this.
4283 */
4284struct module *__module_text_address(unsigned long addr)
4285{
4286 struct module *mod = __module_address(addr);
4287 if (mod) {
4288 /* Make sure it's within the text section. */
Rusty Russell7523e4d2015-11-26 09:44:08 +10304289 if (!within(addr, mod->init_layout.base, mod->init_layout.text_size)
4290 && !within(addr, mod->core_layout.base, mod->core_layout.text_size))
Rusty Russelle6104992009-03-31 13:05:31 -06004291 mod = NULL;
4292 }
4293 return mod;
4294}
Tim Abbottc6b37802008-12-05 19:03:59 -05004295EXPORT_SYMBOL_GPL(__module_text_address);
Rusty Russelle6104992009-03-31 13:05:31 -06004296
Linus Torvalds1da177e2005-04-16 15:20:36 -07004297/* Don't grab lock, we're oopsing. */
4298void print_modules(void)
4299{
4300 struct module *mod;
Petr Mladek7fd83292016-09-21 13:47:22 +02004301 char buf[MODULE_FLAGS_BUF_SIZE];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004302
Linus Torvaldsb2311252009-06-16 11:07:14 -07004303 printk(KERN_DEFAULT "Modules linked in:");
Andi Kleend72b3752008-08-30 10:09:00 +02004304 /* Most callers should already have preempt disabled, but make sure */
4305 preempt_disable();
Rusty Russell0d21b0e2013-01-12 11:38:44 +10304306 list_for_each_entry_rcu(mod, &modules, list) {
4307 if (mod->state == MODULE_STATE_UNFORMED)
4308 continue;
Jiri Slaby27bba4d2014-02-03 11:13:13 +10304309 pr_cont(" %s%s", mod->name, module_flags(mod, buf));
Rusty Russell0d21b0e2013-01-12 11:38:44 +10304310 }
Andi Kleend72b3752008-08-30 10:09:00 +02004311 preempt_enable();
Arjan van de Vene14af7e2008-01-25 21:08:33 +01004312 if (last_unloaded_module[0])
Jiri Slaby27bba4d2014-02-03 11:13:13 +10304313 pr_cont(" [last unloaded: %s]", last_unloaded_module);
4314 pr_cont("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004315}
4316
Linus Torvalds1da177e2005-04-16 15:20:36 -07004317#ifdef CONFIG_MODVERSIONS
Rusty Russell8c8ef422009-03-31 13:05:34 -06004318/* Generate the signature for all relevant module structures here.
4319 * If these change, we don't want to try to parse the module. */
4320void module_layout(struct module *mod,
4321 struct modversion_info *ver,
4322 struct kernel_param *kp,
4323 struct kernel_symbol *ks,
Mathieu Desnoyers65498642011-01-26 17:26:22 -05004324 struct tracepoint * const *tp)
Rusty Russell8c8ef422009-03-31 13:05:34 -06004325{
4326}
4327EXPORT_SYMBOL(module_layout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004328#endif