xref: /linux-6.15/kernel/module/internal.h (revision 01dc0386)
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 
14 #ifndef ARCH_SHF_SMALL
15 #define ARCH_SHF_SMALL 0
16 #endif
17 
18 /* If this is set, the section belongs in the init part of the module */
19 #define INIT_OFFSET_MASK (1UL << (BITS_PER_LONG - 1))
20 /* Maximum number of characters written by module_flags() */
21 #define MODULE_FLAGS_BUF_SIZE (TAINT_FLAGS_COUNT + 4)
22 
23 #ifndef CONFIG_ARCH_WANTS_MODULES_DATA_IN_VMALLOC
24 #define	data_layout core_layout
25 #endif
26 
27 /*
28  * Modules' sections will be aligned on page boundaries
29  * to ensure complete separation of code and data, but
30  * only when CONFIG_STRICT_MODULE_RWX=y
31  */
32 #ifdef CONFIG_STRICT_MODULE_RWX
33 # define strict_align(X) PAGE_ALIGN(X)
34 #else
35 # define strict_align(X) (X)
36 #endif
37 
38 extern struct mutex module_mutex;
39 extern struct list_head modules;
40 
41 extern struct module_attribute *modinfo_attrs[];
42 extern size_t modinfo_attrs_count;
43 
44 /* Provided by the linker */
45 extern const struct kernel_symbol __start___ksymtab[];
46 extern const struct kernel_symbol __stop___ksymtab[];
47 extern const struct kernel_symbol __start___ksymtab_gpl[];
48 extern const struct kernel_symbol __stop___ksymtab_gpl[];
49 extern const s32 __start___kcrctab[];
50 extern const s32 __start___kcrctab_gpl[];
51 
52 struct load_info {
53 	const char *name;
54 	/* pointer to module in temporary copy, freed at end of load_module() */
55 	struct module *mod;
56 	Elf_Ehdr *hdr;
57 	unsigned long len;
58 	Elf_Shdr *sechdrs;
59 	char *secstrings, *strtab;
60 	unsigned long symoffs, stroffs, init_typeoffs, core_typeoffs;
61 	struct _ddebug *debug;
62 	unsigned int num_debug;
63 	bool sig_ok;
64 #ifdef CONFIG_KALLSYMS
65 	unsigned long mod_kallsyms_init_off;
66 #endif
67 #ifdef CONFIG_MODULE_DECOMPRESS
68 	struct page **pages;
69 	unsigned int max_pages;
70 	unsigned int used_pages;
71 #endif
72 	struct {
73 		unsigned int sym, str, mod, vers, info, pcpu;
74 	} index;
75 };
76 
77 enum mod_license {
78 	NOT_GPL_ONLY,
79 	GPL_ONLY,
80 };
81 
82 struct find_symbol_arg {
83 	/* Input */
84 	const char *name;
85 	bool gplok;
86 	bool warn;
87 
88 	/* Output */
89 	struct module *owner;
90 	const s32 *crc;
91 	const struct kernel_symbol *sym;
92 	enum mod_license license;
93 };
94 
95 int mod_verify_sig(const void *mod, struct load_info *info);
96 int try_to_force_load(struct module *mod, const char *reason);
97 bool find_symbol(struct find_symbol_arg *fsa);
98 struct module *find_module_all(const char *name, size_t len, bool even_unformed);
99 int cmp_name(const void *name, const void *sym);
100 long module_get_offset(struct module *mod, unsigned int *size, Elf_Shdr *sechdr,
101 		       unsigned int section);
102 char *module_flags(struct module *mod, char *buf);
103 
104 static inline unsigned long kernel_symbol_value(const struct kernel_symbol *sym)
105 {
106 #ifdef CONFIG_HAVE_ARCH_PREL32_RELOCATIONS
107 	return (unsigned long)offset_to_ptr(&sym->value_offset);
108 #else
109 	return sym->value;
110 #endif
111 }
112 
113 #ifdef CONFIG_LIVEPATCH
114 int copy_module_elf(struct module *mod, struct load_info *info);
115 void free_module_elf(struct module *mod);
116 #else /* !CONFIG_LIVEPATCH */
117 static inline int copy_module_elf(struct module *mod, struct load_info *info)
118 {
119 	return 0;
120 }
121 
122 static inline void free_module_elf(struct module *mod) { }
123 #endif /* CONFIG_LIVEPATCH */
124 
125 static inline bool set_livepatch_module(struct module *mod)
126 {
127 #ifdef CONFIG_LIVEPATCH
128 	mod->klp = true;
129 	return true;
130 #else
131 	return false;
132 #endif
133 }
134 
135 #ifdef CONFIG_MODULE_DECOMPRESS
136 int module_decompress(struct load_info *info, const void *buf, size_t size);
137 void module_decompress_cleanup(struct load_info *info);
138 #else
139 static inline int module_decompress(struct load_info *info,
140 				    const void *buf, size_t size)
141 {
142 	return -EOPNOTSUPP;
143 }
144 
145 static inline void module_decompress_cleanup(struct load_info *info)
146 {
147 }
148 #endif
149 
150 struct mod_tree_root {
151 #ifdef CONFIG_MODULES_TREE_LOOKUP
152 	struct latch_tree_root root;
153 #endif
154 	unsigned long addr_min;
155 	unsigned long addr_max;
156 };
157 
158 extern struct mod_tree_root mod_tree;
159 extern struct mod_tree_root mod_data_tree;
160 
161 #ifdef CONFIG_MODULES_TREE_LOOKUP
162 void mod_tree_insert(struct module *mod);
163 void mod_tree_remove_init(struct module *mod);
164 void mod_tree_remove(struct module *mod);
165 struct module *mod_find(unsigned long addr, struct mod_tree_root *tree);
166 #else /* !CONFIG_MODULES_TREE_LOOKUP */
167 
168 static inline void mod_tree_insert(struct module *mod) { }
169 static inline void mod_tree_remove_init(struct module *mod) { }
170 static inline void mod_tree_remove(struct module *mod) { }
171 static inline struct module *mod_find(unsigned long addr, struct mod_tree_root *tree)
172 {
173 	struct module *mod;
174 
175 	list_for_each_entry_rcu(mod, &modules, list,
176 				lockdep_is_held(&module_mutex)) {
177 		if (within_module(addr, mod))
178 			return mod;
179 	}
180 
181 	return NULL;
182 }
183 #endif /* CONFIG_MODULES_TREE_LOOKUP */
184 
185 void module_enable_ro(const struct module *mod, bool after_init);
186 void module_enable_nx(const struct module *mod);
187 void module_enable_x(const struct module *mod);
188 int module_enforce_rwx_sections(Elf_Ehdr *hdr, Elf_Shdr *sechdrs,
189 				char *secstrings, struct module *mod);
190 bool module_check_misalignment(const struct module *mod);
191 
192 #ifdef CONFIG_MODULE_SIG
193 int module_sig_check(struct load_info *info, int flags);
194 #else /* !CONFIG_MODULE_SIG */
195 static inline int module_sig_check(struct load_info *info, int flags)
196 {
197 	return 0;
198 }
199 #endif /* !CONFIG_MODULE_SIG */
200 
201 #ifdef CONFIG_DEBUG_KMEMLEAK
202 void kmemleak_load_module(const struct module *mod, const struct load_info *info);
203 #else /* !CONFIG_DEBUG_KMEMLEAK */
204 static inline void kmemleak_load_module(const struct module *mod,
205 					const struct load_info *info) { }
206 #endif /* CONFIG_DEBUG_KMEMLEAK */
207 
208 #ifdef CONFIG_KALLSYMS
209 void init_build_id(struct module *mod, const struct load_info *info);
210 void layout_symtab(struct module *mod, struct load_info *info);
211 void add_kallsyms(struct module *mod, const struct load_info *info);
212 unsigned long find_kallsyms_symbol_value(struct module *mod, const char *name);
213 
214 static inline bool sect_empty(const Elf_Shdr *sect)
215 {
216 	return !(sect->sh_flags & SHF_ALLOC) || sect->sh_size == 0;
217 }
218 #else /* !CONFIG_KALLSYMS */
219 static inline void init_build_id(struct module *mod, const struct load_info *info) { }
220 static inline void layout_symtab(struct module *mod, struct load_info *info) { }
221 static inline void add_kallsyms(struct module *mod, const struct load_info *info) { }
222 #endif /* CONFIG_KALLSYMS */
223 
224 #ifdef CONFIG_SYSFS
225 int mod_sysfs_setup(struct module *mod, const struct load_info *info,
226 		    struct kernel_param *kparam, unsigned int num_params);
227 void mod_sysfs_teardown(struct module *mod);
228 void init_param_lock(struct module *mod);
229 #else /* !CONFIG_SYSFS */
230 static inline int mod_sysfs_setup(struct module *mod,
231 			   	  const struct load_info *info,
232 			   	  struct kernel_param *kparam,
233 			   	  unsigned int num_params)
234 {
235 	return 0;
236 }
237 
238 static inline void mod_sysfs_teardown(struct module *mod) { }
239 static inline void init_param_lock(struct module *mod) { }
240 #endif /* CONFIG_SYSFS */
241 
242 #ifdef CONFIG_MODVERSIONS
243 int check_version(const struct load_info *info,
244 		  const char *symname, struct module *mod, const s32 *crc);
245 void module_layout(struct module *mod, struct modversion_info *ver, struct kernel_param *kp,
246 		   struct kernel_symbol *ks, struct tracepoint * const *tp);
247 int check_modstruct_version(const struct load_info *info, struct module *mod);
248 int same_magic(const char *amagic, const char *bmagic, bool has_crcs);
249 #else /* !CONFIG_MODVERSIONS */
250 static inline int check_version(const struct load_info *info,
251 				const char *symname,
252 				struct module *mod,
253 				const s32 *crc)
254 {
255 	return 1;
256 }
257 
258 static inline int check_modstruct_version(const struct load_info *info,
259 					  struct module *mod)
260 {
261 	return 1;
262 }
263 
264 static inline int same_magic(const char *amagic, const char *bmagic, bool has_crcs)
265 {
266 	return strcmp(amagic, bmagic) == 0;
267 }
268 #endif /* CONFIG_MODVERSIONS */
269