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