blob: 5ac22efc3685a6e2cce77e21a1d13e73bd576010 [file] [log] [blame]
Thomas Gleixner1a59d1b82019-05-27 08:55:05 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Arjan van de Venf71d20e2006-06-28 04:26:45 -07002/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 Copyright (C) 2002 Richard Henderson
Rusty Russell51f3d0f2010-08-05 12:59:13 -06004 Copyright (C) 2001 Rusty Russell, 2002, 2010 Rusty Russell IBM.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005
Linus Torvalds1da177e2005-04-16 15:20:36 -07006*/
Paul Gortmaker9984de12011-05-23 14:51:41 -04007#include <linux/export.h>
Paul Gortmaker8a293be2016-07-23 14:01:45 -04008#include <linux/extable.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07009#include <linux/moduleloader.h>
Thiago Jung Bauermannc8424e72019-07-04 15:57:34 -030010#include <linux/module_signature.h>
Steven Rostedt (Red Hat)af658dc2015-04-29 14:36:05 -040011#include <linux/trace_events.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#include <linux/init.h>
Alexey Dobriyanae84e322007-05-08 00:28:38 -070013#include <linux/kallsyms.h>
Kees Cook34e11692012-10-16 07:31:07 +103014#include <linux/file.h>
Alexey Dobriyan3b5d5c62008-10-06 13:19:27 +040015#include <linux/fs.h>
Roland McGrath6d760132007-10-16 23:26:40 -070016#include <linux/sysfs.h>
Randy Dunlap9f158332005-09-13 01:25:16 -070017#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include <linux/slab.h>
19#include <linux/vmalloc.h>
20#include <linux/elf.h>
Alexey Dobriyan3b5d5c62008-10-06 13:19:27 +040021#include <linux/proc_fs.h>
Kees Cook2e72d512012-10-16 07:32:07 +103022#include <linux/security.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <linux/seq_file.h>
24#include <linux/syscalls.h>
25#include <linux/fcntl.h>
26#include <linux/rcupdate.h>
Randy.Dunlapc59ede72006-01-11 12:17:46 -080027#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <linux/cpu.h>
29#include <linux/moduleparam.h>
30#include <linux/errno.h>
31#include <linux/err.h>
32#include <linux/vermagic.h>
33#include <linux/notifier.h>
Al Virof6a57032006-10-18 01:47:25 -040034#include <linux/sched.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include <linux/device.h>
Matt Domschc988d2b2005-06-23 22:05:15 -070036#include <linux/string.h>
Arjan van de Ven97d1f152006-03-23 03:00:24 -080037#include <linux/mutex.h>
Andi Kleend72b3752008-08-30 10:09:00 +020038#include <linux/rculist.h>
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080039#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070040#include <asm/cacheflush.h>
Michael Ellerman563ec5c2017-07-06 15:35:58 -070041#include <linux/set_memory.h>
Bernd Schmidteb8cdec2009-09-21 17:03:57 -070042#include <asm/mmu_context.h>
Sam Ravnborgb817f6f2006-06-09 21:53:55 +020043#include <linux/license.h>
Christoph Lameter6d762392008-02-08 04:18:42 -080044#include <asm/sections.h>
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -040045#include <linux/tracepoint.h>
Steven Rostedt90d595f2008-08-14 15:45:09 -040046#include <linux/ftrace.h>
Jessica Yu7e545d62016-03-16 20:55:39 -040047#include <linux/livepatch.h>
Arjan van de Ven22a9d642009-01-07 08:45:46 -080048#include <linux/async.h>
Tejun Heofbf59bc2009-02-20 16:29:08 +090049#include <linux/percpu.h>
Catalin Marinas4f2294b2009-06-11 13:23:20 +010050#include <linux/kmemleak.h>
Jason Baronbf5438fc2010-09-17 11:09:00 -040051#include <linux/jump_label.h>
matthieu castet84e1c6b2010-11-16 22:35:16 +010052#include <linux/pfn.h>
Alessio Igor Bogani403ed272011-04-20 11:10:52 +020053#include <linux/bsearch.h>
Luis de Bethencourt9d5059c2016-08-02 14:03:47 -070054#include <linux/dynamic_debug.h>
Richard Guy Briggsca86cad2017-02-04 13:10:38 -050055#include <linux/audit.h>
Rusty Russell2f3238a2012-10-22 18:09:41 +103056#include <uapi/linux/module.h>
Rusty Russell106a4ee2012-09-26 10:09:40 +010057#include "module-internal.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
Li Zefan7ead8b82009-08-17 16:56:28 +080059#define CREATE_TRACE_POINTS
60#include <trace/events/module.h>
61
Linus Torvalds1da177e2005-04-16 15:20:36 -070062#ifndef ARCH_SHF_SMALL
63#define ARCH_SHF_SMALL 0
64#endif
65
matthieu castet84e1c6b2010-11-16 22:35:16 +010066/*
67 * Modules' sections will be aligned on page boundaries
68 * to ensure complete separation of code and data, but
Laura Abbott0f5bf6d2017-02-06 16:31:58 -080069 * only when CONFIG_STRICT_MODULE_RWX=y
matthieu castet84e1c6b2010-11-16 22:35:16 +010070 */
Laura Abbott0f5bf6d2017-02-06 16:31:58 -080071#ifdef CONFIG_STRICT_MODULE_RWX
matthieu castet84e1c6b2010-11-16 22:35:16 +010072# define debug_align(X) ALIGN(X, PAGE_SIZE)
73#else
74# define debug_align(X) (X)
75#endif
76
Linus Torvalds1da177e2005-04-16 15:20:36 -070077/* If this is set, the section belongs in the init part of the module */
78#define INIT_OFFSET_MASK (1UL << (BITS_PER_LONG-1))
79
Rusty Russell75676502010-06-05 11:17:36 -060080/*
81 * Mutex protects:
82 * 1) List of modules (also safely readable with preempt_disable),
83 * 2) module_use links,
84 * 3) module_addr_min/module_addr_max.
Masami Hiramatsue513cc12014-11-10 09:30:29 +103085 * (delete and add uses RCU list operations). */
Tim Abbottc6b37802008-12-05 19:03:59 -050086DEFINE_MUTEX(module_mutex);
87EXPORT_SYMBOL_GPL(module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070088static LIST_HEAD(modules);
Peter Zijlstra93c2e102015-05-27 11:09:37 +093089
Rick Edgecombe1a7b7d92019-04-25 17:11:37 -070090/* Work queue for freeing init sections in success case */
91static struct work_struct init_free_wq;
92static struct llist_head init_free_list;
93
Peter Zijlstra6c9692e2015-05-27 11:09:37 +093094#ifdef CONFIG_MODULES_TREE_LOOKUP
95
Peter Zijlstra93c2e102015-05-27 11:09:37 +093096/*
97 * Use a latched RB-tree for __module_address(); this allows us to use
98 * RCU-sched lookups of the address from any context.
99 *
Peter Zijlstra6c9692e2015-05-27 11:09:37 +0930100 * This is conditional on PERF_EVENTS || TRACING because those can really hit
101 * __module_address() hard by doing a lot of stack unwinding; potentially from
102 * NMI context.
Peter Zijlstra93c2e102015-05-27 11:09:37 +0930103 */
104
105static __always_inline unsigned long __mod_tree_val(struct latch_tree_node *n)
106{
Rusty Russell7523e4d2015-11-26 09:44:08 +1030107 struct module_layout *layout = container_of(n, struct module_layout, mtn.node);
Peter Zijlstra93c2e102015-05-27 11:09:37 +0930108
Rusty Russell7523e4d2015-11-26 09:44:08 +1030109 return (unsigned long)layout->base;
Peter Zijlstra93c2e102015-05-27 11:09:37 +0930110}
111
112static __always_inline unsigned long __mod_tree_size(struct latch_tree_node *n)
113{
Rusty Russell7523e4d2015-11-26 09:44:08 +1030114 struct module_layout *layout = container_of(n, struct module_layout, mtn.node);
Peter Zijlstra93c2e102015-05-27 11:09:37 +0930115
Rusty Russell7523e4d2015-11-26 09:44:08 +1030116 return (unsigned long)layout->size;
Peter Zijlstra93c2e102015-05-27 11:09:37 +0930117}
118
119static __always_inline bool
120mod_tree_less(struct latch_tree_node *a, struct latch_tree_node *b)
121{
122 return __mod_tree_val(a) < __mod_tree_val(b);
123}
124
125static __always_inline int
126mod_tree_comp(void *key, struct latch_tree_node *n)
127{
128 unsigned long val = (unsigned long)key;
129 unsigned long start, end;
130
131 start = __mod_tree_val(n);
132 if (val < start)
133 return -1;
134
135 end = start + __mod_tree_size(n);
136 if (val >= end)
137 return 1;
138
139 return 0;
140}
141
142static const struct latch_tree_ops mod_tree_ops = {
143 .less = mod_tree_less,
144 .comp = mod_tree_comp,
145};
146
Peter Zijlstra4f6665462015-05-27 11:09:38 +0930147static struct mod_tree_root {
148 struct latch_tree_root root;
149 unsigned long addr_min;
150 unsigned long addr_max;
151} mod_tree __cacheline_aligned = {
152 .addr_min = -1UL,
153};
154
155#define module_addr_min mod_tree.addr_min
156#define module_addr_max mod_tree.addr_max
157
158static noinline void __mod_tree_insert(struct mod_tree_node *node)
159{
160 latch_tree_insert(&node->node, &mod_tree.root, &mod_tree_ops);
161}
162
163static void __mod_tree_remove(struct mod_tree_node *node)
164{
165 latch_tree_erase(&node->node, &mod_tree.root, &mod_tree_ops);
166}
Peter Zijlstra93c2e102015-05-27 11:09:37 +0930167
168/*
169 * These modifications: insert, remove_init and remove; are serialized by the
170 * module_mutex.
171 */
172static void mod_tree_insert(struct module *mod)
173{
Rusty Russell7523e4d2015-11-26 09:44:08 +1030174 mod->core_layout.mtn.mod = mod;
175 mod->init_layout.mtn.mod = mod;
Peter Zijlstra93c2e102015-05-27 11:09:37 +0930176
Rusty Russell7523e4d2015-11-26 09:44:08 +1030177 __mod_tree_insert(&mod->core_layout.mtn);
178 if (mod->init_layout.size)
179 __mod_tree_insert(&mod->init_layout.mtn);
Peter Zijlstra93c2e102015-05-27 11:09:37 +0930180}
181
182static void mod_tree_remove_init(struct module *mod)
183{
Rusty Russell7523e4d2015-11-26 09:44:08 +1030184 if (mod->init_layout.size)
185 __mod_tree_remove(&mod->init_layout.mtn);
Peter Zijlstra93c2e102015-05-27 11:09:37 +0930186}
187
188static void mod_tree_remove(struct module *mod)
189{
Rusty Russell7523e4d2015-11-26 09:44:08 +1030190 __mod_tree_remove(&mod->core_layout.mtn);
Peter Zijlstra93c2e102015-05-27 11:09:37 +0930191 mod_tree_remove_init(mod);
192}
193
Peter Zijlstra6c9692e2015-05-27 11:09:37 +0930194static struct module *mod_find(unsigned long addr)
Peter Zijlstra93c2e102015-05-27 11:09:37 +0930195{
196 struct latch_tree_node *ltn;
197
Peter Zijlstra4f6665462015-05-27 11:09:38 +0930198 ltn = latch_tree_find((void *)addr, &mod_tree.root, &mod_tree_ops);
Peter Zijlstra93c2e102015-05-27 11:09:37 +0930199 if (!ltn)
200 return NULL;
201
202 return container_of(ltn, struct mod_tree_node, node)->mod;
203}
204
Peter Zijlstra6c9692e2015-05-27 11:09:37 +0930205#else /* MODULES_TREE_LOOKUP */
206
Peter Zijlstra4f6665462015-05-27 11:09:38 +0930207static unsigned long module_addr_min = -1UL, module_addr_max = 0;
208
Peter Zijlstra6c9692e2015-05-27 11:09:37 +0930209static void mod_tree_insert(struct module *mod) { }
210static void mod_tree_remove_init(struct module *mod) { }
211static void mod_tree_remove(struct module *mod) { }
212
213static struct module *mod_find(unsigned long addr)
214{
215 struct module *mod;
216
217 list_for_each_entry_rcu(mod, &modules, list) {
218 if (within_module(addr, mod))
219 return mod;
220 }
221
222 return NULL;
223}
224
225#endif /* MODULES_TREE_LOOKUP */
226
Peter Zijlstra4f6665462015-05-27 11:09:38 +0930227/*
228 * Bounds of module text, for speeding up __module_address.
229 * Protected by module_mutex.
230 */
231static void __mod_update_bounds(void *base, unsigned int size)
232{
233 unsigned long min = (unsigned long)base;
234 unsigned long max = min + size;
235
236 if (min < module_addr_min)
237 module_addr_min = min;
238 if (max > module_addr_max)
239 module_addr_max = max;
240}
241
242static void mod_update_bounds(struct module *mod)
243{
Rusty Russell7523e4d2015-11-26 09:44:08 +1030244 __mod_update_bounds(mod->core_layout.base, mod->core_layout.size);
245 if (mod->init_layout.size)
246 __mod_update_bounds(mod->init_layout.base, mod->init_layout.size);
Peter Zijlstra4f6665462015-05-27 11:09:38 +0930247}
248
Jason Wessel67fc4e02010-05-20 21:04:21 -0500249#ifdef CONFIG_KGDB_KDB
250struct list_head *kdb_modules = &modules; /* kdb needs the list of modules */
251#endif /* CONFIG_KGDB_KDB */
252
Peter Zijlstra0be964b2015-05-27 11:09:35 +0930253static void module_assert_mutex(void)
Rusty Russell106a4ee2012-09-26 10:09:40 +0100254{
Peter Zijlstra0be964b2015-05-27 11:09:35 +0930255 lockdep_assert_held(&module_mutex);
Rusty Russell106a4ee2012-09-26 10:09:40 +0100256}
257
Peter Zijlstra0be964b2015-05-27 11:09:35 +0930258static void module_assert_mutex_or_preempt(void)
259{
260#ifdef CONFIG_LOCKDEP
261 if (unlikely(!debug_locks))
262 return;
Rusty Russell106a4ee2012-09-26 10:09:40 +0100263
Steven Rostedt95025142016-07-19 05:59:24 +0930264 WARN_ON_ONCE(!rcu_read_lock_sched_held() &&
Peter Zijlstra0be964b2015-05-27 11:09:35 +0930265 !lockdep_is_held(&module_mutex));
266#endif
267}
268
Luis R. Rodriguez6727bb92015-05-27 11:09:39 +0930269static bool sig_enforce = IS_ENABLED(CONFIG_MODULE_SIG_FORCE);
Rusty Russell106a4ee2012-09-26 10:09:40 +0100270module_param(sig_enforce, bool_enable_only, 0644);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271
Bruno E. O. Meneguelefda784e2017-10-24 15:37:00 -0200272/*
273 * Export sig_enforce kernel cmdline parameter to allow other subsystems rely
274 * on that instead of directly to CONFIG_MODULE_SIG_FORCE config.
275 */
276bool is_module_sig_enforced(void)
277{
278 return sig_enforce;
279}
280EXPORT_SYMBOL(is_module_sig_enforced);
281
Mimi Zohar8db5da02019-01-27 19:03:45 -0500282void set_module_sig_enforced(void)
283{
284 sig_enforce = true;
285}
286
Stephen Rothwell19e45292009-04-14 17:27:18 +1000287/* Block module loading/unloading? */
288int modules_disabled = 0;
Dave Young02608be2012-02-01 10:33:14 +0800289core_param(nomodule, modules_disabled, bint, 0);
Stephen Rothwell19e45292009-04-14 17:27:18 +1000290
Rusty Russellc9a3ba52008-01-29 17:13:18 -0500291/* Waiting for a module to finish initializing? */
292static DECLARE_WAIT_QUEUE_HEAD(module_wq);
293
Alan Sterne041c682006-03-27 01:16:30 -0800294static BLOCKING_NOTIFIER_HEAD(module_notify_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295
Ionut Alexa6da0b562014-11-10 09:31:29 +1030296int register_module_notifier(struct notifier_block *nb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297{
Alan Sterne041c682006-03-27 01:16:30 -0800298 return blocking_notifier_chain_register(&module_notify_list, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299}
300EXPORT_SYMBOL(register_module_notifier);
301
Ionut Alexa6da0b562014-11-10 09:31:29 +1030302int unregister_module_notifier(struct notifier_block *nb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303{
Alan Sterne041c682006-03-27 01:16:30 -0800304 return blocking_notifier_chain_unregister(&module_notify_list, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305}
306EXPORT_SYMBOL(unregister_module_notifier);
307
Miroslav Benes71d9f502016-11-16 16:45:48 +0100308/*
309 * We require a truly strong try_module_get(): 0 means success.
310 * Otherwise an error is returned due to ongoing or failed
311 * initialization etc.
312 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313static inline int strong_try_module_get(struct module *mod)
314{
Rusty Russell0d21b0e2013-01-12 11:38:44 +1030315 BUG_ON(mod && mod->state == MODULE_STATE_UNFORMED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 if (mod && mod->state == MODULE_STATE_COMING)
Rusty Russellc9a3ba52008-01-29 17:13:18 -0500317 return -EBUSY;
318 if (try_module_get(mod))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 return 0;
Rusty Russellc9a3ba52008-01-29 17:13:18 -0500320 else
321 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322}
323
Rusty Russell373d4d02013-01-21 17:17:39 +1030324static inline void add_taint_module(struct module *mod, unsigned flag,
325 enum lockdep_ok lockdep_ok)
Florin Malitafa3ba2e82006-10-11 01:21:48 -0700326{
Rusty Russell373d4d02013-01-21 17:17:39 +1030327 add_taint(flag, lockdep_ok);
Petr Mladek7fd83292016-09-21 13:47:22 +0200328 set_bit(flag, &mod->taints);
Florin Malitafa3ba2e82006-10-11 01:21:48 -0700329}
330
Robert P. J. Day02a3e592007-05-09 07:26:28 +0200331/*
332 * A thread that wants to hold a reference to a module only while it
333 * is running can call this to safely exit. nfsd and lockd use this.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 */
Jiri Kosinabf262dc2016-04-12 05:02:09 +0930335void __noreturn __module_put_and_exit(struct module *mod, long code)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336{
337 module_put(mod);
338 do_exit(code);
339}
340EXPORT_SYMBOL(__module_put_and_exit);
Daniel Walker22a8bde2007-10-18 03:06:07 -0700341
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342/* Find a module section: 0 means not found. */
Rusty Russell49668682010-08-05 12:59:10 -0600343static unsigned int find_sec(const struct load_info *info, const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344{
345 unsigned int i;
346
Rusty Russell49668682010-08-05 12:59:10 -0600347 for (i = 1; i < info->hdr->e_shnum; i++) {
348 Elf_Shdr *shdr = &info->sechdrs[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 /* Alloc bit cleared means "ignore it." */
Rusty Russell49668682010-08-05 12:59:10 -0600350 if ((shdr->sh_flags & SHF_ALLOC)
351 && strcmp(info->secstrings + shdr->sh_name, name) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 return i;
Rusty Russell49668682010-08-05 12:59:10 -0600353 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 return 0;
355}
356
Rusty Russell5e458cc2008-10-22 10:00:13 -0500357/* Find a module section, or NULL. */
Rusty Russell49668682010-08-05 12:59:10 -0600358static void *section_addr(const struct load_info *info, const char *name)
Rusty Russell5e458cc2008-10-22 10:00:13 -0500359{
360 /* Section 0 has sh_addr 0. */
Rusty Russell49668682010-08-05 12:59:10 -0600361 return (void *)info->sechdrs[find_sec(info, name)].sh_addr;
Rusty Russell5e458cc2008-10-22 10:00:13 -0500362}
363
364/* Find a module section, or NULL. Fill in number of "objects" in section. */
Rusty Russell49668682010-08-05 12:59:10 -0600365static void *section_objs(const struct load_info *info,
Rusty Russell5e458cc2008-10-22 10:00:13 -0500366 const char *name,
367 size_t object_size,
368 unsigned int *num)
369{
Rusty Russell49668682010-08-05 12:59:10 -0600370 unsigned int sec = find_sec(info, name);
Rusty Russell5e458cc2008-10-22 10:00:13 -0500371
372 /* Section 0 has sh_addr 0 and sh_size 0. */
Rusty Russell49668682010-08-05 12:59:10 -0600373 *num = info->sechdrs[sec].sh_size / object_size;
374 return (void *)info->sechdrs[sec].sh_addr;
Rusty Russell5e458cc2008-10-22 10:00:13 -0500375}
376
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377/* Provided by the linker */
378extern const struct kernel_symbol __start___ksymtab[];
379extern const struct kernel_symbol __stop___ksymtab[];
380extern const struct kernel_symbol __start___ksymtab_gpl[];
381extern const struct kernel_symbol __stop___ksymtab_gpl[];
Greg Kroah-Hartman9f28bb72006-03-20 13:17:13 -0800382extern const struct kernel_symbol __start___ksymtab_gpl_future[];
383extern const struct kernel_symbol __stop___ksymtab_gpl_future[];
Ard Biesheuvel71810db2017-02-03 09:54:06 +0000384extern const s32 __start___kcrctab[];
385extern const s32 __start___kcrctab_gpl[];
386extern const s32 __start___kcrctab_gpl_future[];
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500387#ifdef CONFIG_UNUSED_SYMBOLS
388extern const struct kernel_symbol __start___ksymtab_unused[];
389extern const struct kernel_symbol __stop___ksymtab_unused[];
390extern const struct kernel_symbol __start___ksymtab_unused_gpl[];
391extern const struct kernel_symbol __stop___ksymtab_unused_gpl[];
Ard Biesheuvel71810db2017-02-03 09:54:06 +0000392extern const s32 __start___kcrctab_unused[];
393extern const s32 __start___kcrctab_unused_gpl[];
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500394#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395
396#ifndef CONFIG_MODVERSIONS
397#define symversion(base, idx) NULL
398#else
Andrew Mortonf83ca9f2006-03-28 01:56:20 -0800399#define symversion(base, idx) ((base != NULL) ? ((base) + (idx)) : NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400#endif
401
Rusty Russelldafd0942008-07-22 19:24:25 -0500402static bool each_symbol_in_section(const struct symsearch *arr,
403 unsigned int arrsize,
404 struct module *owner,
405 bool (*fn)(const struct symsearch *syms,
406 struct module *owner,
Rusty Russellde4d8d52011-04-19 21:49:58 +0200407 void *data),
Rusty Russelldafd0942008-07-22 19:24:25 -0500408 void *data)
Sam Ravnborg3fd68052006-02-08 21:16:45 +0100409{
Rusty Russellde4d8d52011-04-19 21:49:58 +0200410 unsigned int j;
Rusty Russelldafd0942008-07-22 19:24:25 -0500411
412 for (j = 0; j < arrsize; j++) {
Rusty Russellde4d8d52011-04-19 21:49:58 +0200413 if (fn(&arr[j], owner, data))
414 return true;
Rusty Russelldafd0942008-07-22 19:24:25 -0500415 }
416
417 return false;
Sam Ravnborg3fd68052006-02-08 21:16:45 +0100418}
419
Rusty Russelldafd0942008-07-22 19:24:25 -0500420/* Returns true as soon as fn returns true, otherwise false. */
Rusty Russellde4d8d52011-04-19 21:49:58 +0200421bool each_symbol_section(bool (*fn)(const struct symsearch *arr,
422 struct module *owner,
423 void *data),
424 void *data)
Arjan van de Venf71d20e2006-06-28 04:26:45 -0700425{
Rusty Russelldafd0942008-07-22 19:24:25 -0500426 struct module *mod;
Linus Torvalds44032e62010-08-05 12:59:05 -0600427 static const struct symsearch arr[] = {
Rusty Russelldafd0942008-07-22 19:24:25 -0500428 { __start___ksymtab, __stop___ksymtab, __start___kcrctab,
429 NOT_GPL_ONLY, false },
430 { __start___ksymtab_gpl, __stop___ksymtab_gpl,
431 __start___kcrctab_gpl,
432 GPL_ONLY, false },
433 { __start___ksymtab_gpl_future, __stop___ksymtab_gpl_future,
434 __start___kcrctab_gpl_future,
435 WILL_BE_GPL_ONLY, false },
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500436#ifdef CONFIG_UNUSED_SYMBOLS
Rusty Russelldafd0942008-07-22 19:24:25 -0500437 { __start___ksymtab_unused, __stop___ksymtab_unused,
438 __start___kcrctab_unused,
439 NOT_GPL_ONLY, true },
440 { __start___ksymtab_unused_gpl, __stop___ksymtab_unused_gpl,
441 __start___kcrctab_unused_gpl,
442 GPL_ONLY, true },
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500443#endif
Rusty Russelldafd0942008-07-22 19:24:25 -0500444 };
445
Peter Zijlstra0be964b2015-05-27 11:09:35 +0930446 module_assert_mutex_or_preempt();
447
Rusty Russelldafd0942008-07-22 19:24:25 -0500448 if (each_symbol_in_section(arr, ARRAY_SIZE(arr), NULL, fn, data))
449 return true;
450
Andi Kleend72b3752008-08-30 10:09:00 +0200451 list_for_each_entry_rcu(mod, &modules, list) {
Rusty Russelldafd0942008-07-22 19:24:25 -0500452 struct symsearch arr[] = {
453 { mod->syms, mod->syms + mod->num_syms, mod->crcs,
454 NOT_GPL_ONLY, false },
455 { mod->gpl_syms, mod->gpl_syms + mod->num_gpl_syms,
456 mod->gpl_crcs,
457 GPL_ONLY, false },
458 { mod->gpl_future_syms,
459 mod->gpl_future_syms + mod->num_gpl_future_syms,
460 mod->gpl_future_crcs,
461 WILL_BE_GPL_ONLY, false },
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500462#ifdef CONFIG_UNUSED_SYMBOLS
Rusty Russelldafd0942008-07-22 19:24:25 -0500463 { mod->unused_syms,
464 mod->unused_syms + mod->num_unused_syms,
465 mod->unused_crcs,
466 NOT_GPL_ONLY, true },
467 { mod->unused_gpl_syms,
468 mod->unused_gpl_syms + mod->num_unused_gpl_syms,
469 mod->unused_gpl_crcs,
470 GPL_ONLY, true },
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500471#endif
Rusty Russelldafd0942008-07-22 19:24:25 -0500472 };
473
Rusty Russell0d21b0e2013-01-12 11:38:44 +1030474 if (mod->state == MODULE_STATE_UNFORMED)
475 continue;
476
Rusty Russelldafd0942008-07-22 19:24:25 -0500477 if (each_symbol_in_section(arr, ARRAY_SIZE(arr), mod, fn, data))
478 return true;
479 }
480 return false;
Arjan van de Venf71d20e2006-06-28 04:26:45 -0700481}
Rusty Russellde4d8d52011-04-19 21:49:58 +0200482EXPORT_SYMBOL_GPL(each_symbol_section);
Arjan van de Venf71d20e2006-06-28 04:26:45 -0700483
Rusty Russelldafd0942008-07-22 19:24:25 -0500484struct find_symbol_arg {
485 /* Input */
486 const char *name;
487 bool gplok;
488 bool warn;
489
490 /* Output */
491 struct module *owner;
Ard Biesheuvel71810db2017-02-03 09:54:06 +0000492 const s32 *crc;
Tim Abbott414fd312008-12-05 19:03:56 -0500493 const struct kernel_symbol *sym;
Rusty Russelldafd0942008-07-22 19:24:25 -0500494};
495
Jessica Yu2d25bc52018-11-19 17:43:58 +0100496static bool check_exported_symbol(const struct symsearch *syms,
497 struct module *owner,
498 unsigned int symnum, void *data)
Rusty Russellad9546c2008-05-01 21:14:59 -0500499{
Rusty Russelldafd0942008-07-22 19:24:25 -0500500 struct find_symbol_arg *fsa = data;
501
Rusty Russelldafd0942008-07-22 19:24:25 -0500502 if (!fsa->gplok) {
503 if (syms->licence == GPL_ONLY)
504 return false;
505 if (syms->licence == WILL_BE_GPL_ONLY && fsa->warn) {
Andrew Mortonbddb12b2013-11-12 15:11:28 -0800506 pr_warn("Symbol %s is being used by a non-GPL module, "
507 "which will not be allowed in the future\n",
508 fsa->name);
Rusty Russelldafd0942008-07-22 19:24:25 -0500509 }
510 }
511
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500512#ifdef CONFIG_UNUSED_SYMBOLS
Rusty Russelldafd0942008-07-22 19:24:25 -0500513 if (syms->unused && fsa->warn) {
Andrew Mortonbddb12b2013-11-12 15:11:28 -0800514 pr_warn("Symbol %s is marked as UNUSED, however this module is "
515 "using it.\n", fsa->name);
516 pr_warn("This symbol will go away in the future.\n");
Yannick Guerrini7b63c3a2015-03-24 12:31:40 +1030517 pr_warn("Please evaluate if this is the right api to use and "
518 "if it really is, submit a report to the linux kernel "
519 "mailing list together with submitting your code for "
Andrew Mortonbddb12b2013-11-12 15:11:28 -0800520 "inclusion.\n");
Rusty Russellad9546c2008-05-01 21:14:59 -0500521 }
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500522#endif
Rusty Russelldafd0942008-07-22 19:24:25 -0500523
524 fsa->owner = owner;
525 fsa->crc = symversion(syms->crcs, symnum);
Tim Abbott414fd312008-12-05 19:03:56 -0500526 fsa->sym = &syms->start[symnum];
Rusty Russellad9546c2008-05-01 21:14:59 -0500527 return true;
528}
529
Ard Biesheuvel7290d582018-08-21 21:56:09 -0700530static unsigned long kernel_symbol_value(const struct kernel_symbol *sym)
531{
532#ifdef CONFIG_HAVE_ARCH_PREL32_RELOCATIONS
533 return (unsigned long)offset_to_ptr(&sym->value_offset);
534#else
535 return sym->value;
536#endif
537}
538
539static const char *kernel_symbol_name(const struct kernel_symbol *sym)
540{
541#ifdef CONFIG_HAVE_ARCH_PREL32_RELOCATIONS
542 return offset_to_ptr(&sym->name_offset);
543#else
544 return sym->name;
545#endif
546}
547
Alessio Igor Bogani403ed272011-04-20 11:10:52 +0200548static int cmp_name(const void *va, const void *vb)
549{
550 const char *a;
551 const struct kernel_symbol *b;
552 a = va; b = vb;
Ard Biesheuvel7290d582018-08-21 21:56:09 -0700553 return strcmp(a, kernel_symbol_name(b));
Alessio Igor Bogani403ed272011-04-20 11:10:52 +0200554}
555
Jessica Yu2d25bc52018-11-19 17:43:58 +0100556static bool find_exported_symbol_in_section(const struct symsearch *syms,
557 struct module *owner,
558 void *data)
Rusty Russellde4d8d52011-04-19 21:49:58 +0200559{
560 struct find_symbol_arg *fsa = data;
Alessio Igor Bogani403ed272011-04-20 11:10:52 +0200561 struct kernel_symbol *sym;
Rusty Russellde4d8d52011-04-19 21:49:58 +0200562
Alessio Igor Bogani403ed272011-04-20 11:10:52 +0200563 sym = bsearch(fsa->name, syms->start, syms->stop - syms->start,
564 sizeof(struct kernel_symbol), cmp_name);
565
Jessica Yu2d25bc52018-11-19 17:43:58 +0100566 if (sym != NULL && check_exported_symbol(syms, owner,
567 sym - syms->start, data))
Alessio Igor Bogani403ed272011-04-20 11:10:52 +0200568 return true;
569
Rusty Russellde4d8d52011-04-19 21:49:58 +0200570 return false;
571}
572
Jessica Yu2d25bc52018-11-19 17:43:58 +0100573/* Find an exported symbol and return it, along with, (optional) crc and
Rusty Russell75676502010-06-05 11:17:36 -0600574 * (optional) module which owns it. Needs preempt disabled or module_mutex. */
Tim Abbottc6b37802008-12-05 19:03:59 -0500575const struct kernel_symbol *find_symbol(const char *name,
576 struct module **owner,
Ard Biesheuvel71810db2017-02-03 09:54:06 +0000577 const s32 **crc,
Tim Abbottc6b37802008-12-05 19:03:59 -0500578 bool gplok,
579 bool warn)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580{
Rusty Russelldafd0942008-07-22 19:24:25 -0500581 struct find_symbol_arg fsa;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582
Rusty Russelldafd0942008-07-22 19:24:25 -0500583 fsa.name = name;
584 fsa.gplok = gplok;
585 fsa.warn = warn;
586
Jessica Yu2d25bc52018-11-19 17:43:58 +0100587 if (each_symbol_section(find_exported_symbol_in_section, &fsa)) {
Rusty Russellad9546c2008-05-01 21:14:59 -0500588 if (owner)
Rusty Russelldafd0942008-07-22 19:24:25 -0500589 *owner = fsa.owner;
590 if (crc)
591 *crc = fsa.crc;
Tim Abbott414fd312008-12-05 19:03:56 -0500592 return fsa.sym;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 }
Rusty Russellad9546c2008-05-01 21:14:59 -0500594
Jim Cromie5e124162011-12-06 12:11:31 -0700595 pr_debug("Failed to find symbol %s\n", name);
Tim Abbott414fd312008-12-05 19:03:56 -0500596 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597}
Tim Abbottc6b37802008-12-05 19:03:59 -0500598EXPORT_SYMBOL_GPL(find_symbol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599
Rusty Russellfe0d34d2015-07-29 05:52:14 +0930600/*
601 * Search for module by name: must hold module_mutex (or preempt disabled
602 * for read-only access).
603 */
Mathias Krause4f6de4d52013-07-02 15:35:11 +0930604static struct module *find_module_all(const char *name, size_t len,
Rusty Russell0d21b0e2013-01-12 11:38:44 +1030605 bool even_unformed)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606{
607 struct module *mod;
608
Rusty Russellfe0d34d2015-07-29 05:52:14 +0930609 module_assert_mutex_or_preempt();
Peter Zijlstra0be964b2015-05-27 11:09:35 +0930610
Luis R. Rodriguez93437352017-05-26 14:12:25 -0700611 list_for_each_entry_rcu(mod, &modules, list) {
Rusty Russell0d21b0e2013-01-12 11:38:44 +1030612 if (!even_unformed && mod->state == MODULE_STATE_UNFORMED)
613 continue;
Mathias Krause4f6de4d52013-07-02 15:35:11 +0930614 if (strlen(mod->name) == len && !memcmp(mod->name, name, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 return mod;
616 }
617 return NULL;
618}
Rusty Russell0d21b0e2013-01-12 11:38:44 +1030619
620struct module *find_module(const char *name)
621{
Rusty Russellfe0d34d2015-07-29 05:52:14 +0930622 module_assert_mutex();
Mathias Krause4f6de4d52013-07-02 15:35:11 +0930623 return find_module_all(name, strlen(name), false);
Rusty Russell0d21b0e2013-01-12 11:38:44 +1030624}
Tim Abbottc6b37802008-12-05 19:03:59 -0500625EXPORT_SYMBOL_GPL(find_module);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626
627#ifdef CONFIG_SMP
Tejun Heofbf59bc2009-02-20 16:29:08 +0900628
Tejun Heo259354d2010-03-10 18:56:10 +0900629static inline void __percpu *mod_percpu(struct module *mod)
Tejun Heofbf59bc2009-02-20 16:29:08 +0900630{
Tejun Heo259354d2010-03-10 18:56:10 +0900631 return mod->percpu;
632}
Tejun Heofbf59bc2009-02-20 16:29:08 +0900633
Rusty Russell9eb76d72013-07-03 10:06:29 +0930634static int percpu_modalloc(struct module *mod, struct load_info *info)
Tejun Heo259354d2010-03-10 18:56:10 +0900635{
Rusty Russell9eb76d72013-07-03 10:06:29 +0930636 Elf_Shdr *pcpusec = &info->sechdrs[info->index.pcpu];
637 unsigned long align = pcpusec->sh_addralign;
638
639 if (!pcpusec->sh_size)
640 return 0;
641
Tejun Heofbf59bc2009-02-20 16:29:08 +0900642 if (align > PAGE_SIZE) {
Andrew Mortonbddb12b2013-11-12 15:11:28 -0800643 pr_warn("%s: per-cpu alignment %li > %li\n",
644 mod->name, align, PAGE_SIZE);
Tejun Heofbf59bc2009-02-20 16:29:08 +0900645 align = PAGE_SIZE;
646 }
647
Rusty Russell9eb76d72013-07-03 10:06:29 +0930648 mod->percpu = __alloc_reserved_percpu(pcpusec->sh_size, align);
Tejun Heo259354d2010-03-10 18:56:10 +0900649 if (!mod->percpu) {
Andrew Mortonbddb12b2013-11-12 15:11:28 -0800650 pr_warn("%s: Could not allocate %lu bytes percpu data\n",
651 mod->name, (unsigned long)pcpusec->sh_size);
Tejun Heo259354d2010-03-10 18:56:10 +0900652 return -ENOMEM;
653 }
Rusty Russell9eb76d72013-07-03 10:06:29 +0930654 mod->percpu_size = pcpusec->sh_size;
Tejun Heo259354d2010-03-10 18:56:10 +0900655 return 0;
Tejun Heofbf59bc2009-02-20 16:29:08 +0900656}
657
Tejun Heo259354d2010-03-10 18:56:10 +0900658static void percpu_modfree(struct module *mod)
Tejun Heofbf59bc2009-02-20 16:29:08 +0900659{
Tejun Heo259354d2010-03-10 18:56:10 +0900660 free_percpu(mod->percpu);
Tejun Heofbf59bc2009-02-20 16:29:08 +0900661}
662
Rusty Russell49668682010-08-05 12:59:10 -0600663static unsigned int find_pcpusec(struct load_info *info)
Tejun Heo6b588c12009-02-20 16:29:07 +0900664{
Rusty Russell49668682010-08-05 12:59:10 -0600665 return find_sec(info, ".data..percpu");
Tejun Heo6b588c12009-02-20 16:29:07 +0900666}
667
Tejun Heo259354d2010-03-10 18:56:10 +0900668static void percpu_modcopy(struct module *mod,
669 const void *from, unsigned long size)
Tejun Heo6b588c12009-02-20 16:29:07 +0900670{
671 int cpu;
672
673 for_each_possible_cpu(cpu)
Tejun Heo259354d2010-03-10 18:56:10 +0900674 memcpy(per_cpu_ptr(mod->percpu, cpu), from, size);
Tejun Heo6b588c12009-02-20 16:29:07 +0900675}
676
Thomas Gleixner383776f2017-02-27 15:37:36 +0100677bool __is_module_percpu_address(unsigned long addr, unsigned long *can_addr)
Tejun Heo10fad5e2010-03-10 18:57:54 +0900678{
679 struct module *mod;
680 unsigned int cpu;
681
682 preempt_disable();
683
684 list_for_each_entry_rcu(mod, &modules, list) {
Rusty Russell0d21b0e2013-01-12 11:38:44 +1030685 if (mod->state == MODULE_STATE_UNFORMED)
686 continue;
Tejun Heo10fad5e2010-03-10 18:57:54 +0900687 if (!mod->percpu_size)
688 continue;
689 for_each_possible_cpu(cpu) {
690 void *start = per_cpu_ptr(mod->percpu, cpu);
Thomas Gleixner383776f2017-02-27 15:37:36 +0100691 void *va = (void *)addr;
Tejun Heo10fad5e2010-03-10 18:57:54 +0900692
Thomas Gleixner383776f2017-02-27 15:37:36 +0100693 if (va >= start && va < start + mod->percpu_size) {
Peter Zijlstra8ce371f2017-03-20 12:26:55 +0100694 if (can_addr) {
Thomas Gleixner383776f2017-02-27 15:37:36 +0100695 *can_addr = (unsigned long) (va - start);
Peter Zijlstra8ce371f2017-03-20 12:26:55 +0100696 *can_addr += (unsigned long)
697 per_cpu_ptr(mod->percpu,
698 get_boot_cpu_id());
699 }
Tejun Heo10fad5e2010-03-10 18:57:54 +0900700 preempt_enable();
701 return true;
702 }
703 }
704 }
705
706 preempt_enable();
707 return false;
Daniel Walker22a8bde2007-10-18 03:06:07 -0700708}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709
Thomas Gleixner383776f2017-02-27 15:37:36 +0100710/**
711 * is_module_percpu_address - test whether address is from module static percpu
712 * @addr: address to test
713 *
714 * Test whether @addr belongs to module static percpu area.
715 *
716 * RETURNS:
717 * %true if @addr is from module static percpu area
718 */
719bool is_module_percpu_address(unsigned long addr)
720{
721 return __is_module_percpu_address(addr, NULL);
722}
723
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724#else /* ... !CONFIG_SMP */
Tejun Heo6b588c12009-02-20 16:29:07 +0900725
Tejun Heo259354d2010-03-10 18:56:10 +0900726static inline void __percpu *mod_percpu(struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727{
728 return NULL;
729}
Rusty Russell9eb76d72013-07-03 10:06:29 +0930730static int percpu_modalloc(struct module *mod, struct load_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731{
Rusty Russell9eb76d72013-07-03 10:06:29 +0930732 /* UP modules shouldn't have this section: ENOMEM isn't quite right */
733 if (info->sechdrs[info->index.pcpu].sh_size != 0)
734 return -ENOMEM;
735 return 0;
Tejun Heo259354d2010-03-10 18:56:10 +0900736}
737static inline void percpu_modfree(struct module *mod)
738{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739}
Rusty Russell49668682010-08-05 12:59:10 -0600740static unsigned int find_pcpusec(struct load_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741{
742 return 0;
743}
Tejun Heo259354d2010-03-10 18:56:10 +0900744static inline void percpu_modcopy(struct module *mod,
745 const void *from, unsigned long size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746{
747 /* pcpusec should be 0, and size of that section should be 0. */
748 BUG_ON(size != 0);
749}
Tejun Heo10fad5e2010-03-10 18:57:54 +0900750bool is_module_percpu_address(unsigned long addr)
751{
752 return false;
753}
Tejun Heo6b588c12009-02-20 16:29:07 +0900754
Thomas Gleixner383776f2017-02-27 15:37:36 +0100755bool __is_module_percpu_address(unsigned long addr, unsigned long *can_addr)
756{
757 return false;
758}
759
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760#endif /* CONFIG_SMP */
761
Matt Domschc988d2b2005-06-23 22:05:15 -0700762#define MODINFO_ATTR(field) \
763static void setup_modinfo_##field(struct module *mod, const char *s) \
764{ \
765 mod->field = kstrdup(s, GFP_KERNEL); \
766} \
767static ssize_t show_modinfo_##field(struct module_attribute *mattr, \
Kay Sievers4befb022011-07-24 22:06:04 +0930768 struct module_kobject *mk, char *buffer) \
Matt Domschc988d2b2005-06-23 22:05:15 -0700769{ \
Chen Gangcc56ded2013-08-20 15:34:21 +0930770 return scnprintf(buffer, PAGE_SIZE, "%s\n", mk->mod->field); \
Matt Domschc988d2b2005-06-23 22:05:15 -0700771} \
772static int modinfo_##field##_exists(struct module *mod) \
773{ \
774 return mod->field != NULL; \
775} \
776static void free_modinfo_##field(struct module *mod) \
777{ \
Daniel Walker22a8bde2007-10-18 03:06:07 -0700778 kfree(mod->field); \
779 mod->field = NULL; \
Matt Domschc988d2b2005-06-23 22:05:15 -0700780} \
781static struct module_attribute modinfo_##field = { \
Tejun Heo7b595752007-06-14 03:45:17 +0900782 .attr = { .name = __stringify(field), .mode = 0444 }, \
Matt Domschc988d2b2005-06-23 22:05:15 -0700783 .show = show_modinfo_##field, \
784 .setup = setup_modinfo_##field, \
785 .test = modinfo_##field##_exists, \
786 .free = free_modinfo_##field, \
787};
788
789MODINFO_ATTR(version);
790MODINFO_ATTR(srcversion);
791
Arjan van de Vene14af7e2008-01-25 21:08:33 +0100792static char last_unloaded_module[MODULE_NAME_LEN+1];
793
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -0800794#ifdef CONFIG_MODULE_UNLOAD
Steven Rostedteb0c5372010-03-29 14:25:18 -0400795
796EXPORT_TRACEPOINT_SYMBOL(module_get);
797
Masami Hiramatsue513cc12014-11-10 09:30:29 +1030798/* MODULE_REF_BASE is the base reference count by kmodule loader. */
799#define MODULE_REF_BASE 1
800
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801/* Init the unload section of the module. */
Rusty Russell9f85a4b2010-08-05 12:59:04 -0600802static int module_unload_init(struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803{
Masami Hiramatsue513cc12014-11-10 09:30:29 +1030804 /*
805 * Initialize reference counter to MODULE_REF_BASE.
806 * refcnt == 0 means module is going.
807 */
808 atomic_set(&mod->refcnt, MODULE_REF_BASE);
Rusty Russell9f85a4b2010-08-05 12:59:04 -0600809
Linus Torvalds2c02dfe2010-05-31 12:19:37 -0700810 INIT_LIST_HEAD(&mod->source_list);
811 INIT_LIST_HEAD(&mod->target_list);
Christoph Lametere1783a22010-01-05 15:34:50 +0900812
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 /* Hold reference count during initialization. */
Masami Hiramatsue513cc12014-11-10 09:30:29 +1030814 atomic_inc(&mod->refcnt);
Rusty Russell9f85a4b2010-08-05 12:59:04 -0600815
816 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817}
818
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819/* Does a already use b? */
820static int already_uses(struct module *a, struct module *b)
821{
822 struct module_use *use;
823
Linus Torvalds2c02dfe2010-05-31 12:19:37 -0700824 list_for_each_entry(use, &b->source_list, source_list) {
825 if (use->source == a) {
Jim Cromie5e124162011-12-06 12:11:31 -0700826 pr_debug("%s uses %s!\n", a->name, b->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 return 1;
828 }
829 }
Jim Cromie5e124162011-12-06 12:11:31 -0700830 pr_debug("%s does not use %s!\n", a->name, b->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 return 0;
832}
833
Linus Torvalds2c02dfe2010-05-31 12:19:37 -0700834/*
835 * Module a uses b
836 * - we add 'a' as a "source", 'b' as a "target" of module use
837 * - the module_use is added to the list of 'b' sources (so
838 * 'b' can walk the list to see who sourced them), and of 'a'
839 * targets (so 'a' can see what modules it targets).
840 */
841static int add_module_usage(struct module *a, struct module *b)
842{
Linus Torvalds2c02dfe2010-05-31 12:19:37 -0700843 struct module_use *use;
844
Jim Cromie5e124162011-12-06 12:11:31 -0700845 pr_debug("Allocating new usage for %s.\n", a->name);
Linus Torvalds2c02dfe2010-05-31 12:19:37 -0700846 use = kmalloc(sizeof(*use), GFP_ATOMIC);
Markus Elfring9ad04572017-10-06 16:27:26 +0200847 if (!use)
Linus Torvalds2c02dfe2010-05-31 12:19:37 -0700848 return -ENOMEM;
Linus Torvalds2c02dfe2010-05-31 12:19:37 -0700849
850 use->source = a;
851 use->target = b;
852 list_add(&use->source_list, &b->source_list);
853 list_add(&use->target_list, &a->target_list);
Linus Torvalds2c02dfe2010-05-31 12:19:37 -0700854 return 0;
855}
856
Rusty Russell75676502010-06-05 11:17:36 -0600857/* Module a uses b: caller needs module_mutex() */
Rusty Russell9bea7f22010-06-05 11:17:37 -0600858int ref_module(struct module *a, struct module *b)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859{
Rusty Russellc8e21ce2010-06-05 11:17:35 -0600860 int err;
Kay Sievers270a6c42007-01-18 13:26:15 +0100861
Rusty Russell9bea7f22010-06-05 11:17:37 -0600862 if (b == NULL || already_uses(a, b))
Linus Torvalds218ce732010-05-25 16:48:30 -0700863 return 0;
Linus Torvalds218ce732010-05-25 16:48:30 -0700864
Rusty Russell9bea7f22010-06-05 11:17:37 -0600865 /* If module isn't available, we fail. */
866 err = strong_try_module_get(b);
Rusty Russellc9a3ba52008-01-29 17:13:18 -0500867 if (err)
Rusty Russell9bea7f22010-06-05 11:17:37 -0600868 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869
Linus Torvalds2c02dfe2010-05-31 12:19:37 -0700870 err = add_module_usage(a, b);
871 if (err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 module_put(b);
Rusty Russell9bea7f22010-06-05 11:17:37 -0600873 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 }
Rusty Russell9bea7f22010-06-05 11:17:37 -0600875 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876}
Rusty Russell9bea7f22010-06-05 11:17:37 -0600877EXPORT_SYMBOL_GPL(ref_module);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878
879/* Clear the unload stuff of the module. */
880static void module_unload_free(struct module *mod)
881{
Linus Torvalds2c02dfe2010-05-31 12:19:37 -0700882 struct module_use *use, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883
Rusty Russell75676502010-06-05 11:17:36 -0600884 mutex_lock(&module_mutex);
Linus Torvalds2c02dfe2010-05-31 12:19:37 -0700885 list_for_each_entry_safe(use, tmp, &mod->target_list, target_list) {
886 struct module *i = use->target;
Jim Cromie5e124162011-12-06 12:11:31 -0700887 pr_debug("%s unusing %s\n", mod->name, i->name);
Linus Torvalds2c02dfe2010-05-31 12:19:37 -0700888 module_put(i);
889 list_del(&use->source_list);
890 list_del(&use->target_list);
891 kfree(use);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 }
Rusty Russell75676502010-06-05 11:17:36 -0600893 mutex_unlock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894}
895
896#ifdef CONFIG_MODULE_FORCE_UNLOAD
Akinobu Mitafb169792006-01-08 01:04:29 -0800897static inline int try_force_unload(unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898{
899 int ret = (flags & O_TRUNC);
900 if (ret)
Rusty Russell373d4d02013-01-21 17:17:39 +1030901 add_taint(TAINT_FORCED_RMMOD, LOCKDEP_NOW_UNRELIABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 return ret;
903}
904#else
Akinobu Mitafb169792006-01-08 01:04:29 -0800905static inline int try_force_unload(unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906{
907 return 0;
908}
909#endif /* CONFIG_MODULE_FORCE_UNLOAD */
910
Masami Hiramatsue513cc12014-11-10 09:30:29 +1030911/* Try to release refcount of module, 0 means success. */
912static int try_release_module_ref(struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913{
Masami Hiramatsue513cc12014-11-10 09:30:29 +1030914 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915
Masami Hiramatsue513cc12014-11-10 09:30:29 +1030916 /* Try to decrement refcnt which we set at loading */
917 ret = atomic_sub_return(MODULE_REF_BASE, &mod->refcnt);
918 BUG_ON(ret < 0);
919 if (ret)
920 /* Someone can put this right now, recover with checking */
921 ret = atomic_add_unless(&mod->refcnt, MODULE_REF_BASE, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922
Masami Hiramatsue513cc12014-11-10 09:30:29 +1030923 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924}
925
926static int try_stop_module(struct module *mod, int flags, int *forced)
927{
Masami Hiramatsue513cc12014-11-10 09:30:29 +1030928 /* If it's not unused, quit unless we're forcing. */
929 if (try_release_module_ref(mod) != 0) {
930 *forced = try_force_unload(flags);
931 if (!(*forced))
932 return -EWOULDBLOCK;
933 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934
Masami Hiramatsue513cc12014-11-10 09:30:29 +1030935 /* Mark it as dying. */
936 mod->state = MODULE_STATE_GOING;
937
938 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939}
940
Rusty Russelld5db1392015-01-22 11:13:14 +1030941/**
942 * module_refcount - return the refcount or -1 if unloading
943 *
944 * @mod: the module we're checking
945 *
946 * Returns:
947 * -1 if the module is in the process of unloading
948 * otherwise the number of references in the kernel to the module
949 */
950int module_refcount(struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951{
Rusty Russelld5db1392015-01-22 11:13:14 +1030952 return atomic_read(&mod->refcnt) - MODULE_REF_BASE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953}
954EXPORT_SYMBOL(module_refcount);
955
956/* This exists whether we can unload or not */
957static void free_module(struct module *mod);
958
Heiko Carstens17da2bd2009-01-14 14:14:10 +0100959SYSCALL_DEFINE2(delete_module, const char __user *, name_user,
960 unsigned int, flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961{
962 struct module *mod;
Greg Kroah-Hartmandfff0a02007-02-23 14:54:57 -0800963 char name[MODULE_NAME_LEN];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 int ret, forced = 0;
965
Kees Cook3d433212009-04-02 15:49:29 -0700966 if (!capable(CAP_SYS_MODULE) || modules_disabled)
Greg Kroah-Hartmandfff0a02007-02-23 14:54:57 -0800967 return -EPERM;
968
969 if (strncpy_from_user(name, name_user, MODULE_NAME_LEN-1) < 0)
970 return -EFAULT;
971 name[MODULE_NAME_LEN-1] = '\0';
972
Richard Guy Briggsf6276ac2017-05-02 10:16:04 -0400973 audit_log_kern_module(name);
974
Tejun Heo3fc1f1e2010-05-06 18:49:20 +0200975 if (mutex_lock_interruptible(&module_mutex) != 0)
976 return -EINTR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977
978 mod = find_module(name);
979 if (!mod) {
980 ret = -ENOENT;
981 goto out;
982 }
983
Linus Torvalds2c02dfe2010-05-31 12:19:37 -0700984 if (!list_empty(&mod->source_list)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 /* Other modules depend on us: get rid of them first. */
986 ret = -EWOULDBLOCK;
987 goto out;
988 }
989
990 /* Doing init or already dying? */
991 if (mod->state != MODULE_STATE_LIVE) {
Rusty Russell3f2b9c92013-09-17 05:48:51 +0930992 /* FIXME: if (force), slam module count damn the torpedoes */
Jim Cromie5e124162011-12-06 12:11:31 -0700993 pr_debug("%s already dying\n", mod->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 ret = -EBUSY;
995 goto out;
996 }
997
998 /* If it has an init func, it must have an exit func to unload */
Rusty Russellaf49d922007-10-16 23:26:27 -0700999 if (mod->init && !mod->exit) {
Akinobu Mitafb169792006-01-08 01:04:29 -08001000 forced = try_force_unload(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 if (!forced) {
1002 /* This module can't be removed */
1003 ret = -EBUSY;
1004 goto out;
1005 }
1006 }
1007
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 /* Stop the machine so refcounts can't move and disable module. */
1009 ret = try_stop_module(mod, flags, &forced);
1010 if (ret != 0)
1011 goto out;
1012
Peter Oberparleiterdf4b5652008-04-21 14:34:31 +02001013 mutex_unlock(&module_mutex);
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001014 /* Final destruction now no one is using it. */
Peter Oberparleiterdf4b5652008-04-21 14:34:31 +02001015 if (mod->exit != NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 mod->exit();
Peter Oberparleiterdf4b5652008-04-21 14:34:31 +02001017 blocking_notifier_call_chain(&module_notify_list,
1018 MODULE_STATE_GOING, mod);
Jessica Yu7e545d62016-03-16 20:55:39 -04001019 klp_module_going(mod);
Jessica Yu7dcd1822016-02-16 17:32:33 -05001020 ftrace_release_mod(mod);
1021
Arjan van de Ven22a9d642009-01-07 08:45:46 -08001022 async_synchronize_full();
Rusty Russell75676502010-06-05 11:17:36 -06001023
Arjan van de Vene14af7e2008-01-25 21:08:33 +01001024 /* Store the name of the last unloaded module for diagnostic purposes */
Rusty Russellefa53452008-01-29 17:13:20 -05001025 strlcpy(last_unloaded_module, mod->name, sizeof(last_unloaded_module));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026
Rusty Russell75676502010-06-05 11:17:36 -06001027 free_module(mod);
1028 return 0;
1029out:
Ashutosh Naik6389a382006-03-23 03:00:46 -08001030 mutex_unlock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 return ret;
1032}
1033
Jianjun Kongd1e99d72008-12-08 14:26:29 +08001034static inline void print_unload_info(struct seq_file *m, struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035{
1036 struct module_use *use;
1037 int printed_something = 0;
1038
Rusty Russelld5db1392015-01-22 11:13:14 +10301039 seq_printf(m, " %i ", module_refcount(mod));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040
Ionut Alexa6da0b562014-11-10 09:31:29 +10301041 /*
1042 * Always include a trailing , so userspace can differentiate
1043 * between this and the old multi-field proc format.
1044 */
Linus Torvalds2c02dfe2010-05-31 12:19:37 -07001045 list_for_each_entry(use, &mod->source_list, source_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 printed_something = 1;
Linus Torvalds2c02dfe2010-05-31 12:19:37 -07001047 seq_printf(m, "%s,", use->source->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 }
1049
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 if (mod->init != NULL && mod->exit == NULL) {
1051 printed_something = 1;
Ionut Alexa6da0b562014-11-10 09:31:29 +10301052 seq_puts(m, "[permanent],");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 }
1054
1055 if (!printed_something)
Ionut Alexa6da0b562014-11-10 09:31:29 +10301056 seq_puts(m, "-");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057}
1058
1059void __symbol_put(const char *symbol)
1060{
1061 struct module *owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062
Rusty Russell24da1cb2007-07-15 23:41:46 -07001063 preempt_disable();
Tim Abbott414fd312008-12-05 19:03:56 -05001064 if (!find_symbol(symbol, &owner, NULL, true, false))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 BUG();
1066 module_put(owner);
Rusty Russell24da1cb2007-07-15 23:41:46 -07001067 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068}
1069EXPORT_SYMBOL(__symbol_put);
1070
Rusty Russell7d1d16e2009-08-26 22:02:54 +09301071/* Note this assumes addr is a function, which it currently always is. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072void symbol_put_addr(void *addr)
1073{
Trent Piepho5e376612006-05-15 09:44:06 -07001074 struct module *modaddr;
Rusty Russell7d1d16e2009-08-26 22:02:54 +09301075 unsigned long a = (unsigned long)dereference_function_descriptor(addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076
Rusty Russell7d1d16e2009-08-26 22:02:54 +09301077 if (core_kernel_text(a))
Trent Piepho5e376612006-05-15 09:44:06 -07001078 return;
1079
Peter Zijlstra275d7d42015-08-20 10:34:59 +09301080 /*
1081 * Even though we hold a reference on the module; we still need to
1082 * disable preemption in order to safely traverse the data structure.
1083 */
1084 preempt_disable();
Rusty Russell7d1d16e2009-08-26 22:02:54 +09301085 modaddr = __module_text_address(a);
Rusty Russella6e6abd2009-03-31 13:05:31 -06001086 BUG_ON(!modaddr);
Trent Piepho5e376612006-05-15 09:44:06 -07001087 module_put(modaddr);
Peter Zijlstra275d7d42015-08-20 10:34:59 +09301088 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089}
1090EXPORT_SYMBOL_GPL(symbol_put_addr);
1091
1092static ssize_t show_refcnt(struct module_attribute *mattr,
Kay Sievers4befb022011-07-24 22:06:04 +09301093 struct module_kobject *mk, char *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094{
Rusty Russelld5db1392015-01-22 11:13:14 +10301095 return sprintf(buffer, "%i\n", module_refcount(mk->mod));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096}
1097
Kay Sieverscca3e702012-01-13 09:32:15 +10301098static struct module_attribute modinfo_refcnt =
1099 __ATTR(refcnt, 0444, show_refcnt, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100
Steven Rostedtd53799b2012-03-26 12:50:52 +10301101void __module_get(struct module *module)
1102{
1103 if (module) {
1104 preempt_disable();
Masami Hiramatsu2f35c412014-11-10 09:29:29 +10301105 atomic_inc(&module->refcnt);
Steven Rostedtd53799b2012-03-26 12:50:52 +10301106 trace_module_get(module, _RET_IP_);
1107 preempt_enable();
1108 }
1109}
1110EXPORT_SYMBOL(__module_get);
1111
1112bool try_module_get(struct module *module)
1113{
1114 bool ret = true;
1115
1116 if (module) {
1117 preempt_disable();
Masami Hiramatsue513cc12014-11-10 09:30:29 +10301118 /* Note: here, we can fail to get a reference */
1119 if (likely(module_is_live(module) &&
1120 atomic_inc_not_zero(&module->refcnt) != 0))
Steven Rostedtd53799b2012-03-26 12:50:52 +10301121 trace_module_get(module, _RET_IP_);
Masami Hiramatsue513cc12014-11-10 09:30:29 +10301122 else
Steven Rostedtd53799b2012-03-26 12:50:52 +10301123 ret = false;
1124
1125 preempt_enable();
1126 }
1127 return ret;
1128}
1129EXPORT_SYMBOL(try_module_get);
1130
Al Virof6a57032006-10-18 01:47:25 -04001131void module_put(struct module *module)
1132{
Masami Hiramatsue513cc12014-11-10 09:30:29 +10301133 int ret;
1134
Al Virof6a57032006-10-18 01:47:25 -04001135 if (module) {
Christoph Lametere1783a22010-01-05 15:34:50 +09001136 preempt_disable();
Masami Hiramatsue513cc12014-11-10 09:30:29 +10301137 ret = atomic_dec_if_positive(&module->refcnt);
1138 WARN_ON(ret < 0); /* Failed to put refcount */
Li Zefanae832d12010-03-24 10:57:43 +08001139 trace_module_put(module, _RET_IP_);
Christoph Lametere1783a22010-01-05 15:34:50 +09001140 preempt_enable();
Al Virof6a57032006-10-18 01:47:25 -04001141 }
1142}
1143EXPORT_SYMBOL(module_put);
1144
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145#else /* !CONFIG_MODULE_UNLOAD */
Jianjun Kongd1e99d72008-12-08 14:26:29 +08001146static inline void print_unload_info(struct seq_file *m, struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147{
1148 /* We don't know the usage count, or what modules are using. */
Ionut Alexa6da0b562014-11-10 09:31:29 +10301149 seq_puts(m, " - -");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150}
1151
1152static inline void module_unload_free(struct module *mod)
1153{
1154}
1155
Rusty Russell9bea7f22010-06-05 11:17:37 -06001156int ref_module(struct module *a, struct module *b)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157{
Rusty Russell9bea7f22010-06-05 11:17:37 -06001158 return strong_try_module_get(b);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159}
Rusty Russell9bea7f22010-06-05 11:17:37 -06001160EXPORT_SYMBOL_GPL(ref_module);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161
Rusty Russell9f85a4b2010-08-05 12:59:04 -06001162static inline int module_unload_init(struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163{
Rusty Russell9f85a4b2010-08-05 12:59:04 -06001164 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165}
1166#endif /* CONFIG_MODULE_UNLOAD */
1167
Kevin Winchester53999bf2012-01-15 19:32:55 -04001168static size_t module_flags_taint(struct module *mod, char *buf)
1169{
1170 size_t l = 0;
Petr Mladek7fd83292016-09-21 13:47:22 +02001171 int i;
Kevin Winchester53999bf2012-01-15 19:32:55 -04001172
Petr Mladek7fd83292016-09-21 13:47:22 +02001173 for (i = 0; i < TAINT_FLAGS_COUNT; i++) {
1174 if (taint_flags[i].module && test_bit(i, &mod->taints))
Larry Finger5eb7c0d2017-01-01 20:25:25 -06001175 buf[l++] = taint_flags[i].c_true;
Petr Mladek7fd83292016-09-21 13:47:22 +02001176 }
1177
Kevin Winchester53999bf2012-01-15 19:32:55 -04001178 return l;
1179}
1180
Kay Sievers1f717402006-11-24 12:15:25 +01001181static ssize_t show_initstate(struct module_attribute *mattr,
Kay Sievers4befb022011-07-24 22:06:04 +09301182 struct module_kobject *mk, char *buffer)
Kay Sievers1f717402006-11-24 12:15:25 +01001183{
1184 const char *state = "unknown";
1185
Kay Sievers4befb022011-07-24 22:06:04 +09301186 switch (mk->mod->state) {
Kay Sievers1f717402006-11-24 12:15:25 +01001187 case MODULE_STATE_LIVE:
1188 state = "live";
1189 break;
1190 case MODULE_STATE_COMING:
1191 state = "coming";
1192 break;
1193 case MODULE_STATE_GOING:
1194 state = "going";
1195 break;
Rusty Russell0d21b0e2013-01-12 11:38:44 +10301196 default:
1197 BUG();
Kay Sievers1f717402006-11-24 12:15:25 +01001198 }
1199 return sprintf(buffer, "%s\n", state);
1200}
1201
Kay Sieverscca3e702012-01-13 09:32:15 +10301202static struct module_attribute modinfo_initstate =
1203 __ATTR(initstate, 0444, show_initstate, NULL);
Kay Sievers1f717402006-11-24 12:15:25 +01001204
Kay Sievers88bfa322011-07-24 22:06:04 +09301205static ssize_t store_uevent(struct module_attribute *mattr,
1206 struct module_kobject *mk,
1207 const char *buffer, size_t count)
1208{
Peter Rajnohadf44b472018-12-05 12:27:44 +01001209 int rc;
1210
1211 rc = kobject_synth_uevent(&mk->kobj, buffer, count);
1212 return rc ? rc : count;
Kay Sievers88bfa322011-07-24 22:06:04 +09301213}
1214
Kay Sieverscca3e702012-01-13 09:32:15 +10301215struct module_attribute module_uevent =
1216 __ATTR(uevent, 0200, NULL, store_uevent);
1217
1218static ssize_t show_coresize(struct module_attribute *mattr,
1219 struct module_kobject *mk, char *buffer)
1220{
Rusty Russell7523e4d2015-11-26 09:44:08 +10301221 return sprintf(buffer, "%u\n", mk->mod->core_layout.size);
Kay Sieverscca3e702012-01-13 09:32:15 +10301222}
1223
1224static struct module_attribute modinfo_coresize =
1225 __ATTR(coresize, 0444, show_coresize, NULL);
1226
1227static ssize_t show_initsize(struct module_attribute *mattr,
1228 struct module_kobject *mk, char *buffer)
1229{
Rusty Russell7523e4d2015-11-26 09:44:08 +10301230 return sprintf(buffer, "%u\n", mk->mod->init_layout.size);
Kay Sieverscca3e702012-01-13 09:32:15 +10301231}
1232
1233static struct module_attribute modinfo_initsize =
1234 __ATTR(initsize, 0444, show_initsize, NULL);
1235
1236static ssize_t show_taint(struct module_attribute *mattr,
1237 struct module_kobject *mk, char *buffer)
1238{
1239 size_t l;
1240
1241 l = module_flags_taint(mk->mod, buffer);
1242 buffer[l++] = '\n';
1243 return l;
1244}
1245
1246static struct module_attribute modinfo_taint =
1247 __ATTR(taint, 0444, show_taint, NULL);
Kay Sievers88bfa322011-07-24 22:06:04 +09301248
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001249static struct module_attribute *modinfo_attrs[] = {
Kay Sieverscca3e702012-01-13 09:32:15 +10301250 &module_uevent,
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001251 &modinfo_version,
1252 &modinfo_srcversion,
Kay Sieverscca3e702012-01-13 09:32:15 +10301253 &modinfo_initstate,
1254 &modinfo_coresize,
1255 &modinfo_initsize,
1256 &modinfo_taint,
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001257#ifdef CONFIG_MODULE_UNLOAD
Kay Sieverscca3e702012-01-13 09:32:15 +10301258 &modinfo_refcnt,
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001259#endif
1260 NULL,
1261};
1262
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263static const char vermagic[] = VERMAGIC_STRING;
1264
Rusty Russellc6e665c2009-03-31 13:05:33 -06001265static int try_to_force_load(struct module *mod, const char *reason)
Linus Torvalds826e4502008-05-04 17:04:16 -07001266{
1267#ifdef CONFIG_MODULE_FORCE_LOAD
Andi Kleen25ddbb12008-10-15 22:01:41 -07001268 if (!test_taint(TAINT_FORCED_MODULE))
Andrew Mortonbddb12b2013-11-12 15:11:28 -08001269 pr_warn("%s: %s: kernel tainted.\n", mod->name, reason);
Rusty Russell373d4d02013-01-21 17:17:39 +10301270 add_taint_module(mod, TAINT_FORCED_MODULE, LOCKDEP_NOW_UNRELIABLE);
Linus Torvalds826e4502008-05-04 17:04:16 -07001271 return 0;
1272#else
1273 return -ENOEXEC;
1274#endif
1275}
1276
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277#ifdef CONFIG_MODVERSIONS
Ard Biesheuvel71810db2017-02-03 09:54:06 +00001278
1279static u32 resolve_rel_crc(const s32 *crc)
Rusty Russelld4703ae2009-12-15 16:28:32 -06001280{
Ard Biesheuvel71810db2017-02-03 09:54:06 +00001281 return *(u32 *)((void *)crc + *crc);
Rusty Russelld4703ae2009-12-15 16:28:32 -06001282}
1283
Kees Cook49019422017-04-21 15:35:26 -07001284static int check_version(const struct load_info *info,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285 const char *symname,
Ionut Alexa6da0b562014-11-10 09:31:29 +10301286 struct module *mod,
Ard Biesheuvel71810db2017-02-03 09:54:06 +00001287 const s32 *crc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288{
Kees Cook49019422017-04-21 15:35:26 -07001289 Elf_Shdr *sechdrs = info->sechdrs;
1290 unsigned int versindex = info->index.vers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291 unsigned int i, num_versions;
1292 struct modversion_info *versions;
1293
1294 /* Exporting module didn't supply crcs? OK, we're already tainted. */
1295 if (!crc)
1296 return 1;
1297
Rusty Russella5dd6972008-05-09 16:24:21 +10001298 /* No versions at all? modprobe --force does this. */
1299 if (versindex == 0)
1300 return try_to_force_load(mod, symname) == 0;
1301
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 versions = (void *) sechdrs[versindex].sh_addr;
1303 num_versions = sechdrs[versindex].sh_size
1304 / sizeof(struct modversion_info);
1305
1306 for (i = 0; i < num_versions; i++) {
Ard Biesheuvel71810db2017-02-03 09:54:06 +00001307 u32 crcval;
1308
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309 if (strcmp(versions[i].name, symname) != 0)
1310 continue;
1311
Ard Biesheuvel71810db2017-02-03 09:54:06 +00001312 if (IS_ENABLED(CONFIG_MODULE_REL_CRCS))
1313 crcval = resolve_rel_crc(crc);
1314 else
1315 crcval = *crc;
1316 if (versions[i].crc == crcval)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317 return 1;
Ard Biesheuvel71810db2017-02-03 09:54:06 +00001318 pr_debug("Found checksum %X vs module %lX\n",
1319 crcval, versions[i].crc);
Linus Torvalds826e4502008-05-04 17:04:16 -07001320 goto bad_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321 }
Linus Torvalds826e4502008-05-04 17:04:16 -07001322
Linus Torvaldsfaaae2a2016-11-29 15:20:14 -08001323 /* Broken toolchain. Warn once, then let it go.. */
Kees Cook3e2e8572017-04-21 15:35:27 -07001324 pr_warn_once("%s: no symbol version for %s\n", info->name, symname);
Linus Torvaldsfaaae2a2016-11-29 15:20:14 -08001325 return 1;
Linus Torvalds826e4502008-05-04 17:04:16 -07001326
1327bad_version:
Ionut Alexa6da0b562014-11-10 09:31:29 +10301328 pr_warn("%s: disagrees about version of symbol %s\n",
Kees Cook3e2e8572017-04-21 15:35:27 -07001329 info->name, symname);
Linus Torvalds826e4502008-05-04 17:04:16 -07001330 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331}
1332
Kees Cook49019422017-04-21 15:35:26 -07001333static inline int check_modstruct_version(const struct load_info *info,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334 struct module *mod)
1335{
Ard Biesheuvel71810db2017-02-03 09:54:06 +00001336 const s32 *crc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337
Peter Zijlstra926a59b2015-05-27 11:09:35 +09301338 /*
1339 * Since this should be found in kernel (which can't be removed), no
1340 * locking is necessary -- use preempt_disable() to placate lockdep.
1341 */
1342 preempt_disable();
Masahiro Yamada996302c2018-06-24 00:37:44 +09001343 if (!find_symbol("module_layout", NULL, &crc, true, false)) {
Peter Zijlstra926a59b2015-05-27 11:09:35 +09301344 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345 BUG();
Peter Zijlstra926a59b2015-05-27 11:09:35 +09301346 }
1347 preempt_enable();
Masahiro Yamada996302c2018-06-24 00:37:44 +09001348 return check_version(info, "module_layout", mod, crc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349}
1350
Rusty Russell91e37a72008-05-09 16:25:28 +10001351/* First part is kernel version, which we ignore if module has crcs. */
1352static inline int same_magic(const char *amagic, const char *bmagic,
1353 bool has_crcs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354{
Rusty Russell91e37a72008-05-09 16:25:28 +10001355 if (has_crcs) {
1356 amagic += strcspn(amagic, " ");
1357 bmagic += strcspn(bmagic, " ");
1358 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359 return strcmp(amagic, bmagic) == 0;
1360}
1361#else
Kees Cook49019422017-04-21 15:35:26 -07001362static inline int check_version(const struct load_info *info,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363 const char *symname,
Ionut Alexa6da0b562014-11-10 09:31:29 +10301364 struct module *mod,
Ard Biesheuvel71810db2017-02-03 09:54:06 +00001365 const s32 *crc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366{
1367 return 1;
1368}
1369
Kees Cook49019422017-04-21 15:35:26 -07001370static inline int check_modstruct_version(const struct load_info *info,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371 struct module *mod)
1372{
1373 return 1;
1374}
1375
Rusty Russell91e37a72008-05-09 16:25:28 +10001376static inline int same_magic(const char *amagic, const char *bmagic,
1377 bool has_crcs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378{
1379 return strcmp(amagic, bmagic) == 0;
1380}
1381#endif /* CONFIG_MODVERSIONS */
1382
Rusty Russell75676502010-06-05 11:17:36 -06001383/* Resolve a symbol for this module. I.e. if we find one, record usage. */
Rusty Russell49668682010-08-05 12:59:10 -06001384static const struct kernel_symbol *resolve_symbol(struct module *mod,
1385 const struct load_info *info,
Tim Abbott414fd312008-12-05 19:03:56 -05001386 const char *name,
Rusty Russell9bea7f22010-06-05 11:17:37 -06001387 char ownername[])
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388{
1389 struct module *owner;
Tim Abbott414fd312008-12-05 19:03:56 -05001390 const struct kernel_symbol *sym;
Ard Biesheuvel71810db2017-02-03 09:54:06 +00001391 const s32 *crc;
Rusty Russell9bea7f22010-06-05 11:17:37 -06001392 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393
Peter Zijlstrad64810f2015-02-11 15:01:13 +10301394 /*
1395 * The module_mutex should not be a heavily contended lock;
1396 * if we get the occasional sleep here, we'll go an extra iteration
1397 * in the wait_event_interruptible(), which is harmless.
1398 */
1399 sched_annotate_sleep();
Rusty Russell75676502010-06-05 11:17:36 -06001400 mutex_lock(&module_mutex);
Tim Abbott414fd312008-12-05 19:03:56 -05001401 sym = find_symbol(name, &owner, &crc,
Andi Kleen25ddbb12008-10-15 22:01:41 -07001402 !(mod->taints & (1 << TAINT_PROPRIETARY_MODULE)), true);
Rusty Russell9bea7f22010-06-05 11:17:37 -06001403 if (!sym)
1404 goto unlock;
1405
Kees Cook49019422017-04-21 15:35:26 -07001406 if (!check_version(info, name, mod, crc)) {
Rusty Russell9bea7f22010-06-05 11:17:37 -06001407 sym = ERR_PTR(-EINVAL);
1408 goto getname;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409 }
Rusty Russell9bea7f22010-06-05 11:17:37 -06001410
1411 err = ref_module(mod, owner);
1412 if (err) {
1413 sym = ERR_PTR(err);
1414 goto getname;
1415 }
1416
1417getname:
1418 /* We must make copy under the lock if we failed to get ref. */
1419 strncpy(ownername, module_name(owner), MODULE_NAME_LEN);
1420unlock:
Rusty Russell75676502010-06-05 11:17:36 -06001421 mutex_unlock(&module_mutex);
Linus Torvalds218ce732010-05-25 16:48:30 -07001422 return sym;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423}
1424
Rusty Russell49668682010-08-05 12:59:10 -06001425static const struct kernel_symbol *
1426resolve_symbol_wait(struct module *mod,
1427 const struct load_info *info,
1428 const char *name)
Rusty Russell9bea7f22010-06-05 11:17:37 -06001429{
1430 const struct kernel_symbol *ksym;
Rusty Russell49668682010-08-05 12:59:10 -06001431 char owner[MODULE_NAME_LEN];
Rusty Russell9bea7f22010-06-05 11:17:37 -06001432
1433 if (wait_event_interruptible_timeout(module_wq,
Rusty Russell49668682010-08-05 12:59:10 -06001434 !IS_ERR(ksym = resolve_symbol(mod, info, name, owner))
1435 || PTR_ERR(ksym) != -EBUSY,
Rusty Russell9bea7f22010-06-05 11:17:37 -06001436 30 * HZ) <= 0) {
Andrew Mortonbddb12b2013-11-12 15:11:28 -08001437 pr_warn("%s: gave up waiting for init of module %s.\n",
1438 mod->name, owner);
Rusty Russell9bea7f22010-06-05 11:17:37 -06001439 }
1440 return ksym;
1441}
1442
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443/*
1444 * /sys/module/foo/sections stuff
1445 * J. Corbet <corbet@lwn.net>
1446 */
Rusty Russell8f6d0372010-08-05 12:59:09 -06001447#ifdef CONFIG_SYSFS
Ben Hutchings10b465a2009-12-19 14:43:01 +00001448
Rusty Russell8f6d0372010-08-05 12:59:09 -06001449#ifdef CONFIG_KALLSYMS
Ben Hutchings10b465a2009-12-19 14:43:01 +00001450static inline bool sect_empty(const Elf_Shdr *sect)
1451{
1452 return !(sect->sh_flags & SHF_ALLOC) || sect->sh_size == 0;
1453}
1454
Ionut Alexa6da0b562014-11-10 09:31:29 +10301455struct module_sect_attr {
Rusty Russella58730c2008-03-13 09:03:44 +00001456 struct module_attribute mattr;
1457 char *name;
1458 unsigned long address;
1459};
1460
Ionut Alexa6da0b562014-11-10 09:31:29 +10301461struct module_sect_attrs {
Rusty Russella58730c2008-03-13 09:03:44 +00001462 struct attribute_group grp;
1463 unsigned int nsections;
1464 struct module_sect_attr attrs[0];
1465};
1466
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467static ssize_t module_sect_show(struct module_attribute *mattr,
Kay Sievers4befb022011-07-24 22:06:04 +09301468 struct module_kobject *mk, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469{
1470 struct module_sect_attr *sattr =
1471 container_of(mattr, struct module_sect_attr, mattr);
Thomas Richterbe71eda2018-04-18 09:14:36 +02001472 return sprintf(buf, "0x%px\n", kptr_restrict < 2 ?
1473 (void *)sattr->address : NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474}
1475
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001476static void free_sect_attrs(struct module_sect_attrs *sect_attrs)
1477{
Rusty Russella58730c2008-03-13 09:03:44 +00001478 unsigned int section;
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001479
1480 for (section = 0; section < sect_attrs->nsections; section++)
1481 kfree(sect_attrs->attrs[section].name);
1482 kfree(sect_attrs);
1483}
1484
Rusty Russell8f6d0372010-08-05 12:59:09 -06001485static void add_sect_attrs(struct module *mod, const struct load_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486{
1487 unsigned int nloaded = 0, i, size[2];
1488 struct module_sect_attrs *sect_attrs;
1489 struct module_sect_attr *sattr;
1490 struct attribute **gattr;
Daniel Walker22a8bde2007-10-18 03:06:07 -07001491
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492 /* Count loaded sections and allocate structures */
Rusty Russell8f6d0372010-08-05 12:59:09 -06001493 for (i = 0; i < info->hdr->e_shnum; i++)
1494 if (!sect_empty(&info->sechdrs[i]))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495 nloaded++;
Gustavo A. R. Silva8d1b73d2019-06-06 13:18:53 -05001496 size[0] = ALIGN(struct_size(sect_attrs, attrs, nloaded),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497 sizeof(sect_attrs->grp.attrs[0]));
1498 size[1] = (nloaded + 1) * sizeof(sect_attrs->grp.attrs[0]);
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001499 sect_attrs = kzalloc(size[0] + size[1], GFP_KERNEL);
1500 if (sect_attrs == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501 return;
1502
1503 /* Setup section attributes. */
1504 sect_attrs->grp.name = "sections";
1505 sect_attrs->grp.attrs = (void *)sect_attrs + size[0];
1506
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001507 sect_attrs->nsections = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508 sattr = &sect_attrs->attrs[0];
1509 gattr = &sect_attrs->grp.attrs[0];
Rusty Russell8f6d0372010-08-05 12:59:09 -06001510 for (i = 0; i < info->hdr->e_shnum; i++) {
1511 Elf_Shdr *sec = &info->sechdrs[i];
1512 if (sect_empty(sec))
Helge Deller35dead42009-12-03 00:29:15 +01001513 continue;
Rusty Russell8f6d0372010-08-05 12:59:09 -06001514 sattr->address = sec->sh_addr;
1515 sattr->name = kstrdup(info->secstrings + sec->sh_name,
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001516 GFP_KERNEL);
1517 if (sattr->name == NULL)
1518 goto out;
1519 sect_attrs->nsections++;
Eric W. Biederman361795b2010-02-12 13:41:56 -08001520 sysfs_attr_init(&sattr->mattr.attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521 sattr->mattr.show = module_sect_show;
1522 sattr->mattr.store = NULL;
1523 sattr->mattr.attr.name = sattr->name;
Linus Torvalds277642d2017-11-12 17:00:53 -08001524 sattr->mattr.attr.mode = S_IRUSR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525 *(gattr++) = &(sattr++)->mattr.attr;
1526 }
1527 *gattr = NULL;
1528
1529 if (sysfs_create_group(&mod->mkobj.kobj, &sect_attrs->grp))
1530 goto out;
1531
1532 mod->sect_attrs = sect_attrs;
1533 return;
1534 out:
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001535 free_sect_attrs(sect_attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536}
1537
1538static void remove_sect_attrs(struct module *mod)
1539{
1540 if (mod->sect_attrs) {
1541 sysfs_remove_group(&mod->mkobj.kobj,
1542 &mod->sect_attrs->grp);
1543 /* We are positive that no one is using any sect attrs
1544 * at this point. Deallocate immediately. */
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001545 free_sect_attrs(mod->sect_attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546 mod->sect_attrs = NULL;
1547 }
1548}
1549
Roland McGrath6d760132007-10-16 23:26:40 -07001550/*
1551 * /sys/module/foo/notes/.section.name gives contents of SHT_NOTE sections.
1552 */
1553
1554struct module_notes_attrs {
1555 struct kobject *dir;
1556 unsigned int notes;
1557 struct bin_attribute attrs[0];
1558};
1559
Chris Wright2c3c8be2010-05-12 18:28:57 -07001560static ssize_t module_notes_read(struct file *filp, struct kobject *kobj,
Roland McGrath6d760132007-10-16 23:26:40 -07001561 struct bin_attribute *bin_attr,
1562 char *buf, loff_t pos, size_t count)
1563{
1564 /*
1565 * The caller checked the pos and count against our size.
1566 */
1567 memcpy(buf, bin_attr->private + pos, count);
1568 return count;
1569}
1570
1571static void free_notes_attrs(struct module_notes_attrs *notes_attrs,
1572 unsigned int i)
1573{
1574 if (notes_attrs->dir) {
1575 while (i-- > 0)
1576 sysfs_remove_bin_file(notes_attrs->dir,
1577 &notes_attrs->attrs[i]);
Alexey Dobriyane9432092008-09-23 23:51:11 +04001578 kobject_put(notes_attrs->dir);
Roland McGrath6d760132007-10-16 23:26:40 -07001579 }
1580 kfree(notes_attrs);
1581}
1582
Rusty Russell8f6d0372010-08-05 12:59:09 -06001583static void add_notes_attrs(struct module *mod, const struct load_info *info)
Roland McGrath6d760132007-10-16 23:26:40 -07001584{
1585 unsigned int notes, loaded, i;
1586 struct module_notes_attrs *notes_attrs;
1587 struct bin_attribute *nattr;
1588
Ingo Molnarea6bff32009-08-28 10:44:56 +02001589 /* failed to create section attributes, so can't create notes */
1590 if (!mod->sect_attrs)
1591 return;
1592
Roland McGrath6d760132007-10-16 23:26:40 -07001593 /* Count notes sections and allocate structures. */
1594 notes = 0;
Rusty Russell8f6d0372010-08-05 12:59:09 -06001595 for (i = 0; i < info->hdr->e_shnum; i++)
1596 if (!sect_empty(&info->sechdrs[i]) &&
1597 (info->sechdrs[i].sh_type == SHT_NOTE))
Roland McGrath6d760132007-10-16 23:26:40 -07001598 ++notes;
1599
1600 if (notes == 0)
1601 return;
1602
Kees Cookacafe7e2018-05-08 13:45:50 -07001603 notes_attrs = kzalloc(struct_size(notes_attrs, attrs, notes),
Roland McGrath6d760132007-10-16 23:26:40 -07001604 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 del_usage_links(struct module *mod)
1669{
1670#ifdef CONFIG_MODULE_UNLOAD
1671 struct module_use *use;
1672
Rusty Russell75676502010-06-05 11:17:36 -06001673 mutex_lock(&module_mutex);
Rusty Russell80a3d1b2010-06-05 11:17:36 -06001674 list_for_each_entry(use, &mod->target_list, target_list)
1675 sysfs_remove_link(use->target->holders_dir, mod->name);
Rusty Russell75676502010-06-05 11:17:36 -06001676 mutex_unlock(&module_mutex);
Rusty Russell80a3d1b2010-06-05 11:17:36 -06001677#endif
1678}
1679
Corentin Labbe1ba5c082017-06-06 14:17:39 +02001680static int add_usage_links(struct module *mod)
1681{
1682 int ret = 0;
1683#ifdef CONFIG_MODULE_UNLOAD
1684 struct module_use *use;
1685
1686 mutex_lock(&module_mutex);
1687 list_for_each_entry(use, &mod->target_list, target_list) {
1688 ret = sysfs_create_link(use->target->holders_dir,
1689 &mod->mkobj.kobj, mod->name);
1690 if (ret)
1691 break;
1692 }
1693 mutex_unlock(&module_mutex);
1694 if (ret)
1695 del_usage_links(mod);
1696#endif
1697 return ret;
1698}
1699
YueHaibingbc6f2a72019-06-11 23:00:07 +08001700static void module_remove_modinfo_attrs(struct module *mod, int end);
1701
Rusty Russell6407ebb22010-06-05 11:17:36 -06001702static int module_add_modinfo_attrs(struct module *mod)
Matt Domschc988d2b2005-06-23 22:05:15 -07001703{
1704 struct module_attribute *attr;
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001705 struct module_attribute *temp_attr;
Matt Domschc988d2b2005-06-23 22:05:15 -07001706 int error = 0;
1707 int i;
1708
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001709 mod->modinfo_attrs = kzalloc((sizeof(struct module_attribute) *
1710 (ARRAY_SIZE(modinfo_attrs) + 1)),
1711 GFP_KERNEL);
1712 if (!mod->modinfo_attrs)
1713 return -ENOMEM;
1714
1715 temp_attr = mod->modinfo_attrs;
YueHaibingbc6f2a72019-06-11 23:00:07 +08001716 for (i = 0; (attr = modinfo_attrs[i]); i++) {
Rusty Russellc75b5902016-04-12 05:03:09 +09301717 if (!attr->test || attr->test(mod)) {
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001718 memcpy(temp_attr, attr, sizeof(*temp_attr));
Eric W. Biederman361795b2010-02-12 13:41:56 -08001719 sysfs_attr_init(&temp_attr->attr);
Ionut Alexa6da0b562014-11-10 09:31:29 +10301720 error = sysfs_create_file(&mod->mkobj.kobj,
1721 &temp_attr->attr);
YueHaibingbc6f2a72019-06-11 23:00:07 +08001722 if (error)
1723 goto error_out;
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001724 ++temp_attr;
1725 }
Matt Domschc988d2b2005-06-23 22:05:15 -07001726 }
YueHaibingbc6f2a72019-06-11 23:00:07 +08001727
1728 return 0;
1729
1730error_out:
1731 if (i > 0)
1732 module_remove_modinfo_attrs(mod, --i);
Matt Domschc988d2b2005-06-23 22:05:15 -07001733 return error;
1734}
1735
YueHaibingbc6f2a72019-06-11 23:00:07 +08001736static void module_remove_modinfo_attrs(struct module *mod, int end)
Matt Domschc988d2b2005-06-23 22:05:15 -07001737{
1738 struct module_attribute *attr;
1739 int i;
1740
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001741 for (i = 0; (attr = &mod->modinfo_attrs[i]); i++) {
YueHaibingbc6f2a72019-06-11 23:00:07 +08001742 if (end >= 0 && i > end)
1743 break;
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001744 /* pick a field to test for end of list */
1745 if (!attr->attr.name)
1746 break;
Ionut Alexa6da0b562014-11-10 09:31:29 +10301747 sysfs_remove_file(&mod->mkobj.kobj, &attr->attr);
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001748 if (attr->free)
1749 attr->free(mod);
Matt Domschc988d2b2005-06-23 22:05:15 -07001750 }
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001751 kfree(mod->modinfo_attrs);
Matt Domschc988d2b2005-06-23 22:05:15 -07001752}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753
Li Zhong942e4432013-09-03 16:33:57 +09301754static void mod_kobject_put(struct module *mod)
1755{
1756 DECLARE_COMPLETION_ONSTACK(c);
1757 mod->mkobj.kobj_completion = &c;
1758 kobject_put(&mod->mkobj.kobj);
1759 wait_for_completion(&c);
1760}
1761
Rusty Russell6407ebb22010-06-05 11:17:36 -06001762static int mod_sysfs_init(struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763{
1764 int err;
Greg Kroah-Hartman6494a932008-01-27 15:38:40 -08001765 struct kobject *kobj;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766
Greg Kroah-Hartman823bccf2007-04-13 13:15:19 -07001767 if (!module_sysfs_initialized) {
Andrew Mortonbddb12b2013-11-12 15:11:28 -08001768 pr_err("%s: module sysfs not initialized\n", mod->name);
Ed Swierk1cc5f712006-09-25 16:25:36 -07001769 err = -EINVAL;
1770 goto out;
1771 }
Greg Kroah-Hartman6494a932008-01-27 15:38:40 -08001772
1773 kobj = kset_find_obj(module_kset, mod->name);
1774 if (kobj) {
Andrew Mortonbddb12b2013-11-12 15:11:28 -08001775 pr_err("%s: module is already loaded\n", mod->name);
Greg Kroah-Hartman6494a932008-01-27 15:38:40 -08001776 kobject_put(kobj);
1777 err = -EINVAL;
1778 goto out;
1779 }
1780
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781 mod->mkobj.mod = mod;
Kay Sieverse17e0f52006-11-24 12:15:25 +01001782
Greg Kroah-Hartmanac3c8142007-12-17 23:05:35 -07001783 memset(&mod->mkobj.kobj, 0, sizeof(mod->mkobj.kobj));
1784 mod->mkobj.kobj.kset = module_kset;
1785 err = kobject_init_and_add(&mod->mkobj.kobj, &module_ktype, NULL,
1786 "%s", mod->name);
1787 if (err)
Li Zhong942e4432013-09-03 16:33:57 +09301788 mod_kobject_put(mod);
Kay Sievers270a6c42007-01-18 13:26:15 +01001789
Kay Sievers97c146ef2007-11-29 23:46:11 +01001790 /* delay uevent until full sysfs population */
Kay Sievers270a6c42007-01-18 13:26:15 +01001791out:
1792 return err;
1793}
1794
Rusty Russell6407ebb22010-06-05 11:17:36 -06001795static int mod_sysfs_setup(struct module *mod,
Rusty Russell8f6d0372010-08-05 12:59:09 -06001796 const struct load_info *info,
Kay Sievers270a6c42007-01-18 13:26:15 +01001797 struct kernel_param *kparam,
1798 unsigned int num_params)
1799{
1800 int err;
1801
Rusty Russell80a3d1b2010-06-05 11:17:36 -06001802 err = mod_sysfs_init(mod);
1803 if (err)
1804 goto out;
1805
Greg Kroah-Hartman4ff6abf2007-11-05 22:24:43 -08001806 mod->holders_dir = kobject_create_and_add("holders", &mod->mkobj.kobj);
Akinobu Mita240936e2007-04-26 00:12:09 -07001807 if (!mod->holders_dir) {
1808 err = -ENOMEM;
Kay Sievers270a6c42007-01-18 13:26:15 +01001809 goto out_unreg;
Akinobu Mita240936e2007-04-26 00:12:09 -07001810 }
Kay Sievers270a6c42007-01-18 13:26:15 +01001811
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812 err = module_param_sysfs_setup(mod, kparam, num_params);
1813 if (err)
Kay Sievers270a6c42007-01-18 13:26:15 +01001814 goto out_unreg_holders;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815
Matt Domschc988d2b2005-06-23 22:05:15 -07001816 err = module_add_modinfo_attrs(mod);
1817 if (err)
Kay Sieverse17e0f52006-11-24 12:15:25 +01001818 goto out_unreg_param;
Matt Domschc988d2b2005-06-23 22:05:15 -07001819
Corentin Labbe1ba5c082017-06-06 14:17:39 +02001820 err = add_usage_links(mod);
1821 if (err)
1822 goto out_unreg_modinfo_attrs;
1823
Rusty Russell8f6d0372010-08-05 12:59:09 -06001824 add_sect_attrs(mod, info);
1825 add_notes_attrs(mod, info);
Rusty Russell80a3d1b2010-06-05 11:17:36 -06001826
Kay Sieverse17e0f52006-11-24 12:15:25 +01001827 kobject_uevent(&mod->mkobj.kobj, KOBJ_ADD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828 return 0;
1829
Corentin Labbe1ba5c082017-06-06 14:17:39 +02001830out_unreg_modinfo_attrs:
YueHaibingbc6f2a72019-06-11 23:00:07 +08001831 module_remove_modinfo_attrs(mod, -1);
Kay Sieverse17e0f52006-11-24 12:15:25 +01001832out_unreg_param:
1833 module_param_sysfs_remove(mod);
Kay Sievers270a6c42007-01-18 13:26:15 +01001834out_unreg_holders:
Greg Kroah-Hartman78a2d902007-12-20 08:13:05 -08001835 kobject_put(mod->holders_dir);
Kay Sievers270a6c42007-01-18 13:26:15 +01001836out_unreg:
Li Zhong942e4432013-09-03 16:33:57 +09301837 mod_kobject_put(mod);
Rusty Russell80a3d1b2010-06-05 11:17:36 -06001838out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839 return err;
1840}
Denis V. Lunev34e4e2f2008-05-20 13:59:48 +04001841
1842static void mod_sysfs_fini(struct module *mod)
1843{
Rusty Russell8f6d0372010-08-05 12:59:09 -06001844 remove_notes_attrs(mod);
1845 remove_sect_attrs(mod);
Li Zhong942e4432013-09-03 16:33:57 +09301846 mod_kobject_put(mod);
Denis V. Lunev34e4e2f2008-05-20 13:59:48 +04001847}
1848
Rusty Russellcf2fde72015-06-26 06:44:38 +09301849static void init_param_lock(struct module *mod)
1850{
1851 mutex_init(&mod->param_lock);
1852}
Rusty Russell8f6d0372010-08-05 12:59:09 -06001853#else /* !CONFIG_SYSFS */
Denis V. Lunev34e4e2f2008-05-20 13:59:48 +04001854
Rusty Russell8f6d0372010-08-05 12:59:09 -06001855static int mod_sysfs_setup(struct module *mod,
1856 const struct load_info *info,
Rusty Russell6407ebb22010-06-05 11:17:36 -06001857 struct kernel_param *kparam,
1858 unsigned int num_params)
1859{
1860 return 0;
1861}
1862
Denis V. Lunev34e4e2f2008-05-20 13:59:48 +04001863static void mod_sysfs_fini(struct module *mod)
1864{
1865}
1866
YueHaibingbc6f2a72019-06-11 23:00:07 +08001867static void module_remove_modinfo_attrs(struct module *mod, int end)
Rusty Russell36b03602010-08-05 12:59:09 -06001868{
1869}
1870
Rusty Russell80a3d1b2010-06-05 11:17:36 -06001871static void del_usage_links(struct module *mod)
1872{
1873}
1874
Rusty Russellcf2fde72015-06-26 06:44:38 +09301875static void init_param_lock(struct module *mod)
1876{
1877}
Denis V. Lunev34e4e2f2008-05-20 13:59:48 +04001878#endif /* CONFIG_SYSFS */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879
Rusty Russell36b03602010-08-05 12:59:09 -06001880static void mod_sysfs_teardown(struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881{
Rusty Russell80a3d1b2010-06-05 11:17:36 -06001882 del_usage_links(mod);
YueHaibingbc6f2a72019-06-11 23:00:07 +08001883 module_remove_modinfo_attrs(mod, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884 module_param_sysfs_remove(mod);
Greg Kroah-Hartman78a2d902007-12-20 08:13:05 -08001885 kobject_put(mod->mkobj.drivers_dir);
1886 kobject_put(mod->holders_dir);
Denis V. Lunev34e4e2f2008-05-20 13:59:48 +04001887 mod_sysfs_fini(mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888}
1889
Yang Yingliang93651f82019-06-25 17:40:28 +08001890#ifdef CONFIG_ARCH_HAS_STRICT_MODULE_RWX
matthieu castet84e1c6b2010-11-16 22:35:16 +01001891/*
1892 * LKM RO/NX protection: protect module's text/ro-data
1893 * from modification and any data from execution.
Rusty Russell85c898d2015-11-26 09:45:08 +10301894 *
1895 * General layout of module is:
Jessica Yu444d13f2016-07-27 12:06:21 +09301896 * [text] [read-only-data] [ro-after-init] [writable data]
1897 * text_size -----^ ^ ^ ^
1898 * ro_size ------------------------| | |
1899 * ro_after_init_size -----------------------------| |
1900 * size -----------------------------------------------------------|
Rusty Russell85c898d2015-11-26 09:45:08 +10301901 *
1902 * These values are always page-aligned (as is base)
matthieu castet84e1c6b2010-11-16 22:35:16 +01001903 */
Rusty Russell85c898d2015-11-26 09:45:08 +10301904static void frob_text(const struct module_layout *layout,
1905 int (*set_memory)(unsigned long start, int num_pages))
matthieu castet84e1c6b2010-11-16 22:35:16 +01001906{
Rusty Russell85c898d2015-11-26 09:45:08 +10301907 BUG_ON((unsigned long)layout->base & (PAGE_SIZE-1));
1908 BUG_ON((unsigned long)layout->text_size & (PAGE_SIZE-1));
1909 set_memory((unsigned long)layout->base,
1910 layout->text_size >> PAGE_SHIFT);
matthieu castet84e1c6b2010-11-16 22:35:16 +01001911}
1912
Yang Yingliang93651f82019-06-25 17:40:28 +08001913#ifdef CONFIG_STRICT_MODULE_RWX
Rusty Russell85c898d2015-11-26 09:45:08 +10301914static void frob_rodata(const struct module_layout *layout,
1915 int (*set_memory)(unsigned long start, int num_pages))
matthieu castet84e1c6b2010-11-16 22:35:16 +01001916{
Rusty Russell85c898d2015-11-26 09:45:08 +10301917 BUG_ON((unsigned long)layout->base & (PAGE_SIZE-1));
1918 BUG_ON((unsigned long)layout->text_size & (PAGE_SIZE-1));
1919 BUG_ON((unsigned long)layout->ro_size & (PAGE_SIZE-1));
1920 set_memory((unsigned long)layout->base + layout->text_size,
1921 (layout->ro_size - layout->text_size) >> PAGE_SHIFT);
matthieu castet84e1c6b2010-11-16 22:35:16 +01001922}
1923
Jessica Yu444d13f2016-07-27 12:06:21 +09301924static void frob_ro_after_init(const struct module_layout *layout,
1925 int (*set_memory)(unsigned long start, int num_pages))
1926{
1927 BUG_ON((unsigned long)layout->base & (PAGE_SIZE-1));
1928 BUG_ON((unsigned long)layout->ro_size & (PAGE_SIZE-1));
1929 BUG_ON((unsigned long)layout->ro_after_init_size & (PAGE_SIZE-1));
1930 set_memory((unsigned long)layout->base + layout->ro_size,
1931 (layout->ro_after_init_size - layout->ro_size) >> PAGE_SHIFT);
1932}
1933
Rusty Russell85c898d2015-11-26 09:45:08 +10301934static void frob_writable_data(const struct module_layout *layout,
1935 int (*set_memory)(unsigned long start, int num_pages))
matthieu castet84e1c6b2010-11-16 22:35:16 +01001936{
Rusty Russell85c898d2015-11-26 09:45:08 +10301937 BUG_ON((unsigned long)layout->base & (PAGE_SIZE-1));
Jessica Yu444d13f2016-07-27 12:06:21 +09301938 BUG_ON((unsigned long)layout->ro_after_init_size & (PAGE_SIZE-1));
Rusty Russell85c898d2015-11-26 09:45:08 +10301939 BUG_ON((unsigned long)layout->size & (PAGE_SIZE-1));
Jessica Yu444d13f2016-07-27 12:06:21 +09301940 set_memory((unsigned long)layout->base + layout->ro_after_init_size,
1941 (layout->size - layout->ro_after_init_size) >> PAGE_SHIFT);
Jan Glauber01526ed2011-05-19 16:55:26 -06001942}
1943
Rusty Russell85c898d2015-11-26 09:45:08 +10301944/* livepatching wants to disable read-only so it can frob module. */
1945void module_disable_ro(const struct module *mod)
Jan Glauber01526ed2011-05-19 16:55:26 -06001946{
AKASHI Takahiro39290b32016-11-14 15:15:05 +09001947 if (!rodata_enabled)
1948 return;
1949
Rusty Russell85c898d2015-11-26 09:45:08 +10301950 frob_text(&mod->core_layout, set_memory_rw);
1951 frob_rodata(&mod->core_layout, set_memory_rw);
Jessica Yu444d13f2016-07-27 12:06:21 +09301952 frob_ro_after_init(&mod->core_layout, set_memory_rw);
Rusty Russell85c898d2015-11-26 09:45:08 +10301953 frob_text(&mod->init_layout, set_memory_rw);
1954 frob_rodata(&mod->init_layout, set_memory_rw);
Josh Poimboeuf20ef10c2015-11-26 09:42:08 +10301955}
1956
Jessica Yu444d13f2016-07-27 12:06:21 +09301957void module_enable_ro(const struct module *mod, bool after_init)
Josh Poimboeuf20ef10c2015-11-26 09:42:08 +10301958{
AKASHI Takahiro39290b32016-11-14 15:15:05 +09001959 if (!rodata_enabled)
1960 return;
1961
Rick Edgecombe1a7b7d92019-04-25 17:11:37 -07001962 set_vm_flush_reset_perms(mod->core_layout.base);
1963 set_vm_flush_reset_perms(mod->init_layout.base);
Rusty Russell85c898d2015-11-26 09:45:08 +10301964 frob_text(&mod->core_layout, set_memory_ro);
Nadav Amitf2c65fb2019-04-25 17:11:31 -07001965
Rusty Russell85c898d2015-11-26 09:45:08 +10301966 frob_rodata(&mod->core_layout, set_memory_ro);
1967 frob_text(&mod->init_layout, set_memory_ro);
1968 frob_rodata(&mod->init_layout, set_memory_ro);
Jessica Yu444d13f2016-07-27 12:06:21 +09301969
1970 if (after_init)
1971 frob_ro_after_init(&mod->core_layout, set_memory_ro);
Jan Glauber01526ed2011-05-19 16:55:26 -06001972}
1973
Rusty Russell85c898d2015-11-26 09:45:08 +10301974static void module_enable_nx(const struct module *mod)
Jan Glauber01526ed2011-05-19 16:55:26 -06001975{
Rusty Russell85c898d2015-11-26 09:45:08 +10301976 frob_rodata(&mod->core_layout, set_memory_nx);
Jessica Yu444d13f2016-07-27 12:06:21 +09301977 frob_ro_after_init(&mod->core_layout, set_memory_nx);
Rusty Russell85c898d2015-11-26 09:45:08 +10301978 frob_writable_data(&mod->core_layout, set_memory_nx);
1979 frob_rodata(&mod->init_layout, set_memory_nx);
1980 frob_writable_data(&mod->init_layout, set_memory_nx);
1981}
1982
matthieu castet84e1c6b2010-11-16 22:35:16 +01001983/* Iterate through all modules and set each module's text as RW */
Daniel J Blueman5d05c702011-03-08 22:01:47 +08001984void set_all_modules_text_rw(void)
matthieu castet84e1c6b2010-11-16 22:35:16 +01001985{
1986 struct module *mod;
1987
AKASHI Takahiro39290b32016-11-14 15:15:05 +09001988 if (!rodata_enabled)
1989 return;
1990
matthieu castet84e1c6b2010-11-16 22:35:16 +01001991 mutex_lock(&module_mutex);
1992 list_for_each_entry_rcu(mod, &modules, list) {
Rusty Russell0d21b0e2013-01-12 11:38:44 +10301993 if (mod->state == MODULE_STATE_UNFORMED)
1994 continue;
Rusty Russell85c898d2015-11-26 09:45:08 +10301995
1996 frob_text(&mod->core_layout, set_memory_rw);
1997 frob_text(&mod->init_layout, set_memory_rw);
matthieu castet84e1c6b2010-11-16 22:35:16 +01001998 }
1999 mutex_unlock(&module_mutex);
2000}
2001
2002/* Iterate through all modules and set each module's text as RO */
Daniel J Blueman5d05c702011-03-08 22:01:47 +08002003void set_all_modules_text_ro(void)
matthieu castet84e1c6b2010-11-16 22:35:16 +01002004{
2005 struct module *mod;
2006
AKASHI Takahiro39290b32016-11-14 15:15:05 +09002007 if (!rodata_enabled)
2008 return;
2009
matthieu castet84e1c6b2010-11-16 22:35:16 +01002010 mutex_lock(&module_mutex);
2011 list_for_each_entry_rcu(mod, &modules, list) {
Aaron Tomlin905dd707f2016-10-27 10:36:06 +01002012 /*
2013 * Ignore going modules since it's possible that ro
2014 * protection has already been disabled, otherwise we'll
2015 * run into protection faults at module deallocation.
2016 */
2017 if (mod->state == MODULE_STATE_UNFORMED ||
2018 mod->state == MODULE_STATE_GOING)
Rusty Russell0d21b0e2013-01-12 11:38:44 +10302019 continue;
Rusty Russell85c898d2015-11-26 09:45:08 +10302020
2021 frob_text(&mod->core_layout, set_memory_ro);
2022 frob_text(&mod->init_layout, set_memory_ro);
matthieu castet84e1c6b2010-11-16 22:35:16 +01002023 }
2024 mutex_unlock(&module_mutex);
2025}
Yang Yingliang93651f82019-06-25 17:40:28 +08002026#else /* !CONFIG_STRICT_MODULE_RWX */
Rusty Russell85c898d2015-11-26 09:45:08 +10302027static void module_enable_nx(const struct module *mod) { }
Yang Yingliang93651f82019-06-25 17:40:28 +08002028#endif /* CONFIG_STRICT_MODULE_RWX */
Yang Yingliang2eef1392019-06-20 10:18:14 +08002029static void module_enable_x(const struct module *mod)
2030{
2031 frob_text(&mod->core_layout, set_memory_x);
2032 frob_text(&mod->init_layout, set_memory_x);
2033}
Yang Yingliang93651f82019-06-25 17:40:28 +08002034#else /* !CONFIG_ARCH_HAS_STRICT_MODULE_RWX */
2035static void module_enable_nx(const struct module *mod) { }
2036static void module_enable_x(const struct module *mod) { }
2037#endif /* CONFIG_ARCH_HAS_STRICT_MODULE_RWX */
2038
matthieu castet84e1c6b2010-11-16 22:35:16 +01002039
Jessica Yu1ce15ef2016-03-22 20:03:16 -04002040#ifdef CONFIG_LIVEPATCH
2041/*
2042 * Persist Elf information about a module. Copy the Elf header,
2043 * section header table, section string table, and symtab section
2044 * index from info to mod->klp_info.
2045 */
2046static int copy_module_elf(struct module *mod, struct load_info *info)
2047{
2048 unsigned int size, symndx;
2049 int ret;
2050
2051 size = sizeof(*mod->klp_info);
2052 mod->klp_info = kmalloc(size, GFP_KERNEL);
2053 if (mod->klp_info == NULL)
2054 return -ENOMEM;
2055
2056 /* Elf header */
2057 size = sizeof(mod->klp_info->hdr);
2058 memcpy(&mod->klp_info->hdr, info->hdr, size);
2059
2060 /* Elf section header table */
2061 size = sizeof(*info->sechdrs) * info->hdr->e_shnum;
zhong jiang9be936f2018-08-01 00:56:17 +08002062 mod->klp_info->sechdrs = kmemdup(info->sechdrs, size, GFP_KERNEL);
Jessica Yu1ce15ef2016-03-22 20:03:16 -04002063 if (mod->klp_info->sechdrs == NULL) {
2064 ret = -ENOMEM;
2065 goto free_info;
2066 }
Jessica Yu1ce15ef2016-03-22 20:03:16 -04002067
2068 /* Elf section name string table */
2069 size = info->sechdrs[info->hdr->e_shstrndx].sh_size;
zhong jiang9be936f2018-08-01 00:56:17 +08002070 mod->klp_info->secstrings = kmemdup(info->secstrings, size, GFP_KERNEL);
Jessica Yu1ce15ef2016-03-22 20:03:16 -04002071 if (mod->klp_info->secstrings == NULL) {
2072 ret = -ENOMEM;
2073 goto free_sechdrs;
2074 }
Jessica Yu1ce15ef2016-03-22 20:03:16 -04002075
2076 /* Elf symbol section index */
2077 symndx = info->index.sym;
2078 mod->klp_info->symndx = symndx;
2079
2080 /*
2081 * For livepatch modules, core_kallsyms.symtab is a complete
2082 * copy of the original symbol table. Adjust sh_addr to point
2083 * to core_kallsyms.symtab since the copy of the symtab in module
2084 * init memory is freed at the end of do_init_module().
2085 */
2086 mod->klp_info->sechdrs[symndx].sh_addr = \
2087 (unsigned long) mod->core_kallsyms.symtab;
2088
2089 return 0;
2090
2091free_sechdrs:
2092 kfree(mod->klp_info->sechdrs);
2093free_info:
2094 kfree(mod->klp_info);
2095 return ret;
2096}
2097
2098static void free_module_elf(struct module *mod)
2099{
2100 kfree(mod->klp_info->sechdrs);
2101 kfree(mod->klp_info->secstrings);
2102 kfree(mod->klp_info);
2103}
2104#else /* !CONFIG_LIVEPATCH */
2105static int copy_module_elf(struct module *mod, struct load_info *info)
2106{
2107 return 0;
2108}
2109
2110static void free_module_elf(struct module *mod)
2111{
2112}
2113#endif /* CONFIG_LIVEPATCH */
2114
Rusty Russellbe1f2212015-01-20 09:07:05 +10302115void __weak module_memfree(void *module_region)
Jonas Bonn74e08fc2011-06-30 21:22:11 +02002116{
Rick Edgecombe1a7b7d92019-04-25 17:11:37 -07002117 /*
2118 * This memory may be RO, and freeing RO memory in an interrupt is not
2119 * supported by vmalloc.
2120 */
2121 WARN_ON(in_interrupt());
Jonas Bonn74e08fc2011-06-30 21:22:11 +02002122 vfree(module_region);
2123}
2124
2125void __weak module_arch_cleanup(struct module *mod)
2126{
2127}
2128
Rusty Russelld453cde2015-01-20 09:07:04 +10302129void __weak module_arch_freeing_init(struct module *mod)
2130{
2131}
2132
Rusty Russell75676502010-06-05 11:17:36 -06002133/* Free a module, remove from lists, etc. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002134static void free_module(struct module *mod)
2135{
Li Zefan7ead8b82009-08-17 16:56:28 +08002136 trace_module_free(mod);
2137
Rusty Russell36b03602010-08-05 12:59:09 -06002138 mod_sysfs_teardown(mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139
Rusty Russell944a1fa2013-04-17 13:20:03 +09302140 /* We leave it in list to prevent duplicate loads, but make sure
2141 * that noone uses it while it's being deconstructed. */
Prarit Bhargavad3051b42014-10-14 02:51:39 +10302142 mutex_lock(&module_mutex);
Rusty Russell944a1fa2013-04-17 13:20:03 +09302143 mod->state = MODULE_STATE_UNFORMED;
Prarit Bhargavad3051b42014-10-14 02:51:39 +10302144 mutex_unlock(&module_mutex);
Rusty Russell944a1fa2013-04-17 13:20:03 +09302145
Jason Baronb82bab4b2010-07-27 13:18:01 -07002146 /* Remove dynamic debug info */
2147 ddebug_remove_module(mod->name);
2148
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149 /* Arch-specific cleanup. */
2150 module_arch_cleanup(mod);
2151
2152 /* Module unload stuff */
2153 module_unload_free(mod);
2154
Rusty Russelle180a6b2009-03-31 13:05:29 -06002155 /* Free any allocated parameters. */
2156 destroy_params(mod->kp, mod->num_kp);
2157
Jessica Yu1ce15ef2016-03-22 20:03:16 -04002158 if (is_livepatch_module(mod))
2159 free_module_elf(mod);
2160
Rusty Russell944a1fa2013-04-17 13:20:03 +09302161 /* Now we can delete it from the lists */
2162 mutex_lock(&module_mutex);
Masami Hiramatsu461e34a2014-11-10 09:27:29 +10302163 /* Unlink carefully: kallsyms could be walking list. */
2164 list_del_rcu(&mod->list);
Peter Zijlstra93c2e102015-05-27 11:09:37 +09302165 mod_tree_remove(mod);
Masami Hiramatsu0286b5e2014-11-10 09:28:29 +10302166 /* Remove this module from bug list, this uses list_del_rcu */
Masami Hiramatsu461e34a2014-11-10 09:27:29 +10302167 module_bug_cleanup(mod);
Peter Zijlstra0be964b2015-05-27 11:09:35 +09302168 /* Wait for RCU-sched synchronizing before releasing mod->list and buglist. */
Paul E. McKenneycb2f5532018-11-06 19:17:01 -08002169 synchronize_rcu();
Rusty Russell944a1fa2013-04-17 13:20:03 +09302170 mutex_unlock(&module_mutex);
2171
Rusty Russell85c898d2015-11-26 09:45:08 +10302172 /* This may be empty, but that's OK */
Rusty Russelld453cde2015-01-20 09:07:04 +10302173 module_arch_freeing_init(mod);
Rusty Russell7523e4d2015-11-26 09:44:08 +10302174 module_memfree(mod->init_layout.base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175 kfree(mod->args);
Tejun Heo259354d2010-03-10 18:56:10 +09002176 percpu_modfree(mod);
Rusty Russell9f85a4b2010-08-05 12:59:04 -06002177
Peter Zijlstra35a93932015-02-26 16:23:11 +01002178 /* Free lock-classes; relies on the preceding sync_rcu(). */
Rusty Russell7523e4d2015-11-26 09:44:08 +10302179 lockdep_free_key_range(mod->core_layout.base, mod->core_layout.size);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002180
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181 /* Finally, free the core (containing the module structure) */
Rusty Russell7523e4d2015-11-26 09:44:08 +10302182 module_memfree(mod->core_layout.base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183}
2184
2185void *__symbol_get(const char *symbol)
2186{
2187 struct module *owner;
Tim Abbott414fd312008-12-05 19:03:56 -05002188 const struct kernel_symbol *sym;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189
Rusty Russell24da1cb2007-07-15 23:41:46 -07002190 preempt_disable();
Tim Abbott414fd312008-12-05 19:03:56 -05002191 sym = find_symbol(symbol, &owner, NULL, true, true);
2192 if (sym && strong_try_module_get(owner))
2193 sym = NULL;
Rusty Russell24da1cb2007-07-15 23:41:46 -07002194 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195
Ard Biesheuvel7290d582018-08-21 21:56:09 -07002196 return sym ? (void *)kernel_symbol_value(sym) : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197}
2198EXPORT_SYMBOL_GPL(__symbol_get);
2199
Ashutosh Naikeea8b542006-01-08 01:04:25 -08002200/*
2201 * Ensure that an exported symbol [global namespace] does not already exist
Robert P. J. Day02a3e592007-05-09 07:26:28 +02002202 * in the kernel or in some other module's exported symbol table.
Rusty Russellbe593f42010-06-05 11:17:37 -06002203 *
2204 * You must hold the module_mutex.
Ashutosh Naikeea8b542006-01-08 01:04:25 -08002205 */
Jessica Yu2d25bc52018-11-19 17:43:58 +01002206static int verify_exported_symbols(struct module *mod)
Ashutosh Naikeea8b542006-01-08 01:04:25 -08002207{
Rusty Russellb2111042008-05-01 21:15:00 -05002208 unsigned int i;
Ashutosh Naikeea8b542006-01-08 01:04:25 -08002209 struct module *owner;
Rusty Russellb2111042008-05-01 21:15:00 -05002210 const struct kernel_symbol *s;
2211 struct {
2212 const struct kernel_symbol *sym;
2213 unsigned int num;
2214 } arr[] = {
2215 { mod->syms, mod->num_syms },
2216 { mod->gpl_syms, mod->num_gpl_syms },
2217 { mod->gpl_future_syms, mod->num_gpl_future_syms },
Denys Vlasenkof7f5b672008-07-22 19:24:26 -05002218#ifdef CONFIG_UNUSED_SYMBOLS
Rusty Russellb2111042008-05-01 21:15:00 -05002219 { mod->unused_syms, mod->num_unused_syms },
2220 { mod->unused_gpl_syms, mod->num_unused_gpl_syms },
Denys Vlasenkof7f5b672008-07-22 19:24:26 -05002221#endif
Rusty Russellb2111042008-05-01 21:15:00 -05002222 };
Ashutosh Naikeea8b542006-01-08 01:04:25 -08002223
Rusty Russellb2111042008-05-01 21:15:00 -05002224 for (i = 0; i < ARRAY_SIZE(arr); i++) {
2225 for (s = arr[i].sym; s < arr[i].sym + arr[i].num; s++) {
Ard Biesheuvel7290d582018-08-21 21:56:09 -07002226 if (find_symbol(kernel_symbol_name(s), &owner, NULL,
2227 true, false)) {
Andrew Mortonbddb12b2013-11-12 15:11:28 -08002228 pr_err("%s: exports duplicate symbol %s"
Rusty Russellb2111042008-05-01 21:15:00 -05002229 " (owned by %s)\n",
Ard Biesheuvel7290d582018-08-21 21:56:09 -07002230 mod->name, kernel_symbol_name(s),
2231 module_name(owner));
Rusty Russellb2111042008-05-01 21:15:00 -05002232 return -ENOEXEC;
2233 }
Ashutosh Naikeea8b542006-01-08 01:04:25 -08002234 }
Rusty Russellb2111042008-05-01 21:15:00 -05002235 }
2236 return 0;
Ashutosh Naikeea8b542006-01-08 01:04:25 -08002237}
2238
Matti Linnanvuori9a4b9702007-11-08 08:37:38 -08002239/* Change all symbols so that st_value encodes the pointer directly. */
Rusty Russell49668682010-08-05 12:59:10 -06002240static int simplify_symbols(struct module *mod, const struct load_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002241{
Rusty Russell49668682010-08-05 12:59:10 -06002242 Elf_Shdr *symsec = &info->sechdrs[info->index.sym];
2243 Elf_Sym *sym = (void *)symsec->sh_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002244 unsigned long secbase;
Rusty Russell49668682010-08-05 12:59:10 -06002245 unsigned int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002246 int ret = 0;
Tim Abbott414fd312008-12-05 19:03:56 -05002247 const struct kernel_symbol *ksym;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002248
Rusty Russell49668682010-08-05 12:59:10 -06002249 for (i = 1; i < symsec->sh_size / sizeof(Elf_Sym); i++) {
2250 const char *name = info->strtab + sym[i].st_name;
2251
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252 switch (sym[i].st_shndx) {
2253 case SHN_COMMON:
Joe Mario80375982014-02-08 09:01:09 +01002254 /* Ignore common symbols */
2255 if (!strncmp(name, "__gnu_lto", 9))
2256 break;
2257
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258 /* We compiled with -fno-common. These are not
2259 supposed to happen. */
Jim Cromie5e124162011-12-06 12:11:31 -07002260 pr_debug("Common symbol: %s\n", name);
Ionut Alexa6da0b562014-11-10 09:31:29 +10302261 pr_warn("%s: please compile with -fno-common\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002262 mod->name);
2263 ret = -ENOEXEC;
2264 break;
2265
2266 case SHN_ABS:
2267 /* Don't need to do anything */
Jim Cromie5e124162011-12-06 12:11:31 -07002268 pr_debug("Absolute symbol: 0x%08lx\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002269 (long)sym[i].st_value);
2270 break;
2271
Jessica Yu1ce15ef2016-03-22 20:03:16 -04002272 case SHN_LIVEPATCH:
2273 /* Livepatch symbols are resolved by livepatch */
2274 break;
2275
Linus Torvalds1da177e2005-04-16 15:20:36 -07002276 case SHN_UNDEF:
Rusty Russell49668682010-08-05 12:59:10 -06002277 ksym = resolve_symbol_wait(mod, info, name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002278 /* Ok if resolved. */
Rusty Russell9bea7f22010-06-05 11:17:37 -06002279 if (ksym && !IS_ERR(ksym)) {
Ard Biesheuvel7290d582018-08-21 21:56:09 -07002280 sym[i].st_value = kernel_symbol_value(ksym);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002281 break;
Tim Abbott414fd312008-12-05 19:03:56 -05002282 }
2283
Linus Torvalds1da177e2005-04-16 15:20:36 -07002284 /* Ok if weak. */
Rusty Russell9bea7f22010-06-05 11:17:37 -06002285 if (!ksym && ELF_ST_BIND(sym[i].st_info) == STB_WEAK)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002286 break;
2287
Rusty Russell9bea7f22010-06-05 11:17:37 -06002288 ret = PTR_ERR(ksym) ?: -ENOENT;
Jason A. Donenfeld62267e02018-06-22 17:38:50 +02002289 pr_warn("%s: Unknown symbol %s (err %d)\n",
2290 mod->name, name, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002291 break;
2292
2293 default:
2294 /* Divert to percpu allocation if a percpu var. */
Rusty Russell49668682010-08-05 12:59:10 -06002295 if (sym[i].st_shndx == info->index.pcpu)
Tejun Heo259354d2010-03-10 18:56:10 +09002296 secbase = (unsigned long)mod_percpu(mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002297 else
Rusty Russell49668682010-08-05 12:59:10 -06002298 secbase = info->sechdrs[sym[i].st_shndx].sh_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002299 sym[i].st_value += secbase;
2300 break;
2301 }
2302 }
2303
2304 return ret;
2305}
2306
Rusty Russell49668682010-08-05 12:59:10 -06002307static int apply_relocations(struct module *mod, const struct load_info *info)
Rusty Russell22e268e2010-08-05 12:59:05 -06002308{
2309 unsigned int i;
2310 int err = 0;
2311
2312 /* Now do relocations. */
Rusty Russell49668682010-08-05 12:59:10 -06002313 for (i = 1; i < info->hdr->e_shnum; i++) {
2314 unsigned int infosec = info->sechdrs[i].sh_info;
Rusty Russell22e268e2010-08-05 12:59:05 -06002315
2316 /* Not a valid relocation section? */
Rusty Russell49668682010-08-05 12:59:10 -06002317 if (infosec >= info->hdr->e_shnum)
Rusty Russell22e268e2010-08-05 12:59:05 -06002318 continue;
2319
2320 /* Don't bother with non-allocated sections */
Rusty Russell49668682010-08-05 12:59:10 -06002321 if (!(info->sechdrs[infosec].sh_flags & SHF_ALLOC))
Rusty Russell22e268e2010-08-05 12:59:05 -06002322 continue;
2323
Jessica Yu1ce15ef2016-03-22 20:03:16 -04002324 /* Livepatch relocation sections are applied by livepatch */
2325 if (info->sechdrs[i].sh_flags & SHF_RELA_LIVEPATCH)
2326 continue;
2327
Rusty Russell49668682010-08-05 12:59:10 -06002328 if (info->sechdrs[i].sh_type == SHT_REL)
2329 err = apply_relocate(info->sechdrs, info->strtab,
2330 info->index.sym, i, mod);
2331 else if (info->sechdrs[i].sh_type == SHT_RELA)
2332 err = apply_relocate_add(info->sechdrs, info->strtab,
2333 info->index.sym, i, mod);
Rusty Russell22e268e2010-08-05 12:59:05 -06002334 if (err < 0)
2335 break;
2336 }
2337 return err;
2338}
2339
Helge Deller088af9a2008-12-31 12:31:18 +01002340/* Additional bytes needed by arch in front of individual sections */
2341unsigned int __weak arch_mod_section_prepend(struct module *mod,
2342 unsigned int section)
2343{
2344 /* default implementation just returns zero */
2345 return 0;
2346}
2347
Linus Torvalds1da177e2005-04-16 15:20:36 -07002348/* Update size with this section: return offset. */
Helge Deller088af9a2008-12-31 12:31:18 +01002349static long get_offset(struct module *mod, unsigned int *size,
2350 Elf_Shdr *sechdr, unsigned int section)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002351{
2352 long ret;
2353
Helge Deller088af9a2008-12-31 12:31:18 +01002354 *size += arch_mod_section_prepend(mod, section);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002355 ret = ALIGN(*size, sechdr->sh_addralign ?: 1);
2356 *size = ret + sechdr->sh_size;
2357 return ret;
2358}
2359
2360/* Lay out the SHF_ALLOC sections in a way not dissimilar to how ld
2361 might -- code, read-only data, read-write data, small data. Tally
2362 sizes, and place the offsets into sh_entsize fields: high bit means it
2363 belongs in init. */
Rusty Russell49668682010-08-05 12:59:10 -06002364static void layout_sections(struct module *mod, struct load_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002365{
2366 static unsigned long const masks[][2] = {
2367 /* NOTE: all executable code must be the first section
2368 * in this array; otherwise modify the text_size
2369 * finder in the two loops below */
2370 { SHF_EXECINSTR | SHF_ALLOC, ARCH_SHF_SMALL },
2371 { SHF_ALLOC, SHF_WRITE | ARCH_SHF_SMALL },
Jessica Yu444d13f2016-07-27 12:06:21 +09302372 { SHF_RO_AFTER_INIT | SHF_ALLOC, ARCH_SHF_SMALL },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002373 { SHF_WRITE | SHF_ALLOC, ARCH_SHF_SMALL },
2374 { ARCH_SHF_SMALL | SHF_ALLOC, 0 }
2375 };
2376 unsigned int m, i;
2377
Rusty Russell49668682010-08-05 12:59:10 -06002378 for (i = 0; i < info->hdr->e_shnum; i++)
2379 info->sechdrs[i].sh_entsize = ~0UL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002380
Jim Cromie5e124162011-12-06 12:11:31 -07002381 pr_debug("Core section allocation order:\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002382 for (m = 0; m < ARRAY_SIZE(masks); ++m) {
Rusty Russell49668682010-08-05 12:59:10 -06002383 for (i = 0; i < info->hdr->e_shnum; ++i) {
2384 Elf_Shdr *s = &info->sechdrs[i];
2385 const char *sname = info->secstrings + s->sh_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002386
2387 if ((s->sh_flags & masks[m][0]) != masks[m][0]
2388 || (s->sh_flags & masks[m][1])
2389 || s->sh_entsize != ~0UL
Rusty Russell49668682010-08-05 12:59:10 -06002390 || strstarts(sname, ".init"))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002391 continue;
Rusty Russell7523e4d2015-11-26 09:44:08 +10302392 s->sh_entsize = get_offset(mod, &mod->core_layout.size, s, i);
Jim Cromie5e124162011-12-06 12:11:31 -07002393 pr_debug("\t%s\n", sname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002394 }
matthieu castet84e1c6b2010-11-16 22:35:16 +01002395 switch (m) {
2396 case 0: /* executable */
Rusty Russell7523e4d2015-11-26 09:44:08 +10302397 mod->core_layout.size = debug_align(mod->core_layout.size);
2398 mod->core_layout.text_size = mod->core_layout.size;
matthieu castet84e1c6b2010-11-16 22:35:16 +01002399 break;
2400 case 1: /* RO: text and ro-data */
Rusty Russell7523e4d2015-11-26 09:44:08 +10302401 mod->core_layout.size = debug_align(mod->core_layout.size);
2402 mod->core_layout.ro_size = mod->core_layout.size;
matthieu castet84e1c6b2010-11-16 22:35:16 +01002403 break;
Jessica Yu444d13f2016-07-27 12:06:21 +09302404 case 2: /* RO after init */
2405 mod->core_layout.size = debug_align(mod->core_layout.size);
2406 mod->core_layout.ro_after_init_size = mod->core_layout.size;
2407 break;
2408 case 4: /* whole core */
Rusty Russell7523e4d2015-11-26 09:44:08 +10302409 mod->core_layout.size = debug_align(mod->core_layout.size);
matthieu castet84e1c6b2010-11-16 22:35:16 +01002410 break;
2411 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002412 }
2413
Jim Cromie5e124162011-12-06 12:11:31 -07002414 pr_debug("Init section allocation order:\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002415 for (m = 0; m < ARRAY_SIZE(masks); ++m) {
Rusty Russell49668682010-08-05 12:59:10 -06002416 for (i = 0; i < info->hdr->e_shnum; ++i) {
2417 Elf_Shdr *s = &info->sechdrs[i];
2418 const char *sname = info->secstrings + s->sh_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002419
2420 if ((s->sh_flags & masks[m][0]) != masks[m][0]
2421 || (s->sh_flags & masks[m][1])
2422 || s->sh_entsize != ~0UL
Rusty Russell49668682010-08-05 12:59:10 -06002423 || !strstarts(sname, ".init"))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002424 continue;
Rusty Russell7523e4d2015-11-26 09:44:08 +10302425 s->sh_entsize = (get_offset(mod, &mod->init_layout.size, s, i)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002426 | INIT_OFFSET_MASK);
Jim Cromie5e124162011-12-06 12:11:31 -07002427 pr_debug("\t%s\n", sname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002428 }
matthieu castet84e1c6b2010-11-16 22:35:16 +01002429 switch (m) {
2430 case 0: /* executable */
Rusty Russell7523e4d2015-11-26 09:44:08 +10302431 mod->init_layout.size = debug_align(mod->init_layout.size);
2432 mod->init_layout.text_size = mod->init_layout.size;
matthieu castet84e1c6b2010-11-16 22:35:16 +01002433 break;
2434 case 1: /* RO: text and ro-data */
Rusty Russell7523e4d2015-11-26 09:44:08 +10302435 mod->init_layout.size = debug_align(mod->init_layout.size);
2436 mod->init_layout.ro_size = mod->init_layout.size;
matthieu castet84e1c6b2010-11-16 22:35:16 +01002437 break;
Jessica Yu444d13f2016-07-27 12:06:21 +09302438 case 2:
2439 /*
2440 * RO after init doesn't apply to init_layout (only
2441 * core_layout), so it just takes the value of ro_size.
2442 */
2443 mod->init_layout.ro_after_init_size = mod->init_layout.ro_size;
2444 break;
2445 case 4: /* whole init */
Rusty Russell7523e4d2015-11-26 09:44:08 +10302446 mod->init_layout.size = debug_align(mod->init_layout.size);
matthieu castet84e1c6b2010-11-16 22:35:16 +01002447 break;
2448 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002449 }
2450}
2451
Linus Torvalds1da177e2005-04-16 15:20:36 -07002452static void set_license(struct module *mod, const char *license)
2453{
2454 if (!license)
2455 license = "unspecified";
2456
Florin Malitafa3ba2e82006-10-11 01:21:48 -07002457 if (!license_is_gpl_compatible(license)) {
Andi Kleen25ddbb12008-10-15 22:01:41 -07002458 if (!test_taint(TAINT_PROPRIETARY_MODULE))
Andrew Mortonbddb12b2013-11-12 15:11:28 -08002459 pr_warn("%s: module license '%s' taints kernel.\n",
2460 mod->name, license);
Rusty Russell373d4d02013-01-21 17:17:39 +10302461 add_taint_module(mod, TAINT_PROPRIETARY_MODULE,
2462 LOCKDEP_NOW_UNRELIABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002463 }
2464}
2465
2466/* Parse tag=value strings from .modinfo section */
2467static char *next_string(char *string, unsigned long *secsize)
2468{
2469 /* Skip non-zero chars */
2470 while (string[0]) {
2471 string++;
2472 if ((*secsize)-- <= 1)
2473 return NULL;
2474 }
2475
2476 /* Skip any zero padding. */
2477 while (!string[0]) {
2478 string++;
2479 if ((*secsize)-- <= 1)
2480 return NULL;
2481 }
2482 return string;
2483}
2484
Rusty Russell49668682010-08-05 12:59:10 -06002485static char *get_modinfo(struct load_info *info, const char *tag)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002486{
2487 char *p;
2488 unsigned int taglen = strlen(tag);
Rusty Russell49668682010-08-05 12:59:10 -06002489 Elf_Shdr *infosec = &info->sechdrs[info->index.info];
2490 unsigned long size = infosec->sh_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002491
Jessica Yu5fdc7db2018-06-22 14:00:01 +02002492 /*
2493 * get_modinfo() calls made before rewrite_section_headers()
2494 * must use sh_offset, as sh_addr isn't set!
2495 */
2496 for (p = (char *)info->hdr + infosec->sh_offset; p; p = next_string(p, &size)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002497 if (strncmp(p, tag, taglen) == 0 && p[taglen] == '=')
2498 return p + taglen + 1;
2499 }
2500 return NULL;
2501}
2502
Rusty Russell49668682010-08-05 12:59:10 -06002503static void setup_modinfo(struct module *mod, struct load_info *info)
Matt Domschc988d2b2005-06-23 22:05:15 -07002504{
2505 struct module_attribute *attr;
2506 int i;
2507
2508 for (i = 0; (attr = modinfo_attrs[i]); i++) {
2509 if (attr->setup)
Rusty Russell49668682010-08-05 12:59:10 -06002510 attr->setup(mod, get_modinfo(info, attr->attr.name));
Matt Domschc988d2b2005-06-23 22:05:15 -07002511 }
2512}
Matt Domschc988d2b2005-06-23 22:05:15 -07002513
Rusty Russella263f7762009-09-25 00:32:58 -06002514static void free_modinfo(struct module *mod)
2515{
2516 struct module_attribute *attr;
2517 int i;
2518
2519 for (i = 0; (attr = modinfo_attrs[i]); i++) {
2520 if (attr->free)
2521 attr->free(mod);
2522 }
2523}
2524
Linus Torvalds1da177e2005-04-16 15:20:36 -07002525#ifdef CONFIG_KALLSYMS
WANG Cong15bba372008-07-24 15:41:48 +01002526
Jessica Yu2d25bc52018-11-19 17:43:58 +01002527/* Lookup exported symbol in given range of kernel_symbols */
2528static const struct kernel_symbol *lookup_exported_symbol(const char *name,
2529 const struct kernel_symbol *start,
2530 const struct kernel_symbol *stop)
WANG Cong15bba372008-07-24 15:41:48 +01002531{
Alessio Igor Bogani9d634872011-05-18 22:35:59 +02002532 return bsearch(name, start, stop - start,
2533 sizeof(struct kernel_symbol), cmp_name);
WANG Cong15bba372008-07-24 15:41:48 +01002534}
2535
Tim Abbottca4787b2009-01-05 08:40:10 -06002536static int is_exported(const char *name, unsigned long value,
2537 const struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002538{
Tim Abbottca4787b2009-01-05 08:40:10 -06002539 const struct kernel_symbol *ks;
2540 if (!mod)
Jessica Yu2d25bc52018-11-19 17:43:58 +01002541 ks = lookup_exported_symbol(name, __start___ksymtab, __stop___ksymtab);
Sam Ravnborg3fd68052006-02-08 21:16:45 +01002542 else
Jessica Yu2d25bc52018-11-19 17:43:58 +01002543 ks = lookup_exported_symbol(name, mod->syms, mod->syms + mod->num_syms);
2544
Ard Biesheuvel7290d582018-08-21 21:56:09 -07002545 return ks != NULL && kernel_symbol_value(ks) == value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002546}
2547
2548/* As per nm */
Rusty Russelleded41c2010-08-05 12:59:07 -06002549static char elf_type(const Elf_Sym *sym, const struct load_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002550{
Rusty Russelleded41c2010-08-05 12:59:07 -06002551 const Elf_Shdr *sechdrs = info->sechdrs;
2552
Linus Torvalds1da177e2005-04-16 15:20:36 -07002553 if (ELF_ST_BIND(sym->st_info) == STB_WEAK) {
2554 if (ELF_ST_TYPE(sym->st_info) == STT_OBJECT)
2555 return 'v';
2556 else
2557 return 'w';
2558 }
2559 if (sym->st_shndx == SHN_UNDEF)
2560 return 'U';
Miroslav Benese0224412015-11-26 13:18:06 +10302561 if (sym->st_shndx == SHN_ABS || sym->st_shndx == info->index.pcpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002562 return 'a';
2563 if (sym->st_shndx >= SHN_LORESERVE)
2564 return '?';
2565 if (sechdrs[sym->st_shndx].sh_flags & SHF_EXECINSTR)
2566 return 't';
2567 if (sechdrs[sym->st_shndx].sh_flags & SHF_ALLOC
2568 && sechdrs[sym->st_shndx].sh_type != SHT_NOBITS) {
2569 if (!(sechdrs[sym->st_shndx].sh_flags & SHF_WRITE))
2570 return 'r';
2571 else if (sechdrs[sym->st_shndx].sh_flags & ARCH_SHF_SMALL)
2572 return 'g';
2573 else
2574 return 'd';
2575 }
2576 if (sechdrs[sym->st_shndx].sh_type == SHT_NOBITS) {
2577 if (sechdrs[sym->st_shndx].sh_flags & ARCH_SHF_SMALL)
2578 return 's';
2579 else
2580 return 'b';
2581 }
Rusty Russelleded41c2010-08-05 12:59:07 -06002582 if (strstarts(info->secstrings + sechdrs[sym->st_shndx].sh_name,
2583 ".debug")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002584 return 'n';
Rusty Russelleded41c2010-08-05 12:59:07 -06002585 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002586 return '?';
2587}
2588
Jan Beulich4a496222009-07-06 14:50:42 +01002589static bool is_core_symbol(const Elf_Sym *src, const Elf_Shdr *sechdrs,
Miroslav Benese0224412015-11-26 13:18:06 +10302590 unsigned int shnum, unsigned int pcpundx)
Jan Beulich4a496222009-07-06 14:50:42 +01002591{
2592 const Elf_Shdr *sec;
2593
2594 if (src->st_shndx == SHN_UNDEF
2595 || src->st_shndx >= shnum
2596 || !src->st_name)
2597 return false;
2598
Miroslav Benese0224412015-11-26 13:18:06 +10302599#ifdef CONFIG_KALLSYMS_ALL
2600 if (src->st_shndx == pcpundx)
2601 return true;
2602#endif
2603
Jan Beulich4a496222009-07-06 14:50:42 +01002604 sec = sechdrs + src->st_shndx;
2605 if (!(sec->sh_flags & SHF_ALLOC)
2606#ifndef CONFIG_KALLSYMS_ALL
2607 || !(sec->sh_flags & SHF_EXECINSTR)
2608#endif
2609 || (sec->sh_entsize & INIT_OFFSET_MASK))
2610 return false;
2611
2612 return true;
2613}
2614
Kevin Cernekee48fd1182012-01-13 09:32:14 +10302615/*
2616 * We only allocate and copy the strings needed by the parts of symtab
2617 * we keep. This is simple, but has the effect of making multiple
2618 * copies of duplicates. We could be more sophisticated, see
2619 * linux-kernel thread starting with
2620 * <73defb5e4bca04a6431392cc341112b1@localhost>.
2621 */
Rusty Russell49668682010-08-05 12:59:10 -06002622static void layout_symtab(struct module *mod, struct load_info *info)
Jan Beulich4a496222009-07-06 14:50:42 +01002623{
Rusty Russell49668682010-08-05 12:59:10 -06002624 Elf_Shdr *symsect = info->sechdrs + info->index.sym;
2625 Elf_Shdr *strsect = info->sechdrs + info->index.str;
Jan Beulich4a496222009-07-06 14:50:42 +01002626 const Elf_Sym *src;
Satoru Takeuchi54523ec2012-12-05 12:29:04 +10302627 unsigned int i, nsrc, ndst, strtab_size = 0;
Jan Beulich4a496222009-07-06 14:50:42 +01002628
2629 /* Put symbol section at end of init part of module. */
2630 symsect->sh_flags |= SHF_ALLOC;
Rusty Russell7523e4d2015-11-26 09:44:08 +10302631 symsect->sh_entsize = get_offset(mod, &mod->init_layout.size, symsect,
Rusty Russell49668682010-08-05 12:59:10 -06002632 info->index.sym) | INIT_OFFSET_MASK;
Jim Cromie5e124162011-12-06 12:11:31 -07002633 pr_debug("\t%s\n", info->secstrings + symsect->sh_name);
Jan Beulich4a496222009-07-06 14:50:42 +01002634
Rusty Russell49668682010-08-05 12:59:10 -06002635 src = (void *)info->hdr + symsect->sh_offset;
Jan Beulich4a496222009-07-06 14:50:42 +01002636 nsrc = symsect->sh_size / sizeof(*src);
Kevin Cernekee70b1e9162011-11-12 19:08:55 -08002637
Kevin Cernekee48fd1182012-01-13 09:32:14 +10302638 /* Compute total space required for the core symbols' strtab. */
Rusty Russell59ef28b2012-10-25 10:49:25 +10302639 for (ndst = i = 0; i < nsrc; i++) {
Jessica Yu1ce15ef2016-03-22 20:03:16 -04002640 if (i == 0 || is_livepatch_module(mod) ||
Miroslav Benese0224412015-11-26 13:18:06 +10302641 is_core_symbol(src+i, info->sechdrs, info->hdr->e_shnum,
2642 info->index.pcpu)) {
Rusty Russell59ef28b2012-10-25 10:49:25 +10302643 strtab_size += strlen(&info->strtab[src[i].st_name])+1;
Kevin Cernekee48fd1182012-01-13 09:32:14 +10302644 ndst++;
Jan Beulich554bdfe2009-07-06 14:51:44 +01002645 }
Rusty Russell59ef28b2012-10-25 10:49:25 +10302646 }
Jan Beulich4a496222009-07-06 14:50:42 +01002647
2648 /* Append room for core symbols at end of core part. */
Rusty Russell7523e4d2015-11-26 09:44:08 +10302649 info->symoffs = ALIGN(mod->core_layout.size, symsect->sh_addralign ?: 1);
2650 info->stroffs = mod->core_layout.size = info->symoffs + ndst * sizeof(Elf_Sym);
2651 mod->core_layout.size += strtab_size;
Eugene Loh1c7651f2019-02-25 11:59:58 -08002652 info->core_typeoffs = mod->core_layout.size;
2653 mod->core_layout.size += ndst * sizeof(char);
Rusty Russell7523e4d2015-11-26 09:44:08 +10302654 mod->core_layout.size = debug_align(mod->core_layout.size);
Jan Beulich4a496222009-07-06 14:50:42 +01002655
Jan Beulich554bdfe2009-07-06 14:51:44 +01002656 /* Put string table section at end of init part of module. */
2657 strsect->sh_flags |= SHF_ALLOC;
Rusty Russell7523e4d2015-11-26 09:44:08 +10302658 strsect->sh_entsize = get_offset(mod, &mod->init_layout.size, strsect,
Rusty Russell49668682010-08-05 12:59:10 -06002659 info->index.str) | INIT_OFFSET_MASK;
Jim Cromie5e124162011-12-06 12:11:31 -07002660 pr_debug("\t%s\n", info->secstrings + strsect->sh_name);
Rusty Russell82440622016-02-03 16:55:26 +10302661
2662 /* We'll tack temporary mod_kallsyms on the end. */
2663 mod->init_layout.size = ALIGN(mod->init_layout.size,
2664 __alignof__(struct mod_kallsyms));
2665 info->mod_kallsyms_init_off = mod->init_layout.size;
2666 mod->init_layout.size += sizeof(struct mod_kallsyms);
Eugene Loh1c7651f2019-02-25 11:59:58 -08002667 info->init_typeoffs = mod->init_layout.size;
2668 mod->init_layout.size += nsrc * sizeof(char);
Rusty Russell82440622016-02-03 16:55:26 +10302669 mod->init_layout.size = debug_align(mod->init_layout.size);
Jan Beulich4a496222009-07-06 14:50:42 +01002670}
2671
Rusty Russell82440622016-02-03 16:55:26 +10302672/*
2673 * We use the full symtab and strtab which layout_symtab arranged to
2674 * be appended to the init section. Later we switch to the cut-down
2675 * core-only ones.
2676 */
Rusty Russell811d66a2010-08-05 12:59:12 -06002677static void add_kallsyms(struct module *mod, const struct load_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002678{
Jan Beulich4a496222009-07-06 14:50:42 +01002679 unsigned int i, ndst;
2680 const Elf_Sym *src;
2681 Elf_Sym *dst;
Jan Beulich554bdfe2009-07-06 14:51:44 +01002682 char *s;
Rusty Russelleded41c2010-08-05 12:59:07 -06002683 Elf_Shdr *symsec = &info->sechdrs[info->index.sym];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002684
Rusty Russell82440622016-02-03 16:55:26 +10302685 /* Set up to point into init section. */
2686 mod->kallsyms = mod->init_layout.base + info->mod_kallsyms_init_off;
2687
2688 mod->kallsyms->symtab = (void *)symsec->sh_addr;
2689 mod->kallsyms->num_symtab = symsec->sh_size / sizeof(Elf_Sym);
Rusty Russell511ca6a2010-08-05 12:59:08 -06002690 /* Make sure we get permanent strtab: don't use info->strtab. */
Rusty Russell82440622016-02-03 16:55:26 +10302691 mod->kallsyms->strtab = (void *)info->sechdrs[info->index.str].sh_addr;
Eugene Loh1c7651f2019-02-25 11:59:58 -08002692 mod->kallsyms->typetab = mod->init_layout.base + info->init_typeoffs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002693
Eugene Loh1c7651f2019-02-25 11:59:58 -08002694 /*
2695 * Now populate the cut down core kallsyms for after init
2696 * and set types up while we still have access to sections.
2697 */
Rusty Russell82440622016-02-03 16:55:26 +10302698 mod->core_kallsyms.symtab = dst = mod->core_layout.base + info->symoffs;
2699 mod->core_kallsyms.strtab = s = mod->core_layout.base + info->stroffs;
Eugene Loh1c7651f2019-02-25 11:59:58 -08002700 mod->core_kallsyms.typetab = mod->core_layout.base + info->core_typeoffs;
Rusty Russell82440622016-02-03 16:55:26 +10302701 src = mod->kallsyms->symtab;
2702 for (ndst = i = 0; i < mod->kallsyms->num_symtab; i++) {
Eugene Loh1c7651f2019-02-25 11:59:58 -08002703 mod->kallsyms->typetab[i] = elf_type(src + i, info);
Jessica Yu1ce15ef2016-03-22 20:03:16 -04002704 if (i == 0 || is_livepatch_module(mod) ||
Miroslav Benese0224412015-11-26 13:18:06 +10302705 is_core_symbol(src+i, info->sechdrs, info->hdr->e_shnum,
2706 info->index.pcpu)) {
Eugene Loh1c7651f2019-02-25 11:59:58 -08002707 mod->core_kallsyms.typetab[ndst] =
2708 mod->kallsyms->typetab[i];
Rusty Russell59ef28b2012-10-25 10:49:25 +10302709 dst[ndst] = src[i];
Rusty Russell82440622016-02-03 16:55:26 +10302710 dst[ndst++].st_name = s - mod->core_kallsyms.strtab;
2711 s += strlcpy(s, &mod->kallsyms->strtab[src[i].st_name],
Rusty Russell59ef28b2012-10-25 10:49:25 +10302712 KSYM_NAME_LEN) + 1;
2713 }
Jan Beulich4a496222009-07-06 14:50:42 +01002714 }
Rusty Russell82440622016-02-03 16:55:26 +10302715 mod->core_kallsyms.num_symtab = ndst;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002716}
2717#else
Rusty Russell49668682010-08-05 12:59:10 -06002718static inline void layout_symtab(struct module *mod, struct load_info *info)
Jan Beulich4a496222009-07-06 14:50:42 +01002719{
2720}
Paul Mundt3ae91c22009-10-01 15:43:54 -07002721
Michał Mirosławabbce902010-09-20 01:58:08 +02002722static void add_kallsyms(struct module *mod, const struct load_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002723{
2724}
2725#endif /* CONFIG_KALLSYMS */
2726
Zhou Chengming52796312017-07-07 11:15:58 +08002727static void dynamic_debug_setup(struct module *mod, struct _ddebug *debug, unsigned int num)
Rusty Russell5e458cc2008-10-22 10:00:13 -05002728{
Rusty Russell811d66a2010-08-05 12:59:12 -06002729 if (!debug)
2730 return;
Rasmus Villemoes513770f2019-03-07 16:27:48 -08002731 ddebug_add_module(debug, num, mod->name);
Rusty Russell5e458cc2008-10-22 10:00:13 -05002732}
Jason Baron346e15b2008-08-12 16:46:19 -04002733
Zhou Chengming52796312017-07-07 11:15:58 +08002734static void dynamic_debug_remove(struct module *mod, struct _ddebug *debug)
Yehuda Sadehff49d742010-07-03 13:07:35 +10002735{
2736 if (debug)
Zhou Chengming52796312017-07-07 11:15:58 +08002737 ddebug_remove_module(mod->name);
Yehuda Sadehff49d742010-07-03 13:07:35 +10002738}
2739
Jonas Bonn74e08fc2011-06-30 21:22:11 +02002740void * __weak module_alloc(unsigned long size)
2741{
Rusty Russell82fab442012-12-11 09:38:33 +10302742 return vmalloc_exec(size);
Jonas Bonn74e08fc2011-06-30 21:22:11 +02002743}
2744
Matthias Schiffer38b37d62019-06-07 12:49:11 +02002745bool __weak module_exit_section(const char *name)
2746{
2747 return strstarts(name, ".exit");
2748}
2749
Catalin Marinas4f2294b2009-06-11 13:23:20 +01002750#ifdef CONFIG_DEBUG_KMEMLEAK
Rusty Russell49668682010-08-05 12:59:10 -06002751static void kmemleak_load_module(const struct module *mod,
2752 const struct load_info *info)
Catalin Marinas4f2294b2009-06-11 13:23:20 +01002753{
2754 unsigned int i;
2755
2756 /* only scan the sections containing data */
Catalin Marinasc017b4be32009-10-28 13:33:09 +00002757 kmemleak_scan_area(mod, sizeof(struct module), GFP_KERNEL);
Catalin Marinas4f2294b2009-06-11 13:23:20 +01002758
Rusty Russell49668682010-08-05 12:59:10 -06002759 for (i = 1; i < info->hdr->e_shnum; i++) {
Steven Rostedt06c94942013-05-15 20:33:01 +01002760 /* Scan all writable sections that's not executable */
2761 if (!(info->sechdrs[i].sh_flags & SHF_ALLOC) ||
2762 !(info->sechdrs[i].sh_flags & SHF_WRITE) ||
2763 (info->sechdrs[i].sh_flags & SHF_EXECINSTR))
Catalin Marinas4f2294b2009-06-11 13:23:20 +01002764 continue;
2765
Rusty Russell49668682010-08-05 12:59:10 -06002766 kmemleak_scan_area((void *)info->sechdrs[i].sh_addr,
2767 info->sechdrs[i].sh_size, GFP_KERNEL);
Catalin Marinas4f2294b2009-06-11 13:23:20 +01002768 }
2769}
2770#else
Rusty Russell49668682010-08-05 12:59:10 -06002771static inline void kmemleak_load_module(const struct module *mod,
2772 const struct load_info *info)
Catalin Marinas4f2294b2009-06-11 13:23:20 +01002773{
2774}
2775#endif
2776
Rusty Russell106a4ee2012-09-26 10:09:40 +01002777#ifdef CONFIG_MODULE_SIG
Ben Hutchingsbca014c2016-04-28 09:24:01 +09302778static int module_sig_check(struct load_info *info, int flags)
Rusty Russell106a4ee2012-09-26 10:09:40 +01002779{
2780 int err = -ENOKEY;
Kees Cook34e11692012-10-16 07:31:07 +10302781 const unsigned long markerlen = sizeof(MODULE_SIG_STRING) - 1;
2782 const void *mod = info->hdr;
Rusty Russell106a4ee2012-09-26 10:09:40 +01002783
Ben Hutchingsbca014c2016-04-28 09:24:01 +09302784 /*
2785 * Require flags == 0, as a module with version information
2786 * removed is no longer the module that was signed
2787 */
2788 if (flags == 0 &&
2789 info->len > markerlen &&
Kees Cook34e11692012-10-16 07:31:07 +10302790 memcmp(mod + info->len - markerlen, MODULE_SIG_STRING, markerlen) == 0) {
David Howellscaabe242012-10-20 01:19:29 +01002791 /* We truncate the module to discard the signature */
Kees Cook34e11692012-10-16 07:31:07 +10302792 info->len -= markerlen;
Jessica Yuf314dfe2018-06-29 16:37:08 +02002793 err = mod_verify_sig(mod, info);
Rusty Russell106a4ee2012-09-26 10:09:40 +01002794 }
2795
2796 if (!err) {
2797 info->sig_ok = true;
2798 return 0;
2799 }
2800
2801 /* Not having a signature is only an error if we're strict. */
Jia Zhang2c8fd262018-04-11 11:53:33 +08002802 if (err == -ENOKEY && !is_module_sig_enforced())
Rusty Russell106a4ee2012-09-26 10:09:40 +01002803 err = 0;
2804
2805 return err;
2806}
2807#else /* !CONFIG_MODULE_SIG */
Ben Hutchingsbca014c2016-04-28 09:24:01 +09302808static int module_sig_check(struct load_info *info, int flags)
Rusty Russell106a4ee2012-09-26 10:09:40 +01002809{
2810 return 0;
2811}
2812#endif /* !CONFIG_MODULE_SIG */
2813
Kees Cook34e11692012-10-16 07:31:07 +10302814/* Sanity checks against invalid binaries, wrong arch, weird elf version. */
2815static int elf_header_check(struct load_info *info)
Rusty Russell40dd2562010-08-05 12:59:03 -06002816{
Kees Cook34e11692012-10-16 07:31:07 +10302817 if (info->len < sizeof(*(info->hdr)))
Rusty Russell40dd2562010-08-05 12:59:03 -06002818 return -ENOEXEC;
2819
Kees Cook34e11692012-10-16 07:31:07 +10302820 if (memcmp(info->hdr->e_ident, ELFMAG, SELFMAG) != 0
2821 || info->hdr->e_type != ET_REL
2822 || !elf_check_arch(info->hdr)
2823 || info->hdr->e_shentsize != sizeof(Elf_Shdr))
2824 return -ENOEXEC;
2825
2826 if (info->hdr->e_shoff >= info->len
2827 || (info->hdr->e_shnum * sizeof(Elf_Shdr) >
2828 info->len - info->hdr->e_shoff))
2829 return -ENOEXEC;
2830
2831 return 0;
2832}
2833
Linus Torvalds3afe9f82015-04-07 10:33:49 -07002834#define COPY_CHUNK_SIZE (16*PAGE_SIZE)
2835
2836static int copy_chunked_from_user(void *dst, const void __user *usrc, unsigned long len)
2837{
2838 do {
2839 unsigned long n = min(len, COPY_CHUNK_SIZE);
2840
2841 if (copy_from_user(dst, usrc, n) != 0)
2842 return -EFAULT;
2843 cond_resched();
2844 dst += n;
2845 usrc += n;
2846 len -= n;
2847 } while (len);
2848 return 0;
2849}
2850
Jessica Yu1ce15ef2016-03-22 20:03:16 -04002851#ifdef CONFIG_LIVEPATCH
Josh Poimboeuf2992ef22016-08-25 10:04:45 -05002852static int check_modinfo_livepatch(struct module *mod, struct load_info *info)
Jessica Yu1ce15ef2016-03-22 20:03:16 -04002853{
Josh Poimboeuf2992ef22016-08-25 10:04:45 -05002854 if (get_modinfo(info, "livepatch")) {
2855 mod->klp = true;
2856 add_taint_module(mod, TAINT_LIVEPATCH, LOCKDEP_STILL_OK);
Joe Lawrence7598d162017-01-12 11:57:44 -05002857 pr_notice_once("%s: tainting kernel with TAINT_LIVEPATCH\n",
2858 mod->name);
Josh Poimboeuf2992ef22016-08-25 10:04:45 -05002859 }
Jessica Yu1ce15ef2016-03-22 20:03:16 -04002860
2861 return 0;
2862}
2863#else /* !CONFIG_LIVEPATCH */
Josh Poimboeuf2992ef22016-08-25 10:04:45 -05002864static int check_modinfo_livepatch(struct module *mod, struct load_info *info)
Jessica Yu1ce15ef2016-03-22 20:03:16 -04002865{
2866 if (get_modinfo(info, "livepatch")) {
2867 pr_err("%s: module is marked as livepatch module, but livepatch support is disabled",
2868 mod->name);
2869 return -ENOEXEC;
2870 }
2871
2872 return 0;
2873}
2874#endif /* CONFIG_LIVEPATCH */
2875
Andi Kleencaf75012018-01-25 15:50:28 -08002876static void check_modinfo_retpoline(struct module *mod, struct load_info *info)
2877{
2878 if (retpoline_module_ok(get_modinfo(info, "retpoline")))
2879 return;
2880
2881 pr_warn("%s: loading module not compiled with retpoline compiler.\n",
2882 mod->name);
2883}
2884
Kees Cook34e11692012-10-16 07:31:07 +10302885/* Sets info->hdr and info->len. */
2886static int copy_module_from_user(const void __user *umod, unsigned long len,
2887 struct load_info *info)
2888{
Kees Cook2e72d512012-10-16 07:32:07 +10302889 int err;
2890
Kees Cook34e11692012-10-16 07:31:07 +10302891 info->len = len;
2892 if (info->len < sizeof(*(info->hdr)))
Rusty Russell40dd2562010-08-05 12:59:03 -06002893 return -ENOEXEC;
2894
Mimi Zoharc77b8cd2018-07-13 14:06:02 -04002895 err = security_kernel_load_data(LOADING_MODULE);
Kees Cook2e72d512012-10-16 07:32:07 +10302896 if (err)
2897 return err;
2898
Rusty Russell40dd2562010-08-05 12:59:03 -06002899 /* Suck in entire file: we'll want most of it. */
Kirill A. Shutemovcc9e6052015-03-24 12:31:40 +10302900 info->hdr = __vmalloc(info->len,
Michal Hocko19809c22017-05-08 15:57:44 -07002901 GFP_KERNEL | __GFP_NOWARN, PAGE_KERNEL);
Kees Cook34e11692012-10-16 07:31:07 +10302902 if (!info->hdr)
Rusty Russell40dd2562010-08-05 12:59:03 -06002903 return -ENOMEM;
2904
Linus Torvalds3afe9f82015-04-07 10:33:49 -07002905 if (copy_chunked_from_user(info->hdr, umod, info->len) != 0) {
Kees Cook34e11692012-10-16 07:31:07 +10302906 vfree(info->hdr);
2907 return -EFAULT;
Rusty Russell40dd2562010-08-05 12:59:03 -06002908 }
2909
Rusty Russell40dd2562010-08-05 12:59:03 -06002910 return 0;
Kees Cook34e11692012-10-16 07:31:07 +10302911}
Rusty Russell40dd2562010-08-05 12:59:03 -06002912
Rusty Russelld9131882010-08-05 12:59:08 -06002913static void free_copy(struct load_info *info)
2914{
Rusty Russelld9131882010-08-05 12:59:08 -06002915 vfree(info->hdr);
2916}
2917
Rusty Russell2f3238a2012-10-22 18:09:41 +10302918static int rewrite_section_headers(struct load_info *info, int flags)
Rusty Russell8b5f61a2010-08-05 12:59:06 -06002919{
2920 unsigned int i;
2921
2922 /* This should always be true, but let's be sure. */
2923 info->sechdrs[0].sh_addr = 0;
2924
2925 for (i = 1; i < info->hdr->e_shnum; i++) {
2926 Elf_Shdr *shdr = &info->sechdrs[i];
2927 if (shdr->sh_type != SHT_NOBITS
2928 && info->len < shdr->sh_offset + shdr->sh_size) {
Andrew Mortonbddb12b2013-11-12 15:11:28 -08002929 pr_err("Module len %lu truncated\n", info->len);
Rusty Russell8b5f61a2010-08-05 12:59:06 -06002930 return -ENOEXEC;
2931 }
2932
2933 /* Mark all sections sh_addr with their address in the
2934 temporary image. */
2935 shdr->sh_addr = (size_t)info->hdr + shdr->sh_offset;
2936
2937#ifndef CONFIG_MODULE_UNLOAD
2938 /* Don't load .exit sections */
Matthias Schiffer38b37d62019-06-07 12:49:11 +02002939 if (module_exit_section(info->secstrings+shdr->sh_name))
Rusty Russell8b5f61a2010-08-05 12:59:06 -06002940 shdr->sh_flags &= ~(unsigned long)SHF_ALLOC;
2941#endif
Rusty Russell8b5f61a2010-08-05 12:59:06 -06002942 }
Rusty Russelld6df72a2010-08-05 12:59:07 -06002943
2944 /* Track but don't keep modinfo and version sections. */
Rusty Russelld6df72a2010-08-05 12:59:07 -06002945 info->sechdrs[info->index.vers].sh_flags &= ~(unsigned long)SHF_ALLOC;
Kees Cook3e2e8572017-04-21 15:35:27 -07002946 info->sechdrs[info->index.info].sh_flags &= ~(unsigned long)SHF_ALLOC;
2947
Rusty Russell8b5f61a2010-08-05 12:59:06 -06002948 return 0;
2949}
2950
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002951/*
2952 * Set up our basic convenience variables (pointers to section headers,
2953 * search for module section index etc), and do some basic section
2954 * verification.
2955 *
Jessica Yu81a0abd2018-06-22 13:59:29 +02002956 * Set info->mod to the temporary copy of the module in info->hdr. The final one
2957 * will be allocated in move_module().
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002958 */
Jessica Yu81a0abd2018-06-22 13:59:29 +02002959static int setup_load_info(struct load_info *info, int flags)
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002960{
2961 unsigned int i;
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002962
2963 /* Set up the convenience variables */
2964 info->sechdrs = (void *)info->hdr + info->hdr->e_shoff;
Rusty Russell8b5f61a2010-08-05 12:59:06 -06002965 info->secstrings = (void *)info->hdr
2966 + info->sechdrs[info->hdr->e_shstrndx].sh_offset;
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002967
Jessica Yu5fdc7db2018-06-22 14:00:01 +02002968 /* Try to find a name early so we can log errors with a module name */
2969 info->index.info = find_sec(info, ".modinfo");
2970 if (!info->index.info)
2971 info->name = "(missing .modinfo section)";
2972 else
2973 info->name = get_modinfo(info, "name");
Rusty Russell8b5f61a2010-08-05 12:59:06 -06002974
2975 /* Find internal symbols and strings. */
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002976 for (i = 1; i < info->hdr->e_shnum; i++) {
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002977 if (info->sechdrs[i].sh_type == SHT_SYMTAB) {
2978 info->index.sym = i;
2979 info->index.str = info->sechdrs[i].sh_link;
Rusty Russell8b5f61a2010-08-05 12:59:06 -06002980 info->strtab = (char *)info->hdr
2981 + info->sechdrs[info->index.str].sh_offset;
2982 break;
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002983 }
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002984 }
2985
Jessica Yu5fdc7db2018-06-22 14:00:01 +02002986 if (info->index.sym == 0) {
2987 pr_warn("%s: module has no symbols (stripped?)\n", info->name);
2988 return -ENOEXEC;
2989 }
2990
Rusty Russell49668682010-08-05 12:59:10 -06002991 info->index.mod = find_sec(info, ".gnu.linkonce.this_module");
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002992 if (!info->index.mod) {
Kees Cook3e2e8572017-04-21 15:35:27 -07002993 pr_warn("%s: No module found in object\n",
2994 info->name ?: "(missing .modinfo name field)");
Jessica Yu81a0abd2018-06-22 13:59:29 +02002995 return -ENOEXEC;
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002996 }
2997 /* This is temporary: point mod into copy of data. */
Jessica Yu5fdc7db2018-06-22 14:00:01 +02002998 info->mod = (void *)info->hdr + info->sechdrs[info->index.mod].sh_offset;
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002999
Kees Cook3e2e8572017-04-21 15:35:27 -07003000 /*
Jessica Yu5fdc7db2018-06-22 14:00:01 +02003001 * If we didn't load the .modinfo 'name' field earlier, fall back to
Kees Cook3e2e8572017-04-21 15:35:27 -07003002 * on-disk struct mod 'name' field.
3003 */
3004 if (!info->name)
Jessica Yu81a0abd2018-06-22 13:59:29 +02003005 info->name = info->mod->name;
Kees Cook3e2e8572017-04-21 15:35:27 -07003006
Jessica Yu5fdc7db2018-06-22 14:00:01 +02003007 if (flags & MODULE_INIT_IGNORE_MODVERSIONS)
3008 info->index.vers = 0; /* Pretend no __versions section! */
3009 else
3010 info->index.vers = find_sec(info, "__versions");
Linus Torvalds3264d3f2010-06-02 11:01:06 -07003011
Rusty Russell49668682010-08-05 12:59:10 -06003012 info->index.pcpu = find_pcpusec(info);
Linus Torvalds3264d3f2010-06-02 11:01:06 -07003013
Jessica Yu81a0abd2018-06-22 13:59:29 +02003014 return 0;
Linus Torvalds3264d3f2010-06-02 11:01:06 -07003015}
3016
Rusty Russell2f3238a2012-10-22 18:09:41 +10303017static int check_modinfo(struct module *mod, struct load_info *info, int flags)
Rusty Russell40dd2562010-08-05 12:59:03 -06003018{
Rusty Russell49668682010-08-05 12:59:10 -06003019 const char *modmagic = get_modinfo(info, "vermagic");
Rusty Russell40dd2562010-08-05 12:59:03 -06003020 int err;
3021
Rusty Russell2f3238a2012-10-22 18:09:41 +10303022 if (flags & MODULE_INIT_IGNORE_VERMAGIC)
3023 modmagic = NULL;
3024
Rusty Russell40dd2562010-08-05 12:59:03 -06003025 /* This is allowed: modprobe --force will invalidate it. */
3026 if (!modmagic) {
3027 err = try_to_force_load(mod, "bad vermagic");
3028 if (err)
3029 return err;
Rusty Russell49668682010-08-05 12:59:10 -06003030 } else if (!same_magic(modmagic, vermagic, info->index.vers)) {
Andrew Mortonbddb12b2013-11-12 15:11:28 -08003031 pr_err("%s: version magic '%s' should be '%s'\n",
Kees Cook3e2e8572017-04-21 15:35:27 -07003032 info->name, modmagic, vermagic);
Rusty Russell40dd2562010-08-05 12:59:03 -06003033 return -ENOEXEC;
3034 }
3035
Libor Pechacek3205c362016-04-13 11:06:12 +09303036 if (!get_modinfo(info, "intree")) {
3037 if (!test_taint(TAINT_OOT_MODULE))
3038 pr_warn("%s: loading out-of-tree module taints kernel.\n",
3039 mod->name);
Rusty Russell373d4d02013-01-21 17:17:39 +10303040 add_taint_module(mod, TAINT_OOT_MODULE, LOCKDEP_STILL_OK);
Libor Pechacek3205c362016-04-13 11:06:12 +09303041 }
Ben Hutchings2449b8b2011-10-24 15:12:28 +02003042
Andi Kleencaf75012018-01-25 15:50:28 -08003043 check_modinfo_retpoline(mod, info);
3044
Rusty Russell49668682010-08-05 12:59:10 -06003045 if (get_modinfo(info, "staging")) {
Rusty Russell373d4d02013-01-21 17:17:39 +10303046 add_taint_module(mod, TAINT_CRAP, LOCKDEP_STILL_OK);
Andrew Mortonbddb12b2013-11-12 15:11:28 -08003047 pr_warn("%s: module is from the staging directory, the quality "
3048 "is unknown, you have been warned.\n", mod->name);
Rusty Russell40dd2562010-08-05 12:59:03 -06003049 }
Rusty Russell22e268e2010-08-05 12:59:05 -06003050
Josh Poimboeuf2992ef22016-08-25 10:04:45 -05003051 err = check_modinfo_livepatch(mod, info);
Jessica Yu1ce15ef2016-03-22 20:03:16 -04003052 if (err)
3053 return err;
3054
Rusty Russell22e268e2010-08-05 12:59:05 -06003055 /* Set up license info based on the info section */
Rusty Russell49668682010-08-05 12:59:10 -06003056 set_license(mod, get_modinfo(info, "license"));
Rusty Russell22e268e2010-08-05 12:59:05 -06003057
Rusty Russell40dd2562010-08-05 12:59:03 -06003058 return 0;
3059}
3060
Frantisek Hrbataeb3057d2013-10-14 18:08:46 +10303061static int find_module_sections(struct module *mod, struct load_info *info)
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06003062{
Rusty Russell49668682010-08-05 12:59:10 -06003063 mod->kp = section_objs(info, "__param",
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06003064 sizeof(*mod->kp), &mod->num_kp);
Rusty Russell49668682010-08-05 12:59:10 -06003065 mod->syms = section_objs(info, "__ksymtab",
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06003066 sizeof(*mod->syms), &mod->num_syms);
Rusty Russell49668682010-08-05 12:59:10 -06003067 mod->crcs = section_addr(info, "__kcrctab");
3068 mod->gpl_syms = section_objs(info, "__ksymtab_gpl",
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06003069 sizeof(*mod->gpl_syms),
3070 &mod->num_gpl_syms);
Rusty Russell49668682010-08-05 12:59:10 -06003071 mod->gpl_crcs = section_addr(info, "__kcrctab_gpl");
3072 mod->gpl_future_syms = section_objs(info,
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06003073 "__ksymtab_gpl_future",
3074 sizeof(*mod->gpl_future_syms),
3075 &mod->num_gpl_future_syms);
Rusty Russell49668682010-08-05 12:59:10 -06003076 mod->gpl_future_crcs = section_addr(info, "__kcrctab_gpl_future");
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06003077
3078#ifdef CONFIG_UNUSED_SYMBOLS
Rusty Russell49668682010-08-05 12:59:10 -06003079 mod->unused_syms = section_objs(info, "__ksymtab_unused",
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06003080 sizeof(*mod->unused_syms),
3081 &mod->num_unused_syms);
Rusty Russell49668682010-08-05 12:59:10 -06003082 mod->unused_crcs = section_addr(info, "__kcrctab_unused");
3083 mod->unused_gpl_syms = section_objs(info, "__ksymtab_unused_gpl",
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06003084 sizeof(*mod->unused_gpl_syms),
3085 &mod->num_unused_gpl_syms);
Rusty Russell49668682010-08-05 12:59:10 -06003086 mod->unused_gpl_crcs = section_addr(info, "__kcrctab_unused_gpl");
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06003087#endif
3088#ifdef CONFIG_CONSTRUCTORS
Rusty Russell49668682010-08-05 12:59:10 -06003089 mod->ctors = section_objs(info, ".ctors",
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06003090 sizeof(*mod->ctors), &mod->num_ctors);
Frantisek Hrbataeb3057d2013-10-14 18:08:46 +10303091 if (!mod->ctors)
3092 mod->ctors = section_objs(info, ".init_array",
3093 sizeof(*mod->ctors), &mod->num_ctors);
3094 else if (find_sec(info, ".init_array")) {
3095 /*
3096 * This shouldn't happen with same compiler and binutils
3097 * building all parts of the module.
3098 */
Ionut Alexa6da0b562014-11-10 09:31:29 +10303099 pr_warn("%s: has both .ctors and .init_array.\n",
Frantisek Hrbataeb3057d2013-10-14 18:08:46 +10303100 mod->name);
3101 return -EINVAL;
3102 }
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06003103#endif
3104
3105#ifdef CONFIG_TRACEPOINTS
Mathieu Desnoyers65498642011-01-26 17:26:22 -05003106 mod->tracepoints_ptrs = section_objs(info, "__tracepoints_ptrs",
3107 sizeof(*mod->tracepoints_ptrs),
3108 &mod->num_tracepoints);
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06003109#endif
Paul E. McKenneyfe15b502019-04-05 16:15:00 -07003110#ifdef CONFIG_TREE_SRCU
3111 mod->srcu_struct_ptrs = section_objs(info, "___srcu_struct_ptrs",
3112 sizeof(*mod->srcu_struct_ptrs),
3113 &mod->num_srcu_structs);
3114#endif
Matt Mullinsa38d1102018-12-12 16:42:37 -08003115#ifdef CONFIG_BPF_EVENTS
3116 mod->bpf_raw_events = section_objs(info, "__bpf_raw_tp_map",
3117 sizeof(*mod->bpf_raw_events),
3118 &mod->num_bpf_raw_events);
3119#endif
Masahiro Yamadae9666d12018-12-31 00:14:15 +09003120#ifdef CONFIG_JUMP_LABEL
Jason Baronbf5438fc2010-09-17 11:09:00 -04003121 mod->jump_entries = section_objs(info, "__jump_table",
3122 sizeof(*mod->jump_entries),
3123 &mod->num_jump_entries);
3124#endif
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06003125#ifdef CONFIG_EVENT_TRACING
Rusty Russell49668682010-08-05 12:59:10 -06003126 mod->trace_events = section_objs(info, "_ftrace_events",
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06003127 sizeof(*mod->trace_events),
3128 &mod->num_trace_events);
Jeremy Linton99be6472017-05-31 16:56:44 -05003129 mod->trace_evals = section_objs(info, "_ftrace_eval_map",
3130 sizeof(*mod->trace_evals),
3131 &mod->num_trace_evals);
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06003132#endif
Steven Rostedt13b9b6e2010-11-10 22:19:24 -05003133#ifdef CONFIG_TRACING
3134 mod->trace_bprintk_fmt_start = section_objs(info, "__trace_printk_fmt",
3135 sizeof(*mod->trace_bprintk_fmt_start),
3136 &mod->num_trace_bprintk_fmt);
Steven Rostedt13b9b6e2010-11-10 22:19:24 -05003137#endif
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06003138#ifdef CONFIG_FTRACE_MCOUNT_RECORD
3139 /* sechdrs[0].sh_size is always zero */
Rusty Russell49668682010-08-05 12:59:10 -06003140 mod->ftrace_callsites = section_objs(info, "__mcount_loc",
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06003141 sizeof(*mod->ftrace_callsites),
3142 &mod->num_ftrace_callsites);
3143#endif
Masami Hiramatsu540adea2018-01-13 02:55:03 +09003144#ifdef CONFIG_FUNCTION_ERROR_INJECTION
3145 mod->ei_funcs = section_objs(info, "_error_injection_whitelist",
3146 sizeof(*mod->ei_funcs),
3147 &mod->num_ei_funcs);
Josef Bacik92ace992017-12-11 11:36:46 -05003148#endif
Rusty Russell811d66a2010-08-05 12:59:12 -06003149 mod->extable = section_objs(info, "__ex_table",
3150 sizeof(*mod->extable), &mod->num_exentries);
3151
Rusty Russell49668682010-08-05 12:59:10 -06003152 if (section_addr(info, "__obsparm"))
Andrew Mortonbddb12b2013-11-12 15:11:28 -08003153 pr_warn("%s: Ignoring obsolete parameters\n", mod->name);
Rusty Russell811d66a2010-08-05 12:59:12 -06003154
3155 info->debug = section_objs(info, "__verbose",
3156 sizeof(*info->debug), &info->num_debug);
Frantisek Hrbataeb3057d2013-10-14 18:08:46 +10303157
3158 return 0;
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06003159}
3160
Rusty Russell49668682010-08-05 12:59:10 -06003161static int move_module(struct module *mod, struct load_info *info)
Linus Torvalds65b8a9b2010-08-05 12:59:02 -06003162{
3163 int i;
3164 void *ptr;
3165
3166 /* Do the allocs. */
Rusty Russell7523e4d2015-11-26 09:44:08 +10303167 ptr = module_alloc(mod->core_layout.size);
Linus Torvalds65b8a9b2010-08-05 12:59:02 -06003168 /*
3169 * The pointer to this block is stored in the module structure
3170 * which is inside the block. Just mark it as not being a
3171 * leak.
3172 */
3173 kmemleak_not_leak(ptr);
3174 if (!ptr)
Rusty Russelld9131882010-08-05 12:59:08 -06003175 return -ENOMEM;
Linus Torvalds65b8a9b2010-08-05 12:59:02 -06003176
Rusty Russell7523e4d2015-11-26 09:44:08 +10303177 memset(ptr, 0, mod->core_layout.size);
3178 mod->core_layout.base = ptr;
Linus Torvalds65b8a9b2010-08-05 12:59:02 -06003179
Rusty Russell7523e4d2015-11-26 09:44:08 +10303180 if (mod->init_layout.size) {
3181 ptr = module_alloc(mod->init_layout.size);
Rusty Russell82fab442012-12-11 09:38:33 +10303182 /*
3183 * The pointer to this block is stored in the module structure
3184 * which is inside the block. This block doesn't need to be
3185 * scanned as it contains data and code that will be freed
3186 * after the module is initialized.
3187 */
3188 kmemleak_ignore(ptr);
3189 if (!ptr) {
Rusty Russell7523e4d2015-11-26 09:44:08 +10303190 module_memfree(mod->core_layout.base);
Rusty Russell82fab442012-12-11 09:38:33 +10303191 return -ENOMEM;
3192 }
Rusty Russell7523e4d2015-11-26 09:44:08 +10303193 memset(ptr, 0, mod->init_layout.size);
3194 mod->init_layout.base = ptr;
Rusty Russell82fab442012-12-11 09:38:33 +10303195 } else
Rusty Russell7523e4d2015-11-26 09:44:08 +10303196 mod->init_layout.base = NULL;
Linus Torvalds65b8a9b2010-08-05 12:59:02 -06003197
3198 /* Transfer each section which specifies SHF_ALLOC */
Jim Cromie5e124162011-12-06 12:11:31 -07003199 pr_debug("final section addresses:\n");
Rusty Russell49668682010-08-05 12:59:10 -06003200 for (i = 0; i < info->hdr->e_shnum; i++) {
Linus Torvalds65b8a9b2010-08-05 12:59:02 -06003201 void *dest;
Rusty Russell49668682010-08-05 12:59:10 -06003202 Elf_Shdr *shdr = &info->sechdrs[i];
Linus Torvalds65b8a9b2010-08-05 12:59:02 -06003203
Rusty Russell49668682010-08-05 12:59:10 -06003204 if (!(shdr->sh_flags & SHF_ALLOC))
Linus Torvalds65b8a9b2010-08-05 12:59:02 -06003205 continue;
3206
Rusty Russell49668682010-08-05 12:59:10 -06003207 if (shdr->sh_entsize & INIT_OFFSET_MASK)
Rusty Russell7523e4d2015-11-26 09:44:08 +10303208 dest = mod->init_layout.base
Rusty Russell49668682010-08-05 12:59:10 -06003209 + (shdr->sh_entsize & ~INIT_OFFSET_MASK);
Linus Torvalds65b8a9b2010-08-05 12:59:02 -06003210 else
Rusty Russell7523e4d2015-11-26 09:44:08 +10303211 dest = mod->core_layout.base + shdr->sh_entsize;
Linus Torvalds65b8a9b2010-08-05 12:59:02 -06003212
Rusty Russell49668682010-08-05 12:59:10 -06003213 if (shdr->sh_type != SHT_NOBITS)
3214 memcpy(dest, (void *)shdr->sh_addr, shdr->sh_size);
Linus Torvalds65b8a9b2010-08-05 12:59:02 -06003215 /* Update sh_addr to point to copy in image. */
Rusty Russell49668682010-08-05 12:59:10 -06003216 shdr->sh_addr = (unsigned long)dest;
Jim Cromie5e124162011-12-06 12:11:31 -07003217 pr_debug("\t0x%lx %s\n",
3218 (long)shdr->sh_addr, info->secstrings + shdr->sh_name);
Linus Torvalds65b8a9b2010-08-05 12:59:02 -06003219 }
Rusty Russelld9131882010-08-05 12:59:08 -06003220
3221 return 0;
Linus Torvalds65b8a9b2010-08-05 12:59:02 -06003222}
3223
Rusty Russell49668682010-08-05 12:59:10 -06003224static int check_module_license_and_versions(struct module *mod)
Rusty Russell22e268e2010-08-05 12:59:05 -06003225{
Libor Pechacek3205c362016-04-13 11:06:12 +09303226 int prev_taint = test_taint(TAINT_PROPRIETARY_MODULE);
3227
Rusty Russell22e268e2010-08-05 12:59:05 -06003228 /*
3229 * ndiswrapper is under GPL by itself, but loads proprietary modules.
3230 * Don't use add_taint_module(), as it would prevent ndiswrapper from
3231 * using GPL-only symbols it needs.
3232 */
3233 if (strcmp(mod->name, "ndiswrapper") == 0)
Rusty Russell373d4d02013-01-21 17:17:39 +10303234 add_taint(TAINT_PROPRIETARY_MODULE, LOCKDEP_NOW_UNRELIABLE);
Rusty Russell22e268e2010-08-05 12:59:05 -06003235
3236 /* driverloader was caught wrongly pretending to be under GPL */
3237 if (strcmp(mod->name, "driverloader") == 0)
Rusty Russell373d4d02013-01-21 17:17:39 +10303238 add_taint_module(mod, TAINT_PROPRIETARY_MODULE,
3239 LOCKDEP_NOW_UNRELIABLE);
Rusty Russell22e268e2010-08-05 12:59:05 -06003240
Matthew Garrettc99af372012-06-22 13:49:31 -04003241 /* lve claims to be GPL but upstream won't provide source */
3242 if (strcmp(mod->name, "lve") == 0)
Rusty Russell373d4d02013-01-21 17:17:39 +10303243 add_taint_module(mod, TAINT_PROPRIETARY_MODULE,
3244 LOCKDEP_NOW_UNRELIABLE);
Matthew Garrettc99af372012-06-22 13:49:31 -04003245
Libor Pechacek3205c362016-04-13 11:06:12 +09303246 if (!prev_taint && test_taint(TAINT_PROPRIETARY_MODULE))
3247 pr_warn("%s: module license taints kernel.\n", mod->name);
3248
Rusty Russell22e268e2010-08-05 12:59:05 -06003249#ifdef CONFIG_MODVERSIONS
3250 if ((mod->num_syms && !mod->crcs)
3251 || (mod->num_gpl_syms && !mod->gpl_crcs)
3252 || (mod->num_gpl_future_syms && !mod->gpl_future_crcs)
3253#ifdef CONFIG_UNUSED_SYMBOLS
3254 || (mod->num_unused_syms && !mod->unused_crcs)
3255 || (mod->num_unused_gpl_syms && !mod->unused_gpl_crcs)
3256#endif
3257 ) {
3258 return try_to_force_load(mod,
3259 "no versions for exported symbols");
3260 }
3261#endif
3262 return 0;
3263}
3264
3265static void flush_module_icache(const struct module *mod)
3266{
3267 mm_segment_t old_fs;
3268
3269 /* flush the icache in correct context */
3270 old_fs = get_fs();
3271 set_fs(KERNEL_DS);
3272
3273 /*
3274 * Flush the instruction cache, since we've played with text.
3275 * Do it before processing of module parameters, so the module
3276 * can provide parameter accessor functions of its own.
3277 */
Rusty Russell7523e4d2015-11-26 09:44:08 +10303278 if (mod->init_layout.base)
3279 flush_icache_range((unsigned long)mod->init_layout.base,
3280 (unsigned long)mod->init_layout.base
3281 + mod->init_layout.size);
3282 flush_icache_range((unsigned long)mod->core_layout.base,
3283 (unsigned long)mod->core_layout.base + mod->core_layout.size);
Rusty Russell22e268e2010-08-05 12:59:05 -06003284
3285 set_fs(old_fs);
3286}
3287
Jonas Bonn74e08fc2011-06-30 21:22:11 +02003288int __weak module_frob_arch_sections(Elf_Ehdr *hdr,
3289 Elf_Shdr *sechdrs,
3290 char *secstrings,
3291 struct module *mod)
3292{
3293 return 0;
3294}
3295
Prarit Bhargavabe7de5f2016-07-21 15:37:56 +09303296/* module_blacklist is a comma-separated list of module names */
3297static char *module_blacklist;
Luis R. Rodriguez96b5b192017-06-28 18:32:31 -07003298static bool blacklisted(const char *module_name)
Prarit Bhargavabe7de5f2016-07-21 15:37:56 +09303299{
3300 const char *p;
3301 size_t len;
3302
3303 if (!module_blacklist)
3304 return false;
3305
3306 for (p = module_blacklist; *p; p += len) {
3307 len = strcspn(p, ",");
3308 if (strlen(module_name) == len && !memcmp(module_name, p, len))
3309 return true;
3310 if (p[len] == ',')
3311 len++;
3312 }
3313 return false;
3314}
3315core_param(module_blacklist, module_blacklist, charp, 0400);
3316
Rusty Russell2f3238a2012-10-22 18:09:41 +10303317static struct module *layout_and_allocate(struct load_info *info, int flags)
Rusty Russelld9131882010-08-05 12:59:08 -06003318{
Rusty Russelld9131882010-08-05 12:59:08 -06003319 struct module *mod;
Jessica Yu444d13f2016-07-27 12:06:21 +09303320 unsigned int ndx;
Rusty Russelld9131882010-08-05 12:59:08 -06003321 int err;
3322
Jessica Yu81a0abd2018-06-22 13:59:29 +02003323 err = check_modinfo(info->mod, info, flags);
Rusty Russelld9131882010-08-05 12:59:08 -06003324 if (err)
3325 return ERR_PTR(err);
3326
3327 /* Allow arches to frob section contents and sizes. */
Rusty Russell49668682010-08-05 12:59:10 -06003328 err = module_frob_arch_sections(info->hdr, info->sechdrs,
Jessica Yu81a0abd2018-06-22 13:59:29 +02003329 info->secstrings, info->mod);
Rusty Russelld9131882010-08-05 12:59:08 -06003330 if (err < 0)
Rusty Russell8d8022e2013-07-03 10:06:28 +09303331 return ERR_PTR(err);
Rusty Russelld9131882010-08-05 12:59:08 -06003332
Rusty Russell8d8022e2013-07-03 10:06:28 +09303333 /* We will do a special allocation for per-cpu sections later. */
3334 info->sechdrs[info->index.pcpu].sh_flags &= ~(unsigned long)SHF_ALLOC;
Rusty Russelld9131882010-08-05 12:59:08 -06003335
Jessica Yu444d13f2016-07-27 12:06:21 +09303336 /*
3337 * Mark ro_after_init section with SHF_RO_AFTER_INIT so that
3338 * layout_sections() can put it in the right place.
3339 * Note: ro_after_init sections also have SHF_{WRITE,ALLOC} set.
3340 */
3341 ndx = find_sec(info, ".data..ro_after_init");
3342 if (ndx)
3343 info->sechdrs[ndx].sh_flags |= SHF_RO_AFTER_INIT;
Ard Biesheuvele8722672018-09-18 23:51:43 -07003344 /*
3345 * Mark the __jump_table section as ro_after_init as well: these data
3346 * structures are never modified, with the exception of entries that
3347 * refer to code in the __init section, which are annotated as such
3348 * at module load time.
3349 */
3350 ndx = find_sec(info, "__jump_table");
3351 if (ndx)
3352 info->sechdrs[ndx].sh_flags |= SHF_RO_AFTER_INIT;
Jessica Yu444d13f2016-07-27 12:06:21 +09303353
Rusty Russelld9131882010-08-05 12:59:08 -06003354 /* Determine total sizes, and put offsets in sh_entsize. For now
3355 this is done generically; there doesn't appear to be any
3356 special cases for the architectures. */
Jessica Yu81a0abd2018-06-22 13:59:29 +02003357 layout_sections(info->mod, info);
3358 layout_symtab(info->mod, info);
Rusty Russelld9131882010-08-05 12:59:08 -06003359
3360 /* Allocate and move to the final place */
Jessica Yu81a0abd2018-06-22 13:59:29 +02003361 err = move_module(info->mod, info);
Rusty Russelld9131882010-08-05 12:59:08 -06003362 if (err)
Rusty Russell8d8022e2013-07-03 10:06:28 +09303363 return ERR_PTR(err);
Rusty Russelld9131882010-08-05 12:59:08 -06003364
3365 /* Module has been copied to its final place now: return it. */
3366 mod = (void *)info->sechdrs[info->index.mod].sh_addr;
Rusty Russell49668682010-08-05 12:59:10 -06003367 kmemleak_load_module(mod, info);
Rusty Russelld9131882010-08-05 12:59:08 -06003368 return mod;
Rusty Russelld9131882010-08-05 12:59:08 -06003369}
3370
3371/* mod is no longer valid after this! */
3372static void module_deallocate(struct module *mod, struct load_info *info)
3373{
Rusty Russelld9131882010-08-05 12:59:08 -06003374 percpu_modfree(mod);
Rusty Russelld453cde2015-01-20 09:07:04 +10303375 module_arch_freeing_init(mod);
Rusty Russell7523e4d2015-11-26 09:44:08 +10303376 module_memfree(mod->init_layout.base);
3377 module_memfree(mod->core_layout.base);
Rusty Russelld9131882010-08-05 12:59:08 -06003378}
3379
Jonas Bonn74e08fc2011-06-30 21:22:11 +02003380int __weak module_finalize(const Elf_Ehdr *hdr,
3381 const Elf_Shdr *sechdrs,
3382 struct module *me)
3383{
3384 return 0;
3385}
3386
Rusty Russell811d66a2010-08-05 12:59:12 -06003387static int post_relocation(struct module *mod, const struct load_info *info)
3388{
Rusty Russell51f3d0f2010-08-05 12:59:13 -06003389 /* Sort exception table now relocations are done. */
Rusty Russell811d66a2010-08-05 12:59:12 -06003390 sort_extable(mod->extable, mod->extable + mod->num_exentries);
3391
3392 /* Copy relocated percpu area over. */
3393 percpu_modcopy(mod, (void *)info->sechdrs[info->index.pcpu].sh_addr,
3394 info->sechdrs[info->index.pcpu].sh_size);
3395
Rusty Russell51f3d0f2010-08-05 12:59:13 -06003396 /* Setup kallsyms-specific fields. */
Rusty Russell811d66a2010-08-05 12:59:12 -06003397 add_kallsyms(mod, info);
3398
3399 /* Arch-specific module finalizing. */
3400 return module_finalize(info->hdr, info->sechdrs, mod);
3401}
3402
Rusty Russell9bb9c3b2012-09-28 14:31:03 +09303403/* Is this module of this name done loading? No locks held. */
3404static bool finished_loading(const char *name)
3405{
3406 struct module *mod;
3407 bool ret;
3408
Peter Zijlstra9cc019b2015-02-11 15:01:13 +10303409 /*
3410 * The module_mutex should not be a heavily contended lock;
3411 * if we get the occasional sleep here, we'll go an extra iteration
3412 * in the wait_event_interruptible(), which is harmless.
3413 */
3414 sched_annotate_sleep();
Rusty Russell9bb9c3b2012-09-28 14:31:03 +09303415 mutex_lock(&module_mutex);
Mathias Krause4f6de4d52013-07-02 15:35:11 +09303416 mod = find_module_all(name, strlen(name), true);
Prarit Bhargava6e6de3d2019-05-29 07:26:25 -04003417 ret = !mod || mod->state == MODULE_STATE_LIVE;
Rusty Russell9bb9c3b2012-09-28 14:31:03 +09303418 mutex_unlock(&module_mutex);
3419
3420 return ret;
3421}
3422
Peter Oberparleiterb99b87f2009-06-17 16:28:03 -07003423/* Call module constructors. */
3424static void do_mod_ctors(struct module *mod)
3425{
3426#ifdef CONFIG_CONSTRUCTORS
3427 unsigned long i;
3428
3429 for (i = 0; i < mod->num_ctors; i++)
3430 mod->ctors[i]();
3431#endif
3432}
3433
Rusty Russellc7496372015-01-20 09:07:05 +10303434/* For freeing module_init on success, in case kallsyms traversing */
3435struct mod_initfree {
Rick Edgecombe1a7b7d92019-04-25 17:11:37 -07003436 struct llist_node node;
Rusty Russellc7496372015-01-20 09:07:05 +10303437 void *module_init;
3438};
3439
Rick Edgecombe1a7b7d92019-04-25 17:11:37 -07003440static void do_free_init(struct work_struct *w)
Rusty Russellc7496372015-01-20 09:07:05 +10303441{
Rick Edgecombe1a7b7d92019-04-25 17:11:37 -07003442 struct llist_node *pos, *n, *list;
3443 struct mod_initfree *initfree;
3444
3445 list = llist_del_all(&init_free_list);
3446
3447 synchronize_rcu();
3448
3449 llist_for_each_safe(pos, n, list) {
3450 initfree = container_of(pos, struct mod_initfree, node);
3451 module_memfree(initfree->module_init);
3452 kfree(initfree);
3453 }
Rusty Russellc7496372015-01-20 09:07:05 +10303454}
3455
Rick Edgecombe1a7b7d92019-04-25 17:11:37 -07003456static int __init modules_wq_init(void)
3457{
3458 INIT_WORK(&init_free_wq, do_free_init);
3459 init_llist_head(&init_free_list);
3460 return 0;
3461}
3462module_init(modules_wq_init);
3463
Jan Kiszkabe02a182015-02-17 13:46:50 -08003464/*
3465 * This is where the real work happens.
3466 *
3467 * Keep it uninlined to provide a reliable breakpoint target, e.g. for the gdb
3468 * helper command 'lx-symbols'.
3469 */
3470static noinline int do_init_module(struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003471{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003472 int ret = 0;
Rusty Russellc7496372015-01-20 09:07:05 +10303473 struct mod_initfree *freeinit;
3474
3475 freeinit = kmalloc(sizeof(*freeinit), GFP_KERNEL);
3476 if (!freeinit) {
3477 ret = -ENOMEM;
3478 goto fail;
3479 }
Rusty Russell7523e4d2015-11-26 09:44:08 +10303480 freeinit->module_init = mod->init_layout.base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003481
Tejun Heo774a1222013-01-15 18:52:51 -08003482 /*
3483 * We want to find out whether @mod uses async during init. Clear
3484 * PF_USED_ASYNC. async_schedule*() will set it.
3485 */
3486 current->flags &= ~PF_USED_ASYNC;
3487
Peter Oberparleiterb99b87f2009-06-17 16:28:03 -07003488 do_mod_ctors(mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003489 /* Start the module */
3490 if (mod->init != NULL)
Arjan van de Ven59f94152008-07-30 12:49:02 -07003491 ret = do_one_initcall(mod->init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003492 if (ret < 0) {
Rusty Russellc7496372015-01-20 09:07:05 +10303493 goto fail_free_freeinit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003494 }
Alexey Dobriyane24e2e62008-03-10 11:43:53 -07003495 if (ret > 0) {
Andrew Mortonbddb12b2013-11-12 15:11:28 -08003496 pr_warn("%s: '%s'->init suspiciously returned %d, it should "
3497 "follow 0/-E convention\n"
3498 "%s: loading module anyway...\n",
3499 __func__, mod->name, ret, __func__);
Alexey Dobriyane24e2e62008-03-10 11:43:53 -07003500 dump_stack();
3501 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003502
Rusty Russell6f139092012-09-28 14:31:03 +09303503 /* Now it's a first class citizen! */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003504 mod->state = MODULE_STATE_LIVE;
Masami Hiramatsu0deddf42009-01-06 14:41:54 -08003505 blocking_notifier_call_chain(&module_notify_list,
3506 MODULE_STATE_LIVE, mod);
Rusty Russell6c5db222008-03-10 11:43:52 -07003507
Tejun Heo774a1222013-01-15 18:52:51 -08003508 /*
3509 * We need to finish all async code before the module init sequence
3510 * is done. This has potential to deadlock. For example, a newly
3511 * detected block device can trigger request_module() of the
3512 * default iosched from async probing task. Once userland helper
3513 * reaches here, async_synchronize_full() will wait on the async
3514 * task waiting on request_module() and deadlock.
3515 *
3516 * This deadlock is avoided by perfomring async_synchronize_full()
3517 * iff module init queued any async jobs. This isn't a full
3518 * solution as it will deadlock the same if module loading from
3519 * async jobs nests more than once; however, due to the various
3520 * constraints, this hack seems to be the best option for now.
3521 * Please refer to the following thread for details.
3522 *
3523 * http://thread.gmane.org/gmane.linux.kernel/1420814
3524 */
Luis R. Rodriguezf2411da2015-03-30 16:20:05 -07003525 if (!mod->async_probe_requested && (current->flags & PF_USED_ASYNC))
Tejun Heo774a1222013-01-15 18:52:51 -08003526 async_synchronize_full();
Linus Torvaldsd6de2c82009-04-10 12:17:41 -07003527
Steven Rostedt (VMware)aba4b5c2017-09-01 08:35:38 -04003528 ftrace_free_mem(mod, mod->init_layout.base, mod->init_layout.base +
Steven Rostedt (VMware)3e234282017-03-03 18:00:22 -05003529 mod->init_layout.size);
Rusty Russell6c5db222008-03-10 11:43:52 -07003530 mutex_lock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003531 /* Drop initial reference. */
3532 module_put(mod);
Rusty Russellad6561d2009-06-12 21:47:03 -06003533 trim_init_extable(mod);
Jan Beulich4a496222009-07-06 14:50:42 +01003534#ifdef CONFIG_KALLSYMS
Rusty Russell82440622016-02-03 16:55:26 +10303535 /* Switch to core kallsyms now init is done: kallsyms may be walking! */
3536 rcu_assign_pointer(mod->kallsyms, &mod->core_kallsyms);
Jan Beulich4a496222009-07-06 14:50:42 +01003537#endif
Jessica Yu444d13f2016-07-27 12:06:21 +09303538 module_enable_ro(mod, true);
Peter Zijlstra93c2e102015-05-27 11:09:37 +09303539 mod_tree_remove_init(mod);
Rusty Russelld453cde2015-01-20 09:07:04 +10303540 module_arch_freeing_init(mod);
Rusty Russell7523e4d2015-11-26 09:44:08 +10303541 mod->init_layout.base = NULL;
3542 mod->init_layout.size = 0;
3543 mod->init_layout.ro_size = 0;
Jessica Yu444d13f2016-07-27 12:06:21 +09303544 mod->init_layout.ro_after_init_size = 0;
Rusty Russell7523e4d2015-11-26 09:44:08 +10303545 mod->init_layout.text_size = 0;
Rusty Russellc7496372015-01-20 09:07:05 +10303546 /*
3547 * We want to free module_init, but be aware that kallsyms may be
Peter Zijlstra0be964b2015-05-27 11:09:35 +09303548 * walking this with preempt disabled. In all the failure paths, we
Paul E. McKenneycb2f5532018-11-06 19:17:01 -08003549 * call synchronize_rcu(), but we don't want to slow down the success
Rick Edgecombe1a7b7d92019-04-25 17:11:37 -07003550 * path. module_memfree() cannot be called in an interrupt, so do the
3551 * work and call synchronize_rcu() in a work queue.
3552 *
Jeffrey Hugoae646f02018-05-11 16:01:42 -07003553 * Note that module_alloc() on most architectures creates W+X page
3554 * mappings which won't be cleaned up until do_free_init() runs. Any
3555 * code such as mark_rodata_ro() which depends on those mappings to
3556 * be cleaned up needs to sync with the queued work - ie
Paul E. McKenneycb2f5532018-11-06 19:17:01 -08003557 * rcu_barrier()
Rusty Russellc7496372015-01-20 09:07:05 +10303558 */
Rick Edgecombe1a7b7d92019-04-25 17:11:37 -07003559 if (llist_add(&freeinit->node, &init_free_list))
3560 schedule_work(&init_free_wq);
3561
Ashutosh Naik6389a382006-03-23 03:00:46 -08003562 mutex_unlock(&module_mutex);
Rusty Russell6f139092012-09-28 14:31:03 +09303563 wake_up_all(&module_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003564
3565 return 0;
Rusty Russellc7496372015-01-20 09:07:05 +10303566
3567fail_free_freeinit:
3568 kfree(freeinit);
3569fail:
3570 /* Try to protect us from buggy refcounters. */
3571 mod->state = MODULE_STATE_GOING;
Paul E. McKenneycb2f5532018-11-06 19:17:01 -08003572 synchronize_rcu();
Rusty Russellc7496372015-01-20 09:07:05 +10303573 module_put(mod);
3574 blocking_notifier_call_chain(&module_notify_list,
3575 MODULE_STATE_GOING, mod);
Jessica Yu7e545d62016-03-16 20:55:39 -04003576 klp_module_going(mod);
Jessica Yu7dcd1822016-02-16 17:32:33 -05003577 ftrace_release_mod(mod);
Rusty Russellc7496372015-01-20 09:07:05 +10303578 free_module(mod);
3579 wake_up_all(&module_wq);
3580 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003581}
3582
Kees Cook34e11692012-10-16 07:31:07 +10303583static int may_init_module(void)
3584{
3585 if (!capable(CAP_SYS_MODULE) || modules_disabled)
3586 return -EPERM;
3587
3588 return 0;
3589}
3590
Rusty Russella3535c72013-01-21 17:18:59 +10303591/*
3592 * We try to place it in the list now to make sure it's unique before
3593 * we dedicate too many resources. In particular, temporary percpu
3594 * memory exhaustion.
3595 */
3596static int add_unformed_module(struct module *mod)
3597{
3598 int err;
3599 struct module *old;
3600
3601 mod->state = MODULE_STATE_UNFORMED;
3602
3603again:
3604 mutex_lock(&module_mutex);
Mathias Krause4f6de4d52013-07-02 15:35:11 +09303605 old = find_module_all(mod->name, strlen(mod->name), true);
3606 if (old != NULL) {
Prarit Bhargava6e6de3d2019-05-29 07:26:25 -04003607 if (old->state != MODULE_STATE_LIVE) {
Rusty Russella3535c72013-01-21 17:18:59 +10303608 /* Wait in case it fails to load. */
3609 mutex_unlock(&module_mutex);
Peter Zijlstra9cc019b2015-02-11 15:01:13 +10303610 err = wait_event_interruptible(module_wq,
3611 finished_loading(mod->name));
Rusty Russella3535c72013-01-21 17:18:59 +10303612 if (err)
3613 goto out_unlocked;
3614 goto again;
3615 }
3616 err = -EEXIST;
3617 goto out;
3618 }
Peter Zijlstra4f6665462015-05-27 11:09:38 +09303619 mod_update_bounds(mod);
Rusty Russella3535c72013-01-21 17:18:59 +10303620 list_add_rcu(&mod->list, &modules);
Peter Zijlstra93c2e102015-05-27 11:09:37 +09303621 mod_tree_insert(mod);
Rusty Russella3535c72013-01-21 17:18:59 +10303622 err = 0;
3623
3624out:
3625 mutex_unlock(&module_mutex);
3626out_unlocked:
3627 return err;
3628}
3629
3630static int complete_formation(struct module *mod, struct load_info *info)
3631{
3632 int err;
3633
3634 mutex_lock(&module_mutex);
3635
3636 /* Find duplicate symbols (must be called under lock). */
Jessica Yu2d25bc52018-11-19 17:43:58 +01003637 err = verify_exported_symbols(mod);
Rusty Russella3535c72013-01-21 17:18:59 +10303638 if (err < 0)
3639 goto out;
3640
3641 /* This relies on module_mutex for list integrity. */
3642 module_bug_finalize(info->hdr, info->sechdrs, mod);
3643
Jessica Yu444d13f2016-07-27 12:06:21 +09303644 module_enable_ro(mod, false);
Rusty Russell85c898d2015-11-26 09:45:08 +10303645 module_enable_nx(mod);
Yang Yingliang2eef1392019-06-20 10:18:14 +08003646 module_enable_x(mod);
Rusty Russell49822232014-05-14 10:54:19 +09303647
Rusty Russella3535c72013-01-21 17:18:59 +10303648 /* Mark state as coming so strong_try_module_get() ignores us,
3649 * but kallsyms etc. can see us. */
3650 mod->state = MODULE_STATE_COMING;
Rusty Russell49822232014-05-14 10:54:19 +09303651 mutex_unlock(&module_mutex);
3652
Rusty Russell49822232014-05-14 10:54:19 +09303653 return 0;
Rusty Russella3535c72013-01-21 17:18:59 +10303654
3655out:
3656 mutex_unlock(&module_mutex);
3657 return err;
3658}
3659
Jessica Yu4c973d12016-03-16 20:55:38 -04003660static int prepare_coming_module(struct module *mod)
3661{
Jessica Yu7e545d62016-03-16 20:55:39 -04003662 int err;
3663
Jessica Yu4c973d12016-03-16 20:55:38 -04003664 ftrace_module_enable(mod);
Jessica Yu7e545d62016-03-16 20:55:39 -04003665 err = klp_module_coming(mod);
3666 if (err)
3667 return err;
3668
Jessica Yu4c973d12016-03-16 20:55:38 -04003669 blocking_notifier_call_chain(&module_notify_list,
3670 MODULE_STATE_COMING, mod);
3671 return 0;
3672}
3673
Luis R. Rodriguezecc86172015-03-30 16:20:03 -07003674static int unknown_module_param_cb(char *param, char *val, const char *modname,
3675 void *arg)
Rusty Russell54041d82013-07-02 15:35:12 +09303676{
Luis R. Rodriguezf2411da2015-03-30 16:20:05 -07003677 struct module *mod = arg;
3678 int ret;
3679
3680 if (strcmp(param, "async_probe") == 0) {
3681 mod->async_probe_requested = true;
3682 return 0;
3683 }
3684
Ionut Alexa6da0b562014-11-10 09:31:29 +10303685 /* Check for magic 'dyndbg' arg */
Luis R. Rodriguezf2411da2015-03-30 16:20:05 -07003686 ret = ddebug_dyndbg_module_param_cb(param, val, modname);
Andrew Mortonbddb12b2013-11-12 15:11:28 -08003687 if (ret != 0)
3688 pr_warn("%s: unknown parameter '%s' ignored\n", modname, param);
Rusty Russell54041d82013-07-02 15:35:12 +09303689 return 0;
3690}
3691
Kees Cook34e11692012-10-16 07:31:07 +10303692/* Allocate and load the module: note that size of section 0 is always
3693 zero, and we rely on this for optional sections. */
Rusty Russell2f3238a2012-10-22 18:09:41 +10303694static int load_module(struct load_info *info, const char __user *uargs,
3695 int flags)
Kees Cook34e11692012-10-16 07:31:07 +10303696{
Rusty Russella3535c72013-01-21 17:18:59 +10303697 struct module *mod;
Jessica Yu5fdc7db2018-06-22 14:00:01 +02003698 long err = 0;
Rusty Russell51e158c2014-04-28 11:34:33 +09303699 char *after_dashes;
Kees Cook34e11692012-10-16 07:31:07 +10303700
Jessica Yu5fdc7db2018-06-22 14:00:01 +02003701 err = elf_header_check(info);
3702 if (err)
3703 goto free_copy;
3704
3705 err = setup_load_info(info, flags);
3706 if (err)
3707 goto free_copy;
3708
3709 if (blacklisted(info->name)) {
3710 err = -EPERM;
3711 goto free_copy;
3712 }
3713
Ben Hutchingsbca014c2016-04-28 09:24:01 +09303714 err = module_sig_check(info, flags);
Kees Cook34e11692012-10-16 07:31:07 +10303715 if (err)
3716 goto free_copy;
3717
Jessica Yu5fdc7db2018-06-22 14:00:01 +02003718 err = rewrite_section_headers(info, flags);
Kees Cook34e11692012-10-16 07:31:07 +10303719 if (err)
3720 goto free_copy;
3721
Jessica Yu5fdc7db2018-06-22 14:00:01 +02003722 /* Check module struct version now, before we try to use module. */
3723 if (!check_modstruct_version(info, info->mod)) {
3724 err = -ENOEXEC;
3725 goto free_copy;
3726 }
3727
Kees Cook34e11692012-10-16 07:31:07 +10303728 /* Figure out module layout, and allocate all the memory. */
Rusty Russell2f3238a2012-10-22 18:09:41 +10303729 mod = layout_and_allocate(info, flags);
Kees Cook34e11692012-10-16 07:31:07 +10303730 if (IS_ERR(mod)) {
3731 err = PTR_ERR(mod);
3732 goto free_copy;
3733 }
3734
Richard Guy Briggsca86cad2017-02-04 13:10:38 -05003735 audit_log_kern_module(mod->name);
3736
Rusty Russella3535c72013-01-21 17:18:59 +10303737 /* Reserve our place in the list. */
3738 err = add_unformed_module(mod);
3739 if (err)
Rusty Russell1fb93412013-01-12 13:27:34 +10303740 goto free_module;
Rusty Russell1fb93412013-01-12 13:27:34 +10303741
Kees Cook34e11692012-10-16 07:31:07 +10303742#ifdef CONFIG_MODULE_SIG
3743 mod->sig_ok = info->sig_ok;
Rusty Russell64748a22013-01-21 17:03:02 +10303744 if (!mod->sig_ok) {
Andrew Mortonbddb12b2013-11-12 15:11:28 -08003745 pr_notice_once("%s: module verification failed: signature "
Marcel Holtmannab92ebb2015-02-06 15:09:57 +10303746 "and/or required key missing - tainting "
Andrew Mortonbddb12b2013-11-12 15:11:28 -08003747 "kernel\n", mod->name);
Mathieu Desnoyers66cc69e2014-03-13 12:11:30 +10303748 add_taint_module(mod, TAINT_UNSIGNED_MODULE, LOCKDEP_STILL_OK);
Rusty Russell64748a22013-01-21 17:03:02 +10303749 }
Kees Cook34e11692012-10-16 07:31:07 +10303750#endif
3751
Rusty Russell8d8022e2013-07-03 10:06:28 +09303752 /* To avoid stressing percpu allocator, do this once we're unique. */
Rusty Russell9eb76d72013-07-03 10:06:29 +09303753 err = percpu_modalloc(mod, info);
Rusty Russell8d8022e2013-07-03 10:06:28 +09303754 if (err)
3755 goto unlink_mod;
3756
Kees Cook34e11692012-10-16 07:31:07 +10303757 /* Now module is in final location, initialize linked lists, etc. */
3758 err = module_unload_init(mod);
3759 if (err)
Rusty Russell1fb93412013-01-12 13:27:34 +10303760 goto unlink_mod;
Kees Cook34e11692012-10-16 07:31:07 +10303761
Rusty Russellcf2fde72015-06-26 06:44:38 +09303762 init_param_lock(mod);
Dan Streetmanb51d23e2015-06-17 06:18:52 +09303763
Kees Cook34e11692012-10-16 07:31:07 +10303764 /* Now we've got everything in the final locations, we can
3765 * find optional sections. */
Frantisek Hrbataeb3057d2013-10-14 18:08:46 +10303766 err = find_module_sections(mod, info);
3767 if (err)
3768 goto free_unload;
Kees Cook34e11692012-10-16 07:31:07 +10303769
3770 err = check_module_license_and_versions(mod);
3771 if (err)
3772 goto free_unload;
3773
3774 /* Set up MODINFO_ATTR fields */
3775 setup_modinfo(mod, info);
3776
3777 /* Fix up syms, so that st_value is a pointer to location. */
3778 err = simplify_symbols(mod, info);
3779 if (err < 0)
3780 goto free_modinfo;
3781
3782 err = apply_relocations(mod, info);
3783 if (err < 0)
3784 goto free_modinfo;
3785
3786 err = post_relocation(mod, info);
3787 if (err < 0)
3788 goto free_modinfo;
3789
3790 flush_module_icache(mod);
3791
3792 /* Now copy in args */
3793 mod->args = strndup_user(uargs, ~0UL >> 1);
3794 if (IS_ERR(mod->args)) {
3795 err = PTR_ERR(mod->args);
3796 goto free_arch_cleanup;
3797 }
3798
Zhou Chengming52796312017-07-07 11:15:58 +08003799 dynamic_debug_setup(mod, info->debug, info->num_debug);
Kees Cook34e11692012-10-16 07:31:07 +10303800
Steven Rostedt (Red Hat)a949ae52014-04-24 10:40:12 -04003801 /* Ftrace init must be called in the MODULE_STATE_UNFORMED state */
3802 ftrace_module_init(mod);
3803
Rusty Russella3535c72013-01-21 17:18:59 +10303804 /* Finally it's fully formed, ready to start executing. */
3805 err = complete_formation(mod, info);
3806 if (err)
Rusty Russell1fb93412013-01-12 13:27:34 +10303807 goto ddebug_cleanup;
Kees Cook34e11692012-10-16 07:31:07 +10303808
Jessica Yu4c973d12016-03-16 20:55:38 -04003809 err = prepare_coming_module(mod);
3810 if (err)
3811 goto bug_cleanup;
3812
Kees Cook34e11692012-10-16 07:31:07 +10303813 /* Module is ready to execute: parsing args may do that. */
Rusty Russell51e158c2014-04-28 11:34:33 +09303814 after_dashes = parse_args(mod->name, mod->args, mod->kp, mod->num_kp,
Luis R. Rodriguez4355efb2016-02-03 16:55:26 +10303815 -32768, 32767, mod,
Luis R. Rodriguezecc86172015-03-30 16:20:03 -07003816 unknown_module_param_cb);
Rusty Russell51e158c2014-04-28 11:34:33 +09303817 if (IS_ERR(after_dashes)) {
3818 err = PTR_ERR(after_dashes);
Jessica Yu4c973d12016-03-16 20:55:38 -04003819 goto coming_cleanup;
Rusty Russell51e158c2014-04-28 11:34:33 +09303820 } else if (after_dashes) {
3821 pr_warn("%s: parameters '%s' after `--' ignored\n",
3822 mod->name, after_dashes);
3823 }
Kees Cook34e11692012-10-16 07:31:07 +10303824
Richard Guy Briggsca86cad2017-02-04 13:10:38 -05003825 /* Link in to sysfs. */
Kees Cook34e11692012-10-16 07:31:07 +10303826 err = mod_sysfs_setup(mod, info, mod->kp, mod->num_kp);
3827 if (err < 0)
Jessica Yu4c973d12016-03-16 20:55:38 -04003828 goto coming_cleanup;
Kees Cook34e11692012-10-16 07:31:07 +10303829
Jessica Yu1ce15ef2016-03-22 20:03:16 -04003830 if (is_livepatch_module(mod)) {
3831 err = copy_module_elf(mod, info);
3832 if (err < 0)
3833 goto sysfs_cleanup;
3834 }
3835
Kees Cook34e11692012-10-16 07:31:07 +10303836 /* Get rid of temporary copy. */
3837 free_copy(info);
3838
3839 /* Done! */
3840 trace_module_load(mod);
3841
3842 return do_init_module(mod);
3843
Jessica Yu1ce15ef2016-03-22 20:03:16 -04003844 sysfs_cleanup:
3845 mod_sysfs_teardown(mod);
Jessica Yu4c973d12016-03-16 20:55:38 -04003846 coming_cleanup:
Aaron Tomlin885a78d2016-10-20 17:18:12 +01003847 mod->state = MODULE_STATE_GOING;
Luis R. Rodrigueza5544882017-02-10 14:06:22 -08003848 destroy_params(mod->kp, mod->num_kp);
Jessica Yu4c973d12016-03-16 20:55:38 -04003849 blocking_notifier_call_chain(&module_notify_list,
3850 MODULE_STATE_GOING, mod);
Jessica Yu7e545d62016-03-16 20:55:39 -04003851 klp_module_going(mod);
Rusty Russell1fb93412013-01-12 13:27:34 +10303852 bug_cleanup:
3853 /* module_bug_cleanup needs module_mutex protection */
Kees Cook34e11692012-10-16 07:31:07 +10303854 mutex_lock(&module_mutex);
Kees Cook34e11692012-10-16 07:31:07 +10303855 module_bug_cleanup(mod);
Linus Torvaldsee61abb2013-01-20 20:22:58 -08003856 mutex_unlock(&module_mutex);
Andy Lutomirskiff7e0052014-08-16 04:13:37 +09303857
Rusty Russella3535c72013-01-21 17:18:59 +10303858 ddebug_cleanup:
Namit Gupta1323eac2018-01-08 10:41:21 +05303859 ftrace_release_mod(mod);
Zhou Chengming52796312017-07-07 11:15:58 +08003860 dynamic_debug_remove(mod, info->debug);
Paul E. McKenneycb2f5532018-11-06 19:17:01 -08003861 synchronize_rcu();
Kees Cook34e11692012-10-16 07:31:07 +10303862 kfree(mod->args);
3863 free_arch_cleanup:
3864 module_arch_cleanup(mod);
3865 free_modinfo:
3866 free_modinfo(mod);
3867 free_unload:
3868 module_unload_free(mod);
Rusty Russell1fb93412013-01-12 13:27:34 +10303869 unlink_mod:
3870 mutex_lock(&module_mutex);
3871 /* Unlink carefully: kallsyms could be walking list. */
3872 list_del_rcu(&mod->list);
Peter Zijlstra758556b2015-07-09 06:48:06 +09303873 mod_tree_remove(mod);
Rusty Russell1fb93412013-01-12 13:27:34 +10303874 wake_up_all(&module_wq);
Peter Zijlstra0be964b2015-05-27 11:09:35 +09303875 /* Wait for RCU-sched synchronizing before releasing mod->list. */
Paul E. McKenneycb2f5532018-11-06 19:17:01 -08003876 synchronize_rcu();
Rusty Russell1fb93412013-01-12 13:27:34 +10303877 mutex_unlock(&module_mutex);
Kees Cook34e11692012-10-16 07:31:07 +10303878 free_module:
Peter Zijlstra35a93932015-02-26 16:23:11 +01003879 /* Free lock-classes; relies on the preceding sync_rcu() */
Rusty Russell7523e4d2015-11-26 09:44:08 +10303880 lockdep_free_key_range(mod->core_layout.base, mod->core_layout.size);
Peter Zijlstra35a93932015-02-26 16:23:11 +01003881
Kees Cook34e11692012-10-16 07:31:07 +10303882 module_deallocate(mod, info);
3883 free_copy:
3884 free_copy(info);
3885 return err;
3886}
3887
3888SYSCALL_DEFINE3(init_module, void __user *, umod,
3889 unsigned long, len, const char __user *, uargs)
3890{
3891 int err;
3892 struct load_info info = { };
3893
3894 err = may_init_module();
3895 if (err)
3896 return err;
3897
3898 pr_debug("init_module: umod=%p, len=%lu, uargs=%p\n",
3899 umod, len, uargs);
3900
3901 err = copy_module_from_user(umod, len, &info);
3902 if (err)
3903 return err;
3904
Rusty Russell2f3238a2012-10-22 18:09:41 +10303905 return load_module(&info, uargs, 0);
Kees Cook34e11692012-10-16 07:31:07 +10303906}
3907
Rusty Russell2f3238a2012-10-22 18:09:41 +10303908SYSCALL_DEFINE3(finit_module, int, fd, const char __user *, uargs, int, flags)
Kees Cook34e11692012-10-16 07:31:07 +10303909{
Kees Cook34e11692012-10-16 07:31:07 +10303910 struct load_info info = { };
Mimi Zohara1db7422015-12-30 07:35:30 -05003911 loff_t size;
3912 void *hdr;
3913 int err;
Kees Cook34e11692012-10-16 07:31:07 +10303914
3915 err = may_init_module();
3916 if (err)
3917 return err;
3918
Rusty Russell2f3238a2012-10-22 18:09:41 +10303919 pr_debug("finit_module: fd=%d, uargs=%p, flags=%i\n", fd, uargs, flags);
3920
3921 if (flags & ~(MODULE_INIT_IGNORE_MODVERSIONS
3922 |MODULE_INIT_IGNORE_VERMAGIC))
3923 return -EINVAL;
Kees Cook34e11692012-10-16 07:31:07 +10303924
Mimi Zohara1db7422015-12-30 07:35:30 -05003925 err = kernel_read_file_from_fd(fd, &hdr, &size, INT_MAX,
3926 READING_MODULE);
Kees Cook34e11692012-10-16 07:31:07 +10303927 if (err)
3928 return err;
Mimi Zohara1db7422015-12-30 07:35:30 -05003929 info.hdr = hdr;
3930 info.len = size;
Kees Cook34e11692012-10-16 07:31:07 +10303931
Rusty Russell2f3238a2012-10-22 18:09:41 +10303932 return load_module(&info, uargs, flags);
Kees Cook34e11692012-10-16 07:31:07 +10303933}
3934
Linus Torvalds1da177e2005-04-16 15:20:36 -07003935static inline int within(unsigned long addr, void *start, unsigned long size)
3936{
3937 return ((void *)addr >= start && (void *)addr < start + size);
3938}
3939
3940#ifdef CONFIG_KALLSYMS
3941/*
3942 * This ignores the intensely annoying "mapping symbols" found
3943 * in ARM ELF files: $a, $t and $d.
3944 */
3945static inline int is_arm_mapping_symbol(const char *str)
3946{
Russell King2e3a10a2014-07-27 07:29:01 +09303947 if (str[0] == '.' && str[1] == 'L')
3948 return true;
Kyle McMartin6c34f1f2014-09-16 22:37:18 +01003949 return str[0] == '$' && strchr("axtd", str[1])
Linus Torvalds1da177e2005-04-16 15:20:36 -07003950 && (str[2] == '\0' || str[2] == '.');
3951}
3952
Jessica Yu2d25bc52018-11-19 17:43:58 +01003953static const char *kallsyms_symbol_name(struct mod_kallsyms *kallsyms, unsigned int symnum)
Rusty Russell2e7bac52016-02-03 16:55:26 +10303954{
Rusty Russell82440622016-02-03 16:55:26 +10303955 return kallsyms->strtab + kallsyms->symtab[symnum].st_name;
Rusty Russell2e7bac52016-02-03 16:55:26 +10303956}
3957
Jessica Yu2d25bc52018-11-19 17:43:58 +01003958/*
3959 * Given a module and address, find the corresponding symbol and return its name
3960 * while providing its size and offset if needed.
3961 */
3962static const char *find_kallsyms_symbol(struct module *mod,
3963 unsigned long addr,
3964 unsigned long *size,
3965 unsigned long *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003966{
3967 unsigned int i, best = 0;
Vincent Whitchurch93d77e72018-12-14 17:05:55 +01003968 unsigned long nextval, bestval;
Rusty Russell82440622016-02-03 16:55:26 +10303969 struct mod_kallsyms *kallsyms = rcu_dereference_sched(mod->kallsyms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003970
3971 /* At worse, next value is at end of module */
Masami Hiramatsua06f6212009-01-06 14:41:49 -08003972 if (within_module_init(addr, mod))
Rusty Russell7523e4d2015-11-26 09:44:08 +10303973 nextval = (unsigned long)mod->init_layout.base+mod->init_layout.text_size;
Daniel Walker22a8bde2007-10-18 03:06:07 -07003974 else
Rusty Russell7523e4d2015-11-26 09:44:08 +10303975 nextval = (unsigned long)mod->core_layout.base+mod->core_layout.text_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003976
Vincent Whitchurch93d77e72018-12-14 17:05:55 +01003977 bestval = kallsyms_symbol_value(&kallsyms->symtab[best]);
3978
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003979 /* Scan for closest preceding symbol, and next symbol. (ELF
Daniel Walker22a8bde2007-10-18 03:06:07 -07003980 starts real symbols at 1). */
Rusty Russell82440622016-02-03 16:55:26 +10303981 for (i = 1; i < kallsyms->num_symtab; i++) {
Vincent Whitchurch93d77e72018-12-14 17:05:55 +01003982 const Elf_Sym *sym = &kallsyms->symtab[i];
3983 unsigned long thisval = kallsyms_symbol_value(sym);
3984
3985 if (sym->st_shndx == SHN_UNDEF)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003986 continue;
3987
3988 /* We ignore unnamed symbols: they're uninformative
3989 * and inserted at a whim. */
Jessica Yu2d25bc52018-11-19 17:43:58 +01003990 if (*kallsyms_symbol_name(kallsyms, i) == '\0'
3991 || is_arm_mapping_symbol(kallsyms_symbol_name(kallsyms, i)))
Rusty Russell2e7bac52016-02-03 16:55:26 +10303992 continue;
3993
Vincent Whitchurch93d77e72018-12-14 17:05:55 +01003994 if (thisval <= addr && thisval > bestval) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003995 best = i;
Vincent Whitchurch93d77e72018-12-14 17:05:55 +01003996 bestval = thisval;
3997 }
3998 if (thisval > addr && thisval < nextval)
3999 nextval = thisval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004000 }
4001
4002 if (!best)
4003 return NULL;
4004
Alexey Dobriyanffb45122007-05-08 00:28:41 -07004005 if (size)
Vincent Whitchurch93d77e72018-12-14 17:05:55 +01004006 *size = nextval - bestval;
Alexey Dobriyanffb45122007-05-08 00:28:41 -07004007 if (offset)
Vincent Whitchurch93d77e72018-12-14 17:05:55 +01004008 *offset = addr - bestval;
Jessica Yu2d25bc52018-11-19 17:43:58 +01004009
4010 return kallsyms_symbol_name(kallsyms, best);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004011}
4012
Sergey Senozhatskyb865ea62017-11-10 08:48:25 +09004013void * __weak dereference_module_function_descriptor(struct module *mod,
4014 void *ptr)
4015{
4016 return ptr;
4017}
4018
Rusty Russell6dd06c92008-01-29 17:13:22 -05004019/* For kallsyms to ask for address resolution. NULL means not found. Careful
4020 * not to lock to avoid deadlock on oopses, simply disable preemption. */
Andrew Morton92dfc9d2008-02-08 04:18:43 -08004021const char *module_address_lookup(unsigned long addr,
Rusty Russell6dd06c92008-01-29 17:13:22 -05004022 unsigned long *size,
4023 unsigned long *offset,
4024 char **modname,
4025 char *namebuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004026{
Rusty Russellcb2a5202008-01-14 00:55:03 -08004027 const char *ret = NULL;
Peter Zijlstrab7df4d12015-05-27 11:09:37 +09304028 struct module *mod;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004029
Rusty Russellcb2a5202008-01-14 00:55:03 -08004030 preempt_disable();
Peter Zijlstrab7df4d12015-05-27 11:09:37 +09304031 mod = __module_address(addr);
4032 if (mod) {
4033 if (modname)
4034 *modname = mod->name;
Jessica Yu2d25bc52018-11-19 17:43:58 +01004035
4036 ret = find_kallsyms_symbol(mod, addr, size, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004037 }
Rusty Russell6dd06c92008-01-29 17:13:22 -05004038 /* Make a copy in here where it's safe */
4039 if (ret) {
4040 strncpy(namebuf, ret, KSYM_NAME_LEN - 1);
4041 ret = namebuf;
4042 }
Rusty Russellcb2a5202008-01-14 00:55:03 -08004043 preempt_enable();
Peter Zijlstrab7df4d12015-05-27 11:09:37 +09304044
Andrew Morton92dfc9d2008-02-08 04:18:43 -08004045 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004046}
4047
Alexey Dobriyan9d65cb42007-05-08 00:28:43 -07004048int lookup_module_symbol_name(unsigned long addr, char *symname)
4049{
4050 struct module *mod;
4051
Rusty Russellcb2a5202008-01-14 00:55:03 -08004052 preempt_disable();
Andi Kleend72b3752008-08-30 10:09:00 +02004053 list_for_each_entry_rcu(mod, &modules, list) {
Rusty Russell0d21b0e2013-01-12 11:38:44 +10304054 if (mod->state == MODULE_STATE_UNFORMED)
4055 continue;
Petr Mladek9b20a352014-07-27 07:24:01 +09304056 if (within_module(addr, mod)) {
Alexey Dobriyan9d65cb42007-05-08 00:28:43 -07004057 const char *sym;
4058
Jessica Yu2d25bc52018-11-19 17:43:58 +01004059 sym = find_kallsyms_symbol(mod, addr, NULL, NULL);
Alexey Dobriyan9d65cb42007-05-08 00:28:43 -07004060 if (!sym)
4061 goto out;
Jessica Yu2d25bc52018-11-19 17:43:58 +01004062
Tejun Heo9281ace2007-07-17 04:03:51 -07004063 strlcpy(symname, sym, KSYM_NAME_LEN);
Rusty Russellcb2a5202008-01-14 00:55:03 -08004064 preempt_enable();
Alexey Dobriyan9d65cb42007-05-08 00:28:43 -07004065 return 0;
4066 }
4067 }
4068out:
Rusty Russellcb2a5202008-01-14 00:55:03 -08004069 preempt_enable();
Alexey Dobriyan9d65cb42007-05-08 00:28:43 -07004070 return -ERANGE;
4071}
4072
Alexey Dobriyana5c43da2007-05-08 00:28:47 -07004073int lookup_module_symbol_attrs(unsigned long addr, unsigned long *size,
4074 unsigned long *offset, char *modname, char *name)
4075{
4076 struct module *mod;
4077
Rusty Russellcb2a5202008-01-14 00:55:03 -08004078 preempt_disable();
Andi Kleend72b3752008-08-30 10:09:00 +02004079 list_for_each_entry_rcu(mod, &modules, list) {
Rusty Russell0d21b0e2013-01-12 11:38:44 +10304080 if (mod->state == MODULE_STATE_UNFORMED)
4081 continue;
Petr Mladek9b20a352014-07-27 07:24:01 +09304082 if (within_module(addr, mod)) {
Alexey Dobriyana5c43da2007-05-08 00:28:47 -07004083 const char *sym;
4084
Jessica Yu2d25bc52018-11-19 17:43:58 +01004085 sym = find_kallsyms_symbol(mod, addr, size, offset);
Alexey Dobriyana5c43da2007-05-08 00:28:47 -07004086 if (!sym)
4087 goto out;
4088 if (modname)
Tejun Heo9281ace2007-07-17 04:03:51 -07004089 strlcpy(modname, mod->name, MODULE_NAME_LEN);
Alexey Dobriyana5c43da2007-05-08 00:28:47 -07004090 if (name)
Tejun Heo9281ace2007-07-17 04:03:51 -07004091 strlcpy(name, sym, KSYM_NAME_LEN);
Rusty Russellcb2a5202008-01-14 00:55:03 -08004092 preempt_enable();
Alexey Dobriyana5c43da2007-05-08 00:28:47 -07004093 return 0;
4094 }
4095 }
4096out:
Rusty Russellcb2a5202008-01-14 00:55:03 -08004097 preempt_enable();
Alexey Dobriyana5c43da2007-05-08 00:28:47 -07004098 return -ERANGE;
4099}
4100
Alexey Dobriyanea078902007-05-08 00:28:39 -07004101int module_get_kallsym(unsigned int symnum, unsigned long *value, char *type,
4102 char *name, char *module_name, int *exported)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004103{
4104 struct module *mod;
4105
Rusty Russellcb2a5202008-01-14 00:55:03 -08004106 preempt_disable();
Andi Kleend72b3752008-08-30 10:09:00 +02004107 list_for_each_entry_rcu(mod, &modules, list) {
Rusty Russell82440622016-02-03 16:55:26 +10304108 struct mod_kallsyms *kallsyms;
4109
Rusty Russell0d21b0e2013-01-12 11:38:44 +10304110 if (mod->state == MODULE_STATE_UNFORMED)
4111 continue;
Rusty Russell82440622016-02-03 16:55:26 +10304112 kallsyms = rcu_dereference_sched(mod->kallsyms);
4113 if (symnum < kallsyms->num_symtab) {
Vincent Whitchurch93d77e72018-12-14 17:05:55 +01004114 const Elf_Sym *sym = &kallsyms->symtab[symnum];
4115
4116 *value = kallsyms_symbol_value(sym);
Eugene Loh1c7651f2019-02-25 11:59:58 -08004117 *type = kallsyms->typetab[symnum];
Jessica Yu2d25bc52018-11-19 17:43:58 +01004118 strlcpy(name, kallsyms_symbol_name(kallsyms, symnum), KSYM_NAME_LEN);
Tejun Heo9281ace2007-07-17 04:03:51 -07004119 strlcpy(module_name, mod->name, MODULE_NAME_LEN);
Tim Abbottca4787b2009-01-05 08:40:10 -06004120 *exported = is_exported(name, *value, mod);
Rusty Russellcb2a5202008-01-14 00:55:03 -08004121 preempt_enable();
Alexey Dobriyanea078902007-05-08 00:28:39 -07004122 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004123 }
Rusty Russell82440622016-02-03 16:55:26 +10304124 symnum -= kallsyms->num_symtab;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004125 }
Rusty Russellcb2a5202008-01-14 00:55:03 -08004126 preempt_enable();
Alexey Dobriyanea078902007-05-08 00:28:39 -07004127 return -ERANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004128}
4129
Jessica Yu2d25bc52018-11-19 17:43:58 +01004130/* Given a module and name of symbol, find and return the symbol's value */
4131static unsigned long find_kallsyms_symbol_value(struct module *mod, const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004132{
4133 unsigned int i;
Rusty Russell82440622016-02-03 16:55:26 +10304134 struct mod_kallsyms *kallsyms = rcu_dereference_sched(mod->kallsyms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004135
Vincent Whitchurch93d77e72018-12-14 17:05:55 +01004136 for (i = 0; i < kallsyms->num_symtab; i++) {
4137 const Elf_Sym *sym = &kallsyms->symtab[i];
4138
Jessica Yu2d25bc52018-11-19 17:43:58 +01004139 if (strcmp(name, kallsyms_symbol_name(kallsyms, i)) == 0 &&
Vincent Whitchurch93d77e72018-12-14 17:05:55 +01004140 sym->st_shndx != SHN_UNDEF)
4141 return kallsyms_symbol_value(sym);
4142 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004143 return 0;
4144}
4145
4146/* Look for this name: can be of form module:name. */
4147unsigned long module_kallsyms_lookup_name(const char *name)
4148{
4149 struct module *mod;
4150 char *colon;
4151 unsigned long ret = 0;
4152
4153 /* Don't lock: we're in enough trouble already. */
Rusty Russellcb2a5202008-01-14 00:55:03 -08004154 preempt_disable();
Naveen N. Rao17586182017-04-23 22:53:43 +05304155 if ((colon = strnchr(name, MODULE_NAME_LEN, ':')) != NULL) {
Mathias Krause4f6de4d52013-07-02 15:35:11 +09304156 if ((mod = find_module_all(name, colon - name, false)) != NULL)
Jessica Yu2d25bc52018-11-19 17:43:58 +01004157 ret = find_kallsyms_symbol_value(mod, colon+1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004158 } else {
Rusty Russell0d21b0e2013-01-12 11:38:44 +10304159 list_for_each_entry_rcu(mod, &modules, list) {
4160 if (mod->state == MODULE_STATE_UNFORMED)
4161 continue;
Jessica Yu2d25bc52018-11-19 17:43:58 +01004162 if ((ret = find_kallsyms_symbol_value(mod, name)) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004163 break;
Rusty Russell0d21b0e2013-01-12 11:38:44 +10304164 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004165 }
Rusty Russellcb2a5202008-01-14 00:55:03 -08004166 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004167 return ret;
4168}
Anders Kaseorg75a66612008-12-05 19:03:58 -05004169
4170int module_kallsyms_on_each_symbol(int (*fn)(void *, const char *,
4171 struct module *, unsigned long),
4172 void *data)
4173{
4174 struct module *mod;
4175 unsigned int i;
4176 int ret;
4177
Peter Zijlstra0be964b2015-05-27 11:09:35 +09304178 module_assert_mutex();
4179
Anders Kaseorg75a66612008-12-05 19:03:58 -05004180 list_for_each_entry(mod, &modules, list) {
Rusty Russell82440622016-02-03 16:55:26 +10304181 /* We hold module_mutex: no need for rcu_dereference_sched */
4182 struct mod_kallsyms *kallsyms = mod->kallsyms;
4183
Rusty Russell0d21b0e2013-01-12 11:38:44 +10304184 if (mod->state == MODULE_STATE_UNFORMED)
4185 continue;
Rusty Russell82440622016-02-03 16:55:26 +10304186 for (i = 0; i < kallsyms->num_symtab; i++) {
Vincent Whitchurch93d77e72018-12-14 17:05:55 +01004187 const Elf_Sym *sym = &kallsyms->symtab[i];
Jessica Yu9f2d1e62018-06-05 10:22:52 +02004188
Vincent Whitchurch93d77e72018-12-14 17:05:55 +01004189 if (sym->st_shndx == SHN_UNDEF)
Jessica Yu9f2d1e62018-06-05 10:22:52 +02004190 continue;
4191
Jessica Yu2d25bc52018-11-19 17:43:58 +01004192 ret = fn(data, kallsyms_symbol_name(kallsyms, i),
Vincent Whitchurch93d77e72018-12-14 17:05:55 +01004193 mod, kallsyms_symbol_value(sym));
Anders Kaseorg75a66612008-12-05 19:03:58 -05004194 if (ret != 0)
4195 return ret;
4196 }
4197 }
4198 return 0;
4199}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004200#endif /* CONFIG_KALLSYMS */
4201
Petr Mladek7fd83292016-09-21 13:47:22 +02004202/* Maximum number of characters written by module_flags() */
4203#define MODULE_FLAGS_BUF_SIZE (TAINT_FLAGS_COUNT + 4)
4204
4205/* Keep in sync with MODULE_FLAGS_BUF_SIZE !!! */
Arjan van de Ven21aa9282008-01-25 21:08:33 +01004206static char *module_flags(struct module *mod, char *buf)
Florin Malitafa3ba2e82006-10-11 01:21:48 -07004207{
4208 int bx = 0;
4209
Rusty Russell0d21b0e2013-01-12 11:38:44 +10304210 BUG_ON(mod->state == MODULE_STATE_UNFORMED);
Arjan van de Ven21aa9282008-01-25 21:08:33 +01004211 if (mod->taints ||
4212 mod->state == MODULE_STATE_GOING ||
4213 mod->state == MODULE_STATE_COMING) {
Florin Malitafa3ba2e82006-10-11 01:21:48 -07004214 buf[bx++] = '(';
Kay Sieverscca3e702012-01-13 09:32:15 +10304215 bx += module_flags_taint(mod, buf + bx);
Arjan van de Ven21aa9282008-01-25 21:08:33 +01004216 /* Show a - for module-is-being-unloaded */
4217 if (mod->state == MODULE_STATE_GOING)
4218 buf[bx++] = '-';
4219 /* Show a + for module-is-being-loaded */
4220 if (mod->state == MODULE_STATE_COMING)
4221 buf[bx++] = '+';
Florin Malitafa3ba2e82006-10-11 01:21:48 -07004222 buf[bx++] = ')';
4223 }
4224 buf[bx] = '\0';
4225
4226 return buf;
4227}
4228
Alexey Dobriyan3b5d5c62008-10-06 13:19:27 +04004229#ifdef CONFIG_PROC_FS
4230/* Called by the /proc file system to return a list of modules. */
4231static void *m_start(struct seq_file *m, loff_t *pos)
4232{
4233 mutex_lock(&module_mutex);
4234 return seq_list_start(&modules, *pos);
4235}
4236
4237static void *m_next(struct seq_file *m, void *p, loff_t *pos)
4238{
4239 return seq_list_next(p, &modules, pos);
4240}
4241
4242static void m_stop(struct seq_file *m, void *p)
4243{
4244 mutex_unlock(&module_mutex);
4245}
4246
Linus Torvalds1da177e2005-04-16 15:20:36 -07004247static int m_show(struct seq_file *m, void *p)
4248{
4249 struct module *mod = list_entry(p, struct module, list);
Petr Mladek7fd83292016-09-21 13:47:22 +02004250 char buf[MODULE_FLAGS_BUF_SIZE];
Linus Torvalds668533d2017-11-29 10:30:13 -08004251 void *value;
Florin Malitafa3ba2e82006-10-11 01:21:48 -07004252
Rusty Russell0d21b0e2013-01-12 11:38:44 +10304253 /* We always ignore unformed modules. */
4254 if (mod->state == MODULE_STATE_UNFORMED)
4255 return 0;
4256
Denys Vlasenko2f0f2a32008-07-22 19:24:27 -05004257 seq_printf(m, "%s %u",
Rusty Russell7523e4d2015-11-26 09:44:08 +10304258 mod->name, mod->init_layout.size + mod->core_layout.size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004259 print_unload_info(m, mod);
4260
4261 /* Informative for users. */
4262 seq_printf(m, " %s",
Ionut Alexa6da0b562014-11-10 09:31:29 +10304263 mod->state == MODULE_STATE_GOING ? "Unloading" :
4264 mod->state == MODULE_STATE_COMING ? "Loading" :
Linus Torvalds1da177e2005-04-16 15:20:36 -07004265 "Live");
4266 /* Used by oprofile and other similar tools. */
Linus Torvalds668533d2017-11-29 10:30:13 -08004267 value = m->private ? NULL : mod->core_layout.base;
4268 seq_printf(m, " 0x%px", value);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004269
Florin Malitafa3ba2e82006-10-11 01:21:48 -07004270 /* Taints info */
4271 if (mod->taints)
Arjan van de Ven21aa9282008-01-25 21:08:33 +01004272 seq_printf(m, " %s", module_flags(mod, buf));
Florin Malitafa3ba2e82006-10-11 01:21:48 -07004273
Ionut Alexa6da0b562014-11-10 09:31:29 +10304274 seq_puts(m, "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004275 return 0;
4276}
4277
4278/* Format: modulename size refcount deps address
4279
4280 Where refcount is a number or -, and deps is a comma-separated list
4281 of depends or -.
4282*/
Alexey Dobriyan3b5d5c62008-10-06 13:19:27 +04004283static const struct seq_operations modules_op = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004284 .start = m_start,
4285 .next = m_next,
4286 .stop = m_stop,
4287 .show = m_show
4288};
4289
Linus Torvalds516fb7f2017-11-12 18:44:23 -08004290/*
4291 * This also sets the "private" pointer to non-NULL if the
4292 * kernel pointers should be hidden (so you can just test
4293 * "m->private" to see if you should keep the values private).
4294 *
4295 * We use the same logic as for /proc/kallsyms.
4296 */
Alexey Dobriyan3b5d5c62008-10-06 13:19:27 +04004297static int modules_open(struct inode *inode, struct file *file)
4298{
Linus Torvalds516fb7f2017-11-12 18:44:23 -08004299 int err = seq_open(file, &modules_op);
4300
4301 if (!err) {
4302 struct seq_file *m = file->private_data;
4303 m->private = kallsyms_show_value() ? NULL : (void *)8ul;
4304 }
4305
Leon Yu3f553b32018-03-06 23:16:24 +08004306 return err;
Alexey Dobriyan3b5d5c62008-10-06 13:19:27 +04004307}
4308
4309static const struct file_operations proc_modules_operations = {
4310 .open = modules_open,
4311 .read = seq_read,
4312 .llseek = seq_lseek,
4313 .release = seq_release,
4314};
4315
4316static int __init proc_modules_init(void)
4317{
4318 proc_create("modules", 0, NULL, &proc_modules_operations);
4319 return 0;
4320}
4321module_init(proc_modules_init);
4322#endif
4323
Linus Torvalds1da177e2005-04-16 15:20:36 -07004324/* Given an address, look for it in the module exception tables. */
4325const struct exception_table_entry *search_module_extables(unsigned long addr)
4326{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004327 const struct exception_table_entry *e = NULL;
4328 struct module *mod;
4329
Rusty Russell24da1cb2007-07-15 23:41:46 -07004330 preempt_disable();
Peter Zijlstra5ff22642017-02-08 15:48:01 +01004331 mod = __module_address(addr);
4332 if (!mod)
4333 goto out;
Daniel Walker22a8bde2007-10-18 03:06:07 -07004334
Peter Zijlstra5ff22642017-02-08 15:48:01 +01004335 if (!mod->num_exentries)
4336 goto out;
4337
4338 e = search_extable(mod->extable,
Thomas Meyera94c33d2017-07-10 15:51:58 -07004339 mod->num_exentries,
Peter Zijlstra5ff22642017-02-08 15:48:01 +01004340 addr);
4341out:
Rusty Russell24da1cb2007-07-15 23:41:46 -07004342 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004343
Peter Zijlstra5ff22642017-02-08 15:48:01 +01004344 /*
4345 * Now, if we found one, we are running inside it now, hence
4346 * we cannot unload the module, hence no refcnt needed.
4347 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004348 return e;
4349}
4350
Ingo Molnar4d435f92006-07-03 00:24:24 -07004351/*
Rusty Russelle6104992009-03-31 13:05:31 -06004352 * is_module_address - is this address inside a module?
4353 * @addr: the address to check.
4354 *
4355 * See is_module_text_address() if you simply want to see if the address
4356 * is code (not data).
Ingo Molnar4d435f92006-07-03 00:24:24 -07004357 */
Rusty Russelle6104992009-03-31 13:05:31 -06004358bool is_module_address(unsigned long addr)
Ingo Molnar4d435f92006-07-03 00:24:24 -07004359{
Rusty Russelle6104992009-03-31 13:05:31 -06004360 bool ret;
Ingo Molnar4d435f92006-07-03 00:24:24 -07004361
Rusty Russell24da1cb2007-07-15 23:41:46 -07004362 preempt_disable();
Rusty Russelle6104992009-03-31 13:05:31 -06004363 ret = __module_address(addr) != NULL;
Rusty Russell24da1cb2007-07-15 23:41:46 -07004364 preempt_enable();
Ingo Molnar4d435f92006-07-03 00:24:24 -07004365
Rusty Russelle6104992009-03-31 13:05:31 -06004366 return ret;
Ingo Molnar4d435f92006-07-03 00:24:24 -07004367}
4368
Rusty Russelle6104992009-03-31 13:05:31 -06004369/*
4370 * __module_address - get the module which contains an address.
4371 * @addr: the address.
4372 *
4373 * Must be called with preempt disabled or module mutex held so that
4374 * module doesn't get freed during this.
4375 */
Linus Torvalds714f83d2009-04-05 11:04:19 -07004376struct module *__module_address(unsigned long addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004377{
4378 struct module *mod;
4379
Rusty Russell3a642e92008-07-22 19:24:28 -05004380 if (addr < module_addr_min || addr > module_addr_max)
4381 return NULL;
4382
Peter Zijlstra0be964b2015-05-27 11:09:35 +09304383 module_assert_mutex_or_preempt();
4384
Peter Zijlstra6c9692e2015-05-27 11:09:37 +09304385 mod = mod_find(addr);
Peter Zijlstra93c2e102015-05-27 11:09:37 +09304386 if (mod) {
4387 BUG_ON(!within_module(addr, mod));
Rusty Russell0d21b0e2013-01-12 11:38:44 +10304388 if (mod->state == MODULE_STATE_UNFORMED)
Peter Zijlstra93c2e102015-05-27 11:09:37 +09304389 mod = NULL;
Rusty Russell0d21b0e2013-01-12 11:38:44 +10304390 }
Peter Zijlstra93c2e102015-05-27 11:09:37 +09304391 return mod;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004392}
Tim Abbottc6b37802008-12-05 19:03:59 -05004393EXPORT_SYMBOL_GPL(__module_address);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004394
Rusty Russelle6104992009-03-31 13:05:31 -06004395/*
4396 * is_module_text_address - is this address inside module code?
4397 * @addr: the address to check.
4398 *
4399 * See is_module_address() if you simply want to see if the address is
4400 * anywhere in a module. See kernel_text_address() for testing if an
4401 * address corresponds to kernel or module code.
4402 */
4403bool is_module_text_address(unsigned long addr)
4404{
4405 bool ret;
4406
4407 preempt_disable();
4408 ret = __module_text_address(addr) != NULL;
4409 preempt_enable();
4410
4411 return ret;
4412}
4413
4414/*
4415 * __module_text_address - get the module whose code contains an address.
4416 * @addr: the address.
4417 *
4418 * Must be called with preempt disabled or module mutex held so that
4419 * module doesn't get freed during this.
4420 */
4421struct module *__module_text_address(unsigned long addr)
4422{
4423 struct module *mod = __module_address(addr);
4424 if (mod) {
4425 /* Make sure it's within the text section. */
Rusty Russell7523e4d2015-11-26 09:44:08 +10304426 if (!within(addr, mod->init_layout.base, mod->init_layout.text_size)
4427 && !within(addr, mod->core_layout.base, mod->core_layout.text_size))
Rusty Russelle6104992009-03-31 13:05:31 -06004428 mod = NULL;
4429 }
4430 return mod;
4431}
Tim Abbottc6b37802008-12-05 19:03:59 -05004432EXPORT_SYMBOL_GPL(__module_text_address);
Rusty Russelle6104992009-03-31 13:05:31 -06004433
Linus Torvalds1da177e2005-04-16 15:20:36 -07004434/* Don't grab lock, we're oopsing. */
4435void print_modules(void)
4436{
4437 struct module *mod;
Petr Mladek7fd83292016-09-21 13:47:22 +02004438 char buf[MODULE_FLAGS_BUF_SIZE];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004439
Linus Torvaldsb2311252009-06-16 11:07:14 -07004440 printk(KERN_DEFAULT "Modules linked in:");
Andi Kleend72b3752008-08-30 10:09:00 +02004441 /* Most callers should already have preempt disabled, but make sure */
4442 preempt_disable();
Rusty Russell0d21b0e2013-01-12 11:38:44 +10304443 list_for_each_entry_rcu(mod, &modules, list) {
4444 if (mod->state == MODULE_STATE_UNFORMED)
4445 continue;
Jiri Slaby27bba4d2014-02-03 11:13:13 +10304446 pr_cont(" %s%s", mod->name, module_flags(mod, buf));
Rusty Russell0d21b0e2013-01-12 11:38:44 +10304447 }
Andi Kleend72b3752008-08-30 10:09:00 +02004448 preempt_enable();
Arjan van de Vene14af7e2008-01-25 21:08:33 +01004449 if (last_unloaded_module[0])
Jiri Slaby27bba4d2014-02-03 11:13:13 +10304450 pr_cont(" [last unloaded: %s]", last_unloaded_module);
4451 pr_cont("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004452}
4453
Linus Torvalds1da177e2005-04-16 15:20:36 -07004454#ifdef CONFIG_MODVERSIONS
Rusty Russell8c8ef422009-03-31 13:05:34 -06004455/* Generate the signature for all relevant module structures here.
4456 * If these change, we don't want to try to parse the module. */
4457void module_layout(struct module *mod,
4458 struct modversion_info *ver,
4459 struct kernel_param *kp,
4460 struct kernel_symbol *ks,
Mathieu Desnoyers65498642011-01-26 17:26:22 -05004461 struct tracepoint * const *tp)
Rusty Russell8c8ef422009-03-31 13:05:34 -06004462{
4463}
4464EXPORT_SYMBOL(module_layout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004465#endif