xref: /OK3568_Linux_fs/kernel/kernel/module.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-or-later
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun    Copyright (C) 2002 Richard Henderson
4*4882a593Smuzhiyun    Copyright (C) 2001 Rusty Russell, 2002, 2010 Rusty Russell IBM.
5*4882a593Smuzhiyun 
6*4882a593Smuzhiyun */
7*4882a593Smuzhiyun 
8*4882a593Smuzhiyun #define INCLUDE_VERMAGIC
9*4882a593Smuzhiyun 
10*4882a593Smuzhiyun #include <linux/export.h>
11*4882a593Smuzhiyun #include <linux/extable.h>
12*4882a593Smuzhiyun #include <linux/moduleloader.h>
13*4882a593Smuzhiyun #include <linux/module_signature.h>
14*4882a593Smuzhiyun #include <linux/trace_events.h>
15*4882a593Smuzhiyun #include <linux/init.h>
16*4882a593Smuzhiyun #include <linux/kallsyms.h>
17*4882a593Smuzhiyun #include <linux/file.h>
18*4882a593Smuzhiyun #include <linux/fs.h>
19*4882a593Smuzhiyun #include <linux/sysfs.h>
20*4882a593Smuzhiyun #include <linux/kernel.h>
21*4882a593Smuzhiyun #include <linux/kernel_read_file.h>
22*4882a593Smuzhiyun #include <linux/slab.h>
23*4882a593Smuzhiyun #include <linux/vmalloc.h>
24*4882a593Smuzhiyun #include <linux/elf.h>
25*4882a593Smuzhiyun #include <linux/proc_fs.h>
26*4882a593Smuzhiyun #include <linux/security.h>
27*4882a593Smuzhiyun #include <linux/seq_file.h>
28*4882a593Smuzhiyun #include <linux/syscalls.h>
29*4882a593Smuzhiyun #include <linux/fcntl.h>
30*4882a593Smuzhiyun #include <linux/rcupdate.h>
31*4882a593Smuzhiyun #include <linux/capability.h>
32*4882a593Smuzhiyun #include <linux/cpu.h>
33*4882a593Smuzhiyun #include <linux/moduleparam.h>
34*4882a593Smuzhiyun #include <linux/errno.h>
35*4882a593Smuzhiyun #include <linux/err.h>
36*4882a593Smuzhiyun #include <linux/vermagic.h>
37*4882a593Smuzhiyun #include <linux/notifier.h>
38*4882a593Smuzhiyun #include <linux/sched.h>
39*4882a593Smuzhiyun #include <linux/device.h>
40*4882a593Smuzhiyun #include <linux/string.h>
41*4882a593Smuzhiyun #include <linux/mutex.h>
42*4882a593Smuzhiyun #include <linux/rculist.h>
43*4882a593Smuzhiyun #include <linux/uaccess.h>
44*4882a593Smuzhiyun #include <asm/cacheflush.h>
45*4882a593Smuzhiyun #include <linux/set_memory.h>
46*4882a593Smuzhiyun #include <asm/mmu_context.h>
47*4882a593Smuzhiyun #include <linux/license.h>
48*4882a593Smuzhiyun #include <asm/sections.h>
49*4882a593Smuzhiyun #include <linux/tracepoint.h>
50*4882a593Smuzhiyun #include <linux/ftrace.h>
51*4882a593Smuzhiyun #include <linux/livepatch.h>
52*4882a593Smuzhiyun #include <linux/async.h>
53*4882a593Smuzhiyun #include <linux/percpu.h>
54*4882a593Smuzhiyun #include <linux/kmemleak.h>
55*4882a593Smuzhiyun #include <linux/jump_label.h>
56*4882a593Smuzhiyun #include <linux/pfn.h>
57*4882a593Smuzhiyun #include <linux/bsearch.h>
58*4882a593Smuzhiyun #include <linux/dynamic_debug.h>
59*4882a593Smuzhiyun #include <linux/audit.h>
60*4882a593Smuzhiyun #include <uapi/linux/module.h>
61*4882a593Smuzhiyun #include "module-internal.h"
62*4882a593Smuzhiyun 
63*4882a593Smuzhiyun #define CREATE_TRACE_POINTS
64*4882a593Smuzhiyun #include <trace/events/module.h>
65*4882a593Smuzhiyun 
66*4882a593Smuzhiyun #undef CREATE_TRACE_POINTS
67*4882a593Smuzhiyun #include <trace/hooks/module.h>
68*4882a593Smuzhiyun #include <trace/hooks/memory.h>
69*4882a593Smuzhiyun 
70*4882a593Smuzhiyun #ifndef ARCH_SHF_SMALL
71*4882a593Smuzhiyun #define ARCH_SHF_SMALL 0
72*4882a593Smuzhiyun #endif
73*4882a593Smuzhiyun 
74*4882a593Smuzhiyun /*
75*4882a593Smuzhiyun  * Modules' sections will be aligned on page boundaries
76*4882a593Smuzhiyun  * to ensure complete separation of code and data, but
77*4882a593Smuzhiyun  * only when CONFIG_ARCH_HAS_STRICT_MODULE_RWX=y
78*4882a593Smuzhiyun  */
79*4882a593Smuzhiyun #ifdef CONFIG_ARCH_HAS_STRICT_MODULE_RWX
80*4882a593Smuzhiyun # define debug_align(X) ALIGN(X, PAGE_SIZE)
81*4882a593Smuzhiyun #else
82*4882a593Smuzhiyun # define debug_align(X) (X)
83*4882a593Smuzhiyun #endif
84*4882a593Smuzhiyun 
85*4882a593Smuzhiyun /* If this is set, the section belongs in the init part of the module */
86*4882a593Smuzhiyun #define INIT_OFFSET_MASK (1UL << (BITS_PER_LONG-1))
87*4882a593Smuzhiyun 
88*4882a593Smuzhiyun /*
89*4882a593Smuzhiyun  * Mutex protects:
90*4882a593Smuzhiyun  * 1) List of modules (also safely readable with preempt_disable),
91*4882a593Smuzhiyun  * 2) module_use links,
92*4882a593Smuzhiyun  * 3) module_addr_min/module_addr_max.
93*4882a593Smuzhiyun  * (delete and add uses RCU list operations). */
94*4882a593Smuzhiyun DEFINE_MUTEX(module_mutex);
95*4882a593Smuzhiyun static LIST_HEAD(modules);
96*4882a593Smuzhiyun 
97*4882a593Smuzhiyun /* Work queue for freeing init sections in success case */
98*4882a593Smuzhiyun static void do_free_init(struct work_struct *w);
99*4882a593Smuzhiyun static DECLARE_WORK(init_free_wq, do_free_init);
100*4882a593Smuzhiyun static LLIST_HEAD(init_free_list);
101*4882a593Smuzhiyun 
102*4882a593Smuzhiyun #ifdef CONFIG_MODULES_TREE_LOOKUP
103*4882a593Smuzhiyun 
104*4882a593Smuzhiyun /*
105*4882a593Smuzhiyun  * Use a latched RB-tree for __module_address(); this allows us to use
106*4882a593Smuzhiyun  * RCU-sched lookups of the address from any context.
107*4882a593Smuzhiyun  *
108*4882a593Smuzhiyun  * This is conditional on PERF_EVENTS || TRACING because those can really hit
109*4882a593Smuzhiyun  * __module_address() hard by doing a lot of stack unwinding; potentially from
110*4882a593Smuzhiyun  * NMI context.
111*4882a593Smuzhiyun  */
112*4882a593Smuzhiyun 
__mod_tree_val(struct latch_tree_node * n)113*4882a593Smuzhiyun static __always_inline unsigned long __mod_tree_val(struct latch_tree_node *n)
114*4882a593Smuzhiyun {
115*4882a593Smuzhiyun 	struct module_layout *layout = container_of(n, struct module_layout, mtn.node);
116*4882a593Smuzhiyun 
117*4882a593Smuzhiyun 	return (unsigned long)layout->base;
118*4882a593Smuzhiyun }
119*4882a593Smuzhiyun 
__mod_tree_size(struct latch_tree_node * n)120*4882a593Smuzhiyun static __always_inline unsigned long __mod_tree_size(struct latch_tree_node *n)
121*4882a593Smuzhiyun {
122*4882a593Smuzhiyun 	struct module_layout *layout = container_of(n, struct module_layout, mtn.node);
123*4882a593Smuzhiyun 
124*4882a593Smuzhiyun 	return (unsigned long)layout->size;
125*4882a593Smuzhiyun }
126*4882a593Smuzhiyun 
127*4882a593Smuzhiyun static __always_inline bool
mod_tree_less(struct latch_tree_node * a,struct latch_tree_node * b)128*4882a593Smuzhiyun mod_tree_less(struct latch_tree_node *a, struct latch_tree_node *b)
129*4882a593Smuzhiyun {
130*4882a593Smuzhiyun 	return __mod_tree_val(a) < __mod_tree_val(b);
131*4882a593Smuzhiyun }
132*4882a593Smuzhiyun 
133*4882a593Smuzhiyun static __always_inline int
mod_tree_comp(void * key,struct latch_tree_node * n)134*4882a593Smuzhiyun mod_tree_comp(void *key, struct latch_tree_node *n)
135*4882a593Smuzhiyun {
136*4882a593Smuzhiyun 	unsigned long val = (unsigned long)key;
137*4882a593Smuzhiyun 	unsigned long start, end;
138*4882a593Smuzhiyun 
139*4882a593Smuzhiyun 	start = __mod_tree_val(n);
140*4882a593Smuzhiyun 	if (val < start)
141*4882a593Smuzhiyun 		return -1;
142*4882a593Smuzhiyun 
143*4882a593Smuzhiyun 	end = start + __mod_tree_size(n);
144*4882a593Smuzhiyun 	if (val >= end)
145*4882a593Smuzhiyun 		return 1;
146*4882a593Smuzhiyun 
147*4882a593Smuzhiyun 	return 0;
148*4882a593Smuzhiyun }
149*4882a593Smuzhiyun 
150*4882a593Smuzhiyun static const struct latch_tree_ops mod_tree_ops = {
151*4882a593Smuzhiyun 	.less = mod_tree_less,
152*4882a593Smuzhiyun 	.comp = mod_tree_comp,
153*4882a593Smuzhiyun };
154*4882a593Smuzhiyun 
155*4882a593Smuzhiyun static struct mod_tree_root {
156*4882a593Smuzhiyun 	struct latch_tree_root root;
157*4882a593Smuzhiyun 	unsigned long addr_min;
158*4882a593Smuzhiyun 	unsigned long addr_max;
159*4882a593Smuzhiyun } mod_tree __cacheline_aligned = {
160*4882a593Smuzhiyun 	.addr_min = -1UL,
161*4882a593Smuzhiyun };
162*4882a593Smuzhiyun 
163*4882a593Smuzhiyun #define module_addr_min mod_tree.addr_min
164*4882a593Smuzhiyun #define module_addr_max mod_tree.addr_max
165*4882a593Smuzhiyun 
__mod_tree_insert(struct mod_tree_node * node)166*4882a593Smuzhiyun static noinline void __mod_tree_insert(struct mod_tree_node *node)
167*4882a593Smuzhiyun {
168*4882a593Smuzhiyun 	latch_tree_insert(&node->node, &mod_tree.root, &mod_tree_ops);
169*4882a593Smuzhiyun }
170*4882a593Smuzhiyun 
__mod_tree_remove(struct mod_tree_node * node)171*4882a593Smuzhiyun static void __mod_tree_remove(struct mod_tree_node *node)
172*4882a593Smuzhiyun {
173*4882a593Smuzhiyun 	latch_tree_erase(&node->node, &mod_tree.root, &mod_tree_ops);
174*4882a593Smuzhiyun }
175*4882a593Smuzhiyun 
176*4882a593Smuzhiyun /*
177*4882a593Smuzhiyun  * These modifications: insert, remove_init and remove; are serialized by the
178*4882a593Smuzhiyun  * module_mutex.
179*4882a593Smuzhiyun  */
mod_tree_insert(struct module * mod)180*4882a593Smuzhiyun static void mod_tree_insert(struct module *mod)
181*4882a593Smuzhiyun {
182*4882a593Smuzhiyun 	mod->core_layout.mtn.mod = mod;
183*4882a593Smuzhiyun 	mod->init_layout.mtn.mod = mod;
184*4882a593Smuzhiyun 
185*4882a593Smuzhiyun 	__mod_tree_insert(&mod->core_layout.mtn);
186*4882a593Smuzhiyun 	if (mod->init_layout.size)
187*4882a593Smuzhiyun 		__mod_tree_insert(&mod->init_layout.mtn);
188*4882a593Smuzhiyun }
189*4882a593Smuzhiyun 
mod_tree_remove_init(struct module * mod)190*4882a593Smuzhiyun static void mod_tree_remove_init(struct module *mod)
191*4882a593Smuzhiyun {
192*4882a593Smuzhiyun 	if (mod->init_layout.size)
193*4882a593Smuzhiyun 		__mod_tree_remove(&mod->init_layout.mtn);
194*4882a593Smuzhiyun }
195*4882a593Smuzhiyun 
mod_tree_remove(struct module * mod)196*4882a593Smuzhiyun static void mod_tree_remove(struct module *mod)
197*4882a593Smuzhiyun {
198*4882a593Smuzhiyun 	__mod_tree_remove(&mod->core_layout.mtn);
199*4882a593Smuzhiyun 	mod_tree_remove_init(mod);
200*4882a593Smuzhiyun }
201*4882a593Smuzhiyun 
mod_find(unsigned long addr)202*4882a593Smuzhiyun static struct module *mod_find(unsigned long addr)
203*4882a593Smuzhiyun {
204*4882a593Smuzhiyun 	struct latch_tree_node *ltn;
205*4882a593Smuzhiyun 
206*4882a593Smuzhiyun 	ltn = latch_tree_find((void *)addr, &mod_tree.root, &mod_tree_ops);
207*4882a593Smuzhiyun 	if (!ltn)
208*4882a593Smuzhiyun 		return NULL;
209*4882a593Smuzhiyun 
210*4882a593Smuzhiyun 	return container_of(ltn, struct mod_tree_node, node)->mod;
211*4882a593Smuzhiyun }
212*4882a593Smuzhiyun 
213*4882a593Smuzhiyun #else /* MODULES_TREE_LOOKUP */
214*4882a593Smuzhiyun 
215*4882a593Smuzhiyun static unsigned long module_addr_min = -1UL, module_addr_max = 0;
216*4882a593Smuzhiyun 
mod_tree_insert(struct module * mod)217*4882a593Smuzhiyun static void mod_tree_insert(struct module *mod) { }
mod_tree_remove_init(struct module * mod)218*4882a593Smuzhiyun static void mod_tree_remove_init(struct module *mod) { }
mod_tree_remove(struct module * mod)219*4882a593Smuzhiyun static void mod_tree_remove(struct module *mod) { }
220*4882a593Smuzhiyun 
mod_find(unsigned long addr)221*4882a593Smuzhiyun static struct module *mod_find(unsigned long addr)
222*4882a593Smuzhiyun {
223*4882a593Smuzhiyun 	struct module *mod;
224*4882a593Smuzhiyun 
225*4882a593Smuzhiyun 	list_for_each_entry_rcu(mod, &modules, list,
226*4882a593Smuzhiyun 				lockdep_is_held(&module_mutex)) {
227*4882a593Smuzhiyun 		if (within_module(addr, mod))
228*4882a593Smuzhiyun 			return mod;
229*4882a593Smuzhiyun 	}
230*4882a593Smuzhiyun 
231*4882a593Smuzhiyun 	return NULL;
232*4882a593Smuzhiyun }
233*4882a593Smuzhiyun 
234*4882a593Smuzhiyun #endif /* MODULES_TREE_LOOKUP */
235*4882a593Smuzhiyun 
236*4882a593Smuzhiyun /*
237*4882a593Smuzhiyun  * Bounds of module text, for speeding up __module_address.
238*4882a593Smuzhiyun  * Protected by module_mutex.
239*4882a593Smuzhiyun  */
__mod_update_bounds(void * base,unsigned int size)240*4882a593Smuzhiyun static void __mod_update_bounds(void *base, unsigned int size)
241*4882a593Smuzhiyun {
242*4882a593Smuzhiyun 	unsigned long min = (unsigned long)base;
243*4882a593Smuzhiyun 	unsigned long max = min + size;
244*4882a593Smuzhiyun 
245*4882a593Smuzhiyun 	if (min < module_addr_min)
246*4882a593Smuzhiyun 		module_addr_min = min;
247*4882a593Smuzhiyun 	if (max > module_addr_max)
248*4882a593Smuzhiyun 		module_addr_max = max;
249*4882a593Smuzhiyun }
250*4882a593Smuzhiyun 
mod_update_bounds(struct module * mod)251*4882a593Smuzhiyun static void mod_update_bounds(struct module *mod)
252*4882a593Smuzhiyun {
253*4882a593Smuzhiyun 	__mod_update_bounds(mod->core_layout.base, mod->core_layout.size);
254*4882a593Smuzhiyun 	if (mod->init_layout.size)
255*4882a593Smuzhiyun 		__mod_update_bounds(mod->init_layout.base, mod->init_layout.size);
256*4882a593Smuzhiyun }
257*4882a593Smuzhiyun 
258*4882a593Smuzhiyun #ifdef CONFIG_KGDB_KDB
259*4882a593Smuzhiyun struct list_head *kdb_modules = &modules; /* kdb needs the list of modules */
260*4882a593Smuzhiyun #endif /* CONFIG_KGDB_KDB */
261*4882a593Smuzhiyun 
module_assert_mutex(void)262*4882a593Smuzhiyun static void module_assert_mutex(void)
263*4882a593Smuzhiyun {
264*4882a593Smuzhiyun 	lockdep_assert_held(&module_mutex);
265*4882a593Smuzhiyun }
266*4882a593Smuzhiyun 
module_assert_mutex_or_preempt(void)267*4882a593Smuzhiyun static void module_assert_mutex_or_preempt(void)
268*4882a593Smuzhiyun {
269*4882a593Smuzhiyun #ifdef CONFIG_LOCKDEP
270*4882a593Smuzhiyun 	if (unlikely(!debug_locks))
271*4882a593Smuzhiyun 		return;
272*4882a593Smuzhiyun 
273*4882a593Smuzhiyun 	WARN_ON_ONCE(!rcu_read_lock_sched_held() &&
274*4882a593Smuzhiyun 		!lockdep_is_held(&module_mutex));
275*4882a593Smuzhiyun #endif
276*4882a593Smuzhiyun }
277*4882a593Smuzhiyun 
278*4882a593Smuzhiyun #ifdef CONFIG_MODULE_SIG
279*4882a593Smuzhiyun static bool sig_enforce = IS_ENABLED(CONFIG_MODULE_SIG_FORCE);
280*4882a593Smuzhiyun module_param(sig_enforce, bool_enable_only, 0644);
281*4882a593Smuzhiyun 
set_module_sig_enforced(void)282*4882a593Smuzhiyun void set_module_sig_enforced(void)
283*4882a593Smuzhiyun {
284*4882a593Smuzhiyun 	sig_enforce = true;
285*4882a593Smuzhiyun }
286*4882a593Smuzhiyun #else
287*4882a593Smuzhiyun #define sig_enforce false
288*4882a593Smuzhiyun #endif
289*4882a593Smuzhiyun 
290*4882a593Smuzhiyun /*
291*4882a593Smuzhiyun  * Export sig_enforce kernel cmdline parameter to allow other subsystems rely
292*4882a593Smuzhiyun  * on that instead of directly to CONFIG_MODULE_SIG_FORCE config.
293*4882a593Smuzhiyun  */
is_module_sig_enforced(void)294*4882a593Smuzhiyun bool is_module_sig_enforced(void)
295*4882a593Smuzhiyun {
296*4882a593Smuzhiyun 	return sig_enforce;
297*4882a593Smuzhiyun }
298*4882a593Smuzhiyun EXPORT_SYMBOL(is_module_sig_enforced);
299*4882a593Smuzhiyun 
300*4882a593Smuzhiyun /* Block module loading/unloading? */
301*4882a593Smuzhiyun int modules_disabled = 0;
302*4882a593Smuzhiyun core_param(nomodule, modules_disabled, bint, 0);
303*4882a593Smuzhiyun 
304*4882a593Smuzhiyun /* Waiting for a module to finish initializing? */
305*4882a593Smuzhiyun static DECLARE_WAIT_QUEUE_HEAD(module_wq);
306*4882a593Smuzhiyun 
307*4882a593Smuzhiyun static BLOCKING_NOTIFIER_HEAD(module_notify_list);
308*4882a593Smuzhiyun 
register_module_notifier(struct notifier_block * nb)309*4882a593Smuzhiyun int register_module_notifier(struct notifier_block *nb)
310*4882a593Smuzhiyun {
311*4882a593Smuzhiyun 	return blocking_notifier_chain_register(&module_notify_list, nb);
312*4882a593Smuzhiyun }
313*4882a593Smuzhiyun EXPORT_SYMBOL(register_module_notifier);
314*4882a593Smuzhiyun 
unregister_module_notifier(struct notifier_block * nb)315*4882a593Smuzhiyun int unregister_module_notifier(struct notifier_block *nb)
316*4882a593Smuzhiyun {
317*4882a593Smuzhiyun 	return blocking_notifier_chain_unregister(&module_notify_list, nb);
318*4882a593Smuzhiyun }
319*4882a593Smuzhiyun EXPORT_SYMBOL(unregister_module_notifier);
320*4882a593Smuzhiyun 
321*4882a593Smuzhiyun /*
322*4882a593Smuzhiyun  * We require a truly strong try_module_get(): 0 means success.
323*4882a593Smuzhiyun  * Otherwise an error is returned due to ongoing or failed
324*4882a593Smuzhiyun  * initialization etc.
325*4882a593Smuzhiyun  */
strong_try_module_get(struct module * mod)326*4882a593Smuzhiyun static inline int strong_try_module_get(struct module *mod)
327*4882a593Smuzhiyun {
328*4882a593Smuzhiyun 	BUG_ON(mod && mod->state == MODULE_STATE_UNFORMED);
329*4882a593Smuzhiyun 	if (mod && mod->state == MODULE_STATE_COMING)
330*4882a593Smuzhiyun 		return -EBUSY;
331*4882a593Smuzhiyun 	if (try_module_get(mod))
332*4882a593Smuzhiyun 		return 0;
333*4882a593Smuzhiyun 	else
334*4882a593Smuzhiyun 		return -ENOENT;
335*4882a593Smuzhiyun }
336*4882a593Smuzhiyun 
add_taint_module(struct module * mod,unsigned flag,enum lockdep_ok lockdep_ok)337*4882a593Smuzhiyun static inline void add_taint_module(struct module *mod, unsigned flag,
338*4882a593Smuzhiyun 				    enum lockdep_ok lockdep_ok)
339*4882a593Smuzhiyun {
340*4882a593Smuzhiyun 	add_taint(flag, lockdep_ok);
341*4882a593Smuzhiyun 	set_bit(flag, &mod->taints);
342*4882a593Smuzhiyun }
343*4882a593Smuzhiyun 
344*4882a593Smuzhiyun /*
345*4882a593Smuzhiyun  * A thread that wants to hold a reference to a module only while it
346*4882a593Smuzhiyun  * is running can call this to safely exit.  nfsd and lockd use this.
347*4882a593Smuzhiyun  */
__module_put_and_exit(struct module * mod,long code)348*4882a593Smuzhiyun void __noreturn __module_put_and_exit(struct module *mod, long code)
349*4882a593Smuzhiyun {
350*4882a593Smuzhiyun 	module_put(mod);
351*4882a593Smuzhiyun 	do_exit(code);
352*4882a593Smuzhiyun }
353*4882a593Smuzhiyun EXPORT_SYMBOL(__module_put_and_exit);
354*4882a593Smuzhiyun 
355*4882a593Smuzhiyun /* Find a module section: 0 means not found. */
find_sec(const struct load_info * info,const char * name)356*4882a593Smuzhiyun static unsigned int find_sec(const struct load_info *info, const char *name)
357*4882a593Smuzhiyun {
358*4882a593Smuzhiyun 	unsigned int i;
359*4882a593Smuzhiyun 
360*4882a593Smuzhiyun 	for (i = 1; i < info->hdr->e_shnum; i++) {
361*4882a593Smuzhiyun 		Elf_Shdr *shdr = &info->sechdrs[i];
362*4882a593Smuzhiyun 		/* Alloc bit cleared means "ignore it." */
363*4882a593Smuzhiyun 		if ((shdr->sh_flags & SHF_ALLOC)
364*4882a593Smuzhiyun 		    && strcmp(info->secstrings + shdr->sh_name, name) == 0)
365*4882a593Smuzhiyun 			return i;
366*4882a593Smuzhiyun 	}
367*4882a593Smuzhiyun 	return 0;
368*4882a593Smuzhiyun }
369*4882a593Smuzhiyun 
370*4882a593Smuzhiyun /* Find a module section, or NULL. */
section_addr(const struct load_info * info,const char * name)371*4882a593Smuzhiyun static void *section_addr(const struct load_info *info, const char *name)
372*4882a593Smuzhiyun {
373*4882a593Smuzhiyun 	/* Section 0 has sh_addr 0. */
374*4882a593Smuzhiyun 	return (void *)info->sechdrs[find_sec(info, name)].sh_addr;
375*4882a593Smuzhiyun }
376*4882a593Smuzhiyun 
377*4882a593Smuzhiyun /* Find a module section, or NULL.  Fill in number of "objects" in section. */
section_objs(const struct load_info * info,const char * name,size_t object_size,unsigned int * num)378*4882a593Smuzhiyun static void *section_objs(const struct load_info *info,
379*4882a593Smuzhiyun 			  const char *name,
380*4882a593Smuzhiyun 			  size_t object_size,
381*4882a593Smuzhiyun 			  unsigned int *num)
382*4882a593Smuzhiyun {
383*4882a593Smuzhiyun 	unsigned int sec = find_sec(info, name);
384*4882a593Smuzhiyun 
385*4882a593Smuzhiyun 	/* Section 0 has sh_addr 0 and sh_size 0. */
386*4882a593Smuzhiyun 	*num = info->sechdrs[sec].sh_size / object_size;
387*4882a593Smuzhiyun 	return (void *)info->sechdrs[sec].sh_addr;
388*4882a593Smuzhiyun }
389*4882a593Smuzhiyun 
390*4882a593Smuzhiyun /* Provided by the linker */
391*4882a593Smuzhiyun extern const struct kernel_symbol __start___ksymtab[];
392*4882a593Smuzhiyun extern const struct kernel_symbol __stop___ksymtab[];
393*4882a593Smuzhiyun extern const struct kernel_symbol __start___ksymtab_gpl[];
394*4882a593Smuzhiyun extern const struct kernel_symbol __stop___ksymtab_gpl[];
395*4882a593Smuzhiyun extern const struct kernel_symbol __start___ksymtab_gpl_future[];
396*4882a593Smuzhiyun extern const struct kernel_symbol __stop___ksymtab_gpl_future[];
397*4882a593Smuzhiyun extern const s32 __start___kcrctab[];
398*4882a593Smuzhiyun extern const s32 __start___kcrctab_gpl[];
399*4882a593Smuzhiyun extern const s32 __start___kcrctab_gpl_future[];
400*4882a593Smuzhiyun #ifdef CONFIG_UNUSED_SYMBOLS
401*4882a593Smuzhiyun extern const struct kernel_symbol __start___ksymtab_unused[];
402*4882a593Smuzhiyun extern const struct kernel_symbol __stop___ksymtab_unused[];
403*4882a593Smuzhiyun extern const struct kernel_symbol __start___ksymtab_unused_gpl[];
404*4882a593Smuzhiyun extern const struct kernel_symbol __stop___ksymtab_unused_gpl[];
405*4882a593Smuzhiyun extern const s32 __start___kcrctab_unused[];
406*4882a593Smuzhiyun extern const s32 __start___kcrctab_unused_gpl[];
407*4882a593Smuzhiyun #endif
408*4882a593Smuzhiyun 
409*4882a593Smuzhiyun #ifndef CONFIG_MODVERSIONS
410*4882a593Smuzhiyun #define symversion(base, idx) NULL
411*4882a593Smuzhiyun #else
412*4882a593Smuzhiyun #define symversion(base, idx) ((base != NULL) ? ((base) + (idx)) : NULL)
413*4882a593Smuzhiyun #endif
414*4882a593Smuzhiyun 
each_symbol_in_section(const struct symsearch * arr,unsigned int arrsize,struct module * owner,bool (* fn)(const struct symsearch * syms,struct module * owner,void * data),void * data)415*4882a593Smuzhiyun static bool each_symbol_in_section(const struct symsearch *arr,
416*4882a593Smuzhiyun 				   unsigned int arrsize,
417*4882a593Smuzhiyun 				   struct module *owner,
418*4882a593Smuzhiyun 				   bool (*fn)(const struct symsearch *syms,
419*4882a593Smuzhiyun 					      struct module *owner,
420*4882a593Smuzhiyun 					      void *data),
421*4882a593Smuzhiyun 				   void *data)
422*4882a593Smuzhiyun {
423*4882a593Smuzhiyun 	unsigned int j;
424*4882a593Smuzhiyun 
425*4882a593Smuzhiyun 	for (j = 0; j < arrsize; j++) {
426*4882a593Smuzhiyun 		if (fn(&arr[j], owner, data))
427*4882a593Smuzhiyun 			return true;
428*4882a593Smuzhiyun 	}
429*4882a593Smuzhiyun 
430*4882a593Smuzhiyun 	return false;
431*4882a593Smuzhiyun }
432*4882a593Smuzhiyun 
433*4882a593Smuzhiyun /* Returns true as soon as fn returns true, otherwise false. */
each_symbol_section(bool (* fn)(const struct symsearch * arr,struct module * owner,void * data),void * data)434*4882a593Smuzhiyun static bool each_symbol_section(bool (*fn)(const struct symsearch *arr,
435*4882a593Smuzhiyun 				    struct module *owner,
436*4882a593Smuzhiyun 				    void *data),
437*4882a593Smuzhiyun 			 void *data)
438*4882a593Smuzhiyun {
439*4882a593Smuzhiyun 	struct module *mod;
440*4882a593Smuzhiyun 	static const struct symsearch arr[] = {
441*4882a593Smuzhiyun 		{ __start___ksymtab, __stop___ksymtab, __start___kcrctab,
442*4882a593Smuzhiyun 		  NOT_GPL_ONLY, false },
443*4882a593Smuzhiyun 		{ __start___ksymtab_gpl, __stop___ksymtab_gpl,
444*4882a593Smuzhiyun 		  __start___kcrctab_gpl,
445*4882a593Smuzhiyun 		  GPL_ONLY, false },
446*4882a593Smuzhiyun 		{ __start___ksymtab_gpl_future, __stop___ksymtab_gpl_future,
447*4882a593Smuzhiyun 		  __start___kcrctab_gpl_future,
448*4882a593Smuzhiyun 		  WILL_BE_GPL_ONLY, false },
449*4882a593Smuzhiyun #ifdef CONFIG_UNUSED_SYMBOLS
450*4882a593Smuzhiyun 		{ __start___ksymtab_unused, __stop___ksymtab_unused,
451*4882a593Smuzhiyun 		  __start___kcrctab_unused,
452*4882a593Smuzhiyun 		  NOT_GPL_ONLY, true },
453*4882a593Smuzhiyun 		{ __start___ksymtab_unused_gpl, __stop___ksymtab_unused_gpl,
454*4882a593Smuzhiyun 		  __start___kcrctab_unused_gpl,
455*4882a593Smuzhiyun 		  GPL_ONLY, true },
456*4882a593Smuzhiyun #endif
457*4882a593Smuzhiyun 	};
458*4882a593Smuzhiyun 
459*4882a593Smuzhiyun 	module_assert_mutex_or_preempt();
460*4882a593Smuzhiyun 
461*4882a593Smuzhiyun 	if (each_symbol_in_section(arr, ARRAY_SIZE(arr), NULL, fn, data))
462*4882a593Smuzhiyun 		return true;
463*4882a593Smuzhiyun 
464*4882a593Smuzhiyun 	list_for_each_entry_rcu(mod, &modules, list,
465*4882a593Smuzhiyun 				lockdep_is_held(&module_mutex)) {
466*4882a593Smuzhiyun 		struct symsearch arr[] = {
467*4882a593Smuzhiyun 			{ mod->syms, mod->syms + mod->num_syms, mod->crcs,
468*4882a593Smuzhiyun 			  NOT_GPL_ONLY, false },
469*4882a593Smuzhiyun 			{ mod->gpl_syms, mod->gpl_syms + mod->num_gpl_syms,
470*4882a593Smuzhiyun 			  mod->gpl_crcs,
471*4882a593Smuzhiyun 			  GPL_ONLY, false },
472*4882a593Smuzhiyun 			{ mod->gpl_future_syms,
473*4882a593Smuzhiyun 			  mod->gpl_future_syms + mod->num_gpl_future_syms,
474*4882a593Smuzhiyun 			  mod->gpl_future_crcs,
475*4882a593Smuzhiyun 			  WILL_BE_GPL_ONLY, false },
476*4882a593Smuzhiyun #ifdef CONFIG_UNUSED_SYMBOLS
477*4882a593Smuzhiyun 			{ mod->unused_syms,
478*4882a593Smuzhiyun 			  mod->unused_syms + mod->num_unused_syms,
479*4882a593Smuzhiyun 			  mod->unused_crcs,
480*4882a593Smuzhiyun 			  NOT_GPL_ONLY, true },
481*4882a593Smuzhiyun 			{ mod->unused_gpl_syms,
482*4882a593Smuzhiyun 			  mod->unused_gpl_syms + mod->num_unused_gpl_syms,
483*4882a593Smuzhiyun 			  mod->unused_gpl_crcs,
484*4882a593Smuzhiyun 			  GPL_ONLY, true },
485*4882a593Smuzhiyun #endif
486*4882a593Smuzhiyun 		};
487*4882a593Smuzhiyun 
488*4882a593Smuzhiyun 		if (mod->state == MODULE_STATE_UNFORMED)
489*4882a593Smuzhiyun 			continue;
490*4882a593Smuzhiyun 
491*4882a593Smuzhiyun 		if (each_symbol_in_section(arr, ARRAY_SIZE(arr), mod, fn, data))
492*4882a593Smuzhiyun 			return true;
493*4882a593Smuzhiyun 	}
494*4882a593Smuzhiyun 	return false;
495*4882a593Smuzhiyun }
496*4882a593Smuzhiyun 
497*4882a593Smuzhiyun struct find_symbol_arg {
498*4882a593Smuzhiyun 	/* Input */
499*4882a593Smuzhiyun 	const char *name;
500*4882a593Smuzhiyun 	bool gplok;
501*4882a593Smuzhiyun 	bool warn;
502*4882a593Smuzhiyun 
503*4882a593Smuzhiyun 	/* Output */
504*4882a593Smuzhiyun 	struct module *owner;
505*4882a593Smuzhiyun 	const s32 *crc;
506*4882a593Smuzhiyun 	const struct kernel_symbol *sym;
507*4882a593Smuzhiyun 	enum mod_license license;
508*4882a593Smuzhiyun };
509*4882a593Smuzhiyun 
check_exported_symbol(const struct symsearch * syms,struct module * owner,unsigned int symnum,void * data)510*4882a593Smuzhiyun static bool check_exported_symbol(const struct symsearch *syms,
511*4882a593Smuzhiyun 				  struct module *owner,
512*4882a593Smuzhiyun 				  unsigned int symnum, void *data)
513*4882a593Smuzhiyun {
514*4882a593Smuzhiyun 	struct find_symbol_arg *fsa = data;
515*4882a593Smuzhiyun 
516*4882a593Smuzhiyun 	if (!fsa->gplok) {
517*4882a593Smuzhiyun 		if (syms->license == GPL_ONLY)
518*4882a593Smuzhiyun 			return false;
519*4882a593Smuzhiyun 		if (syms->license == WILL_BE_GPL_ONLY && fsa->warn) {
520*4882a593Smuzhiyun 			pr_warn("Symbol %s is being used by a non-GPL module, "
521*4882a593Smuzhiyun 				"which will not be allowed in the future\n",
522*4882a593Smuzhiyun 				fsa->name);
523*4882a593Smuzhiyun 		}
524*4882a593Smuzhiyun 	}
525*4882a593Smuzhiyun 
526*4882a593Smuzhiyun #ifdef CONFIG_UNUSED_SYMBOLS
527*4882a593Smuzhiyun 	if (syms->unused && fsa->warn) {
528*4882a593Smuzhiyun 		pr_warn("Symbol %s is marked as UNUSED, however this module is "
529*4882a593Smuzhiyun 			"using it.\n", fsa->name);
530*4882a593Smuzhiyun 		pr_warn("This symbol will go away in the future.\n");
531*4882a593Smuzhiyun 		pr_warn("Please evaluate if this is the right api to use and "
532*4882a593Smuzhiyun 			"if it really is, submit a report to the linux kernel "
533*4882a593Smuzhiyun 			"mailing list together with submitting your code for "
534*4882a593Smuzhiyun 			"inclusion.\n");
535*4882a593Smuzhiyun 	}
536*4882a593Smuzhiyun #endif
537*4882a593Smuzhiyun 
538*4882a593Smuzhiyun 	fsa->owner = owner;
539*4882a593Smuzhiyun 	fsa->crc = symversion(syms->crcs, symnum);
540*4882a593Smuzhiyun 	fsa->sym = &syms->start[symnum];
541*4882a593Smuzhiyun 	fsa->license = syms->license;
542*4882a593Smuzhiyun 	return true;
543*4882a593Smuzhiyun }
544*4882a593Smuzhiyun 
kernel_symbol_value(const struct kernel_symbol * sym)545*4882a593Smuzhiyun static unsigned long kernel_symbol_value(const struct kernel_symbol *sym)
546*4882a593Smuzhiyun {
547*4882a593Smuzhiyun #ifdef CONFIG_HAVE_ARCH_PREL32_RELOCATIONS
548*4882a593Smuzhiyun 	return (unsigned long)offset_to_ptr(&sym->value_offset);
549*4882a593Smuzhiyun #else
550*4882a593Smuzhiyun 	return sym->value;
551*4882a593Smuzhiyun #endif
552*4882a593Smuzhiyun }
553*4882a593Smuzhiyun 
kernel_symbol_name(const struct kernel_symbol * sym)554*4882a593Smuzhiyun static const char *kernel_symbol_name(const struct kernel_symbol *sym)
555*4882a593Smuzhiyun {
556*4882a593Smuzhiyun #ifdef CONFIG_HAVE_ARCH_PREL32_RELOCATIONS
557*4882a593Smuzhiyun 	return offset_to_ptr(&sym->name_offset);
558*4882a593Smuzhiyun #else
559*4882a593Smuzhiyun 	return sym->name;
560*4882a593Smuzhiyun #endif
561*4882a593Smuzhiyun }
562*4882a593Smuzhiyun 
kernel_symbol_namespace(const struct kernel_symbol * sym)563*4882a593Smuzhiyun static const char *kernel_symbol_namespace(const struct kernel_symbol *sym)
564*4882a593Smuzhiyun {
565*4882a593Smuzhiyun #ifdef CONFIG_HAVE_ARCH_PREL32_RELOCATIONS
566*4882a593Smuzhiyun 	if (!sym->namespace_offset)
567*4882a593Smuzhiyun 		return NULL;
568*4882a593Smuzhiyun 	return offset_to_ptr(&sym->namespace_offset);
569*4882a593Smuzhiyun #else
570*4882a593Smuzhiyun 	return sym->namespace;
571*4882a593Smuzhiyun #endif
572*4882a593Smuzhiyun }
573*4882a593Smuzhiyun 
cmp_name(const void * name,const void * sym)574*4882a593Smuzhiyun static int cmp_name(const void *name, const void *sym)
575*4882a593Smuzhiyun {
576*4882a593Smuzhiyun 	return strcmp(name, kernel_symbol_name(sym));
577*4882a593Smuzhiyun }
578*4882a593Smuzhiyun 
find_exported_symbol_in_section(const struct symsearch * syms,struct module * owner,void * data)579*4882a593Smuzhiyun static bool find_exported_symbol_in_section(const struct symsearch *syms,
580*4882a593Smuzhiyun 					    struct module *owner,
581*4882a593Smuzhiyun 					    void *data)
582*4882a593Smuzhiyun {
583*4882a593Smuzhiyun 	struct find_symbol_arg *fsa = data;
584*4882a593Smuzhiyun 	struct kernel_symbol *sym;
585*4882a593Smuzhiyun 
586*4882a593Smuzhiyun 	sym = bsearch(fsa->name, syms->start, syms->stop - syms->start,
587*4882a593Smuzhiyun 			sizeof(struct kernel_symbol), cmp_name);
588*4882a593Smuzhiyun 
589*4882a593Smuzhiyun 	if (sym != NULL && check_exported_symbol(syms, owner,
590*4882a593Smuzhiyun 						 sym - syms->start, data))
591*4882a593Smuzhiyun 		return true;
592*4882a593Smuzhiyun 
593*4882a593Smuzhiyun 	return false;
594*4882a593Smuzhiyun }
595*4882a593Smuzhiyun 
596*4882a593Smuzhiyun /* Find an exported symbol and return it, along with, (optional) crc and
597*4882a593Smuzhiyun  * (optional) module which owns it.  Needs preempt disabled or module_mutex. */
find_symbol(const char * name,struct module ** owner,const s32 ** crc,enum mod_license * license,bool gplok,bool warn)598*4882a593Smuzhiyun static const struct kernel_symbol *find_symbol(const char *name,
599*4882a593Smuzhiyun 					struct module **owner,
600*4882a593Smuzhiyun 					const s32 **crc,
601*4882a593Smuzhiyun 					enum mod_license *license,
602*4882a593Smuzhiyun 					bool gplok,
603*4882a593Smuzhiyun 					bool warn)
604*4882a593Smuzhiyun {
605*4882a593Smuzhiyun 	struct find_symbol_arg fsa;
606*4882a593Smuzhiyun 
607*4882a593Smuzhiyun 	fsa.name = name;
608*4882a593Smuzhiyun 	fsa.gplok = gplok;
609*4882a593Smuzhiyun 	fsa.warn = warn;
610*4882a593Smuzhiyun 
611*4882a593Smuzhiyun 	if (each_symbol_section(find_exported_symbol_in_section, &fsa)) {
612*4882a593Smuzhiyun 		if (owner)
613*4882a593Smuzhiyun 			*owner = fsa.owner;
614*4882a593Smuzhiyun 		if (crc)
615*4882a593Smuzhiyun 			*crc = fsa.crc;
616*4882a593Smuzhiyun 		if (license)
617*4882a593Smuzhiyun 			*license = fsa.license;
618*4882a593Smuzhiyun 		return fsa.sym;
619*4882a593Smuzhiyun 	}
620*4882a593Smuzhiyun 
621*4882a593Smuzhiyun 	pr_debug("Failed to find symbol %s\n", name);
622*4882a593Smuzhiyun 	return NULL;
623*4882a593Smuzhiyun }
624*4882a593Smuzhiyun 
625*4882a593Smuzhiyun /*
626*4882a593Smuzhiyun  * Search for module by name: must hold module_mutex (or preempt disabled
627*4882a593Smuzhiyun  * for read-only access).
628*4882a593Smuzhiyun  */
find_module_all(const char * name,size_t len,bool even_unformed)629*4882a593Smuzhiyun static struct module *find_module_all(const char *name, size_t len,
630*4882a593Smuzhiyun 				      bool even_unformed)
631*4882a593Smuzhiyun {
632*4882a593Smuzhiyun 	struct module *mod;
633*4882a593Smuzhiyun 
634*4882a593Smuzhiyun 	module_assert_mutex_or_preempt();
635*4882a593Smuzhiyun 
636*4882a593Smuzhiyun 	list_for_each_entry_rcu(mod, &modules, list,
637*4882a593Smuzhiyun 				lockdep_is_held(&module_mutex)) {
638*4882a593Smuzhiyun 		if (!even_unformed && mod->state == MODULE_STATE_UNFORMED)
639*4882a593Smuzhiyun 			continue;
640*4882a593Smuzhiyun 		if (strlen(mod->name) == len && !memcmp(mod->name, name, len))
641*4882a593Smuzhiyun 			return mod;
642*4882a593Smuzhiyun 	}
643*4882a593Smuzhiyun 	return NULL;
644*4882a593Smuzhiyun }
645*4882a593Smuzhiyun 
find_module(const char * name)646*4882a593Smuzhiyun struct module *find_module(const char *name)
647*4882a593Smuzhiyun {
648*4882a593Smuzhiyun 	module_assert_mutex();
649*4882a593Smuzhiyun 	return find_module_all(name, strlen(name), false);
650*4882a593Smuzhiyun }
651*4882a593Smuzhiyun 
652*4882a593Smuzhiyun #ifdef CONFIG_SMP
653*4882a593Smuzhiyun 
mod_percpu(struct module * mod)654*4882a593Smuzhiyun static inline void __percpu *mod_percpu(struct module *mod)
655*4882a593Smuzhiyun {
656*4882a593Smuzhiyun 	return mod->percpu;
657*4882a593Smuzhiyun }
658*4882a593Smuzhiyun 
percpu_modalloc(struct module * mod,struct load_info * info)659*4882a593Smuzhiyun static int percpu_modalloc(struct module *mod, struct load_info *info)
660*4882a593Smuzhiyun {
661*4882a593Smuzhiyun 	Elf_Shdr *pcpusec = &info->sechdrs[info->index.pcpu];
662*4882a593Smuzhiyun 	unsigned long align = pcpusec->sh_addralign;
663*4882a593Smuzhiyun 
664*4882a593Smuzhiyun 	if (!pcpusec->sh_size)
665*4882a593Smuzhiyun 		return 0;
666*4882a593Smuzhiyun 
667*4882a593Smuzhiyun 	if (align > PAGE_SIZE) {
668*4882a593Smuzhiyun 		pr_warn("%s: per-cpu alignment %li > %li\n",
669*4882a593Smuzhiyun 			mod->name, align, PAGE_SIZE);
670*4882a593Smuzhiyun 		align = PAGE_SIZE;
671*4882a593Smuzhiyun 	}
672*4882a593Smuzhiyun 
673*4882a593Smuzhiyun 	mod->percpu = __alloc_reserved_percpu(pcpusec->sh_size, align);
674*4882a593Smuzhiyun 	if (!mod->percpu) {
675*4882a593Smuzhiyun 		pr_warn("%s: Could not allocate %lu bytes percpu data\n",
676*4882a593Smuzhiyun 			mod->name, (unsigned long)pcpusec->sh_size);
677*4882a593Smuzhiyun 		return -ENOMEM;
678*4882a593Smuzhiyun 	}
679*4882a593Smuzhiyun 	mod->percpu_size = pcpusec->sh_size;
680*4882a593Smuzhiyun 	return 0;
681*4882a593Smuzhiyun }
682*4882a593Smuzhiyun 
percpu_modfree(struct module * mod)683*4882a593Smuzhiyun static void percpu_modfree(struct module *mod)
684*4882a593Smuzhiyun {
685*4882a593Smuzhiyun 	free_percpu(mod->percpu);
686*4882a593Smuzhiyun }
687*4882a593Smuzhiyun 
find_pcpusec(struct load_info * info)688*4882a593Smuzhiyun static unsigned int find_pcpusec(struct load_info *info)
689*4882a593Smuzhiyun {
690*4882a593Smuzhiyun 	return find_sec(info, ".data..percpu");
691*4882a593Smuzhiyun }
692*4882a593Smuzhiyun 
percpu_modcopy(struct module * mod,const void * from,unsigned long size)693*4882a593Smuzhiyun static void percpu_modcopy(struct module *mod,
694*4882a593Smuzhiyun 			   const void *from, unsigned long size)
695*4882a593Smuzhiyun {
696*4882a593Smuzhiyun 	int cpu;
697*4882a593Smuzhiyun 
698*4882a593Smuzhiyun 	for_each_possible_cpu(cpu)
699*4882a593Smuzhiyun 		memcpy(per_cpu_ptr(mod->percpu, cpu), from, size);
700*4882a593Smuzhiyun }
701*4882a593Smuzhiyun 
__is_module_percpu_address(unsigned long addr,unsigned long * can_addr)702*4882a593Smuzhiyun bool __is_module_percpu_address(unsigned long addr, unsigned long *can_addr)
703*4882a593Smuzhiyun {
704*4882a593Smuzhiyun 	struct module *mod;
705*4882a593Smuzhiyun 	unsigned int cpu;
706*4882a593Smuzhiyun 
707*4882a593Smuzhiyun 	preempt_disable();
708*4882a593Smuzhiyun 
709*4882a593Smuzhiyun 	list_for_each_entry_rcu(mod, &modules, list) {
710*4882a593Smuzhiyun 		if (mod->state == MODULE_STATE_UNFORMED)
711*4882a593Smuzhiyun 			continue;
712*4882a593Smuzhiyun 		if (!mod->percpu_size)
713*4882a593Smuzhiyun 			continue;
714*4882a593Smuzhiyun 		for_each_possible_cpu(cpu) {
715*4882a593Smuzhiyun 			void *start = per_cpu_ptr(mod->percpu, cpu);
716*4882a593Smuzhiyun 			void *va = (void *)addr;
717*4882a593Smuzhiyun 
718*4882a593Smuzhiyun 			if (va >= start && va < start + mod->percpu_size) {
719*4882a593Smuzhiyun 				if (can_addr) {
720*4882a593Smuzhiyun 					*can_addr = (unsigned long) (va - start);
721*4882a593Smuzhiyun 					*can_addr += (unsigned long)
722*4882a593Smuzhiyun 						per_cpu_ptr(mod->percpu,
723*4882a593Smuzhiyun 							    get_boot_cpu_id());
724*4882a593Smuzhiyun 				}
725*4882a593Smuzhiyun 				preempt_enable();
726*4882a593Smuzhiyun 				return true;
727*4882a593Smuzhiyun 			}
728*4882a593Smuzhiyun 		}
729*4882a593Smuzhiyun 	}
730*4882a593Smuzhiyun 
731*4882a593Smuzhiyun 	preempt_enable();
732*4882a593Smuzhiyun 	return false;
733*4882a593Smuzhiyun }
734*4882a593Smuzhiyun 
735*4882a593Smuzhiyun /**
736*4882a593Smuzhiyun  * is_module_percpu_address - test whether address is from module static percpu
737*4882a593Smuzhiyun  * @addr: address to test
738*4882a593Smuzhiyun  *
739*4882a593Smuzhiyun  * Test whether @addr belongs to module static percpu area.
740*4882a593Smuzhiyun  *
741*4882a593Smuzhiyun  * RETURNS:
742*4882a593Smuzhiyun  * %true if @addr is from module static percpu area
743*4882a593Smuzhiyun  */
is_module_percpu_address(unsigned long addr)744*4882a593Smuzhiyun bool is_module_percpu_address(unsigned long addr)
745*4882a593Smuzhiyun {
746*4882a593Smuzhiyun 	return __is_module_percpu_address(addr, NULL);
747*4882a593Smuzhiyun }
748*4882a593Smuzhiyun 
749*4882a593Smuzhiyun #else /* ... !CONFIG_SMP */
750*4882a593Smuzhiyun 
mod_percpu(struct module * mod)751*4882a593Smuzhiyun static inline void __percpu *mod_percpu(struct module *mod)
752*4882a593Smuzhiyun {
753*4882a593Smuzhiyun 	return NULL;
754*4882a593Smuzhiyun }
percpu_modalloc(struct module * mod,struct load_info * info)755*4882a593Smuzhiyun static int percpu_modalloc(struct module *mod, struct load_info *info)
756*4882a593Smuzhiyun {
757*4882a593Smuzhiyun 	/* UP modules shouldn't have this section: ENOMEM isn't quite right */
758*4882a593Smuzhiyun 	if (info->sechdrs[info->index.pcpu].sh_size != 0)
759*4882a593Smuzhiyun 		return -ENOMEM;
760*4882a593Smuzhiyun 	return 0;
761*4882a593Smuzhiyun }
percpu_modfree(struct module * mod)762*4882a593Smuzhiyun static inline void percpu_modfree(struct module *mod)
763*4882a593Smuzhiyun {
764*4882a593Smuzhiyun }
find_pcpusec(struct load_info * info)765*4882a593Smuzhiyun static unsigned int find_pcpusec(struct load_info *info)
766*4882a593Smuzhiyun {
767*4882a593Smuzhiyun 	return 0;
768*4882a593Smuzhiyun }
percpu_modcopy(struct module * mod,const void * from,unsigned long size)769*4882a593Smuzhiyun static inline void percpu_modcopy(struct module *mod,
770*4882a593Smuzhiyun 				  const void *from, unsigned long size)
771*4882a593Smuzhiyun {
772*4882a593Smuzhiyun 	/* pcpusec should be 0, and size of that section should be 0. */
773*4882a593Smuzhiyun 	BUG_ON(size != 0);
774*4882a593Smuzhiyun }
is_module_percpu_address(unsigned long addr)775*4882a593Smuzhiyun bool is_module_percpu_address(unsigned long addr)
776*4882a593Smuzhiyun {
777*4882a593Smuzhiyun 	return false;
778*4882a593Smuzhiyun }
779*4882a593Smuzhiyun 
__is_module_percpu_address(unsigned long addr,unsigned long * can_addr)780*4882a593Smuzhiyun bool __is_module_percpu_address(unsigned long addr, unsigned long *can_addr)
781*4882a593Smuzhiyun {
782*4882a593Smuzhiyun 	return false;
783*4882a593Smuzhiyun }
784*4882a593Smuzhiyun 
785*4882a593Smuzhiyun #endif /* CONFIG_SMP */
786*4882a593Smuzhiyun 
787*4882a593Smuzhiyun #define MODINFO_ATTR(field)	\
788*4882a593Smuzhiyun static void setup_modinfo_##field(struct module *mod, const char *s)  \
789*4882a593Smuzhiyun {                                                                     \
790*4882a593Smuzhiyun 	mod->field = kstrdup(s, GFP_KERNEL);                          \
791*4882a593Smuzhiyun }                                                                     \
792*4882a593Smuzhiyun static ssize_t show_modinfo_##field(struct module_attribute *mattr,   \
793*4882a593Smuzhiyun 			struct module_kobject *mk, char *buffer)      \
794*4882a593Smuzhiyun {                                                                     \
795*4882a593Smuzhiyun 	return scnprintf(buffer, PAGE_SIZE, "%s\n", mk->mod->field);  \
796*4882a593Smuzhiyun }                                                                     \
797*4882a593Smuzhiyun static int modinfo_##field##_exists(struct module *mod)               \
798*4882a593Smuzhiyun {                                                                     \
799*4882a593Smuzhiyun 	return mod->field != NULL;                                    \
800*4882a593Smuzhiyun }                                                                     \
801*4882a593Smuzhiyun static void free_modinfo_##field(struct module *mod)                  \
802*4882a593Smuzhiyun {                                                                     \
803*4882a593Smuzhiyun 	kfree(mod->field);                                            \
804*4882a593Smuzhiyun 	mod->field = NULL;                                            \
805*4882a593Smuzhiyun }                                                                     \
806*4882a593Smuzhiyun static struct module_attribute modinfo_##field = {                    \
807*4882a593Smuzhiyun 	.attr = { .name = __stringify(field), .mode = 0444 },         \
808*4882a593Smuzhiyun 	.show = show_modinfo_##field,                                 \
809*4882a593Smuzhiyun 	.setup = setup_modinfo_##field,                               \
810*4882a593Smuzhiyun 	.test = modinfo_##field##_exists,                             \
811*4882a593Smuzhiyun 	.free = free_modinfo_##field,                                 \
812*4882a593Smuzhiyun };
813*4882a593Smuzhiyun 
814*4882a593Smuzhiyun MODINFO_ATTR(version);
815*4882a593Smuzhiyun MODINFO_ATTR(srcversion);
816*4882a593Smuzhiyun MODINFO_ATTR(scmversion);
817*4882a593Smuzhiyun 
818*4882a593Smuzhiyun static char last_unloaded_module[MODULE_NAME_LEN+1];
819*4882a593Smuzhiyun 
820*4882a593Smuzhiyun #ifdef CONFIG_MODULE_UNLOAD
821*4882a593Smuzhiyun 
822*4882a593Smuzhiyun EXPORT_TRACEPOINT_SYMBOL(module_get);
823*4882a593Smuzhiyun 
824*4882a593Smuzhiyun /* MODULE_REF_BASE is the base reference count by kmodule loader. */
825*4882a593Smuzhiyun #define MODULE_REF_BASE	1
826*4882a593Smuzhiyun 
827*4882a593Smuzhiyun /* Init the unload section of the module. */
module_unload_init(struct module * mod)828*4882a593Smuzhiyun static int module_unload_init(struct module *mod)
829*4882a593Smuzhiyun {
830*4882a593Smuzhiyun 	/*
831*4882a593Smuzhiyun 	 * Initialize reference counter to MODULE_REF_BASE.
832*4882a593Smuzhiyun 	 * refcnt == 0 means module is going.
833*4882a593Smuzhiyun 	 */
834*4882a593Smuzhiyun 	atomic_set(&mod->refcnt, MODULE_REF_BASE);
835*4882a593Smuzhiyun 
836*4882a593Smuzhiyun 	INIT_LIST_HEAD(&mod->source_list);
837*4882a593Smuzhiyun 	INIT_LIST_HEAD(&mod->target_list);
838*4882a593Smuzhiyun 
839*4882a593Smuzhiyun 	/* Hold reference count during initialization. */
840*4882a593Smuzhiyun 	atomic_inc(&mod->refcnt);
841*4882a593Smuzhiyun 
842*4882a593Smuzhiyun 	return 0;
843*4882a593Smuzhiyun }
844*4882a593Smuzhiyun 
845*4882a593Smuzhiyun /* Does a already use b? */
already_uses(struct module * a,struct module * b)846*4882a593Smuzhiyun static int already_uses(struct module *a, struct module *b)
847*4882a593Smuzhiyun {
848*4882a593Smuzhiyun 	struct module_use *use;
849*4882a593Smuzhiyun 
850*4882a593Smuzhiyun 	list_for_each_entry(use, &b->source_list, source_list) {
851*4882a593Smuzhiyun 		if (use->source == a) {
852*4882a593Smuzhiyun 			pr_debug("%s uses %s!\n", a->name, b->name);
853*4882a593Smuzhiyun 			return 1;
854*4882a593Smuzhiyun 		}
855*4882a593Smuzhiyun 	}
856*4882a593Smuzhiyun 	pr_debug("%s does not use %s!\n", a->name, b->name);
857*4882a593Smuzhiyun 	return 0;
858*4882a593Smuzhiyun }
859*4882a593Smuzhiyun 
860*4882a593Smuzhiyun /*
861*4882a593Smuzhiyun  * Module a uses b
862*4882a593Smuzhiyun  *  - we add 'a' as a "source", 'b' as a "target" of module use
863*4882a593Smuzhiyun  *  - the module_use is added to the list of 'b' sources (so
864*4882a593Smuzhiyun  *    'b' can walk the list to see who sourced them), and of 'a'
865*4882a593Smuzhiyun  *    targets (so 'a' can see what modules it targets).
866*4882a593Smuzhiyun  */
add_module_usage(struct module * a,struct module * b)867*4882a593Smuzhiyun static int add_module_usage(struct module *a, struct module *b)
868*4882a593Smuzhiyun {
869*4882a593Smuzhiyun 	struct module_use *use;
870*4882a593Smuzhiyun 
871*4882a593Smuzhiyun 	pr_debug("Allocating new usage for %s.\n", a->name);
872*4882a593Smuzhiyun 	use = kmalloc(sizeof(*use), GFP_ATOMIC);
873*4882a593Smuzhiyun 	if (!use)
874*4882a593Smuzhiyun 		return -ENOMEM;
875*4882a593Smuzhiyun 
876*4882a593Smuzhiyun 	use->source = a;
877*4882a593Smuzhiyun 	use->target = b;
878*4882a593Smuzhiyun 	list_add(&use->source_list, &b->source_list);
879*4882a593Smuzhiyun 	list_add(&use->target_list, &a->target_list);
880*4882a593Smuzhiyun 	return 0;
881*4882a593Smuzhiyun }
882*4882a593Smuzhiyun 
883*4882a593Smuzhiyun /* Module a uses b: caller needs module_mutex() */
ref_module(struct module * a,struct module * b)884*4882a593Smuzhiyun static int ref_module(struct module *a, struct module *b)
885*4882a593Smuzhiyun {
886*4882a593Smuzhiyun 	int err;
887*4882a593Smuzhiyun 
888*4882a593Smuzhiyun 	if (b == NULL || already_uses(a, b))
889*4882a593Smuzhiyun 		return 0;
890*4882a593Smuzhiyun 
891*4882a593Smuzhiyun 	/* If module isn't available, we fail. */
892*4882a593Smuzhiyun 	err = strong_try_module_get(b);
893*4882a593Smuzhiyun 	if (err)
894*4882a593Smuzhiyun 		return err;
895*4882a593Smuzhiyun 
896*4882a593Smuzhiyun 	err = add_module_usage(a, b);
897*4882a593Smuzhiyun 	if (err) {
898*4882a593Smuzhiyun 		module_put(b);
899*4882a593Smuzhiyun 		return err;
900*4882a593Smuzhiyun 	}
901*4882a593Smuzhiyun 	return 0;
902*4882a593Smuzhiyun }
903*4882a593Smuzhiyun 
904*4882a593Smuzhiyun /* Clear the unload stuff of the module. */
module_unload_free(struct module * mod)905*4882a593Smuzhiyun static void module_unload_free(struct module *mod)
906*4882a593Smuzhiyun {
907*4882a593Smuzhiyun 	struct module_use *use, *tmp;
908*4882a593Smuzhiyun 
909*4882a593Smuzhiyun 	mutex_lock(&module_mutex);
910*4882a593Smuzhiyun 	list_for_each_entry_safe(use, tmp, &mod->target_list, target_list) {
911*4882a593Smuzhiyun 		struct module *i = use->target;
912*4882a593Smuzhiyun 		pr_debug("%s unusing %s\n", mod->name, i->name);
913*4882a593Smuzhiyun 		module_put(i);
914*4882a593Smuzhiyun 		list_del(&use->source_list);
915*4882a593Smuzhiyun 		list_del(&use->target_list);
916*4882a593Smuzhiyun 		kfree(use);
917*4882a593Smuzhiyun 	}
918*4882a593Smuzhiyun 	mutex_unlock(&module_mutex);
919*4882a593Smuzhiyun }
920*4882a593Smuzhiyun 
921*4882a593Smuzhiyun #ifdef CONFIG_MODULE_FORCE_UNLOAD
try_force_unload(unsigned int flags)922*4882a593Smuzhiyun static inline int try_force_unload(unsigned int flags)
923*4882a593Smuzhiyun {
924*4882a593Smuzhiyun 	int ret = (flags & O_TRUNC);
925*4882a593Smuzhiyun 	if (ret)
926*4882a593Smuzhiyun 		add_taint(TAINT_FORCED_RMMOD, LOCKDEP_NOW_UNRELIABLE);
927*4882a593Smuzhiyun 	return ret;
928*4882a593Smuzhiyun }
929*4882a593Smuzhiyun #else
try_force_unload(unsigned int flags)930*4882a593Smuzhiyun static inline int try_force_unload(unsigned int flags)
931*4882a593Smuzhiyun {
932*4882a593Smuzhiyun 	return 0;
933*4882a593Smuzhiyun }
934*4882a593Smuzhiyun #endif /* CONFIG_MODULE_FORCE_UNLOAD */
935*4882a593Smuzhiyun 
936*4882a593Smuzhiyun /* Try to release refcount of module, 0 means success. */
try_release_module_ref(struct module * mod)937*4882a593Smuzhiyun static int try_release_module_ref(struct module *mod)
938*4882a593Smuzhiyun {
939*4882a593Smuzhiyun 	int ret;
940*4882a593Smuzhiyun 
941*4882a593Smuzhiyun 	/* Try to decrement refcnt which we set at loading */
942*4882a593Smuzhiyun 	ret = atomic_sub_return(MODULE_REF_BASE, &mod->refcnt);
943*4882a593Smuzhiyun 	BUG_ON(ret < 0);
944*4882a593Smuzhiyun 	if (ret)
945*4882a593Smuzhiyun 		/* Someone can put this right now, recover with checking */
946*4882a593Smuzhiyun 		ret = atomic_add_unless(&mod->refcnt, MODULE_REF_BASE, 0);
947*4882a593Smuzhiyun 
948*4882a593Smuzhiyun 	return ret;
949*4882a593Smuzhiyun }
950*4882a593Smuzhiyun 
try_stop_module(struct module * mod,int flags,int * forced)951*4882a593Smuzhiyun static int try_stop_module(struct module *mod, int flags, int *forced)
952*4882a593Smuzhiyun {
953*4882a593Smuzhiyun 	/* If it's not unused, quit unless we're forcing. */
954*4882a593Smuzhiyun 	if (try_release_module_ref(mod) != 0) {
955*4882a593Smuzhiyun 		*forced = try_force_unload(flags);
956*4882a593Smuzhiyun 		if (!(*forced))
957*4882a593Smuzhiyun 			return -EWOULDBLOCK;
958*4882a593Smuzhiyun 	}
959*4882a593Smuzhiyun 
960*4882a593Smuzhiyun 	/* Mark it as dying. */
961*4882a593Smuzhiyun 	mod->state = MODULE_STATE_GOING;
962*4882a593Smuzhiyun 
963*4882a593Smuzhiyun 	return 0;
964*4882a593Smuzhiyun }
965*4882a593Smuzhiyun 
966*4882a593Smuzhiyun /**
967*4882a593Smuzhiyun  * module_refcount - return the refcount or -1 if unloading
968*4882a593Smuzhiyun  *
969*4882a593Smuzhiyun  * @mod:	the module we're checking
970*4882a593Smuzhiyun  *
971*4882a593Smuzhiyun  * Returns:
972*4882a593Smuzhiyun  *	-1 if the module is in the process of unloading
973*4882a593Smuzhiyun  *	otherwise the number of references in the kernel to the module
974*4882a593Smuzhiyun  */
module_refcount(struct module * mod)975*4882a593Smuzhiyun int module_refcount(struct module *mod)
976*4882a593Smuzhiyun {
977*4882a593Smuzhiyun 	return atomic_read(&mod->refcnt) - MODULE_REF_BASE;
978*4882a593Smuzhiyun }
979*4882a593Smuzhiyun EXPORT_SYMBOL(module_refcount);
980*4882a593Smuzhiyun 
981*4882a593Smuzhiyun /* This exists whether we can unload or not */
982*4882a593Smuzhiyun static void free_module(struct module *mod);
983*4882a593Smuzhiyun 
SYSCALL_DEFINE2(delete_module,const char __user *,name_user,unsigned int,flags)984*4882a593Smuzhiyun SYSCALL_DEFINE2(delete_module, const char __user *, name_user,
985*4882a593Smuzhiyun 		unsigned int, flags)
986*4882a593Smuzhiyun {
987*4882a593Smuzhiyun 	struct module *mod;
988*4882a593Smuzhiyun 	char name[MODULE_NAME_LEN];
989*4882a593Smuzhiyun 	int ret, forced = 0;
990*4882a593Smuzhiyun 
991*4882a593Smuzhiyun 	if (!capable(CAP_SYS_MODULE) || modules_disabled)
992*4882a593Smuzhiyun 		return -EPERM;
993*4882a593Smuzhiyun 
994*4882a593Smuzhiyun 	if (strncpy_from_user(name, name_user, MODULE_NAME_LEN-1) < 0)
995*4882a593Smuzhiyun 		return -EFAULT;
996*4882a593Smuzhiyun 	name[MODULE_NAME_LEN-1] = '\0';
997*4882a593Smuzhiyun 
998*4882a593Smuzhiyun 	audit_log_kern_module(name);
999*4882a593Smuzhiyun 
1000*4882a593Smuzhiyun 	if (mutex_lock_interruptible(&module_mutex) != 0)
1001*4882a593Smuzhiyun 		return -EINTR;
1002*4882a593Smuzhiyun 
1003*4882a593Smuzhiyun 	mod = find_module(name);
1004*4882a593Smuzhiyun 	if (!mod) {
1005*4882a593Smuzhiyun 		ret = -ENOENT;
1006*4882a593Smuzhiyun 		goto out;
1007*4882a593Smuzhiyun 	}
1008*4882a593Smuzhiyun 
1009*4882a593Smuzhiyun 	if (!list_empty(&mod->source_list)) {
1010*4882a593Smuzhiyun 		/* Other modules depend on us: get rid of them first. */
1011*4882a593Smuzhiyun 		ret = -EWOULDBLOCK;
1012*4882a593Smuzhiyun 		goto out;
1013*4882a593Smuzhiyun 	}
1014*4882a593Smuzhiyun 
1015*4882a593Smuzhiyun 	/* Doing init or already dying? */
1016*4882a593Smuzhiyun 	if (mod->state != MODULE_STATE_LIVE) {
1017*4882a593Smuzhiyun 		/* FIXME: if (force), slam module count damn the torpedoes */
1018*4882a593Smuzhiyun 		pr_debug("%s already dying\n", mod->name);
1019*4882a593Smuzhiyun 		ret = -EBUSY;
1020*4882a593Smuzhiyun 		goto out;
1021*4882a593Smuzhiyun 	}
1022*4882a593Smuzhiyun 
1023*4882a593Smuzhiyun 	/* If it has an init func, it must have an exit func to unload */
1024*4882a593Smuzhiyun 	if (mod->init && !mod->exit) {
1025*4882a593Smuzhiyun 		forced = try_force_unload(flags);
1026*4882a593Smuzhiyun 		if (!forced) {
1027*4882a593Smuzhiyun 			/* This module can't be removed */
1028*4882a593Smuzhiyun 			ret = -EBUSY;
1029*4882a593Smuzhiyun 			goto out;
1030*4882a593Smuzhiyun 		}
1031*4882a593Smuzhiyun 	}
1032*4882a593Smuzhiyun 
1033*4882a593Smuzhiyun 	/* Stop the machine so refcounts can't move and disable module. */
1034*4882a593Smuzhiyun 	ret = try_stop_module(mod, flags, &forced);
1035*4882a593Smuzhiyun 	if (ret != 0)
1036*4882a593Smuzhiyun 		goto out;
1037*4882a593Smuzhiyun 
1038*4882a593Smuzhiyun 	mutex_unlock(&module_mutex);
1039*4882a593Smuzhiyun 	/* Final destruction now no one is using it. */
1040*4882a593Smuzhiyun 	if (mod->exit != NULL)
1041*4882a593Smuzhiyun 		mod->exit();
1042*4882a593Smuzhiyun 	blocking_notifier_call_chain(&module_notify_list,
1043*4882a593Smuzhiyun 				     MODULE_STATE_GOING, mod);
1044*4882a593Smuzhiyun 	klp_module_going(mod);
1045*4882a593Smuzhiyun 	ftrace_release_mod(mod);
1046*4882a593Smuzhiyun 
1047*4882a593Smuzhiyun 	async_synchronize_full();
1048*4882a593Smuzhiyun 
1049*4882a593Smuzhiyun 	/* Store the name of the last unloaded module for diagnostic purposes */
1050*4882a593Smuzhiyun 	strlcpy(last_unloaded_module, mod->name, sizeof(last_unloaded_module));
1051*4882a593Smuzhiyun 
1052*4882a593Smuzhiyun 	free_module(mod);
1053*4882a593Smuzhiyun 	/* someone could wait for the module in add_unformed_module() */
1054*4882a593Smuzhiyun 	wake_up_all(&module_wq);
1055*4882a593Smuzhiyun 	return 0;
1056*4882a593Smuzhiyun out:
1057*4882a593Smuzhiyun 	mutex_unlock(&module_mutex);
1058*4882a593Smuzhiyun 	return ret;
1059*4882a593Smuzhiyun }
1060*4882a593Smuzhiyun 
print_unload_info(struct seq_file * m,struct module * mod)1061*4882a593Smuzhiyun static inline void print_unload_info(struct seq_file *m, struct module *mod)
1062*4882a593Smuzhiyun {
1063*4882a593Smuzhiyun 	struct module_use *use;
1064*4882a593Smuzhiyun 	int printed_something = 0;
1065*4882a593Smuzhiyun 
1066*4882a593Smuzhiyun 	seq_printf(m, " %i ", module_refcount(mod));
1067*4882a593Smuzhiyun 
1068*4882a593Smuzhiyun 	/*
1069*4882a593Smuzhiyun 	 * Always include a trailing , so userspace can differentiate
1070*4882a593Smuzhiyun 	 * between this and the old multi-field proc format.
1071*4882a593Smuzhiyun 	 */
1072*4882a593Smuzhiyun 	list_for_each_entry(use, &mod->source_list, source_list) {
1073*4882a593Smuzhiyun 		printed_something = 1;
1074*4882a593Smuzhiyun 		seq_printf(m, "%s,", use->source->name);
1075*4882a593Smuzhiyun 	}
1076*4882a593Smuzhiyun 
1077*4882a593Smuzhiyun 	if (mod->init != NULL && mod->exit == NULL) {
1078*4882a593Smuzhiyun 		printed_something = 1;
1079*4882a593Smuzhiyun 		seq_puts(m, "[permanent],");
1080*4882a593Smuzhiyun 	}
1081*4882a593Smuzhiyun 
1082*4882a593Smuzhiyun 	if (!printed_something)
1083*4882a593Smuzhiyun 		seq_puts(m, "-");
1084*4882a593Smuzhiyun }
1085*4882a593Smuzhiyun 
__symbol_put(const char * symbol)1086*4882a593Smuzhiyun void __symbol_put(const char *symbol)
1087*4882a593Smuzhiyun {
1088*4882a593Smuzhiyun 	struct module *owner;
1089*4882a593Smuzhiyun 
1090*4882a593Smuzhiyun 	preempt_disable();
1091*4882a593Smuzhiyun 	if (!find_symbol(symbol, &owner, NULL, NULL, true, false))
1092*4882a593Smuzhiyun 		BUG();
1093*4882a593Smuzhiyun 	module_put(owner);
1094*4882a593Smuzhiyun 	preempt_enable();
1095*4882a593Smuzhiyun }
1096*4882a593Smuzhiyun EXPORT_SYMBOL(__symbol_put);
1097*4882a593Smuzhiyun 
1098*4882a593Smuzhiyun /* Note this assumes addr is a function, which it currently always is. */
symbol_put_addr(void * addr)1099*4882a593Smuzhiyun void symbol_put_addr(void *addr)
1100*4882a593Smuzhiyun {
1101*4882a593Smuzhiyun 	struct module *modaddr;
1102*4882a593Smuzhiyun 	unsigned long a = (unsigned long)dereference_function_descriptor(addr);
1103*4882a593Smuzhiyun 
1104*4882a593Smuzhiyun 	if (core_kernel_text(a))
1105*4882a593Smuzhiyun 		return;
1106*4882a593Smuzhiyun 
1107*4882a593Smuzhiyun 	/*
1108*4882a593Smuzhiyun 	 * Even though we hold a reference on the module; we still need to
1109*4882a593Smuzhiyun 	 * disable preemption in order to safely traverse the data structure.
1110*4882a593Smuzhiyun 	 */
1111*4882a593Smuzhiyun 	preempt_disable();
1112*4882a593Smuzhiyun 	modaddr = __module_text_address(a);
1113*4882a593Smuzhiyun 	BUG_ON(!modaddr);
1114*4882a593Smuzhiyun 	module_put(modaddr);
1115*4882a593Smuzhiyun 	preempt_enable();
1116*4882a593Smuzhiyun }
1117*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(symbol_put_addr);
1118*4882a593Smuzhiyun 
show_refcnt(struct module_attribute * mattr,struct module_kobject * mk,char * buffer)1119*4882a593Smuzhiyun static ssize_t show_refcnt(struct module_attribute *mattr,
1120*4882a593Smuzhiyun 			   struct module_kobject *mk, char *buffer)
1121*4882a593Smuzhiyun {
1122*4882a593Smuzhiyun 	return sprintf(buffer, "%i\n", module_refcount(mk->mod));
1123*4882a593Smuzhiyun }
1124*4882a593Smuzhiyun 
1125*4882a593Smuzhiyun static struct module_attribute modinfo_refcnt =
1126*4882a593Smuzhiyun 	__ATTR(refcnt, 0444, show_refcnt, NULL);
1127*4882a593Smuzhiyun 
__module_get(struct module * module)1128*4882a593Smuzhiyun void __module_get(struct module *module)
1129*4882a593Smuzhiyun {
1130*4882a593Smuzhiyun 	if (module) {
1131*4882a593Smuzhiyun 		preempt_disable();
1132*4882a593Smuzhiyun 		atomic_inc(&module->refcnt);
1133*4882a593Smuzhiyun 		trace_module_get(module, _RET_IP_);
1134*4882a593Smuzhiyun 		preempt_enable();
1135*4882a593Smuzhiyun 	}
1136*4882a593Smuzhiyun }
1137*4882a593Smuzhiyun EXPORT_SYMBOL(__module_get);
1138*4882a593Smuzhiyun 
try_module_get(struct module * module)1139*4882a593Smuzhiyun bool try_module_get(struct module *module)
1140*4882a593Smuzhiyun {
1141*4882a593Smuzhiyun 	bool ret = true;
1142*4882a593Smuzhiyun 
1143*4882a593Smuzhiyun 	if (module) {
1144*4882a593Smuzhiyun 		preempt_disable();
1145*4882a593Smuzhiyun 		/* Note: here, we can fail to get a reference */
1146*4882a593Smuzhiyun 		if (likely(module_is_live(module) &&
1147*4882a593Smuzhiyun 			   atomic_inc_not_zero(&module->refcnt) != 0))
1148*4882a593Smuzhiyun 			trace_module_get(module, _RET_IP_);
1149*4882a593Smuzhiyun 		else
1150*4882a593Smuzhiyun 			ret = false;
1151*4882a593Smuzhiyun 
1152*4882a593Smuzhiyun 		preempt_enable();
1153*4882a593Smuzhiyun 	}
1154*4882a593Smuzhiyun 	return ret;
1155*4882a593Smuzhiyun }
1156*4882a593Smuzhiyun EXPORT_SYMBOL(try_module_get);
1157*4882a593Smuzhiyun 
module_put(struct module * module)1158*4882a593Smuzhiyun void module_put(struct module *module)
1159*4882a593Smuzhiyun {
1160*4882a593Smuzhiyun 	int ret;
1161*4882a593Smuzhiyun 
1162*4882a593Smuzhiyun 	if (module) {
1163*4882a593Smuzhiyun 		preempt_disable();
1164*4882a593Smuzhiyun 		ret = atomic_dec_if_positive(&module->refcnt);
1165*4882a593Smuzhiyun 		WARN_ON(ret < 0);	/* Failed to put refcount */
1166*4882a593Smuzhiyun 		trace_module_put(module, _RET_IP_);
1167*4882a593Smuzhiyun 		preempt_enable();
1168*4882a593Smuzhiyun 	}
1169*4882a593Smuzhiyun }
1170*4882a593Smuzhiyun EXPORT_SYMBOL(module_put);
1171*4882a593Smuzhiyun 
1172*4882a593Smuzhiyun #else /* !CONFIG_MODULE_UNLOAD */
print_unload_info(struct seq_file * m,struct module * mod)1173*4882a593Smuzhiyun static inline void print_unload_info(struct seq_file *m, struct module *mod)
1174*4882a593Smuzhiyun {
1175*4882a593Smuzhiyun 	/* We don't know the usage count, or what modules are using. */
1176*4882a593Smuzhiyun 	seq_puts(m, " - -");
1177*4882a593Smuzhiyun }
1178*4882a593Smuzhiyun 
module_unload_free(struct module * mod)1179*4882a593Smuzhiyun static inline void module_unload_free(struct module *mod)
1180*4882a593Smuzhiyun {
1181*4882a593Smuzhiyun }
1182*4882a593Smuzhiyun 
ref_module(struct module * a,struct module * b)1183*4882a593Smuzhiyun static int ref_module(struct module *a, struct module *b)
1184*4882a593Smuzhiyun {
1185*4882a593Smuzhiyun 	return strong_try_module_get(b);
1186*4882a593Smuzhiyun }
1187*4882a593Smuzhiyun 
module_unload_init(struct module * mod)1188*4882a593Smuzhiyun static inline int module_unload_init(struct module *mod)
1189*4882a593Smuzhiyun {
1190*4882a593Smuzhiyun 	return 0;
1191*4882a593Smuzhiyun }
1192*4882a593Smuzhiyun #endif /* CONFIG_MODULE_UNLOAD */
1193*4882a593Smuzhiyun 
module_flags_taint(struct module * mod,char * buf)1194*4882a593Smuzhiyun static size_t module_flags_taint(struct module *mod, char *buf)
1195*4882a593Smuzhiyun {
1196*4882a593Smuzhiyun 	size_t l = 0;
1197*4882a593Smuzhiyun 	int i;
1198*4882a593Smuzhiyun 
1199*4882a593Smuzhiyun 	for (i = 0; i < TAINT_FLAGS_COUNT; i++) {
1200*4882a593Smuzhiyun 		if (taint_flags[i].module && test_bit(i, &mod->taints))
1201*4882a593Smuzhiyun 			buf[l++] = taint_flags[i].c_true;
1202*4882a593Smuzhiyun 	}
1203*4882a593Smuzhiyun 
1204*4882a593Smuzhiyun 	return l;
1205*4882a593Smuzhiyun }
1206*4882a593Smuzhiyun 
show_initstate(struct module_attribute * mattr,struct module_kobject * mk,char * buffer)1207*4882a593Smuzhiyun static ssize_t show_initstate(struct module_attribute *mattr,
1208*4882a593Smuzhiyun 			      struct module_kobject *mk, char *buffer)
1209*4882a593Smuzhiyun {
1210*4882a593Smuzhiyun 	const char *state = "unknown";
1211*4882a593Smuzhiyun 
1212*4882a593Smuzhiyun 	switch (mk->mod->state) {
1213*4882a593Smuzhiyun 	case MODULE_STATE_LIVE:
1214*4882a593Smuzhiyun 		state = "live";
1215*4882a593Smuzhiyun 		break;
1216*4882a593Smuzhiyun 	case MODULE_STATE_COMING:
1217*4882a593Smuzhiyun 		state = "coming";
1218*4882a593Smuzhiyun 		break;
1219*4882a593Smuzhiyun 	case MODULE_STATE_GOING:
1220*4882a593Smuzhiyun 		state = "going";
1221*4882a593Smuzhiyun 		break;
1222*4882a593Smuzhiyun 	default:
1223*4882a593Smuzhiyun 		BUG();
1224*4882a593Smuzhiyun 	}
1225*4882a593Smuzhiyun 	return sprintf(buffer, "%s\n", state);
1226*4882a593Smuzhiyun }
1227*4882a593Smuzhiyun 
1228*4882a593Smuzhiyun static struct module_attribute modinfo_initstate =
1229*4882a593Smuzhiyun 	__ATTR(initstate, 0444, show_initstate, NULL);
1230*4882a593Smuzhiyun 
store_uevent(struct module_attribute * mattr,struct module_kobject * mk,const char * buffer,size_t count)1231*4882a593Smuzhiyun static ssize_t store_uevent(struct module_attribute *mattr,
1232*4882a593Smuzhiyun 			    struct module_kobject *mk,
1233*4882a593Smuzhiyun 			    const char *buffer, size_t count)
1234*4882a593Smuzhiyun {
1235*4882a593Smuzhiyun 	int rc;
1236*4882a593Smuzhiyun 
1237*4882a593Smuzhiyun 	rc = kobject_synth_uevent(&mk->kobj, buffer, count);
1238*4882a593Smuzhiyun 	return rc ? rc : count;
1239*4882a593Smuzhiyun }
1240*4882a593Smuzhiyun 
1241*4882a593Smuzhiyun struct module_attribute module_uevent =
1242*4882a593Smuzhiyun 	__ATTR(uevent, 0200, NULL, store_uevent);
1243*4882a593Smuzhiyun 
show_coresize(struct module_attribute * mattr,struct module_kobject * mk,char * buffer)1244*4882a593Smuzhiyun static ssize_t show_coresize(struct module_attribute *mattr,
1245*4882a593Smuzhiyun 			     struct module_kobject *mk, char *buffer)
1246*4882a593Smuzhiyun {
1247*4882a593Smuzhiyun 	return sprintf(buffer, "%u\n", mk->mod->core_layout.size);
1248*4882a593Smuzhiyun }
1249*4882a593Smuzhiyun 
1250*4882a593Smuzhiyun static struct module_attribute modinfo_coresize =
1251*4882a593Smuzhiyun 	__ATTR(coresize, 0444, show_coresize, NULL);
1252*4882a593Smuzhiyun 
show_initsize(struct module_attribute * mattr,struct module_kobject * mk,char * buffer)1253*4882a593Smuzhiyun static ssize_t show_initsize(struct module_attribute *mattr,
1254*4882a593Smuzhiyun 			     struct module_kobject *mk, char *buffer)
1255*4882a593Smuzhiyun {
1256*4882a593Smuzhiyun 	return sprintf(buffer, "%u\n", mk->mod->init_layout.size);
1257*4882a593Smuzhiyun }
1258*4882a593Smuzhiyun 
1259*4882a593Smuzhiyun static struct module_attribute modinfo_initsize =
1260*4882a593Smuzhiyun 	__ATTR(initsize, 0444, show_initsize, NULL);
1261*4882a593Smuzhiyun 
show_taint(struct module_attribute * mattr,struct module_kobject * mk,char * buffer)1262*4882a593Smuzhiyun static ssize_t show_taint(struct module_attribute *mattr,
1263*4882a593Smuzhiyun 			  struct module_kobject *mk, char *buffer)
1264*4882a593Smuzhiyun {
1265*4882a593Smuzhiyun 	size_t l;
1266*4882a593Smuzhiyun 
1267*4882a593Smuzhiyun 	l = module_flags_taint(mk->mod, buffer);
1268*4882a593Smuzhiyun 	buffer[l++] = '\n';
1269*4882a593Smuzhiyun 	return l;
1270*4882a593Smuzhiyun }
1271*4882a593Smuzhiyun 
1272*4882a593Smuzhiyun static struct module_attribute modinfo_taint =
1273*4882a593Smuzhiyun 	__ATTR(taint, 0444, show_taint, NULL);
1274*4882a593Smuzhiyun 
1275*4882a593Smuzhiyun static struct module_attribute *modinfo_attrs[] = {
1276*4882a593Smuzhiyun 	&module_uevent,
1277*4882a593Smuzhiyun 	&modinfo_version,
1278*4882a593Smuzhiyun 	&modinfo_srcversion,
1279*4882a593Smuzhiyun 	&modinfo_scmversion,
1280*4882a593Smuzhiyun 	&modinfo_initstate,
1281*4882a593Smuzhiyun 	&modinfo_coresize,
1282*4882a593Smuzhiyun 	&modinfo_initsize,
1283*4882a593Smuzhiyun 	&modinfo_taint,
1284*4882a593Smuzhiyun #ifdef CONFIG_MODULE_UNLOAD
1285*4882a593Smuzhiyun 	&modinfo_refcnt,
1286*4882a593Smuzhiyun #endif
1287*4882a593Smuzhiyun 	NULL,
1288*4882a593Smuzhiyun };
1289*4882a593Smuzhiyun 
1290*4882a593Smuzhiyun static const char vermagic[] = VERMAGIC_STRING;
1291*4882a593Smuzhiyun 
try_to_force_load(struct module * mod,const char * reason)1292*4882a593Smuzhiyun static int try_to_force_load(struct module *mod, const char *reason)
1293*4882a593Smuzhiyun {
1294*4882a593Smuzhiyun #ifdef CONFIG_MODULE_FORCE_LOAD
1295*4882a593Smuzhiyun 	if (!test_taint(TAINT_FORCED_MODULE))
1296*4882a593Smuzhiyun 		pr_warn("%s: %s: kernel tainted.\n", mod->name, reason);
1297*4882a593Smuzhiyun 	add_taint_module(mod, TAINT_FORCED_MODULE, LOCKDEP_NOW_UNRELIABLE);
1298*4882a593Smuzhiyun 	return 0;
1299*4882a593Smuzhiyun #else
1300*4882a593Smuzhiyun 	return -ENOEXEC;
1301*4882a593Smuzhiyun #endif
1302*4882a593Smuzhiyun }
1303*4882a593Smuzhiyun 
1304*4882a593Smuzhiyun #ifdef CONFIG_MODVERSIONS
1305*4882a593Smuzhiyun 
resolve_rel_crc(const s32 * crc)1306*4882a593Smuzhiyun static u32 resolve_rel_crc(const s32 *crc)
1307*4882a593Smuzhiyun {
1308*4882a593Smuzhiyun 	return *(u32 *)((void *)crc + *crc);
1309*4882a593Smuzhiyun }
1310*4882a593Smuzhiyun 
check_version(const struct load_info * info,const char * symname,struct module * mod,const s32 * crc)1311*4882a593Smuzhiyun static int check_version(const struct load_info *info,
1312*4882a593Smuzhiyun 			 const char *symname,
1313*4882a593Smuzhiyun 			 struct module *mod,
1314*4882a593Smuzhiyun 			 const s32 *crc)
1315*4882a593Smuzhiyun {
1316*4882a593Smuzhiyun 	Elf_Shdr *sechdrs = info->sechdrs;
1317*4882a593Smuzhiyun 	unsigned int versindex = info->index.vers;
1318*4882a593Smuzhiyun 	unsigned int i, num_versions;
1319*4882a593Smuzhiyun 	struct modversion_info *versions;
1320*4882a593Smuzhiyun 
1321*4882a593Smuzhiyun 	/* Exporting module didn't supply crcs?  OK, we're already tainted. */
1322*4882a593Smuzhiyun 	if (!crc)
1323*4882a593Smuzhiyun 		return 1;
1324*4882a593Smuzhiyun 
1325*4882a593Smuzhiyun 	/* No versions at all?  modprobe --force does this. */
1326*4882a593Smuzhiyun 	if (versindex == 0)
1327*4882a593Smuzhiyun 		return try_to_force_load(mod, symname) == 0;
1328*4882a593Smuzhiyun 
1329*4882a593Smuzhiyun 	versions = (void *) sechdrs[versindex].sh_addr;
1330*4882a593Smuzhiyun 	num_versions = sechdrs[versindex].sh_size
1331*4882a593Smuzhiyun 		/ sizeof(struct modversion_info);
1332*4882a593Smuzhiyun 
1333*4882a593Smuzhiyun 	for (i = 0; i < num_versions; i++) {
1334*4882a593Smuzhiyun 		u32 crcval;
1335*4882a593Smuzhiyun 
1336*4882a593Smuzhiyun 		if (strcmp(versions[i].name, symname) != 0)
1337*4882a593Smuzhiyun 			continue;
1338*4882a593Smuzhiyun 
1339*4882a593Smuzhiyun 		if (IS_ENABLED(CONFIG_MODULE_REL_CRCS))
1340*4882a593Smuzhiyun 			crcval = resolve_rel_crc(crc);
1341*4882a593Smuzhiyun 		else
1342*4882a593Smuzhiyun 			crcval = *crc;
1343*4882a593Smuzhiyun 		if (versions[i].crc == crcval)
1344*4882a593Smuzhiyun 			return 1;
1345*4882a593Smuzhiyun 		pr_debug("Found checksum %X vs module %lX\n",
1346*4882a593Smuzhiyun 			 crcval, versions[i].crc);
1347*4882a593Smuzhiyun 		goto bad_version;
1348*4882a593Smuzhiyun 	}
1349*4882a593Smuzhiyun 
1350*4882a593Smuzhiyun 	/* Broken toolchain. Warn once, then let it go.. */
1351*4882a593Smuzhiyun 	pr_warn_once("%s: no symbol version for %s\n", info->name, symname);
1352*4882a593Smuzhiyun 	return 1;
1353*4882a593Smuzhiyun 
1354*4882a593Smuzhiyun bad_version:
1355*4882a593Smuzhiyun 	pr_warn("%s: disagrees about version of symbol %s\n",
1356*4882a593Smuzhiyun 	       info->name, symname);
1357*4882a593Smuzhiyun 	return 0;
1358*4882a593Smuzhiyun }
1359*4882a593Smuzhiyun 
check_modstruct_version(const struct load_info * info,struct module * mod)1360*4882a593Smuzhiyun static inline int check_modstruct_version(const struct load_info *info,
1361*4882a593Smuzhiyun 					  struct module *mod)
1362*4882a593Smuzhiyun {
1363*4882a593Smuzhiyun 	const s32 *crc;
1364*4882a593Smuzhiyun 
1365*4882a593Smuzhiyun 	/*
1366*4882a593Smuzhiyun 	 * Since this should be found in kernel (which can't be removed), no
1367*4882a593Smuzhiyun 	 * locking is necessary -- use preempt_disable() to placate lockdep.
1368*4882a593Smuzhiyun 	 */
1369*4882a593Smuzhiyun 	preempt_disable();
1370*4882a593Smuzhiyun 	if (!find_symbol("module_layout", NULL, &crc, NULL, true, false)) {
1371*4882a593Smuzhiyun 		preempt_enable();
1372*4882a593Smuzhiyun 		BUG();
1373*4882a593Smuzhiyun 	}
1374*4882a593Smuzhiyun 	preempt_enable();
1375*4882a593Smuzhiyun 	return check_version(info, "module_layout", mod, crc);
1376*4882a593Smuzhiyun }
1377*4882a593Smuzhiyun 
1378*4882a593Smuzhiyun /* First part is kernel version, which we ignore if module has crcs. */
same_magic(const char * amagic,const char * bmagic,bool has_crcs)1379*4882a593Smuzhiyun static inline int same_magic(const char *amagic, const char *bmagic,
1380*4882a593Smuzhiyun 			     bool has_crcs)
1381*4882a593Smuzhiyun {
1382*4882a593Smuzhiyun 	if (has_crcs) {
1383*4882a593Smuzhiyun 		amagic += strcspn(amagic, " ");
1384*4882a593Smuzhiyun 		bmagic += strcspn(bmagic, " ");
1385*4882a593Smuzhiyun 	}
1386*4882a593Smuzhiyun 	return strcmp(amagic, bmagic) == 0;
1387*4882a593Smuzhiyun }
1388*4882a593Smuzhiyun #else
check_version(const struct load_info * info,const char * symname,struct module * mod,const s32 * crc)1389*4882a593Smuzhiyun static inline int check_version(const struct load_info *info,
1390*4882a593Smuzhiyun 				const char *symname,
1391*4882a593Smuzhiyun 				struct module *mod,
1392*4882a593Smuzhiyun 				const s32 *crc)
1393*4882a593Smuzhiyun {
1394*4882a593Smuzhiyun 	return 1;
1395*4882a593Smuzhiyun }
1396*4882a593Smuzhiyun 
check_modstruct_version(const struct load_info * info,struct module * mod)1397*4882a593Smuzhiyun static inline int check_modstruct_version(const struct load_info *info,
1398*4882a593Smuzhiyun 					  struct module *mod)
1399*4882a593Smuzhiyun {
1400*4882a593Smuzhiyun 	return 1;
1401*4882a593Smuzhiyun }
1402*4882a593Smuzhiyun 
same_magic(const char * amagic,const char * bmagic,bool has_crcs)1403*4882a593Smuzhiyun static inline int same_magic(const char *amagic, const char *bmagic,
1404*4882a593Smuzhiyun 			     bool has_crcs)
1405*4882a593Smuzhiyun {
1406*4882a593Smuzhiyun 	return strcmp(amagic, bmagic) == 0;
1407*4882a593Smuzhiyun }
1408*4882a593Smuzhiyun #endif /* CONFIG_MODVERSIONS */
1409*4882a593Smuzhiyun 
1410*4882a593Smuzhiyun static char *get_modinfo(const struct load_info *info, const char *tag);
1411*4882a593Smuzhiyun static char *get_next_modinfo(const struct load_info *info, const char *tag,
1412*4882a593Smuzhiyun 			      char *prev);
1413*4882a593Smuzhiyun 
verify_namespace_is_imported(const struct load_info * info,const struct kernel_symbol * sym,struct module * mod)1414*4882a593Smuzhiyun static int verify_namespace_is_imported(const struct load_info *info,
1415*4882a593Smuzhiyun 					const struct kernel_symbol *sym,
1416*4882a593Smuzhiyun 					struct module *mod)
1417*4882a593Smuzhiyun {
1418*4882a593Smuzhiyun 	const char *namespace;
1419*4882a593Smuzhiyun 	char *imported_namespace;
1420*4882a593Smuzhiyun 
1421*4882a593Smuzhiyun 	namespace = kernel_symbol_namespace(sym);
1422*4882a593Smuzhiyun 	if (namespace && namespace[0]) {
1423*4882a593Smuzhiyun 		imported_namespace = get_modinfo(info, "import_ns");
1424*4882a593Smuzhiyun 		while (imported_namespace) {
1425*4882a593Smuzhiyun 			if (strcmp(namespace, imported_namespace) == 0)
1426*4882a593Smuzhiyun 				return 0;
1427*4882a593Smuzhiyun 			imported_namespace = get_next_modinfo(
1428*4882a593Smuzhiyun 				info, "import_ns", imported_namespace);
1429*4882a593Smuzhiyun 		}
1430*4882a593Smuzhiyun #ifdef CONFIG_MODULE_ALLOW_MISSING_NAMESPACE_IMPORTS
1431*4882a593Smuzhiyun 		pr_warn(
1432*4882a593Smuzhiyun #else
1433*4882a593Smuzhiyun 		pr_err(
1434*4882a593Smuzhiyun #endif
1435*4882a593Smuzhiyun 			"%s: module uses symbol (%s) from namespace %s, but does not import it.\n",
1436*4882a593Smuzhiyun 			mod->name, kernel_symbol_name(sym), namespace);
1437*4882a593Smuzhiyun #ifndef CONFIG_MODULE_ALLOW_MISSING_NAMESPACE_IMPORTS
1438*4882a593Smuzhiyun 		return -EINVAL;
1439*4882a593Smuzhiyun #endif
1440*4882a593Smuzhiyun 	}
1441*4882a593Smuzhiyun 	return 0;
1442*4882a593Smuzhiyun }
1443*4882a593Smuzhiyun 
inherit_taint(struct module * mod,struct module * owner)1444*4882a593Smuzhiyun static bool inherit_taint(struct module *mod, struct module *owner)
1445*4882a593Smuzhiyun {
1446*4882a593Smuzhiyun 	if (!owner || !test_bit(TAINT_PROPRIETARY_MODULE, &owner->taints))
1447*4882a593Smuzhiyun 		return true;
1448*4882a593Smuzhiyun 
1449*4882a593Smuzhiyun 	if (mod->using_gplonly_symbols) {
1450*4882a593Smuzhiyun 		pr_err("%s: module using GPL-only symbols uses symbols from proprietary module %s.\n",
1451*4882a593Smuzhiyun 			mod->name, owner->name);
1452*4882a593Smuzhiyun 		return false;
1453*4882a593Smuzhiyun 	}
1454*4882a593Smuzhiyun 
1455*4882a593Smuzhiyun 	if (!test_bit(TAINT_PROPRIETARY_MODULE, &mod->taints)) {
1456*4882a593Smuzhiyun 		pr_warn("%s: module uses symbols from proprietary module %s, inheriting taint.\n",
1457*4882a593Smuzhiyun 			mod->name, owner->name);
1458*4882a593Smuzhiyun 		set_bit(TAINT_PROPRIETARY_MODULE, &mod->taints);
1459*4882a593Smuzhiyun 	}
1460*4882a593Smuzhiyun 	return true;
1461*4882a593Smuzhiyun }
1462*4882a593Smuzhiyun 
1463*4882a593Smuzhiyun /* Resolve a symbol for this module.  I.e. if we find one, record usage. */
resolve_symbol(struct module * mod,const struct load_info * info,const char * name,char ownername[])1464*4882a593Smuzhiyun static const struct kernel_symbol *resolve_symbol(struct module *mod,
1465*4882a593Smuzhiyun 						  const struct load_info *info,
1466*4882a593Smuzhiyun 						  const char *name,
1467*4882a593Smuzhiyun 						  char ownername[])
1468*4882a593Smuzhiyun {
1469*4882a593Smuzhiyun 	struct module *owner;
1470*4882a593Smuzhiyun 	const struct kernel_symbol *sym;
1471*4882a593Smuzhiyun 	const s32 *crc;
1472*4882a593Smuzhiyun 	enum mod_license license;
1473*4882a593Smuzhiyun 	int err;
1474*4882a593Smuzhiyun 
1475*4882a593Smuzhiyun 	/*
1476*4882a593Smuzhiyun 	 * The module_mutex should not be a heavily contended lock;
1477*4882a593Smuzhiyun 	 * if we get the occasional sleep here, we'll go an extra iteration
1478*4882a593Smuzhiyun 	 * in the wait_event_interruptible(), which is harmless.
1479*4882a593Smuzhiyun 	 */
1480*4882a593Smuzhiyun 	sched_annotate_sleep();
1481*4882a593Smuzhiyun 	mutex_lock(&module_mutex);
1482*4882a593Smuzhiyun 	sym = find_symbol(name, &owner, &crc, &license,
1483*4882a593Smuzhiyun 			  !(mod->taints & (1 << TAINT_PROPRIETARY_MODULE)), true);
1484*4882a593Smuzhiyun 	if (!sym)
1485*4882a593Smuzhiyun 		goto unlock;
1486*4882a593Smuzhiyun 
1487*4882a593Smuzhiyun 	if (license == GPL_ONLY)
1488*4882a593Smuzhiyun 		mod->using_gplonly_symbols = true;
1489*4882a593Smuzhiyun 
1490*4882a593Smuzhiyun 	if (!inherit_taint(mod, owner)) {
1491*4882a593Smuzhiyun 		sym = NULL;
1492*4882a593Smuzhiyun 		goto getname;
1493*4882a593Smuzhiyun 	}
1494*4882a593Smuzhiyun 
1495*4882a593Smuzhiyun 	if (!check_version(info, name, mod, crc)) {
1496*4882a593Smuzhiyun 		sym = ERR_PTR(-EINVAL);
1497*4882a593Smuzhiyun 		goto getname;
1498*4882a593Smuzhiyun 	}
1499*4882a593Smuzhiyun 
1500*4882a593Smuzhiyun 	err = verify_namespace_is_imported(info, sym, mod);
1501*4882a593Smuzhiyun 	if (err) {
1502*4882a593Smuzhiyun 		sym = ERR_PTR(err);
1503*4882a593Smuzhiyun 		goto getname;
1504*4882a593Smuzhiyun 	}
1505*4882a593Smuzhiyun 
1506*4882a593Smuzhiyun 	err = ref_module(mod, owner);
1507*4882a593Smuzhiyun 	if (err) {
1508*4882a593Smuzhiyun 		sym = ERR_PTR(err);
1509*4882a593Smuzhiyun 		goto getname;
1510*4882a593Smuzhiyun 	}
1511*4882a593Smuzhiyun 
1512*4882a593Smuzhiyun getname:
1513*4882a593Smuzhiyun 	/* We must make copy under the lock if we failed to get ref. */
1514*4882a593Smuzhiyun 	strncpy(ownername, module_name(owner), MODULE_NAME_LEN);
1515*4882a593Smuzhiyun unlock:
1516*4882a593Smuzhiyun 	mutex_unlock(&module_mutex);
1517*4882a593Smuzhiyun 	return sym;
1518*4882a593Smuzhiyun }
1519*4882a593Smuzhiyun 
1520*4882a593Smuzhiyun static const struct kernel_symbol *
resolve_symbol_wait(struct module * mod,const struct load_info * info,const char * name)1521*4882a593Smuzhiyun resolve_symbol_wait(struct module *mod,
1522*4882a593Smuzhiyun 		    const struct load_info *info,
1523*4882a593Smuzhiyun 		    const char *name)
1524*4882a593Smuzhiyun {
1525*4882a593Smuzhiyun 	const struct kernel_symbol *ksym;
1526*4882a593Smuzhiyun 	char owner[MODULE_NAME_LEN];
1527*4882a593Smuzhiyun 
1528*4882a593Smuzhiyun 	if (wait_event_interruptible_timeout(module_wq,
1529*4882a593Smuzhiyun 			!IS_ERR(ksym = resolve_symbol(mod, info, name, owner))
1530*4882a593Smuzhiyun 			|| PTR_ERR(ksym) != -EBUSY,
1531*4882a593Smuzhiyun 					     30 * HZ) <= 0) {
1532*4882a593Smuzhiyun 		pr_warn("%s: gave up waiting for init of module %s.\n",
1533*4882a593Smuzhiyun 			mod->name, owner);
1534*4882a593Smuzhiyun 	}
1535*4882a593Smuzhiyun 	return ksym;
1536*4882a593Smuzhiyun }
1537*4882a593Smuzhiyun 
1538*4882a593Smuzhiyun /*
1539*4882a593Smuzhiyun  * /sys/module/foo/sections stuff
1540*4882a593Smuzhiyun  * J. Corbet <corbet@lwn.net>
1541*4882a593Smuzhiyun  */
1542*4882a593Smuzhiyun #ifdef CONFIG_SYSFS
1543*4882a593Smuzhiyun 
1544*4882a593Smuzhiyun #ifdef CONFIG_KALLSYMS
sect_empty(const Elf_Shdr * sect)1545*4882a593Smuzhiyun static inline bool sect_empty(const Elf_Shdr *sect)
1546*4882a593Smuzhiyun {
1547*4882a593Smuzhiyun 	return !(sect->sh_flags & SHF_ALLOC) || sect->sh_size == 0;
1548*4882a593Smuzhiyun }
1549*4882a593Smuzhiyun 
1550*4882a593Smuzhiyun struct module_sect_attr {
1551*4882a593Smuzhiyun 	struct bin_attribute battr;
1552*4882a593Smuzhiyun 	unsigned long address;
1553*4882a593Smuzhiyun };
1554*4882a593Smuzhiyun 
1555*4882a593Smuzhiyun struct module_sect_attrs {
1556*4882a593Smuzhiyun 	struct attribute_group grp;
1557*4882a593Smuzhiyun 	unsigned int nsections;
1558*4882a593Smuzhiyun 	struct module_sect_attr attrs[];
1559*4882a593Smuzhiyun };
1560*4882a593Smuzhiyun 
1561*4882a593Smuzhiyun #define MODULE_SECT_READ_SIZE (3 /* "0x", "\n" */ + (BITS_PER_LONG / 4))
module_sect_read(struct file * file,struct kobject * kobj,struct bin_attribute * battr,char * buf,loff_t pos,size_t count)1562*4882a593Smuzhiyun static ssize_t module_sect_read(struct file *file, struct kobject *kobj,
1563*4882a593Smuzhiyun 				struct bin_attribute *battr,
1564*4882a593Smuzhiyun 				char *buf, loff_t pos, size_t count)
1565*4882a593Smuzhiyun {
1566*4882a593Smuzhiyun 	struct module_sect_attr *sattr =
1567*4882a593Smuzhiyun 		container_of(battr, struct module_sect_attr, battr);
1568*4882a593Smuzhiyun 	char bounce[MODULE_SECT_READ_SIZE + 1];
1569*4882a593Smuzhiyun 	size_t wrote;
1570*4882a593Smuzhiyun 
1571*4882a593Smuzhiyun 	if (pos != 0)
1572*4882a593Smuzhiyun 		return -EINVAL;
1573*4882a593Smuzhiyun 
1574*4882a593Smuzhiyun 	/*
1575*4882a593Smuzhiyun 	 * Since we're a binary read handler, we must account for the
1576*4882a593Smuzhiyun 	 * trailing NUL byte that sprintf will write: if "buf" is
1577*4882a593Smuzhiyun 	 * too small to hold the NUL, or the NUL is exactly the last
1578*4882a593Smuzhiyun 	 * byte, the read will look like it got truncated by one byte.
1579*4882a593Smuzhiyun 	 * Since there is no way to ask sprintf nicely to not write
1580*4882a593Smuzhiyun 	 * the NUL, we have to use a bounce buffer.
1581*4882a593Smuzhiyun 	 */
1582*4882a593Smuzhiyun 	wrote = scnprintf(bounce, sizeof(bounce), "0x%px\n",
1583*4882a593Smuzhiyun 			 kallsyms_show_value(file->f_cred)
1584*4882a593Smuzhiyun 				? (void *)sattr->address : NULL);
1585*4882a593Smuzhiyun 	count = min(count, wrote);
1586*4882a593Smuzhiyun 	memcpy(buf, bounce, count);
1587*4882a593Smuzhiyun 
1588*4882a593Smuzhiyun 	return count;
1589*4882a593Smuzhiyun }
1590*4882a593Smuzhiyun 
free_sect_attrs(struct module_sect_attrs * sect_attrs)1591*4882a593Smuzhiyun static void free_sect_attrs(struct module_sect_attrs *sect_attrs)
1592*4882a593Smuzhiyun {
1593*4882a593Smuzhiyun 	unsigned int section;
1594*4882a593Smuzhiyun 
1595*4882a593Smuzhiyun 	for (section = 0; section < sect_attrs->nsections; section++)
1596*4882a593Smuzhiyun 		kfree(sect_attrs->attrs[section].battr.attr.name);
1597*4882a593Smuzhiyun 	kfree(sect_attrs);
1598*4882a593Smuzhiyun }
1599*4882a593Smuzhiyun 
add_sect_attrs(struct module * mod,const struct load_info * info)1600*4882a593Smuzhiyun static void add_sect_attrs(struct module *mod, const struct load_info *info)
1601*4882a593Smuzhiyun {
1602*4882a593Smuzhiyun 	unsigned int nloaded = 0, i, size[2];
1603*4882a593Smuzhiyun 	struct module_sect_attrs *sect_attrs;
1604*4882a593Smuzhiyun 	struct module_sect_attr *sattr;
1605*4882a593Smuzhiyun 	struct bin_attribute **gattr;
1606*4882a593Smuzhiyun 
1607*4882a593Smuzhiyun 	/* Count loaded sections and allocate structures */
1608*4882a593Smuzhiyun 	for (i = 0; i < info->hdr->e_shnum; i++)
1609*4882a593Smuzhiyun 		if (!sect_empty(&info->sechdrs[i]))
1610*4882a593Smuzhiyun 			nloaded++;
1611*4882a593Smuzhiyun 	size[0] = ALIGN(struct_size(sect_attrs, attrs, nloaded),
1612*4882a593Smuzhiyun 			sizeof(sect_attrs->grp.bin_attrs[0]));
1613*4882a593Smuzhiyun 	size[1] = (nloaded + 1) * sizeof(sect_attrs->grp.bin_attrs[0]);
1614*4882a593Smuzhiyun 	sect_attrs = kzalloc(size[0] + size[1], GFP_KERNEL);
1615*4882a593Smuzhiyun 	if (sect_attrs == NULL)
1616*4882a593Smuzhiyun 		return;
1617*4882a593Smuzhiyun 
1618*4882a593Smuzhiyun 	/* Setup section attributes. */
1619*4882a593Smuzhiyun 	sect_attrs->grp.name = "sections";
1620*4882a593Smuzhiyun 	sect_attrs->grp.bin_attrs = (void *)sect_attrs + size[0];
1621*4882a593Smuzhiyun 
1622*4882a593Smuzhiyun 	sect_attrs->nsections = 0;
1623*4882a593Smuzhiyun 	sattr = &sect_attrs->attrs[0];
1624*4882a593Smuzhiyun 	gattr = &sect_attrs->grp.bin_attrs[0];
1625*4882a593Smuzhiyun 	for (i = 0; i < info->hdr->e_shnum; i++) {
1626*4882a593Smuzhiyun 		Elf_Shdr *sec = &info->sechdrs[i];
1627*4882a593Smuzhiyun 		if (sect_empty(sec))
1628*4882a593Smuzhiyun 			continue;
1629*4882a593Smuzhiyun 		sysfs_bin_attr_init(&sattr->battr);
1630*4882a593Smuzhiyun 		sattr->address = sec->sh_addr;
1631*4882a593Smuzhiyun 		sattr->battr.attr.name =
1632*4882a593Smuzhiyun 			kstrdup(info->secstrings + sec->sh_name, GFP_KERNEL);
1633*4882a593Smuzhiyun 		if (sattr->battr.attr.name == NULL)
1634*4882a593Smuzhiyun 			goto out;
1635*4882a593Smuzhiyun 		sect_attrs->nsections++;
1636*4882a593Smuzhiyun 		sattr->battr.read = module_sect_read;
1637*4882a593Smuzhiyun 		sattr->battr.size = MODULE_SECT_READ_SIZE;
1638*4882a593Smuzhiyun 		sattr->battr.attr.mode = 0400;
1639*4882a593Smuzhiyun 		*(gattr++) = &(sattr++)->battr;
1640*4882a593Smuzhiyun 	}
1641*4882a593Smuzhiyun 	*gattr = NULL;
1642*4882a593Smuzhiyun 
1643*4882a593Smuzhiyun 	if (sysfs_create_group(&mod->mkobj.kobj, &sect_attrs->grp))
1644*4882a593Smuzhiyun 		goto out;
1645*4882a593Smuzhiyun 
1646*4882a593Smuzhiyun 	mod->sect_attrs = sect_attrs;
1647*4882a593Smuzhiyun 	return;
1648*4882a593Smuzhiyun   out:
1649*4882a593Smuzhiyun 	free_sect_attrs(sect_attrs);
1650*4882a593Smuzhiyun }
1651*4882a593Smuzhiyun 
remove_sect_attrs(struct module * mod)1652*4882a593Smuzhiyun static void remove_sect_attrs(struct module *mod)
1653*4882a593Smuzhiyun {
1654*4882a593Smuzhiyun 	if (mod->sect_attrs) {
1655*4882a593Smuzhiyun 		sysfs_remove_group(&mod->mkobj.kobj,
1656*4882a593Smuzhiyun 				   &mod->sect_attrs->grp);
1657*4882a593Smuzhiyun 		/* We are positive that no one is using any sect attrs
1658*4882a593Smuzhiyun 		 * at this point.  Deallocate immediately. */
1659*4882a593Smuzhiyun 		free_sect_attrs(mod->sect_attrs);
1660*4882a593Smuzhiyun 		mod->sect_attrs = NULL;
1661*4882a593Smuzhiyun 	}
1662*4882a593Smuzhiyun }
1663*4882a593Smuzhiyun 
1664*4882a593Smuzhiyun /*
1665*4882a593Smuzhiyun  * /sys/module/foo/notes/.section.name gives contents of SHT_NOTE sections.
1666*4882a593Smuzhiyun  */
1667*4882a593Smuzhiyun 
1668*4882a593Smuzhiyun struct module_notes_attrs {
1669*4882a593Smuzhiyun 	struct kobject *dir;
1670*4882a593Smuzhiyun 	unsigned int notes;
1671*4882a593Smuzhiyun 	struct bin_attribute attrs[];
1672*4882a593Smuzhiyun };
1673*4882a593Smuzhiyun 
module_notes_read(struct file * filp,struct kobject * kobj,struct bin_attribute * bin_attr,char * buf,loff_t pos,size_t count)1674*4882a593Smuzhiyun static ssize_t module_notes_read(struct file *filp, struct kobject *kobj,
1675*4882a593Smuzhiyun 				 struct bin_attribute *bin_attr,
1676*4882a593Smuzhiyun 				 char *buf, loff_t pos, size_t count)
1677*4882a593Smuzhiyun {
1678*4882a593Smuzhiyun 	/*
1679*4882a593Smuzhiyun 	 * The caller checked the pos and count against our size.
1680*4882a593Smuzhiyun 	 */
1681*4882a593Smuzhiyun 	memcpy(buf, bin_attr->private + pos, count);
1682*4882a593Smuzhiyun 	return count;
1683*4882a593Smuzhiyun }
1684*4882a593Smuzhiyun 
free_notes_attrs(struct module_notes_attrs * notes_attrs,unsigned int i)1685*4882a593Smuzhiyun static void free_notes_attrs(struct module_notes_attrs *notes_attrs,
1686*4882a593Smuzhiyun 			     unsigned int i)
1687*4882a593Smuzhiyun {
1688*4882a593Smuzhiyun 	if (notes_attrs->dir) {
1689*4882a593Smuzhiyun 		while (i-- > 0)
1690*4882a593Smuzhiyun 			sysfs_remove_bin_file(notes_attrs->dir,
1691*4882a593Smuzhiyun 					      &notes_attrs->attrs[i]);
1692*4882a593Smuzhiyun 		kobject_put(notes_attrs->dir);
1693*4882a593Smuzhiyun 	}
1694*4882a593Smuzhiyun 	kfree(notes_attrs);
1695*4882a593Smuzhiyun }
1696*4882a593Smuzhiyun 
add_notes_attrs(struct module * mod,const struct load_info * info)1697*4882a593Smuzhiyun static void add_notes_attrs(struct module *mod, const struct load_info *info)
1698*4882a593Smuzhiyun {
1699*4882a593Smuzhiyun 	unsigned int notes, loaded, i;
1700*4882a593Smuzhiyun 	struct module_notes_attrs *notes_attrs;
1701*4882a593Smuzhiyun 	struct bin_attribute *nattr;
1702*4882a593Smuzhiyun 
1703*4882a593Smuzhiyun 	/* failed to create section attributes, so can't create notes */
1704*4882a593Smuzhiyun 	if (!mod->sect_attrs)
1705*4882a593Smuzhiyun 		return;
1706*4882a593Smuzhiyun 
1707*4882a593Smuzhiyun 	/* Count notes sections and allocate structures.  */
1708*4882a593Smuzhiyun 	notes = 0;
1709*4882a593Smuzhiyun 	for (i = 0; i < info->hdr->e_shnum; i++)
1710*4882a593Smuzhiyun 		if (!sect_empty(&info->sechdrs[i]) &&
1711*4882a593Smuzhiyun 		    (info->sechdrs[i].sh_type == SHT_NOTE))
1712*4882a593Smuzhiyun 			++notes;
1713*4882a593Smuzhiyun 
1714*4882a593Smuzhiyun 	if (notes == 0)
1715*4882a593Smuzhiyun 		return;
1716*4882a593Smuzhiyun 
1717*4882a593Smuzhiyun 	notes_attrs = kzalloc(struct_size(notes_attrs, attrs, notes),
1718*4882a593Smuzhiyun 			      GFP_KERNEL);
1719*4882a593Smuzhiyun 	if (notes_attrs == NULL)
1720*4882a593Smuzhiyun 		return;
1721*4882a593Smuzhiyun 
1722*4882a593Smuzhiyun 	notes_attrs->notes = notes;
1723*4882a593Smuzhiyun 	nattr = &notes_attrs->attrs[0];
1724*4882a593Smuzhiyun 	for (loaded = i = 0; i < info->hdr->e_shnum; ++i) {
1725*4882a593Smuzhiyun 		if (sect_empty(&info->sechdrs[i]))
1726*4882a593Smuzhiyun 			continue;
1727*4882a593Smuzhiyun 		if (info->sechdrs[i].sh_type == SHT_NOTE) {
1728*4882a593Smuzhiyun 			sysfs_bin_attr_init(nattr);
1729*4882a593Smuzhiyun 			nattr->attr.name = mod->sect_attrs->attrs[loaded].battr.attr.name;
1730*4882a593Smuzhiyun 			nattr->attr.mode = S_IRUGO;
1731*4882a593Smuzhiyun 			nattr->size = info->sechdrs[i].sh_size;
1732*4882a593Smuzhiyun 			nattr->private = (void *) info->sechdrs[i].sh_addr;
1733*4882a593Smuzhiyun 			nattr->read = module_notes_read;
1734*4882a593Smuzhiyun 			++nattr;
1735*4882a593Smuzhiyun 		}
1736*4882a593Smuzhiyun 		++loaded;
1737*4882a593Smuzhiyun 	}
1738*4882a593Smuzhiyun 
1739*4882a593Smuzhiyun 	notes_attrs->dir = kobject_create_and_add("notes", &mod->mkobj.kobj);
1740*4882a593Smuzhiyun 	if (!notes_attrs->dir)
1741*4882a593Smuzhiyun 		goto out;
1742*4882a593Smuzhiyun 
1743*4882a593Smuzhiyun 	for (i = 0; i < notes; ++i)
1744*4882a593Smuzhiyun 		if (sysfs_create_bin_file(notes_attrs->dir,
1745*4882a593Smuzhiyun 					  &notes_attrs->attrs[i]))
1746*4882a593Smuzhiyun 			goto out;
1747*4882a593Smuzhiyun 
1748*4882a593Smuzhiyun 	mod->notes_attrs = notes_attrs;
1749*4882a593Smuzhiyun 	return;
1750*4882a593Smuzhiyun 
1751*4882a593Smuzhiyun   out:
1752*4882a593Smuzhiyun 	free_notes_attrs(notes_attrs, i);
1753*4882a593Smuzhiyun }
1754*4882a593Smuzhiyun 
remove_notes_attrs(struct module * mod)1755*4882a593Smuzhiyun static void remove_notes_attrs(struct module *mod)
1756*4882a593Smuzhiyun {
1757*4882a593Smuzhiyun 	if (mod->notes_attrs)
1758*4882a593Smuzhiyun 		free_notes_attrs(mod->notes_attrs, mod->notes_attrs->notes);
1759*4882a593Smuzhiyun }
1760*4882a593Smuzhiyun 
1761*4882a593Smuzhiyun #else
1762*4882a593Smuzhiyun 
add_sect_attrs(struct module * mod,const struct load_info * info)1763*4882a593Smuzhiyun static inline void add_sect_attrs(struct module *mod,
1764*4882a593Smuzhiyun 				  const struct load_info *info)
1765*4882a593Smuzhiyun {
1766*4882a593Smuzhiyun }
1767*4882a593Smuzhiyun 
remove_sect_attrs(struct module * mod)1768*4882a593Smuzhiyun static inline void remove_sect_attrs(struct module *mod)
1769*4882a593Smuzhiyun {
1770*4882a593Smuzhiyun }
1771*4882a593Smuzhiyun 
add_notes_attrs(struct module * mod,const struct load_info * info)1772*4882a593Smuzhiyun static inline void add_notes_attrs(struct module *mod,
1773*4882a593Smuzhiyun 				   const struct load_info *info)
1774*4882a593Smuzhiyun {
1775*4882a593Smuzhiyun }
1776*4882a593Smuzhiyun 
remove_notes_attrs(struct module * mod)1777*4882a593Smuzhiyun static inline void remove_notes_attrs(struct module *mod)
1778*4882a593Smuzhiyun {
1779*4882a593Smuzhiyun }
1780*4882a593Smuzhiyun #endif /* CONFIG_KALLSYMS */
1781*4882a593Smuzhiyun 
del_usage_links(struct module * mod)1782*4882a593Smuzhiyun static void del_usage_links(struct module *mod)
1783*4882a593Smuzhiyun {
1784*4882a593Smuzhiyun #ifdef CONFIG_MODULE_UNLOAD
1785*4882a593Smuzhiyun 	struct module_use *use;
1786*4882a593Smuzhiyun 
1787*4882a593Smuzhiyun 	mutex_lock(&module_mutex);
1788*4882a593Smuzhiyun 	list_for_each_entry(use, &mod->target_list, target_list)
1789*4882a593Smuzhiyun 		sysfs_remove_link(use->target->holders_dir, mod->name);
1790*4882a593Smuzhiyun 	mutex_unlock(&module_mutex);
1791*4882a593Smuzhiyun #endif
1792*4882a593Smuzhiyun }
1793*4882a593Smuzhiyun 
add_usage_links(struct module * mod)1794*4882a593Smuzhiyun static int add_usage_links(struct module *mod)
1795*4882a593Smuzhiyun {
1796*4882a593Smuzhiyun 	int ret = 0;
1797*4882a593Smuzhiyun #ifdef CONFIG_MODULE_UNLOAD
1798*4882a593Smuzhiyun 	struct module_use *use;
1799*4882a593Smuzhiyun 
1800*4882a593Smuzhiyun 	mutex_lock(&module_mutex);
1801*4882a593Smuzhiyun 	list_for_each_entry(use, &mod->target_list, target_list) {
1802*4882a593Smuzhiyun 		ret = sysfs_create_link(use->target->holders_dir,
1803*4882a593Smuzhiyun 					&mod->mkobj.kobj, mod->name);
1804*4882a593Smuzhiyun 		if (ret)
1805*4882a593Smuzhiyun 			break;
1806*4882a593Smuzhiyun 	}
1807*4882a593Smuzhiyun 	mutex_unlock(&module_mutex);
1808*4882a593Smuzhiyun 	if (ret)
1809*4882a593Smuzhiyun 		del_usage_links(mod);
1810*4882a593Smuzhiyun #endif
1811*4882a593Smuzhiyun 	return ret;
1812*4882a593Smuzhiyun }
1813*4882a593Smuzhiyun 
1814*4882a593Smuzhiyun static void module_remove_modinfo_attrs(struct module *mod, int end);
1815*4882a593Smuzhiyun 
module_add_modinfo_attrs(struct module * mod)1816*4882a593Smuzhiyun static int module_add_modinfo_attrs(struct module *mod)
1817*4882a593Smuzhiyun {
1818*4882a593Smuzhiyun 	struct module_attribute *attr;
1819*4882a593Smuzhiyun 	struct module_attribute *temp_attr;
1820*4882a593Smuzhiyun 	int error = 0;
1821*4882a593Smuzhiyun 	int i;
1822*4882a593Smuzhiyun 
1823*4882a593Smuzhiyun 	mod->modinfo_attrs = kzalloc((sizeof(struct module_attribute) *
1824*4882a593Smuzhiyun 					(ARRAY_SIZE(modinfo_attrs) + 1)),
1825*4882a593Smuzhiyun 					GFP_KERNEL);
1826*4882a593Smuzhiyun 	if (!mod->modinfo_attrs)
1827*4882a593Smuzhiyun 		return -ENOMEM;
1828*4882a593Smuzhiyun 
1829*4882a593Smuzhiyun 	temp_attr = mod->modinfo_attrs;
1830*4882a593Smuzhiyun 	for (i = 0; (attr = modinfo_attrs[i]); i++) {
1831*4882a593Smuzhiyun 		if (!attr->test || attr->test(mod)) {
1832*4882a593Smuzhiyun 			memcpy(temp_attr, attr, sizeof(*temp_attr));
1833*4882a593Smuzhiyun 			sysfs_attr_init(&temp_attr->attr);
1834*4882a593Smuzhiyun 			error = sysfs_create_file(&mod->mkobj.kobj,
1835*4882a593Smuzhiyun 					&temp_attr->attr);
1836*4882a593Smuzhiyun 			if (error)
1837*4882a593Smuzhiyun 				goto error_out;
1838*4882a593Smuzhiyun 			++temp_attr;
1839*4882a593Smuzhiyun 		}
1840*4882a593Smuzhiyun 	}
1841*4882a593Smuzhiyun 
1842*4882a593Smuzhiyun 	return 0;
1843*4882a593Smuzhiyun 
1844*4882a593Smuzhiyun error_out:
1845*4882a593Smuzhiyun 	if (i > 0)
1846*4882a593Smuzhiyun 		module_remove_modinfo_attrs(mod, --i);
1847*4882a593Smuzhiyun 	else
1848*4882a593Smuzhiyun 		kfree(mod->modinfo_attrs);
1849*4882a593Smuzhiyun 	return error;
1850*4882a593Smuzhiyun }
1851*4882a593Smuzhiyun 
module_remove_modinfo_attrs(struct module * mod,int end)1852*4882a593Smuzhiyun static void module_remove_modinfo_attrs(struct module *mod, int end)
1853*4882a593Smuzhiyun {
1854*4882a593Smuzhiyun 	struct module_attribute *attr;
1855*4882a593Smuzhiyun 	int i;
1856*4882a593Smuzhiyun 
1857*4882a593Smuzhiyun 	for (i = 0; (attr = &mod->modinfo_attrs[i]); i++) {
1858*4882a593Smuzhiyun 		if (end >= 0 && i > end)
1859*4882a593Smuzhiyun 			break;
1860*4882a593Smuzhiyun 		/* pick a field to test for end of list */
1861*4882a593Smuzhiyun 		if (!attr->attr.name)
1862*4882a593Smuzhiyun 			break;
1863*4882a593Smuzhiyun 		sysfs_remove_file(&mod->mkobj.kobj, &attr->attr);
1864*4882a593Smuzhiyun 		if (attr->free)
1865*4882a593Smuzhiyun 			attr->free(mod);
1866*4882a593Smuzhiyun 	}
1867*4882a593Smuzhiyun 	kfree(mod->modinfo_attrs);
1868*4882a593Smuzhiyun }
1869*4882a593Smuzhiyun 
mod_kobject_put(struct module * mod)1870*4882a593Smuzhiyun static void mod_kobject_put(struct module *mod)
1871*4882a593Smuzhiyun {
1872*4882a593Smuzhiyun 	DECLARE_COMPLETION_ONSTACK(c);
1873*4882a593Smuzhiyun 	mod->mkobj.kobj_completion = &c;
1874*4882a593Smuzhiyun 	kobject_put(&mod->mkobj.kobj);
1875*4882a593Smuzhiyun 	wait_for_completion(&c);
1876*4882a593Smuzhiyun }
1877*4882a593Smuzhiyun 
mod_sysfs_init(struct module * mod)1878*4882a593Smuzhiyun static int mod_sysfs_init(struct module *mod)
1879*4882a593Smuzhiyun {
1880*4882a593Smuzhiyun 	int err;
1881*4882a593Smuzhiyun 	struct kobject *kobj;
1882*4882a593Smuzhiyun 
1883*4882a593Smuzhiyun 	if (!module_sysfs_initialized) {
1884*4882a593Smuzhiyun 		pr_err("%s: module sysfs not initialized\n", mod->name);
1885*4882a593Smuzhiyun 		err = -EINVAL;
1886*4882a593Smuzhiyun 		goto out;
1887*4882a593Smuzhiyun 	}
1888*4882a593Smuzhiyun 
1889*4882a593Smuzhiyun 	kobj = kset_find_obj(module_kset, mod->name);
1890*4882a593Smuzhiyun 	if (kobj) {
1891*4882a593Smuzhiyun 		pr_err("%s: module is already loaded\n", mod->name);
1892*4882a593Smuzhiyun 		kobject_put(kobj);
1893*4882a593Smuzhiyun 		err = -EINVAL;
1894*4882a593Smuzhiyun 		goto out;
1895*4882a593Smuzhiyun 	}
1896*4882a593Smuzhiyun 
1897*4882a593Smuzhiyun 	mod->mkobj.mod = mod;
1898*4882a593Smuzhiyun 
1899*4882a593Smuzhiyun 	memset(&mod->mkobj.kobj, 0, sizeof(mod->mkobj.kobj));
1900*4882a593Smuzhiyun 	mod->mkobj.kobj.kset = module_kset;
1901*4882a593Smuzhiyun 	err = kobject_init_and_add(&mod->mkobj.kobj, &module_ktype, NULL,
1902*4882a593Smuzhiyun 				   "%s", mod->name);
1903*4882a593Smuzhiyun 	if (err)
1904*4882a593Smuzhiyun 		mod_kobject_put(mod);
1905*4882a593Smuzhiyun 
1906*4882a593Smuzhiyun out:
1907*4882a593Smuzhiyun 	return err;
1908*4882a593Smuzhiyun }
1909*4882a593Smuzhiyun 
mod_sysfs_setup(struct module * mod,const struct load_info * info,struct kernel_param * kparam,unsigned int num_params)1910*4882a593Smuzhiyun static int mod_sysfs_setup(struct module *mod,
1911*4882a593Smuzhiyun 			   const struct load_info *info,
1912*4882a593Smuzhiyun 			   struct kernel_param *kparam,
1913*4882a593Smuzhiyun 			   unsigned int num_params)
1914*4882a593Smuzhiyun {
1915*4882a593Smuzhiyun 	int err;
1916*4882a593Smuzhiyun 
1917*4882a593Smuzhiyun 	err = mod_sysfs_init(mod);
1918*4882a593Smuzhiyun 	if (err)
1919*4882a593Smuzhiyun 		goto out;
1920*4882a593Smuzhiyun 
1921*4882a593Smuzhiyun 	mod->holders_dir = kobject_create_and_add("holders", &mod->mkobj.kobj);
1922*4882a593Smuzhiyun 	if (!mod->holders_dir) {
1923*4882a593Smuzhiyun 		err = -ENOMEM;
1924*4882a593Smuzhiyun 		goto out_unreg;
1925*4882a593Smuzhiyun 	}
1926*4882a593Smuzhiyun 
1927*4882a593Smuzhiyun 	err = module_param_sysfs_setup(mod, kparam, num_params);
1928*4882a593Smuzhiyun 	if (err)
1929*4882a593Smuzhiyun 		goto out_unreg_holders;
1930*4882a593Smuzhiyun 
1931*4882a593Smuzhiyun 	err = module_add_modinfo_attrs(mod);
1932*4882a593Smuzhiyun 	if (err)
1933*4882a593Smuzhiyun 		goto out_unreg_param;
1934*4882a593Smuzhiyun 
1935*4882a593Smuzhiyun 	err = add_usage_links(mod);
1936*4882a593Smuzhiyun 	if (err)
1937*4882a593Smuzhiyun 		goto out_unreg_modinfo_attrs;
1938*4882a593Smuzhiyun 
1939*4882a593Smuzhiyun 	add_sect_attrs(mod, info);
1940*4882a593Smuzhiyun 	add_notes_attrs(mod, info);
1941*4882a593Smuzhiyun 
1942*4882a593Smuzhiyun 	return 0;
1943*4882a593Smuzhiyun 
1944*4882a593Smuzhiyun out_unreg_modinfo_attrs:
1945*4882a593Smuzhiyun 	module_remove_modinfo_attrs(mod, -1);
1946*4882a593Smuzhiyun out_unreg_param:
1947*4882a593Smuzhiyun 	module_param_sysfs_remove(mod);
1948*4882a593Smuzhiyun out_unreg_holders:
1949*4882a593Smuzhiyun 	kobject_put(mod->holders_dir);
1950*4882a593Smuzhiyun out_unreg:
1951*4882a593Smuzhiyun 	mod_kobject_put(mod);
1952*4882a593Smuzhiyun out:
1953*4882a593Smuzhiyun 	return err;
1954*4882a593Smuzhiyun }
1955*4882a593Smuzhiyun 
mod_sysfs_fini(struct module * mod)1956*4882a593Smuzhiyun static void mod_sysfs_fini(struct module *mod)
1957*4882a593Smuzhiyun {
1958*4882a593Smuzhiyun 	remove_notes_attrs(mod);
1959*4882a593Smuzhiyun 	remove_sect_attrs(mod);
1960*4882a593Smuzhiyun 	mod_kobject_put(mod);
1961*4882a593Smuzhiyun }
1962*4882a593Smuzhiyun 
init_param_lock(struct module * mod)1963*4882a593Smuzhiyun static void init_param_lock(struct module *mod)
1964*4882a593Smuzhiyun {
1965*4882a593Smuzhiyun 	mutex_init(&mod->param_lock);
1966*4882a593Smuzhiyun }
1967*4882a593Smuzhiyun #else /* !CONFIG_SYSFS */
1968*4882a593Smuzhiyun 
mod_sysfs_setup(struct module * mod,const struct load_info * info,struct kernel_param * kparam,unsigned int num_params)1969*4882a593Smuzhiyun static int mod_sysfs_setup(struct module *mod,
1970*4882a593Smuzhiyun 			   const struct load_info *info,
1971*4882a593Smuzhiyun 			   struct kernel_param *kparam,
1972*4882a593Smuzhiyun 			   unsigned int num_params)
1973*4882a593Smuzhiyun {
1974*4882a593Smuzhiyun 	return 0;
1975*4882a593Smuzhiyun }
1976*4882a593Smuzhiyun 
mod_sysfs_fini(struct module * mod)1977*4882a593Smuzhiyun static void mod_sysfs_fini(struct module *mod)
1978*4882a593Smuzhiyun {
1979*4882a593Smuzhiyun }
1980*4882a593Smuzhiyun 
module_remove_modinfo_attrs(struct module * mod,int end)1981*4882a593Smuzhiyun static void module_remove_modinfo_attrs(struct module *mod, int end)
1982*4882a593Smuzhiyun {
1983*4882a593Smuzhiyun }
1984*4882a593Smuzhiyun 
del_usage_links(struct module * mod)1985*4882a593Smuzhiyun static void del_usage_links(struct module *mod)
1986*4882a593Smuzhiyun {
1987*4882a593Smuzhiyun }
1988*4882a593Smuzhiyun 
init_param_lock(struct module * mod)1989*4882a593Smuzhiyun static void init_param_lock(struct module *mod)
1990*4882a593Smuzhiyun {
1991*4882a593Smuzhiyun }
1992*4882a593Smuzhiyun #endif /* CONFIG_SYSFS */
1993*4882a593Smuzhiyun 
mod_sysfs_teardown(struct module * mod)1994*4882a593Smuzhiyun static void mod_sysfs_teardown(struct module *mod)
1995*4882a593Smuzhiyun {
1996*4882a593Smuzhiyun 	del_usage_links(mod);
1997*4882a593Smuzhiyun 	module_remove_modinfo_attrs(mod, -1);
1998*4882a593Smuzhiyun 	module_param_sysfs_remove(mod);
1999*4882a593Smuzhiyun 	kobject_put(mod->mkobj.drivers_dir);
2000*4882a593Smuzhiyun 	kobject_put(mod->holders_dir);
2001*4882a593Smuzhiyun 	mod_sysfs_fini(mod);
2002*4882a593Smuzhiyun }
2003*4882a593Smuzhiyun 
2004*4882a593Smuzhiyun /*
2005*4882a593Smuzhiyun  * LKM RO/NX protection: protect module's text/ro-data
2006*4882a593Smuzhiyun  * from modification and any data from execution.
2007*4882a593Smuzhiyun  *
2008*4882a593Smuzhiyun  * General layout of module is:
2009*4882a593Smuzhiyun  *          [text] [read-only-data] [ro-after-init] [writable data]
2010*4882a593Smuzhiyun  * text_size -----^                ^               ^               ^
2011*4882a593Smuzhiyun  * ro_size ------------------------|               |               |
2012*4882a593Smuzhiyun  * ro_after_init_size -----------------------------|               |
2013*4882a593Smuzhiyun  * size -----------------------------------------------------------|
2014*4882a593Smuzhiyun  *
2015*4882a593Smuzhiyun  * These values are always page-aligned (as is base)
2016*4882a593Smuzhiyun  */
2017*4882a593Smuzhiyun 
2018*4882a593Smuzhiyun /*
2019*4882a593Smuzhiyun  * Since some arches are moving towards PAGE_KERNEL module allocations instead
2020*4882a593Smuzhiyun  * of PAGE_KERNEL_EXEC, keep frob_text() and module_enable_x() outside of the
2021*4882a593Smuzhiyun  * CONFIG_STRICT_MODULE_RWX block below because they are needed regardless of
2022*4882a593Smuzhiyun  * whether we are strict.
2023*4882a593Smuzhiyun  */
2024*4882a593Smuzhiyun #ifdef CONFIG_ARCH_HAS_STRICT_MODULE_RWX
frob_text(const struct module_layout * layout,int (* set_memory)(unsigned long start,int num_pages))2025*4882a593Smuzhiyun static void frob_text(const struct module_layout *layout,
2026*4882a593Smuzhiyun 		      int (*set_memory)(unsigned long start, int num_pages))
2027*4882a593Smuzhiyun {
2028*4882a593Smuzhiyun 	BUG_ON((unsigned long)layout->base & (PAGE_SIZE-1));
2029*4882a593Smuzhiyun 	BUG_ON((unsigned long)layout->text_size & (PAGE_SIZE-1));
2030*4882a593Smuzhiyun 	set_memory((unsigned long)layout->base,
2031*4882a593Smuzhiyun 		   layout->text_size >> PAGE_SHIFT);
2032*4882a593Smuzhiyun }
2033*4882a593Smuzhiyun 
module_enable_x(const struct module * mod)2034*4882a593Smuzhiyun static void module_enable_x(const struct module *mod)
2035*4882a593Smuzhiyun {
2036*4882a593Smuzhiyun 	frob_text(&mod->core_layout, set_memory_x);
2037*4882a593Smuzhiyun 	frob_text(&mod->init_layout, set_memory_x);
2038*4882a593Smuzhiyun }
2039*4882a593Smuzhiyun #else /* !CONFIG_ARCH_HAS_STRICT_MODULE_RWX */
module_enable_x(const struct module * mod)2040*4882a593Smuzhiyun static void module_enable_x(const struct module *mod) { }
2041*4882a593Smuzhiyun #endif /* CONFIG_ARCH_HAS_STRICT_MODULE_RWX */
2042*4882a593Smuzhiyun 
2043*4882a593Smuzhiyun #ifdef CONFIG_STRICT_MODULE_RWX
frob_rodata(const struct module_layout * layout,int (* set_memory)(unsigned long start,int num_pages))2044*4882a593Smuzhiyun static void frob_rodata(const struct module_layout *layout,
2045*4882a593Smuzhiyun 			int (*set_memory)(unsigned long start, int num_pages))
2046*4882a593Smuzhiyun {
2047*4882a593Smuzhiyun 	BUG_ON((unsigned long)layout->base & (PAGE_SIZE-1));
2048*4882a593Smuzhiyun 	BUG_ON((unsigned long)layout->text_size & (PAGE_SIZE-1));
2049*4882a593Smuzhiyun 	BUG_ON((unsigned long)layout->ro_size & (PAGE_SIZE-1));
2050*4882a593Smuzhiyun 	set_memory((unsigned long)layout->base + layout->text_size,
2051*4882a593Smuzhiyun 		   (layout->ro_size - layout->text_size) >> PAGE_SHIFT);
2052*4882a593Smuzhiyun }
2053*4882a593Smuzhiyun 
frob_ro_after_init(const struct module_layout * layout,int (* set_memory)(unsigned long start,int num_pages))2054*4882a593Smuzhiyun static void frob_ro_after_init(const struct module_layout *layout,
2055*4882a593Smuzhiyun 				int (*set_memory)(unsigned long start, int num_pages))
2056*4882a593Smuzhiyun {
2057*4882a593Smuzhiyun 	BUG_ON((unsigned long)layout->base & (PAGE_SIZE-1));
2058*4882a593Smuzhiyun 	BUG_ON((unsigned long)layout->ro_size & (PAGE_SIZE-1));
2059*4882a593Smuzhiyun 	BUG_ON((unsigned long)layout->ro_after_init_size & (PAGE_SIZE-1));
2060*4882a593Smuzhiyun 	set_memory((unsigned long)layout->base + layout->ro_size,
2061*4882a593Smuzhiyun 		   (layout->ro_after_init_size - layout->ro_size) >> PAGE_SHIFT);
2062*4882a593Smuzhiyun }
2063*4882a593Smuzhiyun 
frob_writable_data(const struct module_layout * layout,int (* set_memory)(unsigned long start,int num_pages))2064*4882a593Smuzhiyun static void frob_writable_data(const struct module_layout *layout,
2065*4882a593Smuzhiyun 			       int (*set_memory)(unsigned long start, int num_pages))
2066*4882a593Smuzhiyun {
2067*4882a593Smuzhiyun 	BUG_ON((unsigned long)layout->base & (PAGE_SIZE-1));
2068*4882a593Smuzhiyun 	BUG_ON((unsigned long)layout->ro_after_init_size & (PAGE_SIZE-1));
2069*4882a593Smuzhiyun 	BUG_ON((unsigned long)layout->size & (PAGE_SIZE-1));
2070*4882a593Smuzhiyun 	set_memory((unsigned long)layout->base + layout->ro_after_init_size,
2071*4882a593Smuzhiyun 		   (layout->size - layout->ro_after_init_size) >> PAGE_SHIFT);
2072*4882a593Smuzhiyun }
2073*4882a593Smuzhiyun 
module_enable_ro(const struct module * mod,bool after_init)2074*4882a593Smuzhiyun static void module_enable_ro(const struct module *mod, bool after_init)
2075*4882a593Smuzhiyun {
2076*4882a593Smuzhiyun 	if (!rodata_enabled)
2077*4882a593Smuzhiyun 		return;
2078*4882a593Smuzhiyun 
2079*4882a593Smuzhiyun 	set_vm_flush_reset_perms(mod->core_layout.base);
2080*4882a593Smuzhiyun 	set_vm_flush_reset_perms(mod->init_layout.base);
2081*4882a593Smuzhiyun 	frob_text(&mod->core_layout, set_memory_ro);
2082*4882a593Smuzhiyun 
2083*4882a593Smuzhiyun 	frob_rodata(&mod->core_layout, set_memory_ro);
2084*4882a593Smuzhiyun 	frob_text(&mod->init_layout, set_memory_ro);
2085*4882a593Smuzhiyun 	frob_rodata(&mod->init_layout, set_memory_ro);
2086*4882a593Smuzhiyun 
2087*4882a593Smuzhiyun 	if (after_init)
2088*4882a593Smuzhiyun 		frob_ro_after_init(&mod->core_layout, set_memory_ro);
2089*4882a593Smuzhiyun }
2090*4882a593Smuzhiyun 
module_enable_nx(const struct module * mod)2091*4882a593Smuzhiyun static void module_enable_nx(const struct module *mod)
2092*4882a593Smuzhiyun {
2093*4882a593Smuzhiyun 	frob_rodata(&mod->core_layout, set_memory_nx);
2094*4882a593Smuzhiyun 	frob_ro_after_init(&mod->core_layout, set_memory_nx);
2095*4882a593Smuzhiyun 	frob_writable_data(&mod->core_layout, set_memory_nx);
2096*4882a593Smuzhiyun 	frob_rodata(&mod->init_layout, set_memory_nx);
2097*4882a593Smuzhiyun 	frob_writable_data(&mod->init_layout, set_memory_nx);
2098*4882a593Smuzhiyun }
2099*4882a593Smuzhiyun 
module_enforce_rwx_sections(Elf_Ehdr * hdr,Elf_Shdr * sechdrs,char * secstrings,struct module * mod)2100*4882a593Smuzhiyun static int module_enforce_rwx_sections(Elf_Ehdr *hdr, Elf_Shdr *sechdrs,
2101*4882a593Smuzhiyun 				       char *secstrings, struct module *mod)
2102*4882a593Smuzhiyun {
2103*4882a593Smuzhiyun 	const unsigned long shf_wx = SHF_WRITE|SHF_EXECINSTR;
2104*4882a593Smuzhiyun 	int i;
2105*4882a593Smuzhiyun 
2106*4882a593Smuzhiyun 	for (i = 0; i < hdr->e_shnum; i++) {
2107*4882a593Smuzhiyun 		if ((sechdrs[i].sh_flags & shf_wx) == shf_wx) {
2108*4882a593Smuzhiyun 			pr_err("%s: section %s (index %d) has invalid WRITE|EXEC flags\n",
2109*4882a593Smuzhiyun 				mod->name, secstrings + sechdrs[i].sh_name, i);
2110*4882a593Smuzhiyun 			return -ENOEXEC;
2111*4882a593Smuzhiyun 		}
2112*4882a593Smuzhiyun 	}
2113*4882a593Smuzhiyun 
2114*4882a593Smuzhiyun 	return 0;
2115*4882a593Smuzhiyun }
2116*4882a593Smuzhiyun 
2117*4882a593Smuzhiyun #else /* !CONFIG_STRICT_MODULE_RWX */
module_enable_nx(const struct module * mod)2118*4882a593Smuzhiyun static void module_enable_nx(const struct module *mod) { }
module_enable_ro(const struct module * mod,bool after_init)2119*4882a593Smuzhiyun static void module_enable_ro(const struct module *mod, bool after_init) {}
module_enforce_rwx_sections(Elf_Ehdr * hdr,Elf_Shdr * sechdrs,char * secstrings,struct module * mod)2120*4882a593Smuzhiyun static int module_enforce_rwx_sections(Elf_Ehdr *hdr, Elf_Shdr *sechdrs,
2121*4882a593Smuzhiyun 				       char *secstrings, struct module *mod)
2122*4882a593Smuzhiyun {
2123*4882a593Smuzhiyun 	return 0;
2124*4882a593Smuzhiyun }
2125*4882a593Smuzhiyun #endif /*  CONFIG_STRICT_MODULE_RWX */
2126*4882a593Smuzhiyun 
2127*4882a593Smuzhiyun #ifdef CONFIG_LIVEPATCH
2128*4882a593Smuzhiyun /*
2129*4882a593Smuzhiyun  * Persist Elf information about a module. Copy the Elf header,
2130*4882a593Smuzhiyun  * section header table, section string table, and symtab section
2131*4882a593Smuzhiyun  * index from info to mod->klp_info.
2132*4882a593Smuzhiyun  */
copy_module_elf(struct module * mod,struct load_info * info)2133*4882a593Smuzhiyun static int copy_module_elf(struct module *mod, struct load_info *info)
2134*4882a593Smuzhiyun {
2135*4882a593Smuzhiyun 	unsigned int size, symndx;
2136*4882a593Smuzhiyun 	int ret;
2137*4882a593Smuzhiyun 
2138*4882a593Smuzhiyun 	size = sizeof(*mod->klp_info);
2139*4882a593Smuzhiyun 	mod->klp_info = kmalloc(size, GFP_KERNEL);
2140*4882a593Smuzhiyun 	if (mod->klp_info == NULL)
2141*4882a593Smuzhiyun 		return -ENOMEM;
2142*4882a593Smuzhiyun 
2143*4882a593Smuzhiyun 	/* Elf header */
2144*4882a593Smuzhiyun 	size = sizeof(mod->klp_info->hdr);
2145*4882a593Smuzhiyun 	memcpy(&mod->klp_info->hdr, info->hdr, size);
2146*4882a593Smuzhiyun 
2147*4882a593Smuzhiyun 	/* Elf section header table */
2148*4882a593Smuzhiyun 	size = sizeof(*info->sechdrs) * info->hdr->e_shnum;
2149*4882a593Smuzhiyun 	mod->klp_info->sechdrs = kmemdup(info->sechdrs, size, GFP_KERNEL);
2150*4882a593Smuzhiyun 	if (mod->klp_info->sechdrs == NULL) {
2151*4882a593Smuzhiyun 		ret = -ENOMEM;
2152*4882a593Smuzhiyun 		goto free_info;
2153*4882a593Smuzhiyun 	}
2154*4882a593Smuzhiyun 
2155*4882a593Smuzhiyun 	/* Elf section name string table */
2156*4882a593Smuzhiyun 	size = info->sechdrs[info->hdr->e_shstrndx].sh_size;
2157*4882a593Smuzhiyun 	mod->klp_info->secstrings = kmemdup(info->secstrings, size, GFP_KERNEL);
2158*4882a593Smuzhiyun 	if (mod->klp_info->secstrings == NULL) {
2159*4882a593Smuzhiyun 		ret = -ENOMEM;
2160*4882a593Smuzhiyun 		goto free_sechdrs;
2161*4882a593Smuzhiyun 	}
2162*4882a593Smuzhiyun 
2163*4882a593Smuzhiyun 	/* Elf symbol section index */
2164*4882a593Smuzhiyun 	symndx = info->index.sym;
2165*4882a593Smuzhiyun 	mod->klp_info->symndx = symndx;
2166*4882a593Smuzhiyun 
2167*4882a593Smuzhiyun 	/*
2168*4882a593Smuzhiyun 	 * For livepatch modules, core_kallsyms.symtab is a complete
2169*4882a593Smuzhiyun 	 * copy of the original symbol table. Adjust sh_addr to point
2170*4882a593Smuzhiyun 	 * to core_kallsyms.symtab since the copy of the symtab in module
2171*4882a593Smuzhiyun 	 * init memory is freed at the end of do_init_module().
2172*4882a593Smuzhiyun 	 */
2173*4882a593Smuzhiyun 	mod->klp_info->sechdrs[symndx].sh_addr = \
2174*4882a593Smuzhiyun 		(unsigned long) mod->core_kallsyms.symtab;
2175*4882a593Smuzhiyun 
2176*4882a593Smuzhiyun 	return 0;
2177*4882a593Smuzhiyun 
2178*4882a593Smuzhiyun free_sechdrs:
2179*4882a593Smuzhiyun 	kfree(mod->klp_info->sechdrs);
2180*4882a593Smuzhiyun free_info:
2181*4882a593Smuzhiyun 	kfree(mod->klp_info);
2182*4882a593Smuzhiyun 	return ret;
2183*4882a593Smuzhiyun }
2184*4882a593Smuzhiyun 
free_module_elf(struct module * mod)2185*4882a593Smuzhiyun static void free_module_elf(struct module *mod)
2186*4882a593Smuzhiyun {
2187*4882a593Smuzhiyun 	kfree(mod->klp_info->sechdrs);
2188*4882a593Smuzhiyun 	kfree(mod->klp_info->secstrings);
2189*4882a593Smuzhiyun 	kfree(mod->klp_info);
2190*4882a593Smuzhiyun }
2191*4882a593Smuzhiyun #else /* !CONFIG_LIVEPATCH */
copy_module_elf(struct module * mod,struct load_info * info)2192*4882a593Smuzhiyun static int copy_module_elf(struct module *mod, struct load_info *info)
2193*4882a593Smuzhiyun {
2194*4882a593Smuzhiyun 	return 0;
2195*4882a593Smuzhiyun }
2196*4882a593Smuzhiyun 
free_module_elf(struct module * mod)2197*4882a593Smuzhiyun static void free_module_elf(struct module *mod)
2198*4882a593Smuzhiyun {
2199*4882a593Smuzhiyun }
2200*4882a593Smuzhiyun #endif /* CONFIG_LIVEPATCH */
2201*4882a593Smuzhiyun 
module_memfree(void * module_region)2202*4882a593Smuzhiyun void __weak module_memfree(void *module_region)
2203*4882a593Smuzhiyun {
2204*4882a593Smuzhiyun 	/*
2205*4882a593Smuzhiyun 	 * This memory may be RO, and freeing RO memory in an interrupt is not
2206*4882a593Smuzhiyun 	 * supported by vmalloc.
2207*4882a593Smuzhiyun 	 */
2208*4882a593Smuzhiyun 	WARN_ON(in_interrupt());
2209*4882a593Smuzhiyun 	vfree(module_region);
2210*4882a593Smuzhiyun }
2211*4882a593Smuzhiyun 
module_arch_cleanup(struct module * mod)2212*4882a593Smuzhiyun void __weak module_arch_cleanup(struct module *mod)
2213*4882a593Smuzhiyun {
2214*4882a593Smuzhiyun }
2215*4882a593Smuzhiyun 
module_arch_freeing_init(struct module * mod)2216*4882a593Smuzhiyun void __weak module_arch_freeing_init(struct module *mod)
2217*4882a593Smuzhiyun {
2218*4882a593Smuzhiyun }
2219*4882a593Smuzhiyun 
2220*4882a593Smuzhiyun static void cfi_cleanup(struct module *mod);
2221*4882a593Smuzhiyun 
2222*4882a593Smuzhiyun /* Free a module, remove from lists, etc. */
free_module(struct module * mod)2223*4882a593Smuzhiyun static void free_module(struct module *mod)
2224*4882a593Smuzhiyun {
2225*4882a593Smuzhiyun 	trace_module_free(mod);
2226*4882a593Smuzhiyun 
2227*4882a593Smuzhiyun 	mod_sysfs_teardown(mod);
2228*4882a593Smuzhiyun 
2229*4882a593Smuzhiyun 	/* We leave it in list to prevent duplicate loads, but make sure
2230*4882a593Smuzhiyun 	 * that noone uses it while it's being deconstructed. */
2231*4882a593Smuzhiyun 	mutex_lock(&module_mutex);
2232*4882a593Smuzhiyun 	mod->state = MODULE_STATE_UNFORMED;
2233*4882a593Smuzhiyun 	mutex_unlock(&module_mutex);
2234*4882a593Smuzhiyun 
2235*4882a593Smuzhiyun 	/* Remove dynamic debug info */
2236*4882a593Smuzhiyun 	ddebug_remove_module(mod->name);
2237*4882a593Smuzhiyun 
2238*4882a593Smuzhiyun 	/* Arch-specific cleanup. */
2239*4882a593Smuzhiyun 	module_arch_cleanup(mod);
2240*4882a593Smuzhiyun 
2241*4882a593Smuzhiyun 	/* Module unload stuff */
2242*4882a593Smuzhiyun 	module_unload_free(mod);
2243*4882a593Smuzhiyun 
2244*4882a593Smuzhiyun 	/* Free any allocated parameters. */
2245*4882a593Smuzhiyun 	destroy_params(mod->kp, mod->num_kp);
2246*4882a593Smuzhiyun 
2247*4882a593Smuzhiyun 	if (is_livepatch_module(mod))
2248*4882a593Smuzhiyun 		free_module_elf(mod);
2249*4882a593Smuzhiyun 
2250*4882a593Smuzhiyun 	/* Now we can delete it from the lists */
2251*4882a593Smuzhiyun 	mutex_lock(&module_mutex);
2252*4882a593Smuzhiyun 	/* Unlink carefully: kallsyms could be walking list. */
2253*4882a593Smuzhiyun 	list_del_rcu(&mod->list);
2254*4882a593Smuzhiyun 	mod_tree_remove(mod);
2255*4882a593Smuzhiyun 	/* Remove this module from bug list, this uses list_del_rcu */
2256*4882a593Smuzhiyun 	module_bug_cleanup(mod);
2257*4882a593Smuzhiyun 	/* Wait for RCU-sched synchronizing before releasing mod->list and buglist. */
2258*4882a593Smuzhiyun 	synchronize_rcu();
2259*4882a593Smuzhiyun 	mutex_unlock(&module_mutex);
2260*4882a593Smuzhiyun 
2261*4882a593Smuzhiyun 	/* Clean up CFI for the module. */
2262*4882a593Smuzhiyun 	cfi_cleanup(mod);
2263*4882a593Smuzhiyun 
2264*4882a593Smuzhiyun 	/* This may be empty, but that's OK */
2265*4882a593Smuzhiyun 	module_arch_freeing_init(mod);
2266*4882a593Smuzhiyun 	trace_android_vh_set_memory_rw((unsigned long)mod->init_layout.base,
2267*4882a593Smuzhiyun 		(mod->init_layout.size)>>PAGE_SHIFT);
2268*4882a593Smuzhiyun 	trace_android_vh_set_memory_nx((unsigned long)mod->init_layout.base,
2269*4882a593Smuzhiyun 		(mod->init_layout.size)>>PAGE_SHIFT);
2270*4882a593Smuzhiyun 	module_memfree(mod->init_layout.base);
2271*4882a593Smuzhiyun 	kfree(mod->args);
2272*4882a593Smuzhiyun 	percpu_modfree(mod);
2273*4882a593Smuzhiyun 
2274*4882a593Smuzhiyun 	/* Free lock-classes; relies on the preceding sync_rcu(). */
2275*4882a593Smuzhiyun 	lockdep_free_key_range(mod->core_layout.base, mod->core_layout.size);
2276*4882a593Smuzhiyun 
2277*4882a593Smuzhiyun 	/* Finally, free the core (containing the module structure) */
2278*4882a593Smuzhiyun 	trace_android_vh_set_memory_rw((unsigned long)mod->core_layout.base,
2279*4882a593Smuzhiyun 		(mod->core_layout.size)>>PAGE_SHIFT);
2280*4882a593Smuzhiyun 	trace_android_vh_set_memory_nx((unsigned long)mod->core_layout.base,
2281*4882a593Smuzhiyun 		(mod->core_layout.size)>>PAGE_SHIFT);
2282*4882a593Smuzhiyun 	module_memfree(mod->core_layout.base);
2283*4882a593Smuzhiyun }
2284*4882a593Smuzhiyun 
__symbol_get(const char * symbol)2285*4882a593Smuzhiyun void *__symbol_get(const char *symbol)
2286*4882a593Smuzhiyun {
2287*4882a593Smuzhiyun 	struct module *owner;
2288*4882a593Smuzhiyun 	const struct kernel_symbol *sym;
2289*4882a593Smuzhiyun 
2290*4882a593Smuzhiyun 	preempt_disable();
2291*4882a593Smuzhiyun 	sym = find_symbol(symbol, &owner, NULL, NULL, true, true);
2292*4882a593Smuzhiyun 	if (sym && strong_try_module_get(owner))
2293*4882a593Smuzhiyun 		sym = NULL;
2294*4882a593Smuzhiyun 	preempt_enable();
2295*4882a593Smuzhiyun 
2296*4882a593Smuzhiyun 	return sym ? (void *)kernel_symbol_value(sym) : NULL;
2297*4882a593Smuzhiyun }
2298*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(__symbol_get);
2299*4882a593Smuzhiyun 
module_init_layout_section(const char * sname)2300*4882a593Smuzhiyun static bool module_init_layout_section(const char *sname)
2301*4882a593Smuzhiyun {
2302*4882a593Smuzhiyun #ifndef CONFIG_MODULE_UNLOAD
2303*4882a593Smuzhiyun 	if (module_exit_section(sname))
2304*4882a593Smuzhiyun 		return true;
2305*4882a593Smuzhiyun #endif
2306*4882a593Smuzhiyun 	return module_init_section(sname);
2307*4882a593Smuzhiyun }
2308*4882a593Smuzhiyun 
2309*4882a593Smuzhiyun /*
2310*4882a593Smuzhiyun  * Ensure that an exported symbol [global namespace] does not already exist
2311*4882a593Smuzhiyun  * in the kernel or in some other module's exported symbol table.
2312*4882a593Smuzhiyun  *
2313*4882a593Smuzhiyun  * You must hold the module_mutex.
2314*4882a593Smuzhiyun  */
verify_exported_symbols(struct module * mod)2315*4882a593Smuzhiyun static int verify_exported_symbols(struct module *mod)
2316*4882a593Smuzhiyun {
2317*4882a593Smuzhiyun 	unsigned int i;
2318*4882a593Smuzhiyun 	struct module *owner;
2319*4882a593Smuzhiyun 	const struct kernel_symbol *s;
2320*4882a593Smuzhiyun 	struct {
2321*4882a593Smuzhiyun 		const struct kernel_symbol *sym;
2322*4882a593Smuzhiyun 		unsigned int num;
2323*4882a593Smuzhiyun 	} arr[] = {
2324*4882a593Smuzhiyun 		{ mod->syms, mod->num_syms },
2325*4882a593Smuzhiyun 		{ mod->gpl_syms, mod->num_gpl_syms },
2326*4882a593Smuzhiyun 		{ mod->gpl_future_syms, mod->num_gpl_future_syms },
2327*4882a593Smuzhiyun #ifdef CONFIG_UNUSED_SYMBOLS
2328*4882a593Smuzhiyun 		{ mod->unused_syms, mod->num_unused_syms },
2329*4882a593Smuzhiyun 		{ mod->unused_gpl_syms, mod->num_unused_gpl_syms },
2330*4882a593Smuzhiyun #endif
2331*4882a593Smuzhiyun 	};
2332*4882a593Smuzhiyun 
2333*4882a593Smuzhiyun 	for (i = 0; i < ARRAY_SIZE(arr); i++) {
2334*4882a593Smuzhiyun 		for (s = arr[i].sym; s < arr[i].sym + arr[i].num; s++) {
2335*4882a593Smuzhiyun 			if (find_symbol(kernel_symbol_name(s), &owner, NULL,
2336*4882a593Smuzhiyun 					NULL, true, false)) {
2337*4882a593Smuzhiyun 				pr_err("%s: exports duplicate symbol %s"
2338*4882a593Smuzhiyun 				       " (owned by %s)\n",
2339*4882a593Smuzhiyun 				       mod->name, kernel_symbol_name(s),
2340*4882a593Smuzhiyun 				       module_name(owner));
2341*4882a593Smuzhiyun 				return -ENOEXEC;
2342*4882a593Smuzhiyun 			}
2343*4882a593Smuzhiyun 		}
2344*4882a593Smuzhiyun 	}
2345*4882a593Smuzhiyun 	return 0;
2346*4882a593Smuzhiyun }
2347*4882a593Smuzhiyun 
ignore_undef_symbol(Elf_Half emachine,const char * name)2348*4882a593Smuzhiyun static bool ignore_undef_symbol(Elf_Half emachine, const char *name)
2349*4882a593Smuzhiyun {
2350*4882a593Smuzhiyun 	/*
2351*4882a593Smuzhiyun 	 * On x86, PIC code and Clang non-PIC code may have call foo@PLT. GNU as
2352*4882a593Smuzhiyun 	 * before 2.37 produces an unreferenced _GLOBAL_OFFSET_TABLE_ on x86-64.
2353*4882a593Smuzhiyun 	 * i386 has a similar problem but may not deserve a fix.
2354*4882a593Smuzhiyun 	 *
2355*4882a593Smuzhiyun 	 * If we ever have to ignore many symbols, consider refactoring the code to
2356*4882a593Smuzhiyun 	 * only warn if referenced by a relocation.
2357*4882a593Smuzhiyun 	 */
2358*4882a593Smuzhiyun 	if (emachine == EM_386 || emachine == EM_X86_64)
2359*4882a593Smuzhiyun 		return !strcmp(name, "_GLOBAL_OFFSET_TABLE_");
2360*4882a593Smuzhiyun 	return false;
2361*4882a593Smuzhiyun }
2362*4882a593Smuzhiyun 
2363*4882a593Smuzhiyun /* Change all symbols so that st_value encodes the pointer directly. */
simplify_symbols(struct module * mod,const struct load_info * info)2364*4882a593Smuzhiyun static int simplify_symbols(struct module *mod, const struct load_info *info)
2365*4882a593Smuzhiyun {
2366*4882a593Smuzhiyun 	Elf_Shdr *symsec = &info->sechdrs[info->index.sym];
2367*4882a593Smuzhiyun 	Elf_Sym *sym = (void *)symsec->sh_addr;
2368*4882a593Smuzhiyun 	unsigned long secbase;
2369*4882a593Smuzhiyun 	unsigned int i;
2370*4882a593Smuzhiyun 	int ret = 0;
2371*4882a593Smuzhiyun 	const struct kernel_symbol *ksym;
2372*4882a593Smuzhiyun 
2373*4882a593Smuzhiyun 	for (i = 1; i < symsec->sh_size / sizeof(Elf_Sym); i++) {
2374*4882a593Smuzhiyun 		const char *name = info->strtab + sym[i].st_name;
2375*4882a593Smuzhiyun 
2376*4882a593Smuzhiyun 		switch (sym[i].st_shndx) {
2377*4882a593Smuzhiyun 		case SHN_COMMON:
2378*4882a593Smuzhiyun 			/* Ignore common symbols */
2379*4882a593Smuzhiyun 			if (!strncmp(name, "__gnu_lto", 9))
2380*4882a593Smuzhiyun 				break;
2381*4882a593Smuzhiyun 
2382*4882a593Smuzhiyun 			/* We compiled with -fno-common.  These are not
2383*4882a593Smuzhiyun 			   supposed to happen.  */
2384*4882a593Smuzhiyun 			pr_debug("Common symbol: %s\n", name);
2385*4882a593Smuzhiyun 			pr_warn("%s: please compile with -fno-common\n",
2386*4882a593Smuzhiyun 			       mod->name);
2387*4882a593Smuzhiyun 			ret = -ENOEXEC;
2388*4882a593Smuzhiyun 			break;
2389*4882a593Smuzhiyun 
2390*4882a593Smuzhiyun 		case SHN_ABS:
2391*4882a593Smuzhiyun 			/* Don't need to do anything */
2392*4882a593Smuzhiyun 			pr_debug("Absolute symbol: 0x%08lx\n",
2393*4882a593Smuzhiyun 			       (long)sym[i].st_value);
2394*4882a593Smuzhiyun 			break;
2395*4882a593Smuzhiyun 
2396*4882a593Smuzhiyun 		case SHN_LIVEPATCH:
2397*4882a593Smuzhiyun 			/* Livepatch symbols are resolved by livepatch */
2398*4882a593Smuzhiyun 			break;
2399*4882a593Smuzhiyun 
2400*4882a593Smuzhiyun 		case SHN_UNDEF:
2401*4882a593Smuzhiyun 			ksym = resolve_symbol_wait(mod, info, name);
2402*4882a593Smuzhiyun 			/* Ok if resolved.  */
2403*4882a593Smuzhiyun 			if (ksym && !IS_ERR(ksym)) {
2404*4882a593Smuzhiyun 				sym[i].st_value = kernel_symbol_value(ksym);
2405*4882a593Smuzhiyun 				break;
2406*4882a593Smuzhiyun 			}
2407*4882a593Smuzhiyun 
2408*4882a593Smuzhiyun 			/* Ok if weak or ignored.  */
2409*4882a593Smuzhiyun 			if (!ksym &&
2410*4882a593Smuzhiyun 			    (ELF_ST_BIND(sym[i].st_info) == STB_WEAK ||
2411*4882a593Smuzhiyun 			     ignore_undef_symbol(info->hdr->e_machine, name)))
2412*4882a593Smuzhiyun 				break;
2413*4882a593Smuzhiyun 
2414*4882a593Smuzhiyun 			ret = PTR_ERR(ksym) ?: -ENOENT;
2415*4882a593Smuzhiyun 			pr_warn("%s: Unknown symbol %s (err %d)\n",
2416*4882a593Smuzhiyun 				mod->name, name, ret);
2417*4882a593Smuzhiyun 			break;
2418*4882a593Smuzhiyun 
2419*4882a593Smuzhiyun 		default:
2420*4882a593Smuzhiyun 			/* Divert to percpu allocation if a percpu var. */
2421*4882a593Smuzhiyun 			if (sym[i].st_shndx == info->index.pcpu)
2422*4882a593Smuzhiyun 				secbase = (unsigned long)mod_percpu(mod);
2423*4882a593Smuzhiyun 			else
2424*4882a593Smuzhiyun 				secbase = info->sechdrs[sym[i].st_shndx].sh_addr;
2425*4882a593Smuzhiyun 			sym[i].st_value += secbase;
2426*4882a593Smuzhiyun 			break;
2427*4882a593Smuzhiyun 		}
2428*4882a593Smuzhiyun 	}
2429*4882a593Smuzhiyun 
2430*4882a593Smuzhiyun 	return ret;
2431*4882a593Smuzhiyun }
2432*4882a593Smuzhiyun 
apply_relocations(struct module * mod,const struct load_info * info)2433*4882a593Smuzhiyun static int apply_relocations(struct module *mod, const struct load_info *info)
2434*4882a593Smuzhiyun {
2435*4882a593Smuzhiyun 	unsigned int i;
2436*4882a593Smuzhiyun 	int err = 0;
2437*4882a593Smuzhiyun 
2438*4882a593Smuzhiyun 	/* Now do relocations. */
2439*4882a593Smuzhiyun 	for (i = 1; i < info->hdr->e_shnum; i++) {
2440*4882a593Smuzhiyun 		unsigned int infosec = info->sechdrs[i].sh_info;
2441*4882a593Smuzhiyun 
2442*4882a593Smuzhiyun 		/* Not a valid relocation section? */
2443*4882a593Smuzhiyun 		if (infosec >= info->hdr->e_shnum)
2444*4882a593Smuzhiyun 			continue;
2445*4882a593Smuzhiyun 
2446*4882a593Smuzhiyun 		/* Don't bother with non-allocated sections */
2447*4882a593Smuzhiyun 		if (!(info->sechdrs[infosec].sh_flags & SHF_ALLOC))
2448*4882a593Smuzhiyun 			continue;
2449*4882a593Smuzhiyun 
2450*4882a593Smuzhiyun 		if (info->sechdrs[i].sh_flags & SHF_RELA_LIVEPATCH)
2451*4882a593Smuzhiyun 			err = klp_apply_section_relocs(mod, info->sechdrs,
2452*4882a593Smuzhiyun 						       info->secstrings,
2453*4882a593Smuzhiyun 						       info->strtab,
2454*4882a593Smuzhiyun 						       info->index.sym, i,
2455*4882a593Smuzhiyun 						       NULL);
2456*4882a593Smuzhiyun 		else if (info->sechdrs[i].sh_type == SHT_REL)
2457*4882a593Smuzhiyun 			err = apply_relocate(info->sechdrs, info->strtab,
2458*4882a593Smuzhiyun 					     info->index.sym, i, mod);
2459*4882a593Smuzhiyun 		else if (info->sechdrs[i].sh_type == SHT_RELA)
2460*4882a593Smuzhiyun 			err = apply_relocate_add(info->sechdrs, info->strtab,
2461*4882a593Smuzhiyun 						 info->index.sym, i, mod);
2462*4882a593Smuzhiyun 		if (err < 0)
2463*4882a593Smuzhiyun 			break;
2464*4882a593Smuzhiyun 	}
2465*4882a593Smuzhiyun 	return err;
2466*4882a593Smuzhiyun }
2467*4882a593Smuzhiyun 
2468*4882a593Smuzhiyun /* Additional bytes needed by arch in front of individual sections */
arch_mod_section_prepend(struct module * mod,unsigned int section)2469*4882a593Smuzhiyun unsigned int __weak arch_mod_section_prepend(struct module *mod,
2470*4882a593Smuzhiyun 					     unsigned int section)
2471*4882a593Smuzhiyun {
2472*4882a593Smuzhiyun 	/* default implementation just returns zero */
2473*4882a593Smuzhiyun 	return 0;
2474*4882a593Smuzhiyun }
2475*4882a593Smuzhiyun 
2476*4882a593Smuzhiyun /* Update size with this section: return offset. */
get_offset(struct module * mod,unsigned int * size,Elf_Shdr * sechdr,unsigned int section)2477*4882a593Smuzhiyun static long get_offset(struct module *mod, unsigned int *size,
2478*4882a593Smuzhiyun 		       Elf_Shdr *sechdr, unsigned int section)
2479*4882a593Smuzhiyun {
2480*4882a593Smuzhiyun 	long ret;
2481*4882a593Smuzhiyun 
2482*4882a593Smuzhiyun 	*size += arch_mod_section_prepend(mod, section);
2483*4882a593Smuzhiyun 	ret = ALIGN(*size, sechdr->sh_addralign ?: 1);
2484*4882a593Smuzhiyun 	*size = ret + sechdr->sh_size;
2485*4882a593Smuzhiyun 	return ret;
2486*4882a593Smuzhiyun }
2487*4882a593Smuzhiyun 
2488*4882a593Smuzhiyun /* Lay out the SHF_ALLOC sections in a way not dissimilar to how ld
2489*4882a593Smuzhiyun    might -- code, read-only data, read-write data, small data.  Tally
2490*4882a593Smuzhiyun    sizes, and place the offsets into sh_entsize fields: high bit means it
2491*4882a593Smuzhiyun    belongs in init. */
layout_sections(struct module * mod,struct load_info * info)2492*4882a593Smuzhiyun static void layout_sections(struct module *mod, struct load_info *info)
2493*4882a593Smuzhiyun {
2494*4882a593Smuzhiyun 	static unsigned long const masks[][2] = {
2495*4882a593Smuzhiyun 		/* NOTE: all executable code must be the first section
2496*4882a593Smuzhiyun 		 * in this array; otherwise modify the text_size
2497*4882a593Smuzhiyun 		 * finder in the two loops below */
2498*4882a593Smuzhiyun 		{ SHF_EXECINSTR | SHF_ALLOC, ARCH_SHF_SMALL },
2499*4882a593Smuzhiyun 		{ SHF_ALLOC, SHF_WRITE | ARCH_SHF_SMALL },
2500*4882a593Smuzhiyun 		{ SHF_RO_AFTER_INIT | SHF_ALLOC, ARCH_SHF_SMALL },
2501*4882a593Smuzhiyun 		{ SHF_WRITE | SHF_ALLOC, ARCH_SHF_SMALL },
2502*4882a593Smuzhiyun 		{ ARCH_SHF_SMALL | SHF_ALLOC, 0 }
2503*4882a593Smuzhiyun 	};
2504*4882a593Smuzhiyun 	unsigned int m, i;
2505*4882a593Smuzhiyun 
2506*4882a593Smuzhiyun 	for (i = 0; i < info->hdr->e_shnum; i++)
2507*4882a593Smuzhiyun 		info->sechdrs[i].sh_entsize = ~0UL;
2508*4882a593Smuzhiyun 
2509*4882a593Smuzhiyun 	pr_debug("Core section allocation order:\n");
2510*4882a593Smuzhiyun 	for (m = 0; m < ARRAY_SIZE(masks); ++m) {
2511*4882a593Smuzhiyun 		for (i = 0; i < info->hdr->e_shnum; ++i) {
2512*4882a593Smuzhiyun 			Elf_Shdr *s = &info->sechdrs[i];
2513*4882a593Smuzhiyun 			const char *sname = info->secstrings + s->sh_name;
2514*4882a593Smuzhiyun 
2515*4882a593Smuzhiyun 			if ((s->sh_flags & masks[m][0]) != masks[m][0]
2516*4882a593Smuzhiyun 			    || (s->sh_flags & masks[m][1])
2517*4882a593Smuzhiyun 			    || s->sh_entsize != ~0UL
2518*4882a593Smuzhiyun 			    || module_init_layout_section(sname))
2519*4882a593Smuzhiyun 				continue;
2520*4882a593Smuzhiyun 			s->sh_entsize = get_offset(mod, &mod->core_layout.size, s, i);
2521*4882a593Smuzhiyun 			pr_debug("\t%s\n", sname);
2522*4882a593Smuzhiyun 		}
2523*4882a593Smuzhiyun 		switch (m) {
2524*4882a593Smuzhiyun 		case 0: /* executable */
2525*4882a593Smuzhiyun 			mod->core_layout.size = debug_align(mod->core_layout.size);
2526*4882a593Smuzhiyun 			mod->core_layout.text_size = mod->core_layout.size;
2527*4882a593Smuzhiyun 			break;
2528*4882a593Smuzhiyun 		case 1: /* RO: text and ro-data */
2529*4882a593Smuzhiyun 			mod->core_layout.size = debug_align(mod->core_layout.size);
2530*4882a593Smuzhiyun 			mod->core_layout.ro_size = mod->core_layout.size;
2531*4882a593Smuzhiyun 			break;
2532*4882a593Smuzhiyun 		case 2: /* RO after init */
2533*4882a593Smuzhiyun 			mod->core_layout.size = debug_align(mod->core_layout.size);
2534*4882a593Smuzhiyun 			mod->core_layout.ro_after_init_size = mod->core_layout.size;
2535*4882a593Smuzhiyun 			break;
2536*4882a593Smuzhiyun 		case 4: /* whole core */
2537*4882a593Smuzhiyun 			mod->core_layout.size = debug_align(mod->core_layout.size);
2538*4882a593Smuzhiyun 			break;
2539*4882a593Smuzhiyun 		}
2540*4882a593Smuzhiyun 	}
2541*4882a593Smuzhiyun 
2542*4882a593Smuzhiyun 	pr_debug("Init section allocation order:\n");
2543*4882a593Smuzhiyun 	for (m = 0; m < ARRAY_SIZE(masks); ++m) {
2544*4882a593Smuzhiyun 		for (i = 0; i < info->hdr->e_shnum; ++i) {
2545*4882a593Smuzhiyun 			Elf_Shdr *s = &info->sechdrs[i];
2546*4882a593Smuzhiyun 			const char *sname = info->secstrings + s->sh_name;
2547*4882a593Smuzhiyun 
2548*4882a593Smuzhiyun 			if ((s->sh_flags & masks[m][0]) != masks[m][0]
2549*4882a593Smuzhiyun 			    || (s->sh_flags & masks[m][1])
2550*4882a593Smuzhiyun 			    || s->sh_entsize != ~0UL
2551*4882a593Smuzhiyun 			    || !module_init_layout_section(sname))
2552*4882a593Smuzhiyun 				continue;
2553*4882a593Smuzhiyun 			s->sh_entsize = (get_offset(mod, &mod->init_layout.size, s, i)
2554*4882a593Smuzhiyun 					 | INIT_OFFSET_MASK);
2555*4882a593Smuzhiyun 			pr_debug("\t%s\n", sname);
2556*4882a593Smuzhiyun 		}
2557*4882a593Smuzhiyun 		switch (m) {
2558*4882a593Smuzhiyun 		case 0: /* executable */
2559*4882a593Smuzhiyun 			mod->init_layout.size = debug_align(mod->init_layout.size);
2560*4882a593Smuzhiyun 			mod->init_layout.text_size = mod->init_layout.size;
2561*4882a593Smuzhiyun 			break;
2562*4882a593Smuzhiyun 		case 1: /* RO: text and ro-data */
2563*4882a593Smuzhiyun 			mod->init_layout.size = debug_align(mod->init_layout.size);
2564*4882a593Smuzhiyun 			mod->init_layout.ro_size = mod->init_layout.size;
2565*4882a593Smuzhiyun 			break;
2566*4882a593Smuzhiyun 		case 2:
2567*4882a593Smuzhiyun 			/*
2568*4882a593Smuzhiyun 			 * RO after init doesn't apply to init_layout (only
2569*4882a593Smuzhiyun 			 * core_layout), so it just takes the value of ro_size.
2570*4882a593Smuzhiyun 			 */
2571*4882a593Smuzhiyun 			mod->init_layout.ro_after_init_size = mod->init_layout.ro_size;
2572*4882a593Smuzhiyun 			break;
2573*4882a593Smuzhiyun 		case 4: /* whole init */
2574*4882a593Smuzhiyun 			mod->init_layout.size = debug_align(mod->init_layout.size);
2575*4882a593Smuzhiyun 			break;
2576*4882a593Smuzhiyun 		}
2577*4882a593Smuzhiyun 	}
2578*4882a593Smuzhiyun }
2579*4882a593Smuzhiyun 
set_license(struct module * mod,const char * license)2580*4882a593Smuzhiyun static void set_license(struct module *mod, const char *license)
2581*4882a593Smuzhiyun {
2582*4882a593Smuzhiyun 	if (!license)
2583*4882a593Smuzhiyun 		license = "unspecified";
2584*4882a593Smuzhiyun 
2585*4882a593Smuzhiyun 	if (!license_is_gpl_compatible(license)) {
2586*4882a593Smuzhiyun 		if (!test_taint(TAINT_PROPRIETARY_MODULE))
2587*4882a593Smuzhiyun 			pr_warn("%s: module license '%s' taints kernel.\n",
2588*4882a593Smuzhiyun 				mod->name, license);
2589*4882a593Smuzhiyun 		add_taint_module(mod, TAINT_PROPRIETARY_MODULE,
2590*4882a593Smuzhiyun 				 LOCKDEP_NOW_UNRELIABLE);
2591*4882a593Smuzhiyun 	}
2592*4882a593Smuzhiyun }
2593*4882a593Smuzhiyun 
2594*4882a593Smuzhiyun /* Parse tag=value strings from .modinfo section */
next_string(char * string,unsigned long * secsize)2595*4882a593Smuzhiyun static char *next_string(char *string, unsigned long *secsize)
2596*4882a593Smuzhiyun {
2597*4882a593Smuzhiyun 	/* Skip non-zero chars */
2598*4882a593Smuzhiyun 	while (string[0]) {
2599*4882a593Smuzhiyun 		string++;
2600*4882a593Smuzhiyun 		if ((*secsize)-- <= 1)
2601*4882a593Smuzhiyun 			return NULL;
2602*4882a593Smuzhiyun 	}
2603*4882a593Smuzhiyun 
2604*4882a593Smuzhiyun 	/* Skip any zero padding. */
2605*4882a593Smuzhiyun 	while (!string[0]) {
2606*4882a593Smuzhiyun 		string++;
2607*4882a593Smuzhiyun 		if ((*secsize)-- <= 1)
2608*4882a593Smuzhiyun 			return NULL;
2609*4882a593Smuzhiyun 	}
2610*4882a593Smuzhiyun 	return string;
2611*4882a593Smuzhiyun }
2612*4882a593Smuzhiyun 
get_next_modinfo(const struct load_info * info,const char * tag,char * prev)2613*4882a593Smuzhiyun static char *get_next_modinfo(const struct load_info *info, const char *tag,
2614*4882a593Smuzhiyun 			      char *prev)
2615*4882a593Smuzhiyun {
2616*4882a593Smuzhiyun 	char *p;
2617*4882a593Smuzhiyun 	unsigned int taglen = strlen(tag);
2618*4882a593Smuzhiyun 	Elf_Shdr *infosec = &info->sechdrs[info->index.info];
2619*4882a593Smuzhiyun 	unsigned long size = infosec->sh_size;
2620*4882a593Smuzhiyun 
2621*4882a593Smuzhiyun 	/*
2622*4882a593Smuzhiyun 	 * get_modinfo() calls made before rewrite_section_headers()
2623*4882a593Smuzhiyun 	 * must use sh_offset, as sh_addr isn't set!
2624*4882a593Smuzhiyun 	 */
2625*4882a593Smuzhiyun 	char *modinfo = (char *)info->hdr + infosec->sh_offset;
2626*4882a593Smuzhiyun 
2627*4882a593Smuzhiyun 	if (prev) {
2628*4882a593Smuzhiyun 		size -= prev - modinfo;
2629*4882a593Smuzhiyun 		modinfo = next_string(prev, &size);
2630*4882a593Smuzhiyun 	}
2631*4882a593Smuzhiyun 
2632*4882a593Smuzhiyun 	for (p = modinfo; p; p = next_string(p, &size)) {
2633*4882a593Smuzhiyun 		if (strncmp(p, tag, taglen) == 0 && p[taglen] == '=')
2634*4882a593Smuzhiyun 			return p + taglen + 1;
2635*4882a593Smuzhiyun 	}
2636*4882a593Smuzhiyun 	return NULL;
2637*4882a593Smuzhiyun }
2638*4882a593Smuzhiyun 
get_modinfo(const struct load_info * info,const char * tag)2639*4882a593Smuzhiyun static char *get_modinfo(const struct load_info *info, const char *tag)
2640*4882a593Smuzhiyun {
2641*4882a593Smuzhiyun 	return get_next_modinfo(info, tag, NULL);
2642*4882a593Smuzhiyun }
2643*4882a593Smuzhiyun 
setup_modinfo(struct module * mod,struct load_info * info)2644*4882a593Smuzhiyun static void setup_modinfo(struct module *mod, struct load_info *info)
2645*4882a593Smuzhiyun {
2646*4882a593Smuzhiyun 	struct module_attribute *attr;
2647*4882a593Smuzhiyun 	int i;
2648*4882a593Smuzhiyun 
2649*4882a593Smuzhiyun 	for (i = 0; (attr = modinfo_attrs[i]); i++) {
2650*4882a593Smuzhiyun 		if (attr->setup)
2651*4882a593Smuzhiyun 			attr->setup(mod, get_modinfo(info, attr->attr.name));
2652*4882a593Smuzhiyun 	}
2653*4882a593Smuzhiyun }
2654*4882a593Smuzhiyun 
free_modinfo(struct module * mod)2655*4882a593Smuzhiyun static void free_modinfo(struct module *mod)
2656*4882a593Smuzhiyun {
2657*4882a593Smuzhiyun 	struct module_attribute *attr;
2658*4882a593Smuzhiyun 	int i;
2659*4882a593Smuzhiyun 
2660*4882a593Smuzhiyun 	for (i = 0; (attr = modinfo_attrs[i]); i++) {
2661*4882a593Smuzhiyun 		if (attr->free)
2662*4882a593Smuzhiyun 			attr->free(mod);
2663*4882a593Smuzhiyun 	}
2664*4882a593Smuzhiyun }
2665*4882a593Smuzhiyun 
2666*4882a593Smuzhiyun #ifdef CONFIG_KALLSYMS
2667*4882a593Smuzhiyun 
2668*4882a593Smuzhiyun /* Lookup exported symbol in given range of kernel_symbols */
lookup_exported_symbol(const char * name,const struct kernel_symbol * start,const struct kernel_symbol * stop)2669*4882a593Smuzhiyun static const struct kernel_symbol *lookup_exported_symbol(const char *name,
2670*4882a593Smuzhiyun 							  const struct kernel_symbol *start,
2671*4882a593Smuzhiyun 							  const struct kernel_symbol *stop)
2672*4882a593Smuzhiyun {
2673*4882a593Smuzhiyun 	return bsearch(name, start, stop - start,
2674*4882a593Smuzhiyun 			sizeof(struct kernel_symbol), cmp_name);
2675*4882a593Smuzhiyun }
2676*4882a593Smuzhiyun 
is_exported(const char * name,unsigned long value,const struct module * mod)2677*4882a593Smuzhiyun static int is_exported(const char *name, unsigned long value,
2678*4882a593Smuzhiyun 		       const struct module *mod)
2679*4882a593Smuzhiyun {
2680*4882a593Smuzhiyun 	const struct kernel_symbol *ks;
2681*4882a593Smuzhiyun 	if (!mod)
2682*4882a593Smuzhiyun 		ks = lookup_exported_symbol(name, __start___ksymtab, __stop___ksymtab);
2683*4882a593Smuzhiyun 	else
2684*4882a593Smuzhiyun 		ks = lookup_exported_symbol(name, mod->syms, mod->syms + mod->num_syms);
2685*4882a593Smuzhiyun 
2686*4882a593Smuzhiyun 	return ks != NULL && kernel_symbol_value(ks) == value;
2687*4882a593Smuzhiyun }
2688*4882a593Smuzhiyun 
2689*4882a593Smuzhiyun /* As per nm */
elf_type(const Elf_Sym * sym,const struct load_info * info)2690*4882a593Smuzhiyun static char elf_type(const Elf_Sym *sym, const struct load_info *info)
2691*4882a593Smuzhiyun {
2692*4882a593Smuzhiyun 	const Elf_Shdr *sechdrs = info->sechdrs;
2693*4882a593Smuzhiyun 
2694*4882a593Smuzhiyun 	if (ELF_ST_BIND(sym->st_info) == STB_WEAK) {
2695*4882a593Smuzhiyun 		if (ELF_ST_TYPE(sym->st_info) == STT_OBJECT)
2696*4882a593Smuzhiyun 			return 'v';
2697*4882a593Smuzhiyun 		else
2698*4882a593Smuzhiyun 			return 'w';
2699*4882a593Smuzhiyun 	}
2700*4882a593Smuzhiyun 	if (sym->st_shndx == SHN_UNDEF)
2701*4882a593Smuzhiyun 		return 'U';
2702*4882a593Smuzhiyun 	if (sym->st_shndx == SHN_ABS || sym->st_shndx == info->index.pcpu)
2703*4882a593Smuzhiyun 		return 'a';
2704*4882a593Smuzhiyun 	if (sym->st_shndx >= SHN_LORESERVE)
2705*4882a593Smuzhiyun 		return '?';
2706*4882a593Smuzhiyun 	if (sechdrs[sym->st_shndx].sh_flags & SHF_EXECINSTR)
2707*4882a593Smuzhiyun 		return 't';
2708*4882a593Smuzhiyun 	if (sechdrs[sym->st_shndx].sh_flags & SHF_ALLOC
2709*4882a593Smuzhiyun 	    && sechdrs[sym->st_shndx].sh_type != SHT_NOBITS) {
2710*4882a593Smuzhiyun 		if (!(sechdrs[sym->st_shndx].sh_flags & SHF_WRITE))
2711*4882a593Smuzhiyun 			return 'r';
2712*4882a593Smuzhiyun 		else if (sechdrs[sym->st_shndx].sh_flags & ARCH_SHF_SMALL)
2713*4882a593Smuzhiyun 			return 'g';
2714*4882a593Smuzhiyun 		else
2715*4882a593Smuzhiyun 			return 'd';
2716*4882a593Smuzhiyun 	}
2717*4882a593Smuzhiyun 	if (sechdrs[sym->st_shndx].sh_type == SHT_NOBITS) {
2718*4882a593Smuzhiyun 		if (sechdrs[sym->st_shndx].sh_flags & ARCH_SHF_SMALL)
2719*4882a593Smuzhiyun 			return 's';
2720*4882a593Smuzhiyun 		else
2721*4882a593Smuzhiyun 			return 'b';
2722*4882a593Smuzhiyun 	}
2723*4882a593Smuzhiyun 	if (strstarts(info->secstrings + sechdrs[sym->st_shndx].sh_name,
2724*4882a593Smuzhiyun 		      ".debug")) {
2725*4882a593Smuzhiyun 		return 'n';
2726*4882a593Smuzhiyun 	}
2727*4882a593Smuzhiyun 	return '?';
2728*4882a593Smuzhiyun }
2729*4882a593Smuzhiyun 
is_core_symbol(const Elf_Sym * src,const Elf_Shdr * sechdrs,unsigned int shnum,unsigned int pcpundx)2730*4882a593Smuzhiyun static bool is_core_symbol(const Elf_Sym *src, const Elf_Shdr *sechdrs,
2731*4882a593Smuzhiyun 			unsigned int shnum, unsigned int pcpundx)
2732*4882a593Smuzhiyun {
2733*4882a593Smuzhiyun 	const Elf_Shdr *sec;
2734*4882a593Smuzhiyun 
2735*4882a593Smuzhiyun 	if (src->st_shndx == SHN_UNDEF
2736*4882a593Smuzhiyun 	    || src->st_shndx >= shnum
2737*4882a593Smuzhiyun 	    || !src->st_name)
2738*4882a593Smuzhiyun 		return false;
2739*4882a593Smuzhiyun 
2740*4882a593Smuzhiyun #ifdef CONFIG_KALLSYMS_ALL
2741*4882a593Smuzhiyun 	if (src->st_shndx == pcpundx)
2742*4882a593Smuzhiyun 		return true;
2743*4882a593Smuzhiyun #endif
2744*4882a593Smuzhiyun 
2745*4882a593Smuzhiyun 	sec = sechdrs + src->st_shndx;
2746*4882a593Smuzhiyun 	if (!(sec->sh_flags & SHF_ALLOC)
2747*4882a593Smuzhiyun #ifndef CONFIG_KALLSYMS_ALL
2748*4882a593Smuzhiyun 	    || !(sec->sh_flags & SHF_EXECINSTR)
2749*4882a593Smuzhiyun #endif
2750*4882a593Smuzhiyun 	    || (sec->sh_entsize & INIT_OFFSET_MASK))
2751*4882a593Smuzhiyun 		return false;
2752*4882a593Smuzhiyun 
2753*4882a593Smuzhiyun 	return true;
2754*4882a593Smuzhiyun }
2755*4882a593Smuzhiyun 
2756*4882a593Smuzhiyun /*
2757*4882a593Smuzhiyun  * We only allocate and copy the strings needed by the parts of symtab
2758*4882a593Smuzhiyun  * we keep.  This is simple, but has the effect of making multiple
2759*4882a593Smuzhiyun  * copies of duplicates.  We could be more sophisticated, see
2760*4882a593Smuzhiyun  * linux-kernel thread starting with
2761*4882a593Smuzhiyun  * <73defb5e4bca04a6431392cc341112b1@localhost>.
2762*4882a593Smuzhiyun  */
layout_symtab(struct module * mod,struct load_info * info)2763*4882a593Smuzhiyun static void layout_symtab(struct module *mod, struct load_info *info)
2764*4882a593Smuzhiyun {
2765*4882a593Smuzhiyun 	Elf_Shdr *symsect = info->sechdrs + info->index.sym;
2766*4882a593Smuzhiyun 	Elf_Shdr *strsect = info->sechdrs + info->index.str;
2767*4882a593Smuzhiyun 	const Elf_Sym *src;
2768*4882a593Smuzhiyun 	unsigned int i, nsrc, ndst, strtab_size = 0;
2769*4882a593Smuzhiyun 
2770*4882a593Smuzhiyun 	/* Put symbol section at end of init part of module. */
2771*4882a593Smuzhiyun 	symsect->sh_flags |= SHF_ALLOC;
2772*4882a593Smuzhiyun 	symsect->sh_entsize = get_offset(mod, &mod->init_layout.size, symsect,
2773*4882a593Smuzhiyun 					 info->index.sym) | INIT_OFFSET_MASK;
2774*4882a593Smuzhiyun 	pr_debug("\t%s\n", info->secstrings + symsect->sh_name);
2775*4882a593Smuzhiyun 
2776*4882a593Smuzhiyun 	src = (void *)info->hdr + symsect->sh_offset;
2777*4882a593Smuzhiyun 	nsrc = symsect->sh_size / sizeof(*src);
2778*4882a593Smuzhiyun 
2779*4882a593Smuzhiyun 	/* Compute total space required for the core symbols' strtab. */
2780*4882a593Smuzhiyun 	for (ndst = i = 0; i < nsrc; i++) {
2781*4882a593Smuzhiyun 		if (i == 0 || is_livepatch_module(mod) ||
2782*4882a593Smuzhiyun 		    is_core_symbol(src+i, info->sechdrs, info->hdr->e_shnum,
2783*4882a593Smuzhiyun 				   info->index.pcpu)) {
2784*4882a593Smuzhiyun 			strtab_size += strlen(&info->strtab[src[i].st_name])+1;
2785*4882a593Smuzhiyun 			ndst++;
2786*4882a593Smuzhiyun 		}
2787*4882a593Smuzhiyun 	}
2788*4882a593Smuzhiyun 
2789*4882a593Smuzhiyun 	/* Append room for core symbols at end of core part. */
2790*4882a593Smuzhiyun 	info->symoffs = ALIGN(mod->core_layout.size, symsect->sh_addralign ?: 1);
2791*4882a593Smuzhiyun 	info->stroffs = mod->core_layout.size = info->symoffs + ndst * sizeof(Elf_Sym);
2792*4882a593Smuzhiyun 	mod->core_layout.size += strtab_size;
2793*4882a593Smuzhiyun 	info->core_typeoffs = mod->core_layout.size;
2794*4882a593Smuzhiyun 	mod->core_layout.size += ndst * sizeof(char);
2795*4882a593Smuzhiyun 	mod->core_layout.size = debug_align(mod->core_layout.size);
2796*4882a593Smuzhiyun 
2797*4882a593Smuzhiyun 	/* Put string table section at end of init part of module. */
2798*4882a593Smuzhiyun 	strsect->sh_flags |= SHF_ALLOC;
2799*4882a593Smuzhiyun 	strsect->sh_entsize = get_offset(mod, &mod->init_layout.size, strsect,
2800*4882a593Smuzhiyun 					 info->index.str) | INIT_OFFSET_MASK;
2801*4882a593Smuzhiyun 	pr_debug("\t%s\n", info->secstrings + strsect->sh_name);
2802*4882a593Smuzhiyun 
2803*4882a593Smuzhiyun 	/* We'll tack temporary mod_kallsyms on the end. */
2804*4882a593Smuzhiyun 	mod->init_layout.size = ALIGN(mod->init_layout.size,
2805*4882a593Smuzhiyun 				      __alignof__(struct mod_kallsyms));
2806*4882a593Smuzhiyun 	info->mod_kallsyms_init_off = mod->init_layout.size;
2807*4882a593Smuzhiyun 	mod->init_layout.size += sizeof(struct mod_kallsyms);
2808*4882a593Smuzhiyun 	info->init_typeoffs = mod->init_layout.size;
2809*4882a593Smuzhiyun 	mod->init_layout.size += nsrc * sizeof(char);
2810*4882a593Smuzhiyun 	mod->init_layout.size = debug_align(mod->init_layout.size);
2811*4882a593Smuzhiyun }
2812*4882a593Smuzhiyun 
2813*4882a593Smuzhiyun /*
2814*4882a593Smuzhiyun  * We use the full symtab and strtab which layout_symtab arranged to
2815*4882a593Smuzhiyun  * be appended to the init section.  Later we switch to the cut-down
2816*4882a593Smuzhiyun  * core-only ones.
2817*4882a593Smuzhiyun  */
add_kallsyms(struct module * mod,const struct load_info * info)2818*4882a593Smuzhiyun static void add_kallsyms(struct module *mod, const struct load_info *info)
2819*4882a593Smuzhiyun {
2820*4882a593Smuzhiyun 	unsigned int i, ndst;
2821*4882a593Smuzhiyun 	const Elf_Sym *src;
2822*4882a593Smuzhiyun 	Elf_Sym *dst;
2823*4882a593Smuzhiyun 	char *s;
2824*4882a593Smuzhiyun 	Elf_Shdr *symsec = &info->sechdrs[info->index.sym];
2825*4882a593Smuzhiyun 
2826*4882a593Smuzhiyun 	/* Set up to point into init section. */
2827*4882a593Smuzhiyun 	mod->kallsyms = mod->init_layout.base + info->mod_kallsyms_init_off;
2828*4882a593Smuzhiyun 
2829*4882a593Smuzhiyun 	mod->kallsyms->symtab = (void *)symsec->sh_addr;
2830*4882a593Smuzhiyun 	mod->kallsyms->num_symtab = symsec->sh_size / sizeof(Elf_Sym);
2831*4882a593Smuzhiyun 	/* Make sure we get permanent strtab: don't use info->strtab. */
2832*4882a593Smuzhiyun 	mod->kallsyms->strtab = (void *)info->sechdrs[info->index.str].sh_addr;
2833*4882a593Smuzhiyun 	mod->kallsyms->typetab = mod->init_layout.base + info->init_typeoffs;
2834*4882a593Smuzhiyun 
2835*4882a593Smuzhiyun 	/*
2836*4882a593Smuzhiyun 	 * Now populate the cut down core kallsyms for after init
2837*4882a593Smuzhiyun 	 * and set types up while we still have access to sections.
2838*4882a593Smuzhiyun 	 */
2839*4882a593Smuzhiyun 	mod->core_kallsyms.symtab = dst = mod->core_layout.base + info->symoffs;
2840*4882a593Smuzhiyun 	mod->core_kallsyms.strtab = s = mod->core_layout.base + info->stroffs;
2841*4882a593Smuzhiyun 	mod->core_kallsyms.typetab = mod->core_layout.base + info->core_typeoffs;
2842*4882a593Smuzhiyun 	src = mod->kallsyms->symtab;
2843*4882a593Smuzhiyun 	for (ndst = i = 0; i < mod->kallsyms->num_symtab; i++) {
2844*4882a593Smuzhiyun 		mod->kallsyms->typetab[i] = elf_type(src + i, info);
2845*4882a593Smuzhiyun 		if (i == 0 || is_livepatch_module(mod) ||
2846*4882a593Smuzhiyun 		    is_core_symbol(src+i, info->sechdrs, info->hdr->e_shnum,
2847*4882a593Smuzhiyun 				   info->index.pcpu)) {
2848*4882a593Smuzhiyun 			mod->core_kallsyms.typetab[ndst] =
2849*4882a593Smuzhiyun 			    mod->kallsyms->typetab[i];
2850*4882a593Smuzhiyun 			dst[ndst] = src[i];
2851*4882a593Smuzhiyun 			dst[ndst++].st_name = s - mod->core_kallsyms.strtab;
2852*4882a593Smuzhiyun 			s += strlcpy(s, &mod->kallsyms->strtab[src[i].st_name],
2853*4882a593Smuzhiyun 				     KSYM_NAME_LEN) + 1;
2854*4882a593Smuzhiyun 		}
2855*4882a593Smuzhiyun 	}
2856*4882a593Smuzhiyun 	mod->core_kallsyms.num_symtab = ndst;
2857*4882a593Smuzhiyun }
2858*4882a593Smuzhiyun #else
layout_symtab(struct module * mod,struct load_info * info)2859*4882a593Smuzhiyun static inline void layout_symtab(struct module *mod, struct load_info *info)
2860*4882a593Smuzhiyun {
2861*4882a593Smuzhiyun }
2862*4882a593Smuzhiyun 
add_kallsyms(struct module * mod,const struct load_info * info)2863*4882a593Smuzhiyun static void add_kallsyms(struct module *mod, const struct load_info *info)
2864*4882a593Smuzhiyun {
2865*4882a593Smuzhiyun }
2866*4882a593Smuzhiyun #endif /* CONFIG_KALLSYMS */
2867*4882a593Smuzhiyun 
dynamic_debug_setup(struct module * mod,struct _ddebug * debug,unsigned int num)2868*4882a593Smuzhiyun static void dynamic_debug_setup(struct module *mod, struct _ddebug *debug, unsigned int num)
2869*4882a593Smuzhiyun {
2870*4882a593Smuzhiyun 	if (!debug)
2871*4882a593Smuzhiyun 		return;
2872*4882a593Smuzhiyun 	ddebug_add_module(debug, num, mod->name);
2873*4882a593Smuzhiyun }
2874*4882a593Smuzhiyun 
dynamic_debug_remove(struct module * mod,struct _ddebug * debug)2875*4882a593Smuzhiyun static void dynamic_debug_remove(struct module *mod, struct _ddebug *debug)
2876*4882a593Smuzhiyun {
2877*4882a593Smuzhiyun 	if (debug)
2878*4882a593Smuzhiyun 		ddebug_remove_module(mod->name);
2879*4882a593Smuzhiyun }
2880*4882a593Smuzhiyun 
module_alloc(unsigned long size)2881*4882a593Smuzhiyun void * __weak module_alloc(unsigned long size)
2882*4882a593Smuzhiyun {
2883*4882a593Smuzhiyun 	return __vmalloc_node_range(size, 1, VMALLOC_START, VMALLOC_END,
2884*4882a593Smuzhiyun 			GFP_KERNEL, PAGE_KERNEL_EXEC, VM_FLUSH_RESET_PERMS,
2885*4882a593Smuzhiyun 			NUMA_NO_NODE, __builtin_return_address(0));
2886*4882a593Smuzhiyun }
2887*4882a593Smuzhiyun 
module_init_section(const char * name)2888*4882a593Smuzhiyun bool __weak module_init_section(const char *name)
2889*4882a593Smuzhiyun {
2890*4882a593Smuzhiyun 	return strstarts(name, ".init");
2891*4882a593Smuzhiyun }
2892*4882a593Smuzhiyun 
module_exit_section(const char * name)2893*4882a593Smuzhiyun bool __weak module_exit_section(const char *name)
2894*4882a593Smuzhiyun {
2895*4882a593Smuzhiyun 	return strstarts(name, ".exit");
2896*4882a593Smuzhiyun }
2897*4882a593Smuzhiyun 
2898*4882a593Smuzhiyun #ifdef CONFIG_DEBUG_KMEMLEAK
kmemleak_load_module(const struct module * mod,const struct load_info * info)2899*4882a593Smuzhiyun static void kmemleak_load_module(const struct module *mod,
2900*4882a593Smuzhiyun 				 const struct load_info *info)
2901*4882a593Smuzhiyun {
2902*4882a593Smuzhiyun 	unsigned int i;
2903*4882a593Smuzhiyun 
2904*4882a593Smuzhiyun 	/* only scan the sections containing data */
2905*4882a593Smuzhiyun 	kmemleak_scan_area(mod, sizeof(struct module), GFP_KERNEL);
2906*4882a593Smuzhiyun 
2907*4882a593Smuzhiyun 	for (i = 1; i < info->hdr->e_shnum; i++) {
2908*4882a593Smuzhiyun 		/* Scan all writable sections that's not executable */
2909*4882a593Smuzhiyun 		if (!(info->sechdrs[i].sh_flags & SHF_ALLOC) ||
2910*4882a593Smuzhiyun 		    !(info->sechdrs[i].sh_flags & SHF_WRITE) ||
2911*4882a593Smuzhiyun 		    (info->sechdrs[i].sh_flags & SHF_EXECINSTR))
2912*4882a593Smuzhiyun 			continue;
2913*4882a593Smuzhiyun 
2914*4882a593Smuzhiyun 		kmemleak_scan_area((void *)info->sechdrs[i].sh_addr,
2915*4882a593Smuzhiyun 				   info->sechdrs[i].sh_size, GFP_KERNEL);
2916*4882a593Smuzhiyun 	}
2917*4882a593Smuzhiyun }
2918*4882a593Smuzhiyun #else
kmemleak_load_module(const struct module * mod,const struct load_info * info)2919*4882a593Smuzhiyun static inline void kmemleak_load_module(const struct module *mod,
2920*4882a593Smuzhiyun 					const struct load_info *info)
2921*4882a593Smuzhiyun {
2922*4882a593Smuzhiyun }
2923*4882a593Smuzhiyun #endif
2924*4882a593Smuzhiyun 
2925*4882a593Smuzhiyun #ifdef CONFIG_MODULE_SIG
module_sig_check(struct load_info * info,int flags)2926*4882a593Smuzhiyun static int module_sig_check(struct load_info *info, int flags)
2927*4882a593Smuzhiyun {
2928*4882a593Smuzhiyun 	int err = -ENODATA;
2929*4882a593Smuzhiyun 	const unsigned long markerlen = sizeof(MODULE_SIG_STRING) - 1;
2930*4882a593Smuzhiyun 	const char *reason;
2931*4882a593Smuzhiyun 	const void *mod = info->hdr;
2932*4882a593Smuzhiyun 
2933*4882a593Smuzhiyun 	/*
2934*4882a593Smuzhiyun 	 * Require flags == 0, as a module with version information
2935*4882a593Smuzhiyun 	 * removed is no longer the module that was signed
2936*4882a593Smuzhiyun 	 */
2937*4882a593Smuzhiyun 	if (flags == 0 &&
2938*4882a593Smuzhiyun 	    info->len > markerlen &&
2939*4882a593Smuzhiyun 	    memcmp(mod + info->len - markerlen, MODULE_SIG_STRING, markerlen) == 0) {
2940*4882a593Smuzhiyun 		/* We truncate the module to discard the signature */
2941*4882a593Smuzhiyun 		info->len -= markerlen;
2942*4882a593Smuzhiyun 		err = mod_verify_sig(mod, info);
2943*4882a593Smuzhiyun 	}
2944*4882a593Smuzhiyun 
2945*4882a593Smuzhiyun 	switch (err) {
2946*4882a593Smuzhiyun 	case 0:
2947*4882a593Smuzhiyun 		info->sig_ok = true;
2948*4882a593Smuzhiyun 		return 0;
2949*4882a593Smuzhiyun 
2950*4882a593Smuzhiyun 		/* We don't permit modules to be loaded into trusted kernels
2951*4882a593Smuzhiyun 		 * without a valid signature on them, but if we're not
2952*4882a593Smuzhiyun 		 * enforcing, certain errors are non-fatal.
2953*4882a593Smuzhiyun 		 */
2954*4882a593Smuzhiyun 	case -ENODATA:
2955*4882a593Smuzhiyun 		reason = "unsigned module";
2956*4882a593Smuzhiyun 		break;
2957*4882a593Smuzhiyun 	case -ENOPKG:
2958*4882a593Smuzhiyun 		reason = "module with unsupported crypto";
2959*4882a593Smuzhiyun 		break;
2960*4882a593Smuzhiyun 	case -ENOKEY:
2961*4882a593Smuzhiyun 		reason = "module with unavailable key";
2962*4882a593Smuzhiyun 		break;
2963*4882a593Smuzhiyun 
2964*4882a593Smuzhiyun 		/* All other errors are fatal, including nomem, unparseable
2965*4882a593Smuzhiyun 		 * signatures and signature check failures - even if signatures
2966*4882a593Smuzhiyun 		 * aren't required.
2967*4882a593Smuzhiyun 		 */
2968*4882a593Smuzhiyun 	default:
2969*4882a593Smuzhiyun 		return err;
2970*4882a593Smuzhiyun 	}
2971*4882a593Smuzhiyun 
2972*4882a593Smuzhiyun 	if (is_module_sig_enforced()) {
2973*4882a593Smuzhiyun 		pr_notice("Loading of %s is rejected\n", reason);
2974*4882a593Smuzhiyun 		return -EKEYREJECTED;
2975*4882a593Smuzhiyun 	}
2976*4882a593Smuzhiyun 
2977*4882a593Smuzhiyun 	return security_locked_down(LOCKDOWN_MODULE_SIGNATURE);
2978*4882a593Smuzhiyun }
2979*4882a593Smuzhiyun #else /* !CONFIG_MODULE_SIG */
module_sig_check(struct load_info * info,int flags)2980*4882a593Smuzhiyun static int module_sig_check(struct load_info *info, int flags)
2981*4882a593Smuzhiyun {
2982*4882a593Smuzhiyun 	return 0;
2983*4882a593Smuzhiyun }
2984*4882a593Smuzhiyun #endif /* !CONFIG_MODULE_SIG */
2985*4882a593Smuzhiyun 
validate_section_offset(struct load_info * info,Elf_Shdr * shdr)2986*4882a593Smuzhiyun static int validate_section_offset(struct load_info *info, Elf_Shdr *shdr)
2987*4882a593Smuzhiyun {
2988*4882a593Smuzhiyun 	unsigned long secend;
2989*4882a593Smuzhiyun 
2990*4882a593Smuzhiyun 	/*
2991*4882a593Smuzhiyun 	 * Check for both overflow and offset/size being
2992*4882a593Smuzhiyun 	 * too large.
2993*4882a593Smuzhiyun 	 */
2994*4882a593Smuzhiyun 	secend = shdr->sh_offset + shdr->sh_size;
2995*4882a593Smuzhiyun 	if (secend < shdr->sh_offset || secend > info->len)
2996*4882a593Smuzhiyun 		return -ENOEXEC;
2997*4882a593Smuzhiyun 
2998*4882a593Smuzhiyun 	return 0;
2999*4882a593Smuzhiyun }
3000*4882a593Smuzhiyun 
3001*4882a593Smuzhiyun /*
3002*4882a593Smuzhiyun  * Sanity checks against invalid binaries, wrong arch, weird elf version.
3003*4882a593Smuzhiyun  *
3004*4882a593Smuzhiyun  * Also do basic validity checks against section offsets and sizes, the
3005*4882a593Smuzhiyun  * section name string table, and the indices used for it (sh_name).
3006*4882a593Smuzhiyun  */
elf_validity_check(struct load_info * info)3007*4882a593Smuzhiyun static int elf_validity_check(struct load_info *info)
3008*4882a593Smuzhiyun {
3009*4882a593Smuzhiyun 	unsigned int i;
3010*4882a593Smuzhiyun 	Elf_Shdr *shdr, *strhdr;
3011*4882a593Smuzhiyun 	int err;
3012*4882a593Smuzhiyun 
3013*4882a593Smuzhiyun 	if (info->len < sizeof(*(info->hdr)))
3014*4882a593Smuzhiyun 		return -ENOEXEC;
3015*4882a593Smuzhiyun 
3016*4882a593Smuzhiyun 	if (memcmp(info->hdr->e_ident, ELFMAG, SELFMAG) != 0
3017*4882a593Smuzhiyun 	    || info->hdr->e_type != ET_REL
3018*4882a593Smuzhiyun 	    || !elf_check_arch(info->hdr)
3019*4882a593Smuzhiyun 	    || info->hdr->e_shentsize != sizeof(Elf_Shdr))
3020*4882a593Smuzhiyun 		return -ENOEXEC;
3021*4882a593Smuzhiyun 
3022*4882a593Smuzhiyun 	/*
3023*4882a593Smuzhiyun 	 * e_shnum is 16 bits, and sizeof(Elf_Shdr) is
3024*4882a593Smuzhiyun 	 * known and small. So e_shnum * sizeof(Elf_Shdr)
3025*4882a593Smuzhiyun 	 * will not overflow unsigned long on any platform.
3026*4882a593Smuzhiyun 	 */
3027*4882a593Smuzhiyun 	if (info->hdr->e_shoff >= info->len
3028*4882a593Smuzhiyun 	    || (info->hdr->e_shnum * sizeof(Elf_Shdr) >
3029*4882a593Smuzhiyun 		info->len - info->hdr->e_shoff))
3030*4882a593Smuzhiyun 		return -ENOEXEC;
3031*4882a593Smuzhiyun 
3032*4882a593Smuzhiyun 	info->sechdrs = (void *)info->hdr + info->hdr->e_shoff;
3033*4882a593Smuzhiyun 
3034*4882a593Smuzhiyun 	/*
3035*4882a593Smuzhiyun 	 * Verify if the section name table index is valid.
3036*4882a593Smuzhiyun 	 */
3037*4882a593Smuzhiyun 	if (info->hdr->e_shstrndx == SHN_UNDEF
3038*4882a593Smuzhiyun 	    || info->hdr->e_shstrndx >= info->hdr->e_shnum)
3039*4882a593Smuzhiyun 		return -ENOEXEC;
3040*4882a593Smuzhiyun 
3041*4882a593Smuzhiyun 	strhdr = &info->sechdrs[info->hdr->e_shstrndx];
3042*4882a593Smuzhiyun 	err = validate_section_offset(info, strhdr);
3043*4882a593Smuzhiyun 	if (err < 0)
3044*4882a593Smuzhiyun 		return err;
3045*4882a593Smuzhiyun 
3046*4882a593Smuzhiyun 	/*
3047*4882a593Smuzhiyun 	 * The section name table must be NUL-terminated, as required
3048*4882a593Smuzhiyun 	 * by the spec. This makes strcmp and pr_* calls that access
3049*4882a593Smuzhiyun 	 * strings in the section safe.
3050*4882a593Smuzhiyun 	 */
3051*4882a593Smuzhiyun 	info->secstrings = (void *)info->hdr + strhdr->sh_offset;
3052*4882a593Smuzhiyun 	if (info->secstrings[strhdr->sh_size - 1] != '\0')
3053*4882a593Smuzhiyun 		return -ENOEXEC;
3054*4882a593Smuzhiyun 
3055*4882a593Smuzhiyun 	/*
3056*4882a593Smuzhiyun 	 * The code assumes that section 0 has a length of zero and
3057*4882a593Smuzhiyun 	 * an addr of zero, so check for it.
3058*4882a593Smuzhiyun 	 */
3059*4882a593Smuzhiyun 	if (info->sechdrs[0].sh_type != SHT_NULL
3060*4882a593Smuzhiyun 	    || info->sechdrs[0].sh_size != 0
3061*4882a593Smuzhiyun 	    || info->sechdrs[0].sh_addr != 0)
3062*4882a593Smuzhiyun 		return -ENOEXEC;
3063*4882a593Smuzhiyun 
3064*4882a593Smuzhiyun 	for (i = 1; i < info->hdr->e_shnum; i++) {
3065*4882a593Smuzhiyun 		shdr = &info->sechdrs[i];
3066*4882a593Smuzhiyun 		switch (shdr->sh_type) {
3067*4882a593Smuzhiyun 		case SHT_NULL:
3068*4882a593Smuzhiyun 		case SHT_NOBITS:
3069*4882a593Smuzhiyun 			continue;
3070*4882a593Smuzhiyun 		case SHT_SYMTAB:
3071*4882a593Smuzhiyun 			if (shdr->sh_link == SHN_UNDEF
3072*4882a593Smuzhiyun 			    || shdr->sh_link >= info->hdr->e_shnum)
3073*4882a593Smuzhiyun 				return -ENOEXEC;
3074*4882a593Smuzhiyun 			fallthrough;
3075*4882a593Smuzhiyun 		default:
3076*4882a593Smuzhiyun 			err = validate_section_offset(info, shdr);
3077*4882a593Smuzhiyun 			if (err < 0) {
3078*4882a593Smuzhiyun 				pr_err("Invalid ELF section in module (section %u type %u)\n",
3079*4882a593Smuzhiyun 					i, shdr->sh_type);
3080*4882a593Smuzhiyun 				return err;
3081*4882a593Smuzhiyun 			}
3082*4882a593Smuzhiyun 
3083*4882a593Smuzhiyun 			if (shdr->sh_flags & SHF_ALLOC) {
3084*4882a593Smuzhiyun 				if (shdr->sh_name >= strhdr->sh_size) {
3085*4882a593Smuzhiyun 					pr_err("Invalid ELF section name in module (section %u type %u)\n",
3086*4882a593Smuzhiyun 					       i, shdr->sh_type);
3087*4882a593Smuzhiyun 					return -ENOEXEC;
3088*4882a593Smuzhiyun 				}
3089*4882a593Smuzhiyun 			}
3090*4882a593Smuzhiyun 			break;
3091*4882a593Smuzhiyun 		}
3092*4882a593Smuzhiyun 	}
3093*4882a593Smuzhiyun 
3094*4882a593Smuzhiyun 	return 0;
3095*4882a593Smuzhiyun }
3096*4882a593Smuzhiyun 
3097*4882a593Smuzhiyun #define COPY_CHUNK_SIZE (16*PAGE_SIZE)
3098*4882a593Smuzhiyun 
copy_chunked_from_user(void * dst,const void __user * usrc,unsigned long len)3099*4882a593Smuzhiyun static int copy_chunked_from_user(void *dst, const void __user *usrc, unsigned long len)
3100*4882a593Smuzhiyun {
3101*4882a593Smuzhiyun 	do {
3102*4882a593Smuzhiyun 		unsigned long n = min(len, COPY_CHUNK_SIZE);
3103*4882a593Smuzhiyun 
3104*4882a593Smuzhiyun 		if (copy_from_user(dst, usrc, n) != 0)
3105*4882a593Smuzhiyun 			return -EFAULT;
3106*4882a593Smuzhiyun 		cond_resched();
3107*4882a593Smuzhiyun 		dst += n;
3108*4882a593Smuzhiyun 		usrc += n;
3109*4882a593Smuzhiyun 		len -= n;
3110*4882a593Smuzhiyun 	} while (len);
3111*4882a593Smuzhiyun 	return 0;
3112*4882a593Smuzhiyun }
3113*4882a593Smuzhiyun 
3114*4882a593Smuzhiyun #ifdef CONFIG_LIVEPATCH
check_modinfo_livepatch(struct module * mod,struct load_info * info)3115*4882a593Smuzhiyun static int check_modinfo_livepatch(struct module *mod, struct load_info *info)
3116*4882a593Smuzhiyun {
3117*4882a593Smuzhiyun 	if (get_modinfo(info, "livepatch")) {
3118*4882a593Smuzhiyun 		mod->klp = true;
3119*4882a593Smuzhiyun 		add_taint_module(mod, TAINT_LIVEPATCH, LOCKDEP_STILL_OK);
3120*4882a593Smuzhiyun 		pr_notice_once("%s: tainting kernel with TAINT_LIVEPATCH\n",
3121*4882a593Smuzhiyun 			       mod->name);
3122*4882a593Smuzhiyun 	}
3123*4882a593Smuzhiyun 
3124*4882a593Smuzhiyun 	return 0;
3125*4882a593Smuzhiyun }
3126*4882a593Smuzhiyun #else /* !CONFIG_LIVEPATCH */
check_modinfo_livepatch(struct module * mod,struct load_info * info)3127*4882a593Smuzhiyun static int check_modinfo_livepatch(struct module *mod, struct load_info *info)
3128*4882a593Smuzhiyun {
3129*4882a593Smuzhiyun 	if (get_modinfo(info, "livepatch")) {
3130*4882a593Smuzhiyun 		pr_err("%s: module is marked as livepatch module, but livepatch support is disabled",
3131*4882a593Smuzhiyun 		       mod->name);
3132*4882a593Smuzhiyun 		return -ENOEXEC;
3133*4882a593Smuzhiyun 	}
3134*4882a593Smuzhiyun 
3135*4882a593Smuzhiyun 	return 0;
3136*4882a593Smuzhiyun }
3137*4882a593Smuzhiyun #endif /* CONFIG_LIVEPATCH */
3138*4882a593Smuzhiyun 
check_modinfo_retpoline(struct module * mod,struct load_info * info)3139*4882a593Smuzhiyun static void check_modinfo_retpoline(struct module *mod, struct load_info *info)
3140*4882a593Smuzhiyun {
3141*4882a593Smuzhiyun 	if (retpoline_module_ok(get_modinfo(info, "retpoline")))
3142*4882a593Smuzhiyun 		return;
3143*4882a593Smuzhiyun 
3144*4882a593Smuzhiyun 	pr_warn("%s: loading module not compiled with retpoline compiler.\n",
3145*4882a593Smuzhiyun 		mod->name);
3146*4882a593Smuzhiyun }
3147*4882a593Smuzhiyun 
3148*4882a593Smuzhiyun /* Sets info->hdr and info->len. */
copy_module_from_user(const void __user * umod,unsigned long len,struct load_info * info)3149*4882a593Smuzhiyun static int copy_module_from_user(const void __user *umod, unsigned long len,
3150*4882a593Smuzhiyun 				  struct load_info *info)
3151*4882a593Smuzhiyun {
3152*4882a593Smuzhiyun 	int err;
3153*4882a593Smuzhiyun 
3154*4882a593Smuzhiyun 	info->len = len;
3155*4882a593Smuzhiyun 	if (info->len < sizeof(*(info->hdr)))
3156*4882a593Smuzhiyun 		return -ENOEXEC;
3157*4882a593Smuzhiyun 
3158*4882a593Smuzhiyun 	err = security_kernel_load_data(LOADING_MODULE, true);
3159*4882a593Smuzhiyun 	if (err)
3160*4882a593Smuzhiyun 		return err;
3161*4882a593Smuzhiyun 
3162*4882a593Smuzhiyun 	/* Suck in entire file: we'll want most of it. */
3163*4882a593Smuzhiyun 	info->hdr = __vmalloc(info->len, GFP_KERNEL | __GFP_NOWARN);
3164*4882a593Smuzhiyun 	if (!info->hdr)
3165*4882a593Smuzhiyun 		return -ENOMEM;
3166*4882a593Smuzhiyun 
3167*4882a593Smuzhiyun 	if (copy_chunked_from_user(info->hdr, umod, info->len) != 0) {
3168*4882a593Smuzhiyun 		err = -EFAULT;
3169*4882a593Smuzhiyun 		goto out;
3170*4882a593Smuzhiyun 	}
3171*4882a593Smuzhiyun 
3172*4882a593Smuzhiyun 	err = security_kernel_post_load_data((char *)info->hdr, info->len,
3173*4882a593Smuzhiyun 					     LOADING_MODULE, "init_module");
3174*4882a593Smuzhiyun out:
3175*4882a593Smuzhiyun 	if (err)
3176*4882a593Smuzhiyun 		vfree(info->hdr);
3177*4882a593Smuzhiyun 
3178*4882a593Smuzhiyun 	return err;
3179*4882a593Smuzhiyun }
3180*4882a593Smuzhiyun 
free_copy(struct load_info * info)3181*4882a593Smuzhiyun static void free_copy(struct load_info *info)
3182*4882a593Smuzhiyun {
3183*4882a593Smuzhiyun 	vfree(info->hdr);
3184*4882a593Smuzhiyun }
3185*4882a593Smuzhiyun 
rewrite_section_headers(struct load_info * info,int flags)3186*4882a593Smuzhiyun static int rewrite_section_headers(struct load_info *info, int flags)
3187*4882a593Smuzhiyun {
3188*4882a593Smuzhiyun 	unsigned int i;
3189*4882a593Smuzhiyun 
3190*4882a593Smuzhiyun 	/* This should always be true, but let's be sure. */
3191*4882a593Smuzhiyun 	info->sechdrs[0].sh_addr = 0;
3192*4882a593Smuzhiyun 
3193*4882a593Smuzhiyun 	for (i = 1; i < info->hdr->e_shnum; i++) {
3194*4882a593Smuzhiyun 		Elf_Shdr *shdr = &info->sechdrs[i];
3195*4882a593Smuzhiyun 
3196*4882a593Smuzhiyun 		/* Mark all sections sh_addr with their address in the
3197*4882a593Smuzhiyun 		   temporary image. */
3198*4882a593Smuzhiyun 		shdr->sh_addr = (size_t)info->hdr + shdr->sh_offset;
3199*4882a593Smuzhiyun 
3200*4882a593Smuzhiyun 	}
3201*4882a593Smuzhiyun 
3202*4882a593Smuzhiyun 	/* Track but don't keep modinfo and version sections. */
3203*4882a593Smuzhiyun 	info->sechdrs[info->index.vers].sh_flags &= ~(unsigned long)SHF_ALLOC;
3204*4882a593Smuzhiyun 	info->sechdrs[info->index.info].sh_flags &= ~(unsigned long)SHF_ALLOC;
3205*4882a593Smuzhiyun 
3206*4882a593Smuzhiyun 	return 0;
3207*4882a593Smuzhiyun }
3208*4882a593Smuzhiyun 
3209*4882a593Smuzhiyun /*
3210*4882a593Smuzhiyun  * Set up our basic convenience variables (pointers to section headers,
3211*4882a593Smuzhiyun  * search for module section index etc), and do some basic section
3212*4882a593Smuzhiyun  * verification.
3213*4882a593Smuzhiyun  *
3214*4882a593Smuzhiyun  * Set info->mod to the temporary copy of the module in info->hdr. The final one
3215*4882a593Smuzhiyun  * will be allocated in move_module().
3216*4882a593Smuzhiyun  */
setup_load_info(struct load_info * info,int flags)3217*4882a593Smuzhiyun static int setup_load_info(struct load_info *info, int flags)
3218*4882a593Smuzhiyun {
3219*4882a593Smuzhiyun 	unsigned int i;
3220*4882a593Smuzhiyun 
3221*4882a593Smuzhiyun 	/* Try to find a name early so we can log errors with a module name */
3222*4882a593Smuzhiyun 	info->index.info = find_sec(info, ".modinfo");
3223*4882a593Smuzhiyun 	if (info->index.info)
3224*4882a593Smuzhiyun 		info->name = get_modinfo(info, "name");
3225*4882a593Smuzhiyun 
3226*4882a593Smuzhiyun 	/* Find internal symbols and strings. */
3227*4882a593Smuzhiyun 	for (i = 1; i < info->hdr->e_shnum; i++) {
3228*4882a593Smuzhiyun 		if (info->sechdrs[i].sh_type == SHT_SYMTAB) {
3229*4882a593Smuzhiyun 			info->index.sym = i;
3230*4882a593Smuzhiyun 			info->index.str = info->sechdrs[i].sh_link;
3231*4882a593Smuzhiyun 			info->strtab = (char *)info->hdr
3232*4882a593Smuzhiyun 				+ info->sechdrs[info->index.str].sh_offset;
3233*4882a593Smuzhiyun 			break;
3234*4882a593Smuzhiyun 		}
3235*4882a593Smuzhiyun 	}
3236*4882a593Smuzhiyun 
3237*4882a593Smuzhiyun 	if (info->index.sym == 0) {
3238*4882a593Smuzhiyun 		pr_warn("%s: module has no symbols (stripped?)\n",
3239*4882a593Smuzhiyun 			info->name ?: "(missing .modinfo section or name field)");
3240*4882a593Smuzhiyun 		return -ENOEXEC;
3241*4882a593Smuzhiyun 	}
3242*4882a593Smuzhiyun 
3243*4882a593Smuzhiyun 	info->index.mod = find_sec(info, ".gnu.linkonce.this_module");
3244*4882a593Smuzhiyun 	if (!info->index.mod) {
3245*4882a593Smuzhiyun 		pr_warn("%s: No module found in object\n",
3246*4882a593Smuzhiyun 			info->name ?: "(missing .modinfo section or name field)");
3247*4882a593Smuzhiyun 		return -ENOEXEC;
3248*4882a593Smuzhiyun 	}
3249*4882a593Smuzhiyun 	/* This is temporary: point mod into copy of data. */
3250*4882a593Smuzhiyun 	info->mod = (void *)info->hdr + info->sechdrs[info->index.mod].sh_offset;
3251*4882a593Smuzhiyun 
3252*4882a593Smuzhiyun 	/*
3253*4882a593Smuzhiyun 	 * If we didn't load the .modinfo 'name' field earlier, fall back to
3254*4882a593Smuzhiyun 	 * on-disk struct mod 'name' field.
3255*4882a593Smuzhiyun 	 */
3256*4882a593Smuzhiyun 	if (!info->name)
3257*4882a593Smuzhiyun 		info->name = info->mod->name;
3258*4882a593Smuzhiyun 
3259*4882a593Smuzhiyun 	if (flags & MODULE_INIT_IGNORE_MODVERSIONS)
3260*4882a593Smuzhiyun 		info->index.vers = 0; /* Pretend no __versions section! */
3261*4882a593Smuzhiyun 	else
3262*4882a593Smuzhiyun 		info->index.vers = find_sec(info, "__versions");
3263*4882a593Smuzhiyun 
3264*4882a593Smuzhiyun 	info->index.pcpu = find_pcpusec(info);
3265*4882a593Smuzhiyun 
3266*4882a593Smuzhiyun 	return 0;
3267*4882a593Smuzhiyun }
3268*4882a593Smuzhiyun 
check_modinfo(struct module * mod,struct load_info * info,int flags)3269*4882a593Smuzhiyun static int check_modinfo(struct module *mod, struct load_info *info, int flags)
3270*4882a593Smuzhiyun {
3271*4882a593Smuzhiyun 	const char *modmagic = get_modinfo(info, "vermagic");
3272*4882a593Smuzhiyun 	int err;
3273*4882a593Smuzhiyun 
3274*4882a593Smuzhiyun 	if (flags & MODULE_INIT_IGNORE_VERMAGIC)
3275*4882a593Smuzhiyun 		modmagic = NULL;
3276*4882a593Smuzhiyun 
3277*4882a593Smuzhiyun 	/* This is allowed: modprobe --force will invalidate it. */
3278*4882a593Smuzhiyun 	if (!modmagic) {
3279*4882a593Smuzhiyun 		err = try_to_force_load(mod, "bad vermagic");
3280*4882a593Smuzhiyun 		if (err)
3281*4882a593Smuzhiyun 			return err;
3282*4882a593Smuzhiyun 	} else if (!same_magic(modmagic, vermagic, info->index.vers)) {
3283*4882a593Smuzhiyun 		pr_err("%s: version magic '%s' should be '%s'\n",
3284*4882a593Smuzhiyun 		       info->name, modmagic, vermagic);
3285*4882a593Smuzhiyun 		return -ENOEXEC;
3286*4882a593Smuzhiyun 	}
3287*4882a593Smuzhiyun 
3288*4882a593Smuzhiyun 	if (!get_modinfo(info, "intree")) {
3289*4882a593Smuzhiyun 		if (!test_taint(TAINT_OOT_MODULE))
3290*4882a593Smuzhiyun 			pr_warn("%s: loading out-of-tree module taints kernel.\n",
3291*4882a593Smuzhiyun 				mod->name);
3292*4882a593Smuzhiyun 		add_taint_module(mod, TAINT_OOT_MODULE, LOCKDEP_STILL_OK);
3293*4882a593Smuzhiyun 	}
3294*4882a593Smuzhiyun 
3295*4882a593Smuzhiyun 	check_modinfo_retpoline(mod, info);
3296*4882a593Smuzhiyun 
3297*4882a593Smuzhiyun 	if (get_modinfo(info, "staging")) {
3298*4882a593Smuzhiyun 		add_taint_module(mod, TAINT_CRAP, LOCKDEP_STILL_OK);
3299*4882a593Smuzhiyun 		pr_warn("%s: module is from the staging directory, the quality "
3300*4882a593Smuzhiyun 			"is unknown, you have been warned.\n", mod->name);
3301*4882a593Smuzhiyun 	}
3302*4882a593Smuzhiyun 
3303*4882a593Smuzhiyun 	err = check_modinfo_livepatch(mod, info);
3304*4882a593Smuzhiyun 	if (err)
3305*4882a593Smuzhiyun 		return err;
3306*4882a593Smuzhiyun 
3307*4882a593Smuzhiyun 	/* Set up license info based on the info section */
3308*4882a593Smuzhiyun 	set_license(mod, get_modinfo(info, "license"));
3309*4882a593Smuzhiyun 
3310*4882a593Smuzhiyun 	return 0;
3311*4882a593Smuzhiyun }
3312*4882a593Smuzhiyun 
find_module_sections(struct module * mod,struct load_info * info)3313*4882a593Smuzhiyun static int find_module_sections(struct module *mod, struct load_info *info)
3314*4882a593Smuzhiyun {
3315*4882a593Smuzhiyun 	mod->kp = section_objs(info, "__param",
3316*4882a593Smuzhiyun 			       sizeof(*mod->kp), &mod->num_kp);
3317*4882a593Smuzhiyun 	mod->syms = section_objs(info, "__ksymtab",
3318*4882a593Smuzhiyun 				 sizeof(*mod->syms), &mod->num_syms);
3319*4882a593Smuzhiyun 	mod->crcs = section_addr(info, "__kcrctab");
3320*4882a593Smuzhiyun 	mod->gpl_syms = section_objs(info, "__ksymtab_gpl",
3321*4882a593Smuzhiyun 				     sizeof(*mod->gpl_syms),
3322*4882a593Smuzhiyun 				     &mod->num_gpl_syms);
3323*4882a593Smuzhiyun 	mod->gpl_crcs = section_addr(info, "__kcrctab_gpl");
3324*4882a593Smuzhiyun 	mod->gpl_future_syms = section_objs(info,
3325*4882a593Smuzhiyun 					    "__ksymtab_gpl_future",
3326*4882a593Smuzhiyun 					    sizeof(*mod->gpl_future_syms),
3327*4882a593Smuzhiyun 					    &mod->num_gpl_future_syms);
3328*4882a593Smuzhiyun 	mod->gpl_future_crcs = section_addr(info, "__kcrctab_gpl_future");
3329*4882a593Smuzhiyun 
3330*4882a593Smuzhiyun #ifdef CONFIG_UNUSED_SYMBOLS
3331*4882a593Smuzhiyun 	mod->unused_syms = section_objs(info, "__ksymtab_unused",
3332*4882a593Smuzhiyun 					sizeof(*mod->unused_syms),
3333*4882a593Smuzhiyun 					&mod->num_unused_syms);
3334*4882a593Smuzhiyun 	mod->unused_crcs = section_addr(info, "__kcrctab_unused");
3335*4882a593Smuzhiyun 	mod->unused_gpl_syms = section_objs(info, "__ksymtab_unused_gpl",
3336*4882a593Smuzhiyun 					    sizeof(*mod->unused_gpl_syms),
3337*4882a593Smuzhiyun 					    &mod->num_unused_gpl_syms);
3338*4882a593Smuzhiyun 	mod->unused_gpl_crcs = section_addr(info, "__kcrctab_unused_gpl");
3339*4882a593Smuzhiyun #endif
3340*4882a593Smuzhiyun #ifdef CONFIG_CONSTRUCTORS
3341*4882a593Smuzhiyun 	mod->ctors = section_objs(info, ".ctors",
3342*4882a593Smuzhiyun 				  sizeof(*mod->ctors), &mod->num_ctors);
3343*4882a593Smuzhiyun 	if (!mod->ctors)
3344*4882a593Smuzhiyun 		mod->ctors = section_objs(info, ".init_array",
3345*4882a593Smuzhiyun 				sizeof(*mod->ctors), &mod->num_ctors);
3346*4882a593Smuzhiyun 	else if (find_sec(info, ".init_array")) {
3347*4882a593Smuzhiyun 		/*
3348*4882a593Smuzhiyun 		 * This shouldn't happen with same compiler and binutils
3349*4882a593Smuzhiyun 		 * building all parts of the module.
3350*4882a593Smuzhiyun 		 */
3351*4882a593Smuzhiyun 		pr_warn("%s: has both .ctors and .init_array.\n",
3352*4882a593Smuzhiyun 		       mod->name);
3353*4882a593Smuzhiyun 		return -EINVAL;
3354*4882a593Smuzhiyun 	}
3355*4882a593Smuzhiyun #endif
3356*4882a593Smuzhiyun 
3357*4882a593Smuzhiyun 	mod->noinstr_text_start = section_objs(info, ".noinstr.text", 1,
3358*4882a593Smuzhiyun 						&mod->noinstr_text_size);
3359*4882a593Smuzhiyun 
3360*4882a593Smuzhiyun #ifdef CONFIG_TRACEPOINTS
3361*4882a593Smuzhiyun 	mod->tracepoints_ptrs = section_objs(info, "__tracepoints_ptrs",
3362*4882a593Smuzhiyun 					     sizeof(*mod->tracepoints_ptrs),
3363*4882a593Smuzhiyun 					     &mod->num_tracepoints);
3364*4882a593Smuzhiyun #endif
3365*4882a593Smuzhiyun #ifdef CONFIG_TREE_SRCU
3366*4882a593Smuzhiyun 	mod->srcu_struct_ptrs = section_objs(info, "___srcu_struct_ptrs",
3367*4882a593Smuzhiyun 					     sizeof(*mod->srcu_struct_ptrs),
3368*4882a593Smuzhiyun 					     &mod->num_srcu_structs);
3369*4882a593Smuzhiyun #endif
3370*4882a593Smuzhiyun #ifdef CONFIG_BPF_EVENTS
3371*4882a593Smuzhiyun 	mod->bpf_raw_events = section_objs(info, "__bpf_raw_tp_map",
3372*4882a593Smuzhiyun 					   sizeof(*mod->bpf_raw_events),
3373*4882a593Smuzhiyun 					   &mod->num_bpf_raw_events);
3374*4882a593Smuzhiyun #endif
3375*4882a593Smuzhiyun #ifdef CONFIG_JUMP_LABEL
3376*4882a593Smuzhiyun 	mod->jump_entries = section_objs(info, "__jump_table",
3377*4882a593Smuzhiyun 					sizeof(*mod->jump_entries),
3378*4882a593Smuzhiyun 					&mod->num_jump_entries);
3379*4882a593Smuzhiyun #endif
3380*4882a593Smuzhiyun #ifdef CONFIG_EVENT_TRACING
3381*4882a593Smuzhiyun 	mod->trace_events = section_objs(info, "_ftrace_events",
3382*4882a593Smuzhiyun 					 sizeof(*mod->trace_events),
3383*4882a593Smuzhiyun 					 &mod->num_trace_events);
3384*4882a593Smuzhiyun 	mod->trace_evals = section_objs(info, "_ftrace_eval_map",
3385*4882a593Smuzhiyun 					sizeof(*mod->trace_evals),
3386*4882a593Smuzhiyun 					&mod->num_trace_evals);
3387*4882a593Smuzhiyun #endif
3388*4882a593Smuzhiyun #ifdef CONFIG_TRACING
3389*4882a593Smuzhiyun 	mod->trace_bprintk_fmt_start = section_objs(info, "__trace_printk_fmt",
3390*4882a593Smuzhiyun 					 sizeof(*mod->trace_bprintk_fmt_start),
3391*4882a593Smuzhiyun 					 &mod->num_trace_bprintk_fmt);
3392*4882a593Smuzhiyun #endif
3393*4882a593Smuzhiyun #ifdef CONFIG_FTRACE_MCOUNT_RECORD
3394*4882a593Smuzhiyun 	/* sechdrs[0].sh_size is always zero */
3395*4882a593Smuzhiyun 	mod->ftrace_callsites = section_objs(info, FTRACE_CALLSITE_SECTION,
3396*4882a593Smuzhiyun 					     sizeof(*mod->ftrace_callsites),
3397*4882a593Smuzhiyun 					     &mod->num_ftrace_callsites);
3398*4882a593Smuzhiyun #endif
3399*4882a593Smuzhiyun #ifdef CONFIG_FUNCTION_ERROR_INJECTION
3400*4882a593Smuzhiyun 	mod->ei_funcs = section_objs(info, "_error_injection_whitelist",
3401*4882a593Smuzhiyun 					    sizeof(*mod->ei_funcs),
3402*4882a593Smuzhiyun 					    &mod->num_ei_funcs);
3403*4882a593Smuzhiyun #endif
3404*4882a593Smuzhiyun #ifdef CONFIG_KPROBES
3405*4882a593Smuzhiyun 	mod->kprobes_text_start = section_objs(info, ".kprobes.text", 1,
3406*4882a593Smuzhiyun 						&mod->kprobes_text_size);
3407*4882a593Smuzhiyun 	mod->kprobe_blacklist = section_objs(info, "_kprobe_blacklist",
3408*4882a593Smuzhiyun 						sizeof(unsigned long),
3409*4882a593Smuzhiyun 						&mod->num_kprobe_blacklist);
3410*4882a593Smuzhiyun #endif
3411*4882a593Smuzhiyun #ifdef CONFIG_HAVE_STATIC_CALL_INLINE
3412*4882a593Smuzhiyun 	mod->static_call_sites = section_objs(info, ".static_call_sites",
3413*4882a593Smuzhiyun 					      sizeof(*mod->static_call_sites),
3414*4882a593Smuzhiyun 					      &mod->num_static_call_sites);
3415*4882a593Smuzhiyun #endif
3416*4882a593Smuzhiyun 	mod->extable = section_objs(info, "__ex_table",
3417*4882a593Smuzhiyun 				    sizeof(*mod->extable), &mod->num_exentries);
3418*4882a593Smuzhiyun 
3419*4882a593Smuzhiyun 	if (section_addr(info, "__obsparm"))
3420*4882a593Smuzhiyun 		pr_warn("%s: Ignoring obsolete parameters\n", mod->name);
3421*4882a593Smuzhiyun 
3422*4882a593Smuzhiyun 	info->debug = section_objs(info, "__dyndbg",
3423*4882a593Smuzhiyun 				   sizeof(*info->debug), &info->num_debug);
3424*4882a593Smuzhiyun 
3425*4882a593Smuzhiyun 	return 0;
3426*4882a593Smuzhiyun }
3427*4882a593Smuzhiyun 
move_module(struct module * mod,struct load_info * info)3428*4882a593Smuzhiyun static int move_module(struct module *mod, struct load_info *info)
3429*4882a593Smuzhiyun {
3430*4882a593Smuzhiyun 	int i;
3431*4882a593Smuzhiyun 	void *ptr;
3432*4882a593Smuzhiyun 
3433*4882a593Smuzhiyun 	/* Do the allocs. */
3434*4882a593Smuzhiyun 	ptr = module_alloc(mod->core_layout.size);
3435*4882a593Smuzhiyun 	/*
3436*4882a593Smuzhiyun 	 * The pointer to this block is stored in the module structure
3437*4882a593Smuzhiyun 	 * which is inside the block. Just mark it as not being a
3438*4882a593Smuzhiyun 	 * leak.
3439*4882a593Smuzhiyun 	 */
3440*4882a593Smuzhiyun 	kmemleak_not_leak(ptr);
3441*4882a593Smuzhiyun 	if (!ptr)
3442*4882a593Smuzhiyun 		return -ENOMEM;
3443*4882a593Smuzhiyun 
3444*4882a593Smuzhiyun 	memset(ptr, 0, mod->core_layout.size);
3445*4882a593Smuzhiyun 	mod->core_layout.base = ptr;
3446*4882a593Smuzhiyun 
3447*4882a593Smuzhiyun 	if (mod->init_layout.size) {
3448*4882a593Smuzhiyun 		ptr = module_alloc(mod->init_layout.size);
3449*4882a593Smuzhiyun 		/*
3450*4882a593Smuzhiyun 		 * The pointer to this block is stored in the module structure
3451*4882a593Smuzhiyun 		 * which is inside the block. This block doesn't need to be
3452*4882a593Smuzhiyun 		 * scanned as it contains data and code that will be freed
3453*4882a593Smuzhiyun 		 * after the module is initialized.
3454*4882a593Smuzhiyun 		 */
3455*4882a593Smuzhiyun 		kmemleak_ignore(ptr);
3456*4882a593Smuzhiyun 		if (!ptr) {
3457*4882a593Smuzhiyun 			module_memfree(mod->core_layout.base);
3458*4882a593Smuzhiyun 			return -ENOMEM;
3459*4882a593Smuzhiyun 		}
3460*4882a593Smuzhiyun 		memset(ptr, 0, mod->init_layout.size);
3461*4882a593Smuzhiyun 		mod->init_layout.base = ptr;
3462*4882a593Smuzhiyun 	} else
3463*4882a593Smuzhiyun 		mod->init_layout.base = NULL;
3464*4882a593Smuzhiyun 
3465*4882a593Smuzhiyun 	/* Transfer each section which specifies SHF_ALLOC */
3466*4882a593Smuzhiyun 	pr_debug("final section addresses:\n");
3467*4882a593Smuzhiyun 	for (i = 0; i < info->hdr->e_shnum; i++) {
3468*4882a593Smuzhiyun 		void *dest;
3469*4882a593Smuzhiyun 		Elf_Shdr *shdr = &info->sechdrs[i];
3470*4882a593Smuzhiyun 
3471*4882a593Smuzhiyun 		if (!(shdr->sh_flags & SHF_ALLOC))
3472*4882a593Smuzhiyun 			continue;
3473*4882a593Smuzhiyun 
3474*4882a593Smuzhiyun 		if (shdr->sh_entsize & INIT_OFFSET_MASK)
3475*4882a593Smuzhiyun 			dest = mod->init_layout.base
3476*4882a593Smuzhiyun 				+ (shdr->sh_entsize & ~INIT_OFFSET_MASK);
3477*4882a593Smuzhiyun 		else
3478*4882a593Smuzhiyun 			dest = mod->core_layout.base + shdr->sh_entsize;
3479*4882a593Smuzhiyun 
3480*4882a593Smuzhiyun 		if (shdr->sh_type != SHT_NOBITS)
3481*4882a593Smuzhiyun 			memcpy(dest, (void *)shdr->sh_addr, shdr->sh_size);
3482*4882a593Smuzhiyun 		/* Update sh_addr to point to copy in image. */
3483*4882a593Smuzhiyun 		shdr->sh_addr = (unsigned long)dest;
3484*4882a593Smuzhiyun 		pr_debug("\t0x%lx %s\n",
3485*4882a593Smuzhiyun 			 (long)shdr->sh_addr, info->secstrings + shdr->sh_name);
3486*4882a593Smuzhiyun 	}
3487*4882a593Smuzhiyun 
3488*4882a593Smuzhiyun 	return 0;
3489*4882a593Smuzhiyun }
3490*4882a593Smuzhiyun 
check_module_license_and_versions(struct module * mod)3491*4882a593Smuzhiyun static int check_module_license_and_versions(struct module *mod)
3492*4882a593Smuzhiyun {
3493*4882a593Smuzhiyun 	int prev_taint = test_taint(TAINT_PROPRIETARY_MODULE);
3494*4882a593Smuzhiyun 
3495*4882a593Smuzhiyun 	/*
3496*4882a593Smuzhiyun 	 * ndiswrapper is under GPL by itself, but loads proprietary modules.
3497*4882a593Smuzhiyun 	 * Don't use add_taint_module(), as it would prevent ndiswrapper from
3498*4882a593Smuzhiyun 	 * using GPL-only symbols it needs.
3499*4882a593Smuzhiyun 	 */
3500*4882a593Smuzhiyun 	if (strcmp(mod->name, "ndiswrapper") == 0)
3501*4882a593Smuzhiyun 		add_taint(TAINT_PROPRIETARY_MODULE, LOCKDEP_NOW_UNRELIABLE);
3502*4882a593Smuzhiyun 
3503*4882a593Smuzhiyun 	/* driverloader was caught wrongly pretending to be under GPL */
3504*4882a593Smuzhiyun 	if (strcmp(mod->name, "driverloader") == 0)
3505*4882a593Smuzhiyun 		add_taint_module(mod, TAINT_PROPRIETARY_MODULE,
3506*4882a593Smuzhiyun 				 LOCKDEP_NOW_UNRELIABLE);
3507*4882a593Smuzhiyun 
3508*4882a593Smuzhiyun 	/* lve claims to be GPL but upstream won't provide source */
3509*4882a593Smuzhiyun 	if (strcmp(mod->name, "lve") == 0)
3510*4882a593Smuzhiyun 		add_taint_module(mod, TAINT_PROPRIETARY_MODULE,
3511*4882a593Smuzhiyun 				 LOCKDEP_NOW_UNRELIABLE);
3512*4882a593Smuzhiyun 
3513*4882a593Smuzhiyun 	if (!prev_taint && test_taint(TAINT_PROPRIETARY_MODULE))
3514*4882a593Smuzhiyun 		pr_warn("%s: module license taints kernel.\n", mod->name);
3515*4882a593Smuzhiyun 
3516*4882a593Smuzhiyun #ifdef CONFIG_MODVERSIONS
3517*4882a593Smuzhiyun 	if ((mod->num_syms && !mod->crcs)
3518*4882a593Smuzhiyun 	    || (mod->num_gpl_syms && !mod->gpl_crcs)
3519*4882a593Smuzhiyun 	    || (mod->num_gpl_future_syms && !mod->gpl_future_crcs)
3520*4882a593Smuzhiyun #ifdef CONFIG_UNUSED_SYMBOLS
3521*4882a593Smuzhiyun 	    || (mod->num_unused_syms && !mod->unused_crcs)
3522*4882a593Smuzhiyun 	    || (mod->num_unused_gpl_syms && !mod->unused_gpl_crcs)
3523*4882a593Smuzhiyun #endif
3524*4882a593Smuzhiyun 		) {
3525*4882a593Smuzhiyun 		return try_to_force_load(mod,
3526*4882a593Smuzhiyun 					 "no versions for exported symbols");
3527*4882a593Smuzhiyun 	}
3528*4882a593Smuzhiyun #endif
3529*4882a593Smuzhiyun 	return 0;
3530*4882a593Smuzhiyun }
3531*4882a593Smuzhiyun 
flush_module_icache(const struct module * mod)3532*4882a593Smuzhiyun static void flush_module_icache(const struct module *mod)
3533*4882a593Smuzhiyun {
3534*4882a593Smuzhiyun 	/*
3535*4882a593Smuzhiyun 	 * Flush the instruction cache, since we've played with text.
3536*4882a593Smuzhiyun 	 * Do it before processing of module parameters, so the module
3537*4882a593Smuzhiyun 	 * can provide parameter accessor functions of its own.
3538*4882a593Smuzhiyun 	 */
3539*4882a593Smuzhiyun 	if (mod->init_layout.base)
3540*4882a593Smuzhiyun 		flush_icache_range((unsigned long)mod->init_layout.base,
3541*4882a593Smuzhiyun 				   (unsigned long)mod->init_layout.base
3542*4882a593Smuzhiyun 				   + mod->init_layout.size);
3543*4882a593Smuzhiyun 	flush_icache_range((unsigned long)mod->core_layout.base,
3544*4882a593Smuzhiyun 			   (unsigned long)mod->core_layout.base + mod->core_layout.size);
3545*4882a593Smuzhiyun }
3546*4882a593Smuzhiyun 
module_frob_arch_sections(Elf_Ehdr * hdr,Elf_Shdr * sechdrs,char * secstrings,struct module * mod)3547*4882a593Smuzhiyun int __weak module_frob_arch_sections(Elf_Ehdr *hdr,
3548*4882a593Smuzhiyun 				     Elf_Shdr *sechdrs,
3549*4882a593Smuzhiyun 				     char *secstrings,
3550*4882a593Smuzhiyun 				     struct module *mod)
3551*4882a593Smuzhiyun {
3552*4882a593Smuzhiyun 	return 0;
3553*4882a593Smuzhiyun }
3554*4882a593Smuzhiyun 
3555*4882a593Smuzhiyun /* module_blacklist is a comma-separated list of module names */
3556*4882a593Smuzhiyun static char *module_blacklist;
blacklisted(const char * module_name)3557*4882a593Smuzhiyun static bool blacklisted(const char *module_name)
3558*4882a593Smuzhiyun {
3559*4882a593Smuzhiyun 	const char *p;
3560*4882a593Smuzhiyun 	size_t len;
3561*4882a593Smuzhiyun 
3562*4882a593Smuzhiyun 	if (!module_blacklist)
3563*4882a593Smuzhiyun 		return false;
3564*4882a593Smuzhiyun 
3565*4882a593Smuzhiyun 	for (p = module_blacklist; *p; p += len) {
3566*4882a593Smuzhiyun 		len = strcspn(p, ",");
3567*4882a593Smuzhiyun 		if (strlen(module_name) == len && !memcmp(module_name, p, len))
3568*4882a593Smuzhiyun 			return true;
3569*4882a593Smuzhiyun 		if (p[len] == ',')
3570*4882a593Smuzhiyun 			len++;
3571*4882a593Smuzhiyun 	}
3572*4882a593Smuzhiyun 	return false;
3573*4882a593Smuzhiyun }
3574*4882a593Smuzhiyun core_param(module_blacklist, module_blacklist, charp, 0400);
3575*4882a593Smuzhiyun 
layout_and_allocate(struct load_info * info,int flags)3576*4882a593Smuzhiyun static struct module *layout_and_allocate(struct load_info *info, int flags)
3577*4882a593Smuzhiyun {
3578*4882a593Smuzhiyun 	struct module *mod;
3579*4882a593Smuzhiyun 	unsigned int ndx;
3580*4882a593Smuzhiyun 	int err;
3581*4882a593Smuzhiyun 
3582*4882a593Smuzhiyun 	err = check_modinfo(info->mod, info, flags);
3583*4882a593Smuzhiyun 	if (err)
3584*4882a593Smuzhiyun 		return ERR_PTR(err);
3585*4882a593Smuzhiyun 
3586*4882a593Smuzhiyun 	/* Allow arches to frob section contents and sizes.  */
3587*4882a593Smuzhiyun 	err = module_frob_arch_sections(info->hdr, info->sechdrs,
3588*4882a593Smuzhiyun 					info->secstrings, info->mod);
3589*4882a593Smuzhiyun 	if (err < 0)
3590*4882a593Smuzhiyun 		return ERR_PTR(err);
3591*4882a593Smuzhiyun 
3592*4882a593Smuzhiyun 	err = module_enforce_rwx_sections(info->hdr, info->sechdrs,
3593*4882a593Smuzhiyun 					  info->secstrings, info->mod);
3594*4882a593Smuzhiyun 	if (err < 0)
3595*4882a593Smuzhiyun 		return ERR_PTR(err);
3596*4882a593Smuzhiyun 
3597*4882a593Smuzhiyun 	/* We will do a special allocation for per-cpu sections later. */
3598*4882a593Smuzhiyun 	info->sechdrs[info->index.pcpu].sh_flags &= ~(unsigned long)SHF_ALLOC;
3599*4882a593Smuzhiyun 
3600*4882a593Smuzhiyun 	/*
3601*4882a593Smuzhiyun 	 * Mark ro_after_init section with SHF_RO_AFTER_INIT so that
3602*4882a593Smuzhiyun 	 * layout_sections() can put it in the right place.
3603*4882a593Smuzhiyun 	 * Note: ro_after_init sections also have SHF_{WRITE,ALLOC} set.
3604*4882a593Smuzhiyun 	 */
3605*4882a593Smuzhiyun 	ndx = find_sec(info, ".data..ro_after_init");
3606*4882a593Smuzhiyun 	if (ndx)
3607*4882a593Smuzhiyun 		info->sechdrs[ndx].sh_flags |= SHF_RO_AFTER_INIT;
3608*4882a593Smuzhiyun 	/*
3609*4882a593Smuzhiyun 	 * Mark the __jump_table section as ro_after_init as well: these data
3610*4882a593Smuzhiyun 	 * structures are never modified, with the exception of entries that
3611*4882a593Smuzhiyun 	 * refer to code in the __init section, which are annotated as such
3612*4882a593Smuzhiyun 	 * at module load time.
3613*4882a593Smuzhiyun 	 */
3614*4882a593Smuzhiyun 	ndx = find_sec(info, "__jump_table");
3615*4882a593Smuzhiyun 	if (ndx)
3616*4882a593Smuzhiyun 		info->sechdrs[ndx].sh_flags |= SHF_RO_AFTER_INIT;
3617*4882a593Smuzhiyun 
3618*4882a593Smuzhiyun 	/* Determine total sizes, and put offsets in sh_entsize.  For now
3619*4882a593Smuzhiyun 	   this is done generically; there doesn't appear to be any
3620*4882a593Smuzhiyun 	   special cases for the architectures. */
3621*4882a593Smuzhiyun 	layout_sections(info->mod, info);
3622*4882a593Smuzhiyun 	layout_symtab(info->mod, info);
3623*4882a593Smuzhiyun 
3624*4882a593Smuzhiyun 	/* Allocate and move to the final place */
3625*4882a593Smuzhiyun 	err = move_module(info->mod, info);
3626*4882a593Smuzhiyun 	if (err)
3627*4882a593Smuzhiyun 		return ERR_PTR(err);
3628*4882a593Smuzhiyun 
3629*4882a593Smuzhiyun 	/* Module has been copied to its final place now: return it. */
3630*4882a593Smuzhiyun 	mod = (void *)info->sechdrs[info->index.mod].sh_addr;
3631*4882a593Smuzhiyun 	kmemleak_load_module(mod, info);
3632*4882a593Smuzhiyun 	return mod;
3633*4882a593Smuzhiyun }
3634*4882a593Smuzhiyun 
3635*4882a593Smuzhiyun /* mod is no longer valid after this! */
module_deallocate(struct module * mod,struct load_info * info)3636*4882a593Smuzhiyun static void module_deallocate(struct module *mod, struct load_info *info)
3637*4882a593Smuzhiyun {
3638*4882a593Smuzhiyun 	percpu_modfree(mod);
3639*4882a593Smuzhiyun 	module_arch_freeing_init(mod);
3640*4882a593Smuzhiyun 	trace_android_vh_set_memory_rw((unsigned long)mod->init_layout.base,
3641*4882a593Smuzhiyun 		(mod->init_layout.size)>>PAGE_SHIFT);
3642*4882a593Smuzhiyun 	trace_android_vh_set_memory_nx((unsigned long)mod->init_layout.base,
3643*4882a593Smuzhiyun 		(mod->init_layout.size)>>PAGE_SHIFT);
3644*4882a593Smuzhiyun 	module_memfree(mod->init_layout.base);
3645*4882a593Smuzhiyun 	trace_android_vh_set_memory_rw((unsigned long)mod->core_layout.base,
3646*4882a593Smuzhiyun 		(mod->core_layout.size)>>PAGE_SHIFT);
3647*4882a593Smuzhiyun 	trace_android_vh_set_memory_nx((unsigned long)mod->core_layout.base,
3648*4882a593Smuzhiyun 		(mod->core_layout.size)>>PAGE_SHIFT);
3649*4882a593Smuzhiyun 	module_memfree(mod->core_layout.base);
3650*4882a593Smuzhiyun }
3651*4882a593Smuzhiyun 
module_finalize(const Elf_Ehdr * hdr,const Elf_Shdr * sechdrs,struct module * me)3652*4882a593Smuzhiyun int __weak module_finalize(const Elf_Ehdr *hdr,
3653*4882a593Smuzhiyun 			   const Elf_Shdr *sechdrs,
3654*4882a593Smuzhiyun 			   struct module *me)
3655*4882a593Smuzhiyun {
3656*4882a593Smuzhiyun 	return 0;
3657*4882a593Smuzhiyun }
3658*4882a593Smuzhiyun 
post_relocation(struct module * mod,const struct load_info * info)3659*4882a593Smuzhiyun static int post_relocation(struct module *mod, const struct load_info *info)
3660*4882a593Smuzhiyun {
3661*4882a593Smuzhiyun 	/* Sort exception table now relocations are done. */
3662*4882a593Smuzhiyun 	sort_extable(mod->extable, mod->extable + mod->num_exentries);
3663*4882a593Smuzhiyun 
3664*4882a593Smuzhiyun 	/* Copy relocated percpu area over. */
3665*4882a593Smuzhiyun 	percpu_modcopy(mod, (void *)info->sechdrs[info->index.pcpu].sh_addr,
3666*4882a593Smuzhiyun 		       info->sechdrs[info->index.pcpu].sh_size);
3667*4882a593Smuzhiyun 
3668*4882a593Smuzhiyun 	/* Setup kallsyms-specific fields. */
3669*4882a593Smuzhiyun 	add_kallsyms(mod, info);
3670*4882a593Smuzhiyun 
3671*4882a593Smuzhiyun 	/* Arch-specific module finalizing. */
3672*4882a593Smuzhiyun 	return module_finalize(info->hdr, info->sechdrs, mod);
3673*4882a593Smuzhiyun }
3674*4882a593Smuzhiyun 
3675*4882a593Smuzhiyun /* Is this module of this name done loading?  No locks held. */
finished_loading(const char * name)3676*4882a593Smuzhiyun static bool finished_loading(const char *name)
3677*4882a593Smuzhiyun {
3678*4882a593Smuzhiyun 	struct module *mod;
3679*4882a593Smuzhiyun 	bool ret;
3680*4882a593Smuzhiyun 
3681*4882a593Smuzhiyun 	/*
3682*4882a593Smuzhiyun 	 * The module_mutex should not be a heavily contended lock;
3683*4882a593Smuzhiyun 	 * if we get the occasional sleep here, we'll go an extra iteration
3684*4882a593Smuzhiyun 	 * in the wait_event_interruptible(), which is harmless.
3685*4882a593Smuzhiyun 	 */
3686*4882a593Smuzhiyun 	sched_annotate_sleep();
3687*4882a593Smuzhiyun 	mutex_lock(&module_mutex);
3688*4882a593Smuzhiyun 	mod = find_module_all(name, strlen(name), true);
3689*4882a593Smuzhiyun 	ret = !mod || mod->state == MODULE_STATE_LIVE;
3690*4882a593Smuzhiyun 	mutex_unlock(&module_mutex);
3691*4882a593Smuzhiyun 
3692*4882a593Smuzhiyun 	return ret;
3693*4882a593Smuzhiyun }
3694*4882a593Smuzhiyun 
3695*4882a593Smuzhiyun /* Call module constructors. */
do_mod_ctors(struct module * mod)3696*4882a593Smuzhiyun static void do_mod_ctors(struct module *mod)
3697*4882a593Smuzhiyun {
3698*4882a593Smuzhiyun #ifdef CONFIG_CONSTRUCTORS
3699*4882a593Smuzhiyun 	unsigned long i;
3700*4882a593Smuzhiyun 
3701*4882a593Smuzhiyun 	for (i = 0; i < mod->num_ctors; i++)
3702*4882a593Smuzhiyun 		mod->ctors[i]();
3703*4882a593Smuzhiyun #endif
3704*4882a593Smuzhiyun }
3705*4882a593Smuzhiyun 
3706*4882a593Smuzhiyun /* For freeing module_init on success, in case kallsyms traversing */
3707*4882a593Smuzhiyun struct mod_initfree {
3708*4882a593Smuzhiyun 	struct llist_node node;
3709*4882a593Smuzhiyun 	void *module_init;
3710*4882a593Smuzhiyun };
3711*4882a593Smuzhiyun 
do_free_init(struct work_struct * w)3712*4882a593Smuzhiyun static void do_free_init(struct work_struct *w)
3713*4882a593Smuzhiyun {
3714*4882a593Smuzhiyun 	struct llist_node *pos, *n, *list;
3715*4882a593Smuzhiyun 	struct mod_initfree *initfree;
3716*4882a593Smuzhiyun 
3717*4882a593Smuzhiyun 	list = llist_del_all(&init_free_list);
3718*4882a593Smuzhiyun 
3719*4882a593Smuzhiyun 	synchronize_rcu();
3720*4882a593Smuzhiyun 
3721*4882a593Smuzhiyun 	llist_for_each_safe(pos, n, list) {
3722*4882a593Smuzhiyun 		initfree = container_of(pos, struct mod_initfree, node);
3723*4882a593Smuzhiyun 		module_memfree(initfree->module_init);
3724*4882a593Smuzhiyun 		kfree(initfree);
3725*4882a593Smuzhiyun 	}
3726*4882a593Smuzhiyun }
3727*4882a593Smuzhiyun 
3728*4882a593Smuzhiyun /*
3729*4882a593Smuzhiyun  * This is where the real work happens.
3730*4882a593Smuzhiyun  *
3731*4882a593Smuzhiyun  * Keep it uninlined to provide a reliable breakpoint target, e.g. for the gdb
3732*4882a593Smuzhiyun  * helper command 'lx-symbols'.
3733*4882a593Smuzhiyun  */
do_init_module(struct module * mod)3734*4882a593Smuzhiyun static noinline int do_init_module(struct module *mod)
3735*4882a593Smuzhiyun {
3736*4882a593Smuzhiyun 	int ret = 0;
3737*4882a593Smuzhiyun 	struct mod_initfree *freeinit;
3738*4882a593Smuzhiyun 
3739*4882a593Smuzhiyun 	freeinit = kmalloc(sizeof(*freeinit), GFP_KERNEL);
3740*4882a593Smuzhiyun 	if (!freeinit) {
3741*4882a593Smuzhiyun 		ret = -ENOMEM;
3742*4882a593Smuzhiyun 		goto fail;
3743*4882a593Smuzhiyun 	}
3744*4882a593Smuzhiyun 	freeinit->module_init = mod->init_layout.base;
3745*4882a593Smuzhiyun 
3746*4882a593Smuzhiyun 	do_mod_ctors(mod);
3747*4882a593Smuzhiyun 	/* Start the module */
3748*4882a593Smuzhiyun 	if (mod->init != NULL)
3749*4882a593Smuzhiyun 		ret = do_one_initcall(mod->init);
3750*4882a593Smuzhiyun 	if (ret < 0) {
3751*4882a593Smuzhiyun 		goto fail_free_freeinit;
3752*4882a593Smuzhiyun 	}
3753*4882a593Smuzhiyun 	if (ret > 0) {
3754*4882a593Smuzhiyun 		pr_warn("%s: '%s'->init suspiciously returned %d, it should "
3755*4882a593Smuzhiyun 			"follow 0/-E convention\n"
3756*4882a593Smuzhiyun 			"%s: loading module anyway...\n",
3757*4882a593Smuzhiyun 			__func__, mod->name, ret, __func__);
3758*4882a593Smuzhiyun 		dump_stack();
3759*4882a593Smuzhiyun 	}
3760*4882a593Smuzhiyun 
3761*4882a593Smuzhiyun 	/* Now it's a first class citizen! */
3762*4882a593Smuzhiyun 	mod->state = MODULE_STATE_LIVE;
3763*4882a593Smuzhiyun 	blocking_notifier_call_chain(&module_notify_list,
3764*4882a593Smuzhiyun 				     MODULE_STATE_LIVE, mod);
3765*4882a593Smuzhiyun 
3766*4882a593Smuzhiyun 	/* Delay uevent until module has finished its init routine */
3767*4882a593Smuzhiyun 	kobject_uevent(&mod->mkobj.kobj, KOBJ_ADD);
3768*4882a593Smuzhiyun 
3769*4882a593Smuzhiyun 	/*
3770*4882a593Smuzhiyun 	 * We need to finish all async code before the module init sequence
3771*4882a593Smuzhiyun 	 * is done. This has potential to deadlock if synchronous module
3772*4882a593Smuzhiyun 	 * loading is requested from async (which is not allowed!).
3773*4882a593Smuzhiyun 	 *
3774*4882a593Smuzhiyun 	 * See commit 0fdff3ec6d87 ("async, kmod: warn on synchronous
3775*4882a593Smuzhiyun 	 * request_module() from async workers") for more details.
3776*4882a593Smuzhiyun 	 */
3777*4882a593Smuzhiyun 	if (!mod->async_probe_requested)
3778*4882a593Smuzhiyun 		async_synchronize_full();
3779*4882a593Smuzhiyun 
3780*4882a593Smuzhiyun 	ftrace_free_mem(mod, mod->init_layout.base, mod->init_layout.base +
3781*4882a593Smuzhiyun 			mod->init_layout.size);
3782*4882a593Smuzhiyun 	mutex_lock(&module_mutex);
3783*4882a593Smuzhiyun 	/* Drop initial reference. */
3784*4882a593Smuzhiyun 	module_put(mod);
3785*4882a593Smuzhiyun 	trim_init_extable(mod);
3786*4882a593Smuzhiyun #ifdef CONFIG_KALLSYMS
3787*4882a593Smuzhiyun 	/* Switch to core kallsyms now init is done: kallsyms may be walking! */
3788*4882a593Smuzhiyun 	rcu_assign_pointer(mod->kallsyms, &mod->core_kallsyms);
3789*4882a593Smuzhiyun #endif
3790*4882a593Smuzhiyun 	module_enable_ro(mod, true);
3791*4882a593Smuzhiyun 	trace_android_vh_set_module_permit_after_init(mod);
3792*4882a593Smuzhiyun 	mod_tree_remove_init(mod);
3793*4882a593Smuzhiyun 	module_arch_freeing_init(mod);
3794*4882a593Smuzhiyun 	trace_android_vh_set_memory_rw((unsigned long)mod->init_layout.base,
3795*4882a593Smuzhiyun 		(mod->init_layout.size)>>PAGE_SHIFT);
3796*4882a593Smuzhiyun 	trace_android_vh_set_memory_nx((unsigned long)mod->init_layout.base,
3797*4882a593Smuzhiyun 		(mod->init_layout.size)>>PAGE_SHIFT);
3798*4882a593Smuzhiyun 	mod->init_layout.base = NULL;
3799*4882a593Smuzhiyun 	mod->init_layout.size = 0;
3800*4882a593Smuzhiyun 	mod->init_layout.ro_size = 0;
3801*4882a593Smuzhiyun 	mod->init_layout.ro_after_init_size = 0;
3802*4882a593Smuzhiyun 	mod->init_layout.text_size = 0;
3803*4882a593Smuzhiyun 	/*
3804*4882a593Smuzhiyun 	 * We want to free module_init, but be aware that kallsyms may be
3805*4882a593Smuzhiyun 	 * walking this with preempt disabled.  In all the failure paths, we
3806*4882a593Smuzhiyun 	 * call synchronize_rcu(), but we don't want to slow down the success
3807*4882a593Smuzhiyun 	 * path. module_memfree() cannot be called in an interrupt, so do the
3808*4882a593Smuzhiyun 	 * work and call synchronize_rcu() in a work queue.
3809*4882a593Smuzhiyun 	 *
3810*4882a593Smuzhiyun 	 * Note that module_alloc() on most architectures creates W+X page
3811*4882a593Smuzhiyun 	 * mappings which won't be cleaned up until do_free_init() runs.  Any
3812*4882a593Smuzhiyun 	 * code such as mark_rodata_ro() which depends on those mappings to
3813*4882a593Smuzhiyun 	 * be cleaned up needs to sync with the queued work - ie
3814*4882a593Smuzhiyun 	 * rcu_barrier()
3815*4882a593Smuzhiyun 	 */
3816*4882a593Smuzhiyun 	if (llist_add(&freeinit->node, &init_free_list))
3817*4882a593Smuzhiyun 		schedule_work(&init_free_wq);
3818*4882a593Smuzhiyun 
3819*4882a593Smuzhiyun 	mutex_unlock(&module_mutex);
3820*4882a593Smuzhiyun 	wake_up_all(&module_wq);
3821*4882a593Smuzhiyun 
3822*4882a593Smuzhiyun 	return 0;
3823*4882a593Smuzhiyun 
3824*4882a593Smuzhiyun fail_free_freeinit:
3825*4882a593Smuzhiyun 	kfree(freeinit);
3826*4882a593Smuzhiyun fail:
3827*4882a593Smuzhiyun 	/* Try to protect us from buggy refcounters. */
3828*4882a593Smuzhiyun 	mod->state = MODULE_STATE_GOING;
3829*4882a593Smuzhiyun 	synchronize_rcu();
3830*4882a593Smuzhiyun 	module_put(mod);
3831*4882a593Smuzhiyun 	blocking_notifier_call_chain(&module_notify_list,
3832*4882a593Smuzhiyun 				     MODULE_STATE_GOING, mod);
3833*4882a593Smuzhiyun 	klp_module_going(mod);
3834*4882a593Smuzhiyun 	ftrace_release_mod(mod);
3835*4882a593Smuzhiyun 	free_module(mod);
3836*4882a593Smuzhiyun 	wake_up_all(&module_wq);
3837*4882a593Smuzhiyun 	return ret;
3838*4882a593Smuzhiyun }
3839*4882a593Smuzhiyun 
may_init_module(void)3840*4882a593Smuzhiyun static int may_init_module(void)
3841*4882a593Smuzhiyun {
3842*4882a593Smuzhiyun 	if (!capable(CAP_SYS_MODULE) || modules_disabled)
3843*4882a593Smuzhiyun 		return -EPERM;
3844*4882a593Smuzhiyun 
3845*4882a593Smuzhiyun 	return 0;
3846*4882a593Smuzhiyun }
3847*4882a593Smuzhiyun 
3848*4882a593Smuzhiyun /*
3849*4882a593Smuzhiyun  * We try to place it in the list now to make sure it's unique before
3850*4882a593Smuzhiyun  * we dedicate too many resources.  In particular, temporary percpu
3851*4882a593Smuzhiyun  * memory exhaustion.
3852*4882a593Smuzhiyun  */
add_unformed_module(struct module * mod)3853*4882a593Smuzhiyun static int add_unformed_module(struct module *mod)
3854*4882a593Smuzhiyun {
3855*4882a593Smuzhiyun 	int err;
3856*4882a593Smuzhiyun 	struct module *old;
3857*4882a593Smuzhiyun 
3858*4882a593Smuzhiyun 	mod->state = MODULE_STATE_UNFORMED;
3859*4882a593Smuzhiyun 
3860*4882a593Smuzhiyun again:
3861*4882a593Smuzhiyun 	mutex_lock(&module_mutex);
3862*4882a593Smuzhiyun 	old = find_module_all(mod->name, strlen(mod->name), true);
3863*4882a593Smuzhiyun 	if (old != NULL) {
3864*4882a593Smuzhiyun 		if (old->state != MODULE_STATE_LIVE) {
3865*4882a593Smuzhiyun 			/* Wait in case it fails to load. */
3866*4882a593Smuzhiyun 			mutex_unlock(&module_mutex);
3867*4882a593Smuzhiyun 			err = wait_event_interruptible(module_wq,
3868*4882a593Smuzhiyun 					       finished_loading(mod->name));
3869*4882a593Smuzhiyun 			if (err)
3870*4882a593Smuzhiyun 				goto out_unlocked;
3871*4882a593Smuzhiyun 			goto again;
3872*4882a593Smuzhiyun 		}
3873*4882a593Smuzhiyun 		err = -EEXIST;
3874*4882a593Smuzhiyun 		goto out;
3875*4882a593Smuzhiyun 	}
3876*4882a593Smuzhiyun 	mod_update_bounds(mod);
3877*4882a593Smuzhiyun 	list_add_rcu(&mod->list, &modules);
3878*4882a593Smuzhiyun 	mod_tree_insert(mod);
3879*4882a593Smuzhiyun 	err = 0;
3880*4882a593Smuzhiyun 
3881*4882a593Smuzhiyun out:
3882*4882a593Smuzhiyun 	mutex_unlock(&module_mutex);
3883*4882a593Smuzhiyun out_unlocked:
3884*4882a593Smuzhiyun 	return err;
3885*4882a593Smuzhiyun }
3886*4882a593Smuzhiyun 
complete_formation(struct module * mod,struct load_info * info)3887*4882a593Smuzhiyun static int complete_formation(struct module *mod, struct load_info *info)
3888*4882a593Smuzhiyun {
3889*4882a593Smuzhiyun 	int err;
3890*4882a593Smuzhiyun 
3891*4882a593Smuzhiyun 	mutex_lock(&module_mutex);
3892*4882a593Smuzhiyun 
3893*4882a593Smuzhiyun 	/* Find duplicate symbols (must be called under lock). */
3894*4882a593Smuzhiyun 	err = verify_exported_symbols(mod);
3895*4882a593Smuzhiyun 	if (err < 0)
3896*4882a593Smuzhiyun 		goto out;
3897*4882a593Smuzhiyun 
3898*4882a593Smuzhiyun 	/* This relies on module_mutex for list integrity. */
3899*4882a593Smuzhiyun 	module_bug_finalize(info->hdr, info->sechdrs, mod);
3900*4882a593Smuzhiyun 
3901*4882a593Smuzhiyun 	module_enable_ro(mod, false);
3902*4882a593Smuzhiyun 	module_enable_nx(mod);
3903*4882a593Smuzhiyun 	module_enable_x(mod);
3904*4882a593Smuzhiyun 	trace_android_vh_set_module_permit_before_init(mod);
3905*4882a593Smuzhiyun 
3906*4882a593Smuzhiyun 	/* Mark state as coming so strong_try_module_get() ignores us,
3907*4882a593Smuzhiyun 	 * but kallsyms etc. can see us. */
3908*4882a593Smuzhiyun 	mod->state = MODULE_STATE_COMING;
3909*4882a593Smuzhiyun 	mutex_unlock(&module_mutex);
3910*4882a593Smuzhiyun 
3911*4882a593Smuzhiyun 	return 0;
3912*4882a593Smuzhiyun 
3913*4882a593Smuzhiyun out:
3914*4882a593Smuzhiyun 	mutex_unlock(&module_mutex);
3915*4882a593Smuzhiyun 	return err;
3916*4882a593Smuzhiyun }
3917*4882a593Smuzhiyun 
prepare_coming_module(struct module * mod)3918*4882a593Smuzhiyun static int prepare_coming_module(struct module *mod)
3919*4882a593Smuzhiyun {
3920*4882a593Smuzhiyun 	int err;
3921*4882a593Smuzhiyun 
3922*4882a593Smuzhiyun 	ftrace_module_enable(mod);
3923*4882a593Smuzhiyun 	err = klp_module_coming(mod);
3924*4882a593Smuzhiyun 	if (err)
3925*4882a593Smuzhiyun 		return err;
3926*4882a593Smuzhiyun 
3927*4882a593Smuzhiyun 	err = blocking_notifier_call_chain_robust(&module_notify_list,
3928*4882a593Smuzhiyun 			MODULE_STATE_COMING, MODULE_STATE_GOING, mod);
3929*4882a593Smuzhiyun 	err = notifier_to_errno(err);
3930*4882a593Smuzhiyun 	if (err)
3931*4882a593Smuzhiyun 		klp_module_going(mod);
3932*4882a593Smuzhiyun 
3933*4882a593Smuzhiyun 	return err;
3934*4882a593Smuzhiyun }
3935*4882a593Smuzhiyun 
unknown_module_param_cb(char * param,char * val,const char * modname,void * arg)3936*4882a593Smuzhiyun static int unknown_module_param_cb(char *param, char *val, const char *modname,
3937*4882a593Smuzhiyun 				   void *arg)
3938*4882a593Smuzhiyun {
3939*4882a593Smuzhiyun 	struct module *mod = arg;
3940*4882a593Smuzhiyun 	int ret;
3941*4882a593Smuzhiyun 
3942*4882a593Smuzhiyun 	if (strcmp(param, "async_probe") == 0) {
3943*4882a593Smuzhiyun 		mod->async_probe_requested = true;
3944*4882a593Smuzhiyun 		return 0;
3945*4882a593Smuzhiyun 	}
3946*4882a593Smuzhiyun 
3947*4882a593Smuzhiyun 	/* Check for magic 'dyndbg' arg */
3948*4882a593Smuzhiyun 	ret = ddebug_dyndbg_module_param_cb(param, val, modname);
3949*4882a593Smuzhiyun 	if (ret != 0)
3950*4882a593Smuzhiyun 		pr_warn("%s: unknown parameter '%s' ignored\n", modname, param);
3951*4882a593Smuzhiyun 	return 0;
3952*4882a593Smuzhiyun }
3953*4882a593Smuzhiyun 
3954*4882a593Smuzhiyun static void cfi_init(struct module *mod);
3955*4882a593Smuzhiyun 
3956*4882a593Smuzhiyun /* Allocate and load the module: note that size of section 0 is always
3957*4882a593Smuzhiyun    zero, and we rely on this for optional sections. */
load_module(struct load_info * info,const char __user * uargs,int flags)3958*4882a593Smuzhiyun static int load_module(struct load_info *info, const char __user *uargs,
3959*4882a593Smuzhiyun 		       int flags)
3960*4882a593Smuzhiyun {
3961*4882a593Smuzhiyun 	struct module *mod;
3962*4882a593Smuzhiyun 	long err = 0;
3963*4882a593Smuzhiyun 	char *after_dashes;
3964*4882a593Smuzhiyun 
3965*4882a593Smuzhiyun 	/*
3966*4882a593Smuzhiyun 	 * Do the signature check (if any) first. All that
3967*4882a593Smuzhiyun 	 * the signature check needs is info->len, it does
3968*4882a593Smuzhiyun 	 * not need any of the section info. That can be
3969*4882a593Smuzhiyun 	 * set up later. This will minimize the chances
3970*4882a593Smuzhiyun 	 * of a corrupt module causing problems before
3971*4882a593Smuzhiyun 	 * we even get to the signature check.
3972*4882a593Smuzhiyun 	 *
3973*4882a593Smuzhiyun 	 * The check will also adjust info->len by stripping
3974*4882a593Smuzhiyun 	 * off the sig length at the end of the module, making
3975*4882a593Smuzhiyun 	 * checks against info->len more correct.
3976*4882a593Smuzhiyun 	 */
3977*4882a593Smuzhiyun 	err = module_sig_check(info, flags);
3978*4882a593Smuzhiyun 	if (err)
3979*4882a593Smuzhiyun 		goto free_copy;
3980*4882a593Smuzhiyun 
3981*4882a593Smuzhiyun 	/*
3982*4882a593Smuzhiyun 	 * Do basic sanity checks against the ELF header and
3983*4882a593Smuzhiyun 	 * sections.
3984*4882a593Smuzhiyun 	 */
3985*4882a593Smuzhiyun 	err = elf_validity_check(info);
3986*4882a593Smuzhiyun 	if (err) {
3987*4882a593Smuzhiyun 		pr_err("Module has invalid ELF structures\n");
3988*4882a593Smuzhiyun 		goto free_copy;
3989*4882a593Smuzhiyun 	}
3990*4882a593Smuzhiyun 
3991*4882a593Smuzhiyun 	/*
3992*4882a593Smuzhiyun 	 * Everything checks out, so set up the section info
3993*4882a593Smuzhiyun 	 * in the info structure.
3994*4882a593Smuzhiyun 	 */
3995*4882a593Smuzhiyun 	err = setup_load_info(info, flags);
3996*4882a593Smuzhiyun 	if (err)
3997*4882a593Smuzhiyun 		goto free_copy;
3998*4882a593Smuzhiyun 
3999*4882a593Smuzhiyun 	/*
4000*4882a593Smuzhiyun 	 * Now that we know we have the correct module name, check
4001*4882a593Smuzhiyun 	 * if it's blacklisted.
4002*4882a593Smuzhiyun 	 */
4003*4882a593Smuzhiyun 	if (blacklisted(info->name)) {
4004*4882a593Smuzhiyun 		err = -EPERM;
4005*4882a593Smuzhiyun 		pr_err("Module %s is blacklisted\n", info->name);
4006*4882a593Smuzhiyun 		goto free_copy;
4007*4882a593Smuzhiyun 	}
4008*4882a593Smuzhiyun 
4009*4882a593Smuzhiyun 	err = rewrite_section_headers(info, flags);
4010*4882a593Smuzhiyun 	if (err)
4011*4882a593Smuzhiyun 		goto free_copy;
4012*4882a593Smuzhiyun 
4013*4882a593Smuzhiyun 	/* Check module struct version now, before we try to use module. */
4014*4882a593Smuzhiyun 	if (!check_modstruct_version(info, info->mod)) {
4015*4882a593Smuzhiyun 		err = -ENOEXEC;
4016*4882a593Smuzhiyun 		goto free_copy;
4017*4882a593Smuzhiyun 	}
4018*4882a593Smuzhiyun 
4019*4882a593Smuzhiyun 	/* Figure out module layout, and allocate all the memory. */
4020*4882a593Smuzhiyun 	mod = layout_and_allocate(info, flags);
4021*4882a593Smuzhiyun 	if (IS_ERR(mod)) {
4022*4882a593Smuzhiyun 		err = PTR_ERR(mod);
4023*4882a593Smuzhiyun 		goto free_copy;
4024*4882a593Smuzhiyun 	}
4025*4882a593Smuzhiyun 
4026*4882a593Smuzhiyun 	audit_log_kern_module(mod->name);
4027*4882a593Smuzhiyun 
4028*4882a593Smuzhiyun 	/* Reserve our place in the list. */
4029*4882a593Smuzhiyun 	err = add_unformed_module(mod);
4030*4882a593Smuzhiyun 	if (err)
4031*4882a593Smuzhiyun 		goto free_module;
4032*4882a593Smuzhiyun 
4033*4882a593Smuzhiyun #ifdef CONFIG_MODULE_SIG
4034*4882a593Smuzhiyun 	mod->sig_ok = info->sig_ok;
4035*4882a593Smuzhiyun 	if (!mod->sig_ok) {
4036*4882a593Smuzhiyun 		pr_notice_once("%s: module verification failed: signature "
4037*4882a593Smuzhiyun 			       "and/or required key missing - tainting "
4038*4882a593Smuzhiyun 			       "kernel\n", mod->name);
4039*4882a593Smuzhiyun 		add_taint_module(mod, TAINT_UNSIGNED_MODULE, LOCKDEP_STILL_OK);
4040*4882a593Smuzhiyun 	}
4041*4882a593Smuzhiyun #endif
4042*4882a593Smuzhiyun 
4043*4882a593Smuzhiyun 	/* To avoid stressing percpu allocator, do this once we're unique. */
4044*4882a593Smuzhiyun 	err = percpu_modalloc(mod, info);
4045*4882a593Smuzhiyun 	if (err)
4046*4882a593Smuzhiyun 		goto unlink_mod;
4047*4882a593Smuzhiyun 
4048*4882a593Smuzhiyun 	/* Now module is in final location, initialize linked lists, etc. */
4049*4882a593Smuzhiyun 	err = module_unload_init(mod);
4050*4882a593Smuzhiyun 	if (err)
4051*4882a593Smuzhiyun 		goto unlink_mod;
4052*4882a593Smuzhiyun 
4053*4882a593Smuzhiyun 	init_param_lock(mod);
4054*4882a593Smuzhiyun 
4055*4882a593Smuzhiyun 	/* Now we've got everything in the final locations, we can
4056*4882a593Smuzhiyun 	 * find optional sections. */
4057*4882a593Smuzhiyun 	err = find_module_sections(mod, info);
4058*4882a593Smuzhiyun 	if (err)
4059*4882a593Smuzhiyun 		goto free_unload;
4060*4882a593Smuzhiyun 
4061*4882a593Smuzhiyun 	err = check_module_license_and_versions(mod);
4062*4882a593Smuzhiyun 	if (err)
4063*4882a593Smuzhiyun 		goto free_unload;
4064*4882a593Smuzhiyun 
4065*4882a593Smuzhiyun 	/* Set up MODINFO_ATTR fields */
4066*4882a593Smuzhiyun 	setup_modinfo(mod, info);
4067*4882a593Smuzhiyun 
4068*4882a593Smuzhiyun 	/* Fix up syms, so that st_value is a pointer to location. */
4069*4882a593Smuzhiyun 	err = simplify_symbols(mod, info);
4070*4882a593Smuzhiyun 	if (err < 0)
4071*4882a593Smuzhiyun 		goto free_modinfo;
4072*4882a593Smuzhiyun 
4073*4882a593Smuzhiyun 	err = apply_relocations(mod, info);
4074*4882a593Smuzhiyun 	if (err < 0)
4075*4882a593Smuzhiyun 		goto free_modinfo;
4076*4882a593Smuzhiyun 
4077*4882a593Smuzhiyun 	err = post_relocation(mod, info);
4078*4882a593Smuzhiyun 	if (err < 0)
4079*4882a593Smuzhiyun 		goto free_modinfo;
4080*4882a593Smuzhiyun 
4081*4882a593Smuzhiyun 	flush_module_icache(mod);
4082*4882a593Smuzhiyun 
4083*4882a593Smuzhiyun 	/* Setup CFI for the module. */
4084*4882a593Smuzhiyun 	cfi_init(mod);
4085*4882a593Smuzhiyun 
4086*4882a593Smuzhiyun 	/* Now copy in args */
4087*4882a593Smuzhiyun 	mod->args = strndup_user(uargs, ~0UL >> 1);
4088*4882a593Smuzhiyun 	if (IS_ERR(mod->args)) {
4089*4882a593Smuzhiyun 		err = PTR_ERR(mod->args);
4090*4882a593Smuzhiyun 		goto free_arch_cleanup;
4091*4882a593Smuzhiyun 	}
4092*4882a593Smuzhiyun 
4093*4882a593Smuzhiyun 	dynamic_debug_setup(mod, info->debug, info->num_debug);
4094*4882a593Smuzhiyun 
4095*4882a593Smuzhiyun 	/* Ftrace init must be called in the MODULE_STATE_UNFORMED state */
4096*4882a593Smuzhiyun 	ftrace_module_init(mod);
4097*4882a593Smuzhiyun 
4098*4882a593Smuzhiyun 	/* Finally it's fully formed, ready to start executing. */
4099*4882a593Smuzhiyun 	err = complete_formation(mod, info);
4100*4882a593Smuzhiyun 	if (err)
4101*4882a593Smuzhiyun 		goto ddebug_cleanup;
4102*4882a593Smuzhiyun 
4103*4882a593Smuzhiyun 	err = prepare_coming_module(mod);
4104*4882a593Smuzhiyun 	if (err)
4105*4882a593Smuzhiyun 		goto bug_cleanup;
4106*4882a593Smuzhiyun 
4107*4882a593Smuzhiyun 	/* Module is ready to execute: parsing args may do that. */
4108*4882a593Smuzhiyun 	after_dashes = parse_args(mod->name, mod->args, mod->kp, mod->num_kp,
4109*4882a593Smuzhiyun 				  -32768, 32767, mod,
4110*4882a593Smuzhiyun 				  unknown_module_param_cb);
4111*4882a593Smuzhiyun 	if (IS_ERR(after_dashes)) {
4112*4882a593Smuzhiyun 		err = PTR_ERR(after_dashes);
4113*4882a593Smuzhiyun 		goto coming_cleanup;
4114*4882a593Smuzhiyun 	} else if (after_dashes) {
4115*4882a593Smuzhiyun 		pr_warn("%s: parameters '%s' after `--' ignored\n",
4116*4882a593Smuzhiyun 		       mod->name, after_dashes);
4117*4882a593Smuzhiyun 	}
4118*4882a593Smuzhiyun 
4119*4882a593Smuzhiyun 	/* Link in to sysfs. */
4120*4882a593Smuzhiyun 	err = mod_sysfs_setup(mod, info, mod->kp, mod->num_kp);
4121*4882a593Smuzhiyun 	if (err < 0)
4122*4882a593Smuzhiyun 		goto coming_cleanup;
4123*4882a593Smuzhiyun 
4124*4882a593Smuzhiyun 	if (is_livepatch_module(mod)) {
4125*4882a593Smuzhiyun 		err = copy_module_elf(mod, info);
4126*4882a593Smuzhiyun 		if (err < 0)
4127*4882a593Smuzhiyun 			goto sysfs_cleanup;
4128*4882a593Smuzhiyun 	}
4129*4882a593Smuzhiyun 
4130*4882a593Smuzhiyun 	/* Get rid of temporary copy. */
4131*4882a593Smuzhiyun 	free_copy(info);
4132*4882a593Smuzhiyun 
4133*4882a593Smuzhiyun 	/* Done! */
4134*4882a593Smuzhiyun 	trace_module_load(mod);
4135*4882a593Smuzhiyun 
4136*4882a593Smuzhiyun 	return do_init_module(mod);
4137*4882a593Smuzhiyun 
4138*4882a593Smuzhiyun  sysfs_cleanup:
4139*4882a593Smuzhiyun 	mod_sysfs_teardown(mod);
4140*4882a593Smuzhiyun  coming_cleanup:
4141*4882a593Smuzhiyun 	mod->state = MODULE_STATE_GOING;
4142*4882a593Smuzhiyun 	destroy_params(mod->kp, mod->num_kp);
4143*4882a593Smuzhiyun 	blocking_notifier_call_chain(&module_notify_list,
4144*4882a593Smuzhiyun 				     MODULE_STATE_GOING, mod);
4145*4882a593Smuzhiyun 	klp_module_going(mod);
4146*4882a593Smuzhiyun  bug_cleanup:
4147*4882a593Smuzhiyun 	mod->state = MODULE_STATE_GOING;
4148*4882a593Smuzhiyun 	/* module_bug_cleanup needs module_mutex protection */
4149*4882a593Smuzhiyun 	mutex_lock(&module_mutex);
4150*4882a593Smuzhiyun 	module_bug_cleanup(mod);
4151*4882a593Smuzhiyun 	mutex_unlock(&module_mutex);
4152*4882a593Smuzhiyun 
4153*4882a593Smuzhiyun  ddebug_cleanup:
4154*4882a593Smuzhiyun 	ftrace_release_mod(mod);
4155*4882a593Smuzhiyun 	dynamic_debug_remove(mod, info->debug);
4156*4882a593Smuzhiyun 	synchronize_rcu();
4157*4882a593Smuzhiyun 	kfree(mod->args);
4158*4882a593Smuzhiyun  free_arch_cleanup:
4159*4882a593Smuzhiyun 	cfi_cleanup(mod);
4160*4882a593Smuzhiyun 	module_arch_cleanup(mod);
4161*4882a593Smuzhiyun  free_modinfo:
4162*4882a593Smuzhiyun 	free_modinfo(mod);
4163*4882a593Smuzhiyun  free_unload:
4164*4882a593Smuzhiyun 	module_unload_free(mod);
4165*4882a593Smuzhiyun  unlink_mod:
4166*4882a593Smuzhiyun 	mutex_lock(&module_mutex);
4167*4882a593Smuzhiyun 	/* Unlink carefully: kallsyms could be walking list. */
4168*4882a593Smuzhiyun 	list_del_rcu(&mod->list);
4169*4882a593Smuzhiyun 	mod_tree_remove(mod);
4170*4882a593Smuzhiyun 	wake_up_all(&module_wq);
4171*4882a593Smuzhiyun 	/* Wait for RCU-sched synchronizing before releasing mod->list. */
4172*4882a593Smuzhiyun 	synchronize_rcu();
4173*4882a593Smuzhiyun 	mutex_unlock(&module_mutex);
4174*4882a593Smuzhiyun  free_module:
4175*4882a593Smuzhiyun 	/* Free lock-classes; relies on the preceding sync_rcu() */
4176*4882a593Smuzhiyun 	lockdep_free_key_range(mod->core_layout.base, mod->core_layout.size);
4177*4882a593Smuzhiyun 
4178*4882a593Smuzhiyun 	module_deallocate(mod, info);
4179*4882a593Smuzhiyun  free_copy:
4180*4882a593Smuzhiyun 	free_copy(info);
4181*4882a593Smuzhiyun 	return err;
4182*4882a593Smuzhiyun }
4183*4882a593Smuzhiyun 
SYSCALL_DEFINE3(init_module,void __user *,umod,unsigned long,len,const char __user *,uargs)4184*4882a593Smuzhiyun SYSCALL_DEFINE3(init_module, void __user *, umod,
4185*4882a593Smuzhiyun 		unsigned long, len, const char __user *, uargs)
4186*4882a593Smuzhiyun {
4187*4882a593Smuzhiyun 	int err;
4188*4882a593Smuzhiyun 	struct load_info info = { };
4189*4882a593Smuzhiyun 
4190*4882a593Smuzhiyun 	err = may_init_module();
4191*4882a593Smuzhiyun 	if (err)
4192*4882a593Smuzhiyun 		return err;
4193*4882a593Smuzhiyun 
4194*4882a593Smuzhiyun 	pr_debug("init_module: umod=%p, len=%lu, uargs=%p\n",
4195*4882a593Smuzhiyun 	       umod, len, uargs);
4196*4882a593Smuzhiyun 
4197*4882a593Smuzhiyun 	err = copy_module_from_user(umod, len, &info);
4198*4882a593Smuzhiyun 	if (err)
4199*4882a593Smuzhiyun 		return err;
4200*4882a593Smuzhiyun 
4201*4882a593Smuzhiyun 	return load_module(&info, uargs, 0);
4202*4882a593Smuzhiyun }
4203*4882a593Smuzhiyun 
SYSCALL_DEFINE3(finit_module,int,fd,const char __user *,uargs,int,flags)4204*4882a593Smuzhiyun SYSCALL_DEFINE3(finit_module, int, fd, const char __user *, uargs, int, flags)
4205*4882a593Smuzhiyun {
4206*4882a593Smuzhiyun 	struct load_info info = { };
4207*4882a593Smuzhiyun 	void *hdr = NULL;
4208*4882a593Smuzhiyun 	int err;
4209*4882a593Smuzhiyun 
4210*4882a593Smuzhiyun 	err = may_init_module();
4211*4882a593Smuzhiyun 	if (err)
4212*4882a593Smuzhiyun 		return err;
4213*4882a593Smuzhiyun 
4214*4882a593Smuzhiyun 	pr_debug("finit_module: fd=%d, uargs=%p, flags=%i\n", fd, uargs, flags);
4215*4882a593Smuzhiyun 
4216*4882a593Smuzhiyun 	if (flags & ~(MODULE_INIT_IGNORE_MODVERSIONS
4217*4882a593Smuzhiyun 		      |MODULE_INIT_IGNORE_VERMAGIC))
4218*4882a593Smuzhiyun 		return -EINVAL;
4219*4882a593Smuzhiyun 
4220*4882a593Smuzhiyun 	err = kernel_read_file_from_fd(fd, 0, &hdr, INT_MAX, NULL,
4221*4882a593Smuzhiyun 				       READING_MODULE);
4222*4882a593Smuzhiyun 	if (err < 0)
4223*4882a593Smuzhiyun 		return err;
4224*4882a593Smuzhiyun 	info.hdr = hdr;
4225*4882a593Smuzhiyun 	info.len = err;
4226*4882a593Smuzhiyun 
4227*4882a593Smuzhiyun 	return load_module(&info, uargs, flags);
4228*4882a593Smuzhiyun }
4229*4882a593Smuzhiyun 
within(unsigned long addr,void * start,unsigned long size)4230*4882a593Smuzhiyun static inline int within(unsigned long addr, void *start, unsigned long size)
4231*4882a593Smuzhiyun {
4232*4882a593Smuzhiyun 	return ((void *)addr >= start && (void *)addr < start + size);
4233*4882a593Smuzhiyun }
4234*4882a593Smuzhiyun 
4235*4882a593Smuzhiyun #ifdef CONFIG_KALLSYMS
4236*4882a593Smuzhiyun /*
4237*4882a593Smuzhiyun  * This ignores the intensely annoying "mapping symbols" found
4238*4882a593Smuzhiyun  * in ARM ELF files: $a, $t and $d.
4239*4882a593Smuzhiyun  */
is_arm_mapping_symbol(const char * str)4240*4882a593Smuzhiyun static inline int is_arm_mapping_symbol(const char *str)
4241*4882a593Smuzhiyun {
4242*4882a593Smuzhiyun 	if (str[0] == '.' && str[1] == 'L')
4243*4882a593Smuzhiyun 		return true;
4244*4882a593Smuzhiyun 	return str[0] == '$' && strchr("axtd", str[1])
4245*4882a593Smuzhiyun 	       && (str[2] == '\0' || str[2] == '.');
4246*4882a593Smuzhiyun }
4247*4882a593Smuzhiyun 
is_cfi_typeid_symbol(const char * str)4248*4882a593Smuzhiyun static inline int is_cfi_typeid_symbol(const char *str)
4249*4882a593Smuzhiyun {
4250*4882a593Smuzhiyun 	return !strncmp(str, "__typeid__", 10);
4251*4882a593Smuzhiyun }
4252*4882a593Smuzhiyun 
kallsyms_symbol_name(struct mod_kallsyms * kallsyms,unsigned int symnum)4253*4882a593Smuzhiyun static const char *kallsyms_symbol_name(struct mod_kallsyms *kallsyms, unsigned int symnum)
4254*4882a593Smuzhiyun {
4255*4882a593Smuzhiyun 	return kallsyms->strtab + kallsyms->symtab[symnum].st_name;
4256*4882a593Smuzhiyun }
4257*4882a593Smuzhiyun 
4258*4882a593Smuzhiyun /*
4259*4882a593Smuzhiyun  * Given a module and address, find the corresponding symbol and return its name
4260*4882a593Smuzhiyun  * while providing its size and offset if needed.
4261*4882a593Smuzhiyun  */
find_kallsyms_symbol(struct module * mod,unsigned long addr,unsigned long * size,unsigned long * offset)4262*4882a593Smuzhiyun static const char *find_kallsyms_symbol(struct module *mod,
4263*4882a593Smuzhiyun 					unsigned long addr,
4264*4882a593Smuzhiyun 					unsigned long *size,
4265*4882a593Smuzhiyun 					unsigned long *offset)
4266*4882a593Smuzhiyun {
4267*4882a593Smuzhiyun 	unsigned int i, best = 0;
4268*4882a593Smuzhiyun 	unsigned long nextval, bestval;
4269*4882a593Smuzhiyun 	struct mod_kallsyms *kallsyms = rcu_dereference_sched(mod->kallsyms);
4270*4882a593Smuzhiyun 
4271*4882a593Smuzhiyun 	/* At worse, next value is at end of module */
4272*4882a593Smuzhiyun 	if (within_module_init(addr, mod))
4273*4882a593Smuzhiyun 		nextval = (unsigned long)mod->init_layout.base+mod->init_layout.text_size;
4274*4882a593Smuzhiyun 	else
4275*4882a593Smuzhiyun 		nextval = (unsigned long)mod->core_layout.base+mod->core_layout.text_size;
4276*4882a593Smuzhiyun 
4277*4882a593Smuzhiyun 	bestval = kallsyms_symbol_value(&kallsyms->symtab[best]);
4278*4882a593Smuzhiyun 
4279*4882a593Smuzhiyun 	/* Scan for closest preceding symbol, and next symbol. (ELF
4280*4882a593Smuzhiyun 	   starts real symbols at 1). */
4281*4882a593Smuzhiyun 	for (i = 1; i < kallsyms->num_symtab; i++) {
4282*4882a593Smuzhiyun 		const Elf_Sym *sym = &kallsyms->symtab[i];
4283*4882a593Smuzhiyun 		unsigned long thisval = kallsyms_symbol_value(sym);
4284*4882a593Smuzhiyun 
4285*4882a593Smuzhiyun 		if (sym->st_shndx == SHN_UNDEF)
4286*4882a593Smuzhiyun 			continue;
4287*4882a593Smuzhiyun 
4288*4882a593Smuzhiyun 		/* We ignore unnamed symbols: they're uninformative
4289*4882a593Smuzhiyun 		 * and inserted at a whim. */
4290*4882a593Smuzhiyun 		if (*kallsyms_symbol_name(kallsyms, i) == '\0'
4291*4882a593Smuzhiyun 		    || is_arm_mapping_symbol(kallsyms_symbol_name(kallsyms, i))
4292*4882a593Smuzhiyun 		    || is_cfi_typeid_symbol(kallsyms_symbol_name(kallsyms, i)))
4293*4882a593Smuzhiyun 			continue;
4294*4882a593Smuzhiyun 
4295*4882a593Smuzhiyun 		if (thisval <= addr && thisval > bestval) {
4296*4882a593Smuzhiyun 			best = i;
4297*4882a593Smuzhiyun 			bestval = thisval;
4298*4882a593Smuzhiyun 		}
4299*4882a593Smuzhiyun 		if (thisval > addr && thisval < nextval)
4300*4882a593Smuzhiyun 			nextval = thisval;
4301*4882a593Smuzhiyun 	}
4302*4882a593Smuzhiyun 
4303*4882a593Smuzhiyun 	if (!best)
4304*4882a593Smuzhiyun 		return NULL;
4305*4882a593Smuzhiyun 
4306*4882a593Smuzhiyun 	if (size)
4307*4882a593Smuzhiyun 		*size = nextval - bestval;
4308*4882a593Smuzhiyun 	if (offset)
4309*4882a593Smuzhiyun 		*offset = addr - bestval;
4310*4882a593Smuzhiyun 
4311*4882a593Smuzhiyun 	return kallsyms_symbol_name(kallsyms, best);
4312*4882a593Smuzhiyun }
4313*4882a593Smuzhiyun 
dereference_module_function_descriptor(struct module * mod,void * ptr)4314*4882a593Smuzhiyun void * __weak dereference_module_function_descriptor(struct module *mod,
4315*4882a593Smuzhiyun 						     void *ptr)
4316*4882a593Smuzhiyun {
4317*4882a593Smuzhiyun 	return ptr;
4318*4882a593Smuzhiyun }
4319*4882a593Smuzhiyun 
4320*4882a593Smuzhiyun /* For kallsyms to ask for address resolution.  NULL means not found.  Careful
4321*4882a593Smuzhiyun  * not to lock to avoid deadlock on oopses, simply disable preemption. */
module_address_lookup(unsigned long addr,unsigned long * size,unsigned long * offset,char ** modname,char * namebuf)4322*4882a593Smuzhiyun const char *module_address_lookup(unsigned long addr,
4323*4882a593Smuzhiyun 			    unsigned long *size,
4324*4882a593Smuzhiyun 			    unsigned long *offset,
4325*4882a593Smuzhiyun 			    char **modname,
4326*4882a593Smuzhiyun 			    char *namebuf)
4327*4882a593Smuzhiyun {
4328*4882a593Smuzhiyun 	const char *ret = NULL;
4329*4882a593Smuzhiyun 	struct module *mod;
4330*4882a593Smuzhiyun 
4331*4882a593Smuzhiyun 	preempt_disable();
4332*4882a593Smuzhiyun 	mod = __module_address(addr);
4333*4882a593Smuzhiyun 	if (mod) {
4334*4882a593Smuzhiyun 		if (modname)
4335*4882a593Smuzhiyun 			*modname = mod->name;
4336*4882a593Smuzhiyun 
4337*4882a593Smuzhiyun 		ret = find_kallsyms_symbol(mod, addr, size, offset);
4338*4882a593Smuzhiyun 	}
4339*4882a593Smuzhiyun 	/* Make a copy in here where it's safe */
4340*4882a593Smuzhiyun 	if (ret) {
4341*4882a593Smuzhiyun 		strncpy(namebuf, ret, KSYM_NAME_LEN - 1);
4342*4882a593Smuzhiyun 		ret = namebuf;
4343*4882a593Smuzhiyun 	}
4344*4882a593Smuzhiyun 	preempt_enable();
4345*4882a593Smuzhiyun 
4346*4882a593Smuzhiyun 	return ret;
4347*4882a593Smuzhiyun }
4348*4882a593Smuzhiyun 
lookup_module_symbol_name(unsigned long addr,char * symname)4349*4882a593Smuzhiyun int lookup_module_symbol_name(unsigned long addr, char *symname)
4350*4882a593Smuzhiyun {
4351*4882a593Smuzhiyun 	struct module *mod;
4352*4882a593Smuzhiyun 
4353*4882a593Smuzhiyun 	preempt_disable();
4354*4882a593Smuzhiyun 	list_for_each_entry_rcu(mod, &modules, list) {
4355*4882a593Smuzhiyun 		if (mod->state == MODULE_STATE_UNFORMED)
4356*4882a593Smuzhiyun 			continue;
4357*4882a593Smuzhiyun 		if (within_module(addr, mod)) {
4358*4882a593Smuzhiyun 			const char *sym;
4359*4882a593Smuzhiyun 
4360*4882a593Smuzhiyun 			sym = find_kallsyms_symbol(mod, addr, NULL, NULL);
4361*4882a593Smuzhiyun 			if (!sym)
4362*4882a593Smuzhiyun 				goto out;
4363*4882a593Smuzhiyun 
4364*4882a593Smuzhiyun 			strlcpy(symname, sym, KSYM_NAME_LEN);
4365*4882a593Smuzhiyun 			preempt_enable();
4366*4882a593Smuzhiyun 			return 0;
4367*4882a593Smuzhiyun 		}
4368*4882a593Smuzhiyun 	}
4369*4882a593Smuzhiyun out:
4370*4882a593Smuzhiyun 	preempt_enable();
4371*4882a593Smuzhiyun 	return -ERANGE;
4372*4882a593Smuzhiyun }
4373*4882a593Smuzhiyun 
lookup_module_symbol_attrs(unsigned long addr,unsigned long * size,unsigned long * offset,char * modname,char * name)4374*4882a593Smuzhiyun int lookup_module_symbol_attrs(unsigned long addr, unsigned long *size,
4375*4882a593Smuzhiyun 			unsigned long *offset, char *modname, char *name)
4376*4882a593Smuzhiyun {
4377*4882a593Smuzhiyun 	struct module *mod;
4378*4882a593Smuzhiyun 
4379*4882a593Smuzhiyun 	preempt_disable();
4380*4882a593Smuzhiyun 	list_for_each_entry_rcu(mod, &modules, list) {
4381*4882a593Smuzhiyun 		if (mod->state == MODULE_STATE_UNFORMED)
4382*4882a593Smuzhiyun 			continue;
4383*4882a593Smuzhiyun 		if (within_module(addr, mod)) {
4384*4882a593Smuzhiyun 			const char *sym;
4385*4882a593Smuzhiyun 
4386*4882a593Smuzhiyun 			sym = find_kallsyms_symbol(mod, addr, size, offset);
4387*4882a593Smuzhiyun 			if (!sym)
4388*4882a593Smuzhiyun 				goto out;
4389*4882a593Smuzhiyun 			if (modname)
4390*4882a593Smuzhiyun 				strlcpy(modname, mod->name, MODULE_NAME_LEN);
4391*4882a593Smuzhiyun 			if (name)
4392*4882a593Smuzhiyun 				strlcpy(name, sym, KSYM_NAME_LEN);
4393*4882a593Smuzhiyun 			preempt_enable();
4394*4882a593Smuzhiyun 			return 0;
4395*4882a593Smuzhiyun 		}
4396*4882a593Smuzhiyun 	}
4397*4882a593Smuzhiyun out:
4398*4882a593Smuzhiyun 	preempt_enable();
4399*4882a593Smuzhiyun 	return -ERANGE;
4400*4882a593Smuzhiyun }
4401*4882a593Smuzhiyun 
module_get_kallsym(unsigned int symnum,unsigned long * value,char * type,char * name,char * module_name,int * exported)4402*4882a593Smuzhiyun int module_get_kallsym(unsigned int symnum, unsigned long *value, char *type,
4403*4882a593Smuzhiyun 			char *name, char *module_name, int *exported)
4404*4882a593Smuzhiyun {
4405*4882a593Smuzhiyun 	struct module *mod;
4406*4882a593Smuzhiyun 
4407*4882a593Smuzhiyun 	preempt_disable();
4408*4882a593Smuzhiyun 	list_for_each_entry_rcu(mod, &modules, list) {
4409*4882a593Smuzhiyun 		struct mod_kallsyms *kallsyms;
4410*4882a593Smuzhiyun 
4411*4882a593Smuzhiyun 		if (mod->state == MODULE_STATE_UNFORMED)
4412*4882a593Smuzhiyun 			continue;
4413*4882a593Smuzhiyun 		kallsyms = rcu_dereference_sched(mod->kallsyms);
4414*4882a593Smuzhiyun 		if (symnum < kallsyms->num_symtab) {
4415*4882a593Smuzhiyun 			const Elf_Sym *sym = &kallsyms->symtab[symnum];
4416*4882a593Smuzhiyun 
4417*4882a593Smuzhiyun 			*value = kallsyms_symbol_value(sym);
4418*4882a593Smuzhiyun 			*type = kallsyms->typetab[symnum];
4419*4882a593Smuzhiyun 			strlcpy(name, kallsyms_symbol_name(kallsyms, symnum), KSYM_NAME_LEN);
4420*4882a593Smuzhiyun 			strlcpy(module_name, mod->name, MODULE_NAME_LEN);
4421*4882a593Smuzhiyun 			*exported = is_exported(name, *value, mod);
4422*4882a593Smuzhiyun 			preempt_enable();
4423*4882a593Smuzhiyun 			return 0;
4424*4882a593Smuzhiyun 		}
4425*4882a593Smuzhiyun 		symnum -= kallsyms->num_symtab;
4426*4882a593Smuzhiyun 	}
4427*4882a593Smuzhiyun 	preempt_enable();
4428*4882a593Smuzhiyun 	return -ERANGE;
4429*4882a593Smuzhiyun }
4430*4882a593Smuzhiyun 
4431*4882a593Smuzhiyun /* Given a module and name of symbol, find and return the symbol's value */
find_kallsyms_symbol_value(struct module * mod,const char * name)4432*4882a593Smuzhiyun static unsigned long find_kallsyms_symbol_value(struct module *mod, const char *name)
4433*4882a593Smuzhiyun {
4434*4882a593Smuzhiyun 	unsigned int i;
4435*4882a593Smuzhiyun 	struct mod_kallsyms *kallsyms = rcu_dereference_sched(mod->kallsyms);
4436*4882a593Smuzhiyun 
4437*4882a593Smuzhiyun 	for (i = 0; i < kallsyms->num_symtab; i++) {
4438*4882a593Smuzhiyun 		const Elf_Sym *sym = &kallsyms->symtab[i];
4439*4882a593Smuzhiyun 
4440*4882a593Smuzhiyun 		if (strcmp(name, kallsyms_symbol_name(kallsyms, i)) == 0 &&
4441*4882a593Smuzhiyun 		    sym->st_shndx != SHN_UNDEF)
4442*4882a593Smuzhiyun 			return kallsyms_symbol_value(sym);
4443*4882a593Smuzhiyun 	}
4444*4882a593Smuzhiyun 	return 0;
4445*4882a593Smuzhiyun }
4446*4882a593Smuzhiyun 
4447*4882a593Smuzhiyun /* Look for this name: can be of form module:name. */
module_kallsyms_lookup_name(const char * name)4448*4882a593Smuzhiyun unsigned long module_kallsyms_lookup_name(const char *name)
4449*4882a593Smuzhiyun {
4450*4882a593Smuzhiyun 	struct module *mod;
4451*4882a593Smuzhiyun 	char *colon;
4452*4882a593Smuzhiyun 	unsigned long ret = 0;
4453*4882a593Smuzhiyun 
4454*4882a593Smuzhiyun 	/* Don't lock: we're in enough trouble already. */
4455*4882a593Smuzhiyun 	preempt_disable();
4456*4882a593Smuzhiyun 	if ((colon = strnchr(name, MODULE_NAME_LEN, ':')) != NULL) {
4457*4882a593Smuzhiyun 		if ((mod = find_module_all(name, colon - name, false)) != NULL)
4458*4882a593Smuzhiyun 			ret = find_kallsyms_symbol_value(mod, colon+1);
4459*4882a593Smuzhiyun 	} else {
4460*4882a593Smuzhiyun 		list_for_each_entry_rcu(mod, &modules, list) {
4461*4882a593Smuzhiyun 			if (mod->state == MODULE_STATE_UNFORMED)
4462*4882a593Smuzhiyun 				continue;
4463*4882a593Smuzhiyun 			if ((ret = find_kallsyms_symbol_value(mod, name)) != 0)
4464*4882a593Smuzhiyun 				break;
4465*4882a593Smuzhiyun 		}
4466*4882a593Smuzhiyun 	}
4467*4882a593Smuzhiyun 	preempt_enable();
4468*4882a593Smuzhiyun 	return ret;
4469*4882a593Smuzhiyun }
4470*4882a593Smuzhiyun 
module_kallsyms_on_each_symbol(int (* fn)(void *,const char *,struct module *,unsigned long),void * data)4471*4882a593Smuzhiyun int module_kallsyms_on_each_symbol(int (*fn)(void *, const char *,
4472*4882a593Smuzhiyun 					     struct module *, unsigned long),
4473*4882a593Smuzhiyun 				   void *data)
4474*4882a593Smuzhiyun {
4475*4882a593Smuzhiyun 	struct module *mod;
4476*4882a593Smuzhiyun 	unsigned int i;
4477*4882a593Smuzhiyun 	int ret;
4478*4882a593Smuzhiyun 
4479*4882a593Smuzhiyun 	module_assert_mutex();
4480*4882a593Smuzhiyun 
4481*4882a593Smuzhiyun 	list_for_each_entry(mod, &modules, list) {
4482*4882a593Smuzhiyun 		/* We hold module_mutex: no need for rcu_dereference_sched */
4483*4882a593Smuzhiyun 		struct mod_kallsyms *kallsyms = mod->kallsyms;
4484*4882a593Smuzhiyun 
4485*4882a593Smuzhiyun 		if (mod->state == MODULE_STATE_UNFORMED)
4486*4882a593Smuzhiyun 			continue;
4487*4882a593Smuzhiyun 		for (i = 0; i < kallsyms->num_symtab; i++) {
4488*4882a593Smuzhiyun 			const Elf_Sym *sym = &kallsyms->symtab[i];
4489*4882a593Smuzhiyun 
4490*4882a593Smuzhiyun 			if (sym->st_shndx == SHN_UNDEF)
4491*4882a593Smuzhiyun 				continue;
4492*4882a593Smuzhiyun 
4493*4882a593Smuzhiyun 			ret = fn(data, kallsyms_symbol_name(kallsyms, i),
4494*4882a593Smuzhiyun 				 mod, kallsyms_symbol_value(sym));
4495*4882a593Smuzhiyun 			if (ret != 0)
4496*4882a593Smuzhiyun 				return ret;
4497*4882a593Smuzhiyun 		}
4498*4882a593Smuzhiyun 	}
4499*4882a593Smuzhiyun 	return 0;
4500*4882a593Smuzhiyun }
4501*4882a593Smuzhiyun #endif /* CONFIG_KALLSYMS */
4502*4882a593Smuzhiyun 
cfi_init(struct module * mod)4503*4882a593Smuzhiyun static void cfi_init(struct module *mod)
4504*4882a593Smuzhiyun {
4505*4882a593Smuzhiyun #ifdef CONFIG_CFI_CLANG
4506*4882a593Smuzhiyun 	initcall_t *init;
4507*4882a593Smuzhiyun 	exitcall_t *exit;
4508*4882a593Smuzhiyun 
4509*4882a593Smuzhiyun 	rcu_read_lock_sched();
4510*4882a593Smuzhiyun 	mod->cfi_check = (cfi_check_fn)
4511*4882a593Smuzhiyun 		find_kallsyms_symbol_value(mod, "__cfi_check");
4512*4882a593Smuzhiyun 	init = (initcall_t *)
4513*4882a593Smuzhiyun 		find_kallsyms_symbol_value(mod, "__cfi_jt_init_module");
4514*4882a593Smuzhiyun 	exit = (exitcall_t *)
4515*4882a593Smuzhiyun 		find_kallsyms_symbol_value(mod, "__cfi_jt_cleanup_module");
4516*4882a593Smuzhiyun 	rcu_read_unlock_sched();
4517*4882a593Smuzhiyun 
4518*4882a593Smuzhiyun 	/* Fix init/exit functions to point to the CFI jump table */
4519*4882a593Smuzhiyun 	if (init) mod->init = *init;
4520*4882a593Smuzhiyun 	if (exit) mod->exit = *exit;
4521*4882a593Smuzhiyun 
4522*4882a593Smuzhiyun 	cfi_module_add(mod, module_addr_min);
4523*4882a593Smuzhiyun #endif
4524*4882a593Smuzhiyun }
4525*4882a593Smuzhiyun 
cfi_cleanup(struct module * mod)4526*4882a593Smuzhiyun static void cfi_cleanup(struct module *mod)
4527*4882a593Smuzhiyun {
4528*4882a593Smuzhiyun #ifdef CONFIG_CFI_CLANG
4529*4882a593Smuzhiyun 	cfi_module_remove(mod, module_addr_min);
4530*4882a593Smuzhiyun #endif
4531*4882a593Smuzhiyun }
4532*4882a593Smuzhiyun 
4533*4882a593Smuzhiyun /* Maximum number of characters written by module_flags() */
4534*4882a593Smuzhiyun #define MODULE_FLAGS_BUF_SIZE (TAINT_FLAGS_COUNT + 4)
4535*4882a593Smuzhiyun 
4536*4882a593Smuzhiyun /* Keep in sync with MODULE_FLAGS_BUF_SIZE !!! */
module_flags(struct module * mod,char * buf)4537*4882a593Smuzhiyun static char *module_flags(struct module *mod, char *buf)
4538*4882a593Smuzhiyun {
4539*4882a593Smuzhiyun 	int bx = 0;
4540*4882a593Smuzhiyun 
4541*4882a593Smuzhiyun 	BUG_ON(mod->state == MODULE_STATE_UNFORMED);
4542*4882a593Smuzhiyun 	if (mod->taints ||
4543*4882a593Smuzhiyun 	    mod->state == MODULE_STATE_GOING ||
4544*4882a593Smuzhiyun 	    mod->state == MODULE_STATE_COMING) {
4545*4882a593Smuzhiyun 		buf[bx++] = '(';
4546*4882a593Smuzhiyun 		bx += module_flags_taint(mod, buf + bx);
4547*4882a593Smuzhiyun 		/* Show a - for module-is-being-unloaded */
4548*4882a593Smuzhiyun 		if (mod->state == MODULE_STATE_GOING)
4549*4882a593Smuzhiyun 			buf[bx++] = '-';
4550*4882a593Smuzhiyun 		/* Show a + for module-is-being-loaded */
4551*4882a593Smuzhiyun 		if (mod->state == MODULE_STATE_COMING)
4552*4882a593Smuzhiyun 			buf[bx++] = '+';
4553*4882a593Smuzhiyun 		buf[bx++] = ')';
4554*4882a593Smuzhiyun 	}
4555*4882a593Smuzhiyun 	buf[bx] = '\0';
4556*4882a593Smuzhiyun 
4557*4882a593Smuzhiyun 	return buf;
4558*4882a593Smuzhiyun }
4559*4882a593Smuzhiyun 
4560*4882a593Smuzhiyun #ifdef CONFIG_PROC_FS
4561*4882a593Smuzhiyun /* Called by the /proc file system to return a list of modules. */
m_start(struct seq_file * m,loff_t * pos)4562*4882a593Smuzhiyun static void *m_start(struct seq_file *m, loff_t *pos)
4563*4882a593Smuzhiyun {
4564*4882a593Smuzhiyun 	mutex_lock(&module_mutex);
4565*4882a593Smuzhiyun 	return seq_list_start(&modules, *pos);
4566*4882a593Smuzhiyun }
4567*4882a593Smuzhiyun 
m_next(struct seq_file * m,void * p,loff_t * pos)4568*4882a593Smuzhiyun static void *m_next(struct seq_file *m, void *p, loff_t *pos)
4569*4882a593Smuzhiyun {
4570*4882a593Smuzhiyun 	return seq_list_next(p, &modules, pos);
4571*4882a593Smuzhiyun }
4572*4882a593Smuzhiyun 
m_stop(struct seq_file * m,void * p)4573*4882a593Smuzhiyun static void m_stop(struct seq_file *m, void *p)
4574*4882a593Smuzhiyun {
4575*4882a593Smuzhiyun 	mutex_unlock(&module_mutex);
4576*4882a593Smuzhiyun }
4577*4882a593Smuzhiyun 
m_show(struct seq_file * m,void * p)4578*4882a593Smuzhiyun static int m_show(struct seq_file *m, void *p)
4579*4882a593Smuzhiyun {
4580*4882a593Smuzhiyun 	struct module *mod = list_entry(p, struct module, list);
4581*4882a593Smuzhiyun 	char buf[MODULE_FLAGS_BUF_SIZE];
4582*4882a593Smuzhiyun 	void *value;
4583*4882a593Smuzhiyun 
4584*4882a593Smuzhiyun 	/* We always ignore unformed modules. */
4585*4882a593Smuzhiyun 	if (mod->state == MODULE_STATE_UNFORMED)
4586*4882a593Smuzhiyun 		return 0;
4587*4882a593Smuzhiyun 
4588*4882a593Smuzhiyun 	seq_printf(m, "%s %u",
4589*4882a593Smuzhiyun 		   mod->name, mod->init_layout.size + mod->core_layout.size);
4590*4882a593Smuzhiyun 	print_unload_info(m, mod);
4591*4882a593Smuzhiyun 
4592*4882a593Smuzhiyun 	/* Informative for users. */
4593*4882a593Smuzhiyun 	seq_printf(m, " %s",
4594*4882a593Smuzhiyun 		   mod->state == MODULE_STATE_GOING ? "Unloading" :
4595*4882a593Smuzhiyun 		   mod->state == MODULE_STATE_COMING ? "Loading" :
4596*4882a593Smuzhiyun 		   "Live");
4597*4882a593Smuzhiyun 	/* Used by oprofile and other similar tools. */
4598*4882a593Smuzhiyun 	value = m->private ? NULL : mod->core_layout.base;
4599*4882a593Smuzhiyun 	seq_printf(m, " 0x%px", value);
4600*4882a593Smuzhiyun 
4601*4882a593Smuzhiyun 	/* Taints info */
4602*4882a593Smuzhiyun 	if (mod->taints)
4603*4882a593Smuzhiyun 		seq_printf(m, " %s", module_flags(mod, buf));
4604*4882a593Smuzhiyun 
4605*4882a593Smuzhiyun 	seq_puts(m, "\n");
4606*4882a593Smuzhiyun 	return 0;
4607*4882a593Smuzhiyun }
4608*4882a593Smuzhiyun 
4609*4882a593Smuzhiyun /* Format: modulename size refcount deps address
4610*4882a593Smuzhiyun 
4611*4882a593Smuzhiyun    Where refcount is a number or -, and deps is a comma-separated list
4612*4882a593Smuzhiyun    of depends or -.
4613*4882a593Smuzhiyun */
4614*4882a593Smuzhiyun static const struct seq_operations modules_op = {
4615*4882a593Smuzhiyun 	.start	= m_start,
4616*4882a593Smuzhiyun 	.next	= m_next,
4617*4882a593Smuzhiyun 	.stop	= m_stop,
4618*4882a593Smuzhiyun 	.show	= m_show
4619*4882a593Smuzhiyun };
4620*4882a593Smuzhiyun 
4621*4882a593Smuzhiyun /*
4622*4882a593Smuzhiyun  * This also sets the "private" pointer to non-NULL if the
4623*4882a593Smuzhiyun  * kernel pointers should be hidden (so you can just test
4624*4882a593Smuzhiyun  * "m->private" to see if you should keep the values private).
4625*4882a593Smuzhiyun  *
4626*4882a593Smuzhiyun  * We use the same logic as for /proc/kallsyms.
4627*4882a593Smuzhiyun  */
modules_open(struct inode * inode,struct file * file)4628*4882a593Smuzhiyun static int modules_open(struct inode *inode, struct file *file)
4629*4882a593Smuzhiyun {
4630*4882a593Smuzhiyun 	int err = seq_open(file, &modules_op);
4631*4882a593Smuzhiyun 
4632*4882a593Smuzhiyun 	if (!err) {
4633*4882a593Smuzhiyun 		struct seq_file *m = file->private_data;
4634*4882a593Smuzhiyun 		m->private = kallsyms_show_value(file->f_cred) ? NULL : (void *)8ul;
4635*4882a593Smuzhiyun 	}
4636*4882a593Smuzhiyun 
4637*4882a593Smuzhiyun 	return err;
4638*4882a593Smuzhiyun }
4639*4882a593Smuzhiyun 
4640*4882a593Smuzhiyun static const struct proc_ops modules_proc_ops = {
4641*4882a593Smuzhiyun 	.proc_flags	= PROC_ENTRY_PERMANENT,
4642*4882a593Smuzhiyun 	.proc_open	= modules_open,
4643*4882a593Smuzhiyun 	.proc_read	= seq_read,
4644*4882a593Smuzhiyun 	.proc_lseek	= seq_lseek,
4645*4882a593Smuzhiyun 	.proc_release	= seq_release,
4646*4882a593Smuzhiyun };
4647*4882a593Smuzhiyun 
proc_modules_init(void)4648*4882a593Smuzhiyun static int __init proc_modules_init(void)
4649*4882a593Smuzhiyun {
4650*4882a593Smuzhiyun 	proc_create("modules", 0, NULL, &modules_proc_ops);
4651*4882a593Smuzhiyun 	return 0;
4652*4882a593Smuzhiyun }
4653*4882a593Smuzhiyun module_init(proc_modules_init);
4654*4882a593Smuzhiyun #endif
4655*4882a593Smuzhiyun 
4656*4882a593Smuzhiyun /* Given an address, look for it in the module exception tables. */
search_module_extables(unsigned long addr)4657*4882a593Smuzhiyun const struct exception_table_entry *search_module_extables(unsigned long addr)
4658*4882a593Smuzhiyun {
4659*4882a593Smuzhiyun 	const struct exception_table_entry *e = NULL;
4660*4882a593Smuzhiyun 	struct module *mod;
4661*4882a593Smuzhiyun 
4662*4882a593Smuzhiyun 	preempt_disable();
4663*4882a593Smuzhiyun 	mod = __module_address(addr);
4664*4882a593Smuzhiyun 	if (!mod)
4665*4882a593Smuzhiyun 		goto out;
4666*4882a593Smuzhiyun 
4667*4882a593Smuzhiyun 	if (!mod->num_exentries)
4668*4882a593Smuzhiyun 		goto out;
4669*4882a593Smuzhiyun 
4670*4882a593Smuzhiyun 	e = search_extable(mod->extable,
4671*4882a593Smuzhiyun 			   mod->num_exentries,
4672*4882a593Smuzhiyun 			   addr);
4673*4882a593Smuzhiyun out:
4674*4882a593Smuzhiyun 	preempt_enable();
4675*4882a593Smuzhiyun 
4676*4882a593Smuzhiyun 	/*
4677*4882a593Smuzhiyun 	 * Now, if we found one, we are running inside it now, hence
4678*4882a593Smuzhiyun 	 * we cannot unload the module, hence no refcnt needed.
4679*4882a593Smuzhiyun 	 */
4680*4882a593Smuzhiyun 	return e;
4681*4882a593Smuzhiyun }
4682*4882a593Smuzhiyun 
4683*4882a593Smuzhiyun /*
4684*4882a593Smuzhiyun  * is_module_address - is this address inside a module?
4685*4882a593Smuzhiyun  * @addr: the address to check.
4686*4882a593Smuzhiyun  *
4687*4882a593Smuzhiyun  * See is_module_text_address() if you simply want to see if the address
4688*4882a593Smuzhiyun  * is code (not data).
4689*4882a593Smuzhiyun  */
is_module_address(unsigned long addr)4690*4882a593Smuzhiyun bool is_module_address(unsigned long addr)
4691*4882a593Smuzhiyun {
4692*4882a593Smuzhiyun 	bool ret;
4693*4882a593Smuzhiyun 
4694*4882a593Smuzhiyun 	preempt_disable();
4695*4882a593Smuzhiyun 	ret = __module_address(addr) != NULL;
4696*4882a593Smuzhiyun 	preempt_enable();
4697*4882a593Smuzhiyun 
4698*4882a593Smuzhiyun 	return ret;
4699*4882a593Smuzhiyun }
4700*4882a593Smuzhiyun 
4701*4882a593Smuzhiyun /*
4702*4882a593Smuzhiyun  * __module_address - get the module which contains an address.
4703*4882a593Smuzhiyun  * @addr: the address.
4704*4882a593Smuzhiyun  *
4705*4882a593Smuzhiyun  * Must be called with preempt disabled or module mutex held so that
4706*4882a593Smuzhiyun  * module doesn't get freed during this.
4707*4882a593Smuzhiyun  */
__module_address(unsigned long addr)4708*4882a593Smuzhiyun struct module *__module_address(unsigned long addr)
4709*4882a593Smuzhiyun {
4710*4882a593Smuzhiyun 	struct module *mod;
4711*4882a593Smuzhiyun 
4712*4882a593Smuzhiyun 	if (addr < module_addr_min || addr > module_addr_max)
4713*4882a593Smuzhiyun 		return NULL;
4714*4882a593Smuzhiyun 
4715*4882a593Smuzhiyun 	module_assert_mutex_or_preempt();
4716*4882a593Smuzhiyun 
4717*4882a593Smuzhiyun 	mod = mod_find(addr);
4718*4882a593Smuzhiyun 	if (mod) {
4719*4882a593Smuzhiyun 		BUG_ON(!within_module(addr, mod));
4720*4882a593Smuzhiyun 		if (mod->state == MODULE_STATE_UNFORMED)
4721*4882a593Smuzhiyun 			mod = NULL;
4722*4882a593Smuzhiyun 	}
4723*4882a593Smuzhiyun 	return mod;
4724*4882a593Smuzhiyun }
4725*4882a593Smuzhiyun 
4726*4882a593Smuzhiyun /*
4727*4882a593Smuzhiyun  * is_module_text_address - is this address inside module code?
4728*4882a593Smuzhiyun  * @addr: the address to check.
4729*4882a593Smuzhiyun  *
4730*4882a593Smuzhiyun  * See is_module_address() if you simply want to see if the address is
4731*4882a593Smuzhiyun  * anywhere in a module.  See kernel_text_address() for testing if an
4732*4882a593Smuzhiyun  * address corresponds to kernel or module code.
4733*4882a593Smuzhiyun  */
is_module_text_address(unsigned long addr)4734*4882a593Smuzhiyun bool is_module_text_address(unsigned long addr)
4735*4882a593Smuzhiyun {
4736*4882a593Smuzhiyun 	bool ret;
4737*4882a593Smuzhiyun 
4738*4882a593Smuzhiyun 	preempt_disable();
4739*4882a593Smuzhiyun 	ret = __module_text_address(addr) != NULL;
4740*4882a593Smuzhiyun 	preempt_enable();
4741*4882a593Smuzhiyun 
4742*4882a593Smuzhiyun 	return ret;
4743*4882a593Smuzhiyun }
4744*4882a593Smuzhiyun 
4745*4882a593Smuzhiyun /*
4746*4882a593Smuzhiyun  * __module_text_address - get the module whose code contains an address.
4747*4882a593Smuzhiyun  * @addr: the address.
4748*4882a593Smuzhiyun  *
4749*4882a593Smuzhiyun  * Must be called with preempt disabled or module mutex held so that
4750*4882a593Smuzhiyun  * module doesn't get freed during this.
4751*4882a593Smuzhiyun  */
__module_text_address(unsigned long addr)4752*4882a593Smuzhiyun struct module *__module_text_address(unsigned long addr)
4753*4882a593Smuzhiyun {
4754*4882a593Smuzhiyun 	struct module *mod = __module_address(addr);
4755*4882a593Smuzhiyun 	if (mod) {
4756*4882a593Smuzhiyun 		/* Make sure it's within the text section. */
4757*4882a593Smuzhiyun 		if (!within(addr, mod->init_layout.base, mod->init_layout.text_size)
4758*4882a593Smuzhiyun 		    && !within(addr, mod->core_layout.base, mod->core_layout.text_size))
4759*4882a593Smuzhiyun 			mod = NULL;
4760*4882a593Smuzhiyun 	}
4761*4882a593Smuzhiyun 	return mod;
4762*4882a593Smuzhiyun }
4763*4882a593Smuzhiyun 
4764*4882a593Smuzhiyun /* Don't grab lock, we're oopsing. */
print_modules(void)4765*4882a593Smuzhiyun void print_modules(void)
4766*4882a593Smuzhiyun {
4767*4882a593Smuzhiyun 	struct module *mod;
4768*4882a593Smuzhiyun 	char buf[MODULE_FLAGS_BUF_SIZE];
4769*4882a593Smuzhiyun 
4770*4882a593Smuzhiyun 	printk(KERN_DEFAULT "Modules linked in:");
4771*4882a593Smuzhiyun 	/* Most callers should already have preempt disabled, but make sure */
4772*4882a593Smuzhiyun 	preempt_disable();
4773*4882a593Smuzhiyun 	list_for_each_entry_rcu(mod, &modules, list) {
4774*4882a593Smuzhiyun 		if (mod->state == MODULE_STATE_UNFORMED)
4775*4882a593Smuzhiyun 			continue;
4776*4882a593Smuzhiyun 		pr_cont(" %s%s", mod->name, module_flags(mod, buf));
4777*4882a593Smuzhiyun 	}
4778*4882a593Smuzhiyun 	preempt_enable();
4779*4882a593Smuzhiyun 	if (last_unloaded_module[0])
4780*4882a593Smuzhiyun 		pr_cont(" [last unloaded: %s]", last_unloaded_module);
4781*4882a593Smuzhiyun 	pr_cont("\n");
4782*4882a593Smuzhiyun }
4783*4882a593Smuzhiyun 
4784*4882a593Smuzhiyun #ifdef CONFIG_ANDROID_DEBUG_SYMBOLS
android_debug_for_each_module(int (* fn)(const char * mod_name,void * mod_addr,void * data),void * data)4785*4882a593Smuzhiyun void android_debug_for_each_module(int (*fn)(const char *mod_name, void *mod_addr, void *data),
4786*4882a593Smuzhiyun 	void *data)
4787*4882a593Smuzhiyun {
4788*4882a593Smuzhiyun 	struct module *module;
4789*4882a593Smuzhiyun 
4790*4882a593Smuzhiyun 	preempt_disable();
4791*4882a593Smuzhiyun 	list_for_each_entry_rcu(module, &modules, list) {
4792*4882a593Smuzhiyun 		if (fn(module->name, module->core_layout.base, data))
4793*4882a593Smuzhiyun 			goto out;
4794*4882a593Smuzhiyun 	}
4795*4882a593Smuzhiyun out:
4796*4882a593Smuzhiyun 	preempt_enable();
4797*4882a593Smuzhiyun }
4798*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(android_debug_for_each_module);
4799*4882a593Smuzhiyun #endif
4800*4882a593Smuzhiyun 
4801*4882a593Smuzhiyun #ifdef CONFIG_MODVERSIONS
4802*4882a593Smuzhiyun /* Generate the signature for all relevant module structures here.
4803*4882a593Smuzhiyun  * If these change, we don't want to try to parse the module. */
module_layout(struct module * mod,struct modversion_info * ver,struct kernel_param * kp,struct kernel_symbol * ks,struct tracepoint * const * tp)4804*4882a593Smuzhiyun void module_layout(struct module *mod,
4805*4882a593Smuzhiyun 		   struct modversion_info *ver,
4806*4882a593Smuzhiyun 		   struct kernel_param *kp,
4807*4882a593Smuzhiyun 		   struct kernel_symbol *ks,
4808*4882a593Smuzhiyun 		   struct tracepoint * const *tp)
4809*4882a593Smuzhiyun {
4810*4882a593Smuzhiyun }
4811*4882a593Smuzhiyun EXPORT_SYMBOL(module_layout);
4812*4882a593Smuzhiyun #endif
4813