xref: /linux-6.15/kernel/module/internal.h (revision 7deabd67)
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 ([email protected])
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 static inline void module_assert_mutex_or_preempt(void)
100 {
101 #ifdef CONFIG_LOCKDEP
102 	if (unlikely(!debug_locks))
103 		return;
104 
105 	WARN_ON_ONCE(!rcu_read_lock_sched_held() &&
106 		     !lockdep_is_held(&module_mutex));
107 #endif
108 }
109 
110 static inline unsigned long kernel_symbol_value(const struct kernel_symbol *sym)
111 {
112 #ifdef CONFIG_HAVE_ARCH_PREL32_RELOCATIONS
113 	return (unsigned long)offset_to_ptr(&sym->value_offset);
114 #else
115 	return sym->value;
116 #endif
117 }
118 
119 #ifdef CONFIG_LIVEPATCH
120 int copy_module_elf(struct module *mod, struct load_info *info);
121 void free_module_elf(struct module *mod);
122 #else /* !CONFIG_LIVEPATCH */
123 static inline int copy_module_elf(struct module *mod, struct load_info *info)
124 {
125 	return 0;
126 }
127 
128 static inline void free_module_elf(struct module *mod) { }
129 #endif /* CONFIG_LIVEPATCH */
130 
131 static inline bool set_livepatch_module(struct module *mod)
132 {
133 #ifdef CONFIG_LIVEPATCH
134 	mod->klp = true;
135 	return true;
136 #else
137 	return false;
138 #endif
139 }
140 
141 #ifdef CONFIG_MODULE_UNLOAD_TAINT_TRACKING
142 struct mod_unload_taint {
143 	struct list_head list;
144 	char name[MODULE_NAME_LEN];
145 	unsigned long taints;
146 	u64 count;
147 };
148 
149 int try_add_tainted_module(struct module *mod);
150 void print_unloaded_tainted_modules(void);
151 #else /* !CONFIG_MODULE_UNLOAD_TAINT_TRACKING */
152 static inline int try_add_tainted_module(struct module *mod)
153 {
154 	return 0;
155 }
156 
157 static inline void print_unloaded_tainted_modules(void)
158 {
159 }
160 #endif /* CONFIG_MODULE_UNLOAD_TAINT_TRACKING */
161 
162 #ifdef CONFIG_MODULE_DECOMPRESS
163 int module_decompress(struct load_info *info, const void *buf, size_t size);
164 void module_decompress_cleanup(struct load_info *info);
165 #else
166 static inline int module_decompress(struct load_info *info,
167 				    const void *buf, size_t size)
168 {
169 	return -EOPNOTSUPP;
170 }
171 
172 static inline void module_decompress_cleanup(struct load_info *info)
173 {
174 }
175 #endif
176 
177 struct mod_tree_root {
178 #ifdef CONFIG_MODULES_TREE_LOOKUP
179 	struct latch_tree_root root;
180 #endif
181 	unsigned long addr_min;
182 	unsigned long addr_max;
183 #ifdef CONFIG_ARCH_WANTS_MODULES_DATA_IN_VMALLOC
184 	unsigned long data_addr_min;
185 	unsigned long data_addr_max;
186 #endif
187 };
188 
189 extern struct mod_tree_root mod_tree;
190 
191 #ifdef CONFIG_MODULES_TREE_LOOKUP
192 void mod_tree_insert(struct module *mod);
193 void mod_tree_remove_init(struct module *mod);
194 void mod_tree_remove(struct module *mod);
195 struct module *mod_find(unsigned long addr, struct mod_tree_root *tree);
196 #else /* !CONFIG_MODULES_TREE_LOOKUP */
197 
198 static inline void mod_tree_insert(struct module *mod) { }
199 static inline void mod_tree_remove_init(struct module *mod) { }
200 static inline void mod_tree_remove(struct module *mod) { }
201 static inline struct module *mod_find(unsigned long addr, struct mod_tree_root *tree)
202 {
203 	struct module *mod;
204 
205 	list_for_each_entry_rcu(mod, &modules, list,
206 				lockdep_is_held(&module_mutex)) {
207 		if (within_module(addr, mod))
208 			return mod;
209 	}
210 
211 	return NULL;
212 }
213 #endif /* CONFIG_MODULES_TREE_LOOKUP */
214 
215 void module_enable_ro(const struct module *mod, bool after_init);
216 void module_enable_nx(const struct module *mod);
217 void module_enable_x(const struct module *mod);
218 int module_enforce_rwx_sections(Elf_Ehdr *hdr, Elf_Shdr *sechdrs,
219 				char *secstrings, struct module *mod);
220 
221 #ifdef CONFIG_MODULE_SIG
222 int module_sig_check(struct load_info *info, int flags);
223 #else /* !CONFIG_MODULE_SIG */
224 static inline int module_sig_check(struct load_info *info, int flags)
225 {
226 	return 0;
227 }
228 #endif /* !CONFIG_MODULE_SIG */
229 
230 #ifdef CONFIG_DEBUG_KMEMLEAK
231 void kmemleak_load_module(const struct module *mod, const struct load_info *info);
232 #else /* !CONFIG_DEBUG_KMEMLEAK */
233 static inline void kmemleak_load_module(const struct module *mod,
234 					const struct load_info *info) { }
235 #endif /* CONFIG_DEBUG_KMEMLEAK */
236 
237 #ifdef CONFIG_KALLSYMS
238 void init_build_id(struct module *mod, const struct load_info *info);
239 void layout_symtab(struct module *mod, struct load_info *info);
240 void add_kallsyms(struct module *mod, const struct load_info *info);
241 unsigned long find_kallsyms_symbol_value(struct module *mod, const char *name);
242 
243 static inline bool sect_empty(const Elf_Shdr *sect)
244 {
245 	return !(sect->sh_flags & SHF_ALLOC) || sect->sh_size == 0;
246 }
247 #else /* !CONFIG_KALLSYMS */
248 static inline void init_build_id(struct module *mod, const struct load_info *info) { }
249 static inline void layout_symtab(struct module *mod, struct load_info *info) { }
250 static inline void add_kallsyms(struct module *mod, const struct load_info *info) { }
251 #endif /* CONFIG_KALLSYMS */
252 
253 #ifdef CONFIG_SYSFS
254 int mod_sysfs_setup(struct module *mod, const struct load_info *info,
255 		    struct kernel_param *kparam, unsigned int num_params);
256 void mod_sysfs_teardown(struct module *mod);
257 void init_param_lock(struct module *mod);
258 #else /* !CONFIG_SYSFS */
259 static inline int mod_sysfs_setup(struct module *mod,
260 			   	  const struct load_info *info,
261 			   	  struct kernel_param *kparam,
262 			   	  unsigned int num_params)
263 {
264 	return 0;
265 }
266 
267 static inline void mod_sysfs_teardown(struct module *mod) { }
268 static inline void init_param_lock(struct module *mod) { }
269 #endif /* CONFIG_SYSFS */
270 
271 #ifdef CONFIG_MODVERSIONS
272 int check_version(const struct load_info *info,
273 		  const char *symname, struct module *mod, const s32 *crc);
274 void module_layout(struct module *mod, struct modversion_info *ver, struct kernel_param *kp,
275 		   struct kernel_symbol *ks, struct tracepoint * const *tp);
276 int check_modstruct_version(const struct load_info *info, struct module *mod);
277 int same_magic(const char *amagic, const char *bmagic, bool has_crcs);
278 #else /* !CONFIG_MODVERSIONS */
279 static inline int check_version(const struct load_info *info,
280 				const char *symname,
281 				struct module *mod,
282 				const s32 *crc)
283 {
284 	return 1;
285 }
286 
287 static inline int check_modstruct_version(const struct load_info *info,
288 					  struct module *mod)
289 {
290 	return 1;
291 }
292 
293 static inline int same_magic(const char *amagic, const char *bmagic, bool has_crcs)
294 {
295 	return strcmp(amagic, bmagic) == 0;
296 }
297 #endif /* CONFIG_MODVERSIONS */
298