xref: /openbmc/linux/kernel/module/internal.h (revision ae6385af)
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 /* Module internals
3  *
4  * Copyright (C) 2012 Red Hat, Inc. All Rights Reserved.
5  * Written by David Howells (dhowells@redhat.com)
6  */
7 
8 #include <linux/elf.h>
9 #include <linux/compiler.h>
10 #include <linux/module.h>
11 #include <linux/mutex.h>
12 #include <linux/rculist.h>
13 #include <linux/rcupdate.h>
14 #include <linux/mm.h>
15 
16 #ifndef ARCH_SHF_SMALL
17 #define ARCH_SHF_SMALL 0
18 #endif
19 
20 /*
21  * Use highest 4 bits of sh_entsize to store the mod_mem_type of this
22  * section. This leaves 28 bits for offset on 32-bit systems, which is
23  * about 256 MiB (WARN_ON_ONCE if we exceed that).
24  */
25 
26 #define SH_ENTSIZE_TYPE_BITS	4
27 #define SH_ENTSIZE_TYPE_SHIFT	(BITS_PER_LONG - SH_ENTSIZE_TYPE_BITS)
28 #define SH_ENTSIZE_TYPE_MASK	((1UL << SH_ENTSIZE_TYPE_BITS) - 1)
29 #define SH_ENTSIZE_OFFSET_MASK	((1UL << (BITS_PER_LONG - SH_ENTSIZE_TYPE_BITS)) - 1)
30 
31 /* Maximum number of characters written by module_flags() */
32 #define MODULE_FLAGS_BUF_SIZE (TAINT_FLAGS_COUNT + 4)
33 
34 extern struct mutex module_mutex;
35 extern struct list_head modules;
36 
37 extern struct module_attribute *modinfo_attrs[];
38 extern size_t modinfo_attrs_count;
39 
40 /* Provided by the linker */
41 extern const struct kernel_symbol __start___ksymtab[];
42 extern const struct kernel_symbol __stop___ksymtab[];
43 extern const struct kernel_symbol __start___ksymtab_gpl[];
44 extern const struct kernel_symbol __stop___ksymtab_gpl[];
45 extern const s32 __start___kcrctab[];
46 extern const s32 __start___kcrctab_gpl[];
47 
48 struct load_info {
49 	const char *name;
50 	/* pointer to module in temporary copy, freed at end of load_module() */
51 	struct module *mod;
52 	Elf_Ehdr *hdr;
53 	unsigned long len;
54 	Elf_Shdr *sechdrs;
55 	char *secstrings, *strtab;
56 	unsigned long symoffs, stroffs, init_typeoffs, core_typeoffs;
57 	bool sig_ok;
58 #ifdef CONFIG_KALLSYMS
59 	unsigned long mod_kallsyms_init_off;
60 #endif
61 #ifdef CONFIG_MODULE_DECOMPRESS
62 	struct page **pages;
63 	unsigned int max_pages;
64 	unsigned int used_pages;
65 #endif
66 	struct {
67 		unsigned int sym, str, mod, vers, info, pcpu;
68 	} index;
69 };
70 
71 enum mod_license {
72 	NOT_GPL_ONLY,
73 	GPL_ONLY,
74 };
75 
76 struct find_symbol_arg {
77 	/* Input */
78 	const char *name;
79 	bool gplok;
80 	bool warn;
81 
82 	/* Output */
83 	struct module *owner;
84 	const s32 *crc;
85 	const struct kernel_symbol *sym;
86 	enum mod_license license;
87 };
88 
89 int mod_verify_sig(const void *mod, struct load_info *info);
90 int try_to_force_load(struct module *mod, const char *reason);
91 bool find_symbol(struct find_symbol_arg *fsa);
92 struct module *find_module_all(const char *name, size_t len, bool even_unformed);
93 int cmp_name(const void *name, const void *sym);
94 long module_get_offset_and_type(struct module *mod, enum mod_mem_type type,
95 				Elf_Shdr *sechdr, unsigned int section);
96 char *module_flags(struct module *mod, char *buf, bool show_state);
97 size_t module_flags_taint(unsigned long taints, char *buf);
98 
99 char *module_next_tag_pair(char *string, unsigned long *secsize);
100 
101 #define for_each_modinfo_entry(entry, info, name) \
102 	for (entry = get_modinfo(info, name); entry; entry = get_next_modinfo(info, name, entry))
103 
104 static inline void module_assert_mutex_or_preempt(void)
105 {
106 #ifdef CONFIG_LOCKDEP
107 	if (unlikely(!debug_locks))
108 		return;
109 
110 	WARN_ON_ONCE(!rcu_read_lock_sched_held() &&
111 		     !lockdep_is_held(&module_mutex));
112 #endif
113 }
114 
115 static inline unsigned long kernel_symbol_value(const struct kernel_symbol *sym)
116 {
117 #ifdef CONFIG_HAVE_ARCH_PREL32_RELOCATIONS
118 	return (unsigned long)offset_to_ptr(&sym->value_offset);
119 #else
120 	return sym->value;
121 #endif
122 }
123 
124 #ifdef CONFIG_LIVEPATCH
125 int copy_module_elf(struct module *mod, struct load_info *info);
126 void free_module_elf(struct module *mod);
127 #else /* !CONFIG_LIVEPATCH */
128 static inline int copy_module_elf(struct module *mod, struct load_info *info)
129 {
130 	return 0;
131 }
132 
133 static inline void free_module_elf(struct module *mod) { }
134 #endif /* CONFIG_LIVEPATCH */
135 
136 static inline bool set_livepatch_module(struct module *mod)
137 {
138 #ifdef CONFIG_LIVEPATCH
139 	mod->klp = true;
140 	return true;
141 #else
142 	return false;
143 #endif
144 }
145 
146 #ifdef CONFIG_MODULE_UNLOAD_TAINT_TRACKING
147 struct mod_unload_taint {
148 	struct list_head list;
149 	char name[MODULE_NAME_LEN];
150 	unsigned long taints;
151 	u64 count;
152 };
153 
154 int try_add_tainted_module(struct module *mod);
155 void print_unloaded_tainted_modules(void);
156 #else /* !CONFIG_MODULE_UNLOAD_TAINT_TRACKING */
157 static inline int try_add_tainted_module(struct module *mod)
158 {
159 	return 0;
160 }
161 
162 static inline void print_unloaded_tainted_modules(void)
163 {
164 }
165 #endif /* CONFIG_MODULE_UNLOAD_TAINT_TRACKING */
166 
167 #ifdef CONFIG_MODULE_DECOMPRESS
168 int module_decompress(struct load_info *info, const void *buf, size_t size);
169 void module_decompress_cleanup(struct load_info *info);
170 #else
171 static inline int module_decompress(struct load_info *info,
172 				    const void *buf, size_t size)
173 {
174 	return -EOPNOTSUPP;
175 }
176 
177 static inline void module_decompress_cleanup(struct load_info *info)
178 {
179 }
180 #endif
181 
182 struct mod_tree_root {
183 #ifdef CONFIG_MODULES_TREE_LOOKUP
184 	struct latch_tree_root root;
185 #endif
186 	unsigned long addr_min;
187 	unsigned long addr_max;
188 #ifdef CONFIG_ARCH_WANTS_MODULES_DATA_IN_VMALLOC
189 	unsigned long data_addr_min;
190 	unsigned long data_addr_max;
191 #endif
192 };
193 
194 extern struct mod_tree_root mod_tree;
195 
196 #ifdef CONFIG_MODULES_TREE_LOOKUP
197 void mod_tree_insert(struct module *mod);
198 void mod_tree_remove_init(struct module *mod);
199 void mod_tree_remove(struct module *mod);
200 struct module *mod_find(unsigned long addr, struct mod_tree_root *tree);
201 #else /* !CONFIG_MODULES_TREE_LOOKUP */
202 
203 static inline void mod_tree_insert(struct module *mod) { }
204 static inline void mod_tree_remove_init(struct module *mod) { }
205 static inline void mod_tree_remove(struct module *mod) { }
206 static inline struct module *mod_find(unsigned long addr, struct mod_tree_root *tree)
207 {
208 	struct module *mod;
209 
210 	list_for_each_entry_rcu(mod, &modules, list,
211 				lockdep_is_held(&module_mutex)) {
212 		if (within_module(addr, mod))
213 			return mod;
214 	}
215 
216 	return NULL;
217 }
218 #endif /* CONFIG_MODULES_TREE_LOOKUP */
219 
220 void module_enable_ro(const struct module *mod, bool after_init);
221 void module_enable_nx(const struct module *mod);
222 void module_enable_x(const struct module *mod);
223 int module_enforce_rwx_sections(Elf_Ehdr *hdr, Elf_Shdr *sechdrs,
224 				char *secstrings, struct module *mod);
225 
226 #ifdef CONFIG_MODULE_SIG
227 int module_sig_check(struct load_info *info, int flags);
228 #else /* !CONFIG_MODULE_SIG */
229 static inline int module_sig_check(struct load_info *info, int flags)
230 {
231 	return 0;
232 }
233 #endif /* !CONFIG_MODULE_SIG */
234 
235 #ifdef CONFIG_DEBUG_KMEMLEAK
236 void kmemleak_load_module(const struct module *mod, const struct load_info *info);
237 #else /* !CONFIG_DEBUG_KMEMLEAK */
238 static inline void kmemleak_load_module(const struct module *mod,
239 					const struct load_info *info) { }
240 #endif /* CONFIG_DEBUG_KMEMLEAK */
241 
242 #ifdef CONFIG_KALLSYMS
243 void init_build_id(struct module *mod, const struct load_info *info);
244 void layout_symtab(struct module *mod, struct load_info *info);
245 void add_kallsyms(struct module *mod, const struct load_info *info);
246 unsigned long find_kallsyms_symbol_value(struct module *mod, const char *name);
247 
248 static inline bool sect_empty(const Elf_Shdr *sect)
249 {
250 	return !(sect->sh_flags & SHF_ALLOC) || sect->sh_size == 0;
251 }
252 #else /* !CONFIG_KALLSYMS */
253 static inline void init_build_id(struct module *mod, const struct load_info *info) { }
254 static inline void layout_symtab(struct module *mod, struct load_info *info) { }
255 static inline void add_kallsyms(struct module *mod, const struct load_info *info) { }
256 #endif /* CONFIG_KALLSYMS */
257 
258 #ifdef CONFIG_SYSFS
259 int mod_sysfs_setup(struct module *mod, const struct load_info *info,
260 		    struct kernel_param *kparam, unsigned int num_params);
261 void mod_sysfs_teardown(struct module *mod);
262 void init_param_lock(struct module *mod);
263 #else /* !CONFIG_SYSFS */
264 static inline int mod_sysfs_setup(struct module *mod,
265 			   	  const struct load_info *info,
266 			   	  struct kernel_param *kparam,
267 			   	  unsigned int num_params)
268 {
269 	return 0;
270 }
271 
272 static inline void mod_sysfs_teardown(struct module *mod) { }
273 static inline void init_param_lock(struct module *mod) { }
274 #endif /* CONFIG_SYSFS */
275 
276 #ifdef CONFIG_MODVERSIONS
277 int check_version(const struct load_info *info,
278 		  const char *symname, struct module *mod, const s32 *crc);
279 void module_layout(struct module *mod, struct modversion_info *ver, struct kernel_param *kp,
280 		   struct kernel_symbol *ks, struct tracepoint * const *tp);
281 int check_modstruct_version(const struct load_info *info, struct module *mod);
282 int same_magic(const char *amagic, const char *bmagic, bool has_crcs);
283 #else /* !CONFIG_MODVERSIONS */
284 static inline int check_version(const struct load_info *info,
285 				const char *symname,
286 				struct module *mod,
287 				const s32 *crc)
288 {
289 	return 1;
290 }
291 
292 static inline int check_modstruct_version(const struct load_info *info,
293 					  struct module *mod)
294 {
295 	return 1;
296 }
297 
298 static inline int same_magic(const char *amagic, const char *bmagic, bool has_crcs)
299 {
300 	return strcmp(amagic, bmagic) == 0;
301 }
302 #endif /* CONFIG_MODVERSIONS */
303