blob: cd8df516666d65d9396a6c2252f76724829e83df [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>
Steven Rostedt (Red Hat)af658dc2015-04-29 14:36:05 -040010#include <linux/trace_events.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#include <linux/init.h>
Alexey Dobriyanae84e322007-05-08 00:28:38 -070012#include <linux/kallsyms.h>
Kees Cook34e11692012-10-16 07:31:07 +103013#include <linux/file.h>
Alexey Dobriyan3b5d5c62008-10-06 13:19:27 +040014#include <linux/fs.h>
Roland McGrath6d760132007-10-16 23:26:40 -070015#include <linux/sysfs.h>
Randy Dunlap9f158332005-09-13 01:25:16 -070016#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <linux/slab.h>
18#include <linux/vmalloc.h>
19#include <linux/elf.h>
Alexey Dobriyan3b5d5c62008-10-06 13:19:27 +040020#include <linux/proc_fs.h>
Kees Cook2e72d512012-10-16 07:32:07 +103021#include <linux/security.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/seq_file.h>
23#include <linux/syscalls.h>
24#include <linux/fcntl.h>
25#include <linux/rcupdate.h>
Randy.Dunlapc59ede72006-01-11 12:17:46 -080026#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <linux/cpu.h>
28#include <linux/moduleparam.h>
29#include <linux/errno.h>
30#include <linux/err.h>
31#include <linux/vermagic.h>
32#include <linux/notifier.h>
Al Virof6a57032006-10-18 01:47:25 -040033#include <linux/sched.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include <linux/device.h>
Matt Domschc988d2b2005-06-23 22:05:15 -070035#include <linux/string.h>
Arjan van de Ven97d1f152006-03-23 03:00:24 -080036#include <linux/mutex.h>
Andi Kleend72b3752008-08-30 10:09:00 +020037#include <linux/rculist.h>
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080038#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include <asm/cacheflush.h>
Michael Ellerman563ec5c2017-07-06 15:35:58 -070040#include <linux/set_memory.h>
Bernd Schmidteb8cdec2009-09-21 17:03:57 -070041#include <asm/mmu_context.h>
Sam Ravnborgb817f6f2006-06-09 21:53:55 +020042#include <linux/license.h>
Christoph Lameter6d762392008-02-08 04:18:42 -080043#include <asm/sections.h>
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -040044#include <linux/tracepoint.h>
Steven Rostedt90d595f2008-08-14 15:45:09 -040045#include <linux/ftrace.h>
Jessica Yu7e545d62016-03-16 20:55:39 -040046#include <linux/livepatch.h>
Arjan van de Ven22a9d642009-01-07 08:45:46 -080047#include <linux/async.h>
Tejun Heofbf59bc2009-02-20 16:29:08 +090048#include <linux/percpu.h>
Catalin Marinas4f2294b2009-06-11 13:23:20 +010049#include <linux/kmemleak.h>
Jason Baronbf5438fc2010-09-17 11:09:00 -040050#include <linux/jump_label.h>
matthieu castet84e1c6b2010-11-16 22:35:16 +010051#include <linux/pfn.h>
Alessio Igor Bogani403ed272011-04-20 11:10:52 +020052#include <linux/bsearch.h>
Luis de Bethencourt9d5059c2016-08-02 14:03:47 -070053#include <linux/dynamic_debug.h>
Richard Guy Briggsca86cad2017-02-04 13:10:38 -050054#include <linux/audit.h>
Rusty Russell2f3238a2012-10-22 18:09:41 +103055#include <uapi/linux/module.h>
Rusty Russell106a4ee2012-09-26 10:09:40 +010056#include "module-internal.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070057
Li Zefan7ead8b82009-08-17 16:56:28 +080058#define CREATE_TRACE_POINTS
59#include <trace/events/module.h>
60
Linus Torvalds1da177e2005-04-16 15:20:36 -070061#ifndef ARCH_SHF_SMALL
62#define ARCH_SHF_SMALL 0
63#endif
64
matthieu castet84e1c6b2010-11-16 22:35:16 +010065/*
66 * Modules' sections will be aligned on page boundaries
Jessica Yu38f054d2019-07-23 15:26:28 +020067 * to ensure complete separation of code and data
matthieu castet84e1c6b2010-11-16 22:35:16 +010068 */
matthieu castet84e1c6b2010-11-16 22:35:16 +010069# define debug_align(X) ALIGN(X, PAGE_SIZE)
matthieu castet84e1c6b2010-11-16 22:35:16 +010070
Linus Torvalds1da177e2005-04-16 15:20:36 -070071/* If this is set, the section belongs in the init part of the module */
72#define INIT_OFFSET_MASK (1UL << (BITS_PER_LONG-1))
73
Rusty Russell75676502010-06-05 11:17:36 -060074/*
75 * Mutex protects:
76 * 1) List of modules (also safely readable with preempt_disable),
77 * 2) module_use links,
78 * 3) module_addr_min/module_addr_max.
Masami Hiramatsue513cc12014-11-10 09:30:29 +103079 * (delete and add uses RCU list operations). */
Tim Abbottc6b37802008-12-05 19:03:59 -050080DEFINE_MUTEX(module_mutex);
81EXPORT_SYMBOL_GPL(module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070082static LIST_HEAD(modules);
Peter Zijlstra93c2e102015-05-27 11:09:37 +093083
Rick Edgecombe1a7b7d92019-04-25 17:11:37 -070084/* Work queue for freeing init sections in success case */
85static struct work_struct init_free_wq;
86static struct llist_head init_free_list;
87
Peter Zijlstra6c9692e2015-05-27 11:09:37 +093088#ifdef CONFIG_MODULES_TREE_LOOKUP
89
Peter Zijlstra93c2e102015-05-27 11:09:37 +093090/*
91 * Use a latched RB-tree for __module_address(); this allows us to use
92 * RCU-sched lookups of the address from any context.
93 *
Peter Zijlstra6c9692e2015-05-27 11:09:37 +093094 * This is conditional on PERF_EVENTS || TRACING because those can really hit
95 * __module_address() hard by doing a lot of stack unwinding; potentially from
96 * NMI context.
Peter Zijlstra93c2e102015-05-27 11:09:37 +093097 */
98
99static __always_inline unsigned long __mod_tree_val(struct latch_tree_node *n)
100{
Rusty Russell7523e4d2015-11-26 09:44:08 +1030101 struct module_layout *layout = container_of(n, struct module_layout, mtn.node);
Peter Zijlstra93c2e102015-05-27 11:09:37 +0930102
Rusty Russell7523e4d2015-11-26 09:44:08 +1030103 return (unsigned long)layout->base;
Peter Zijlstra93c2e102015-05-27 11:09:37 +0930104}
105
106static __always_inline unsigned long __mod_tree_size(struct latch_tree_node *n)
107{
Rusty Russell7523e4d2015-11-26 09:44:08 +1030108 struct module_layout *layout = container_of(n, struct module_layout, mtn.node);
Peter Zijlstra93c2e102015-05-27 11:09:37 +0930109
Rusty Russell7523e4d2015-11-26 09:44:08 +1030110 return (unsigned long)layout->size;
Peter Zijlstra93c2e102015-05-27 11:09:37 +0930111}
112
113static __always_inline bool
114mod_tree_less(struct latch_tree_node *a, struct latch_tree_node *b)
115{
116 return __mod_tree_val(a) < __mod_tree_val(b);
117}
118
119static __always_inline int
120mod_tree_comp(void *key, struct latch_tree_node *n)
121{
122 unsigned long val = (unsigned long)key;
123 unsigned long start, end;
124
125 start = __mod_tree_val(n);
126 if (val < start)
127 return -1;
128
129 end = start + __mod_tree_size(n);
130 if (val >= end)
131 return 1;
132
133 return 0;
134}
135
136static const struct latch_tree_ops mod_tree_ops = {
137 .less = mod_tree_less,
138 .comp = mod_tree_comp,
139};
140
Peter Zijlstra4f6665462015-05-27 11:09:38 +0930141static struct mod_tree_root {
142 struct latch_tree_root root;
143 unsigned long addr_min;
144 unsigned long addr_max;
145} mod_tree __cacheline_aligned = {
146 .addr_min = -1UL,
147};
148
149#define module_addr_min mod_tree.addr_min
150#define module_addr_max mod_tree.addr_max
151
152static noinline void __mod_tree_insert(struct mod_tree_node *node)
153{
154 latch_tree_insert(&node->node, &mod_tree.root, &mod_tree_ops);
155}
156
157static void __mod_tree_remove(struct mod_tree_node *node)
158{
159 latch_tree_erase(&node->node, &mod_tree.root, &mod_tree_ops);
160}
Peter Zijlstra93c2e102015-05-27 11:09:37 +0930161
162/*
163 * These modifications: insert, remove_init and remove; are serialized by the
164 * module_mutex.
165 */
166static void mod_tree_insert(struct module *mod)
167{
Rusty Russell7523e4d2015-11-26 09:44:08 +1030168 mod->core_layout.mtn.mod = mod;
169 mod->init_layout.mtn.mod = mod;
Peter Zijlstra93c2e102015-05-27 11:09:37 +0930170
Rusty Russell7523e4d2015-11-26 09:44:08 +1030171 __mod_tree_insert(&mod->core_layout.mtn);
172 if (mod->init_layout.size)
173 __mod_tree_insert(&mod->init_layout.mtn);
Peter Zijlstra93c2e102015-05-27 11:09:37 +0930174}
175
176static void mod_tree_remove_init(struct module *mod)
177{
Rusty Russell7523e4d2015-11-26 09:44:08 +1030178 if (mod->init_layout.size)
179 __mod_tree_remove(&mod->init_layout.mtn);
Peter Zijlstra93c2e102015-05-27 11:09:37 +0930180}
181
182static void mod_tree_remove(struct module *mod)
183{
Rusty Russell7523e4d2015-11-26 09:44:08 +1030184 __mod_tree_remove(&mod->core_layout.mtn);
Peter Zijlstra93c2e102015-05-27 11:09:37 +0930185 mod_tree_remove_init(mod);
186}
187
Peter Zijlstra6c9692e2015-05-27 11:09:37 +0930188static struct module *mod_find(unsigned long addr)
Peter Zijlstra93c2e102015-05-27 11:09:37 +0930189{
190 struct latch_tree_node *ltn;
191
Peter Zijlstra4f6665462015-05-27 11:09:38 +0930192 ltn = latch_tree_find((void *)addr, &mod_tree.root, &mod_tree_ops);
Peter Zijlstra93c2e102015-05-27 11:09:37 +0930193 if (!ltn)
194 return NULL;
195
196 return container_of(ltn, struct mod_tree_node, node)->mod;
197}
198
Peter Zijlstra6c9692e2015-05-27 11:09:37 +0930199#else /* MODULES_TREE_LOOKUP */
200
Peter Zijlstra4f6665462015-05-27 11:09:38 +0930201static unsigned long module_addr_min = -1UL, module_addr_max = 0;
202
Peter Zijlstra6c9692e2015-05-27 11:09:37 +0930203static void mod_tree_insert(struct module *mod) { }
204static void mod_tree_remove_init(struct module *mod) { }
205static void mod_tree_remove(struct module *mod) { }
206
207static struct module *mod_find(unsigned long addr)
208{
209 struct module *mod;
210
211 list_for_each_entry_rcu(mod, &modules, list) {
212 if (within_module(addr, mod))
213 return mod;
214 }
215
216 return NULL;
217}
218
219#endif /* MODULES_TREE_LOOKUP */
220
Peter Zijlstra4f6665462015-05-27 11:09:38 +0930221/*
222 * Bounds of module text, for speeding up __module_address.
223 * Protected by module_mutex.
224 */
225static void __mod_update_bounds(void *base, unsigned int size)
226{
227 unsigned long min = (unsigned long)base;
228 unsigned long max = min + size;
229
230 if (min < module_addr_min)
231 module_addr_min = min;
232 if (max > module_addr_max)
233 module_addr_max = max;
234}
235
236static void mod_update_bounds(struct module *mod)
237{
Rusty Russell7523e4d2015-11-26 09:44:08 +1030238 __mod_update_bounds(mod->core_layout.base, mod->core_layout.size);
239 if (mod->init_layout.size)
240 __mod_update_bounds(mod->init_layout.base, mod->init_layout.size);
Peter Zijlstra4f6665462015-05-27 11:09:38 +0930241}
242
Jason Wessel67fc4e02010-05-20 21:04:21 -0500243#ifdef CONFIG_KGDB_KDB
244struct list_head *kdb_modules = &modules; /* kdb needs the list of modules */
245#endif /* CONFIG_KGDB_KDB */
246
Peter Zijlstra0be964b2015-05-27 11:09:35 +0930247static void module_assert_mutex(void)
Rusty Russell106a4ee2012-09-26 10:09:40 +0100248{
Peter Zijlstra0be964b2015-05-27 11:09:35 +0930249 lockdep_assert_held(&module_mutex);
Rusty Russell106a4ee2012-09-26 10:09:40 +0100250}
251
Peter Zijlstra0be964b2015-05-27 11:09:35 +0930252static void module_assert_mutex_or_preempt(void)
253{
254#ifdef CONFIG_LOCKDEP
255 if (unlikely(!debug_locks))
256 return;
Rusty Russell106a4ee2012-09-26 10:09:40 +0100257
Steven Rostedt95025142016-07-19 05:59:24 +0930258 WARN_ON_ONCE(!rcu_read_lock_sched_held() &&
Peter Zijlstra0be964b2015-05-27 11:09:35 +0930259 !lockdep_is_held(&module_mutex));
260#endif
261}
262
Luis R. Rodriguez6727bb92015-05-27 11:09:39 +0930263static bool sig_enforce = IS_ENABLED(CONFIG_MODULE_SIG_FORCE);
Rusty Russell106a4ee2012-09-26 10:09:40 +0100264module_param(sig_enforce, bool_enable_only, 0644);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265
Bruno E. O. Meneguelefda784e2017-10-24 15:37:00 -0200266/*
267 * Export sig_enforce kernel cmdline parameter to allow other subsystems rely
268 * on that instead of directly to CONFIG_MODULE_SIG_FORCE config.
269 */
270bool is_module_sig_enforced(void)
271{
272 return sig_enforce;
273}
274EXPORT_SYMBOL(is_module_sig_enforced);
275
Mimi Zohar8db5da02019-01-27 19:03:45 -0500276void set_module_sig_enforced(void)
277{
278 sig_enforce = true;
279}
280
Stephen Rothwell19e45292009-04-14 17:27:18 +1000281/* Block module loading/unloading? */
282int modules_disabled = 0;
Dave Young02608be2012-02-01 10:33:14 +0800283core_param(nomodule, modules_disabled, bint, 0);
Stephen Rothwell19e45292009-04-14 17:27:18 +1000284
Rusty Russellc9a3ba52008-01-29 17:13:18 -0500285/* Waiting for a module to finish initializing? */
286static DECLARE_WAIT_QUEUE_HEAD(module_wq);
287
Alan Sterne041c682006-03-27 01:16:30 -0800288static BLOCKING_NOTIFIER_HEAD(module_notify_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289
Ionut Alexa6da0b562014-11-10 09:31:29 +1030290int register_module_notifier(struct notifier_block *nb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291{
Alan Sterne041c682006-03-27 01:16:30 -0800292 return blocking_notifier_chain_register(&module_notify_list, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293}
294EXPORT_SYMBOL(register_module_notifier);
295
Ionut Alexa6da0b562014-11-10 09:31:29 +1030296int unregister_module_notifier(struct notifier_block *nb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297{
Alan Sterne041c682006-03-27 01:16:30 -0800298 return blocking_notifier_chain_unregister(&module_notify_list, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299}
300EXPORT_SYMBOL(unregister_module_notifier);
301
Miroslav Benes71d9f502016-11-16 16:45:48 +0100302/*
303 * We require a truly strong try_module_get(): 0 means success.
304 * Otherwise an error is returned due to ongoing or failed
305 * initialization etc.
306 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307static inline int strong_try_module_get(struct module *mod)
308{
Rusty Russell0d21b0e2013-01-12 11:38:44 +1030309 BUG_ON(mod && mod->state == MODULE_STATE_UNFORMED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 if (mod && mod->state == MODULE_STATE_COMING)
Rusty Russellc9a3ba52008-01-29 17:13:18 -0500311 return -EBUSY;
312 if (try_module_get(mod))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 return 0;
Rusty Russellc9a3ba52008-01-29 17:13:18 -0500314 else
315 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316}
317
Rusty Russell373d4d02013-01-21 17:17:39 +1030318static inline void add_taint_module(struct module *mod, unsigned flag,
319 enum lockdep_ok lockdep_ok)
Florin Malitafa3ba2e82006-10-11 01:21:48 -0700320{
Rusty Russell373d4d02013-01-21 17:17:39 +1030321 add_taint(flag, lockdep_ok);
Petr Mladek7fd83292016-09-21 13:47:22 +0200322 set_bit(flag, &mod->taints);
Florin Malitafa3ba2e82006-10-11 01:21:48 -0700323}
324
Robert P. J. Day02a3e592007-05-09 07:26:28 +0200325/*
326 * A thread that wants to hold a reference to a module only while it
327 * is running can call this to safely exit. nfsd and lockd use this.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 */
Jiri Kosinabf262dc2016-04-12 05:02:09 +0930329void __noreturn __module_put_and_exit(struct module *mod, long code)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330{
331 module_put(mod);
332 do_exit(code);
333}
334EXPORT_SYMBOL(__module_put_and_exit);
Daniel Walker22a8bde2007-10-18 03:06:07 -0700335
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336/* Find a module section: 0 means not found. */
Rusty Russell49668682010-08-05 12:59:10 -0600337static unsigned int find_sec(const struct load_info *info, const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338{
339 unsigned int i;
340
Rusty Russell49668682010-08-05 12:59:10 -0600341 for (i = 1; i < info->hdr->e_shnum; i++) {
342 Elf_Shdr *shdr = &info->sechdrs[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 /* Alloc bit cleared means "ignore it." */
Rusty Russell49668682010-08-05 12:59:10 -0600344 if ((shdr->sh_flags & SHF_ALLOC)
345 && strcmp(info->secstrings + shdr->sh_name, name) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 return i;
Rusty Russell49668682010-08-05 12:59:10 -0600347 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 return 0;
349}
350
Rusty Russell5e458cc2008-10-22 10:00:13 -0500351/* Find a module section, or NULL. */
Rusty Russell49668682010-08-05 12:59:10 -0600352static void *section_addr(const struct load_info *info, const char *name)
Rusty Russell5e458cc2008-10-22 10:00:13 -0500353{
354 /* Section 0 has sh_addr 0. */
Rusty Russell49668682010-08-05 12:59:10 -0600355 return (void *)info->sechdrs[find_sec(info, name)].sh_addr;
Rusty Russell5e458cc2008-10-22 10:00:13 -0500356}
357
358/* Find a module section, or NULL. Fill in number of "objects" in section. */
Rusty Russell49668682010-08-05 12:59:10 -0600359static void *section_objs(const struct load_info *info,
Rusty Russell5e458cc2008-10-22 10:00:13 -0500360 const char *name,
361 size_t object_size,
362 unsigned int *num)
363{
Rusty Russell49668682010-08-05 12:59:10 -0600364 unsigned int sec = find_sec(info, name);
Rusty Russell5e458cc2008-10-22 10:00:13 -0500365
366 /* Section 0 has sh_addr 0 and sh_size 0. */
Rusty Russell49668682010-08-05 12:59:10 -0600367 *num = info->sechdrs[sec].sh_size / object_size;
368 return (void *)info->sechdrs[sec].sh_addr;
Rusty Russell5e458cc2008-10-22 10:00:13 -0500369}
370
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371/* Provided by the linker */
372extern const struct kernel_symbol __start___ksymtab[];
373extern const struct kernel_symbol __stop___ksymtab[];
374extern const struct kernel_symbol __start___ksymtab_gpl[];
375extern const struct kernel_symbol __stop___ksymtab_gpl[];
Greg Kroah-Hartman9f28bb72006-03-20 13:17:13 -0800376extern const struct kernel_symbol __start___ksymtab_gpl_future[];
377extern const struct kernel_symbol __stop___ksymtab_gpl_future[];
Ard Biesheuvel71810db2017-02-03 09:54:06 +0000378extern const s32 __start___kcrctab[];
379extern const s32 __start___kcrctab_gpl[];
380extern const s32 __start___kcrctab_gpl_future[];
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500381#ifdef CONFIG_UNUSED_SYMBOLS
382extern const struct kernel_symbol __start___ksymtab_unused[];
383extern const struct kernel_symbol __stop___ksymtab_unused[];
384extern const struct kernel_symbol __start___ksymtab_unused_gpl[];
385extern const struct kernel_symbol __stop___ksymtab_unused_gpl[];
Ard Biesheuvel71810db2017-02-03 09:54:06 +0000386extern const s32 __start___kcrctab_unused[];
387extern const s32 __start___kcrctab_unused_gpl[];
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500388#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389
390#ifndef CONFIG_MODVERSIONS
391#define symversion(base, idx) NULL
392#else
Andrew Mortonf83ca9f2006-03-28 01:56:20 -0800393#define symversion(base, idx) ((base != NULL) ? ((base) + (idx)) : NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394#endif
395
Rusty Russelldafd0942008-07-22 19:24:25 -0500396static bool each_symbol_in_section(const struct symsearch *arr,
397 unsigned int arrsize,
398 struct module *owner,
399 bool (*fn)(const struct symsearch *syms,
400 struct module *owner,
Rusty Russellde4d8d52011-04-19 21:49:58 +0200401 void *data),
Rusty Russelldafd0942008-07-22 19:24:25 -0500402 void *data)
Sam Ravnborg3fd68052006-02-08 21:16:45 +0100403{
Rusty Russellde4d8d52011-04-19 21:49:58 +0200404 unsigned int j;
Rusty Russelldafd0942008-07-22 19:24:25 -0500405
406 for (j = 0; j < arrsize; j++) {
Rusty Russellde4d8d52011-04-19 21:49:58 +0200407 if (fn(&arr[j], owner, data))
408 return true;
Rusty Russelldafd0942008-07-22 19:24:25 -0500409 }
410
411 return false;
Sam Ravnborg3fd68052006-02-08 21:16:45 +0100412}
413
Rusty Russelldafd0942008-07-22 19:24:25 -0500414/* Returns true as soon as fn returns true, otherwise false. */
Rusty Russellde4d8d52011-04-19 21:49:58 +0200415bool each_symbol_section(bool (*fn)(const struct symsearch *arr,
416 struct module *owner,
417 void *data),
418 void *data)
Arjan van de Venf71d20e2006-06-28 04:26:45 -0700419{
Rusty Russelldafd0942008-07-22 19:24:25 -0500420 struct module *mod;
Linus Torvalds44032e62010-08-05 12:59:05 -0600421 static const struct symsearch arr[] = {
Rusty Russelldafd0942008-07-22 19:24:25 -0500422 { __start___ksymtab, __stop___ksymtab, __start___kcrctab,
423 NOT_GPL_ONLY, false },
424 { __start___ksymtab_gpl, __stop___ksymtab_gpl,
425 __start___kcrctab_gpl,
426 GPL_ONLY, false },
427 { __start___ksymtab_gpl_future, __stop___ksymtab_gpl_future,
428 __start___kcrctab_gpl_future,
429 WILL_BE_GPL_ONLY, false },
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500430#ifdef CONFIG_UNUSED_SYMBOLS
Rusty Russelldafd0942008-07-22 19:24:25 -0500431 { __start___ksymtab_unused, __stop___ksymtab_unused,
432 __start___kcrctab_unused,
433 NOT_GPL_ONLY, true },
434 { __start___ksymtab_unused_gpl, __stop___ksymtab_unused_gpl,
435 __start___kcrctab_unused_gpl,
436 GPL_ONLY, true },
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500437#endif
Rusty Russelldafd0942008-07-22 19:24:25 -0500438 };
439
Peter Zijlstra0be964b2015-05-27 11:09:35 +0930440 module_assert_mutex_or_preempt();
441
Rusty Russelldafd0942008-07-22 19:24:25 -0500442 if (each_symbol_in_section(arr, ARRAY_SIZE(arr), NULL, fn, data))
443 return true;
444
Andi Kleend72b3752008-08-30 10:09:00 +0200445 list_for_each_entry_rcu(mod, &modules, list) {
Rusty Russelldafd0942008-07-22 19:24:25 -0500446 struct symsearch arr[] = {
447 { mod->syms, mod->syms + mod->num_syms, mod->crcs,
448 NOT_GPL_ONLY, false },
449 { mod->gpl_syms, mod->gpl_syms + mod->num_gpl_syms,
450 mod->gpl_crcs,
451 GPL_ONLY, false },
452 { mod->gpl_future_syms,
453 mod->gpl_future_syms + mod->num_gpl_future_syms,
454 mod->gpl_future_crcs,
455 WILL_BE_GPL_ONLY, false },
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500456#ifdef CONFIG_UNUSED_SYMBOLS
Rusty Russelldafd0942008-07-22 19:24:25 -0500457 { mod->unused_syms,
458 mod->unused_syms + mod->num_unused_syms,
459 mod->unused_crcs,
460 NOT_GPL_ONLY, true },
461 { mod->unused_gpl_syms,
462 mod->unused_gpl_syms + mod->num_unused_gpl_syms,
463 mod->unused_gpl_crcs,
464 GPL_ONLY, true },
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500465#endif
Rusty Russelldafd0942008-07-22 19:24:25 -0500466 };
467
Rusty Russell0d21b0e2013-01-12 11:38:44 +1030468 if (mod->state == MODULE_STATE_UNFORMED)
469 continue;
470
Rusty Russelldafd0942008-07-22 19:24:25 -0500471 if (each_symbol_in_section(arr, ARRAY_SIZE(arr), mod, fn, data))
472 return true;
473 }
474 return false;
Arjan van de Venf71d20e2006-06-28 04:26:45 -0700475}
Rusty Russellde4d8d52011-04-19 21:49:58 +0200476EXPORT_SYMBOL_GPL(each_symbol_section);
Arjan van de Venf71d20e2006-06-28 04:26:45 -0700477
Rusty Russelldafd0942008-07-22 19:24:25 -0500478struct find_symbol_arg {
479 /* Input */
480 const char *name;
481 bool gplok;
482 bool warn;
483
484 /* Output */
485 struct module *owner;
Ard Biesheuvel71810db2017-02-03 09:54:06 +0000486 const s32 *crc;
Tim Abbott414fd312008-12-05 19:03:56 -0500487 const struct kernel_symbol *sym;
Rusty Russelldafd0942008-07-22 19:24:25 -0500488};
489
Jessica Yu2d25bc52018-11-19 17:43:58 +0100490static bool check_exported_symbol(const struct symsearch *syms,
491 struct module *owner,
492 unsigned int symnum, void *data)
Rusty Russellad9546c2008-05-01 21:14:59 -0500493{
Rusty Russelldafd0942008-07-22 19:24:25 -0500494 struct find_symbol_arg *fsa = data;
495
Rusty Russelldafd0942008-07-22 19:24:25 -0500496 if (!fsa->gplok) {
497 if (syms->licence == GPL_ONLY)
498 return false;
499 if (syms->licence == WILL_BE_GPL_ONLY && fsa->warn) {
Andrew Mortonbddb12b2013-11-12 15:11:28 -0800500 pr_warn("Symbol %s is being used by a non-GPL module, "
501 "which will not be allowed in the future\n",
502 fsa->name);
Rusty Russelldafd0942008-07-22 19:24:25 -0500503 }
504 }
505
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500506#ifdef CONFIG_UNUSED_SYMBOLS
Rusty Russelldafd0942008-07-22 19:24:25 -0500507 if (syms->unused && fsa->warn) {
Andrew Mortonbddb12b2013-11-12 15:11:28 -0800508 pr_warn("Symbol %s is marked as UNUSED, however this module is "
509 "using it.\n", fsa->name);
510 pr_warn("This symbol will go away in the future.\n");
Yannick Guerrini7b63c3a2015-03-24 12:31:40 +1030511 pr_warn("Please evaluate if this is the right api to use and "
512 "if it really is, submit a report to the linux kernel "
513 "mailing list together with submitting your code for "
Andrew Mortonbddb12b2013-11-12 15:11:28 -0800514 "inclusion.\n");
Rusty Russellad9546c2008-05-01 21:14:59 -0500515 }
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500516#endif
Rusty Russelldafd0942008-07-22 19:24:25 -0500517
518 fsa->owner = owner;
519 fsa->crc = symversion(syms->crcs, symnum);
Tim Abbott414fd312008-12-05 19:03:56 -0500520 fsa->sym = &syms->start[symnum];
Rusty Russellad9546c2008-05-01 21:14:59 -0500521 return true;
522}
523
Ard Biesheuvel7290d582018-08-21 21:56:09 -0700524static unsigned long kernel_symbol_value(const struct kernel_symbol *sym)
525{
526#ifdef CONFIG_HAVE_ARCH_PREL32_RELOCATIONS
527 return (unsigned long)offset_to_ptr(&sym->value_offset);
528#else
529 return sym->value;
530#endif
531}
532
533static const char *kernel_symbol_name(const struct kernel_symbol *sym)
534{
535#ifdef CONFIG_HAVE_ARCH_PREL32_RELOCATIONS
536 return offset_to_ptr(&sym->name_offset);
537#else
538 return sym->name;
539#endif
540}
541
Alessio Igor Bogani403ed272011-04-20 11:10:52 +0200542static int cmp_name(const void *va, const void *vb)
543{
544 const char *a;
545 const struct kernel_symbol *b;
546 a = va; b = vb;
Ard Biesheuvel7290d582018-08-21 21:56:09 -0700547 return strcmp(a, kernel_symbol_name(b));
Alessio Igor Bogani403ed272011-04-20 11:10:52 +0200548}
549
Jessica Yu2d25bc52018-11-19 17:43:58 +0100550static bool find_exported_symbol_in_section(const struct symsearch *syms,
551 struct module *owner,
552 void *data)
Rusty Russellde4d8d52011-04-19 21:49:58 +0200553{
554 struct find_symbol_arg *fsa = data;
Alessio Igor Bogani403ed272011-04-20 11:10:52 +0200555 struct kernel_symbol *sym;
Rusty Russellde4d8d52011-04-19 21:49:58 +0200556
Alessio Igor Bogani403ed272011-04-20 11:10:52 +0200557 sym = bsearch(fsa->name, syms->start, syms->stop - syms->start,
558 sizeof(struct kernel_symbol), cmp_name);
559
Jessica Yu2d25bc52018-11-19 17:43:58 +0100560 if (sym != NULL && check_exported_symbol(syms, owner,
561 sym - syms->start, data))
Alessio Igor Bogani403ed272011-04-20 11:10:52 +0200562 return true;
563
Rusty Russellde4d8d52011-04-19 21:49:58 +0200564 return false;
565}
566
Jessica Yu2d25bc52018-11-19 17:43:58 +0100567/* Find an exported symbol and return it, along with, (optional) crc and
Rusty Russell75676502010-06-05 11:17:36 -0600568 * (optional) module which owns it. Needs preempt disabled or module_mutex. */
Tim Abbottc6b37802008-12-05 19:03:59 -0500569const struct kernel_symbol *find_symbol(const char *name,
570 struct module **owner,
Ard Biesheuvel71810db2017-02-03 09:54:06 +0000571 const s32 **crc,
Tim Abbottc6b37802008-12-05 19:03:59 -0500572 bool gplok,
573 bool warn)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574{
Rusty Russelldafd0942008-07-22 19:24:25 -0500575 struct find_symbol_arg fsa;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576
Rusty Russelldafd0942008-07-22 19:24:25 -0500577 fsa.name = name;
578 fsa.gplok = gplok;
579 fsa.warn = warn;
580
Jessica Yu2d25bc52018-11-19 17:43:58 +0100581 if (each_symbol_section(find_exported_symbol_in_section, &fsa)) {
Rusty Russellad9546c2008-05-01 21:14:59 -0500582 if (owner)
Rusty Russelldafd0942008-07-22 19:24:25 -0500583 *owner = fsa.owner;
584 if (crc)
585 *crc = fsa.crc;
Tim Abbott414fd312008-12-05 19:03:56 -0500586 return fsa.sym;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 }
Rusty Russellad9546c2008-05-01 21:14:59 -0500588
Jim Cromie5e124162011-12-06 12:11:31 -0700589 pr_debug("Failed to find symbol %s\n", name);
Tim Abbott414fd312008-12-05 19:03:56 -0500590 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591}
Tim Abbottc6b37802008-12-05 19:03:59 -0500592EXPORT_SYMBOL_GPL(find_symbol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593
Rusty Russellfe0d34d2015-07-29 05:52:14 +0930594/*
595 * Search for module by name: must hold module_mutex (or preempt disabled
596 * for read-only access).
597 */
Mathias Krause4f6de4d52013-07-02 15:35:11 +0930598static struct module *find_module_all(const char *name, size_t len,
Rusty Russell0d21b0e2013-01-12 11:38:44 +1030599 bool even_unformed)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600{
601 struct module *mod;
602
Rusty Russellfe0d34d2015-07-29 05:52:14 +0930603 module_assert_mutex_or_preempt();
Peter Zijlstra0be964b2015-05-27 11:09:35 +0930604
Luis R. Rodriguez93437352017-05-26 14:12:25 -0700605 list_for_each_entry_rcu(mod, &modules, list) {
Rusty Russell0d21b0e2013-01-12 11:38:44 +1030606 if (!even_unformed && mod->state == MODULE_STATE_UNFORMED)
607 continue;
Mathias Krause4f6de4d52013-07-02 15:35:11 +0930608 if (strlen(mod->name) == len && !memcmp(mod->name, name, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 return mod;
610 }
611 return NULL;
612}
Rusty Russell0d21b0e2013-01-12 11:38:44 +1030613
614struct module *find_module(const char *name)
615{
Rusty Russellfe0d34d2015-07-29 05:52:14 +0930616 module_assert_mutex();
Mathias Krause4f6de4d52013-07-02 15:35:11 +0930617 return find_module_all(name, strlen(name), false);
Rusty Russell0d21b0e2013-01-12 11:38:44 +1030618}
Tim Abbottc6b37802008-12-05 19:03:59 -0500619EXPORT_SYMBOL_GPL(find_module);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620
621#ifdef CONFIG_SMP
Tejun Heofbf59bc2009-02-20 16:29:08 +0900622
Tejun Heo259354d2010-03-10 18:56:10 +0900623static inline void __percpu *mod_percpu(struct module *mod)
Tejun Heofbf59bc2009-02-20 16:29:08 +0900624{
Tejun Heo259354d2010-03-10 18:56:10 +0900625 return mod->percpu;
626}
Tejun Heofbf59bc2009-02-20 16:29:08 +0900627
Rusty Russell9eb76d72013-07-03 10:06:29 +0930628static int percpu_modalloc(struct module *mod, struct load_info *info)
Tejun Heo259354d2010-03-10 18:56:10 +0900629{
Rusty Russell9eb76d72013-07-03 10:06:29 +0930630 Elf_Shdr *pcpusec = &info->sechdrs[info->index.pcpu];
631 unsigned long align = pcpusec->sh_addralign;
632
633 if (!pcpusec->sh_size)
634 return 0;
635
Tejun Heofbf59bc2009-02-20 16:29:08 +0900636 if (align > PAGE_SIZE) {
Andrew Mortonbddb12b2013-11-12 15:11:28 -0800637 pr_warn("%s: per-cpu alignment %li > %li\n",
638 mod->name, align, PAGE_SIZE);
Tejun Heofbf59bc2009-02-20 16:29:08 +0900639 align = PAGE_SIZE;
640 }
641
Rusty Russell9eb76d72013-07-03 10:06:29 +0930642 mod->percpu = __alloc_reserved_percpu(pcpusec->sh_size, align);
Tejun Heo259354d2010-03-10 18:56:10 +0900643 if (!mod->percpu) {
Andrew Mortonbddb12b2013-11-12 15:11:28 -0800644 pr_warn("%s: Could not allocate %lu bytes percpu data\n",
645 mod->name, (unsigned long)pcpusec->sh_size);
Tejun Heo259354d2010-03-10 18:56:10 +0900646 return -ENOMEM;
647 }
Rusty Russell9eb76d72013-07-03 10:06:29 +0930648 mod->percpu_size = pcpusec->sh_size;
Tejun Heo259354d2010-03-10 18:56:10 +0900649 return 0;
Tejun Heofbf59bc2009-02-20 16:29:08 +0900650}
651
Tejun Heo259354d2010-03-10 18:56:10 +0900652static void percpu_modfree(struct module *mod)
Tejun Heofbf59bc2009-02-20 16:29:08 +0900653{
Tejun Heo259354d2010-03-10 18:56:10 +0900654 free_percpu(mod->percpu);
Tejun Heofbf59bc2009-02-20 16:29:08 +0900655}
656
Rusty Russell49668682010-08-05 12:59:10 -0600657static unsigned int find_pcpusec(struct load_info *info)
Tejun Heo6b588c12009-02-20 16:29:07 +0900658{
Rusty Russell49668682010-08-05 12:59:10 -0600659 return find_sec(info, ".data..percpu");
Tejun Heo6b588c12009-02-20 16:29:07 +0900660}
661
Tejun Heo259354d2010-03-10 18:56:10 +0900662static void percpu_modcopy(struct module *mod,
663 const void *from, unsigned long size)
Tejun Heo6b588c12009-02-20 16:29:07 +0900664{
665 int cpu;
666
667 for_each_possible_cpu(cpu)
Tejun Heo259354d2010-03-10 18:56:10 +0900668 memcpy(per_cpu_ptr(mod->percpu, cpu), from, size);
Tejun Heo6b588c12009-02-20 16:29:07 +0900669}
670
Thomas Gleixner383776f2017-02-27 15:37:36 +0100671bool __is_module_percpu_address(unsigned long addr, unsigned long *can_addr)
Tejun Heo10fad5e2010-03-10 18:57:54 +0900672{
673 struct module *mod;
674 unsigned int cpu;
675
676 preempt_disable();
677
678 list_for_each_entry_rcu(mod, &modules, list) {
Rusty Russell0d21b0e2013-01-12 11:38:44 +1030679 if (mod->state == MODULE_STATE_UNFORMED)
680 continue;
Tejun Heo10fad5e2010-03-10 18:57:54 +0900681 if (!mod->percpu_size)
682 continue;
683 for_each_possible_cpu(cpu) {
684 void *start = per_cpu_ptr(mod->percpu, cpu);
Thomas Gleixner383776f2017-02-27 15:37:36 +0100685 void *va = (void *)addr;
Tejun Heo10fad5e2010-03-10 18:57:54 +0900686
Thomas Gleixner383776f2017-02-27 15:37:36 +0100687 if (va >= start && va < start + mod->percpu_size) {
Peter Zijlstra8ce371f2017-03-20 12:26:55 +0100688 if (can_addr) {
Thomas Gleixner383776f2017-02-27 15:37:36 +0100689 *can_addr = (unsigned long) (va - start);
Peter Zijlstra8ce371f2017-03-20 12:26:55 +0100690 *can_addr += (unsigned long)
691 per_cpu_ptr(mod->percpu,
692 get_boot_cpu_id());
693 }
Tejun Heo10fad5e2010-03-10 18:57:54 +0900694 preempt_enable();
695 return true;
696 }
697 }
698 }
699
700 preempt_enable();
701 return false;
Daniel Walker22a8bde2007-10-18 03:06:07 -0700702}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703
Thomas Gleixner383776f2017-02-27 15:37:36 +0100704/**
705 * is_module_percpu_address - test whether address is from module static percpu
706 * @addr: address to test
707 *
708 * Test whether @addr belongs to module static percpu area.
709 *
710 * RETURNS:
711 * %true if @addr is from module static percpu area
712 */
713bool is_module_percpu_address(unsigned long addr)
714{
715 return __is_module_percpu_address(addr, NULL);
716}
717
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718#else /* ... !CONFIG_SMP */
Tejun Heo6b588c12009-02-20 16:29:07 +0900719
Tejun Heo259354d2010-03-10 18:56:10 +0900720static inline void __percpu *mod_percpu(struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721{
722 return NULL;
723}
Rusty Russell9eb76d72013-07-03 10:06:29 +0930724static int percpu_modalloc(struct module *mod, struct load_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725{
Rusty Russell9eb76d72013-07-03 10:06:29 +0930726 /* UP modules shouldn't have this section: ENOMEM isn't quite right */
727 if (info->sechdrs[info->index.pcpu].sh_size != 0)
728 return -ENOMEM;
729 return 0;
Tejun Heo259354d2010-03-10 18:56:10 +0900730}
731static inline void percpu_modfree(struct module *mod)
732{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733}
Rusty Russell49668682010-08-05 12:59:10 -0600734static unsigned int find_pcpusec(struct load_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735{
736 return 0;
737}
Tejun Heo259354d2010-03-10 18:56:10 +0900738static inline void percpu_modcopy(struct module *mod,
739 const void *from, unsigned long size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740{
741 /* pcpusec should be 0, and size of that section should be 0. */
742 BUG_ON(size != 0);
743}
Tejun Heo10fad5e2010-03-10 18:57:54 +0900744bool is_module_percpu_address(unsigned long addr)
745{
746 return false;
747}
Tejun Heo6b588c12009-02-20 16:29:07 +0900748
Thomas Gleixner383776f2017-02-27 15:37:36 +0100749bool __is_module_percpu_address(unsigned long addr, unsigned long *can_addr)
750{
751 return false;
752}
753
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754#endif /* CONFIG_SMP */
755
Matt Domschc988d2b2005-06-23 22:05:15 -0700756#define MODINFO_ATTR(field) \
757static void setup_modinfo_##field(struct module *mod, const char *s) \
758{ \
759 mod->field = kstrdup(s, GFP_KERNEL); \
760} \
761static ssize_t show_modinfo_##field(struct module_attribute *mattr, \
Kay Sievers4befb022011-07-24 22:06:04 +0930762 struct module_kobject *mk, char *buffer) \
Matt Domschc988d2b2005-06-23 22:05:15 -0700763{ \
Chen Gangcc56ded2013-08-20 15:34:21 +0930764 return scnprintf(buffer, PAGE_SIZE, "%s\n", mk->mod->field); \
Matt Domschc988d2b2005-06-23 22:05:15 -0700765} \
766static int modinfo_##field##_exists(struct module *mod) \
767{ \
768 return mod->field != NULL; \
769} \
770static void free_modinfo_##field(struct module *mod) \
771{ \
Daniel Walker22a8bde2007-10-18 03:06:07 -0700772 kfree(mod->field); \
773 mod->field = NULL; \
Matt Domschc988d2b2005-06-23 22:05:15 -0700774} \
775static struct module_attribute modinfo_##field = { \
Tejun Heo7b595752007-06-14 03:45:17 +0900776 .attr = { .name = __stringify(field), .mode = 0444 }, \
Matt Domschc988d2b2005-06-23 22:05:15 -0700777 .show = show_modinfo_##field, \
778 .setup = setup_modinfo_##field, \
779 .test = modinfo_##field##_exists, \
780 .free = free_modinfo_##field, \
781};
782
783MODINFO_ATTR(version);
784MODINFO_ATTR(srcversion);
785
Arjan van de Vene14af7e2008-01-25 21:08:33 +0100786static char last_unloaded_module[MODULE_NAME_LEN+1];
787
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -0800788#ifdef CONFIG_MODULE_UNLOAD
Steven Rostedteb0c5372010-03-29 14:25:18 -0400789
790EXPORT_TRACEPOINT_SYMBOL(module_get);
791
Masami Hiramatsue513cc12014-11-10 09:30:29 +1030792/* MODULE_REF_BASE is the base reference count by kmodule loader. */
793#define MODULE_REF_BASE 1
794
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795/* Init the unload section of the module. */
Rusty Russell9f85a4b2010-08-05 12:59:04 -0600796static int module_unload_init(struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797{
Masami Hiramatsue513cc12014-11-10 09:30:29 +1030798 /*
799 * Initialize reference counter to MODULE_REF_BASE.
800 * refcnt == 0 means module is going.
801 */
802 atomic_set(&mod->refcnt, MODULE_REF_BASE);
Rusty Russell9f85a4b2010-08-05 12:59:04 -0600803
Linus Torvalds2c02dfe2010-05-31 12:19:37 -0700804 INIT_LIST_HEAD(&mod->source_list);
805 INIT_LIST_HEAD(&mod->target_list);
Christoph Lametere1783a22010-01-05 15:34:50 +0900806
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 /* Hold reference count during initialization. */
Masami Hiramatsue513cc12014-11-10 09:30:29 +1030808 atomic_inc(&mod->refcnt);
Rusty Russell9f85a4b2010-08-05 12:59:04 -0600809
810 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811}
812
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813/* Does a already use b? */
814static int already_uses(struct module *a, struct module *b)
815{
816 struct module_use *use;
817
Linus Torvalds2c02dfe2010-05-31 12:19:37 -0700818 list_for_each_entry(use, &b->source_list, source_list) {
819 if (use->source == a) {
Jim Cromie5e124162011-12-06 12:11:31 -0700820 pr_debug("%s uses %s!\n", a->name, b->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 return 1;
822 }
823 }
Jim Cromie5e124162011-12-06 12:11:31 -0700824 pr_debug("%s does not use %s!\n", a->name, b->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 return 0;
826}
827
Linus Torvalds2c02dfe2010-05-31 12:19:37 -0700828/*
829 * Module a uses b
830 * - we add 'a' as a "source", 'b' as a "target" of module use
831 * - the module_use is added to the list of 'b' sources (so
832 * 'b' can walk the list to see who sourced them), and of 'a'
833 * targets (so 'a' can see what modules it targets).
834 */
835static int add_module_usage(struct module *a, struct module *b)
836{
Linus Torvalds2c02dfe2010-05-31 12:19:37 -0700837 struct module_use *use;
838
Jim Cromie5e124162011-12-06 12:11:31 -0700839 pr_debug("Allocating new usage for %s.\n", a->name);
Linus Torvalds2c02dfe2010-05-31 12:19:37 -0700840 use = kmalloc(sizeof(*use), GFP_ATOMIC);
Markus Elfring9ad04572017-10-06 16:27:26 +0200841 if (!use)
Linus Torvalds2c02dfe2010-05-31 12:19:37 -0700842 return -ENOMEM;
Linus Torvalds2c02dfe2010-05-31 12:19:37 -0700843
844 use->source = a;
845 use->target = b;
846 list_add(&use->source_list, &b->source_list);
847 list_add(&use->target_list, &a->target_list);
Linus Torvalds2c02dfe2010-05-31 12:19:37 -0700848 return 0;
849}
850
Rusty Russell75676502010-06-05 11:17:36 -0600851/* Module a uses b: caller needs module_mutex() */
Rusty Russell9bea7f22010-06-05 11:17:37 -0600852int ref_module(struct module *a, struct module *b)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853{
Rusty Russellc8e21ce2010-06-05 11:17:35 -0600854 int err;
Kay Sievers270a6c42007-01-18 13:26:15 +0100855
Rusty Russell9bea7f22010-06-05 11:17:37 -0600856 if (b == NULL || already_uses(a, b))
Linus Torvalds218ce732010-05-25 16:48:30 -0700857 return 0;
Linus Torvalds218ce732010-05-25 16:48:30 -0700858
Rusty Russell9bea7f22010-06-05 11:17:37 -0600859 /* If module isn't available, we fail. */
860 err = strong_try_module_get(b);
Rusty Russellc9a3ba52008-01-29 17:13:18 -0500861 if (err)
Rusty Russell9bea7f22010-06-05 11:17:37 -0600862 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863
Linus Torvalds2c02dfe2010-05-31 12:19:37 -0700864 err = add_module_usage(a, b);
865 if (err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 module_put(b);
Rusty Russell9bea7f22010-06-05 11:17:37 -0600867 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 }
Rusty Russell9bea7f22010-06-05 11:17:37 -0600869 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870}
Rusty Russell9bea7f22010-06-05 11:17:37 -0600871EXPORT_SYMBOL_GPL(ref_module);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872
873/* Clear the unload stuff of the module. */
874static void module_unload_free(struct module *mod)
875{
Linus Torvalds2c02dfe2010-05-31 12:19:37 -0700876 struct module_use *use, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877
Rusty Russell75676502010-06-05 11:17:36 -0600878 mutex_lock(&module_mutex);
Linus Torvalds2c02dfe2010-05-31 12:19:37 -0700879 list_for_each_entry_safe(use, tmp, &mod->target_list, target_list) {
880 struct module *i = use->target;
Jim Cromie5e124162011-12-06 12:11:31 -0700881 pr_debug("%s unusing %s\n", mod->name, i->name);
Linus Torvalds2c02dfe2010-05-31 12:19:37 -0700882 module_put(i);
883 list_del(&use->source_list);
884 list_del(&use->target_list);
885 kfree(use);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 }
Rusty Russell75676502010-06-05 11:17:36 -0600887 mutex_unlock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888}
889
890#ifdef CONFIG_MODULE_FORCE_UNLOAD
Akinobu Mitafb169792006-01-08 01:04:29 -0800891static inline int try_force_unload(unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892{
893 int ret = (flags & O_TRUNC);
894 if (ret)
Rusty Russell373d4d02013-01-21 17:17:39 +1030895 add_taint(TAINT_FORCED_RMMOD, LOCKDEP_NOW_UNRELIABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 return ret;
897}
898#else
Akinobu Mitafb169792006-01-08 01:04:29 -0800899static inline int try_force_unload(unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900{
901 return 0;
902}
903#endif /* CONFIG_MODULE_FORCE_UNLOAD */
904
Masami Hiramatsue513cc12014-11-10 09:30:29 +1030905/* Try to release refcount of module, 0 means success. */
906static int try_release_module_ref(struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907{
Masami Hiramatsue513cc12014-11-10 09:30:29 +1030908 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909
Masami Hiramatsue513cc12014-11-10 09:30:29 +1030910 /* Try to decrement refcnt which we set at loading */
911 ret = atomic_sub_return(MODULE_REF_BASE, &mod->refcnt);
912 BUG_ON(ret < 0);
913 if (ret)
914 /* Someone can put this right now, recover with checking */
915 ret = atomic_add_unless(&mod->refcnt, MODULE_REF_BASE, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916
Masami Hiramatsue513cc12014-11-10 09:30:29 +1030917 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918}
919
920static int try_stop_module(struct module *mod, int flags, int *forced)
921{
Masami Hiramatsue513cc12014-11-10 09:30:29 +1030922 /* If it's not unused, quit unless we're forcing. */
923 if (try_release_module_ref(mod) != 0) {
924 *forced = try_force_unload(flags);
925 if (!(*forced))
926 return -EWOULDBLOCK;
927 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928
Masami Hiramatsue513cc12014-11-10 09:30:29 +1030929 /* Mark it as dying. */
930 mod->state = MODULE_STATE_GOING;
931
932 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933}
934
Rusty Russelld5db1392015-01-22 11:13:14 +1030935/**
936 * module_refcount - return the refcount or -1 if unloading
937 *
938 * @mod: the module we're checking
939 *
940 * Returns:
941 * -1 if the module is in the process of unloading
942 * otherwise the number of references in the kernel to the module
943 */
944int module_refcount(struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945{
Rusty Russelld5db1392015-01-22 11:13:14 +1030946 return atomic_read(&mod->refcnt) - MODULE_REF_BASE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947}
948EXPORT_SYMBOL(module_refcount);
949
950/* This exists whether we can unload or not */
951static void free_module(struct module *mod);
952
Heiko Carstens17da2bd2009-01-14 14:14:10 +0100953SYSCALL_DEFINE2(delete_module, const char __user *, name_user,
954 unsigned int, flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955{
956 struct module *mod;
Greg Kroah-Hartmandfff0a02007-02-23 14:54:57 -0800957 char name[MODULE_NAME_LEN];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 int ret, forced = 0;
959
Kees Cook3d433212009-04-02 15:49:29 -0700960 if (!capable(CAP_SYS_MODULE) || modules_disabled)
Greg Kroah-Hartmandfff0a02007-02-23 14:54:57 -0800961 return -EPERM;
962
963 if (strncpy_from_user(name, name_user, MODULE_NAME_LEN-1) < 0)
964 return -EFAULT;
965 name[MODULE_NAME_LEN-1] = '\0';
966
Richard Guy Briggsf6276ac2017-05-02 10:16:04 -0400967 audit_log_kern_module(name);
968
Tejun Heo3fc1f1e2010-05-06 18:49:20 +0200969 if (mutex_lock_interruptible(&module_mutex) != 0)
970 return -EINTR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971
972 mod = find_module(name);
973 if (!mod) {
974 ret = -ENOENT;
975 goto out;
976 }
977
Linus Torvalds2c02dfe2010-05-31 12:19:37 -0700978 if (!list_empty(&mod->source_list)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 /* Other modules depend on us: get rid of them first. */
980 ret = -EWOULDBLOCK;
981 goto out;
982 }
983
984 /* Doing init or already dying? */
985 if (mod->state != MODULE_STATE_LIVE) {
Rusty Russell3f2b9c92013-09-17 05:48:51 +0930986 /* FIXME: if (force), slam module count damn the torpedoes */
Jim Cromie5e124162011-12-06 12:11:31 -0700987 pr_debug("%s already dying\n", mod->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 ret = -EBUSY;
989 goto out;
990 }
991
992 /* If it has an init func, it must have an exit func to unload */
Rusty Russellaf49d922007-10-16 23:26:27 -0700993 if (mod->init && !mod->exit) {
Akinobu Mitafb169792006-01-08 01:04:29 -0800994 forced = try_force_unload(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 if (!forced) {
996 /* This module can't be removed */
997 ret = -EBUSY;
998 goto out;
999 }
1000 }
1001
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 /* Stop the machine so refcounts can't move and disable module. */
1003 ret = try_stop_module(mod, flags, &forced);
1004 if (ret != 0)
1005 goto out;
1006
Peter Oberparleiterdf4b5652008-04-21 14:34:31 +02001007 mutex_unlock(&module_mutex);
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001008 /* Final destruction now no one is using it. */
Peter Oberparleiterdf4b5652008-04-21 14:34:31 +02001009 if (mod->exit != NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 mod->exit();
Peter Oberparleiterdf4b5652008-04-21 14:34:31 +02001011 blocking_notifier_call_chain(&module_notify_list,
1012 MODULE_STATE_GOING, mod);
Jessica Yu7e545d62016-03-16 20:55:39 -04001013 klp_module_going(mod);
Jessica Yu7dcd1822016-02-16 17:32:33 -05001014 ftrace_release_mod(mod);
1015
Arjan van de Ven22a9d642009-01-07 08:45:46 -08001016 async_synchronize_full();
Rusty Russell75676502010-06-05 11:17:36 -06001017
Arjan van de Vene14af7e2008-01-25 21:08:33 +01001018 /* Store the name of the last unloaded module for diagnostic purposes */
Rusty Russellefa53452008-01-29 17:13:20 -05001019 strlcpy(last_unloaded_module, mod->name, sizeof(last_unloaded_module));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020
Rusty Russell75676502010-06-05 11:17:36 -06001021 free_module(mod);
1022 return 0;
1023out:
Ashutosh Naik6389a382006-03-23 03:00:46 -08001024 mutex_unlock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 return ret;
1026}
1027
Jianjun Kongd1e99d72008-12-08 14:26:29 +08001028static inline void print_unload_info(struct seq_file *m, struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029{
1030 struct module_use *use;
1031 int printed_something = 0;
1032
Rusty Russelld5db1392015-01-22 11:13:14 +10301033 seq_printf(m, " %i ", module_refcount(mod));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034
Ionut Alexa6da0b562014-11-10 09:31:29 +10301035 /*
1036 * Always include a trailing , so userspace can differentiate
1037 * between this and the old multi-field proc format.
1038 */
Linus Torvalds2c02dfe2010-05-31 12:19:37 -07001039 list_for_each_entry(use, &mod->source_list, source_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 printed_something = 1;
Linus Torvalds2c02dfe2010-05-31 12:19:37 -07001041 seq_printf(m, "%s,", use->source->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 }
1043
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 if (mod->init != NULL && mod->exit == NULL) {
1045 printed_something = 1;
Ionut Alexa6da0b562014-11-10 09:31:29 +10301046 seq_puts(m, "[permanent],");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 }
1048
1049 if (!printed_something)
Ionut Alexa6da0b562014-11-10 09:31:29 +10301050 seq_puts(m, "-");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051}
1052
1053void __symbol_put(const char *symbol)
1054{
1055 struct module *owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056
Rusty Russell24da1cb2007-07-15 23:41:46 -07001057 preempt_disable();
Tim Abbott414fd312008-12-05 19:03:56 -05001058 if (!find_symbol(symbol, &owner, NULL, true, false))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 BUG();
1060 module_put(owner);
Rusty Russell24da1cb2007-07-15 23:41:46 -07001061 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062}
1063EXPORT_SYMBOL(__symbol_put);
1064
Rusty Russell7d1d16e2009-08-26 22:02:54 +09301065/* Note this assumes addr is a function, which it currently always is. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066void symbol_put_addr(void *addr)
1067{
Trent Piepho5e376612006-05-15 09:44:06 -07001068 struct module *modaddr;
Rusty Russell7d1d16e2009-08-26 22:02:54 +09301069 unsigned long a = (unsigned long)dereference_function_descriptor(addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070
Rusty Russell7d1d16e2009-08-26 22:02:54 +09301071 if (core_kernel_text(a))
Trent Piepho5e376612006-05-15 09:44:06 -07001072 return;
1073
Peter Zijlstra275d7d42015-08-20 10:34:59 +09301074 /*
1075 * Even though we hold a reference on the module; we still need to
1076 * disable preemption in order to safely traverse the data structure.
1077 */
1078 preempt_disable();
Rusty Russell7d1d16e2009-08-26 22:02:54 +09301079 modaddr = __module_text_address(a);
Rusty Russella6e6abd2009-03-31 13:05:31 -06001080 BUG_ON(!modaddr);
Trent Piepho5e376612006-05-15 09:44:06 -07001081 module_put(modaddr);
Peter Zijlstra275d7d42015-08-20 10:34:59 +09301082 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083}
1084EXPORT_SYMBOL_GPL(symbol_put_addr);
1085
1086static ssize_t show_refcnt(struct module_attribute *mattr,
Kay Sievers4befb022011-07-24 22:06:04 +09301087 struct module_kobject *mk, char *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088{
Rusty Russelld5db1392015-01-22 11:13:14 +10301089 return sprintf(buffer, "%i\n", module_refcount(mk->mod));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090}
1091
Kay Sieverscca3e702012-01-13 09:32:15 +10301092static struct module_attribute modinfo_refcnt =
1093 __ATTR(refcnt, 0444, show_refcnt, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094
Steven Rostedtd53799b2012-03-26 12:50:52 +10301095void __module_get(struct module *module)
1096{
1097 if (module) {
1098 preempt_disable();
Masami Hiramatsu2f35c412014-11-10 09:29:29 +10301099 atomic_inc(&module->refcnt);
Steven Rostedtd53799b2012-03-26 12:50:52 +10301100 trace_module_get(module, _RET_IP_);
1101 preempt_enable();
1102 }
1103}
1104EXPORT_SYMBOL(__module_get);
1105
1106bool try_module_get(struct module *module)
1107{
1108 bool ret = true;
1109
1110 if (module) {
1111 preempt_disable();
Masami Hiramatsue513cc12014-11-10 09:30:29 +10301112 /* Note: here, we can fail to get a reference */
1113 if (likely(module_is_live(module) &&
1114 atomic_inc_not_zero(&module->refcnt) != 0))
Steven Rostedtd53799b2012-03-26 12:50:52 +10301115 trace_module_get(module, _RET_IP_);
Masami Hiramatsue513cc12014-11-10 09:30:29 +10301116 else
Steven Rostedtd53799b2012-03-26 12:50:52 +10301117 ret = false;
1118
1119 preempt_enable();
1120 }
1121 return ret;
1122}
1123EXPORT_SYMBOL(try_module_get);
1124
Al Virof6a57032006-10-18 01:47:25 -04001125void module_put(struct module *module)
1126{
Masami Hiramatsue513cc12014-11-10 09:30:29 +10301127 int ret;
1128
Al Virof6a57032006-10-18 01:47:25 -04001129 if (module) {
Christoph Lametere1783a22010-01-05 15:34:50 +09001130 preempt_disable();
Masami Hiramatsue513cc12014-11-10 09:30:29 +10301131 ret = atomic_dec_if_positive(&module->refcnt);
1132 WARN_ON(ret < 0); /* Failed to put refcount */
Li Zefanae832d12010-03-24 10:57:43 +08001133 trace_module_put(module, _RET_IP_);
Christoph Lametere1783a22010-01-05 15:34:50 +09001134 preempt_enable();
Al Virof6a57032006-10-18 01:47:25 -04001135 }
1136}
1137EXPORT_SYMBOL(module_put);
1138
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139#else /* !CONFIG_MODULE_UNLOAD */
Jianjun Kongd1e99d72008-12-08 14:26:29 +08001140static inline void print_unload_info(struct seq_file *m, struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141{
1142 /* We don't know the usage count, or what modules are using. */
Ionut Alexa6da0b562014-11-10 09:31:29 +10301143 seq_puts(m, " - -");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144}
1145
1146static inline void module_unload_free(struct module *mod)
1147{
1148}
1149
Rusty Russell9bea7f22010-06-05 11:17:37 -06001150int ref_module(struct module *a, struct module *b)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151{
Rusty Russell9bea7f22010-06-05 11:17:37 -06001152 return strong_try_module_get(b);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153}
Rusty Russell9bea7f22010-06-05 11:17:37 -06001154EXPORT_SYMBOL_GPL(ref_module);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155
Rusty Russell9f85a4b2010-08-05 12:59:04 -06001156static inline int module_unload_init(struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157{
Rusty Russell9f85a4b2010-08-05 12:59:04 -06001158 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159}
1160#endif /* CONFIG_MODULE_UNLOAD */
1161
Kevin Winchester53999bf2012-01-15 19:32:55 -04001162static size_t module_flags_taint(struct module *mod, char *buf)
1163{
1164 size_t l = 0;
Petr Mladek7fd83292016-09-21 13:47:22 +02001165 int i;
Kevin Winchester53999bf2012-01-15 19:32:55 -04001166
Petr Mladek7fd83292016-09-21 13:47:22 +02001167 for (i = 0; i < TAINT_FLAGS_COUNT; i++) {
1168 if (taint_flags[i].module && test_bit(i, &mod->taints))
Larry Finger5eb7c0d2017-01-01 20:25:25 -06001169 buf[l++] = taint_flags[i].c_true;
Petr Mladek7fd83292016-09-21 13:47:22 +02001170 }
1171
Kevin Winchester53999bf2012-01-15 19:32:55 -04001172 return l;
1173}
1174
Kay Sievers1f717402006-11-24 12:15:25 +01001175static ssize_t show_initstate(struct module_attribute *mattr,
Kay Sievers4befb022011-07-24 22:06:04 +09301176 struct module_kobject *mk, char *buffer)
Kay Sievers1f717402006-11-24 12:15:25 +01001177{
1178 const char *state = "unknown";
1179
Kay Sievers4befb022011-07-24 22:06:04 +09301180 switch (mk->mod->state) {
Kay Sievers1f717402006-11-24 12:15:25 +01001181 case MODULE_STATE_LIVE:
1182 state = "live";
1183 break;
1184 case MODULE_STATE_COMING:
1185 state = "coming";
1186 break;
1187 case MODULE_STATE_GOING:
1188 state = "going";
1189 break;
Rusty Russell0d21b0e2013-01-12 11:38:44 +10301190 default:
1191 BUG();
Kay Sievers1f717402006-11-24 12:15:25 +01001192 }
1193 return sprintf(buffer, "%s\n", state);
1194}
1195
Kay Sieverscca3e702012-01-13 09:32:15 +10301196static struct module_attribute modinfo_initstate =
1197 __ATTR(initstate, 0444, show_initstate, NULL);
Kay Sievers1f717402006-11-24 12:15:25 +01001198
Kay Sievers88bfa322011-07-24 22:06:04 +09301199static ssize_t store_uevent(struct module_attribute *mattr,
1200 struct module_kobject *mk,
1201 const char *buffer, size_t count)
1202{
Peter Rajnohadf44b472018-12-05 12:27:44 +01001203 int rc;
1204
1205 rc = kobject_synth_uevent(&mk->kobj, buffer, count);
1206 return rc ? rc : count;
Kay Sievers88bfa322011-07-24 22:06:04 +09301207}
1208
Kay Sieverscca3e702012-01-13 09:32:15 +10301209struct module_attribute module_uevent =
1210 __ATTR(uevent, 0200, NULL, store_uevent);
1211
1212static ssize_t show_coresize(struct module_attribute *mattr,
1213 struct module_kobject *mk, char *buffer)
1214{
Rusty Russell7523e4d2015-11-26 09:44:08 +10301215 return sprintf(buffer, "%u\n", mk->mod->core_layout.size);
Kay Sieverscca3e702012-01-13 09:32:15 +10301216}
1217
1218static struct module_attribute modinfo_coresize =
1219 __ATTR(coresize, 0444, show_coresize, NULL);
1220
1221static ssize_t show_initsize(struct module_attribute *mattr,
1222 struct module_kobject *mk, char *buffer)
1223{
Rusty Russell7523e4d2015-11-26 09:44:08 +10301224 return sprintf(buffer, "%u\n", mk->mod->init_layout.size);
Kay Sieverscca3e702012-01-13 09:32:15 +10301225}
1226
1227static struct module_attribute modinfo_initsize =
1228 __ATTR(initsize, 0444, show_initsize, NULL);
1229
1230static ssize_t show_taint(struct module_attribute *mattr,
1231 struct module_kobject *mk, char *buffer)
1232{
1233 size_t l;
1234
1235 l = module_flags_taint(mk->mod, buffer);
1236 buffer[l++] = '\n';
1237 return l;
1238}
1239
1240static struct module_attribute modinfo_taint =
1241 __ATTR(taint, 0444, show_taint, NULL);
Kay Sievers88bfa322011-07-24 22:06:04 +09301242
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001243static struct module_attribute *modinfo_attrs[] = {
Kay Sieverscca3e702012-01-13 09:32:15 +10301244 &module_uevent,
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001245 &modinfo_version,
1246 &modinfo_srcversion,
Kay Sieverscca3e702012-01-13 09:32:15 +10301247 &modinfo_initstate,
1248 &modinfo_coresize,
1249 &modinfo_initsize,
1250 &modinfo_taint,
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001251#ifdef CONFIG_MODULE_UNLOAD
Kay Sieverscca3e702012-01-13 09:32:15 +10301252 &modinfo_refcnt,
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001253#endif
1254 NULL,
1255};
1256
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257static const char vermagic[] = VERMAGIC_STRING;
1258
Rusty Russellc6e665c2009-03-31 13:05:33 -06001259static int try_to_force_load(struct module *mod, const char *reason)
Linus Torvalds826e4502008-05-04 17:04:16 -07001260{
1261#ifdef CONFIG_MODULE_FORCE_LOAD
Andi Kleen25ddbb12008-10-15 22:01:41 -07001262 if (!test_taint(TAINT_FORCED_MODULE))
Andrew Mortonbddb12b2013-11-12 15:11:28 -08001263 pr_warn("%s: %s: kernel tainted.\n", mod->name, reason);
Rusty Russell373d4d02013-01-21 17:17:39 +10301264 add_taint_module(mod, TAINT_FORCED_MODULE, LOCKDEP_NOW_UNRELIABLE);
Linus Torvalds826e4502008-05-04 17:04:16 -07001265 return 0;
1266#else
1267 return -ENOEXEC;
1268#endif
1269}
1270
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271#ifdef CONFIG_MODVERSIONS
Ard Biesheuvel71810db2017-02-03 09:54:06 +00001272
1273static u32 resolve_rel_crc(const s32 *crc)
Rusty Russelld4703ae2009-12-15 16:28:32 -06001274{
Ard Biesheuvel71810db2017-02-03 09:54:06 +00001275 return *(u32 *)((void *)crc + *crc);
Rusty Russelld4703ae2009-12-15 16:28:32 -06001276}
1277
Kees Cook49019422017-04-21 15:35:26 -07001278static int check_version(const struct load_info *info,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279 const char *symname,
Ionut Alexa6da0b562014-11-10 09:31:29 +10301280 struct module *mod,
Ard Biesheuvel71810db2017-02-03 09:54:06 +00001281 const s32 *crc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282{
Kees Cook49019422017-04-21 15:35:26 -07001283 Elf_Shdr *sechdrs = info->sechdrs;
1284 unsigned int versindex = info->index.vers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285 unsigned int i, num_versions;
1286 struct modversion_info *versions;
1287
1288 /* Exporting module didn't supply crcs? OK, we're already tainted. */
1289 if (!crc)
1290 return 1;
1291
Rusty Russella5dd6972008-05-09 16:24:21 +10001292 /* No versions at all? modprobe --force does this. */
1293 if (versindex == 0)
1294 return try_to_force_load(mod, symname) == 0;
1295
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 versions = (void *) sechdrs[versindex].sh_addr;
1297 num_versions = sechdrs[versindex].sh_size
1298 / sizeof(struct modversion_info);
1299
1300 for (i = 0; i < num_versions; i++) {
Ard Biesheuvel71810db2017-02-03 09:54:06 +00001301 u32 crcval;
1302
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303 if (strcmp(versions[i].name, symname) != 0)
1304 continue;
1305
Ard Biesheuvel71810db2017-02-03 09:54:06 +00001306 if (IS_ENABLED(CONFIG_MODULE_REL_CRCS))
1307 crcval = resolve_rel_crc(crc);
1308 else
1309 crcval = *crc;
1310 if (versions[i].crc == crcval)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311 return 1;
Ard Biesheuvel71810db2017-02-03 09:54:06 +00001312 pr_debug("Found checksum %X vs module %lX\n",
1313 crcval, versions[i].crc);
Linus Torvalds826e4502008-05-04 17:04:16 -07001314 goto bad_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315 }
Linus Torvalds826e4502008-05-04 17:04:16 -07001316
Linus Torvaldsfaaae2a2016-11-29 15:20:14 -08001317 /* Broken toolchain. Warn once, then let it go.. */
Kees Cook3e2e8572017-04-21 15:35:27 -07001318 pr_warn_once("%s: no symbol version for %s\n", info->name, symname);
Linus Torvaldsfaaae2a2016-11-29 15:20:14 -08001319 return 1;
Linus Torvalds826e4502008-05-04 17:04:16 -07001320
1321bad_version:
Ionut Alexa6da0b562014-11-10 09:31:29 +10301322 pr_warn("%s: disagrees about version of symbol %s\n",
Kees Cook3e2e8572017-04-21 15:35:27 -07001323 info->name, symname);
Linus Torvalds826e4502008-05-04 17:04:16 -07001324 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325}
1326
Kees Cook49019422017-04-21 15:35:26 -07001327static inline int check_modstruct_version(const struct load_info *info,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328 struct module *mod)
1329{
Ard Biesheuvel71810db2017-02-03 09:54:06 +00001330 const s32 *crc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331
Peter Zijlstra926a59b2015-05-27 11:09:35 +09301332 /*
1333 * Since this should be found in kernel (which can't be removed), no
1334 * locking is necessary -- use preempt_disable() to placate lockdep.
1335 */
1336 preempt_disable();
Masahiro Yamada996302c2018-06-24 00:37:44 +09001337 if (!find_symbol("module_layout", NULL, &crc, true, false)) {
Peter Zijlstra926a59b2015-05-27 11:09:35 +09301338 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339 BUG();
Peter Zijlstra926a59b2015-05-27 11:09:35 +09301340 }
1341 preempt_enable();
Masahiro Yamada996302c2018-06-24 00:37:44 +09001342 return check_version(info, "module_layout", mod, crc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343}
1344
Rusty Russell91e37a72008-05-09 16:25:28 +10001345/* First part is kernel version, which we ignore if module has crcs. */
1346static inline int same_magic(const char *amagic, const char *bmagic,
1347 bool has_crcs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348{
Rusty Russell91e37a72008-05-09 16:25:28 +10001349 if (has_crcs) {
1350 amagic += strcspn(amagic, " ");
1351 bmagic += strcspn(bmagic, " ");
1352 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353 return strcmp(amagic, bmagic) == 0;
1354}
1355#else
Kees Cook49019422017-04-21 15:35:26 -07001356static inline int check_version(const struct load_info *info,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357 const char *symname,
Ionut Alexa6da0b562014-11-10 09:31:29 +10301358 struct module *mod,
Ard Biesheuvel71810db2017-02-03 09:54:06 +00001359 const s32 *crc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360{
1361 return 1;
1362}
1363
Kees Cook49019422017-04-21 15:35:26 -07001364static inline int check_modstruct_version(const struct load_info *info,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365 struct module *mod)
1366{
1367 return 1;
1368}
1369
Rusty Russell91e37a72008-05-09 16:25:28 +10001370static inline int same_magic(const char *amagic, const char *bmagic,
1371 bool has_crcs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372{
1373 return strcmp(amagic, bmagic) == 0;
1374}
1375#endif /* CONFIG_MODVERSIONS */
1376
Rusty Russell75676502010-06-05 11:17:36 -06001377/* Resolve a symbol for this module. I.e. if we find one, record usage. */
Rusty Russell49668682010-08-05 12:59:10 -06001378static const struct kernel_symbol *resolve_symbol(struct module *mod,
1379 const struct load_info *info,
Tim Abbott414fd312008-12-05 19:03:56 -05001380 const char *name,
Rusty Russell9bea7f22010-06-05 11:17:37 -06001381 char ownername[])
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382{
1383 struct module *owner;
Tim Abbott414fd312008-12-05 19:03:56 -05001384 const struct kernel_symbol *sym;
Ard Biesheuvel71810db2017-02-03 09:54:06 +00001385 const s32 *crc;
Rusty Russell9bea7f22010-06-05 11:17:37 -06001386 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387
Peter Zijlstrad64810f2015-02-11 15:01:13 +10301388 /*
1389 * The module_mutex should not be a heavily contended lock;
1390 * if we get the occasional sleep here, we'll go an extra iteration
1391 * in the wait_event_interruptible(), which is harmless.
1392 */
1393 sched_annotate_sleep();
Rusty Russell75676502010-06-05 11:17:36 -06001394 mutex_lock(&module_mutex);
Tim Abbott414fd312008-12-05 19:03:56 -05001395 sym = find_symbol(name, &owner, &crc,
Andi Kleen25ddbb12008-10-15 22:01:41 -07001396 !(mod->taints & (1 << TAINT_PROPRIETARY_MODULE)), true);
Rusty Russell9bea7f22010-06-05 11:17:37 -06001397 if (!sym)
1398 goto unlock;
1399
Kees Cook49019422017-04-21 15:35:26 -07001400 if (!check_version(info, name, mod, crc)) {
Rusty Russell9bea7f22010-06-05 11:17:37 -06001401 sym = ERR_PTR(-EINVAL);
1402 goto getname;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403 }
Rusty Russell9bea7f22010-06-05 11:17:37 -06001404
1405 err = ref_module(mod, owner);
1406 if (err) {
1407 sym = ERR_PTR(err);
1408 goto getname;
1409 }
1410
1411getname:
1412 /* We must make copy under the lock if we failed to get ref. */
1413 strncpy(ownername, module_name(owner), MODULE_NAME_LEN);
1414unlock:
Rusty Russell75676502010-06-05 11:17:36 -06001415 mutex_unlock(&module_mutex);
Linus Torvalds218ce732010-05-25 16:48:30 -07001416 return sym;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417}
1418
Rusty Russell49668682010-08-05 12:59:10 -06001419static const struct kernel_symbol *
1420resolve_symbol_wait(struct module *mod,
1421 const struct load_info *info,
1422 const char *name)
Rusty Russell9bea7f22010-06-05 11:17:37 -06001423{
1424 const struct kernel_symbol *ksym;
Rusty Russell49668682010-08-05 12:59:10 -06001425 char owner[MODULE_NAME_LEN];
Rusty Russell9bea7f22010-06-05 11:17:37 -06001426
1427 if (wait_event_interruptible_timeout(module_wq,
Rusty Russell49668682010-08-05 12:59:10 -06001428 !IS_ERR(ksym = resolve_symbol(mod, info, name, owner))
1429 || PTR_ERR(ksym) != -EBUSY,
Rusty Russell9bea7f22010-06-05 11:17:37 -06001430 30 * HZ) <= 0) {
Andrew Mortonbddb12b2013-11-12 15:11:28 -08001431 pr_warn("%s: gave up waiting for init of module %s.\n",
1432 mod->name, owner);
Rusty Russell9bea7f22010-06-05 11:17:37 -06001433 }
1434 return ksym;
1435}
1436
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437/*
1438 * /sys/module/foo/sections stuff
1439 * J. Corbet <corbet@lwn.net>
1440 */
Rusty Russell8f6d0372010-08-05 12:59:09 -06001441#ifdef CONFIG_SYSFS
Ben Hutchings10b465a2009-12-19 14:43:01 +00001442
Rusty Russell8f6d0372010-08-05 12:59:09 -06001443#ifdef CONFIG_KALLSYMS
Ben Hutchings10b465a2009-12-19 14:43:01 +00001444static inline bool sect_empty(const Elf_Shdr *sect)
1445{
1446 return !(sect->sh_flags & SHF_ALLOC) || sect->sh_size == 0;
1447}
1448
Ionut Alexa6da0b562014-11-10 09:31:29 +10301449struct module_sect_attr {
Rusty Russella58730c2008-03-13 09:03:44 +00001450 struct module_attribute mattr;
1451 char *name;
1452 unsigned long address;
1453};
1454
Ionut Alexa6da0b562014-11-10 09:31:29 +10301455struct module_sect_attrs {
Rusty Russella58730c2008-03-13 09:03:44 +00001456 struct attribute_group grp;
1457 unsigned int nsections;
1458 struct module_sect_attr attrs[0];
1459};
1460
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461static ssize_t module_sect_show(struct module_attribute *mattr,
Kay Sievers4befb022011-07-24 22:06:04 +09301462 struct module_kobject *mk, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463{
1464 struct module_sect_attr *sattr =
1465 container_of(mattr, struct module_sect_attr, mattr);
Thomas Richterbe71eda2018-04-18 09:14:36 +02001466 return sprintf(buf, "0x%px\n", kptr_restrict < 2 ?
1467 (void *)sattr->address : NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468}
1469
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001470static void free_sect_attrs(struct module_sect_attrs *sect_attrs)
1471{
Rusty Russella58730c2008-03-13 09:03:44 +00001472 unsigned int section;
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001473
1474 for (section = 0; section < sect_attrs->nsections; section++)
1475 kfree(sect_attrs->attrs[section].name);
1476 kfree(sect_attrs);
1477}
1478
Rusty Russell8f6d0372010-08-05 12:59:09 -06001479static void add_sect_attrs(struct module *mod, const struct load_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480{
1481 unsigned int nloaded = 0, i, size[2];
1482 struct module_sect_attrs *sect_attrs;
1483 struct module_sect_attr *sattr;
1484 struct attribute **gattr;
Daniel Walker22a8bde2007-10-18 03:06:07 -07001485
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486 /* Count loaded sections and allocate structures */
Rusty Russell8f6d0372010-08-05 12:59:09 -06001487 for (i = 0; i < info->hdr->e_shnum; i++)
1488 if (!sect_empty(&info->sechdrs[i]))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489 nloaded++;
Gustavo A. R. Silva8d1b73d2019-06-06 13:18:53 -05001490 size[0] = ALIGN(struct_size(sect_attrs, attrs, nloaded),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491 sizeof(sect_attrs->grp.attrs[0]));
1492 size[1] = (nloaded + 1) * sizeof(sect_attrs->grp.attrs[0]);
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001493 sect_attrs = kzalloc(size[0] + size[1], GFP_KERNEL);
1494 if (sect_attrs == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495 return;
1496
1497 /* Setup section attributes. */
1498 sect_attrs->grp.name = "sections";
1499 sect_attrs->grp.attrs = (void *)sect_attrs + size[0];
1500
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001501 sect_attrs->nsections = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502 sattr = &sect_attrs->attrs[0];
1503 gattr = &sect_attrs->grp.attrs[0];
Rusty Russell8f6d0372010-08-05 12:59:09 -06001504 for (i = 0; i < info->hdr->e_shnum; i++) {
1505 Elf_Shdr *sec = &info->sechdrs[i];
1506 if (sect_empty(sec))
Helge Deller35dead42009-12-03 00:29:15 +01001507 continue;
Rusty Russell8f6d0372010-08-05 12:59:09 -06001508 sattr->address = sec->sh_addr;
1509 sattr->name = kstrdup(info->secstrings + sec->sh_name,
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001510 GFP_KERNEL);
1511 if (sattr->name == NULL)
1512 goto out;
1513 sect_attrs->nsections++;
Eric W. Biederman361795b2010-02-12 13:41:56 -08001514 sysfs_attr_init(&sattr->mattr.attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515 sattr->mattr.show = module_sect_show;
1516 sattr->mattr.store = NULL;
1517 sattr->mattr.attr.name = sattr->name;
Linus Torvalds277642d2017-11-12 17:00:53 -08001518 sattr->mattr.attr.mode = S_IRUSR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519 *(gattr++) = &(sattr++)->mattr.attr;
1520 }
1521 *gattr = NULL;
1522
1523 if (sysfs_create_group(&mod->mkobj.kobj, &sect_attrs->grp))
1524 goto out;
1525
1526 mod->sect_attrs = sect_attrs;
1527 return;
1528 out:
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001529 free_sect_attrs(sect_attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530}
1531
1532static void remove_sect_attrs(struct module *mod)
1533{
1534 if (mod->sect_attrs) {
1535 sysfs_remove_group(&mod->mkobj.kobj,
1536 &mod->sect_attrs->grp);
1537 /* We are positive that no one is using any sect attrs
1538 * at this point. Deallocate immediately. */
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001539 free_sect_attrs(mod->sect_attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540 mod->sect_attrs = NULL;
1541 }
1542}
1543
Roland McGrath6d760132007-10-16 23:26:40 -07001544/*
1545 * /sys/module/foo/notes/.section.name gives contents of SHT_NOTE sections.
1546 */
1547
1548struct module_notes_attrs {
1549 struct kobject *dir;
1550 unsigned int notes;
1551 struct bin_attribute attrs[0];
1552};
1553
Chris Wright2c3c8be2010-05-12 18:28:57 -07001554static ssize_t module_notes_read(struct file *filp, struct kobject *kobj,
Roland McGrath6d760132007-10-16 23:26:40 -07001555 struct bin_attribute *bin_attr,
1556 char *buf, loff_t pos, size_t count)
1557{
1558 /*
1559 * The caller checked the pos and count against our size.
1560 */
1561 memcpy(buf, bin_attr->private + pos, count);
1562 return count;
1563}
1564
1565static void free_notes_attrs(struct module_notes_attrs *notes_attrs,
1566 unsigned int i)
1567{
1568 if (notes_attrs->dir) {
1569 while (i-- > 0)
1570 sysfs_remove_bin_file(notes_attrs->dir,
1571 &notes_attrs->attrs[i]);
Alexey Dobriyane9432092008-09-23 23:51:11 +04001572 kobject_put(notes_attrs->dir);
Roland McGrath6d760132007-10-16 23:26:40 -07001573 }
1574 kfree(notes_attrs);
1575}
1576
Rusty Russell8f6d0372010-08-05 12:59:09 -06001577static void add_notes_attrs(struct module *mod, const struct load_info *info)
Roland McGrath6d760132007-10-16 23:26:40 -07001578{
1579 unsigned int notes, loaded, i;
1580 struct module_notes_attrs *notes_attrs;
1581 struct bin_attribute *nattr;
1582
Ingo Molnarea6bff32009-08-28 10:44:56 +02001583 /* failed to create section attributes, so can't create notes */
1584 if (!mod->sect_attrs)
1585 return;
1586
Roland McGrath6d760132007-10-16 23:26:40 -07001587 /* Count notes sections and allocate structures. */
1588 notes = 0;
Rusty Russell8f6d0372010-08-05 12:59:09 -06001589 for (i = 0; i < info->hdr->e_shnum; i++)
1590 if (!sect_empty(&info->sechdrs[i]) &&
1591 (info->sechdrs[i].sh_type == SHT_NOTE))
Roland McGrath6d760132007-10-16 23:26:40 -07001592 ++notes;
1593
1594 if (notes == 0)
1595 return;
1596
Kees Cookacafe7e2018-05-08 13:45:50 -07001597 notes_attrs = kzalloc(struct_size(notes_attrs, attrs, notes),
Roland McGrath6d760132007-10-16 23:26:40 -07001598 GFP_KERNEL);
1599 if (notes_attrs == NULL)
1600 return;
1601
1602 notes_attrs->notes = notes;
1603 nattr = &notes_attrs->attrs[0];
Rusty Russell8f6d0372010-08-05 12:59:09 -06001604 for (loaded = i = 0; i < info->hdr->e_shnum; ++i) {
1605 if (sect_empty(&info->sechdrs[i]))
Roland McGrath6d760132007-10-16 23:26:40 -07001606 continue;
Rusty Russell8f6d0372010-08-05 12:59:09 -06001607 if (info->sechdrs[i].sh_type == SHT_NOTE) {
Eric W. Biederman361795b2010-02-12 13:41:56 -08001608 sysfs_bin_attr_init(nattr);
Roland McGrath6d760132007-10-16 23:26:40 -07001609 nattr->attr.name = mod->sect_attrs->attrs[loaded].name;
1610 nattr->attr.mode = S_IRUGO;
Rusty Russell8f6d0372010-08-05 12:59:09 -06001611 nattr->size = info->sechdrs[i].sh_size;
1612 nattr->private = (void *) info->sechdrs[i].sh_addr;
Roland McGrath6d760132007-10-16 23:26:40 -07001613 nattr->read = module_notes_read;
1614 ++nattr;
1615 }
1616 ++loaded;
1617 }
1618
Greg Kroah-Hartman4ff6abf2007-11-05 22:24:43 -08001619 notes_attrs->dir = kobject_create_and_add("notes", &mod->mkobj.kobj);
Roland McGrath6d760132007-10-16 23:26:40 -07001620 if (!notes_attrs->dir)
1621 goto out;
1622
1623 for (i = 0; i < notes; ++i)
1624 if (sysfs_create_bin_file(notes_attrs->dir,
1625 &notes_attrs->attrs[i]))
1626 goto out;
1627
1628 mod->notes_attrs = notes_attrs;
1629 return;
1630
1631 out:
1632 free_notes_attrs(notes_attrs, i);
1633}
1634
1635static void remove_notes_attrs(struct module *mod)
1636{
1637 if (mod->notes_attrs)
1638 free_notes_attrs(mod->notes_attrs, mod->notes_attrs->notes);
1639}
1640
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641#else
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001642
Rusty Russell8f6d0372010-08-05 12:59:09 -06001643static inline void add_sect_attrs(struct module *mod,
1644 const struct load_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645{
1646}
1647
1648static inline void remove_sect_attrs(struct module *mod)
1649{
1650}
Roland McGrath6d760132007-10-16 23:26:40 -07001651
Rusty Russell8f6d0372010-08-05 12:59:09 -06001652static inline void add_notes_attrs(struct module *mod,
1653 const struct load_info *info)
Roland McGrath6d760132007-10-16 23:26:40 -07001654{
1655}
1656
1657static inline void remove_notes_attrs(struct module *mod)
1658{
1659}
Rusty Russell8f6d0372010-08-05 12:59:09 -06001660#endif /* CONFIG_KALLSYMS */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661
Rusty Russell80a3d1b2010-06-05 11:17:36 -06001662static void del_usage_links(struct module *mod)
1663{
1664#ifdef CONFIG_MODULE_UNLOAD
1665 struct module_use *use;
1666
Rusty Russell75676502010-06-05 11:17:36 -06001667 mutex_lock(&module_mutex);
Rusty Russell80a3d1b2010-06-05 11:17:36 -06001668 list_for_each_entry(use, &mod->target_list, target_list)
1669 sysfs_remove_link(use->target->holders_dir, mod->name);
Rusty Russell75676502010-06-05 11:17:36 -06001670 mutex_unlock(&module_mutex);
Rusty Russell80a3d1b2010-06-05 11:17:36 -06001671#endif
1672}
1673
Corentin Labbe1ba5c082017-06-06 14:17:39 +02001674static int add_usage_links(struct module *mod)
1675{
1676 int ret = 0;
1677#ifdef CONFIG_MODULE_UNLOAD
1678 struct module_use *use;
1679
1680 mutex_lock(&module_mutex);
1681 list_for_each_entry(use, &mod->target_list, target_list) {
1682 ret = sysfs_create_link(use->target->holders_dir,
1683 &mod->mkobj.kobj, mod->name);
1684 if (ret)
1685 break;
1686 }
1687 mutex_unlock(&module_mutex);
1688 if (ret)
1689 del_usage_links(mod);
1690#endif
1691 return ret;
1692}
1693
YueHaibingbc6f2a72019-06-11 23:00:07 +08001694static void module_remove_modinfo_attrs(struct module *mod, int end);
1695
Rusty Russell6407ebb22010-06-05 11:17:36 -06001696static int module_add_modinfo_attrs(struct module *mod)
Matt Domschc988d2b2005-06-23 22:05:15 -07001697{
1698 struct module_attribute *attr;
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001699 struct module_attribute *temp_attr;
Matt Domschc988d2b2005-06-23 22:05:15 -07001700 int error = 0;
1701 int i;
1702
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001703 mod->modinfo_attrs = kzalloc((sizeof(struct module_attribute) *
1704 (ARRAY_SIZE(modinfo_attrs) + 1)),
1705 GFP_KERNEL);
1706 if (!mod->modinfo_attrs)
1707 return -ENOMEM;
1708
1709 temp_attr = mod->modinfo_attrs;
YueHaibingbc6f2a72019-06-11 23:00:07 +08001710 for (i = 0; (attr = modinfo_attrs[i]); i++) {
Rusty Russellc75b5902016-04-12 05:03:09 +09301711 if (!attr->test || attr->test(mod)) {
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001712 memcpy(temp_attr, attr, sizeof(*temp_attr));
Eric W. Biederman361795b2010-02-12 13:41:56 -08001713 sysfs_attr_init(&temp_attr->attr);
Ionut Alexa6da0b562014-11-10 09:31:29 +10301714 error = sysfs_create_file(&mod->mkobj.kobj,
1715 &temp_attr->attr);
YueHaibingbc6f2a72019-06-11 23:00:07 +08001716 if (error)
1717 goto error_out;
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001718 ++temp_attr;
1719 }
Matt Domschc988d2b2005-06-23 22:05:15 -07001720 }
YueHaibingbc6f2a72019-06-11 23:00:07 +08001721
1722 return 0;
1723
1724error_out:
1725 if (i > 0)
1726 module_remove_modinfo_attrs(mod, --i);
Matt Domschc988d2b2005-06-23 22:05:15 -07001727 return error;
1728}
1729
YueHaibingbc6f2a72019-06-11 23:00:07 +08001730static void module_remove_modinfo_attrs(struct module *mod, int end)
Matt Domschc988d2b2005-06-23 22:05:15 -07001731{
1732 struct module_attribute *attr;
1733 int i;
1734
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001735 for (i = 0; (attr = &mod->modinfo_attrs[i]); i++) {
YueHaibingbc6f2a72019-06-11 23:00:07 +08001736 if (end >= 0 && i > end)
1737 break;
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001738 /* pick a field to test for end of list */
1739 if (!attr->attr.name)
1740 break;
Ionut Alexa6da0b562014-11-10 09:31:29 +10301741 sysfs_remove_file(&mod->mkobj.kobj, &attr->attr);
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001742 if (attr->free)
1743 attr->free(mod);
Matt Domschc988d2b2005-06-23 22:05:15 -07001744 }
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001745 kfree(mod->modinfo_attrs);
Matt Domschc988d2b2005-06-23 22:05:15 -07001746}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747
Li Zhong942e4432013-09-03 16:33:57 +09301748static void mod_kobject_put(struct module *mod)
1749{
1750 DECLARE_COMPLETION_ONSTACK(c);
1751 mod->mkobj.kobj_completion = &c;
1752 kobject_put(&mod->mkobj.kobj);
1753 wait_for_completion(&c);
1754}
1755
Rusty Russell6407ebb22010-06-05 11:17:36 -06001756static int mod_sysfs_init(struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757{
1758 int err;
Greg Kroah-Hartman6494a932008-01-27 15:38:40 -08001759 struct kobject *kobj;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760
Greg Kroah-Hartman823bccf2007-04-13 13:15:19 -07001761 if (!module_sysfs_initialized) {
Andrew Mortonbddb12b2013-11-12 15:11:28 -08001762 pr_err("%s: module sysfs not initialized\n", mod->name);
Ed Swierk1cc5f712006-09-25 16:25:36 -07001763 err = -EINVAL;
1764 goto out;
1765 }
Greg Kroah-Hartman6494a932008-01-27 15:38:40 -08001766
1767 kobj = kset_find_obj(module_kset, mod->name);
1768 if (kobj) {
Andrew Mortonbddb12b2013-11-12 15:11:28 -08001769 pr_err("%s: module is already loaded\n", mod->name);
Greg Kroah-Hartman6494a932008-01-27 15:38:40 -08001770 kobject_put(kobj);
1771 err = -EINVAL;
1772 goto out;
1773 }
1774
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775 mod->mkobj.mod = mod;
Kay Sieverse17e0f52006-11-24 12:15:25 +01001776
Greg Kroah-Hartmanac3c8142007-12-17 23:05:35 -07001777 memset(&mod->mkobj.kobj, 0, sizeof(mod->mkobj.kobj));
1778 mod->mkobj.kobj.kset = module_kset;
1779 err = kobject_init_and_add(&mod->mkobj.kobj, &module_ktype, NULL,
1780 "%s", mod->name);
1781 if (err)
Li Zhong942e4432013-09-03 16:33:57 +09301782 mod_kobject_put(mod);
Kay Sievers270a6c42007-01-18 13:26:15 +01001783
Kay Sievers97c146ef2007-11-29 23:46:11 +01001784 /* delay uevent until full sysfs population */
Kay Sievers270a6c42007-01-18 13:26:15 +01001785out:
1786 return err;
1787}
1788
Rusty Russell6407ebb22010-06-05 11:17:36 -06001789static int mod_sysfs_setup(struct module *mod,
Rusty Russell8f6d0372010-08-05 12:59:09 -06001790 const struct load_info *info,
Kay Sievers270a6c42007-01-18 13:26:15 +01001791 struct kernel_param *kparam,
1792 unsigned int num_params)
1793{
1794 int err;
1795
Rusty Russell80a3d1b2010-06-05 11:17:36 -06001796 err = mod_sysfs_init(mod);
1797 if (err)
1798 goto out;
1799
Greg Kroah-Hartman4ff6abf2007-11-05 22:24:43 -08001800 mod->holders_dir = kobject_create_and_add("holders", &mod->mkobj.kobj);
Akinobu Mita240936e2007-04-26 00:12:09 -07001801 if (!mod->holders_dir) {
1802 err = -ENOMEM;
Kay Sievers270a6c42007-01-18 13:26:15 +01001803 goto out_unreg;
Akinobu Mita240936e2007-04-26 00:12:09 -07001804 }
Kay Sievers270a6c42007-01-18 13:26:15 +01001805
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806 err = module_param_sysfs_setup(mod, kparam, num_params);
1807 if (err)
Kay Sievers270a6c42007-01-18 13:26:15 +01001808 goto out_unreg_holders;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809
Matt Domschc988d2b2005-06-23 22:05:15 -07001810 err = module_add_modinfo_attrs(mod);
1811 if (err)
Kay Sieverse17e0f52006-11-24 12:15:25 +01001812 goto out_unreg_param;
Matt Domschc988d2b2005-06-23 22:05:15 -07001813
Corentin Labbe1ba5c082017-06-06 14:17:39 +02001814 err = add_usage_links(mod);
1815 if (err)
1816 goto out_unreg_modinfo_attrs;
1817
Rusty Russell8f6d0372010-08-05 12:59:09 -06001818 add_sect_attrs(mod, info);
1819 add_notes_attrs(mod, info);
Rusty Russell80a3d1b2010-06-05 11:17:36 -06001820
Kay Sieverse17e0f52006-11-24 12:15:25 +01001821 kobject_uevent(&mod->mkobj.kobj, KOBJ_ADD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822 return 0;
1823
Corentin Labbe1ba5c082017-06-06 14:17:39 +02001824out_unreg_modinfo_attrs:
YueHaibingbc6f2a72019-06-11 23:00:07 +08001825 module_remove_modinfo_attrs(mod, -1);
Kay Sieverse17e0f52006-11-24 12:15:25 +01001826out_unreg_param:
1827 module_param_sysfs_remove(mod);
Kay Sievers270a6c42007-01-18 13:26:15 +01001828out_unreg_holders:
Greg Kroah-Hartman78a2d902007-12-20 08:13:05 -08001829 kobject_put(mod->holders_dir);
Kay Sievers270a6c42007-01-18 13:26:15 +01001830out_unreg:
Li Zhong942e4432013-09-03 16:33:57 +09301831 mod_kobject_put(mod);
Rusty Russell80a3d1b2010-06-05 11:17:36 -06001832out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833 return err;
1834}
Denis V. Lunev34e4e2f2008-05-20 13:59:48 +04001835
1836static void mod_sysfs_fini(struct module *mod)
1837{
Rusty Russell8f6d0372010-08-05 12:59:09 -06001838 remove_notes_attrs(mod);
1839 remove_sect_attrs(mod);
Li Zhong942e4432013-09-03 16:33:57 +09301840 mod_kobject_put(mod);
Denis V. Lunev34e4e2f2008-05-20 13:59:48 +04001841}
1842
Rusty Russellcf2fde72015-06-26 06:44:38 +09301843static void init_param_lock(struct module *mod)
1844{
1845 mutex_init(&mod->param_lock);
1846}
Rusty Russell8f6d0372010-08-05 12:59:09 -06001847#else /* !CONFIG_SYSFS */
Denis V. Lunev34e4e2f2008-05-20 13:59:48 +04001848
Rusty Russell8f6d0372010-08-05 12:59:09 -06001849static int mod_sysfs_setup(struct module *mod,
1850 const struct load_info *info,
Rusty Russell6407ebb22010-06-05 11:17:36 -06001851 struct kernel_param *kparam,
1852 unsigned int num_params)
1853{
1854 return 0;
1855}
1856
Denis V. Lunev34e4e2f2008-05-20 13:59:48 +04001857static void mod_sysfs_fini(struct module *mod)
1858{
1859}
1860
YueHaibingbc6f2a72019-06-11 23:00:07 +08001861static void module_remove_modinfo_attrs(struct module *mod, int end)
Rusty Russell36b03602010-08-05 12:59:09 -06001862{
1863}
1864
Rusty Russell80a3d1b2010-06-05 11:17:36 -06001865static void del_usage_links(struct module *mod)
1866{
1867}
1868
Rusty Russellcf2fde72015-06-26 06:44:38 +09301869static void init_param_lock(struct module *mod)
1870{
1871}
Denis V. Lunev34e4e2f2008-05-20 13:59:48 +04001872#endif /* CONFIG_SYSFS */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873
Rusty Russell36b03602010-08-05 12:59:09 -06001874static void mod_sysfs_teardown(struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875{
Rusty Russell80a3d1b2010-06-05 11:17:36 -06001876 del_usage_links(mod);
YueHaibingbc6f2a72019-06-11 23:00:07 +08001877 module_remove_modinfo_attrs(mod, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878 module_param_sysfs_remove(mod);
Greg Kroah-Hartman78a2d902007-12-20 08:13:05 -08001879 kobject_put(mod->mkobj.drivers_dir);
1880 kobject_put(mod->holders_dir);
Denis V. Lunev34e4e2f2008-05-20 13:59:48 +04001881 mod_sysfs_fini(mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882}
1883
Yang Yingliang93651f82019-06-25 17:40:28 +08001884#ifdef CONFIG_ARCH_HAS_STRICT_MODULE_RWX
matthieu castet84e1c6b2010-11-16 22:35:16 +01001885/*
1886 * LKM RO/NX protection: protect module's text/ro-data
1887 * from modification and any data from execution.
Rusty Russell85c898d2015-11-26 09:45:08 +10301888 *
1889 * General layout of module is:
Jessica Yu444d13f2016-07-27 12:06:21 +09301890 * [text] [read-only-data] [ro-after-init] [writable data]
1891 * text_size -----^ ^ ^ ^
1892 * ro_size ------------------------| | |
1893 * ro_after_init_size -----------------------------| |
1894 * size -----------------------------------------------------------|
Rusty Russell85c898d2015-11-26 09:45:08 +10301895 *
1896 * These values are always page-aligned (as is base)
matthieu castet84e1c6b2010-11-16 22:35:16 +01001897 */
Rusty Russell85c898d2015-11-26 09:45:08 +10301898static void frob_text(const struct module_layout *layout,
1899 int (*set_memory)(unsigned long start, int num_pages))
matthieu castet84e1c6b2010-11-16 22:35:16 +01001900{
Rusty Russell85c898d2015-11-26 09:45:08 +10301901 BUG_ON((unsigned long)layout->base & (PAGE_SIZE-1));
1902 BUG_ON((unsigned long)layout->text_size & (PAGE_SIZE-1));
1903 set_memory((unsigned long)layout->base,
1904 layout->text_size >> PAGE_SHIFT);
matthieu castet84e1c6b2010-11-16 22:35:16 +01001905}
1906
Yang Yingliang93651f82019-06-25 17:40:28 +08001907#ifdef CONFIG_STRICT_MODULE_RWX
Rusty Russell85c898d2015-11-26 09:45:08 +10301908static void frob_rodata(const struct module_layout *layout,
1909 int (*set_memory)(unsigned long start, int num_pages))
matthieu castet84e1c6b2010-11-16 22:35:16 +01001910{
Rusty Russell85c898d2015-11-26 09:45:08 +10301911 BUG_ON((unsigned long)layout->base & (PAGE_SIZE-1));
1912 BUG_ON((unsigned long)layout->text_size & (PAGE_SIZE-1));
1913 BUG_ON((unsigned long)layout->ro_size & (PAGE_SIZE-1));
1914 set_memory((unsigned long)layout->base + layout->text_size,
1915 (layout->ro_size - layout->text_size) >> PAGE_SHIFT);
matthieu castet84e1c6b2010-11-16 22:35:16 +01001916}
1917
Jessica Yu444d13f2016-07-27 12:06:21 +09301918static void frob_ro_after_init(const struct module_layout *layout,
1919 int (*set_memory)(unsigned long start, int num_pages))
1920{
1921 BUG_ON((unsigned long)layout->base & (PAGE_SIZE-1));
1922 BUG_ON((unsigned long)layout->ro_size & (PAGE_SIZE-1));
1923 BUG_ON((unsigned long)layout->ro_after_init_size & (PAGE_SIZE-1));
1924 set_memory((unsigned long)layout->base + layout->ro_size,
1925 (layout->ro_after_init_size - layout->ro_size) >> PAGE_SHIFT);
1926}
1927
Rusty Russell85c898d2015-11-26 09:45:08 +10301928static void frob_writable_data(const struct module_layout *layout,
1929 int (*set_memory)(unsigned long start, int num_pages))
matthieu castet84e1c6b2010-11-16 22:35:16 +01001930{
Rusty Russell85c898d2015-11-26 09:45:08 +10301931 BUG_ON((unsigned long)layout->base & (PAGE_SIZE-1));
Jessica Yu444d13f2016-07-27 12:06:21 +09301932 BUG_ON((unsigned long)layout->ro_after_init_size & (PAGE_SIZE-1));
Rusty Russell85c898d2015-11-26 09:45:08 +10301933 BUG_ON((unsigned long)layout->size & (PAGE_SIZE-1));
Jessica Yu444d13f2016-07-27 12:06:21 +09301934 set_memory((unsigned long)layout->base + layout->ro_after_init_size,
1935 (layout->size - layout->ro_after_init_size) >> PAGE_SHIFT);
Jan Glauber01526ed2011-05-19 16:55:26 -06001936}
1937
Rusty Russell85c898d2015-11-26 09:45:08 +10301938/* livepatching wants to disable read-only so it can frob module. */
1939void module_disable_ro(const struct module *mod)
Jan Glauber01526ed2011-05-19 16:55:26 -06001940{
AKASHI Takahiro39290b32016-11-14 15:15:05 +09001941 if (!rodata_enabled)
1942 return;
1943
Rusty Russell85c898d2015-11-26 09:45:08 +10301944 frob_text(&mod->core_layout, set_memory_rw);
1945 frob_rodata(&mod->core_layout, set_memory_rw);
Jessica Yu444d13f2016-07-27 12:06:21 +09301946 frob_ro_after_init(&mod->core_layout, set_memory_rw);
Rusty Russell85c898d2015-11-26 09:45:08 +10301947 frob_text(&mod->init_layout, set_memory_rw);
1948 frob_rodata(&mod->init_layout, set_memory_rw);
Josh Poimboeuf20ef10c2015-11-26 09:42:08 +10301949}
1950
Jessica Yu444d13f2016-07-27 12:06:21 +09301951void module_enable_ro(const struct module *mod, bool after_init)
Josh Poimboeuf20ef10c2015-11-26 09:42:08 +10301952{
AKASHI Takahiro39290b32016-11-14 15:15:05 +09001953 if (!rodata_enabled)
1954 return;
1955
Rick Edgecombe1a7b7d92019-04-25 17:11:37 -07001956 set_vm_flush_reset_perms(mod->core_layout.base);
1957 set_vm_flush_reset_perms(mod->init_layout.base);
Rusty Russell85c898d2015-11-26 09:45:08 +10301958 frob_text(&mod->core_layout, set_memory_ro);
Nadav Amitf2c65fb2019-04-25 17:11:31 -07001959
Rusty Russell85c898d2015-11-26 09:45:08 +10301960 frob_rodata(&mod->core_layout, set_memory_ro);
1961 frob_text(&mod->init_layout, set_memory_ro);
1962 frob_rodata(&mod->init_layout, set_memory_ro);
Jessica Yu444d13f2016-07-27 12:06:21 +09301963
1964 if (after_init)
1965 frob_ro_after_init(&mod->core_layout, set_memory_ro);
Jan Glauber01526ed2011-05-19 16:55:26 -06001966}
1967
Rusty Russell85c898d2015-11-26 09:45:08 +10301968static void module_enable_nx(const struct module *mod)
Jan Glauber01526ed2011-05-19 16:55:26 -06001969{
Rusty Russell85c898d2015-11-26 09:45:08 +10301970 frob_rodata(&mod->core_layout, set_memory_nx);
Jessica Yu444d13f2016-07-27 12:06:21 +09301971 frob_ro_after_init(&mod->core_layout, set_memory_nx);
Rusty Russell85c898d2015-11-26 09:45:08 +10301972 frob_writable_data(&mod->core_layout, set_memory_nx);
1973 frob_rodata(&mod->init_layout, set_memory_nx);
1974 frob_writable_data(&mod->init_layout, set_memory_nx);
1975}
1976
matthieu castet84e1c6b2010-11-16 22:35:16 +01001977/* Iterate through all modules and set each module's text as RW */
Daniel J Blueman5d05c702011-03-08 22:01:47 +08001978void set_all_modules_text_rw(void)
matthieu castet84e1c6b2010-11-16 22:35:16 +01001979{
1980 struct module *mod;
1981
AKASHI Takahiro39290b32016-11-14 15:15:05 +09001982 if (!rodata_enabled)
1983 return;
1984
matthieu castet84e1c6b2010-11-16 22:35:16 +01001985 mutex_lock(&module_mutex);
1986 list_for_each_entry_rcu(mod, &modules, list) {
Rusty Russell0d21b0e2013-01-12 11:38:44 +10301987 if (mod->state == MODULE_STATE_UNFORMED)
1988 continue;
Rusty Russell85c898d2015-11-26 09:45:08 +10301989
1990 frob_text(&mod->core_layout, set_memory_rw);
1991 frob_text(&mod->init_layout, set_memory_rw);
matthieu castet84e1c6b2010-11-16 22:35:16 +01001992 }
1993 mutex_unlock(&module_mutex);
1994}
1995
1996/* Iterate through all modules and set each module's text as RO */
Daniel J Blueman5d05c702011-03-08 22:01:47 +08001997void set_all_modules_text_ro(void)
matthieu castet84e1c6b2010-11-16 22:35:16 +01001998{
1999 struct module *mod;
2000
AKASHI Takahiro39290b32016-11-14 15:15:05 +09002001 if (!rodata_enabled)
2002 return;
2003
matthieu castet84e1c6b2010-11-16 22:35:16 +01002004 mutex_lock(&module_mutex);
2005 list_for_each_entry_rcu(mod, &modules, list) {
Aaron Tomlin905dd707f2016-10-27 10:36:06 +01002006 /*
2007 * Ignore going modules since it's possible that ro
2008 * protection has already been disabled, otherwise we'll
2009 * run into protection faults at module deallocation.
2010 */
2011 if (mod->state == MODULE_STATE_UNFORMED ||
2012 mod->state == MODULE_STATE_GOING)
Rusty Russell0d21b0e2013-01-12 11:38:44 +10302013 continue;
Rusty Russell85c898d2015-11-26 09:45:08 +10302014
2015 frob_text(&mod->core_layout, set_memory_ro);
2016 frob_text(&mod->init_layout, set_memory_ro);
matthieu castet84e1c6b2010-11-16 22:35:16 +01002017 }
2018 mutex_unlock(&module_mutex);
2019}
Yang Yingliang93651f82019-06-25 17:40:28 +08002020#else /* !CONFIG_STRICT_MODULE_RWX */
Rusty Russell85c898d2015-11-26 09:45:08 +10302021static void module_enable_nx(const struct module *mod) { }
Yang Yingliang93651f82019-06-25 17:40:28 +08002022#endif /* CONFIG_STRICT_MODULE_RWX */
Yang Yingliang2eef1392019-06-20 10:18:14 +08002023static void module_enable_x(const struct module *mod)
2024{
2025 frob_text(&mod->core_layout, set_memory_x);
2026 frob_text(&mod->init_layout, set_memory_x);
2027}
Yang Yingliang93651f82019-06-25 17:40:28 +08002028#else /* !CONFIG_ARCH_HAS_STRICT_MODULE_RWX */
2029static void module_enable_nx(const struct module *mod) { }
2030static void module_enable_x(const struct module *mod) { }
2031#endif /* CONFIG_ARCH_HAS_STRICT_MODULE_RWX */
2032
matthieu castet84e1c6b2010-11-16 22:35:16 +01002033
Jessica Yu1ce15ef2016-03-22 20:03:16 -04002034#ifdef CONFIG_LIVEPATCH
2035/*
2036 * Persist Elf information about a module. Copy the Elf header,
2037 * section header table, section string table, and symtab section
2038 * index from info to mod->klp_info.
2039 */
2040static int copy_module_elf(struct module *mod, struct load_info *info)
2041{
2042 unsigned int size, symndx;
2043 int ret;
2044
2045 size = sizeof(*mod->klp_info);
2046 mod->klp_info = kmalloc(size, GFP_KERNEL);
2047 if (mod->klp_info == NULL)
2048 return -ENOMEM;
2049
2050 /* Elf header */
2051 size = sizeof(mod->klp_info->hdr);
2052 memcpy(&mod->klp_info->hdr, info->hdr, size);
2053
2054 /* Elf section header table */
2055 size = sizeof(*info->sechdrs) * info->hdr->e_shnum;
zhong jiang9be936f2018-08-01 00:56:17 +08002056 mod->klp_info->sechdrs = kmemdup(info->sechdrs, size, GFP_KERNEL);
Jessica Yu1ce15ef2016-03-22 20:03:16 -04002057 if (mod->klp_info->sechdrs == NULL) {
2058 ret = -ENOMEM;
2059 goto free_info;
2060 }
Jessica Yu1ce15ef2016-03-22 20:03:16 -04002061
2062 /* Elf section name string table */
2063 size = info->sechdrs[info->hdr->e_shstrndx].sh_size;
zhong jiang9be936f2018-08-01 00:56:17 +08002064 mod->klp_info->secstrings = kmemdup(info->secstrings, size, GFP_KERNEL);
Jessica Yu1ce15ef2016-03-22 20:03:16 -04002065 if (mod->klp_info->secstrings == NULL) {
2066 ret = -ENOMEM;
2067 goto free_sechdrs;
2068 }
Jessica Yu1ce15ef2016-03-22 20:03:16 -04002069
2070 /* Elf symbol section index */
2071 symndx = info->index.sym;
2072 mod->klp_info->symndx = symndx;
2073
2074 /*
2075 * For livepatch modules, core_kallsyms.symtab is a complete
2076 * copy of the original symbol table. Adjust sh_addr to point
2077 * to core_kallsyms.symtab since the copy of the symtab in module
2078 * init memory is freed at the end of do_init_module().
2079 */
2080 mod->klp_info->sechdrs[symndx].sh_addr = \
2081 (unsigned long) mod->core_kallsyms.symtab;
2082
2083 return 0;
2084
2085free_sechdrs:
2086 kfree(mod->klp_info->sechdrs);
2087free_info:
2088 kfree(mod->klp_info);
2089 return ret;
2090}
2091
2092static void free_module_elf(struct module *mod)
2093{
2094 kfree(mod->klp_info->sechdrs);
2095 kfree(mod->klp_info->secstrings);
2096 kfree(mod->klp_info);
2097}
2098#else /* !CONFIG_LIVEPATCH */
2099static int copy_module_elf(struct module *mod, struct load_info *info)
2100{
2101 return 0;
2102}
2103
2104static void free_module_elf(struct module *mod)
2105{
2106}
2107#endif /* CONFIG_LIVEPATCH */
2108
Rusty Russellbe1f2212015-01-20 09:07:05 +10302109void __weak module_memfree(void *module_region)
Jonas Bonn74e08fc2011-06-30 21:22:11 +02002110{
Rick Edgecombe1a7b7d92019-04-25 17:11:37 -07002111 /*
2112 * This memory may be RO, and freeing RO memory in an interrupt is not
2113 * supported by vmalloc.
2114 */
2115 WARN_ON(in_interrupt());
Jonas Bonn74e08fc2011-06-30 21:22:11 +02002116 vfree(module_region);
2117}
2118
2119void __weak module_arch_cleanup(struct module *mod)
2120{
2121}
2122
Rusty Russelld453cde2015-01-20 09:07:04 +10302123void __weak module_arch_freeing_init(struct module *mod)
2124{
2125}
2126
Rusty Russell75676502010-06-05 11:17:36 -06002127/* Free a module, remove from lists, etc. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128static void free_module(struct module *mod)
2129{
Li Zefan7ead8b82009-08-17 16:56:28 +08002130 trace_module_free(mod);
2131
Rusty Russell36b03602010-08-05 12:59:09 -06002132 mod_sysfs_teardown(mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133
Rusty Russell944a1fa2013-04-17 13:20:03 +09302134 /* We leave it in list to prevent duplicate loads, but make sure
2135 * that noone uses it while it's being deconstructed. */
Prarit Bhargavad3051b42014-10-14 02:51:39 +10302136 mutex_lock(&module_mutex);
Rusty Russell944a1fa2013-04-17 13:20:03 +09302137 mod->state = MODULE_STATE_UNFORMED;
Prarit Bhargavad3051b42014-10-14 02:51:39 +10302138 mutex_unlock(&module_mutex);
Rusty Russell944a1fa2013-04-17 13:20:03 +09302139
Jason Baronb82bab4b2010-07-27 13:18:01 -07002140 /* Remove dynamic debug info */
2141 ddebug_remove_module(mod->name);
2142
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143 /* Arch-specific cleanup. */
2144 module_arch_cleanup(mod);
2145
2146 /* Module unload stuff */
2147 module_unload_free(mod);
2148
Rusty Russelle180a6b2009-03-31 13:05:29 -06002149 /* Free any allocated parameters. */
2150 destroy_params(mod->kp, mod->num_kp);
2151
Jessica Yu1ce15ef2016-03-22 20:03:16 -04002152 if (is_livepatch_module(mod))
2153 free_module_elf(mod);
2154
Rusty Russell944a1fa2013-04-17 13:20:03 +09302155 /* Now we can delete it from the lists */
2156 mutex_lock(&module_mutex);
Masami Hiramatsu461e34a2014-11-10 09:27:29 +10302157 /* Unlink carefully: kallsyms could be walking list. */
2158 list_del_rcu(&mod->list);
Peter Zijlstra93c2e102015-05-27 11:09:37 +09302159 mod_tree_remove(mod);
Masami Hiramatsu0286b5e2014-11-10 09:28:29 +10302160 /* Remove this module from bug list, this uses list_del_rcu */
Masami Hiramatsu461e34a2014-11-10 09:27:29 +10302161 module_bug_cleanup(mod);
Peter Zijlstra0be964b2015-05-27 11:09:35 +09302162 /* Wait for RCU-sched synchronizing before releasing mod->list and buglist. */
Paul E. McKenneycb2f5532018-11-06 19:17:01 -08002163 synchronize_rcu();
Rusty Russell944a1fa2013-04-17 13:20:03 +09302164 mutex_unlock(&module_mutex);
2165
Rusty Russell85c898d2015-11-26 09:45:08 +10302166 /* This may be empty, but that's OK */
Rusty Russelld453cde2015-01-20 09:07:04 +10302167 module_arch_freeing_init(mod);
Rusty Russell7523e4d2015-11-26 09:44:08 +10302168 module_memfree(mod->init_layout.base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169 kfree(mod->args);
Tejun Heo259354d2010-03-10 18:56:10 +09002170 percpu_modfree(mod);
Rusty Russell9f85a4b2010-08-05 12:59:04 -06002171
Peter Zijlstra35a93932015-02-26 16:23:11 +01002172 /* Free lock-classes; relies on the preceding sync_rcu(). */
Rusty Russell7523e4d2015-11-26 09:44:08 +10302173 lockdep_free_key_range(mod->core_layout.base, mod->core_layout.size);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002174
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175 /* Finally, free the core (containing the module structure) */
Rusty Russell7523e4d2015-11-26 09:44:08 +10302176 module_memfree(mod->core_layout.base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177}
2178
2179void *__symbol_get(const char *symbol)
2180{
2181 struct module *owner;
Tim Abbott414fd312008-12-05 19:03:56 -05002182 const struct kernel_symbol *sym;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183
Rusty Russell24da1cb2007-07-15 23:41:46 -07002184 preempt_disable();
Tim Abbott414fd312008-12-05 19:03:56 -05002185 sym = find_symbol(symbol, &owner, NULL, true, true);
2186 if (sym && strong_try_module_get(owner))
2187 sym = NULL;
Rusty Russell24da1cb2007-07-15 23:41:46 -07002188 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189
Ard Biesheuvel7290d582018-08-21 21:56:09 -07002190 return sym ? (void *)kernel_symbol_value(sym) : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191}
2192EXPORT_SYMBOL_GPL(__symbol_get);
2193
Ashutosh Naikeea8b542006-01-08 01:04:25 -08002194/*
2195 * Ensure that an exported symbol [global namespace] does not already exist
Robert P. J. Day02a3e592007-05-09 07:26:28 +02002196 * in the kernel or in some other module's exported symbol table.
Rusty Russellbe593f42010-06-05 11:17:37 -06002197 *
2198 * You must hold the module_mutex.
Ashutosh Naikeea8b542006-01-08 01:04:25 -08002199 */
Jessica Yu2d25bc52018-11-19 17:43:58 +01002200static int verify_exported_symbols(struct module *mod)
Ashutosh Naikeea8b542006-01-08 01:04:25 -08002201{
Rusty Russellb2111042008-05-01 21:15:00 -05002202 unsigned int i;
Ashutosh Naikeea8b542006-01-08 01:04:25 -08002203 struct module *owner;
Rusty Russellb2111042008-05-01 21:15:00 -05002204 const struct kernel_symbol *s;
2205 struct {
2206 const struct kernel_symbol *sym;
2207 unsigned int num;
2208 } arr[] = {
2209 { mod->syms, mod->num_syms },
2210 { mod->gpl_syms, mod->num_gpl_syms },
2211 { mod->gpl_future_syms, mod->num_gpl_future_syms },
Denys Vlasenkof7f5b672008-07-22 19:24:26 -05002212#ifdef CONFIG_UNUSED_SYMBOLS
Rusty Russellb2111042008-05-01 21:15:00 -05002213 { mod->unused_syms, mod->num_unused_syms },
2214 { mod->unused_gpl_syms, mod->num_unused_gpl_syms },
Denys Vlasenkof7f5b672008-07-22 19:24:26 -05002215#endif
Rusty Russellb2111042008-05-01 21:15:00 -05002216 };
Ashutosh Naikeea8b542006-01-08 01:04:25 -08002217
Rusty Russellb2111042008-05-01 21:15:00 -05002218 for (i = 0; i < ARRAY_SIZE(arr); i++) {
2219 for (s = arr[i].sym; s < arr[i].sym + arr[i].num; s++) {
Ard Biesheuvel7290d582018-08-21 21:56:09 -07002220 if (find_symbol(kernel_symbol_name(s), &owner, NULL,
2221 true, false)) {
Andrew Mortonbddb12b2013-11-12 15:11:28 -08002222 pr_err("%s: exports duplicate symbol %s"
Rusty Russellb2111042008-05-01 21:15:00 -05002223 " (owned by %s)\n",
Ard Biesheuvel7290d582018-08-21 21:56:09 -07002224 mod->name, kernel_symbol_name(s),
2225 module_name(owner));
Rusty Russellb2111042008-05-01 21:15:00 -05002226 return -ENOEXEC;
2227 }
Ashutosh Naikeea8b542006-01-08 01:04:25 -08002228 }
Rusty Russellb2111042008-05-01 21:15:00 -05002229 }
2230 return 0;
Ashutosh Naikeea8b542006-01-08 01:04:25 -08002231}
2232
Matti Linnanvuori9a4b9702007-11-08 08:37:38 -08002233/* Change all symbols so that st_value encodes the pointer directly. */
Rusty Russell49668682010-08-05 12:59:10 -06002234static int simplify_symbols(struct module *mod, const struct load_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002235{
Rusty Russell49668682010-08-05 12:59:10 -06002236 Elf_Shdr *symsec = &info->sechdrs[info->index.sym];
2237 Elf_Sym *sym = (void *)symsec->sh_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238 unsigned long secbase;
Rusty Russell49668682010-08-05 12:59:10 -06002239 unsigned int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002240 int ret = 0;
Tim Abbott414fd312008-12-05 19:03:56 -05002241 const struct kernel_symbol *ksym;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002242
Rusty Russell49668682010-08-05 12:59:10 -06002243 for (i = 1; i < symsec->sh_size / sizeof(Elf_Sym); i++) {
2244 const char *name = info->strtab + sym[i].st_name;
2245
Linus Torvalds1da177e2005-04-16 15:20:36 -07002246 switch (sym[i].st_shndx) {
2247 case SHN_COMMON:
Joe Mario80375982014-02-08 09:01:09 +01002248 /* Ignore common symbols */
2249 if (!strncmp(name, "__gnu_lto", 9))
2250 break;
2251
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252 /* We compiled with -fno-common. These are not
2253 supposed to happen. */
Jim Cromie5e124162011-12-06 12:11:31 -07002254 pr_debug("Common symbol: %s\n", name);
Ionut Alexa6da0b562014-11-10 09:31:29 +10302255 pr_warn("%s: please compile with -fno-common\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256 mod->name);
2257 ret = -ENOEXEC;
2258 break;
2259
2260 case SHN_ABS:
2261 /* Don't need to do anything */
Jim Cromie5e124162011-12-06 12:11:31 -07002262 pr_debug("Absolute symbol: 0x%08lx\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002263 (long)sym[i].st_value);
2264 break;
2265
Jessica Yu1ce15ef2016-03-22 20:03:16 -04002266 case SHN_LIVEPATCH:
2267 /* Livepatch symbols are resolved by livepatch */
2268 break;
2269
Linus Torvalds1da177e2005-04-16 15:20:36 -07002270 case SHN_UNDEF:
Rusty Russell49668682010-08-05 12:59:10 -06002271 ksym = resolve_symbol_wait(mod, info, name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002272 /* Ok if resolved. */
Rusty Russell9bea7f22010-06-05 11:17:37 -06002273 if (ksym && !IS_ERR(ksym)) {
Ard Biesheuvel7290d582018-08-21 21:56:09 -07002274 sym[i].st_value = kernel_symbol_value(ksym);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002275 break;
Tim Abbott414fd312008-12-05 19:03:56 -05002276 }
2277
Linus Torvalds1da177e2005-04-16 15:20:36 -07002278 /* Ok if weak. */
Rusty Russell9bea7f22010-06-05 11:17:37 -06002279 if (!ksym && ELF_ST_BIND(sym[i].st_info) == STB_WEAK)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002280 break;
2281
Rusty Russell9bea7f22010-06-05 11:17:37 -06002282 ret = PTR_ERR(ksym) ?: -ENOENT;
Jason A. Donenfeld62267e02018-06-22 17:38:50 +02002283 pr_warn("%s: Unknown symbol %s (err %d)\n",
2284 mod->name, name, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002285 break;
2286
2287 default:
2288 /* Divert to percpu allocation if a percpu var. */
Rusty Russell49668682010-08-05 12:59:10 -06002289 if (sym[i].st_shndx == info->index.pcpu)
Tejun Heo259354d2010-03-10 18:56:10 +09002290 secbase = (unsigned long)mod_percpu(mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002291 else
Rusty Russell49668682010-08-05 12:59:10 -06002292 secbase = info->sechdrs[sym[i].st_shndx].sh_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002293 sym[i].st_value += secbase;
2294 break;
2295 }
2296 }
2297
2298 return ret;
2299}
2300
Rusty Russell49668682010-08-05 12:59:10 -06002301static int apply_relocations(struct module *mod, const struct load_info *info)
Rusty Russell22e268e2010-08-05 12:59:05 -06002302{
2303 unsigned int i;
2304 int err = 0;
2305
2306 /* Now do relocations. */
Rusty Russell49668682010-08-05 12:59:10 -06002307 for (i = 1; i < info->hdr->e_shnum; i++) {
2308 unsigned int infosec = info->sechdrs[i].sh_info;
Rusty Russell22e268e2010-08-05 12:59:05 -06002309
2310 /* Not a valid relocation section? */
Rusty Russell49668682010-08-05 12:59:10 -06002311 if (infosec >= info->hdr->e_shnum)
Rusty Russell22e268e2010-08-05 12:59:05 -06002312 continue;
2313
2314 /* Don't bother with non-allocated sections */
Rusty Russell49668682010-08-05 12:59:10 -06002315 if (!(info->sechdrs[infosec].sh_flags & SHF_ALLOC))
Rusty Russell22e268e2010-08-05 12:59:05 -06002316 continue;
2317
Jessica Yu1ce15ef2016-03-22 20:03:16 -04002318 /* Livepatch relocation sections are applied by livepatch */
2319 if (info->sechdrs[i].sh_flags & SHF_RELA_LIVEPATCH)
2320 continue;
2321
Rusty Russell49668682010-08-05 12:59:10 -06002322 if (info->sechdrs[i].sh_type == SHT_REL)
2323 err = apply_relocate(info->sechdrs, info->strtab,
2324 info->index.sym, i, mod);
2325 else if (info->sechdrs[i].sh_type == SHT_RELA)
2326 err = apply_relocate_add(info->sechdrs, info->strtab,
2327 info->index.sym, i, mod);
Rusty Russell22e268e2010-08-05 12:59:05 -06002328 if (err < 0)
2329 break;
2330 }
2331 return err;
2332}
2333
Helge Deller088af9a2008-12-31 12:31:18 +01002334/* Additional bytes needed by arch in front of individual sections */
2335unsigned int __weak arch_mod_section_prepend(struct module *mod,
2336 unsigned int section)
2337{
2338 /* default implementation just returns zero */
2339 return 0;
2340}
2341
Linus Torvalds1da177e2005-04-16 15:20:36 -07002342/* Update size with this section: return offset. */
Helge Deller088af9a2008-12-31 12:31:18 +01002343static long get_offset(struct module *mod, unsigned int *size,
2344 Elf_Shdr *sechdr, unsigned int section)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002345{
2346 long ret;
2347
Helge Deller088af9a2008-12-31 12:31:18 +01002348 *size += arch_mod_section_prepend(mod, section);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002349 ret = ALIGN(*size, sechdr->sh_addralign ?: 1);
2350 *size = ret + sechdr->sh_size;
2351 return ret;
2352}
2353
2354/* Lay out the SHF_ALLOC sections in a way not dissimilar to how ld
2355 might -- code, read-only data, read-write data, small data. Tally
2356 sizes, and place the offsets into sh_entsize fields: high bit means it
2357 belongs in init. */
Rusty Russell49668682010-08-05 12:59:10 -06002358static void layout_sections(struct module *mod, struct load_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002359{
2360 static unsigned long const masks[][2] = {
2361 /* NOTE: all executable code must be the first section
2362 * in this array; otherwise modify the text_size
2363 * finder in the two loops below */
2364 { SHF_EXECINSTR | SHF_ALLOC, ARCH_SHF_SMALL },
2365 { SHF_ALLOC, SHF_WRITE | ARCH_SHF_SMALL },
Jessica Yu444d13f2016-07-27 12:06:21 +09302366 { SHF_RO_AFTER_INIT | SHF_ALLOC, ARCH_SHF_SMALL },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367 { SHF_WRITE | SHF_ALLOC, ARCH_SHF_SMALL },
2368 { ARCH_SHF_SMALL | SHF_ALLOC, 0 }
2369 };
2370 unsigned int m, i;
2371
Rusty Russell49668682010-08-05 12:59:10 -06002372 for (i = 0; i < info->hdr->e_shnum; i++)
2373 info->sechdrs[i].sh_entsize = ~0UL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002374
Jim Cromie5e124162011-12-06 12:11:31 -07002375 pr_debug("Core section allocation order:\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002376 for (m = 0; m < ARRAY_SIZE(masks); ++m) {
Rusty Russell49668682010-08-05 12:59:10 -06002377 for (i = 0; i < info->hdr->e_shnum; ++i) {
2378 Elf_Shdr *s = &info->sechdrs[i];
2379 const char *sname = info->secstrings + s->sh_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002380
2381 if ((s->sh_flags & masks[m][0]) != masks[m][0]
2382 || (s->sh_flags & masks[m][1])
2383 || s->sh_entsize != ~0UL
Rusty Russell49668682010-08-05 12:59:10 -06002384 || strstarts(sname, ".init"))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002385 continue;
Rusty Russell7523e4d2015-11-26 09:44:08 +10302386 s->sh_entsize = get_offset(mod, &mod->core_layout.size, s, i);
Jim Cromie5e124162011-12-06 12:11:31 -07002387 pr_debug("\t%s\n", sname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002388 }
matthieu castet84e1c6b2010-11-16 22:35:16 +01002389 switch (m) {
2390 case 0: /* executable */
Rusty Russell7523e4d2015-11-26 09:44:08 +10302391 mod->core_layout.size = debug_align(mod->core_layout.size);
2392 mod->core_layout.text_size = mod->core_layout.size;
matthieu castet84e1c6b2010-11-16 22:35:16 +01002393 break;
2394 case 1: /* RO: text and ro-data */
Rusty Russell7523e4d2015-11-26 09:44:08 +10302395 mod->core_layout.size = debug_align(mod->core_layout.size);
2396 mod->core_layout.ro_size = mod->core_layout.size;
matthieu castet84e1c6b2010-11-16 22:35:16 +01002397 break;
Jessica Yu444d13f2016-07-27 12:06:21 +09302398 case 2: /* RO after init */
2399 mod->core_layout.size = debug_align(mod->core_layout.size);
2400 mod->core_layout.ro_after_init_size = mod->core_layout.size;
2401 break;
2402 case 4: /* whole core */
Rusty Russell7523e4d2015-11-26 09:44:08 +10302403 mod->core_layout.size = debug_align(mod->core_layout.size);
matthieu castet84e1c6b2010-11-16 22:35:16 +01002404 break;
2405 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002406 }
2407
Jim Cromie5e124162011-12-06 12:11:31 -07002408 pr_debug("Init section allocation order:\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002409 for (m = 0; m < ARRAY_SIZE(masks); ++m) {
Rusty Russell49668682010-08-05 12:59:10 -06002410 for (i = 0; i < info->hdr->e_shnum; ++i) {
2411 Elf_Shdr *s = &info->sechdrs[i];
2412 const char *sname = info->secstrings + s->sh_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002413
2414 if ((s->sh_flags & masks[m][0]) != masks[m][0]
2415 || (s->sh_flags & masks[m][1])
2416 || s->sh_entsize != ~0UL
Rusty Russell49668682010-08-05 12:59:10 -06002417 || !strstarts(sname, ".init"))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002418 continue;
Rusty Russell7523e4d2015-11-26 09:44:08 +10302419 s->sh_entsize = (get_offset(mod, &mod->init_layout.size, s, i)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002420 | INIT_OFFSET_MASK);
Jim Cromie5e124162011-12-06 12:11:31 -07002421 pr_debug("\t%s\n", sname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002422 }
matthieu castet84e1c6b2010-11-16 22:35:16 +01002423 switch (m) {
2424 case 0: /* executable */
Rusty Russell7523e4d2015-11-26 09:44:08 +10302425 mod->init_layout.size = debug_align(mod->init_layout.size);
2426 mod->init_layout.text_size = mod->init_layout.size;
matthieu castet84e1c6b2010-11-16 22:35:16 +01002427 break;
2428 case 1: /* RO: text and ro-data */
Rusty Russell7523e4d2015-11-26 09:44:08 +10302429 mod->init_layout.size = debug_align(mod->init_layout.size);
2430 mod->init_layout.ro_size = mod->init_layout.size;
matthieu castet84e1c6b2010-11-16 22:35:16 +01002431 break;
Jessica Yu444d13f2016-07-27 12:06:21 +09302432 case 2:
2433 /*
2434 * RO after init doesn't apply to init_layout (only
2435 * core_layout), so it just takes the value of ro_size.
2436 */
2437 mod->init_layout.ro_after_init_size = mod->init_layout.ro_size;
2438 break;
2439 case 4: /* whole init */
Rusty Russell7523e4d2015-11-26 09:44:08 +10302440 mod->init_layout.size = debug_align(mod->init_layout.size);
matthieu castet84e1c6b2010-11-16 22:35:16 +01002441 break;
2442 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002443 }
2444}
2445
Linus Torvalds1da177e2005-04-16 15:20:36 -07002446static void set_license(struct module *mod, const char *license)
2447{
2448 if (!license)
2449 license = "unspecified";
2450
Florin Malitafa3ba2e82006-10-11 01:21:48 -07002451 if (!license_is_gpl_compatible(license)) {
Andi Kleen25ddbb12008-10-15 22:01:41 -07002452 if (!test_taint(TAINT_PROPRIETARY_MODULE))
Andrew Mortonbddb12b2013-11-12 15:11:28 -08002453 pr_warn("%s: module license '%s' taints kernel.\n",
2454 mod->name, license);
Rusty Russell373d4d02013-01-21 17:17:39 +10302455 add_taint_module(mod, TAINT_PROPRIETARY_MODULE,
2456 LOCKDEP_NOW_UNRELIABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002457 }
2458}
2459
2460/* Parse tag=value strings from .modinfo section */
2461static char *next_string(char *string, unsigned long *secsize)
2462{
2463 /* Skip non-zero chars */
2464 while (string[0]) {
2465 string++;
2466 if ((*secsize)-- <= 1)
2467 return NULL;
2468 }
2469
2470 /* Skip any zero padding. */
2471 while (!string[0]) {
2472 string++;
2473 if ((*secsize)-- <= 1)
2474 return NULL;
2475 }
2476 return string;
2477}
2478
Rusty Russell49668682010-08-05 12:59:10 -06002479static char *get_modinfo(struct load_info *info, const char *tag)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002480{
2481 char *p;
2482 unsigned int taglen = strlen(tag);
Rusty Russell49668682010-08-05 12:59:10 -06002483 Elf_Shdr *infosec = &info->sechdrs[info->index.info];
2484 unsigned long size = infosec->sh_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002485
Jessica Yu5fdc7db2018-06-22 14:00:01 +02002486 /*
2487 * get_modinfo() calls made before rewrite_section_headers()
2488 * must use sh_offset, as sh_addr isn't set!
2489 */
2490 for (p = (char *)info->hdr + infosec->sh_offset; p; p = next_string(p, &size)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002491 if (strncmp(p, tag, taglen) == 0 && p[taglen] == '=')
2492 return p + taglen + 1;
2493 }
2494 return NULL;
2495}
2496
Rusty Russell49668682010-08-05 12:59:10 -06002497static void setup_modinfo(struct module *mod, struct load_info *info)
Matt Domschc988d2b2005-06-23 22:05:15 -07002498{
2499 struct module_attribute *attr;
2500 int i;
2501
2502 for (i = 0; (attr = modinfo_attrs[i]); i++) {
2503 if (attr->setup)
Rusty Russell49668682010-08-05 12:59:10 -06002504 attr->setup(mod, get_modinfo(info, attr->attr.name));
Matt Domschc988d2b2005-06-23 22:05:15 -07002505 }
2506}
Matt Domschc988d2b2005-06-23 22:05:15 -07002507
Rusty Russella263f7762009-09-25 00:32:58 -06002508static void free_modinfo(struct module *mod)
2509{
2510 struct module_attribute *attr;
2511 int i;
2512
2513 for (i = 0; (attr = modinfo_attrs[i]); i++) {
2514 if (attr->free)
2515 attr->free(mod);
2516 }
2517}
2518
Linus Torvalds1da177e2005-04-16 15:20:36 -07002519#ifdef CONFIG_KALLSYMS
WANG Cong15bba372008-07-24 15:41:48 +01002520
Jessica Yu2d25bc52018-11-19 17:43:58 +01002521/* Lookup exported symbol in given range of kernel_symbols */
2522static const struct kernel_symbol *lookup_exported_symbol(const char *name,
2523 const struct kernel_symbol *start,
2524 const struct kernel_symbol *stop)
WANG Cong15bba372008-07-24 15:41:48 +01002525{
Alessio Igor Bogani9d634872011-05-18 22:35:59 +02002526 return bsearch(name, start, stop - start,
2527 sizeof(struct kernel_symbol), cmp_name);
WANG Cong15bba372008-07-24 15:41:48 +01002528}
2529
Tim Abbottca4787b2009-01-05 08:40:10 -06002530static int is_exported(const char *name, unsigned long value,
2531 const struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002532{
Tim Abbottca4787b2009-01-05 08:40:10 -06002533 const struct kernel_symbol *ks;
2534 if (!mod)
Jessica Yu2d25bc52018-11-19 17:43:58 +01002535 ks = lookup_exported_symbol(name, __start___ksymtab, __stop___ksymtab);
Sam Ravnborg3fd68052006-02-08 21:16:45 +01002536 else
Jessica Yu2d25bc52018-11-19 17:43:58 +01002537 ks = lookup_exported_symbol(name, mod->syms, mod->syms + mod->num_syms);
2538
Ard Biesheuvel7290d582018-08-21 21:56:09 -07002539 return ks != NULL && kernel_symbol_value(ks) == value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002540}
2541
2542/* As per nm */
Rusty Russelleded41c2010-08-05 12:59:07 -06002543static char elf_type(const Elf_Sym *sym, const struct load_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002544{
Rusty Russelleded41c2010-08-05 12:59:07 -06002545 const Elf_Shdr *sechdrs = info->sechdrs;
2546
Linus Torvalds1da177e2005-04-16 15:20:36 -07002547 if (ELF_ST_BIND(sym->st_info) == STB_WEAK) {
2548 if (ELF_ST_TYPE(sym->st_info) == STT_OBJECT)
2549 return 'v';
2550 else
2551 return 'w';
2552 }
2553 if (sym->st_shndx == SHN_UNDEF)
2554 return 'U';
Miroslav Benese0224412015-11-26 13:18:06 +10302555 if (sym->st_shndx == SHN_ABS || sym->st_shndx == info->index.pcpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002556 return 'a';
2557 if (sym->st_shndx >= SHN_LORESERVE)
2558 return '?';
2559 if (sechdrs[sym->st_shndx].sh_flags & SHF_EXECINSTR)
2560 return 't';
2561 if (sechdrs[sym->st_shndx].sh_flags & SHF_ALLOC
2562 && sechdrs[sym->st_shndx].sh_type != SHT_NOBITS) {
2563 if (!(sechdrs[sym->st_shndx].sh_flags & SHF_WRITE))
2564 return 'r';
2565 else if (sechdrs[sym->st_shndx].sh_flags & ARCH_SHF_SMALL)
2566 return 'g';
2567 else
2568 return 'd';
2569 }
2570 if (sechdrs[sym->st_shndx].sh_type == SHT_NOBITS) {
2571 if (sechdrs[sym->st_shndx].sh_flags & ARCH_SHF_SMALL)
2572 return 's';
2573 else
2574 return 'b';
2575 }
Rusty Russelleded41c2010-08-05 12:59:07 -06002576 if (strstarts(info->secstrings + sechdrs[sym->st_shndx].sh_name,
2577 ".debug")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002578 return 'n';
Rusty Russelleded41c2010-08-05 12:59:07 -06002579 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002580 return '?';
2581}
2582
Jan Beulich4a496222009-07-06 14:50:42 +01002583static bool is_core_symbol(const Elf_Sym *src, const Elf_Shdr *sechdrs,
Miroslav Benese0224412015-11-26 13:18:06 +10302584 unsigned int shnum, unsigned int pcpundx)
Jan Beulich4a496222009-07-06 14:50:42 +01002585{
2586 const Elf_Shdr *sec;
2587
2588 if (src->st_shndx == SHN_UNDEF
2589 || src->st_shndx >= shnum
2590 || !src->st_name)
2591 return false;
2592
Miroslav Benese0224412015-11-26 13:18:06 +10302593#ifdef CONFIG_KALLSYMS_ALL
2594 if (src->st_shndx == pcpundx)
2595 return true;
2596#endif
2597
Jan Beulich4a496222009-07-06 14:50:42 +01002598 sec = sechdrs + src->st_shndx;
2599 if (!(sec->sh_flags & SHF_ALLOC)
2600#ifndef CONFIG_KALLSYMS_ALL
2601 || !(sec->sh_flags & SHF_EXECINSTR)
2602#endif
2603 || (sec->sh_entsize & INIT_OFFSET_MASK))
2604 return false;
2605
2606 return true;
2607}
2608
Kevin Cernekee48fd1182012-01-13 09:32:14 +10302609/*
2610 * We only allocate and copy the strings needed by the parts of symtab
2611 * we keep. This is simple, but has the effect of making multiple
2612 * copies of duplicates. We could be more sophisticated, see
2613 * linux-kernel thread starting with
2614 * <73defb5e4bca04a6431392cc341112b1@localhost>.
2615 */
Rusty Russell49668682010-08-05 12:59:10 -06002616static void layout_symtab(struct module *mod, struct load_info *info)
Jan Beulich4a496222009-07-06 14:50:42 +01002617{
Rusty Russell49668682010-08-05 12:59:10 -06002618 Elf_Shdr *symsect = info->sechdrs + info->index.sym;
2619 Elf_Shdr *strsect = info->sechdrs + info->index.str;
Jan Beulich4a496222009-07-06 14:50:42 +01002620 const Elf_Sym *src;
Satoru Takeuchi54523ec2012-12-05 12:29:04 +10302621 unsigned int i, nsrc, ndst, strtab_size = 0;
Jan Beulich4a496222009-07-06 14:50:42 +01002622
2623 /* Put symbol section at end of init part of module. */
2624 symsect->sh_flags |= SHF_ALLOC;
Rusty Russell7523e4d2015-11-26 09:44:08 +10302625 symsect->sh_entsize = get_offset(mod, &mod->init_layout.size, symsect,
Rusty Russell49668682010-08-05 12:59:10 -06002626 info->index.sym) | INIT_OFFSET_MASK;
Jim Cromie5e124162011-12-06 12:11:31 -07002627 pr_debug("\t%s\n", info->secstrings + symsect->sh_name);
Jan Beulich4a496222009-07-06 14:50:42 +01002628
Rusty Russell49668682010-08-05 12:59:10 -06002629 src = (void *)info->hdr + symsect->sh_offset;
Jan Beulich4a496222009-07-06 14:50:42 +01002630 nsrc = symsect->sh_size / sizeof(*src);
Kevin Cernekee70b1e9162011-11-12 19:08:55 -08002631
Kevin Cernekee48fd1182012-01-13 09:32:14 +10302632 /* Compute total space required for the core symbols' strtab. */
Rusty Russell59ef28b2012-10-25 10:49:25 +10302633 for (ndst = i = 0; i < nsrc; i++) {
Jessica Yu1ce15ef2016-03-22 20:03:16 -04002634 if (i == 0 || is_livepatch_module(mod) ||
Miroslav Benese0224412015-11-26 13:18:06 +10302635 is_core_symbol(src+i, info->sechdrs, info->hdr->e_shnum,
2636 info->index.pcpu)) {
Rusty Russell59ef28b2012-10-25 10:49:25 +10302637 strtab_size += strlen(&info->strtab[src[i].st_name])+1;
Kevin Cernekee48fd1182012-01-13 09:32:14 +10302638 ndst++;
Jan Beulich554bdfe2009-07-06 14:51:44 +01002639 }
Rusty Russell59ef28b2012-10-25 10:49:25 +10302640 }
Jan Beulich4a496222009-07-06 14:50:42 +01002641
2642 /* Append room for core symbols at end of core part. */
Rusty Russell7523e4d2015-11-26 09:44:08 +10302643 info->symoffs = ALIGN(mod->core_layout.size, symsect->sh_addralign ?: 1);
2644 info->stroffs = mod->core_layout.size = info->symoffs + ndst * sizeof(Elf_Sym);
2645 mod->core_layout.size += strtab_size;
Eugene Loh1c7651f2019-02-25 11:59:58 -08002646 info->core_typeoffs = mod->core_layout.size;
2647 mod->core_layout.size += ndst * sizeof(char);
Rusty Russell7523e4d2015-11-26 09:44:08 +10302648 mod->core_layout.size = debug_align(mod->core_layout.size);
Jan Beulich4a496222009-07-06 14:50:42 +01002649
Jan Beulich554bdfe2009-07-06 14:51:44 +01002650 /* Put string table section at end of init part of module. */
2651 strsect->sh_flags |= SHF_ALLOC;
Rusty Russell7523e4d2015-11-26 09:44:08 +10302652 strsect->sh_entsize = get_offset(mod, &mod->init_layout.size, strsect,
Rusty Russell49668682010-08-05 12:59:10 -06002653 info->index.str) | INIT_OFFSET_MASK;
Jim Cromie5e124162011-12-06 12:11:31 -07002654 pr_debug("\t%s\n", info->secstrings + strsect->sh_name);
Rusty Russell82440622016-02-03 16:55:26 +10302655
2656 /* We'll tack temporary mod_kallsyms on the end. */
2657 mod->init_layout.size = ALIGN(mod->init_layout.size,
2658 __alignof__(struct mod_kallsyms));
2659 info->mod_kallsyms_init_off = mod->init_layout.size;
2660 mod->init_layout.size += sizeof(struct mod_kallsyms);
Eugene Loh1c7651f2019-02-25 11:59:58 -08002661 info->init_typeoffs = mod->init_layout.size;
2662 mod->init_layout.size += nsrc * sizeof(char);
Rusty Russell82440622016-02-03 16:55:26 +10302663 mod->init_layout.size = debug_align(mod->init_layout.size);
Jan Beulich4a496222009-07-06 14:50:42 +01002664}
2665
Rusty Russell82440622016-02-03 16:55:26 +10302666/*
2667 * We use the full symtab and strtab which layout_symtab arranged to
2668 * be appended to the init section. Later we switch to the cut-down
2669 * core-only ones.
2670 */
Rusty Russell811d66a2010-08-05 12:59:12 -06002671static void add_kallsyms(struct module *mod, const struct load_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002672{
Jan Beulich4a496222009-07-06 14:50:42 +01002673 unsigned int i, ndst;
2674 const Elf_Sym *src;
2675 Elf_Sym *dst;
Jan Beulich554bdfe2009-07-06 14:51:44 +01002676 char *s;
Rusty Russelleded41c2010-08-05 12:59:07 -06002677 Elf_Shdr *symsec = &info->sechdrs[info->index.sym];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002678
Rusty Russell82440622016-02-03 16:55:26 +10302679 /* Set up to point into init section. */
2680 mod->kallsyms = mod->init_layout.base + info->mod_kallsyms_init_off;
2681
2682 mod->kallsyms->symtab = (void *)symsec->sh_addr;
2683 mod->kallsyms->num_symtab = symsec->sh_size / sizeof(Elf_Sym);
Rusty Russell511ca6a2010-08-05 12:59:08 -06002684 /* Make sure we get permanent strtab: don't use info->strtab. */
Rusty Russell82440622016-02-03 16:55:26 +10302685 mod->kallsyms->strtab = (void *)info->sechdrs[info->index.str].sh_addr;
Eugene Loh1c7651f2019-02-25 11:59:58 -08002686 mod->kallsyms->typetab = mod->init_layout.base + info->init_typeoffs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002687
Eugene Loh1c7651f2019-02-25 11:59:58 -08002688 /*
2689 * Now populate the cut down core kallsyms for after init
2690 * and set types up while we still have access to sections.
2691 */
Rusty Russell82440622016-02-03 16:55:26 +10302692 mod->core_kallsyms.symtab = dst = mod->core_layout.base + info->symoffs;
2693 mod->core_kallsyms.strtab = s = mod->core_layout.base + info->stroffs;
Eugene Loh1c7651f2019-02-25 11:59:58 -08002694 mod->core_kallsyms.typetab = mod->core_layout.base + info->core_typeoffs;
Rusty Russell82440622016-02-03 16:55:26 +10302695 src = mod->kallsyms->symtab;
2696 for (ndst = i = 0; i < mod->kallsyms->num_symtab; i++) {
Eugene Loh1c7651f2019-02-25 11:59:58 -08002697 mod->kallsyms->typetab[i] = elf_type(src + i, info);
Jessica Yu1ce15ef2016-03-22 20:03:16 -04002698 if (i == 0 || is_livepatch_module(mod) ||
Miroslav Benese0224412015-11-26 13:18:06 +10302699 is_core_symbol(src+i, info->sechdrs, info->hdr->e_shnum,
2700 info->index.pcpu)) {
Eugene Loh1c7651f2019-02-25 11:59:58 -08002701 mod->core_kallsyms.typetab[ndst] =
2702 mod->kallsyms->typetab[i];
Rusty Russell59ef28b2012-10-25 10:49:25 +10302703 dst[ndst] = src[i];
Rusty Russell82440622016-02-03 16:55:26 +10302704 dst[ndst++].st_name = s - mod->core_kallsyms.strtab;
2705 s += strlcpy(s, &mod->kallsyms->strtab[src[i].st_name],
Rusty Russell59ef28b2012-10-25 10:49:25 +10302706 KSYM_NAME_LEN) + 1;
2707 }
Jan Beulich4a496222009-07-06 14:50:42 +01002708 }
Rusty Russell82440622016-02-03 16:55:26 +10302709 mod->core_kallsyms.num_symtab = ndst;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002710}
2711#else
Rusty Russell49668682010-08-05 12:59:10 -06002712static inline void layout_symtab(struct module *mod, struct load_info *info)
Jan Beulich4a496222009-07-06 14:50:42 +01002713{
2714}
Paul Mundt3ae91c22009-10-01 15:43:54 -07002715
Michał Mirosławabbce902010-09-20 01:58:08 +02002716static void add_kallsyms(struct module *mod, const struct load_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002717{
2718}
2719#endif /* CONFIG_KALLSYMS */
2720
Zhou Chengming52796312017-07-07 11:15:58 +08002721static void dynamic_debug_setup(struct module *mod, struct _ddebug *debug, unsigned int num)
Rusty Russell5e458cc2008-10-22 10:00:13 -05002722{
Rusty Russell811d66a2010-08-05 12:59:12 -06002723 if (!debug)
2724 return;
Rasmus Villemoes513770f2019-03-07 16:27:48 -08002725 ddebug_add_module(debug, num, mod->name);
Rusty Russell5e458cc2008-10-22 10:00:13 -05002726}
Jason Baron346e15b2008-08-12 16:46:19 -04002727
Zhou Chengming52796312017-07-07 11:15:58 +08002728static void dynamic_debug_remove(struct module *mod, struct _ddebug *debug)
Yehuda Sadehff49d742010-07-03 13:07:35 +10002729{
2730 if (debug)
Zhou Chengming52796312017-07-07 11:15:58 +08002731 ddebug_remove_module(mod->name);
Yehuda Sadehff49d742010-07-03 13:07:35 +10002732}
2733
Jonas Bonn74e08fc2011-06-30 21:22:11 +02002734void * __weak module_alloc(unsigned long size)
2735{
Rusty Russell82fab442012-12-11 09:38:33 +10302736 return vmalloc_exec(size);
Jonas Bonn74e08fc2011-06-30 21:22:11 +02002737}
2738
Matthias Schiffer38b37d62019-06-07 12:49:11 +02002739bool __weak module_exit_section(const char *name)
2740{
2741 return strstarts(name, ".exit");
2742}
2743
Catalin Marinas4f2294b2009-06-11 13:23:20 +01002744#ifdef CONFIG_DEBUG_KMEMLEAK
Rusty Russell49668682010-08-05 12:59:10 -06002745static void kmemleak_load_module(const struct module *mod,
2746 const struct load_info *info)
Catalin Marinas4f2294b2009-06-11 13:23:20 +01002747{
2748 unsigned int i;
2749
2750 /* only scan the sections containing data */
Catalin Marinasc017b4be32009-10-28 13:33:09 +00002751 kmemleak_scan_area(mod, sizeof(struct module), GFP_KERNEL);
Catalin Marinas4f2294b2009-06-11 13:23:20 +01002752
Rusty Russell49668682010-08-05 12:59:10 -06002753 for (i = 1; i < info->hdr->e_shnum; i++) {
Steven Rostedt06c94942013-05-15 20:33:01 +01002754 /* Scan all writable sections that's not executable */
2755 if (!(info->sechdrs[i].sh_flags & SHF_ALLOC) ||
2756 !(info->sechdrs[i].sh_flags & SHF_WRITE) ||
2757 (info->sechdrs[i].sh_flags & SHF_EXECINSTR))
Catalin Marinas4f2294b2009-06-11 13:23:20 +01002758 continue;
2759
Rusty Russell49668682010-08-05 12:59:10 -06002760 kmemleak_scan_area((void *)info->sechdrs[i].sh_addr,
2761 info->sechdrs[i].sh_size, GFP_KERNEL);
Catalin Marinas4f2294b2009-06-11 13:23:20 +01002762 }
2763}
2764#else
Rusty Russell49668682010-08-05 12:59:10 -06002765static inline void kmemleak_load_module(const struct module *mod,
2766 const struct load_info *info)
Catalin Marinas4f2294b2009-06-11 13:23:20 +01002767{
2768}
2769#endif
2770
Rusty Russell106a4ee2012-09-26 10:09:40 +01002771#ifdef CONFIG_MODULE_SIG
Ben Hutchingsbca014c2016-04-28 09:24:01 +09302772static int module_sig_check(struct load_info *info, int flags)
Rusty Russell106a4ee2012-09-26 10:09:40 +01002773{
2774 int err = -ENOKEY;
Kees Cook34e11692012-10-16 07:31:07 +10302775 const unsigned long markerlen = sizeof(MODULE_SIG_STRING) - 1;
2776 const void *mod = info->hdr;
Rusty Russell106a4ee2012-09-26 10:09:40 +01002777
Ben Hutchingsbca014c2016-04-28 09:24:01 +09302778 /*
2779 * Require flags == 0, as a module with version information
2780 * removed is no longer the module that was signed
2781 */
2782 if (flags == 0 &&
2783 info->len > markerlen &&
Kees Cook34e11692012-10-16 07:31:07 +10302784 memcmp(mod + info->len - markerlen, MODULE_SIG_STRING, markerlen) == 0) {
David Howellscaabe242012-10-20 01:19:29 +01002785 /* We truncate the module to discard the signature */
Kees Cook34e11692012-10-16 07:31:07 +10302786 info->len -= markerlen;
Jessica Yuf314dfe2018-06-29 16:37:08 +02002787 err = mod_verify_sig(mod, info);
Rusty Russell106a4ee2012-09-26 10:09:40 +01002788 }
2789
2790 if (!err) {
2791 info->sig_ok = true;
2792 return 0;
2793 }
2794
2795 /* Not having a signature is only an error if we're strict. */
Jia Zhang2c8fd262018-04-11 11:53:33 +08002796 if (err == -ENOKEY && !is_module_sig_enforced())
Rusty Russell106a4ee2012-09-26 10:09:40 +01002797 err = 0;
2798
2799 return err;
2800}
2801#else /* !CONFIG_MODULE_SIG */
Ben Hutchingsbca014c2016-04-28 09:24:01 +09302802static int module_sig_check(struct load_info *info, int flags)
Rusty Russell106a4ee2012-09-26 10:09:40 +01002803{
2804 return 0;
2805}
2806#endif /* !CONFIG_MODULE_SIG */
2807
Kees Cook34e11692012-10-16 07:31:07 +10302808/* Sanity checks against invalid binaries, wrong arch, weird elf version. */
2809static int elf_header_check(struct load_info *info)
Rusty Russell40dd2562010-08-05 12:59:03 -06002810{
Kees Cook34e11692012-10-16 07:31:07 +10302811 if (info->len < sizeof(*(info->hdr)))
Rusty Russell40dd2562010-08-05 12:59:03 -06002812 return -ENOEXEC;
2813
Kees Cook34e11692012-10-16 07:31:07 +10302814 if (memcmp(info->hdr->e_ident, ELFMAG, SELFMAG) != 0
2815 || info->hdr->e_type != ET_REL
2816 || !elf_check_arch(info->hdr)
2817 || info->hdr->e_shentsize != sizeof(Elf_Shdr))
2818 return -ENOEXEC;
2819
2820 if (info->hdr->e_shoff >= info->len
2821 || (info->hdr->e_shnum * sizeof(Elf_Shdr) >
2822 info->len - info->hdr->e_shoff))
2823 return -ENOEXEC;
2824
2825 return 0;
2826}
2827
Linus Torvalds3afe9f82015-04-07 10:33:49 -07002828#define COPY_CHUNK_SIZE (16*PAGE_SIZE)
2829
2830static int copy_chunked_from_user(void *dst, const void __user *usrc, unsigned long len)
2831{
2832 do {
2833 unsigned long n = min(len, COPY_CHUNK_SIZE);
2834
2835 if (copy_from_user(dst, usrc, n) != 0)
2836 return -EFAULT;
2837 cond_resched();
2838 dst += n;
2839 usrc += n;
2840 len -= n;
2841 } while (len);
2842 return 0;
2843}
2844
Jessica Yu1ce15ef2016-03-22 20:03:16 -04002845#ifdef CONFIG_LIVEPATCH
Josh Poimboeuf2992ef22016-08-25 10:04:45 -05002846static int check_modinfo_livepatch(struct module *mod, struct load_info *info)
Jessica Yu1ce15ef2016-03-22 20:03:16 -04002847{
Josh Poimboeuf2992ef22016-08-25 10:04:45 -05002848 if (get_modinfo(info, "livepatch")) {
2849 mod->klp = true;
2850 add_taint_module(mod, TAINT_LIVEPATCH, LOCKDEP_STILL_OK);
Joe Lawrence7598d162017-01-12 11:57:44 -05002851 pr_notice_once("%s: tainting kernel with TAINT_LIVEPATCH\n",
2852 mod->name);
Josh Poimboeuf2992ef22016-08-25 10:04:45 -05002853 }
Jessica Yu1ce15ef2016-03-22 20:03:16 -04002854
2855 return 0;
2856}
2857#else /* !CONFIG_LIVEPATCH */
Josh Poimboeuf2992ef22016-08-25 10:04:45 -05002858static int check_modinfo_livepatch(struct module *mod, struct load_info *info)
Jessica Yu1ce15ef2016-03-22 20:03:16 -04002859{
2860 if (get_modinfo(info, "livepatch")) {
2861 pr_err("%s: module is marked as livepatch module, but livepatch support is disabled",
2862 mod->name);
2863 return -ENOEXEC;
2864 }
2865
2866 return 0;
2867}
2868#endif /* CONFIG_LIVEPATCH */
2869
Andi Kleencaf75012018-01-25 15:50:28 -08002870static void check_modinfo_retpoline(struct module *mod, struct load_info *info)
2871{
2872 if (retpoline_module_ok(get_modinfo(info, "retpoline")))
2873 return;
2874
2875 pr_warn("%s: loading module not compiled with retpoline compiler.\n",
2876 mod->name);
2877}
2878
Kees Cook34e11692012-10-16 07:31:07 +10302879/* Sets info->hdr and info->len. */
2880static int copy_module_from_user(const void __user *umod, unsigned long len,
2881 struct load_info *info)
2882{
Kees Cook2e72d512012-10-16 07:32:07 +10302883 int err;
2884
Kees Cook34e11692012-10-16 07:31:07 +10302885 info->len = len;
2886 if (info->len < sizeof(*(info->hdr)))
Rusty Russell40dd2562010-08-05 12:59:03 -06002887 return -ENOEXEC;
2888
Mimi Zoharc77b8cd2018-07-13 14:06:02 -04002889 err = security_kernel_load_data(LOADING_MODULE);
Kees Cook2e72d512012-10-16 07:32:07 +10302890 if (err)
2891 return err;
2892
Rusty Russell40dd2562010-08-05 12:59:03 -06002893 /* Suck in entire file: we'll want most of it. */
Kirill A. Shutemovcc9e6052015-03-24 12:31:40 +10302894 info->hdr = __vmalloc(info->len,
Michal Hocko19809c22017-05-08 15:57:44 -07002895 GFP_KERNEL | __GFP_NOWARN, PAGE_KERNEL);
Kees Cook34e11692012-10-16 07:31:07 +10302896 if (!info->hdr)
Rusty Russell40dd2562010-08-05 12:59:03 -06002897 return -ENOMEM;
2898
Linus Torvalds3afe9f82015-04-07 10:33:49 -07002899 if (copy_chunked_from_user(info->hdr, umod, info->len) != 0) {
Kees Cook34e11692012-10-16 07:31:07 +10302900 vfree(info->hdr);
2901 return -EFAULT;
Rusty Russell40dd2562010-08-05 12:59:03 -06002902 }
2903
Rusty Russell40dd2562010-08-05 12:59:03 -06002904 return 0;
Kees Cook34e11692012-10-16 07:31:07 +10302905}
Rusty Russell40dd2562010-08-05 12:59:03 -06002906
Rusty Russelld9131882010-08-05 12:59:08 -06002907static void free_copy(struct load_info *info)
2908{
Rusty Russelld9131882010-08-05 12:59:08 -06002909 vfree(info->hdr);
2910}
2911
Rusty Russell2f3238a2012-10-22 18:09:41 +10302912static int rewrite_section_headers(struct load_info *info, int flags)
Rusty Russell8b5f61a2010-08-05 12:59:06 -06002913{
2914 unsigned int i;
2915
2916 /* This should always be true, but let's be sure. */
2917 info->sechdrs[0].sh_addr = 0;
2918
2919 for (i = 1; i < info->hdr->e_shnum; i++) {
2920 Elf_Shdr *shdr = &info->sechdrs[i];
2921 if (shdr->sh_type != SHT_NOBITS
2922 && info->len < shdr->sh_offset + shdr->sh_size) {
Andrew Mortonbddb12b2013-11-12 15:11:28 -08002923 pr_err("Module len %lu truncated\n", info->len);
Rusty Russell8b5f61a2010-08-05 12:59:06 -06002924 return -ENOEXEC;
2925 }
2926
2927 /* Mark all sections sh_addr with their address in the
2928 temporary image. */
2929 shdr->sh_addr = (size_t)info->hdr + shdr->sh_offset;
2930
2931#ifndef CONFIG_MODULE_UNLOAD
2932 /* Don't load .exit sections */
Matthias Schiffer38b37d62019-06-07 12:49:11 +02002933 if (module_exit_section(info->secstrings+shdr->sh_name))
Rusty Russell8b5f61a2010-08-05 12:59:06 -06002934 shdr->sh_flags &= ~(unsigned long)SHF_ALLOC;
2935#endif
Rusty Russell8b5f61a2010-08-05 12:59:06 -06002936 }
Rusty Russelld6df72a2010-08-05 12:59:07 -06002937
2938 /* Track but don't keep modinfo and version sections. */
Rusty Russelld6df72a2010-08-05 12:59:07 -06002939 info->sechdrs[info->index.vers].sh_flags &= ~(unsigned long)SHF_ALLOC;
Kees Cook3e2e8572017-04-21 15:35:27 -07002940 info->sechdrs[info->index.info].sh_flags &= ~(unsigned long)SHF_ALLOC;
2941
Rusty Russell8b5f61a2010-08-05 12:59:06 -06002942 return 0;
2943}
2944
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002945/*
2946 * Set up our basic convenience variables (pointers to section headers,
2947 * search for module section index etc), and do some basic section
2948 * verification.
2949 *
Jessica Yu81a0abd2018-06-22 13:59:29 +02002950 * Set info->mod to the temporary copy of the module in info->hdr. The final one
2951 * will be allocated in move_module().
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002952 */
Jessica Yu81a0abd2018-06-22 13:59:29 +02002953static int setup_load_info(struct load_info *info, int flags)
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002954{
2955 unsigned int i;
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002956
2957 /* Set up the convenience variables */
2958 info->sechdrs = (void *)info->hdr + info->hdr->e_shoff;
Rusty Russell8b5f61a2010-08-05 12:59:06 -06002959 info->secstrings = (void *)info->hdr
2960 + info->sechdrs[info->hdr->e_shstrndx].sh_offset;
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002961
Jessica Yu5fdc7db2018-06-22 14:00:01 +02002962 /* Try to find a name early so we can log errors with a module name */
2963 info->index.info = find_sec(info, ".modinfo");
2964 if (!info->index.info)
2965 info->name = "(missing .modinfo section)";
2966 else
2967 info->name = get_modinfo(info, "name");
Rusty Russell8b5f61a2010-08-05 12:59:06 -06002968
2969 /* Find internal symbols and strings. */
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002970 for (i = 1; i < info->hdr->e_shnum; i++) {
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002971 if (info->sechdrs[i].sh_type == SHT_SYMTAB) {
2972 info->index.sym = i;
2973 info->index.str = info->sechdrs[i].sh_link;
Rusty Russell8b5f61a2010-08-05 12:59:06 -06002974 info->strtab = (char *)info->hdr
2975 + info->sechdrs[info->index.str].sh_offset;
2976 break;
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002977 }
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002978 }
2979
Jessica Yu5fdc7db2018-06-22 14:00:01 +02002980 if (info->index.sym == 0) {
2981 pr_warn("%s: module has no symbols (stripped?)\n", info->name);
2982 return -ENOEXEC;
2983 }
2984
Rusty Russell49668682010-08-05 12:59:10 -06002985 info->index.mod = find_sec(info, ".gnu.linkonce.this_module");
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002986 if (!info->index.mod) {
Kees Cook3e2e8572017-04-21 15:35:27 -07002987 pr_warn("%s: No module found in object\n",
2988 info->name ?: "(missing .modinfo name field)");
Jessica Yu81a0abd2018-06-22 13:59:29 +02002989 return -ENOEXEC;
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002990 }
2991 /* This is temporary: point mod into copy of data. */
Jessica Yu5fdc7db2018-06-22 14:00:01 +02002992 info->mod = (void *)info->hdr + info->sechdrs[info->index.mod].sh_offset;
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002993
Kees Cook3e2e8572017-04-21 15:35:27 -07002994 /*
Jessica Yu5fdc7db2018-06-22 14:00:01 +02002995 * If we didn't load the .modinfo 'name' field earlier, fall back to
Kees Cook3e2e8572017-04-21 15:35:27 -07002996 * on-disk struct mod 'name' field.
2997 */
2998 if (!info->name)
Jessica Yu81a0abd2018-06-22 13:59:29 +02002999 info->name = info->mod->name;
Kees Cook3e2e8572017-04-21 15:35:27 -07003000
Jessica Yu5fdc7db2018-06-22 14:00:01 +02003001 if (flags & MODULE_INIT_IGNORE_MODVERSIONS)
3002 info->index.vers = 0; /* Pretend no __versions section! */
3003 else
3004 info->index.vers = find_sec(info, "__versions");
Linus Torvalds3264d3f2010-06-02 11:01:06 -07003005
Rusty Russell49668682010-08-05 12:59:10 -06003006 info->index.pcpu = find_pcpusec(info);
Linus Torvalds3264d3f2010-06-02 11:01:06 -07003007
Jessica Yu81a0abd2018-06-22 13:59:29 +02003008 return 0;
Linus Torvalds3264d3f2010-06-02 11:01:06 -07003009}
3010
Rusty Russell2f3238a2012-10-22 18:09:41 +10303011static int check_modinfo(struct module *mod, struct load_info *info, int flags)
Rusty Russell40dd2562010-08-05 12:59:03 -06003012{
Rusty Russell49668682010-08-05 12:59:10 -06003013 const char *modmagic = get_modinfo(info, "vermagic");
Rusty Russell40dd2562010-08-05 12:59:03 -06003014 int err;
3015
Rusty Russell2f3238a2012-10-22 18:09:41 +10303016 if (flags & MODULE_INIT_IGNORE_VERMAGIC)
3017 modmagic = NULL;
3018
Rusty Russell40dd2562010-08-05 12:59:03 -06003019 /* This is allowed: modprobe --force will invalidate it. */
3020 if (!modmagic) {
3021 err = try_to_force_load(mod, "bad vermagic");
3022 if (err)
3023 return err;
Rusty Russell49668682010-08-05 12:59:10 -06003024 } else if (!same_magic(modmagic, vermagic, info->index.vers)) {
Andrew Mortonbddb12b2013-11-12 15:11:28 -08003025 pr_err("%s: version magic '%s' should be '%s'\n",
Kees Cook3e2e8572017-04-21 15:35:27 -07003026 info->name, modmagic, vermagic);
Rusty Russell40dd2562010-08-05 12:59:03 -06003027 return -ENOEXEC;
3028 }
3029
Libor Pechacek3205c362016-04-13 11:06:12 +09303030 if (!get_modinfo(info, "intree")) {
3031 if (!test_taint(TAINT_OOT_MODULE))
3032 pr_warn("%s: loading out-of-tree module taints kernel.\n",
3033 mod->name);
Rusty Russell373d4d02013-01-21 17:17:39 +10303034 add_taint_module(mod, TAINT_OOT_MODULE, LOCKDEP_STILL_OK);
Libor Pechacek3205c362016-04-13 11:06:12 +09303035 }
Ben Hutchings2449b8b2011-10-24 15:12:28 +02003036
Andi Kleencaf75012018-01-25 15:50:28 -08003037 check_modinfo_retpoline(mod, info);
3038
Rusty Russell49668682010-08-05 12:59:10 -06003039 if (get_modinfo(info, "staging")) {
Rusty Russell373d4d02013-01-21 17:17:39 +10303040 add_taint_module(mod, TAINT_CRAP, LOCKDEP_STILL_OK);
Andrew Mortonbddb12b2013-11-12 15:11:28 -08003041 pr_warn("%s: module is from the staging directory, the quality "
3042 "is unknown, you have been warned.\n", mod->name);
Rusty Russell40dd2562010-08-05 12:59:03 -06003043 }
Rusty Russell22e268e2010-08-05 12:59:05 -06003044
Josh Poimboeuf2992ef22016-08-25 10:04:45 -05003045 err = check_modinfo_livepatch(mod, info);
Jessica Yu1ce15ef2016-03-22 20:03:16 -04003046 if (err)
3047 return err;
3048
Rusty Russell22e268e2010-08-05 12:59:05 -06003049 /* Set up license info based on the info section */
Rusty Russell49668682010-08-05 12:59:10 -06003050 set_license(mod, get_modinfo(info, "license"));
Rusty Russell22e268e2010-08-05 12:59:05 -06003051
Rusty Russell40dd2562010-08-05 12:59:03 -06003052 return 0;
3053}
3054
Frantisek Hrbataeb3057d2013-10-14 18:08:46 +10303055static int find_module_sections(struct module *mod, struct load_info *info)
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06003056{
Rusty Russell49668682010-08-05 12:59:10 -06003057 mod->kp = section_objs(info, "__param",
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06003058 sizeof(*mod->kp), &mod->num_kp);
Rusty Russell49668682010-08-05 12:59:10 -06003059 mod->syms = section_objs(info, "__ksymtab",
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06003060 sizeof(*mod->syms), &mod->num_syms);
Rusty Russell49668682010-08-05 12:59:10 -06003061 mod->crcs = section_addr(info, "__kcrctab");
3062 mod->gpl_syms = section_objs(info, "__ksymtab_gpl",
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06003063 sizeof(*mod->gpl_syms),
3064 &mod->num_gpl_syms);
Rusty Russell49668682010-08-05 12:59:10 -06003065 mod->gpl_crcs = section_addr(info, "__kcrctab_gpl");
3066 mod->gpl_future_syms = section_objs(info,
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06003067 "__ksymtab_gpl_future",
3068 sizeof(*mod->gpl_future_syms),
3069 &mod->num_gpl_future_syms);
Rusty Russell49668682010-08-05 12:59:10 -06003070 mod->gpl_future_crcs = section_addr(info, "__kcrctab_gpl_future");
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06003071
3072#ifdef CONFIG_UNUSED_SYMBOLS
Rusty Russell49668682010-08-05 12:59:10 -06003073 mod->unused_syms = section_objs(info, "__ksymtab_unused",
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06003074 sizeof(*mod->unused_syms),
3075 &mod->num_unused_syms);
Rusty Russell49668682010-08-05 12:59:10 -06003076 mod->unused_crcs = section_addr(info, "__kcrctab_unused");
3077 mod->unused_gpl_syms = section_objs(info, "__ksymtab_unused_gpl",
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06003078 sizeof(*mod->unused_gpl_syms),
3079 &mod->num_unused_gpl_syms);
Rusty Russell49668682010-08-05 12:59:10 -06003080 mod->unused_gpl_crcs = section_addr(info, "__kcrctab_unused_gpl");
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06003081#endif
3082#ifdef CONFIG_CONSTRUCTORS
Rusty Russell49668682010-08-05 12:59:10 -06003083 mod->ctors = section_objs(info, ".ctors",
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06003084 sizeof(*mod->ctors), &mod->num_ctors);
Frantisek Hrbataeb3057d2013-10-14 18:08:46 +10303085 if (!mod->ctors)
3086 mod->ctors = section_objs(info, ".init_array",
3087 sizeof(*mod->ctors), &mod->num_ctors);
3088 else if (find_sec(info, ".init_array")) {
3089 /*
3090 * This shouldn't happen with same compiler and binutils
3091 * building all parts of the module.
3092 */
Ionut Alexa6da0b562014-11-10 09:31:29 +10303093 pr_warn("%s: has both .ctors and .init_array.\n",
Frantisek Hrbataeb3057d2013-10-14 18:08:46 +10303094 mod->name);
3095 return -EINVAL;
3096 }
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06003097#endif
3098
3099#ifdef CONFIG_TRACEPOINTS
Mathieu Desnoyers65498642011-01-26 17:26:22 -05003100 mod->tracepoints_ptrs = section_objs(info, "__tracepoints_ptrs",
3101 sizeof(*mod->tracepoints_ptrs),
3102 &mod->num_tracepoints);
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06003103#endif
Paul E. McKenneyfe15b502019-04-05 16:15:00 -07003104#ifdef CONFIG_TREE_SRCU
3105 mod->srcu_struct_ptrs = section_objs(info, "___srcu_struct_ptrs",
3106 sizeof(*mod->srcu_struct_ptrs),
3107 &mod->num_srcu_structs);
3108#endif
Matt Mullinsa38d1102018-12-12 16:42:37 -08003109#ifdef CONFIG_BPF_EVENTS
3110 mod->bpf_raw_events = section_objs(info, "__bpf_raw_tp_map",
3111 sizeof(*mod->bpf_raw_events),
3112 &mod->num_bpf_raw_events);
3113#endif
Masahiro Yamadae9666d12018-12-31 00:14:15 +09003114#ifdef CONFIG_JUMP_LABEL
Jason Baronbf5438fc2010-09-17 11:09:00 -04003115 mod->jump_entries = section_objs(info, "__jump_table",
3116 sizeof(*mod->jump_entries),
3117 &mod->num_jump_entries);
3118#endif
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06003119#ifdef CONFIG_EVENT_TRACING
Rusty Russell49668682010-08-05 12:59:10 -06003120 mod->trace_events = section_objs(info, "_ftrace_events",
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06003121 sizeof(*mod->trace_events),
3122 &mod->num_trace_events);
Jeremy Linton99be6472017-05-31 16:56:44 -05003123 mod->trace_evals = section_objs(info, "_ftrace_eval_map",
3124 sizeof(*mod->trace_evals),
3125 &mod->num_trace_evals);
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06003126#endif
Steven Rostedt13b9b6e2010-11-10 22:19:24 -05003127#ifdef CONFIG_TRACING
3128 mod->trace_bprintk_fmt_start = section_objs(info, "__trace_printk_fmt",
3129 sizeof(*mod->trace_bprintk_fmt_start),
3130 &mod->num_trace_bprintk_fmt);
Steven Rostedt13b9b6e2010-11-10 22:19:24 -05003131#endif
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06003132#ifdef CONFIG_FTRACE_MCOUNT_RECORD
3133 /* sechdrs[0].sh_size is always zero */
Rusty Russell49668682010-08-05 12:59:10 -06003134 mod->ftrace_callsites = section_objs(info, "__mcount_loc",
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06003135 sizeof(*mod->ftrace_callsites),
3136 &mod->num_ftrace_callsites);
3137#endif
Masami Hiramatsu540adea2018-01-13 02:55:03 +09003138#ifdef CONFIG_FUNCTION_ERROR_INJECTION
3139 mod->ei_funcs = section_objs(info, "_error_injection_whitelist",
3140 sizeof(*mod->ei_funcs),
3141 &mod->num_ei_funcs);
Josef Bacik92ace992017-12-11 11:36:46 -05003142#endif
Rusty Russell811d66a2010-08-05 12:59:12 -06003143 mod->extable = section_objs(info, "__ex_table",
3144 sizeof(*mod->extable), &mod->num_exentries);
3145
Rusty Russell49668682010-08-05 12:59:10 -06003146 if (section_addr(info, "__obsparm"))
Andrew Mortonbddb12b2013-11-12 15:11:28 -08003147 pr_warn("%s: Ignoring obsolete parameters\n", mod->name);
Rusty Russell811d66a2010-08-05 12:59:12 -06003148
3149 info->debug = section_objs(info, "__verbose",
3150 sizeof(*info->debug), &info->num_debug);
Frantisek Hrbataeb3057d2013-10-14 18:08:46 +10303151
3152 return 0;
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06003153}
3154
Rusty Russell49668682010-08-05 12:59:10 -06003155static int move_module(struct module *mod, struct load_info *info)
Linus Torvalds65b8a9b2010-08-05 12:59:02 -06003156{
3157 int i;
3158 void *ptr;
3159
3160 /* Do the allocs. */
Rusty Russell7523e4d2015-11-26 09:44:08 +10303161 ptr = module_alloc(mod->core_layout.size);
Linus Torvalds65b8a9b2010-08-05 12:59:02 -06003162 /*
3163 * The pointer to this block is stored in the module structure
3164 * which is inside the block. Just mark it as not being a
3165 * leak.
3166 */
3167 kmemleak_not_leak(ptr);
3168 if (!ptr)
Rusty Russelld9131882010-08-05 12:59:08 -06003169 return -ENOMEM;
Linus Torvalds65b8a9b2010-08-05 12:59:02 -06003170
Rusty Russell7523e4d2015-11-26 09:44:08 +10303171 memset(ptr, 0, mod->core_layout.size);
3172 mod->core_layout.base = ptr;
Linus Torvalds65b8a9b2010-08-05 12:59:02 -06003173
Rusty Russell7523e4d2015-11-26 09:44:08 +10303174 if (mod->init_layout.size) {
3175 ptr = module_alloc(mod->init_layout.size);
Rusty Russell82fab442012-12-11 09:38:33 +10303176 /*
3177 * The pointer to this block is stored in the module structure
3178 * which is inside the block. This block doesn't need to be
3179 * scanned as it contains data and code that will be freed
3180 * after the module is initialized.
3181 */
3182 kmemleak_ignore(ptr);
3183 if (!ptr) {
Rusty Russell7523e4d2015-11-26 09:44:08 +10303184 module_memfree(mod->core_layout.base);
Rusty Russell82fab442012-12-11 09:38:33 +10303185 return -ENOMEM;
3186 }
Rusty Russell7523e4d2015-11-26 09:44:08 +10303187 memset(ptr, 0, mod->init_layout.size);
3188 mod->init_layout.base = ptr;
Rusty Russell82fab442012-12-11 09:38:33 +10303189 } else
Rusty Russell7523e4d2015-11-26 09:44:08 +10303190 mod->init_layout.base = NULL;
Linus Torvalds65b8a9b2010-08-05 12:59:02 -06003191
3192 /* Transfer each section which specifies SHF_ALLOC */
Jim Cromie5e124162011-12-06 12:11:31 -07003193 pr_debug("final section addresses:\n");
Rusty Russell49668682010-08-05 12:59:10 -06003194 for (i = 0; i < info->hdr->e_shnum; i++) {
Linus Torvalds65b8a9b2010-08-05 12:59:02 -06003195 void *dest;
Rusty Russell49668682010-08-05 12:59:10 -06003196 Elf_Shdr *shdr = &info->sechdrs[i];
Linus Torvalds65b8a9b2010-08-05 12:59:02 -06003197
Rusty Russell49668682010-08-05 12:59:10 -06003198 if (!(shdr->sh_flags & SHF_ALLOC))
Linus Torvalds65b8a9b2010-08-05 12:59:02 -06003199 continue;
3200
Rusty Russell49668682010-08-05 12:59:10 -06003201 if (shdr->sh_entsize & INIT_OFFSET_MASK)
Rusty Russell7523e4d2015-11-26 09:44:08 +10303202 dest = mod->init_layout.base
Rusty Russell49668682010-08-05 12:59:10 -06003203 + (shdr->sh_entsize & ~INIT_OFFSET_MASK);
Linus Torvalds65b8a9b2010-08-05 12:59:02 -06003204 else
Rusty Russell7523e4d2015-11-26 09:44:08 +10303205 dest = mod->core_layout.base + shdr->sh_entsize;
Linus Torvalds65b8a9b2010-08-05 12:59:02 -06003206
Rusty Russell49668682010-08-05 12:59:10 -06003207 if (shdr->sh_type != SHT_NOBITS)
3208 memcpy(dest, (void *)shdr->sh_addr, shdr->sh_size);
Linus Torvalds65b8a9b2010-08-05 12:59:02 -06003209 /* Update sh_addr to point to copy in image. */
Rusty Russell49668682010-08-05 12:59:10 -06003210 shdr->sh_addr = (unsigned long)dest;
Jim Cromie5e124162011-12-06 12:11:31 -07003211 pr_debug("\t0x%lx %s\n",
3212 (long)shdr->sh_addr, info->secstrings + shdr->sh_name);
Linus Torvalds65b8a9b2010-08-05 12:59:02 -06003213 }
Rusty Russelld9131882010-08-05 12:59:08 -06003214
3215 return 0;
Linus Torvalds65b8a9b2010-08-05 12:59:02 -06003216}
3217
Rusty Russell49668682010-08-05 12:59:10 -06003218static int check_module_license_and_versions(struct module *mod)
Rusty Russell22e268e2010-08-05 12:59:05 -06003219{
Libor Pechacek3205c362016-04-13 11:06:12 +09303220 int prev_taint = test_taint(TAINT_PROPRIETARY_MODULE);
3221
Rusty Russell22e268e2010-08-05 12:59:05 -06003222 /*
3223 * ndiswrapper is under GPL by itself, but loads proprietary modules.
3224 * Don't use add_taint_module(), as it would prevent ndiswrapper from
3225 * using GPL-only symbols it needs.
3226 */
3227 if (strcmp(mod->name, "ndiswrapper") == 0)
Rusty Russell373d4d02013-01-21 17:17:39 +10303228 add_taint(TAINT_PROPRIETARY_MODULE, LOCKDEP_NOW_UNRELIABLE);
Rusty Russell22e268e2010-08-05 12:59:05 -06003229
3230 /* driverloader was caught wrongly pretending to be under GPL */
3231 if (strcmp(mod->name, "driverloader") == 0)
Rusty Russell373d4d02013-01-21 17:17:39 +10303232 add_taint_module(mod, TAINT_PROPRIETARY_MODULE,
3233 LOCKDEP_NOW_UNRELIABLE);
Rusty Russell22e268e2010-08-05 12:59:05 -06003234
Matthew Garrettc99af372012-06-22 13:49:31 -04003235 /* lve claims to be GPL but upstream won't provide source */
3236 if (strcmp(mod->name, "lve") == 0)
Rusty Russell373d4d02013-01-21 17:17:39 +10303237 add_taint_module(mod, TAINT_PROPRIETARY_MODULE,
3238 LOCKDEP_NOW_UNRELIABLE);
Matthew Garrettc99af372012-06-22 13:49:31 -04003239
Libor Pechacek3205c362016-04-13 11:06:12 +09303240 if (!prev_taint && test_taint(TAINT_PROPRIETARY_MODULE))
3241 pr_warn("%s: module license taints kernel.\n", mod->name);
3242
Rusty Russell22e268e2010-08-05 12:59:05 -06003243#ifdef CONFIG_MODVERSIONS
3244 if ((mod->num_syms && !mod->crcs)
3245 || (mod->num_gpl_syms && !mod->gpl_crcs)
3246 || (mod->num_gpl_future_syms && !mod->gpl_future_crcs)
3247#ifdef CONFIG_UNUSED_SYMBOLS
3248 || (mod->num_unused_syms && !mod->unused_crcs)
3249 || (mod->num_unused_gpl_syms && !mod->unused_gpl_crcs)
3250#endif
3251 ) {
3252 return try_to_force_load(mod,
3253 "no versions for exported symbols");
3254 }
3255#endif
3256 return 0;
3257}
3258
3259static void flush_module_icache(const struct module *mod)
3260{
3261 mm_segment_t old_fs;
3262
3263 /* flush the icache in correct context */
3264 old_fs = get_fs();
3265 set_fs(KERNEL_DS);
3266
3267 /*
3268 * Flush the instruction cache, since we've played with text.
3269 * Do it before processing of module parameters, so the module
3270 * can provide parameter accessor functions of its own.
3271 */
Rusty Russell7523e4d2015-11-26 09:44:08 +10303272 if (mod->init_layout.base)
3273 flush_icache_range((unsigned long)mod->init_layout.base,
3274 (unsigned long)mod->init_layout.base
3275 + mod->init_layout.size);
3276 flush_icache_range((unsigned long)mod->core_layout.base,
3277 (unsigned long)mod->core_layout.base + mod->core_layout.size);
Rusty Russell22e268e2010-08-05 12:59:05 -06003278
3279 set_fs(old_fs);
3280}
3281
Jonas Bonn74e08fc2011-06-30 21:22:11 +02003282int __weak module_frob_arch_sections(Elf_Ehdr *hdr,
3283 Elf_Shdr *sechdrs,
3284 char *secstrings,
3285 struct module *mod)
3286{
3287 return 0;
3288}
3289
Prarit Bhargavabe7de5f2016-07-21 15:37:56 +09303290/* module_blacklist is a comma-separated list of module names */
3291static char *module_blacklist;
Luis R. Rodriguez96b5b192017-06-28 18:32:31 -07003292static bool blacklisted(const char *module_name)
Prarit Bhargavabe7de5f2016-07-21 15:37:56 +09303293{
3294 const char *p;
3295 size_t len;
3296
3297 if (!module_blacklist)
3298 return false;
3299
3300 for (p = module_blacklist; *p; p += len) {
3301 len = strcspn(p, ",");
3302 if (strlen(module_name) == len && !memcmp(module_name, p, len))
3303 return true;
3304 if (p[len] == ',')
3305 len++;
3306 }
3307 return false;
3308}
3309core_param(module_blacklist, module_blacklist, charp, 0400);
3310
Rusty Russell2f3238a2012-10-22 18:09:41 +10303311static struct module *layout_and_allocate(struct load_info *info, int flags)
Rusty Russelld9131882010-08-05 12:59:08 -06003312{
Rusty Russelld9131882010-08-05 12:59:08 -06003313 struct module *mod;
Jessica Yu444d13f2016-07-27 12:06:21 +09303314 unsigned int ndx;
Rusty Russelld9131882010-08-05 12:59:08 -06003315 int err;
3316
Jessica Yu81a0abd2018-06-22 13:59:29 +02003317 err = check_modinfo(info->mod, info, flags);
Rusty Russelld9131882010-08-05 12:59:08 -06003318 if (err)
3319 return ERR_PTR(err);
3320
3321 /* Allow arches to frob section contents and sizes. */
Rusty Russell49668682010-08-05 12:59:10 -06003322 err = module_frob_arch_sections(info->hdr, info->sechdrs,
Jessica Yu81a0abd2018-06-22 13:59:29 +02003323 info->secstrings, info->mod);
Rusty Russelld9131882010-08-05 12:59:08 -06003324 if (err < 0)
Rusty Russell8d8022e2013-07-03 10:06:28 +09303325 return ERR_PTR(err);
Rusty Russelld9131882010-08-05 12:59:08 -06003326
Rusty Russell8d8022e2013-07-03 10:06:28 +09303327 /* We will do a special allocation for per-cpu sections later. */
3328 info->sechdrs[info->index.pcpu].sh_flags &= ~(unsigned long)SHF_ALLOC;
Rusty Russelld9131882010-08-05 12:59:08 -06003329
Jessica Yu444d13f2016-07-27 12:06:21 +09303330 /*
3331 * Mark ro_after_init section with SHF_RO_AFTER_INIT so that
3332 * layout_sections() can put it in the right place.
3333 * Note: ro_after_init sections also have SHF_{WRITE,ALLOC} set.
3334 */
3335 ndx = find_sec(info, ".data..ro_after_init");
3336 if (ndx)
3337 info->sechdrs[ndx].sh_flags |= SHF_RO_AFTER_INIT;
Ard Biesheuvele8722672018-09-18 23:51:43 -07003338 /*
3339 * Mark the __jump_table section as ro_after_init as well: these data
3340 * structures are never modified, with the exception of entries that
3341 * refer to code in the __init section, which are annotated as such
3342 * at module load time.
3343 */
3344 ndx = find_sec(info, "__jump_table");
3345 if (ndx)
3346 info->sechdrs[ndx].sh_flags |= SHF_RO_AFTER_INIT;
Jessica Yu444d13f2016-07-27 12:06:21 +09303347
Rusty Russelld9131882010-08-05 12:59:08 -06003348 /* Determine total sizes, and put offsets in sh_entsize. For now
3349 this is done generically; there doesn't appear to be any
3350 special cases for the architectures. */
Jessica Yu81a0abd2018-06-22 13:59:29 +02003351 layout_sections(info->mod, info);
3352 layout_symtab(info->mod, info);
Rusty Russelld9131882010-08-05 12:59:08 -06003353
3354 /* Allocate and move to the final place */
Jessica Yu81a0abd2018-06-22 13:59:29 +02003355 err = move_module(info->mod, info);
Rusty Russelld9131882010-08-05 12:59:08 -06003356 if (err)
Rusty Russell8d8022e2013-07-03 10:06:28 +09303357 return ERR_PTR(err);
Rusty Russelld9131882010-08-05 12:59:08 -06003358
3359 /* Module has been copied to its final place now: return it. */
3360 mod = (void *)info->sechdrs[info->index.mod].sh_addr;
Rusty Russell49668682010-08-05 12:59:10 -06003361 kmemleak_load_module(mod, info);
Rusty Russelld9131882010-08-05 12:59:08 -06003362 return mod;
Rusty Russelld9131882010-08-05 12:59:08 -06003363}
3364
3365/* mod is no longer valid after this! */
3366static void module_deallocate(struct module *mod, struct load_info *info)
3367{
Rusty Russelld9131882010-08-05 12:59:08 -06003368 percpu_modfree(mod);
Rusty Russelld453cde2015-01-20 09:07:04 +10303369 module_arch_freeing_init(mod);
Rusty Russell7523e4d2015-11-26 09:44:08 +10303370 module_memfree(mod->init_layout.base);
3371 module_memfree(mod->core_layout.base);
Rusty Russelld9131882010-08-05 12:59:08 -06003372}
3373
Jonas Bonn74e08fc2011-06-30 21:22:11 +02003374int __weak module_finalize(const Elf_Ehdr *hdr,
3375 const Elf_Shdr *sechdrs,
3376 struct module *me)
3377{
3378 return 0;
3379}
3380
Rusty Russell811d66a2010-08-05 12:59:12 -06003381static int post_relocation(struct module *mod, const struct load_info *info)
3382{
Rusty Russell51f3d0f2010-08-05 12:59:13 -06003383 /* Sort exception table now relocations are done. */
Rusty Russell811d66a2010-08-05 12:59:12 -06003384 sort_extable(mod->extable, mod->extable + mod->num_exentries);
3385
3386 /* Copy relocated percpu area over. */
3387 percpu_modcopy(mod, (void *)info->sechdrs[info->index.pcpu].sh_addr,
3388 info->sechdrs[info->index.pcpu].sh_size);
3389
Rusty Russell51f3d0f2010-08-05 12:59:13 -06003390 /* Setup kallsyms-specific fields. */
Rusty Russell811d66a2010-08-05 12:59:12 -06003391 add_kallsyms(mod, info);
3392
3393 /* Arch-specific module finalizing. */
3394 return module_finalize(info->hdr, info->sechdrs, mod);
3395}
3396
Rusty Russell9bb9c3b2012-09-28 14:31:03 +09303397/* Is this module of this name done loading? No locks held. */
3398static bool finished_loading(const char *name)
3399{
3400 struct module *mod;
3401 bool ret;
3402
Peter Zijlstra9cc019b2015-02-11 15:01:13 +10303403 /*
3404 * The module_mutex should not be a heavily contended lock;
3405 * if we get the occasional sleep here, we'll go an extra iteration
3406 * in the wait_event_interruptible(), which is harmless.
3407 */
3408 sched_annotate_sleep();
Rusty Russell9bb9c3b2012-09-28 14:31:03 +09303409 mutex_lock(&module_mutex);
Mathias Krause4f6de4d52013-07-02 15:35:11 +09303410 mod = find_module_all(name, strlen(name), true);
Prarit Bhargava6e6de3d2019-05-29 07:26:25 -04003411 ret = !mod || mod->state == MODULE_STATE_LIVE;
Rusty Russell9bb9c3b2012-09-28 14:31:03 +09303412 mutex_unlock(&module_mutex);
3413
3414 return ret;
3415}
3416
Peter Oberparleiterb99b87f2009-06-17 16:28:03 -07003417/* Call module constructors. */
3418static void do_mod_ctors(struct module *mod)
3419{
3420#ifdef CONFIG_CONSTRUCTORS
3421 unsigned long i;
3422
3423 for (i = 0; i < mod->num_ctors; i++)
3424 mod->ctors[i]();
3425#endif
3426}
3427
Rusty Russellc7496372015-01-20 09:07:05 +10303428/* For freeing module_init on success, in case kallsyms traversing */
3429struct mod_initfree {
Rick Edgecombe1a7b7d92019-04-25 17:11:37 -07003430 struct llist_node node;
Rusty Russellc7496372015-01-20 09:07:05 +10303431 void *module_init;
3432};
3433
Rick Edgecombe1a7b7d92019-04-25 17:11:37 -07003434static void do_free_init(struct work_struct *w)
Rusty Russellc7496372015-01-20 09:07:05 +10303435{
Rick Edgecombe1a7b7d92019-04-25 17:11:37 -07003436 struct llist_node *pos, *n, *list;
3437 struct mod_initfree *initfree;
3438
3439 list = llist_del_all(&init_free_list);
3440
3441 synchronize_rcu();
3442
3443 llist_for_each_safe(pos, n, list) {
3444 initfree = container_of(pos, struct mod_initfree, node);
3445 module_memfree(initfree->module_init);
3446 kfree(initfree);
3447 }
Rusty Russellc7496372015-01-20 09:07:05 +10303448}
3449
Rick Edgecombe1a7b7d92019-04-25 17:11:37 -07003450static int __init modules_wq_init(void)
3451{
3452 INIT_WORK(&init_free_wq, do_free_init);
3453 init_llist_head(&init_free_list);
3454 return 0;
3455}
3456module_init(modules_wq_init);
3457
Jan Kiszkabe02a182015-02-17 13:46:50 -08003458/*
3459 * This is where the real work happens.
3460 *
3461 * Keep it uninlined to provide a reliable breakpoint target, e.g. for the gdb
3462 * helper command 'lx-symbols'.
3463 */
3464static noinline int do_init_module(struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003465{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003466 int ret = 0;
Rusty Russellc7496372015-01-20 09:07:05 +10303467 struct mod_initfree *freeinit;
3468
3469 freeinit = kmalloc(sizeof(*freeinit), GFP_KERNEL);
3470 if (!freeinit) {
3471 ret = -ENOMEM;
3472 goto fail;
3473 }
Rusty Russell7523e4d2015-11-26 09:44:08 +10303474 freeinit->module_init = mod->init_layout.base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003475
Tejun Heo774a1222013-01-15 18:52:51 -08003476 /*
3477 * We want to find out whether @mod uses async during init. Clear
3478 * PF_USED_ASYNC. async_schedule*() will set it.
3479 */
3480 current->flags &= ~PF_USED_ASYNC;
3481
Peter Oberparleiterb99b87f2009-06-17 16:28:03 -07003482 do_mod_ctors(mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003483 /* Start the module */
3484 if (mod->init != NULL)
Arjan van de Ven59f94152008-07-30 12:49:02 -07003485 ret = do_one_initcall(mod->init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003486 if (ret < 0) {
Rusty Russellc7496372015-01-20 09:07:05 +10303487 goto fail_free_freeinit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003488 }
Alexey Dobriyane24e2e62008-03-10 11:43:53 -07003489 if (ret > 0) {
Andrew Mortonbddb12b2013-11-12 15:11:28 -08003490 pr_warn("%s: '%s'->init suspiciously returned %d, it should "
3491 "follow 0/-E convention\n"
3492 "%s: loading module anyway...\n",
3493 __func__, mod->name, ret, __func__);
Alexey Dobriyane24e2e62008-03-10 11:43:53 -07003494 dump_stack();
3495 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003496
Rusty Russell6f139092012-09-28 14:31:03 +09303497 /* Now it's a first class citizen! */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003498 mod->state = MODULE_STATE_LIVE;
Masami Hiramatsu0deddf42009-01-06 14:41:54 -08003499 blocking_notifier_call_chain(&module_notify_list,
3500 MODULE_STATE_LIVE, mod);
Rusty Russell6c5db222008-03-10 11:43:52 -07003501
Tejun Heo774a1222013-01-15 18:52:51 -08003502 /*
3503 * We need to finish all async code before the module init sequence
3504 * is done. This has potential to deadlock. For example, a newly
3505 * detected block device can trigger request_module() of the
3506 * default iosched from async probing task. Once userland helper
3507 * reaches here, async_synchronize_full() will wait on the async
3508 * task waiting on request_module() and deadlock.
3509 *
3510 * This deadlock is avoided by perfomring async_synchronize_full()
3511 * iff module init queued any async jobs. This isn't a full
3512 * solution as it will deadlock the same if module loading from
3513 * async jobs nests more than once; however, due to the various
3514 * constraints, this hack seems to be the best option for now.
3515 * Please refer to the following thread for details.
3516 *
3517 * http://thread.gmane.org/gmane.linux.kernel/1420814
3518 */
Luis R. Rodriguezf2411da2015-03-30 16:20:05 -07003519 if (!mod->async_probe_requested && (current->flags & PF_USED_ASYNC))
Tejun Heo774a1222013-01-15 18:52:51 -08003520 async_synchronize_full();
Linus Torvaldsd6de2c82009-04-10 12:17:41 -07003521
Steven Rostedt (VMware)aba4b5c2017-09-01 08:35:38 -04003522 ftrace_free_mem(mod, mod->init_layout.base, mod->init_layout.base +
Steven Rostedt (VMware)3e234282017-03-03 18:00:22 -05003523 mod->init_layout.size);
Rusty Russell6c5db222008-03-10 11:43:52 -07003524 mutex_lock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003525 /* Drop initial reference. */
3526 module_put(mod);
Rusty Russellad6561d2009-06-12 21:47:03 -06003527 trim_init_extable(mod);
Jan Beulich4a496222009-07-06 14:50:42 +01003528#ifdef CONFIG_KALLSYMS
Rusty Russell82440622016-02-03 16:55:26 +10303529 /* Switch to core kallsyms now init is done: kallsyms may be walking! */
3530 rcu_assign_pointer(mod->kallsyms, &mod->core_kallsyms);
Jan Beulich4a496222009-07-06 14:50:42 +01003531#endif
Jessica Yu444d13f2016-07-27 12:06:21 +09303532 module_enable_ro(mod, true);
Peter Zijlstra93c2e102015-05-27 11:09:37 +09303533 mod_tree_remove_init(mod);
Rusty Russelld453cde2015-01-20 09:07:04 +10303534 module_arch_freeing_init(mod);
Rusty Russell7523e4d2015-11-26 09:44:08 +10303535 mod->init_layout.base = NULL;
3536 mod->init_layout.size = 0;
3537 mod->init_layout.ro_size = 0;
Jessica Yu444d13f2016-07-27 12:06:21 +09303538 mod->init_layout.ro_after_init_size = 0;
Rusty Russell7523e4d2015-11-26 09:44:08 +10303539 mod->init_layout.text_size = 0;
Rusty Russellc7496372015-01-20 09:07:05 +10303540 /*
3541 * We want to free module_init, but be aware that kallsyms may be
Peter Zijlstra0be964b2015-05-27 11:09:35 +09303542 * walking this with preempt disabled. In all the failure paths, we
Paul E. McKenneycb2f5532018-11-06 19:17:01 -08003543 * call synchronize_rcu(), but we don't want to slow down the success
Rick Edgecombe1a7b7d92019-04-25 17:11:37 -07003544 * path. module_memfree() cannot be called in an interrupt, so do the
3545 * work and call synchronize_rcu() in a work queue.
3546 *
Jeffrey Hugoae646f02018-05-11 16:01:42 -07003547 * Note that module_alloc() on most architectures creates W+X page
3548 * mappings which won't be cleaned up until do_free_init() runs. Any
3549 * code such as mark_rodata_ro() which depends on those mappings to
3550 * be cleaned up needs to sync with the queued work - ie
Paul E. McKenneycb2f5532018-11-06 19:17:01 -08003551 * rcu_barrier()
Rusty Russellc7496372015-01-20 09:07:05 +10303552 */
Rick Edgecombe1a7b7d92019-04-25 17:11:37 -07003553 if (llist_add(&freeinit->node, &init_free_list))
3554 schedule_work(&init_free_wq);
3555
Ashutosh Naik6389a382006-03-23 03:00:46 -08003556 mutex_unlock(&module_mutex);
Rusty Russell6f139092012-09-28 14:31:03 +09303557 wake_up_all(&module_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003558
3559 return 0;
Rusty Russellc7496372015-01-20 09:07:05 +10303560
3561fail_free_freeinit:
3562 kfree(freeinit);
3563fail:
3564 /* Try to protect us from buggy refcounters. */
3565 mod->state = MODULE_STATE_GOING;
Paul E. McKenneycb2f5532018-11-06 19:17:01 -08003566 synchronize_rcu();
Rusty Russellc7496372015-01-20 09:07:05 +10303567 module_put(mod);
3568 blocking_notifier_call_chain(&module_notify_list,
3569 MODULE_STATE_GOING, mod);
Jessica Yu7e545d62016-03-16 20:55:39 -04003570 klp_module_going(mod);
Jessica Yu7dcd1822016-02-16 17:32:33 -05003571 ftrace_release_mod(mod);
Rusty Russellc7496372015-01-20 09:07:05 +10303572 free_module(mod);
3573 wake_up_all(&module_wq);
3574 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003575}
3576
Kees Cook34e11692012-10-16 07:31:07 +10303577static int may_init_module(void)
3578{
3579 if (!capable(CAP_SYS_MODULE) || modules_disabled)
3580 return -EPERM;
3581
3582 return 0;
3583}
3584
Rusty Russella3535c72013-01-21 17:18:59 +10303585/*
3586 * We try to place it in the list now to make sure it's unique before
3587 * we dedicate too many resources. In particular, temporary percpu
3588 * memory exhaustion.
3589 */
3590static int add_unformed_module(struct module *mod)
3591{
3592 int err;
3593 struct module *old;
3594
3595 mod->state = MODULE_STATE_UNFORMED;
3596
3597again:
3598 mutex_lock(&module_mutex);
Mathias Krause4f6de4d52013-07-02 15:35:11 +09303599 old = find_module_all(mod->name, strlen(mod->name), true);
3600 if (old != NULL) {
Prarit Bhargava6e6de3d2019-05-29 07:26:25 -04003601 if (old->state != MODULE_STATE_LIVE) {
Rusty Russella3535c72013-01-21 17:18:59 +10303602 /* Wait in case it fails to load. */
3603 mutex_unlock(&module_mutex);
Peter Zijlstra9cc019b2015-02-11 15:01:13 +10303604 err = wait_event_interruptible(module_wq,
3605 finished_loading(mod->name));
Rusty Russella3535c72013-01-21 17:18:59 +10303606 if (err)
3607 goto out_unlocked;
3608 goto again;
3609 }
3610 err = -EEXIST;
3611 goto out;
3612 }
Peter Zijlstra4f6665462015-05-27 11:09:38 +09303613 mod_update_bounds(mod);
Rusty Russella3535c72013-01-21 17:18:59 +10303614 list_add_rcu(&mod->list, &modules);
Peter Zijlstra93c2e102015-05-27 11:09:37 +09303615 mod_tree_insert(mod);
Rusty Russella3535c72013-01-21 17:18:59 +10303616 err = 0;
3617
3618out:
3619 mutex_unlock(&module_mutex);
3620out_unlocked:
3621 return err;
3622}
3623
3624static int complete_formation(struct module *mod, struct load_info *info)
3625{
3626 int err;
3627
3628 mutex_lock(&module_mutex);
3629
3630 /* Find duplicate symbols (must be called under lock). */
Jessica Yu2d25bc52018-11-19 17:43:58 +01003631 err = verify_exported_symbols(mod);
Rusty Russella3535c72013-01-21 17:18:59 +10303632 if (err < 0)
3633 goto out;
3634
3635 /* This relies on module_mutex for list integrity. */
3636 module_bug_finalize(info->hdr, info->sechdrs, mod);
3637
Jessica Yu444d13f2016-07-27 12:06:21 +09303638 module_enable_ro(mod, false);
Rusty Russell85c898d2015-11-26 09:45:08 +10303639 module_enable_nx(mod);
Yang Yingliang2eef1392019-06-20 10:18:14 +08003640 module_enable_x(mod);
Rusty Russell49822232014-05-14 10:54:19 +09303641
Rusty Russella3535c72013-01-21 17:18:59 +10303642 /* Mark state as coming so strong_try_module_get() ignores us,
3643 * but kallsyms etc. can see us. */
3644 mod->state = MODULE_STATE_COMING;
Rusty Russell49822232014-05-14 10:54:19 +09303645 mutex_unlock(&module_mutex);
3646
Rusty Russell49822232014-05-14 10:54:19 +09303647 return 0;
Rusty Russella3535c72013-01-21 17:18:59 +10303648
3649out:
3650 mutex_unlock(&module_mutex);
3651 return err;
3652}
3653
Jessica Yu4c973d12016-03-16 20:55:38 -04003654static int prepare_coming_module(struct module *mod)
3655{
Jessica Yu7e545d62016-03-16 20:55:39 -04003656 int err;
3657
Jessica Yu4c973d12016-03-16 20:55:38 -04003658 ftrace_module_enable(mod);
Jessica Yu7e545d62016-03-16 20:55:39 -04003659 err = klp_module_coming(mod);
3660 if (err)
3661 return err;
3662
Jessica Yu4c973d12016-03-16 20:55:38 -04003663 blocking_notifier_call_chain(&module_notify_list,
3664 MODULE_STATE_COMING, mod);
3665 return 0;
3666}
3667
Luis R. Rodriguezecc86172015-03-30 16:20:03 -07003668static int unknown_module_param_cb(char *param, char *val, const char *modname,
3669 void *arg)
Rusty Russell54041d82013-07-02 15:35:12 +09303670{
Luis R. Rodriguezf2411da2015-03-30 16:20:05 -07003671 struct module *mod = arg;
3672 int ret;
3673
3674 if (strcmp(param, "async_probe") == 0) {
3675 mod->async_probe_requested = true;
3676 return 0;
3677 }
3678
Ionut Alexa6da0b562014-11-10 09:31:29 +10303679 /* Check for magic 'dyndbg' arg */
Luis R. Rodriguezf2411da2015-03-30 16:20:05 -07003680 ret = ddebug_dyndbg_module_param_cb(param, val, modname);
Andrew Mortonbddb12b2013-11-12 15:11:28 -08003681 if (ret != 0)
3682 pr_warn("%s: unknown parameter '%s' ignored\n", modname, param);
Rusty Russell54041d82013-07-02 15:35:12 +09303683 return 0;
3684}
3685
Kees Cook34e11692012-10-16 07:31:07 +10303686/* Allocate and load the module: note that size of section 0 is always
3687 zero, and we rely on this for optional sections. */
Rusty Russell2f3238a2012-10-22 18:09:41 +10303688static int load_module(struct load_info *info, const char __user *uargs,
3689 int flags)
Kees Cook34e11692012-10-16 07:31:07 +10303690{
Rusty Russella3535c72013-01-21 17:18:59 +10303691 struct module *mod;
Jessica Yu5fdc7db2018-06-22 14:00:01 +02003692 long err = 0;
Rusty Russell51e158c2014-04-28 11:34:33 +09303693 char *after_dashes;
Kees Cook34e11692012-10-16 07:31:07 +10303694
Jessica Yu5fdc7db2018-06-22 14:00:01 +02003695 err = elf_header_check(info);
3696 if (err)
3697 goto free_copy;
3698
3699 err = setup_load_info(info, flags);
3700 if (err)
3701 goto free_copy;
3702
3703 if (blacklisted(info->name)) {
3704 err = -EPERM;
3705 goto free_copy;
3706 }
3707
Ben Hutchingsbca014c2016-04-28 09:24:01 +09303708 err = module_sig_check(info, flags);
Kees Cook34e11692012-10-16 07:31:07 +10303709 if (err)
3710 goto free_copy;
3711
Jessica Yu5fdc7db2018-06-22 14:00:01 +02003712 err = rewrite_section_headers(info, flags);
Kees Cook34e11692012-10-16 07:31:07 +10303713 if (err)
3714 goto free_copy;
3715
Jessica Yu5fdc7db2018-06-22 14:00:01 +02003716 /* Check module struct version now, before we try to use module. */
3717 if (!check_modstruct_version(info, info->mod)) {
3718 err = -ENOEXEC;
3719 goto free_copy;
3720 }
3721
Kees Cook34e11692012-10-16 07:31:07 +10303722 /* Figure out module layout, and allocate all the memory. */
Rusty Russell2f3238a2012-10-22 18:09:41 +10303723 mod = layout_and_allocate(info, flags);
Kees Cook34e11692012-10-16 07:31:07 +10303724 if (IS_ERR(mod)) {
3725 err = PTR_ERR(mod);
3726 goto free_copy;
3727 }
3728
Richard Guy Briggsca86cad2017-02-04 13:10:38 -05003729 audit_log_kern_module(mod->name);
3730
Rusty Russella3535c72013-01-21 17:18:59 +10303731 /* Reserve our place in the list. */
3732 err = add_unformed_module(mod);
3733 if (err)
Rusty Russell1fb93412013-01-12 13:27:34 +10303734 goto free_module;
Rusty Russell1fb93412013-01-12 13:27:34 +10303735
Kees Cook34e11692012-10-16 07:31:07 +10303736#ifdef CONFIG_MODULE_SIG
3737 mod->sig_ok = info->sig_ok;
Rusty Russell64748a22013-01-21 17:03:02 +10303738 if (!mod->sig_ok) {
Andrew Mortonbddb12b2013-11-12 15:11:28 -08003739 pr_notice_once("%s: module verification failed: signature "
Marcel Holtmannab92ebb2015-02-06 15:09:57 +10303740 "and/or required key missing - tainting "
Andrew Mortonbddb12b2013-11-12 15:11:28 -08003741 "kernel\n", mod->name);
Mathieu Desnoyers66cc69e2014-03-13 12:11:30 +10303742 add_taint_module(mod, TAINT_UNSIGNED_MODULE, LOCKDEP_STILL_OK);
Rusty Russell64748a22013-01-21 17:03:02 +10303743 }
Kees Cook34e11692012-10-16 07:31:07 +10303744#endif
3745
Rusty Russell8d8022e2013-07-03 10:06:28 +09303746 /* To avoid stressing percpu allocator, do this once we're unique. */
Rusty Russell9eb76d72013-07-03 10:06:29 +09303747 err = percpu_modalloc(mod, info);
Rusty Russell8d8022e2013-07-03 10:06:28 +09303748 if (err)
3749 goto unlink_mod;
3750
Kees Cook34e11692012-10-16 07:31:07 +10303751 /* Now module is in final location, initialize linked lists, etc. */
3752 err = module_unload_init(mod);
3753 if (err)
Rusty Russell1fb93412013-01-12 13:27:34 +10303754 goto unlink_mod;
Kees Cook34e11692012-10-16 07:31:07 +10303755
Rusty Russellcf2fde72015-06-26 06:44:38 +09303756 init_param_lock(mod);
Dan Streetmanb51d23e2015-06-17 06:18:52 +09303757
Kees Cook34e11692012-10-16 07:31:07 +10303758 /* Now we've got everything in the final locations, we can
3759 * find optional sections. */
Frantisek Hrbataeb3057d2013-10-14 18:08:46 +10303760 err = find_module_sections(mod, info);
3761 if (err)
3762 goto free_unload;
Kees Cook34e11692012-10-16 07:31:07 +10303763
3764 err = check_module_license_and_versions(mod);
3765 if (err)
3766 goto free_unload;
3767
3768 /* Set up MODINFO_ATTR fields */
3769 setup_modinfo(mod, info);
3770
3771 /* Fix up syms, so that st_value is a pointer to location. */
3772 err = simplify_symbols(mod, info);
3773 if (err < 0)
3774 goto free_modinfo;
3775
3776 err = apply_relocations(mod, info);
3777 if (err < 0)
3778 goto free_modinfo;
3779
3780 err = post_relocation(mod, info);
3781 if (err < 0)
3782 goto free_modinfo;
3783
3784 flush_module_icache(mod);
3785
3786 /* Now copy in args */
3787 mod->args = strndup_user(uargs, ~0UL >> 1);
3788 if (IS_ERR(mod->args)) {
3789 err = PTR_ERR(mod->args);
3790 goto free_arch_cleanup;
3791 }
3792
Zhou Chengming52796312017-07-07 11:15:58 +08003793 dynamic_debug_setup(mod, info->debug, info->num_debug);
Kees Cook34e11692012-10-16 07:31:07 +10303794
Steven Rostedt (Red Hat)a949ae52014-04-24 10:40:12 -04003795 /* Ftrace init must be called in the MODULE_STATE_UNFORMED state */
3796 ftrace_module_init(mod);
3797
Rusty Russella3535c72013-01-21 17:18:59 +10303798 /* Finally it's fully formed, ready to start executing. */
3799 err = complete_formation(mod, info);
3800 if (err)
Rusty Russell1fb93412013-01-12 13:27:34 +10303801 goto ddebug_cleanup;
Kees Cook34e11692012-10-16 07:31:07 +10303802
Jessica Yu4c973d12016-03-16 20:55:38 -04003803 err = prepare_coming_module(mod);
3804 if (err)
3805 goto bug_cleanup;
3806
Kees Cook34e11692012-10-16 07:31:07 +10303807 /* Module is ready to execute: parsing args may do that. */
Rusty Russell51e158c2014-04-28 11:34:33 +09303808 after_dashes = parse_args(mod->name, mod->args, mod->kp, mod->num_kp,
Luis R. Rodriguez4355efb2016-02-03 16:55:26 +10303809 -32768, 32767, mod,
Luis R. Rodriguezecc86172015-03-30 16:20:03 -07003810 unknown_module_param_cb);
Rusty Russell51e158c2014-04-28 11:34:33 +09303811 if (IS_ERR(after_dashes)) {
3812 err = PTR_ERR(after_dashes);
Jessica Yu4c973d12016-03-16 20:55:38 -04003813 goto coming_cleanup;
Rusty Russell51e158c2014-04-28 11:34:33 +09303814 } else if (after_dashes) {
3815 pr_warn("%s: parameters '%s' after `--' ignored\n",
3816 mod->name, after_dashes);
3817 }
Kees Cook34e11692012-10-16 07:31:07 +10303818
Richard Guy Briggsca86cad2017-02-04 13:10:38 -05003819 /* Link in to sysfs. */
Kees Cook34e11692012-10-16 07:31:07 +10303820 err = mod_sysfs_setup(mod, info, mod->kp, mod->num_kp);
3821 if (err < 0)
Jessica Yu4c973d12016-03-16 20:55:38 -04003822 goto coming_cleanup;
Kees Cook34e11692012-10-16 07:31:07 +10303823
Jessica Yu1ce15ef2016-03-22 20:03:16 -04003824 if (is_livepatch_module(mod)) {
3825 err = copy_module_elf(mod, info);
3826 if (err < 0)
3827 goto sysfs_cleanup;
3828 }
3829
Kees Cook34e11692012-10-16 07:31:07 +10303830 /* Get rid of temporary copy. */
3831 free_copy(info);
3832
3833 /* Done! */
3834 trace_module_load(mod);
3835
3836 return do_init_module(mod);
3837
Jessica Yu1ce15ef2016-03-22 20:03:16 -04003838 sysfs_cleanup:
3839 mod_sysfs_teardown(mod);
Jessica Yu4c973d12016-03-16 20:55:38 -04003840 coming_cleanup:
Aaron Tomlin885a78d2016-10-20 17:18:12 +01003841 mod->state = MODULE_STATE_GOING;
Luis R. Rodrigueza5544882017-02-10 14:06:22 -08003842 destroy_params(mod->kp, mod->num_kp);
Jessica Yu4c973d12016-03-16 20:55:38 -04003843 blocking_notifier_call_chain(&module_notify_list,
3844 MODULE_STATE_GOING, mod);
Jessica Yu7e545d62016-03-16 20:55:39 -04003845 klp_module_going(mod);
Rusty Russell1fb93412013-01-12 13:27:34 +10303846 bug_cleanup:
3847 /* module_bug_cleanup needs module_mutex protection */
Kees Cook34e11692012-10-16 07:31:07 +10303848 mutex_lock(&module_mutex);
Kees Cook34e11692012-10-16 07:31:07 +10303849 module_bug_cleanup(mod);
Linus Torvaldsee61abb2013-01-20 20:22:58 -08003850 mutex_unlock(&module_mutex);
Andy Lutomirskiff7e0052014-08-16 04:13:37 +09303851
Rusty Russella3535c72013-01-21 17:18:59 +10303852 ddebug_cleanup:
Namit Gupta1323eac2018-01-08 10:41:21 +05303853 ftrace_release_mod(mod);
Zhou Chengming52796312017-07-07 11:15:58 +08003854 dynamic_debug_remove(mod, info->debug);
Paul E. McKenneycb2f5532018-11-06 19:17:01 -08003855 synchronize_rcu();
Kees Cook34e11692012-10-16 07:31:07 +10303856 kfree(mod->args);
3857 free_arch_cleanup:
3858 module_arch_cleanup(mod);
3859 free_modinfo:
3860 free_modinfo(mod);
3861 free_unload:
3862 module_unload_free(mod);
Rusty Russell1fb93412013-01-12 13:27:34 +10303863 unlink_mod:
3864 mutex_lock(&module_mutex);
3865 /* Unlink carefully: kallsyms could be walking list. */
3866 list_del_rcu(&mod->list);
Peter Zijlstra758556b2015-07-09 06:48:06 +09303867 mod_tree_remove(mod);
Rusty Russell1fb93412013-01-12 13:27:34 +10303868 wake_up_all(&module_wq);
Peter Zijlstra0be964b2015-05-27 11:09:35 +09303869 /* Wait for RCU-sched synchronizing before releasing mod->list. */
Paul E. McKenneycb2f5532018-11-06 19:17:01 -08003870 synchronize_rcu();
Rusty Russell1fb93412013-01-12 13:27:34 +10303871 mutex_unlock(&module_mutex);
Kees Cook34e11692012-10-16 07:31:07 +10303872 free_module:
Peter Zijlstra35a93932015-02-26 16:23:11 +01003873 /* Free lock-classes; relies on the preceding sync_rcu() */
Rusty Russell7523e4d2015-11-26 09:44:08 +10303874 lockdep_free_key_range(mod->core_layout.base, mod->core_layout.size);
Peter Zijlstra35a93932015-02-26 16:23:11 +01003875
Kees Cook34e11692012-10-16 07:31:07 +10303876 module_deallocate(mod, info);
3877 free_copy:
3878 free_copy(info);
3879 return err;
3880}
3881
3882SYSCALL_DEFINE3(init_module, void __user *, umod,
3883 unsigned long, len, const char __user *, uargs)
3884{
3885 int err;
3886 struct load_info info = { };
3887
3888 err = may_init_module();
3889 if (err)
3890 return err;
3891
3892 pr_debug("init_module: umod=%p, len=%lu, uargs=%p\n",
3893 umod, len, uargs);
3894
3895 err = copy_module_from_user(umod, len, &info);
3896 if (err)
3897 return err;
3898
Rusty Russell2f3238a2012-10-22 18:09:41 +10303899 return load_module(&info, uargs, 0);
Kees Cook34e11692012-10-16 07:31:07 +10303900}
3901
Rusty Russell2f3238a2012-10-22 18:09:41 +10303902SYSCALL_DEFINE3(finit_module, int, fd, const char __user *, uargs, int, flags)
Kees Cook34e11692012-10-16 07:31:07 +10303903{
Kees Cook34e11692012-10-16 07:31:07 +10303904 struct load_info info = { };
Mimi Zohara1db7422015-12-30 07:35:30 -05003905 loff_t size;
3906 void *hdr;
3907 int err;
Kees Cook34e11692012-10-16 07:31:07 +10303908
3909 err = may_init_module();
3910 if (err)
3911 return err;
3912
Rusty Russell2f3238a2012-10-22 18:09:41 +10303913 pr_debug("finit_module: fd=%d, uargs=%p, flags=%i\n", fd, uargs, flags);
3914
3915 if (flags & ~(MODULE_INIT_IGNORE_MODVERSIONS
3916 |MODULE_INIT_IGNORE_VERMAGIC))
3917 return -EINVAL;
Kees Cook34e11692012-10-16 07:31:07 +10303918
Mimi Zohara1db7422015-12-30 07:35:30 -05003919 err = kernel_read_file_from_fd(fd, &hdr, &size, INT_MAX,
3920 READING_MODULE);
Kees Cook34e11692012-10-16 07:31:07 +10303921 if (err)
3922 return err;
Mimi Zohara1db7422015-12-30 07:35:30 -05003923 info.hdr = hdr;
3924 info.len = size;
Kees Cook34e11692012-10-16 07:31:07 +10303925
Rusty Russell2f3238a2012-10-22 18:09:41 +10303926 return load_module(&info, uargs, flags);
Kees Cook34e11692012-10-16 07:31:07 +10303927}
3928
Linus Torvalds1da177e2005-04-16 15:20:36 -07003929static inline int within(unsigned long addr, void *start, unsigned long size)
3930{
3931 return ((void *)addr >= start && (void *)addr < start + size);
3932}
3933
3934#ifdef CONFIG_KALLSYMS
3935/*
3936 * This ignores the intensely annoying "mapping symbols" found
3937 * in ARM ELF files: $a, $t and $d.
3938 */
3939static inline int is_arm_mapping_symbol(const char *str)
3940{
Russell King2e3a10a2014-07-27 07:29:01 +09303941 if (str[0] == '.' && str[1] == 'L')
3942 return true;
Kyle McMartin6c34f1f2014-09-16 22:37:18 +01003943 return str[0] == '$' && strchr("axtd", str[1])
Linus Torvalds1da177e2005-04-16 15:20:36 -07003944 && (str[2] == '\0' || str[2] == '.');
3945}
3946
Jessica Yu2d25bc52018-11-19 17:43:58 +01003947static const char *kallsyms_symbol_name(struct mod_kallsyms *kallsyms, unsigned int symnum)
Rusty Russell2e7bac52016-02-03 16:55:26 +10303948{
Rusty Russell82440622016-02-03 16:55:26 +10303949 return kallsyms->strtab + kallsyms->symtab[symnum].st_name;
Rusty Russell2e7bac52016-02-03 16:55:26 +10303950}
3951
Jessica Yu2d25bc52018-11-19 17:43:58 +01003952/*
3953 * Given a module and address, find the corresponding symbol and return its name
3954 * while providing its size and offset if needed.
3955 */
3956static const char *find_kallsyms_symbol(struct module *mod,
3957 unsigned long addr,
3958 unsigned long *size,
3959 unsigned long *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003960{
3961 unsigned int i, best = 0;
Vincent Whitchurch93d77e72018-12-14 17:05:55 +01003962 unsigned long nextval, bestval;
Rusty Russell82440622016-02-03 16:55:26 +10303963 struct mod_kallsyms *kallsyms = rcu_dereference_sched(mod->kallsyms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003964
3965 /* At worse, next value is at end of module */
Masami Hiramatsua06f6212009-01-06 14:41:49 -08003966 if (within_module_init(addr, mod))
Rusty Russell7523e4d2015-11-26 09:44:08 +10303967 nextval = (unsigned long)mod->init_layout.base+mod->init_layout.text_size;
Daniel Walker22a8bde2007-10-18 03:06:07 -07003968 else
Rusty Russell7523e4d2015-11-26 09:44:08 +10303969 nextval = (unsigned long)mod->core_layout.base+mod->core_layout.text_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003970
Vincent Whitchurch93d77e72018-12-14 17:05:55 +01003971 bestval = kallsyms_symbol_value(&kallsyms->symtab[best]);
3972
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003973 /* Scan for closest preceding symbol, and next symbol. (ELF
Daniel Walker22a8bde2007-10-18 03:06:07 -07003974 starts real symbols at 1). */
Rusty Russell82440622016-02-03 16:55:26 +10303975 for (i = 1; i < kallsyms->num_symtab; i++) {
Vincent Whitchurch93d77e72018-12-14 17:05:55 +01003976 const Elf_Sym *sym = &kallsyms->symtab[i];
3977 unsigned long thisval = kallsyms_symbol_value(sym);
3978
3979 if (sym->st_shndx == SHN_UNDEF)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003980 continue;
3981
3982 /* We ignore unnamed symbols: they're uninformative
3983 * and inserted at a whim. */
Jessica Yu2d25bc52018-11-19 17:43:58 +01003984 if (*kallsyms_symbol_name(kallsyms, i) == '\0'
3985 || is_arm_mapping_symbol(kallsyms_symbol_name(kallsyms, i)))
Rusty Russell2e7bac52016-02-03 16:55:26 +10303986 continue;
3987
Vincent Whitchurch93d77e72018-12-14 17:05:55 +01003988 if (thisval <= addr && thisval > bestval) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003989 best = i;
Vincent Whitchurch93d77e72018-12-14 17:05:55 +01003990 bestval = thisval;
3991 }
3992 if (thisval > addr && thisval < nextval)
3993 nextval = thisval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003994 }
3995
3996 if (!best)
3997 return NULL;
3998
Alexey Dobriyanffb45122007-05-08 00:28:41 -07003999 if (size)
Vincent Whitchurch93d77e72018-12-14 17:05:55 +01004000 *size = nextval - bestval;
Alexey Dobriyanffb45122007-05-08 00:28:41 -07004001 if (offset)
Vincent Whitchurch93d77e72018-12-14 17:05:55 +01004002 *offset = addr - bestval;
Jessica Yu2d25bc52018-11-19 17:43:58 +01004003
4004 return kallsyms_symbol_name(kallsyms, best);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004005}
4006
Sergey Senozhatskyb865ea62017-11-10 08:48:25 +09004007void * __weak dereference_module_function_descriptor(struct module *mod,
4008 void *ptr)
4009{
4010 return ptr;
4011}
4012
Rusty Russell6dd06c92008-01-29 17:13:22 -05004013/* For kallsyms to ask for address resolution. NULL means not found. Careful
4014 * not to lock to avoid deadlock on oopses, simply disable preemption. */
Andrew Morton92dfc9d2008-02-08 04:18:43 -08004015const char *module_address_lookup(unsigned long addr,
Rusty Russell6dd06c92008-01-29 17:13:22 -05004016 unsigned long *size,
4017 unsigned long *offset,
4018 char **modname,
4019 char *namebuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004020{
Rusty Russellcb2a5202008-01-14 00:55:03 -08004021 const char *ret = NULL;
Peter Zijlstrab7df4d12015-05-27 11:09:37 +09304022 struct module *mod;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004023
Rusty Russellcb2a5202008-01-14 00:55:03 -08004024 preempt_disable();
Peter Zijlstrab7df4d12015-05-27 11:09:37 +09304025 mod = __module_address(addr);
4026 if (mod) {
4027 if (modname)
4028 *modname = mod->name;
Jessica Yu2d25bc52018-11-19 17:43:58 +01004029
4030 ret = find_kallsyms_symbol(mod, addr, size, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004031 }
Rusty Russell6dd06c92008-01-29 17:13:22 -05004032 /* Make a copy in here where it's safe */
4033 if (ret) {
4034 strncpy(namebuf, ret, KSYM_NAME_LEN - 1);
4035 ret = namebuf;
4036 }
Rusty Russellcb2a5202008-01-14 00:55:03 -08004037 preempt_enable();
Peter Zijlstrab7df4d12015-05-27 11:09:37 +09304038
Andrew Morton92dfc9d2008-02-08 04:18:43 -08004039 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004040}
4041
Alexey Dobriyan9d65cb42007-05-08 00:28:43 -07004042int lookup_module_symbol_name(unsigned long addr, char *symname)
4043{
4044 struct module *mod;
4045
Rusty Russellcb2a5202008-01-14 00:55:03 -08004046 preempt_disable();
Andi Kleend72b3752008-08-30 10:09:00 +02004047 list_for_each_entry_rcu(mod, &modules, list) {
Rusty Russell0d21b0e2013-01-12 11:38:44 +10304048 if (mod->state == MODULE_STATE_UNFORMED)
4049 continue;
Petr Mladek9b20a352014-07-27 07:24:01 +09304050 if (within_module(addr, mod)) {
Alexey Dobriyan9d65cb42007-05-08 00:28:43 -07004051 const char *sym;
4052
Jessica Yu2d25bc52018-11-19 17:43:58 +01004053 sym = find_kallsyms_symbol(mod, addr, NULL, NULL);
Alexey Dobriyan9d65cb42007-05-08 00:28:43 -07004054 if (!sym)
4055 goto out;
Jessica Yu2d25bc52018-11-19 17:43:58 +01004056
Tejun Heo9281ace2007-07-17 04:03:51 -07004057 strlcpy(symname, sym, KSYM_NAME_LEN);
Rusty Russellcb2a5202008-01-14 00:55:03 -08004058 preempt_enable();
Alexey Dobriyan9d65cb42007-05-08 00:28:43 -07004059 return 0;
4060 }
4061 }
4062out:
Rusty Russellcb2a5202008-01-14 00:55:03 -08004063 preempt_enable();
Alexey Dobriyan9d65cb42007-05-08 00:28:43 -07004064 return -ERANGE;
4065}
4066
Alexey Dobriyana5c43da2007-05-08 00:28:47 -07004067int lookup_module_symbol_attrs(unsigned long addr, unsigned long *size,
4068 unsigned long *offset, char *modname, char *name)
4069{
4070 struct module *mod;
4071
Rusty Russellcb2a5202008-01-14 00:55:03 -08004072 preempt_disable();
Andi Kleend72b3752008-08-30 10:09:00 +02004073 list_for_each_entry_rcu(mod, &modules, list) {
Rusty Russell0d21b0e2013-01-12 11:38:44 +10304074 if (mod->state == MODULE_STATE_UNFORMED)
4075 continue;
Petr Mladek9b20a352014-07-27 07:24:01 +09304076 if (within_module(addr, mod)) {
Alexey Dobriyana5c43da2007-05-08 00:28:47 -07004077 const char *sym;
4078
Jessica Yu2d25bc52018-11-19 17:43:58 +01004079 sym = find_kallsyms_symbol(mod, addr, size, offset);
Alexey Dobriyana5c43da2007-05-08 00:28:47 -07004080 if (!sym)
4081 goto out;
4082 if (modname)
Tejun Heo9281ace2007-07-17 04:03:51 -07004083 strlcpy(modname, mod->name, MODULE_NAME_LEN);
Alexey Dobriyana5c43da2007-05-08 00:28:47 -07004084 if (name)
Tejun Heo9281ace2007-07-17 04:03:51 -07004085 strlcpy(name, sym, KSYM_NAME_LEN);
Rusty Russellcb2a5202008-01-14 00:55:03 -08004086 preempt_enable();
Alexey Dobriyana5c43da2007-05-08 00:28:47 -07004087 return 0;
4088 }
4089 }
4090out:
Rusty Russellcb2a5202008-01-14 00:55:03 -08004091 preempt_enable();
Alexey Dobriyana5c43da2007-05-08 00:28:47 -07004092 return -ERANGE;
4093}
4094
Alexey Dobriyanea078902007-05-08 00:28:39 -07004095int module_get_kallsym(unsigned int symnum, unsigned long *value, char *type,
4096 char *name, char *module_name, int *exported)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004097{
4098 struct module *mod;
4099
Rusty Russellcb2a5202008-01-14 00:55:03 -08004100 preempt_disable();
Andi Kleend72b3752008-08-30 10:09:00 +02004101 list_for_each_entry_rcu(mod, &modules, list) {
Rusty Russell82440622016-02-03 16:55:26 +10304102 struct mod_kallsyms *kallsyms;
4103
Rusty Russell0d21b0e2013-01-12 11:38:44 +10304104 if (mod->state == MODULE_STATE_UNFORMED)
4105 continue;
Rusty Russell82440622016-02-03 16:55:26 +10304106 kallsyms = rcu_dereference_sched(mod->kallsyms);
4107 if (symnum < kallsyms->num_symtab) {
Vincent Whitchurch93d77e72018-12-14 17:05:55 +01004108 const Elf_Sym *sym = &kallsyms->symtab[symnum];
4109
4110 *value = kallsyms_symbol_value(sym);
Eugene Loh1c7651f2019-02-25 11:59:58 -08004111 *type = kallsyms->typetab[symnum];
Jessica Yu2d25bc52018-11-19 17:43:58 +01004112 strlcpy(name, kallsyms_symbol_name(kallsyms, symnum), KSYM_NAME_LEN);
Tejun Heo9281ace2007-07-17 04:03:51 -07004113 strlcpy(module_name, mod->name, MODULE_NAME_LEN);
Tim Abbottca4787b2009-01-05 08:40:10 -06004114 *exported = is_exported(name, *value, mod);
Rusty Russellcb2a5202008-01-14 00:55:03 -08004115 preempt_enable();
Alexey Dobriyanea078902007-05-08 00:28:39 -07004116 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004117 }
Rusty Russell82440622016-02-03 16:55:26 +10304118 symnum -= kallsyms->num_symtab;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004119 }
Rusty Russellcb2a5202008-01-14 00:55:03 -08004120 preempt_enable();
Alexey Dobriyanea078902007-05-08 00:28:39 -07004121 return -ERANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004122}
4123
Jessica Yu2d25bc52018-11-19 17:43:58 +01004124/* Given a module and name of symbol, find and return the symbol's value */
4125static unsigned long find_kallsyms_symbol_value(struct module *mod, const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004126{
4127 unsigned int i;
Rusty Russell82440622016-02-03 16:55:26 +10304128 struct mod_kallsyms *kallsyms = rcu_dereference_sched(mod->kallsyms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004129
Vincent Whitchurch93d77e72018-12-14 17:05:55 +01004130 for (i = 0; i < kallsyms->num_symtab; i++) {
4131 const Elf_Sym *sym = &kallsyms->symtab[i];
4132
Jessica Yu2d25bc52018-11-19 17:43:58 +01004133 if (strcmp(name, kallsyms_symbol_name(kallsyms, i)) == 0 &&
Vincent Whitchurch93d77e72018-12-14 17:05:55 +01004134 sym->st_shndx != SHN_UNDEF)
4135 return kallsyms_symbol_value(sym);
4136 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004137 return 0;
4138}
4139
4140/* Look for this name: can be of form module:name. */
4141unsigned long module_kallsyms_lookup_name(const char *name)
4142{
4143 struct module *mod;
4144 char *colon;
4145 unsigned long ret = 0;
4146
4147 /* Don't lock: we're in enough trouble already. */
Rusty Russellcb2a5202008-01-14 00:55:03 -08004148 preempt_disable();
Naveen N. Rao17586182017-04-23 22:53:43 +05304149 if ((colon = strnchr(name, MODULE_NAME_LEN, ':')) != NULL) {
Mathias Krause4f6de4d52013-07-02 15:35:11 +09304150 if ((mod = find_module_all(name, colon - name, false)) != NULL)
Jessica Yu2d25bc52018-11-19 17:43:58 +01004151 ret = find_kallsyms_symbol_value(mod, colon+1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004152 } else {
Rusty Russell0d21b0e2013-01-12 11:38:44 +10304153 list_for_each_entry_rcu(mod, &modules, list) {
4154 if (mod->state == MODULE_STATE_UNFORMED)
4155 continue;
Jessica Yu2d25bc52018-11-19 17:43:58 +01004156 if ((ret = find_kallsyms_symbol_value(mod, name)) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004157 break;
Rusty Russell0d21b0e2013-01-12 11:38:44 +10304158 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004159 }
Rusty Russellcb2a5202008-01-14 00:55:03 -08004160 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004161 return ret;
4162}
Anders Kaseorg75a66612008-12-05 19:03:58 -05004163
4164int module_kallsyms_on_each_symbol(int (*fn)(void *, const char *,
4165 struct module *, unsigned long),
4166 void *data)
4167{
4168 struct module *mod;
4169 unsigned int i;
4170 int ret;
4171
Peter Zijlstra0be964b2015-05-27 11:09:35 +09304172 module_assert_mutex();
4173
Anders Kaseorg75a66612008-12-05 19:03:58 -05004174 list_for_each_entry(mod, &modules, list) {
Rusty Russell82440622016-02-03 16:55:26 +10304175 /* We hold module_mutex: no need for rcu_dereference_sched */
4176 struct mod_kallsyms *kallsyms = mod->kallsyms;
4177
Rusty Russell0d21b0e2013-01-12 11:38:44 +10304178 if (mod->state == MODULE_STATE_UNFORMED)
4179 continue;
Rusty Russell82440622016-02-03 16:55:26 +10304180 for (i = 0; i < kallsyms->num_symtab; i++) {
Vincent Whitchurch93d77e72018-12-14 17:05:55 +01004181 const Elf_Sym *sym = &kallsyms->symtab[i];
Jessica Yu9f2d1e62018-06-05 10:22:52 +02004182
Vincent Whitchurch93d77e72018-12-14 17:05:55 +01004183 if (sym->st_shndx == SHN_UNDEF)
Jessica Yu9f2d1e62018-06-05 10:22:52 +02004184 continue;
4185
Jessica Yu2d25bc52018-11-19 17:43:58 +01004186 ret = fn(data, kallsyms_symbol_name(kallsyms, i),
Vincent Whitchurch93d77e72018-12-14 17:05:55 +01004187 mod, kallsyms_symbol_value(sym));
Anders Kaseorg75a66612008-12-05 19:03:58 -05004188 if (ret != 0)
4189 return ret;
4190 }
4191 }
4192 return 0;
4193}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004194#endif /* CONFIG_KALLSYMS */
4195
Petr Mladek7fd83292016-09-21 13:47:22 +02004196/* Maximum number of characters written by module_flags() */
4197#define MODULE_FLAGS_BUF_SIZE (TAINT_FLAGS_COUNT + 4)
4198
4199/* Keep in sync with MODULE_FLAGS_BUF_SIZE !!! */
Arjan van de Ven21aa9282008-01-25 21:08:33 +01004200static char *module_flags(struct module *mod, char *buf)
Florin Malitafa3ba2e82006-10-11 01:21:48 -07004201{
4202 int bx = 0;
4203
Rusty Russell0d21b0e2013-01-12 11:38:44 +10304204 BUG_ON(mod->state == MODULE_STATE_UNFORMED);
Arjan van de Ven21aa9282008-01-25 21:08:33 +01004205 if (mod->taints ||
4206 mod->state == MODULE_STATE_GOING ||
4207 mod->state == MODULE_STATE_COMING) {
Florin Malitafa3ba2e82006-10-11 01:21:48 -07004208 buf[bx++] = '(';
Kay Sieverscca3e702012-01-13 09:32:15 +10304209 bx += module_flags_taint(mod, buf + bx);
Arjan van de Ven21aa9282008-01-25 21:08:33 +01004210 /* Show a - for module-is-being-unloaded */
4211 if (mod->state == MODULE_STATE_GOING)
4212 buf[bx++] = '-';
4213 /* Show a + for module-is-being-loaded */
4214 if (mod->state == MODULE_STATE_COMING)
4215 buf[bx++] = '+';
Florin Malitafa3ba2e82006-10-11 01:21:48 -07004216 buf[bx++] = ')';
4217 }
4218 buf[bx] = '\0';
4219
4220 return buf;
4221}
4222
Alexey Dobriyan3b5d5c62008-10-06 13:19:27 +04004223#ifdef CONFIG_PROC_FS
4224/* Called by the /proc file system to return a list of modules. */
4225static void *m_start(struct seq_file *m, loff_t *pos)
4226{
4227 mutex_lock(&module_mutex);
4228 return seq_list_start(&modules, *pos);
4229}
4230
4231static void *m_next(struct seq_file *m, void *p, loff_t *pos)
4232{
4233 return seq_list_next(p, &modules, pos);
4234}
4235
4236static void m_stop(struct seq_file *m, void *p)
4237{
4238 mutex_unlock(&module_mutex);
4239}
4240
Linus Torvalds1da177e2005-04-16 15:20:36 -07004241static int m_show(struct seq_file *m, void *p)
4242{
4243 struct module *mod = list_entry(p, struct module, list);
Petr Mladek7fd83292016-09-21 13:47:22 +02004244 char buf[MODULE_FLAGS_BUF_SIZE];
Linus Torvalds668533d2017-11-29 10:30:13 -08004245 void *value;
Florin Malitafa3ba2e82006-10-11 01:21:48 -07004246
Rusty Russell0d21b0e2013-01-12 11:38:44 +10304247 /* We always ignore unformed modules. */
4248 if (mod->state == MODULE_STATE_UNFORMED)
4249 return 0;
4250
Denys Vlasenko2f0f2a32008-07-22 19:24:27 -05004251 seq_printf(m, "%s %u",
Rusty Russell7523e4d2015-11-26 09:44:08 +10304252 mod->name, mod->init_layout.size + mod->core_layout.size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004253 print_unload_info(m, mod);
4254
4255 /* Informative for users. */
4256 seq_printf(m, " %s",
Ionut Alexa6da0b562014-11-10 09:31:29 +10304257 mod->state == MODULE_STATE_GOING ? "Unloading" :
4258 mod->state == MODULE_STATE_COMING ? "Loading" :
Linus Torvalds1da177e2005-04-16 15:20:36 -07004259 "Live");
4260 /* Used by oprofile and other similar tools. */
Linus Torvalds668533d2017-11-29 10:30:13 -08004261 value = m->private ? NULL : mod->core_layout.base;
4262 seq_printf(m, " 0x%px", value);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004263
Florin Malitafa3ba2e82006-10-11 01:21:48 -07004264 /* Taints info */
4265 if (mod->taints)
Arjan van de Ven21aa9282008-01-25 21:08:33 +01004266 seq_printf(m, " %s", module_flags(mod, buf));
Florin Malitafa3ba2e82006-10-11 01:21:48 -07004267
Ionut Alexa6da0b562014-11-10 09:31:29 +10304268 seq_puts(m, "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004269 return 0;
4270}
4271
4272/* Format: modulename size refcount deps address
4273
4274 Where refcount is a number or -, and deps is a comma-separated list
4275 of depends or -.
4276*/
Alexey Dobriyan3b5d5c62008-10-06 13:19:27 +04004277static const struct seq_operations modules_op = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004278 .start = m_start,
4279 .next = m_next,
4280 .stop = m_stop,
4281 .show = m_show
4282};
4283
Linus Torvalds516fb7f2017-11-12 18:44:23 -08004284/*
4285 * This also sets the "private" pointer to non-NULL if the
4286 * kernel pointers should be hidden (so you can just test
4287 * "m->private" to see if you should keep the values private).
4288 *
4289 * We use the same logic as for /proc/kallsyms.
4290 */
Alexey Dobriyan3b5d5c62008-10-06 13:19:27 +04004291static int modules_open(struct inode *inode, struct file *file)
4292{
Linus Torvalds516fb7f2017-11-12 18:44:23 -08004293 int err = seq_open(file, &modules_op);
4294
4295 if (!err) {
4296 struct seq_file *m = file->private_data;
4297 m->private = kallsyms_show_value() ? NULL : (void *)8ul;
4298 }
4299
Leon Yu3f553b32018-03-06 23:16:24 +08004300 return err;
Alexey Dobriyan3b5d5c62008-10-06 13:19:27 +04004301}
4302
4303static const struct file_operations proc_modules_operations = {
4304 .open = modules_open,
4305 .read = seq_read,
4306 .llseek = seq_lseek,
4307 .release = seq_release,
4308};
4309
4310static int __init proc_modules_init(void)
4311{
4312 proc_create("modules", 0, NULL, &proc_modules_operations);
4313 return 0;
4314}
4315module_init(proc_modules_init);
4316#endif
4317
Linus Torvalds1da177e2005-04-16 15:20:36 -07004318/* Given an address, look for it in the module exception tables. */
4319const struct exception_table_entry *search_module_extables(unsigned long addr)
4320{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004321 const struct exception_table_entry *e = NULL;
4322 struct module *mod;
4323
Rusty Russell24da1cb2007-07-15 23:41:46 -07004324 preempt_disable();
Peter Zijlstra5ff22642017-02-08 15:48:01 +01004325 mod = __module_address(addr);
4326 if (!mod)
4327 goto out;
Daniel Walker22a8bde2007-10-18 03:06:07 -07004328
Peter Zijlstra5ff22642017-02-08 15:48:01 +01004329 if (!mod->num_exentries)
4330 goto out;
4331
4332 e = search_extable(mod->extable,
Thomas Meyera94c33d2017-07-10 15:51:58 -07004333 mod->num_exentries,
Peter Zijlstra5ff22642017-02-08 15:48:01 +01004334 addr);
4335out:
Rusty Russell24da1cb2007-07-15 23:41:46 -07004336 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004337
Peter Zijlstra5ff22642017-02-08 15:48:01 +01004338 /*
4339 * Now, if we found one, we are running inside it now, hence
4340 * we cannot unload the module, hence no refcnt needed.
4341 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004342 return e;
4343}
4344
Ingo Molnar4d435f92006-07-03 00:24:24 -07004345/*
Rusty Russelle6104992009-03-31 13:05:31 -06004346 * is_module_address - is this address inside a module?
4347 * @addr: the address to check.
4348 *
4349 * See is_module_text_address() if you simply want to see if the address
4350 * is code (not data).
Ingo Molnar4d435f92006-07-03 00:24:24 -07004351 */
Rusty Russelle6104992009-03-31 13:05:31 -06004352bool is_module_address(unsigned long addr)
Ingo Molnar4d435f92006-07-03 00:24:24 -07004353{
Rusty Russelle6104992009-03-31 13:05:31 -06004354 bool ret;
Ingo Molnar4d435f92006-07-03 00:24:24 -07004355
Rusty Russell24da1cb2007-07-15 23:41:46 -07004356 preempt_disable();
Rusty Russelle6104992009-03-31 13:05:31 -06004357 ret = __module_address(addr) != NULL;
Rusty Russell24da1cb2007-07-15 23:41:46 -07004358 preempt_enable();
Ingo Molnar4d435f92006-07-03 00:24:24 -07004359
Rusty Russelle6104992009-03-31 13:05:31 -06004360 return ret;
Ingo Molnar4d435f92006-07-03 00:24:24 -07004361}
4362
Rusty Russelle6104992009-03-31 13:05:31 -06004363/*
4364 * __module_address - get the module which contains an address.
4365 * @addr: the address.
4366 *
4367 * Must be called with preempt disabled or module mutex held so that
4368 * module doesn't get freed during this.
4369 */
Linus Torvalds714f83d2009-04-05 11:04:19 -07004370struct module *__module_address(unsigned long addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004371{
4372 struct module *mod;
4373
Rusty Russell3a642e92008-07-22 19:24:28 -05004374 if (addr < module_addr_min || addr > module_addr_max)
4375 return NULL;
4376
Peter Zijlstra0be964b2015-05-27 11:09:35 +09304377 module_assert_mutex_or_preempt();
4378
Peter Zijlstra6c9692e2015-05-27 11:09:37 +09304379 mod = mod_find(addr);
Peter Zijlstra93c2e102015-05-27 11:09:37 +09304380 if (mod) {
4381 BUG_ON(!within_module(addr, mod));
Rusty Russell0d21b0e2013-01-12 11:38:44 +10304382 if (mod->state == MODULE_STATE_UNFORMED)
Peter Zijlstra93c2e102015-05-27 11:09:37 +09304383 mod = NULL;
Rusty Russell0d21b0e2013-01-12 11:38:44 +10304384 }
Peter Zijlstra93c2e102015-05-27 11:09:37 +09304385 return mod;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004386}
Tim Abbottc6b37802008-12-05 19:03:59 -05004387EXPORT_SYMBOL_GPL(__module_address);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004388
Rusty Russelle6104992009-03-31 13:05:31 -06004389/*
4390 * is_module_text_address - is this address inside module code?
4391 * @addr: the address to check.
4392 *
4393 * See is_module_address() if you simply want to see if the address is
4394 * anywhere in a module. See kernel_text_address() for testing if an
4395 * address corresponds to kernel or module code.
4396 */
4397bool is_module_text_address(unsigned long addr)
4398{
4399 bool ret;
4400
4401 preempt_disable();
4402 ret = __module_text_address(addr) != NULL;
4403 preempt_enable();
4404
4405 return ret;
4406}
4407
4408/*
4409 * __module_text_address - get the module whose code contains an address.
4410 * @addr: the address.
4411 *
4412 * Must be called with preempt disabled or module mutex held so that
4413 * module doesn't get freed during this.
4414 */
4415struct module *__module_text_address(unsigned long addr)
4416{
4417 struct module *mod = __module_address(addr);
4418 if (mod) {
4419 /* Make sure it's within the text section. */
Rusty Russell7523e4d2015-11-26 09:44:08 +10304420 if (!within(addr, mod->init_layout.base, mod->init_layout.text_size)
4421 && !within(addr, mod->core_layout.base, mod->core_layout.text_size))
Rusty Russelle6104992009-03-31 13:05:31 -06004422 mod = NULL;
4423 }
4424 return mod;
4425}
Tim Abbottc6b37802008-12-05 19:03:59 -05004426EXPORT_SYMBOL_GPL(__module_text_address);
Rusty Russelle6104992009-03-31 13:05:31 -06004427
Linus Torvalds1da177e2005-04-16 15:20:36 -07004428/* Don't grab lock, we're oopsing. */
4429void print_modules(void)
4430{
4431 struct module *mod;
Petr Mladek7fd83292016-09-21 13:47:22 +02004432 char buf[MODULE_FLAGS_BUF_SIZE];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004433
Linus Torvaldsb2311252009-06-16 11:07:14 -07004434 printk(KERN_DEFAULT "Modules linked in:");
Andi Kleend72b3752008-08-30 10:09:00 +02004435 /* Most callers should already have preempt disabled, but make sure */
4436 preempt_disable();
Rusty Russell0d21b0e2013-01-12 11:38:44 +10304437 list_for_each_entry_rcu(mod, &modules, list) {
4438 if (mod->state == MODULE_STATE_UNFORMED)
4439 continue;
Jiri Slaby27bba4d2014-02-03 11:13:13 +10304440 pr_cont(" %s%s", mod->name, module_flags(mod, buf));
Rusty Russell0d21b0e2013-01-12 11:38:44 +10304441 }
Andi Kleend72b3752008-08-30 10:09:00 +02004442 preempt_enable();
Arjan van de Vene14af7e2008-01-25 21:08:33 +01004443 if (last_unloaded_module[0])
Jiri Slaby27bba4d2014-02-03 11:13:13 +10304444 pr_cont(" [last unloaded: %s]", last_unloaded_module);
4445 pr_cont("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004446}
4447
Linus Torvalds1da177e2005-04-16 15:20:36 -07004448#ifdef CONFIG_MODVERSIONS
Rusty Russell8c8ef422009-03-31 13:05:34 -06004449/* Generate the signature for all relevant module structures here.
4450 * If these change, we don't want to try to parse the module. */
4451void module_layout(struct module *mod,
4452 struct modversion_info *ver,
4453 struct kernel_param *kp,
4454 struct kernel_symbol *ks,
Mathieu Desnoyers65498642011-01-26 17:26:22 -05004455 struct tracepoint * const *tp)
Rusty Russell8c8ef422009-03-31 13:05:34 -06004456{
4457}
4458EXPORT_SYMBOL(module_layout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004459#endif