| 1 |
|
| 2 | |
| 3 | *
|
| 4 | * Copyright (C) 2012 Red Hat, Inc. All Rights Reserved.
|
| 5 | * Written by David Howells ([email protected])
|
| 6 | * Copyright (C) 2023 Luis Chamberlain <[email protected]>
|
| 7 |
|
| 8 |
|
| 9 | #include <linux/elf.h>
|
| 10 | #include <linux/compiler.h>
|
| 11 | #include <linux/module.h>
|
| 12 | #include <linux/mutex.h>
|
| 13 | #include <linux/rculist.h>
|
| 14 | #include <linux/rcupdate.h>
|
| 15 | #include <linux/mm.h>
|
| 16 |
|
| 17 | #ifndef ARCH_SHF_SMALL
|
| 18 | #define ARCH_SHF_SMALL 0
|
| 19 | #endif
|
| 20 |
|
| 21 | |
| 22 | * Use highest 4 bits of sh_entsize to store the mod_mem_type of this
|
| 23 | * section. This leaves 28 bits for offset on 32-bit systems, which is
|
| 24 | * about 256 MiB (WARN_ON_ONCE if we exceed that).
|
| 25 |
|
| 26 |
|
| 27 | #define SH_ENTSIZE_TYPE_BITS 4
|
| 28 | #define SH_ENTSIZE_TYPE_SHIFT (BITS_PER_LONG - SH_ENTSIZE_TYPE_BITS)
|
| 29 | #define SH_ENTSIZE_TYPE_MASK ((1UL << SH_ENTSIZE_TYPE_BITS) - 1)
|
| 30 | #define SH_ENTSIZE_OFFSET_MASK ((1UL << (BITS_PER_LONG - SH_ENTSIZE_TYPE_BITS)) - 1)
|
| 31 |
|
| 32 |
|
| 33 | #define MODULE_FLAGS_BUF_SIZE (TAINT_FLAGS_COUNT + 4)
|
| 34 |
|
| 35 | struct kernel_symbol {
|
| 36 | #ifdef CONFIG_HAVE_ARCH_PREL32_RELOCATIONS
|
| 37 | int value_offset;
|
| 38 | int name_offset;
|
| 39 | int namespace_offset;
|
| 40 | #else
|
| 41 | unsigned long value;
|
| 42 | const char *name;
|
| 43 | const char *namespace;
|
| 44 | #endif
|
| 45 | };
|
| 46 |
|
| 47 | extern struct mutex module_mutex;
|
| 48 | extern struct list_head modules;
|
| 49 |
|
| 50 | extern const struct module_attribute *const modinfo_attrs[];
|
| 51 | extern const size_t modinfo_attrs_count;
|
| 52 |
|
| 53 |
|
| 54 | extern const struct kernel_symbol __start___ksymtab[];
|
| 55 | extern const struct kernel_symbol __stop___ksymtab[];
|
| 56 | extern const u32 __start___kcrctab[];
|
| 57 | extern const u8 __start___kflagstab[];
|
| 58 |
|
| 59 | #define KMOD_PATH_LEN 256
|
| 60 | extern char modprobe_path[];
|
| 61 |
|
| 62 | struct load_info {
|
| 63 | const char *name;
|
| 64 |
|
| 65 | struct module *mod;
|
| 66 | Elf_Ehdr *hdr;
|
| 67 | unsigned long len;
|
| 68 | Elf_Shdr *sechdrs;
|
| 69 | char *secstrings, *strtab;
|
| 70 | unsigned long symoffs, stroffs, init_typeoffs, core_typeoffs;
|
| 71 | bool sig_ok;
|
| 72 | #ifdef CONFIG_KALLSYMS
|
| 73 | unsigned long mod_kallsyms_init_off;
|
| 74 | #endif
|
| 75 | #ifdef CONFIG_MODULE_DECOMPRESS
|
| 76 | #ifdef CONFIG_MODULE_STATS
|
| 77 | unsigned long compressed_len;
|
| 78 | #endif
|
| 79 | struct page **pages;
|
| 80 | unsigned int max_pages;
|
| 81 | unsigned int used_pages;
|
| 82 | #endif
|
| 83 | struct {
|
| 84 | unsigned int sym;
|
| 85 | unsigned int str;
|
| 86 | unsigned int mod;
|
| 87 | unsigned int vers;
|
| 88 | unsigned int info;
|
| 89 | unsigned int pcpu;
|
| 90 | unsigned int vers_ext_crc;
|
| 91 | unsigned int vers_ext_name;
|
| 92 | } index;
|
| 93 | };
|
| 94 |
|
| 95 | enum mod_license {
|
| 96 | NOT_GPL_ONLY,
|
| 97 | GPL_ONLY,
|
| 98 | };
|
| 99 |
|
| 100 | struct find_symbol_arg {
|
| 101 |
|
| 102 | const char *name;
|
| 103 | bool gplok;
|
| 104 | bool warn;
|
| 105 |
|
| 106 |
|
| 107 | struct module *owner;
|
| 108 | const u32 *crc;
|
| 109 | const struct kernel_symbol *sym;
|
| 110 | enum mod_license license;
|
| 111 | };
|
| 112 |
|
| 113 |
|
| 114 | struct module_use {
|
| 115 | struct list_head source_list;
|
| 116 | struct list_head target_list;
|
| 117 | struct module *source, *target;
|
| 118 | };
|
| 119 |
|
| 120 | int mod_verify_sig(const void *mod, struct load_info *info);
|
| 121 | int try_to_force_load(struct module *mod, const char *reason);
|
| 122 | bool find_symbol(struct find_symbol_arg *fsa);
|
| 123 | struct module *find_module_all(const char *name, size_t len, bool even_unformed);
|
| 124 | int cmp_name(const void *name, const void *sym);
|
| 125 | long module_get_offset_and_type(struct module *mod, enum mod_mem_type type,
|
| 126 | Elf_Shdr *sechdr, unsigned int section);
|
| 127 | char *module_flags(struct module *mod, char *buf, bool show_state);
|
| 128 | size_t module_flags_taint(unsigned long taints, char *buf);
|
| 129 |
|
| 130 | char *module_next_tag_pair(char *string, unsigned long *secsize);
|
| 131 |
|
| 132 | #define for_each_modinfo_entry(entry, info, name) \
|
| 133 | for (entry = get_modinfo(info, name); entry; entry = get_next_modinfo(info, name, entry))
|
| 134 |
|
| 135 | static inline unsigned long kernel_symbol_value(const struct kernel_symbol *sym)
|
| 136 | {
|
| 137 | #ifdef CONFIG_HAVE_ARCH_PREL32_RELOCATIONS
|
| 138 | return (unsigned long)offset_to_ptr(&sym->value_offset);
|
| 139 | #else
|
| 140 | return sym->value;
|
| 141 | #endif
|
| 142 | }
|
| 143 |
|
| 144 | #ifdef CONFIG_LIVEPATCH
|
| 145 | int copy_module_elf(struct module *mod, struct load_info *info);
|
| 146 | void free_module_elf(struct module *mod);
|
| 147 | #else
|
| 148 | static inline int copy_module_elf(struct module *mod, struct load_info *info)
|
| 149 | {
|
| 150 | return 0;
|
| 151 | }
|
| 152 |
|
| 153 | static inline void free_module_elf(struct module *mod) { }
|
| 154 | #endif
|
| 155 |
|
| 156 | static inline bool set_livepatch_module(struct module *mod)
|
| 157 | {
|
| 158 | #ifdef CONFIG_LIVEPATCH
|
| 159 | mod->klp = true;
|
| 160 | return true;
|
| 161 | #else
|
| 162 | return false;
|
| 163 | #endif
|
| 164 | }
|
| 165 |
|
| 166 | |
| 167 | * enum fail_dup_mod_reason - state at which a duplicate module was detected
|
| 168 | *
|
| 169 | * @FAIL_DUP_MOD_BECOMING: the module is read properly, passes all checks but
|
| 170 | * we've determined that another module with the same name is already loaded
|
| 171 | * or being processed on our &modules list. This happens on early_mod_check()
|
| 172 | * right before layout_and_allocate(). The kernel would have already
|
| 173 | * vmalloc()'d space for the entire module through finit_module(). If
|
| 174 | * decompression was used two vmap() spaces were used. These failures can
|
| 175 | * happen when userspace has not seen the module present on the kernel and
|
| 176 | * tries to load the module multiple times at same time.
|
| 177 | * @FAIL_DUP_MOD_LOAD: the module has been read properly, passes all validation
|
| 178 | * checks and the kernel determines that the module was unique and because
|
| 179 | * of this allocated yet another private kernel copy of the module space in
|
| 180 | * layout_and_allocate() but after this determined in add_unformed_module()
|
| 181 | * that another module with the same name is already loaded or being processed.
|
| 182 | * These failures should be mitigated as much as possible and are indicative
|
| 183 | * of really fast races in loading modules. Without module decompression
|
| 184 | * they waste twice as much vmap space. With module decompression three
|
| 185 | * times the module's size vmap space is wasted.
|
| 186 |
|
| 187 | enum fail_dup_mod_reason {
|
| 188 | FAIL_DUP_MOD_BECOMING = 0,
|
| 189 | FAIL_DUP_MOD_LOAD,
|
| 190 | };
|
| 191 |
|
| 192 | #ifdef CONFIG_MODULE_DEBUGFS
|
| 193 | extern struct dentry *mod_debugfs_root;
|
| 194 | #endif
|
| 195 |
|
| 196 | #ifdef CONFIG_MODULE_STATS
|
| 197 |
|
| 198 | #define mod_stat_add_long(count, var) atomic_long_add(count, var)
|
| 199 | #define mod_stat_inc(name) atomic_inc(name)
|
| 200 |
|
| 201 | extern atomic_long_t total_mod_size;
|
| 202 | extern atomic_long_t total_text_size;
|
| 203 | extern atomic_long_t invalid_kread_bytes;
|
| 204 | extern atomic_long_t invalid_decompress_bytes;
|
| 205 |
|
| 206 | extern atomic_t modcount;
|
| 207 | extern atomic_t failed_kreads;
|
| 208 | extern atomic_t failed_decompress;
|
| 209 | struct mod_fail_load {
|
| 210 | struct list_head list;
|
| 211 | char name[MODULE_NAME_LEN];
|
| 212 | atomic_long_t count;
|
| 213 | unsigned long dup_fail_mask;
|
| 214 | };
|
| 215 |
|
| 216 | int try_add_failed_module(const char *name, enum fail_dup_mod_reason reason);
|
| 217 | void mod_stat_bump_invalid(struct load_info *info, int flags);
|
| 218 | void mod_stat_bump_becoming(struct load_info *info, int flags);
|
| 219 |
|
| 220 | #else
|
| 221 |
|
| 222 | #define mod_stat_add_long(name, var)
|
| 223 | #define mod_stat_inc(name)
|
| 224 |
|
| 225 | static inline int try_add_failed_module(const char *name,
|
| 226 | enum fail_dup_mod_reason reason)
|
| 227 | {
|
| 228 | return 0;
|
| 229 | }
|
| 230 |
|
| 231 | static inline void mod_stat_bump_invalid(struct load_info *info, int flags)
|
| 232 | {
|
| 233 | }
|
| 234 |
|
| 235 | static inline void mod_stat_bump_becoming(struct load_info *info, int flags)
|
| 236 | {
|
| 237 | }
|
| 238 |
|
| 239 | #endif
|
| 240 |
|
| 241 | #ifdef CONFIG_MODULE_DEBUG_AUTOLOAD_DUPS
|
| 242 | bool kmod_dup_request_exists_wait(char *module_name, bool wait, int *dup_ret);
|
| 243 | void kmod_dup_request_announce(char *module_name, int ret);
|
| 244 | #else
|
| 245 | static inline bool kmod_dup_request_exists_wait(char *module_name, bool wait, int *dup_ret)
|
| 246 | {
|
| 247 | return false;
|
| 248 | }
|
| 249 |
|
| 250 | static inline void kmod_dup_request_announce(char *module_name, int ret)
|
| 251 | {
|
| 252 | }
|
| 253 | #endif
|
| 254 |
|
| 255 | #ifdef CONFIG_MODULE_UNLOAD_TAINT_TRACKING
|
| 256 | struct mod_unload_taint {
|
| 257 | struct list_head list;
|
| 258 | char name[MODULE_NAME_LEN];
|
| 259 | unsigned long taints;
|
| 260 | u64 count;
|
| 261 | };
|
| 262 |
|
| 263 | int try_add_tainted_module(struct module *mod);
|
| 264 | void print_unloaded_tainted_modules(void);
|
| 265 | #else
|
| 266 | static inline int try_add_tainted_module(struct module *mod)
|
| 267 | {
|
| 268 | return 0;
|
| 269 | }
|
| 270 |
|
| 271 | static inline void print_unloaded_tainted_modules(void)
|
| 272 | {
|
| 273 | }
|
| 274 | #endif
|
| 275 |
|
| 276 | #ifdef CONFIG_MODULE_DECOMPRESS
|
| 277 | int module_decompress(struct load_info *info, const void *buf, size_t size);
|
| 278 | void module_decompress_cleanup(struct load_info *info);
|
| 279 | #else
|
| 280 | static inline int module_decompress(struct load_info *info,
|
| 281 | const void *buf, size_t size)
|
| 282 | {
|
| 283 | return -EOPNOTSUPP;
|
| 284 | }
|
| 285 |
|
| 286 | static inline void module_decompress_cleanup(struct load_info *info)
|
| 287 | {
|
| 288 | }
|
| 289 | #endif
|
| 290 |
|
| 291 | struct mod_tree_root {
|
| 292 | #ifdef CONFIG_MODULES_TREE_LOOKUP
|
| 293 | struct latch_tree_root root;
|
| 294 | #endif
|
| 295 | unsigned long addr_min;
|
| 296 | unsigned long addr_max;
|
| 297 | #ifdef CONFIG_ARCH_WANTS_MODULES_DATA_IN_VMALLOC
|
| 298 | unsigned long data_addr_min;
|
| 299 | unsigned long data_addr_max;
|
| 300 | #endif
|
| 301 | };
|
| 302 |
|
| 303 | extern struct mod_tree_root mod_tree;
|
| 304 |
|
| 305 | #ifdef CONFIG_MODULES_TREE_LOOKUP
|
| 306 | void mod_tree_insert(struct module *mod);
|
| 307 | void mod_tree_remove_init(struct module *mod);
|
| 308 | void mod_tree_remove(struct module *mod);
|
| 309 | struct module *mod_find(unsigned long addr, struct mod_tree_root *tree);
|
| 310 | #else
|
| 311 |
|
| 312 | static inline void mod_tree_insert(struct module *mod) { }
|
| 313 | static inline void mod_tree_remove_init(struct module *mod) { }
|
| 314 | static inline void mod_tree_remove(struct module *mod) { }
|
| 315 | static inline struct module *mod_find(unsigned long addr, struct mod_tree_root *tree)
|
| 316 | {
|
| 317 | struct module *mod;
|
| 318 |
|
| 319 | list_for_each_entry_rcu(mod, &modules, list,
|
| 320 | lockdep_is_held(&module_mutex)) {
|
| 321 | if (within_module(addr, mod))
|
| 322 | return mod;
|
| 323 | }
|
| 324 |
|
| 325 | return NULL;
|
| 326 | }
|
| 327 | #endif
|
| 328 |
|
| 329 | int module_enable_rodata_ro(const struct module *mod);
|
| 330 | int module_enable_rodata_ro_after_init(const struct module *mod);
|
| 331 | int module_enable_data_nx(const struct module *mod);
|
| 332 | int module_enable_text_rox(const struct module *mod);
|
| 333 | int module_enforce_rwx_sections(const Elf_Ehdr *hdr, const Elf_Shdr *sechdrs,
|
| 334 | const char *secstrings,
|
| 335 | const struct module *mod);
|
| 336 | void module_mark_ro_after_init(const Elf_Ehdr *hdr, Elf_Shdr *sechdrs,
|
| 337 | const char *secstrings);
|
| 338 |
|
| 339 | #ifdef CONFIG_MODULE_SIG
|
| 340 | int module_sig_check(struct load_info *info, int flags);
|
| 341 | #else
|
| 342 | static inline int module_sig_check(struct load_info *info, int flags)
|
| 343 | {
|
| 344 | return 0;
|
| 345 | }
|
| 346 | #endif
|
| 347 |
|
| 348 | #ifdef CONFIG_DEBUG_KMEMLEAK
|
| 349 | void kmemleak_load_module(const struct module *mod, const struct load_info *info);
|
| 350 | #else
|
| 351 | static inline void kmemleak_load_module(const struct module *mod,
|
| 352 | const struct load_info *info) { }
|
| 353 | #endif
|
| 354 |
|
| 355 | #ifdef CONFIG_KALLSYMS
|
| 356 | void init_build_id(struct module *mod, const struct load_info *info);
|
| 357 | void layout_symtab(struct module *mod, struct load_info *info);
|
| 358 | void add_kallsyms(struct module *mod, const struct load_info *info);
|
| 359 |
|
| 360 | static inline bool sect_empty(const Elf_Shdr *sect)
|
| 361 | {
|
| 362 | return !(sect->sh_flags & SHF_ALLOC) || sect->sh_size == 0;
|
| 363 | }
|
| 364 | #else
|
| 365 | static inline void init_build_id(struct module *mod, const struct load_info *info) { }
|
| 366 | static inline void layout_symtab(struct module *mod, struct load_info *info) { }
|
| 367 | static inline void add_kallsyms(struct module *mod, const struct load_info *info) { }
|
| 368 | #endif
|
| 369 |
|
| 370 | #ifdef CONFIG_SYSFS
|
| 371 | int mod_sysfs_setup(struct module *mod, const struct load_info *info,
|
| 372 | struct kernel_param *kparam, unsigned int num_params);
|
| 373 | void mod_sysfs_teardown(struct module *mod);
|
| 374 | void init_param_lock(struct module *mod);
|
| 375 | #else
|
| 376 | static inline int mod_sysfs_setup(struct module *mod,
|
| 377 | const struct load_info *info,
|
| 378 | struct kernel_param *kparam,
|
| 379 | unsigned int num_params)
|
| 380 | {
|
| 381 | return 0;
|
| 382 | }
|
| 383 |
|
| 384 | static inline void mod_sysfs_teardown(struct module *mod) { }
|
| 385 | static inline void init_param_lock(struct module *mod) { }
|
| 386 | #endif
|
| 387 |
|
| 388 | #ifdef CONFIG_MODVERSIONS
|
| 389 | int check_version(const struct load_info *info,
|
| 390 | const char *symname, struct module *mod, const u32 *crc);
|
| 391 | void module_layout(struct module *mod, struct modversion_info *ver, struct kernel_param *kp,
|
| 392 | struct kernel_symbol *ks, struct tracepoint * const *tp);
|
| 393 | int check_modstruct_version(const struct load_info *info, struct module *mod);
|
| 394 | int same_magic(const char *amagic, const char *bmagic, bool has_crcs);
|
| 395 | struct modversion_info_ext {
|
| 396 | size_t remaining;
|
| 397 | const u32 *crc;
|
| 398 | const char *name;
|
| 399 | };
|
| 400 | void modversion_ext_start(const struct load_info *info, struct modversion_info_ext *ver);
|
| 401 | void modversion_ext_advance(struct modversion_info_ext *ver);
|
| 402 | #define for_each_modversion_info_ext(ver, info) \
|
| 403 | for (modversion_ext_start(info, &ver); ver.remaining > 0; modversion_ext_advance(&ver))
|
| 404 | #else
|
| 405 | static inline int check_version(const struct load_info *info,
|
| 406 | const char *symname,
|
| 407 | struct module *mod,
|
| 408 | const u32 *crc)
|
| 409 | {
|
| 410 | return 1;
|
| 411 | }
|
| 412 |
|
| 413 | static inline int check_modstruct_version(const struct load_info *info,
|
| 414 | struct module *mod)
|
| 415 | {
|
| 416 | return 1;
|
| 417 | }
|
| 418 |
|
| 419 | static inline int same_magic(const char *amagic, const char *bmagic, bool has_crcs)
|
| 420 | {
|
| 421 | return strcmp(amagic, bmagic) == 0;
|
| 422 | }
|
| 423 | #endif
|