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