| @@ -14,16 +14,12 @@ · 13 unmodified lines |
| 14 | #include <linux/init.h> | 14 | #include <linux/init.h> |
| 15 | #include <linux/kallsyms.h> | 15 | #include <linux/kallsyms.h> |
| 16 | #include <linux/buildid.h> | 16 | #include <linux/buildid.h> |
| 17 | #include <linux/file.h> | | |
| 18 | #include <linux/fs.h> | 17 | #include <linux/fs.h> |
| 19 | #include <linux/sysfs.h> | | |
| 20 | #include <linux/kernel.h> | 18 | #include <linux/kernel.h> |
| 21 | #include <linux/kernel_read_file.h> | 19 | #include <linux/kernel_read_file.h> |
| 22 | #include <linux/slab.h> | 20 | #include <linux/slab.h> |
| 23 | #include <linux/vmalloc.h> | 21 | #include <linux/vmalloc.h> |
| 24 | #include <linux/elf.h> | 22 | #include <linux/elf.h> |
| 25 | #include <linux/proc_fs.h> | | |
| 26 | #include <linux/security.h> | | |
| 27 | #include <linux/seq_file.h> | 23 | #include <linux/seq_file.h> |
| 28 | #include <linux/syscalls.h> | 24 | #include <linux/syscalls.h> |
| 29 | #include <linux/fcntl.h> | 25 | #include <linux/fcntl.h> |
| @@ -58,236 +54,69 @@ · 28 unmodified lines |
| 58 | #include <linux/dynamic_debug.h> | 54 | #include <linux/dynamic_debug.h> |
| 59 | #include <linux/audit.h> | 55 | #include <linux/audit.h> |
| 60 | #include <uapi/linux/module.h> | 56 | #include <uapi/linux/module.h> |
| 61 | #include "module-internal.h" | 57 | #include "internal.h" |
| 62 | | 58 | |
| 63 | #define CREATE_TRACE_POINTS | 59 | #define CREATE_TRACE_POINTS |
| 64 | #include <trace/events/module.h> | 60 | #include <trace/events/module.h> |
| 65 | | 61 | |
| 66 | #ifndef ARCH_SHF_SMALL | | |
| 67 | #define ARCH_SHF_SMALL 0 | | |
| 68 | #endif | | |
| 69 | | | |
| 70 | /* | | |
| 71 | * Modules' sections will be aligned on page boundaries | | |
| 72 | * to ensure complete separation of code and data, but | | |
| 73 | * only when CONFIG_ARCH_HAS_STRICT_MODULE_RWX=y | | |
| 74 | */ | | |
| 75 | #ifdef CONFIG_ARCH_HAS_STRICT_MODULE_RWX | | |
| 76 | # define debug_align(X) ALIGN(X, PAGE_SIZE) | | |
| 77 | #else | | |
| 78 | # define debug_align(X) (X) | | |
| 79 | #endif | | |
| 80 | | | |
| 81 | /* If this is set, the section belongs in the init part of the module */ | | |
| 82 | #define INIT_OFFSET_MASK (1UL << (BITS_PER_LONG-1)) | | |
| 83 | | | |
| 84 | /* | 62 | /* |
| 85 | * Mutex protects: | 63 | * Mutex protects: |
| 86 | * 1) List of modules (also safely readable with preempt_disable), | 64 | * 1) List of modules (also safely readable with preempt_disable), |
| 87 | * 2) module_use links, | 65 | * 2) module_use links, |
| 88 | * 3) module_addr_min/module_addr_max. | 66 | * 3) mod_tree.addr_min/mod_tree.addr_max. |
| 89 | * (delete and add uses RCU list operations). | 67 | * (delete and add uses RCU list operations). |
| 90 | */ | 68 | */ |
| 91 | static DEFINE_MUTEX(module_mutex); | 69 | DEFINE_MUTEX(module_mutex); |
| 92 | static LIST_HEAD(modules); | 70 | LIST_HEAD(modules); |
| 93 | | 71 | |
| 94 | /* Work queue for freeing init sections in success case */ | 72 | /* Work queue for freeing init sections in success case */ |
| 95 | static void do_free_init(struct work_struct *w); | 73 | static void do_free_init(struct work_struct *w); |
| 96 | static DECLARE_WORK(init_free_wq, do_free_init); | 74 | static DECLARE_WORK(init_free_wq, do_free_init); |
| 97 | static LLIST_HEAD(init_free_list); | 75 | static LLIST_HEAD(init_free_list); |
| 98 | | 76 | |
| 99 | #ifdef CONFIG_MODULES_TREE_LOOKUP | 77 | struct mod_tree_root mod_tree __cacheline_aligned = { |
| 100 | | 78 | .addr_min = -1UL, |
| 101 | /* | | |
| 102 | * Use a latched RB-tree for __module_address(); this allows us to use | | |
| 103 | * RCU-sched lookups of the address from any context. | | |
| 104 | * | | |
| 105 | * This is conditional on PERF_EVENTS || TRACING because those can really hit | | |
| 106 | * __module_address() hard by doing a lot of stack unwinding; potentially from | | |
| 107 | * NMI context. | | |
| 108 | */ | | |
| 109 | | | |
| 110 | static __always_inline unsigned long __mod_tree_val(struct latch_tree_node *n) | | |
| 111 | { | | |
| 112 | struct module_layout *layout = container_of(n, struct module_layout, mtn.node); | | |
| 113 | | | |
| 114 | return (unsigned long)layout->base; | | |
| 115 | } | | |
| 116 | | | |
| 117 | static __always_inline unsigned long __mod_tree_size(struct latch_tree_node *n) | | |
| 118 | { | | |
| 119 | struct module_layout *layout = container_of(n, struct module_layout, mtn.node); | | |
| 120 | | | |
| 121 | return (unsigned long)layout->size; | | |
| 122 | } | | |
| 123 | | | |
| 124 | static __always_inline bool | | |
| 125 | mod_tree_less(struct latch_tree_node *a, struct latch_tree_node *b) | | |
| 126 | { | | |
| 127 | return __mod_tree_val(a) < __mod_tree_val(b); | | |
| 128 | } | | |
| 129 | | | |
| 130 | static __always_inline int | | |
| 131 | mod_tree_comp(void *key, struct latch_tree_node *n) | | |
| 132 | { | | |
| 133 | unsigned long val = (unsigned long)key; | | |
| 134 | unsigned long start, end; | | |
| 135 | | | |
| 136 | start = __mod_tree_val(n); | | |
| 137 | if (val < start) | | |
| 138 | return -1; | | |
| 139 | | | |
| 140 | end = start + __mod_tree_size(n); | | |
| 141 | if (val >= end) | | |
| 142 | return 1; | | |
| 143 | | | |
| 144 | return 0; | | |
| 145 | } | | |
| 146 | | | |
| 147 | static const struct latch_tree_ops mod_tree_ops = { | | |
| 148 | .less = mod_tree_less, | | |
| 149 | .comp = mod_tree_comp, | | |
| 150 | }; | 79 | }; |
| 151 | | 80 | |
| 152 | static struct mod_tree_root { | 81 | #ifdef CONFIG_ARCH_WANTS_MODULES_DATA_IN_VMALLOC |
| 153 | struct latch_tree_root root; | 82 | struct mod_tree_root mod_data_tree __cacheline_aligned = { |
| 154 | unsigned long addr_min; | | |
| 155 | unsigned long addr_max; | | |
| 156 | } mod_tree __cacheline_aligned = { | | |
| 157 | .addr_min = -1UL, | 83 | .addr_min = -1UL, |
| 158 | }; | 84 | }; |
| | 85 | #endif |
| 159 | | 86 | |
| 160 | #define module_addr_min mod_tree.addr_min | 87 | #define module_addr_min mod_tree.addr_min |
| 161 | #define module_addr_max mod_tree.addr_max | 88 | #define module_addr_max mod_tree.addr_max |
| 162 | | 89 | |
| 163 | static noinline void __mod_tree_insert(struct mod_tree_node *node) | 90 | struct symsearch { |
| 164 | { | 91 | const struct kernel_symbol *start, *stop; |
| 165 | latch_tree_insert(&node->node, &mod_tree.root, &mod_tree_ops); | 92 | const s32 *crcs; |
| 166 | } | 93 | enum mod_license license; |
| 167 | | 94 | }; |
| 168 | static void __mod_tree_remove(struct mod_tree_node *node) | | |
| 169 | { | | |
| 170 | latch_tree_erase(&node->node, &mod_tree.root, &mod_tree_ops); | | |
| 171 | } | | |
| 172 | | | |
| 173 | /* | | |
| 174 | * These modifications: insert, remove_init and remove; are serialized by the | | |
| 175 | * module_mutex. | | |
| 176 | */ | | |
| 177 | static void mod_tree_insert(struct module *mod) | | |
| 178 | { | | |
| 179 | mod->core_layout.mtn.mod = mod; | | |
| 180 | mod->init_layout.mtn.mod = mod; | | |
| 181 | | | |
| 182 | __mod_tree_insert(&mod->core_layout.mtn); | | |
| 183 | if (mod->init_layout.size) | | |
| 184 | __mod_tree_insert(&mod->init_layout.mtn); | | |
| 185 | } | | |
| 186 | | | |
| 187 | static void mod_tree_remove_init(struct module *mod) | | |
| 188 | { | | |
| 189 | if (mod->init_layout.size) | | |
| 190 | __mod_tree_remove(&mod->init_layout.mtn); | | |
| 191 | } | | |
| 192 | | | |
| 193 | static void mod_tree_remove(struct module *mod) | | |
| 194 | { | | |
| 195 | __mod_tree_remove(&mod->core_layout.mtn); | | |
| 196 | mod_tree_remove_init(mod); | | |
| 197 | } | | |
| 198 | | | |
| 199 | static struct module *mod_find(unsigned long addr) | | |
| 200 | { | | |
| 201 | struct latch_tree_node *ltn; | | |
| 202 | | | |
| 203 | ltn = latch_tree_find((void *)addr, &mod_tree.root, &mod_tree_ops); | | |
| 204 | if (!ltn) | | |
| 205 | return NULL; | | |
| 206 | | | |
| 207 | return container_of(ltn, struct mod_tree_node, node)->mod; | | |
| 208 | } | | |
| 209 | | | |
| 210 | #else /* MODULES_TREE_LOOKUP */ | | |
| 211 | | | |
| 212 | static unsigned long module_addr_min = -1UL, module_addr_max = 0; | | |
| 213 | | | |
| 214 | static void mod_tree_insert(struct module *mod) { } | | |
| 215 | static void mod_tree_remove_init(struct module *mod) { } | | |
| 216 | static void mod_tree_remove(struct module *mod) { } | | |
| 217 | | | |
| 218 | static struct module *mod_find(unsigned long addr) | | |
| 219 | { | | |
| 220 | struct module *mod; | | |
| 221 | | | |
| 222 | list_for_each_entry_rcu(mod, &modules, list, | | |
| 223 | lockdep_is_held(&module_mutex)) { | | |
| 224 | if (within_module(addr, mod)) | | |
| 225 | return mod; | | |
| 226 | } | | |
| 227 | | | |
| 228 | return NULL; | | |
| 229 | } | | |
| 230 | | | |
| 231 | #endif /* MODULES_TREE_LOOKUP */ | | |
| 232 | | 95 | |
| 233 | /* | 96 | /* |
| 234 | * Bounds of module text, for speeding up __module_address. | 97 | * Bounds of module text, for speeding up __module_address. |
| 235 | * Protected by module_mutex. | 98 | * Protected by module_mutex. |
| 236 | */ | 99 | */ |
| 237 | static void __mod_update_bounds(void *base, unsigned int size) | 100 | static void __mod_update_bounds(void *base, unsigned int size, struct mod_tree_root *tree) |
| 238 | { | 101 | { |
| 239 | unsigned long min = (unsigned long)base; | 102 | unsigned long min = (unsigned long)base; |
| 240 | unsigned long max = min + size; | 103 | unsigned long max = min + size; |
| 241 | | 104 | |
| 242 | if (min < module_addr_min) | 105 | if (min < tree->addr_min) |
| 243 | module_addr_min = min; | 106 | tree->addr_min = min; |
| 244 | if (max > module_addr_max) | 107 | if (max > tree->addr_max) |
| 245 | module_addr_max = max; | 108 | tree->addr_max = max; |
| 246 | } | 109 | } |
| 247 | | 110 | |
| 248 | static void mod_update_bounds(struct module *mod) | 111 | static void mod_update_bounds(struct module *mod) |
| 249 | { | 112 | { |
| 250 | __mod_update_bounds(mod->core_layout.base, mod->core_layout.size); | 113 | __mod_update_bounds(mod->core_layout.base, mod->core_layout.size, &mod_tree); |
| 251 | if (mod->init_layout.size) | 114 | if (mod->init_layout.size) |
| 252 | __mod_update_bounds(mod->init_layout.base, mod->init_layout.size); | 115 | __mod_update_bounds(mod->init_layout.base, mod->init_layout.size, &mod_tree); |
| 253 | } | 116 | #ifdef CONFIG_ARCH_WANTS_MODULES_DATA_IN_VMALLOC |
| 254 | | 117 | __mod_update_bounds(mod->data_layout.base, mod->data_layout.size, &mod_data_tree); |
| 255 | #ifdef CONFIG_KGDB_KDB | | |
| 256 | struct list_head *kdb_modules = &modules; /* kdb needs the list of modules */ | | |
| 257 | #endif /* CONFIG_KGDB_KDB */ | | |
| 258 | | | |
| 259 | static void module_assert_mutex_or_preempt(void) | | |
| 260 | { | | |
| 261 | #ifdef CONFIG_LOCKDEP | | |
| 262 | if (unlikely(!debug_locks)) | | |
| 263 | return; | | |
| 264 | | | |
| 265 | WARN_ON_ONCE(!rcu_read_lock_sched_held() && | | |
| 266 | !lockdep_is_held(&module_mutex)); | | |
| 267 | #endif | | |
| 268 | } | | |
| 269 | | | |
| 270 | #ifdef CONFIG_MODULE_SIG | | |
| 271 | static bool sig_enforce = IS_ENABLED(CONFIG_MODULE_SIG_FORCE); | | |
| 272 | module_param(sig_enforce, bool_enable_only, 0644); | | |
| 273 | | | |
| 274 | void set_module_sig_enforced(void) | | |
| 275 | { | | |
| 276 | sig_enforce = true; | | |
| 277 | } | | |
| 278 | #else | | |
| 279 | #define sig_enforce false | | |
| 280 | #endif | 118 | #endif |
| 281 | | | |
| 282 | /* | | |
| 283 | * Export sig_enforce kernel cmdline parameter to allow other subsystems rely | | |
| 284 | * on that instead of directly to CONFIG_MODULE_SIG_FORCE config. | | |
| 285 | */ | | |
| 286 | bool is_module_sig_enforced(void) | | |
| 287 | { | | |
| 288 | return sig_enforce; | | |
| 289 | } | 119 | } |
| 290 | EXPORT_SYMBOL(is_module_sig_enforced); | | |
| 291 | | 120 | |
| 292 | /* Block module loading/unloading? */ | 121 | /* Block module loading/unloading? */ |
| 293 | int modules_disabled = 0; | 122 | int modules_disabled = 0; |
| @@ -408,66 +237,12 @@ static __maybe_unused void *any_section_objs(const struct load_info *info, · 114 unmodified lines |
| 408 | return (void *)info->sechdrs[sec].sh_addr; | 237 | return (void *)info->sechdrs[sec].sh_addr; |
| 409 | } | 238 | } |
| 410 | | 239 | |
| 411 | /* Provided by the linker */ | | |
| 412 | extern const struct kernel_symbol __start___ksymtab[]; | | |
| 413 | extern const struct kernel_symbol __stop___ksymtab[]; | | |
| 414 | extern const struct kernel_symbol __start___ksymtab_gpl[]; | | |
| 415 | extern const struct kernel_symbol __stop___ksymtab_gpl[]; | | |
| 416 | extern const s32 __start___kcrctab[]; | | |
| 417 | extern const s32 __start___kcrctab_gpl[]; | | |
| 418 | | | |
| 419 | #ifndef CONFIG_MODVERSIONS | 240 | #ifndef CONFIG_MODVERSIONS |
| 420 | #define symversion(base, idx) NULL | 241 | #define symversion(base, idx) NULL |
| 421 | #else | 242 | #else |
| 422 | #define symversion(base, idx) ((base != NULL) ? ((base) + (idx)) : NULL) | 243 | #define symversion(base, idx) ((base != NULL) ? ((base) + (idx)) : NULL) |
| 423 | #endif | 244 | #endif |
| 424 | | 245 | |
| 425 | struct symsearch { | | |
| 426 | const struct kernel_symbol *start, *stop; | | |
| 427 | const s32 *crcs; | | |
| 428 | enum mod_license { | | |
| 429 | NOT_GPL_ONLY, | | |
| 430 | GPL_ONLY, | | |
| 431 | } license; | | |
| 432 | }; | | |
| 433 | | | |
| 434 | struct find_symbol_arg { | | |
| 435 | /* Input */ | | |
| 436 | const char *name; | | |
| 437 | bool gplok; | | |
| 438 | bool warn; | | |
| 439 | | | |
| 440 | /* Output */ | | |
| 441 | struct module *owner; | | |
| 442 | const s32 *crc; | | |
| 443 | const struct kernel_symbol *sym; | | |
| 444 | enum mod_license license; | | |
| 445 | }; | | |
| 446 | | | |
| 447 | static bool check_exported_symbol(const struct symsearch *syms, | | |
| 448 | struct module *owner, | | |
| 449 | unsigned int symnum, void *data) | | |
| 450 | { | | |
| 451 | struct find_symbol_arg *fsa = data; | | |
| 452 | | | |
| 453 | if (!fsa->gplok && syms->license == GPL_ONLY) | | |
| 454 | return false; | | |
| 455 | fsa->owner = owner; | | |
| 456 | fsa->crc = symversion(syms->crcs, symnum); | | |
| 457 | fsa->sym = &syms->start[symnum]; | | |
| 458 | fsa->license = syms->license; | | |
| 459 | return true; | | |
| 460 | } | | |
| 461 | | | |
| 462 | static unsigned long kernel_symbol_value(const struct kernel_symbol *sym) | | |
| 463 | { | | |
| 464 | #ifdef CONFIG_HAVE_ARCH_PREL32_RELOCATIONS | | |
| 465 | return (unsigned long)offset_to_ptr(&sym->value_offset); | | |
| 466 | #else | | |
| 467 | return sym->value; | | |
| 468 | #endif | | |
| 469 | } | | |
| 470 | | | |
| 471 | static const char *kernel_symbol_name(const struct kernel_symbol *sym) | 246 | static const char *kernel_symbol_name(const struct kernel_symbol *sym) |
| 472 | { | 247 | { |
| 473 | #ifdef CONFIG_HAVE_ARCH_PREL32_RELOCATIONS | 248 | #ifdef CONFIG_HAVE_ARCH_PREL32_RELOCATIONS |
| @@ -488,33 +263,38 @@ static const char *kernel_symbol_namespace(const struct kernel_symbol *sym) · 14 unmodified lines |
| 488 | #endif | 263 | #endif |
| 489 | } | 264 | } |
| 490 | | 265 | |
| 491 | static int cmp_name(const void *name, const void *sym) | 266 | int cmp_name(const void *name, const void *sym) |
| 492 | { | 267 | { |
| 493 | return strcmp(name, kernel_symbol_name(sym)); | 268 | return strcmp(name, kernel_symbol_name(sym)); |
| 494 | } | 269 | } |
| 495 | | 270 | |
| 496 | static bool find_exported_symbol_in_section(const struct symsearch *syms, | 271 | static bool find_exported_symbol_in_section(const struct symsearch *syms, |
| 497 | struct module *owner, | 272 | struct module *owner, |
| 498 | void *data) | 273 | struct find_symbol_arg *fsa) |
| 499 | { | 274 | { |
| 500 | struct find_symbol_arg *fsa = data; | | |
| 501 | struct kernel_symbol *sym; | 275 | struct kernel_symbol *sym; |
| 502 | | 276 | |
| | 277 | if (!fsa->gplok && syms->license == GPL_ONLY) |
| | 278 | return false; |
| | 279 | |
| 503 | sym = bsearch(fsa->name, syms->start, syms->stop - syms->start, | 280 | sym = bsearch(fsa->name, syms->start, syms->stop - syms->start, |
| 504 | sizeof(struct kernel_symbol), cmp_name); | 281 | sizeof(struct kernel_symbol), cmp_name); |
| | 282 | if (!sym) |
| | 283 | return false; |
| 505 | | 284 | |
| 506 | if (sym != NULL && check_exported_symbol(syms, owner, | 285 | fsa->owner = owner; |
| 507 | sym - syms->start, data)) | 286 | fsa->crc = symversion(syms->crcs, sym - syms->start); |
| 508 | return true; | 287 | fsa->sym = sym; |
| | 288 | fsa->license = syms->license; |
| 509 | | 289 | |
| 510 | return false; | 290 | return true; |
| 511 | } | 291 | } |
| 512 | | 292 | |
| 513 | /* | 293 | /* |
| 514 | * Find an exported symbol and return it, along with, (optional) crc and | 294 | * Find an exported symbol and return it, along with, (optional) crc and |
| 515 | * (optional) module which owns it. Needs preempt disabled or module_mutex. | 295 | * (optional) module which owns it. Needs preempt disabled or module_mutex. |
| 516 | */ | 296 | */ |
| 517 | static bool find_symbol(struct find_symbol_arg *fsa) | 297 | bool find_symbol(struct find_symbol_arg *fsa) |
| 518 | { | 298 | { |
| 519 | static const struct symsearch arr[] = { | 299 | static const struct symsearch arr[] = { |
| 520 | { __start___ksymtab, __stop___ksymtab, __start___kcrctab, | 300 | { __start___ksymtab, __stop___ksymtab, __start___kcrctab, |
| @@ -558,8 +338,8 @@ static bool find_symbol(struct find_symbol_arg *fsa) · 37 unmodified lines |
| 558 | * Search for module by name: must hold module_mutex (or preempt disabled | 338 | * Search for module by name: must hold module_mutex (or preempt disabled |
| 559 | * for read-only access). | 339 | * for read-only access). |
| 560 | */ | 340 | */ |
| 561 | static struct module *find_module_all(const char *name, size_t len, | 341 | struct module *find_module_all(const char *name, size_t len, |
| 562 | bool even_unformed) | 342 | bool even_unformed) |
| 563 | { | 343 | { |
| 564 | struct module *mod; | 344 | struct module *mod; |
| 565 | | 345 | |
| @@ -985,31 +765,6 @@ out: · 419 unmodified lines |
| 985 | return ret; | 765 | return ret; |
| 986 | } | 766 | } |
| 987 | | 767 | |
| 988 | static inline void print_unload_info(struct seq_file *m, struct module *mod) | | |
| 989 | { | | |
| 990 | struct module_use *use; | | |
| 991 | int printed_something = 0; | | |
| 992 | | | |
| 993 | seq_printf(m, " %i ", module_refcount(mod)); | | |
| 994 | | | |
| 995 | /* | | |
| 996 | * Always include a trailing , so userspace can differentiate | | |
| 997 | * between this and the old multi-field proc format. | | |
| 998 | */ | | |
| 999 | list_for_each_entry(use, &mod->source_list, source_list) { | | |
| 1000 | printed_something = 1; | | |
| 1001 | seq_printf(m, "%s,", use->source->name); | | |
| 1002 | } | | |
| 1003 | | | |
| 1004 | if (mod->init != NULL && mod->exit == NULL) { | | |
| 1005 | printed_something = 1; | | |
| 1006 | seq_puts(m, "[permanent],"); | | |
| 1007 | } | | |
| 1008 | | | |
| 1009 | if (!printed_something) | | |
| 1010 | seq_puts(m, "-"); | | |
| 1011 | } | | |
| 1012 | | | |
| 1013 | void __symbol_put(const char *symbol) | 768 | void __symbol_put(const char *symbol) |
| 1014 | { | 769 | { |
| 1015 | struct find_symbol_arg fsa = { | 770 | struct find_symbol_arg fsa = { |
| @@ -1099,12 +854,6 @@ void module_put(struct module *module) · 83 unmodified lines |
| 1099 | EXPORT_SYMBOL(module_put); | 854 | EXPORT_SYMBOL(module_put); |
| 1100 | | 855 | |
| 1101 | #else /* !CONFIG_MODULE_UNLOAD */ | 856 | #else /* !CONFIG_MODULE_UNLOAD */ |
| 1102 | static inline void print_unload_info(struct seq_file *m, struct module *mod) | | |
| 1103 | { | | |
| 1104 | /* We don't know the usage count, or what modules are using. */ | | |
| 1105 | seq_puts(m, " - -"); | | |
| 1106 | } | | |
| 1107 | | | |
| 1108 | static inline void module_unload_free(struct module *mod) | 857 | static inline void module_unload_free(struct module *mod) |
| 1109 | { | 858 | { |
| 1110 | } | 859 | } |
| @@ -1120,13 +869,13 @@ static inline int module_unload_init(struct module *mod) · 9 unmodified lines |
| 1120 | } | 869 | } |
| 1121 | #endif /* CONFIG_MODULE_UNLOAD */ | 870 | #endif /* CONFIG_MODULE_UNLOAD */ |
| 1122 | | 871 | |
| 1123 | static size_t module_flags_taint(struct module *mod, char *buf) | 872 | size_t module_flags_taint(unsigned long taints, char *buf) |
| 1124 | { | 873 | { |
| 1125 | size_t l = 0; | 874 | size_t l = 0; |
| 1126 | int i; | 875 | int i; |
| 1127 | | 876 | |
| 1128 | for (i = 0; i < TAINT_FLAGS_COUNT; i++) { | 877 | for (i = 0; i < TAINT_FLAGS_COUNT; i++) { |
| 1129 | if (taint_flags[i].module && test_bit(i, &mod->taints)) | 878 | if (taint_flags[i].module && test_bit(i, &taints)) |
| 1130 | buf[l++] = taint_flags[i].c_true; | 879 | buf[l++] = taint_flags[i].c_true; |
| 1131 | } | 880 | } |
| 1132 | | 881 | |
| @@ -1179,6 +928,17 @@ static ssize_t show_coresize(struct module_attribute *mattr, · 46 unmodified lines |
| 1179 | static struct module_attribute modinfo_coresize = | 928 | static struct module_attribute modinfo_coresize = |
| 1180 | __ATTR(coresize, 0444, show_coresize, NULL); | 929 | __ATTR(coresize, 0444, show_coresize, NULL); |
| 1181 | | 930 | |
| | 931 | #ifdef CONFIG_ARCH_WANTS_MODULES_DATA_IN_VMALLOC |
| | 932 | static ssize_t show_datasize(struct module_attribute *mattr, |
| | 933 | struct module_kobject *mk, char *buffer) |
| | 934 | { |
| | 935 | return sprintf(buffer, "%u\n", mk->mod->data_layout.size); |
| | 936 | } |
| | 937 | |
| | 938 | static struct module_attribute modinfo_datasize = |
| | 939 | __ATTR(datasize, 0444, show_datasize, NULL); |
| | 940 | #endif |
| | 941 | |
| 1182 | static ssize_t show_initsize(struct module_attribute *mattr, | 942 | static ssize_t show_initsize(struct module_attribute *mattr, |
| 1183 | struct module_kobject *mk, char *buffer) | 943 | struct module_kobject *mk, char *buffer) |
| 1184 | { | 944 | { |
| @@ -1193,7 +953,7 @@ static ssize_t show_taint(struct module_attribute *mattr, · 8 unmodified lines |
| 1193 | { | 953 | { |
| 1194 | size_t l; | 954 | size_t l; |
| 1195 | | 955 | |
| 1196 | l = module_flags_taint(mk->mod, buffer); | 956 | l = module_flags_taint(mk->mod->taints, buffer); |
| 1197 | buffer[l++] = '\n'; | 957 | buffer[l++] = '\n'; |
| 1198 | return l; | 958 | return l; |
| 1199 | } | 959 | } |
| @@ -1201,12 +961,15 @@ static ssize_t show_taint(struct module_attribute *mattr, · 1 unmodified line |
| 1201 | static struct module_attribute modinfo_taint = | 961 | static struct module_attribute modinfo_taint = |
| 1202 | __ATTR(taint, 0444, show_taint, NULL); | 962 | __ATTR(taint, 0444, show_taint, NULL); |
| 1203 | | 963 | |
| 1204 | static struct module_attribute *modinfo_attrs[] = { | 964 | struct module_attribute *modinfo_attrs[] = { |
| 1205 | &module_uevent, | 965 | &module_uevent, |
| 1206 | &modinfo_version, | 966 | &modinfo_version, |
| 1207 | &modinfo_srcversion, | 967 | &modinfo_srcversion, |
| 1208 | &modinfo_initstate, | 968 | &modinfo_initstate, |
| 1209 | &modinfo_coresize, | 969 | &modinfo_coresize, |
| | 970 | #ifdef CONFIG_ARCH_WANTS_MODULES_DATA_IN_VMALLOC |
| | 971 | &modinfo_datasize, |
| | 972 | #endif |
| 1210 | &modinfo_initsize, | 973 | &modinfo_initsize, |
| 1211 | &modinfo_taint, | 974 | &modinfo_taint, |
| 1212 | #ifdef CONFIG_MODULE_UNLOAD | 975 | #ifdef CONFIG_MODULE_UNLOAD |
| @@ -1215,9 +978,11 @@ static struct module_attribute *modinfo_attrs[] = { · 2 unmodified lines |
| 1215 | NULL, | 978 | NULL, |
| 1216 | }; | 979 | }; |
| 1217 | | 980 | |
| | 981 | size_t modinfo_attrs_count = ARRAY_SIZE(modinfo_attrs); |
| | 982 | |
| 1218 | static const char vermagic[] = VERMAGIC_STRING; | 983 | static const char vermagic[] = VERMAGIC_STRING; |
| 1219 | | 984 | |
| 1220 | static int try_to_force_load(struct module *mod, const char *reason) | 985 | int try_to_force_load(struct module *mod, const char *reason) |
| 1221 | { | 986 | { |
| 1222 | #ifdef CONFIG_MODULE_FORCE_LOAD | 987 | #ifdef CONFIG_MODULE_FORCE_LOAD |
| 1223 | if (!test_taint(TAINT_FORCED_MODULE)) | 988 | if (!test_taint(TAINT_FORCED_MODULE)) |
| @@ -1229,107 +994,6 @@ static int try_to_force_load(struct module *mod, const char *reason) · 5 unmodified lines |
| 1229 | #endif | 994 | #endif |
| 1230 | } | 995 | } |
| 1231 | | 996 | |
| 1232 | #ifdef CONFIG_MODVERSIONS | | |
| 1233 | | | |
| 1234 | static int check_version(const struct load_info *info, | | |
| 1235 | const char *symname, | | |
| 1236 | struct module *mod, | | |
| 1237 | const s32 *crc) | | |
| 1238 | { | | |
| 1239 | Elf_Shdr *sechdrs = info->sechdrs; | | |
| 1240 | unsigned int versindex = info->index.vers; | | |
| 1241 | unsigned int i, num_versions; | | |
| 1242 | struct modversion_info *versions; | | |
| 1243 | | | |
| 1244 | /* Exporting module didn't supply crcs? OK, we're already tainted. */ | | |
| 1245 | if (!crc) | | |
| 1246 | return 1; | | |
| 1247 | | | |
| 1248 | /* No versions at all? modprobe --force does this. */ | | |
| 1249 | if (versindex == 0) | | |
| 1250 | return try_to_force_load(mod, symname) == 0; | | |
| 1251 | | | |
| 1252 | versions = (void *) sechdrs[versindex].sh_addr; | | |
| 1253 | num_versions = sechdrs[versindex].sh_size | | |
| 1254 | / sizeof(struct modversion_info); | | |
| 1255 | | | |
| 1256 | for (i = 0; i < num_versions; i++) { | | |
| 1257 | u32 crcval; | | |
| 1258 | | | |
| 1259 | if (strcmp(versions[i].name, symname) != 0) | | |
| 1260 | continue; | | |
| 1261 | | | |
| 1262 | crcval = *crc; | | |
| 1263 | if (versions[i].crc == crcval) | | |
| 1264 | return 1; | | |
| 1265 | pr_debug("Found checksum %X vs module %lX\n", | | |
| 1266 | crcval, versions[i].crc); | | |
| 1267 | goto bad_version; | | |
| 1268 | } | | |
| 1269 | | | |
| 1270 | /* Broken toolchain. Warn once, then let it go.. */ | | |
| 1271 | pr_warn_once("%s: no symbol version for %s\n", info->name, symname); | | |
| 1272 | return 1; | | |
| 1273 | | | |
| 1274 | bad_version: | | |
| 1275 | pr_warn("%s: disagrees about version of symbol %s\n", | | |
| 1276 | info->name, symname); | | |
| 1277 | return 0; | | |
| 1278 | } | | |
| 1279 | | | |
| 1280 | static inline int check_modstruct_version(const struct load_info *info, | | |
| 1281 | struct module *mod) | | |
| 1282 | { | | |
| 1283 | struct find_symbol_arg fsa = { | | |
| 1284 | .name = "module_layout", | | |
| 1285 | .gplok = true, | | |
| 1286 | }; | | |
| 1287 | | | |
| 1288 | /* | | |
| 1289 | * Since this should be found in kernel (which can't be removed), no | | |
| 1290 | * locking is necessary -- use preempt_disable() to placate lockdep. | | |
| 1291 | */ | | |
| 1292 | preempt_disable(); | | |
| 1293 | if (!find_symbol(&fsa)) { | | |
| 1294 | preempt_enable(); | | |
| 1295 | BUG(); | | |
| 1296 | } | | |
| 1297 | preempt_enable(); | | |
| 1298 | return check_version(info, "module_layout", mod, fsa.crc); | | |
| 1299 | } | | |
| 1300 | | | |
| 1301 | /* First part is kernel version, which we ignore if module has crcs. */ | | |
| 1302 | static inline int same_magic(const char *amagic, const char *bmagic, | | |
| 1303 | bool has_crcs) | | |
| 1304 | { | | |
| 1305 | if (has_crcs) { | | |
| 1306 | amagic += strcspn(amagic, " "); | | |
| 1307 | bmagic += strcspn(bmagic, " "); | | |
| 1308 | } | | |
| 1309 | return strcmp(amagic, bmagic) == 0; | | |
| 1310 | } | | |
| 1311 | #else | | |
| 1312 | static inline int check_version(const struct load_info *info, | | |
| 1313 | const char *symname, | | |
| 1314 | struct module *mod, | | |
| 1315 | const s32 *crc) | | |
| 1316 | { | | |
| 1317 | return 1; | | |
| 1318 | } | | |
| 1319 | | | |
| 1320 | static inline int check_modstruct_version(const struct load_info *info, | | |
| 1321 | struct module *mod) | | |
| 1322 | { | | |
| 1323 | return 1; | | |
| 1324 | } | | |
| 1325 | | | |
| 1326 | static inline int same_magic(const char *amagic, const char *bmagic, | | |
| 1327 | bool has_crcs) | | |
| 1328 | { | | |
| 1329 | return strcmp(amagic, bmagic) == 0; | | |
| 1330 | } | | |
| 1331 | #endif /* CONFIG_MODVERSIONS */ | | |
| 1332 | | | |
| 1333 | static char *get_modinfo(const struct load_info *info, const char *tag); | 997 | static char *get_modinfo(const struct load_info *info, const char *tag); |
| 1334 | static char *get_next_modinfo(const struct load_info *info, const char *tag, | 998 | static char *get_next_modinfo(const struct load_info *info, const char *tag, |
| 1335 | char *prev); | 999 | char *prev); |
| @@ -1364,20 +1028,20 @@ static int verify_namespace_is_imported(const struct load_info *info, · 28 unmodified lines |
| 1364 | return 0; | 1028 | return 0; |
| 1365 | } | 1029 | } |
| 1366 | | 1030 | |
| 1367 | static bool inherit_taint(struct module *mod, struct module *owner) | 1031 | static bool inherit_taint(struct module *mod, struct module *owner, const char *name) |
| 1368 | { | 1032 | { |
| 1369 | if (!owner || !test_bit(TAINT_PROPRIETARY_MODULE, &owner->taints)) | 1033 | if (!owner || !test_bit(TAINT_PROPRIETARY_MODULE, &owner->taints)) |
| 1370 | return true; | 1034 | return true; |
| 1371 | | 1035 | |
| 1372 | if (mod->using_gplonly_symbols) { | 1036 | if (mod->using_gplonly_symbols) { |
| 1373 | pr_err("%s: module using GPL-only symbols uses symbols from proprietary module %s.\n", | 1037 | pr_err("%s: module using GPL-only symbols uses symbols %s from proprietary module %s.\n", |
| 1374 | mod->name, owner->name); | 1038 | mod->name, name, owner->name); |
| 1375 | return false; | 1039 | return false; |
| 1376 | } | 1040 | } |
| 1377 | | 1041 | |
| 1378 | if (!test_bit(TAINT_PROPRIETARY_MODULE, &mod->taints)) { | 1042 | if (!test_bit(TAINT_PROPRIETARY_MODULE, &mod->taints)) { |
| 1379 | pr_warn("%s: module uses symbols from proprietary module %s, inheriting taint.\n", | 1043 | pr_warn("%s: module uses symbols %s from proprietary module %s, inheriting taint.\n", |
| 1380 | mod->name, owner->name); | 1044 | mod->name, name, owner->name); |
| 1381 | set_bit(TAINT_PROPRIETARY_MODULE, &mod->taints); | 1045 | set_bit(TAINT_PROPRIETARY_MODULE, &mod->taints); |
| 1382 | } | 1046 | } |
| 1383 | return true; | 1047 | return true; |
| @@ -1409,7 +1073,7 @@ static const struct kernel_symbol *resolve_symbol(struct module *mod, · 25 unmodified lines |
| 1409 | if (fsa.license == GPL_ONLY) | 1073 | if (fsa.license == GPL_ONLY) |
| 1410 | mod->using_gplonly_symbols = true; | 1074 | mod->using_gplonly_symbols = true; |
| 1411 | | 1075 | |
| 1412 | if (!inherit_taint(mod, fsa.owner)) { | 1076 | if (!inherit_taint(mod, fsa.owner, name)) { |
| 1413 | fsa.sym = NULL; | 1077 | fsa.sym = NULL; |
| 1414 | goto getname; | 1078 | goto getname; |
| 1415 | } | 1079 | } |
| @@ -1457,734 +1121,69 @@ resolve_symbol_wait(struct module *mod, · 41 unmodified lines |
| 1457 | return ksym; | 1121 | return ksym; |
| 1458 | } | 1122 | } |
| 1459 | | 1123 | |
| 1460 | #ifdef CONFIG_KALLSYMS | 1124 | void __weak module_memfree(void *module_region) |
| 1461 | static inline bool sect_empty(const Elf_Shdr *sect) | | |
| 1462 | { | 1125 | { |
| 1463 | return !(sect->sh_flags & SHF_ALLOC) || sect->sh_size == 0; | 1126 | /* |
| | 1127 | * This memory may be RO, and freeing RO memory in an interrupt is not |
| | 1128 | * supported by vmalloc. |
| | 1129 | */ |
| | 1130 | WARN_ON(in_interrupt()); |
| | 1131 | vfree(module_region); |
| 1464 | } | 1132 | } |
| 1465 | #endif | | |
| 1466 | | 1133 | |
| 1467 | /* | 1134 | void __weak module_arch_cleanup(struct module *mod) |
| 1468 | * /sys/module/foo/sections stuff | 1135 | { |
| 1469 | * J. Corbet <corbet@lwn.net> | 1136 | } |
| 1470 | */ | | |
| 1471 | #ifdef CONFIG_SYSFS | | |
| 1472 | | 1137 | |
| 1473 | #ifdef CONFIG_KALLSYMS | 1138 | void __weak module_arch_freeing_init(struct module *mod) |
| 1474 | struct module_sect_attr { | 1139 | { |
| 1475 | struct bin_attribute battr; | 1140 | } |
| 1476 | unsigned long address; | | |
| 1477 | }; | | |
| 1478 | | 1141 | |
| 1479 | struct module_sect_attrs { | 1142 | static void cfi_cleanup(struct module *mod); |
| 1480 | struct attribute_group grp; | | |
| 1481 | unsigned int nsections; | | |
| 1482 | struct module_sect_attr attrs[]; | | |
| 1483 | }; | | |
| 1484 | | 1143 | |
| 1485 | #define MODULE_SECT_READ_SIZE (3 /* "0x", "\n" */ + (BITS_PER_LONG / 4)) | 1144 | /* Free a module, remove from lists, etc. */ |
| 1486 | static ssize_t module_sect_read(struct file *file, struct kobject *kobj, | 1145 | static void free_module(struct module *mod) |
| 1487 | struct bin_attribute *battr, | | |
| 1488 | char *buf, loff_t pos, size_t count) | | |
| 1489 | { | 1146 | { |
| 1490 | struct module_sect_attr *sattr = | 1147 | trace_module_free(mod); |
| 1491 | container_of(battr, struct module_sect_attr, battr); | | |
| 1492 | char bounce[MODULE_SECT_READ_SIZE + 1]; | | |
| 1493 | size_t wrote; | | |
| 1494 | | 1148 | |
| 1495 | if (pos != 0) | 1149 | mod_sysfs_teardown(mod); |
| 1496 | return -EINVAL; | | |
| 1497 | | 1150 | |
| 1498 | /* | 1151 | /* |
| 1499 | * Since we're a binary read handler, we must account for the | 1152 | * We leave it in list to prevent duplicate loads, but make sure |
| 1500 | * trailing NUL byte that sprintf will write: if "buf" is | 1153 | * that noone uses it while it's being deconstructed. |
| 1501 | * too small to hold the NUL, or the NUL is exactly the last | | |
| 1502 | * byte, the read will look like it got truncated by one byte. | | |
| 1503 | * Since there is no way to ask sprintf nicely to not write | | |
| 1504 | * the NUL, we have to use a bounce buffer. | | |
| 1505 | */ | 1154 | */ |
| 1506 | wrote = scnprintf(bounce, sizeof(bounce), "0x%px\n", | 1155 | mutex_lock(&module_mutex); |
| 1507 | kallsyms_show_value(file->f_cred) | 1156 | mod->state = MODULE_STATE_UNFORMED; |
| 1508 | ? (void *)sattr->address : NULL); | 1157 | mutex_unlock(&module_mutex); |
| 1509 | count = min(count, wrote); | | |
| 1510 | memcpy(buf, bounce, count); | | |
| 1511 | | 1158 | |
| 1512 | return count; | 1159 | /* Remove dynamic debug info */ |
| 1513 | } | 1160 | ddebug_remove_module(mod->name); |
| 1514 | | 1161 | |
| 1515 | static void free_sect_attrs(struct module_sect_attrs *sect_attrs) | 1162 | /* Arch-specific cleanup. */ |
| 1516 | { | 1163 | module_arch_cleanup(mod); |
| 1517 | unsigned int section; | | |
| 1518 | | 1164 | |
| 1519 | for (section = 0; section < sect_attrs->nsections; section++) | 1165 | /* Module unload stuff */ |
| 1520 | kfree(sect_attrs->attrs[section].battr.attr.name); | 1166 | module_unload_free(mod); |
| 1521 | kfree(sect_attrs); | | |
| 1522 | } | | |
| 1523 | | 1167 | |
| 1524 | static void add_sect_attrs(struct module *mod, const struct load_info *info) | 1168 | /* Free any allocated parameters. */ |
| 1525 | { | 1169 | destroy_params(mod->kp, mod->num_kp); |
| 1526 | unsigned int nloaded = 0, i, size[2]; | | |
| 1527 | struct module_sect_attrs *sect_attrs; | | |
| 1528 | struct module_sect_attr *sattr; | | |
| 1529 | struct bin_attribute **gattr; | | |
| 1530 | | 1170 | |
| 1531 | /* Count loaded sections and allocate structures */ | 1171 | if (is_livepatch_module(mod)) |
| 1532 | for (i = 0; i < info->hdr->e_shnum; i++) | 1172 | free_module_elf(mod); |
| 1533 | if (!sect_empty(&info->sechdrs[i])) | | |
| 1534 | nloaded++; | | |
| 1535 | size[0] = ALIGN(struct_size(sect_attrs, attrs, nloaded), | | |
| 1536 | sizeof(sect_attrs->grp.bin_attrs[0])); | | |
| 1537 | size[1] = (nloaded + 1) * sizeof(sect_attrs->grp.bin_attrs[0]); | | |
| 1538 | sect_attrs = kzalloc(size[0] + size[1], GFP_KERNEL); | | |
| 1539 | if (sect_attrs == NULL) | | |
| 1540 | return; | | |
| 1541 | | 1173 | |
| 1542 | /* Setup section attributes. */ | 1174 | /* Now we can delete it from the lists */ |
| 1543 | sect_attrs->grp.name = "sections"; | 1175 | mutex_lock(&module_mutex); |
| 1544 | sect_attrs->grp.bin_attrs = (void *)sect_attrs + size[0]; | 1176 | /* Unlink carefully: kallsyms could be walking list. */ |
| 1545 | | 1177 | list_del_rcu(&mod->list); |
| 1546 | sect_attrs->nsections = 0; | 1178 | mod_tree_remove(mod); |
| 1547 | sattr = §_attrs->attrs[0]; | 1179 | /* Remove this module from bug list, this uses list_del_rcu */ |
| 1548 | gattr = §_attrs->grp.bin_attrs[0]; | 1180 | module_bug_cleanup(mod); |
| 1549 | for (i = 0; i < info->hdr->e_shnum; i++) { | 1181 | /* Wait for RCU-sched synchronizing before releasing mod->list and buglist. */ |
| 1550 | Elf_Shdr *sec = &info->sechdrs[i]; | 1182 | synchronize_rcu(); |
| 1551 | if (sect_empty(sec)) | 1183 | if (try_add_tainted_module(mod)) |
| 1552 | continue; | 1184 | pr_err("%s: adding tainted module to the unloaded tainted modules list failed.\n", |
| 1553 | sysfs_bin_attr_init(&sattr->battr); | 1185 | mod->name); |
| 1554 | sattr->address = sec->sh_addr; | 1186 | mutex_unlock(&module_mutex); |
| 1555 | sattr->battr.attr.name = | | |
| 1556 | kstrdup(info->secstrings + sec->sh_name, GFP_KERNEL); | | |
| 1557 | if (sattr->battr.attr.name == NULL) | | |
| 1558 | goto out; | | |
| 1559 | sect_attrs->nsections++; | | |
| 1560 | sattr->battr.read = module_sect_read; | | |
| 1561 | sattr->battr.size = MODULE_SECT_READ_SIZE; | | |
| 1562 | sattr->battr.attr.mode = 0400; | | |
| 1563 | *(gattr++) = &(sattr++)->battr; | | |
| 1564 | } | | |
| 1565 | *gattr = NULL; | | |
| 1566 | | | |
| 1567 | if (sysfs_create_group(&mod->mkobj.kobj, §_attrs->grp)) | | |
| 1568 | goto out; | | |
| 1569 | | | |
| 1570 | mod->sect_attrs = sect_attrs; | | |
| 1571 | return; | | |
| 1572 | out: | | |
| 1573 | free_sect_attrs(sect_attrs); | | |
| 1574 | } | | |
| 1575 | | | |
| 1576 | static void remove_sect_attrs(struct module *mod) | | |
| 1577 | { | | |
| 1578 | if (mod->sect_attrs) { | | |
| 1579 | sysfs_remove_group(&mod->mkobj.kobj, | | |
| 1580 | &mod->sect_attrs->grp); | | |
| 1581 | /* | | |
| 1582 | * We are positive that no one is using any sect attrs | | |
| 1583 | * at this point. Deallocate immediately. | | |
| 1584 | */ | | |
| 1585 | free_sect_attrs(mod->sect_attrs); | | |
| 1586 | mod->sect_attrs = NULL; | | |
| 1587 | } | | |
| 1588 | } | | |
| 1589 | | | |
| 1590 | /* | | |
| 1591 | * /sys/module/foo/notes/.section.name gives contents of SHT_NOTE sections. | | |
| 1592 | */ | | |
| 1593 | | | |
| 1594 | struct module_notes_attrs { | | |
| 1595 | struct kobject *dir; | | |
| 1596 | unsigned int notes; | | |
| 1597 | struct bin_attribute attrs[]; | | |
| 1598 | }; | | |
| 1599 | | | |
| 1600 | static ssize_t module_notes_read(struct file *filp, struct kobject *kobj, | | |
| 1601 | struct bin_attribute *bin_attr, | | |
| 1602 | char *buf, loff_t pos, size_t count) | | |
| 1603 | { | | |
| 1604 | /* | | |
| 1605 | * The caller checked the pos and count against our size. | | |
| 1606 | */ | | |
| 1607 | memcpy(buf, bin_attr->private + pos, count); | | |
| 1608 | return count; | | |
| 1609 | } | | |
| 1610 | | | |
| 1611 | static void free_notes_attrs(struct module_notes_attrs *notes_attrs, | | |
| 1612 | unsigned int i) | | |
| 1613 | { | | |
| 1614 | if (notes_attrs->dir) { | | |
| 1615 | while (i-- > 0) | | |
| 1616 | sysfs_remove_bin_file(notes_attrs->dir, | | |
| 1617 | ¬es_attrs->attrs[i]); | | |
| 1618 | kobject_put(notes_attrs->dir); | | |
| 1619 | } | | |
| 1620 | kfree(notes_attrs); | | |
| 1621 | } | | |
| 1622 | | | |
| 1623 | static void add_notes_attrs(struct module *mod, const struct load_info *info) | | |
| 1624 | { | | |
| 1625 | unsigned int notes, loaded, i; | | |
| 1626 | struct module_notes_attrs *notes_attrs; | | |
| 1627 | struct bin_attribute *nattr; | | |
| 1628 | | | |
| 1629 | /* failed to create section attributes, so can't create notes */ | | |
| 1630 | if (!mod->sect_attrs) | | |
| 1631 | return; | | |
| 1632 | | | |
| 1633 | /* Count notes sections and allocate structures. */ | | |
| 1634 | notes = 0; | | |
| 1635 | for (i = 0; i < info->hdr->e_shnum; i++) | | |
| 1636 | if (!sect_empty(&info->sechdrs[i]) && | | |
| 1637 | (info->sechdrs[i].sh_type == SHT_NOTE)) | | |
| 1638 | ++notes; | | |
| 1639 | | | |
| 1640 | if (notes == 0) | | |
| 1641 | return; | | |
| 1642 | | | |
| 1643 | notes_attrs = kzalloc(struct_size(notes_attrs, attrs, notes), | | |
| 1644 | GFP_KERNEL); | | |
| 1645 | if (notes_attrs == NULL) | | |
| 1646 | return; | | |
| 1647 | | | |
| 1648 | notes_attrs->notes = notes; | | |
| 1649 | nattr = ¬es_attrs->attrs[0]; | | |
| 1650 | for (loaded = i = 0; i < info->hdr->e_shnum; ++i) { | | |
| 1651 | if (sect_empty(&info->sechdrs[i])) | | |
| 1652 | continue; | | |
| 1653 | if (info->sechdrs[i].sh_type == SHT_NOTE) { | | |
| 1654 | sysfs_bin_attr_init(nattr); | | |
| 1655 | nattr->attr.name = mod->sect_attrs->attrs[loaded].battr.attr.name; | | |
| 1656 | nattr->attr.mode = S_IRUGO; | | |
| 1657 | nattr->size = info->sechdrs[i].sh_size; | | |
| 1658 | nattr->private = (void *) info->sechdrs[i].sh_addr; | | |
| 1659 | nattr->read = module_notes_read; | | |
| 1660 | ++nattr; | | |
| 1661 | } | | |
| 1662 | ++loaded; | | |
| 1663 | } | | |
| 1664 | | | |
| 1665 | notes_attrs->dir = kobject_create_and_add("notes", &mod->mkobj.kobj); | | |
| 1666 | if (!notes_attrs->dir) | | |
| 1667 | goto out; | | |
| 1668 | | | |
| 1669 | for (i = 0; i < notes; ++i) | | |
| 1670 | if (sysfs_create_bin_file(notes_attrs->dir, | | |
| 1671 | ¬es_attrs->attrs[i])) | | |
| 1672 | goto out; | | |
| 1673 | | | |
| 1674 | mod->notes_attrs = notes_attrs; | | |
| 1675 | return; | | |
| 1676 | | | |
| 1677 | out: | | |
| 1678 | free_notes_attrs(notes_attrs, i); | | |
| 1679 | } | | |
| 1680 | | | |
| 1681 | static void remove_notes_attrs(struct module *mod) | | |
| 1682 | { | | |
| 1683 | if (mod->notes_attrs) | | |
| 1684 | free_notes_attrs(mod->notes_attrs, mod->notes_attrs->notes); | | |
| 1685 | } | | |
| 1686 | | | |
| 1687 | #else | | |
| 1688 | | | |
| 1689 | static inline void add_sect_attrs(struct module *mod, | | |
| 1690 | const struct load_info *info) | | |
| 1691 | { | | |
| 1692 | } | | |
| 1693 | | | |
| 1694 | static inline void remove_sect_attrs(struct module *mod) | | |
| 1695 | { | | |
| 1696 | } | | |
| 1697 | | | |
| 1698 | static inline void add_notes_attrs(struct module *mod, | | |
| 1699 | const struct load_info *info) | | |
| 1700 | { | | |
| 1701 | } | | |
| 1702 | | | |
| 1703 | static inline void remove_notes_attrs(struct module *mod) | | |
| 1704 | { | | |
| 1705 | } | | |
| 1706 | #endif /* CONFIG_KALLSYMS */ | | |
| 1707 | | | |
| 1708 | static void del_usage_links(struct module *mod) | | |
| 1709 | { | | |
| 1710 | #ifdef CONFIG_MODULE_UNLOAD | | |
| 1711 | struct module_use *use; | | |
| 1712 | | | |
| 1713 | mutex_lock(&module_mutex); | | |
| 1714 | list_for_each_entry(use, &mod->target_list, target_list) | | |
| 1715 | sysfs_remove_link(use->target->holders_dir, mod->name); | | |
| 1716 | mutex_unlock(&module_mutex); | | |
| 1717 | #endif | | |
| 1718 | } | | |
| 1719 | | | |
| 1720 | static int add_usage_links(struct module *mod) | | |
| 1721 | { | | |
| 1722 | int ret = 0; | | |
| 1723 | #ifdef CONFIG_MODULE_UNLOAD | | |
| 1724 | struct module_use *use; | | |
| 1725 | | | |
| 1726 | mutex_lock(&module_mutex); | | |
| 1727 | list_for_each_entry(use, &mod->target_list, target_list) { | | |
| 1728 | ret = sysfs_create_link(use->target->holders_dir, | | |
| 1729 | &mod->mkobj.kobj, mod->name); | | |
| 1730 | if (ret) | | |
| 1731 | break; | | |
| 1732 | } | | |
| 1733 | mutex_unlock(&module_mutex); | | |
| 1734 | if (ret) | | |
| 1735 | del_usage_links(mod); | | |
| 1736 | #endif | | |
| 1737 | return ret; | | |
| 1738 | } | | |
| 1739 | | | |
| 1740 | static void module_remove_modinfo_attrs(struct module *mod, int end); | | |
| 1741 | | | |
| 1742 | static int module_add_modinfo_attrs(struct module *mod) | | |
| 1743 | { | | |
| 1744 | struct module_attribute *attr; | | |
| 1745 | struct module_attribute *temp_attr; | | |
| 1746 | int error = 0; | | |
| 1747 | int i; | | |
| 1748 | | | |
| 1749 | mod->modinfo_attrs = kzalloc((sizeof(struct module_attribute) * | | |
| 1750 | (ARRAY_SIZE(modinfo_attrs) + 1)), | | |
| 1751 | GFP_KERNEL); | | |
| 1752 | if (!mod->modinfo_attrs) | | |
| 1753 | return -ENOMEM; | | |
| 1754 | | | |
| 1755 | temp_attr = mod->modinfo_attrs; | | |
| 1756 | for (i = 0; (attr = modinfo_attrs[i]); i++) { | | |
| 1757 | if (!attr->test || attr->test(mod)) { | | |
| 1758 | memcpy(temp_attr, attr, sizeof(*temp_attr)); | | |
| 1759 | sysfs_attr_init(&temp_attr->attr); | | |
| 1760 | error = sysfs_create_file(&mod->mkobj.kobj, | | |
| 1761 | &temp_attr->attr); | | |
| 1762 | if (error) | | |
| 1763 | goto error_out; | | |
| 1764 | ++temp_attr; | | |
| 1765 | } | | |
| 1766 | } | | |
| 1767 | | | |
| 1768 | return 0; | | |
| 1769 | | | |
| 1770 | error_out: | | |
| 1771 | if (i > 0) | | |
| 1772 | module_remove_modinfo_attrs(mod, --i); | | |
| 1773 | else | | |
| 1774 | kfree(mod->modinfo_attrs); | | |
| 1775 | return error; | | |
| 1776 | } | | |
| 1777 | | | |
| 1778 | static void module_remove_modinfo_attrs(struct module *mod, int end) | | |
| 1779 | { | | |
| 1780 | struct module_attribute *attr; | | |
| 1781 | int i; | | |
| 1782 | | | |
| 1783 | for (i = 0; (attr = &mod->modinfo_attrs[i]); i++) { | | |
| 1784 | if (end >= 0 && i > end) | | |
| 1785 | break; | | |
| 1786 | /* pick a field to test for end of list */ | | |
| 1787 | if (!attr->attr.name) | | |
| 1788 | break; | | |
| 1789 | sysfs_remove_file(&mod->mkobj.kobj, &attr->attr); | | |
| 1790 | if (attr->free) | | |
| 1791 | attr->free(mod); | | |
| 1792 | } | | |
| 1793 | kfree(mod->modinfo_attrs); | | |
| 1794 | } | | |
| 1795 | | | |
| 1796 | static void mod_kobject_put(struct module *mod) | | |
| 1797 | { | | |
| 1798 | DECLARE_COMPLETION_ONSTACK(c); | | |
| 1799 | mod->mkobj.kobj_completion = &c; | | |
| 1800 | kobject_put(&mod->mkobj.kobj); | | |
| 1801 | wait_for_completion(&c); | | |
| 1802 | } | | |
| 1803 | | | |
| 1804 | static int mod_sysfs_init(struct module *mod) | | |
| 1805 | { | | |
| 1806 | int err; | | |
| 1807 | struct kobject *kobj; | | |
| 1808 | | | |
| 1809 | if (!module_sysfs_initialized) { | | |
| 1810 | pr_err("%s: module sysfs not initialized\n", mod->name); | | |
| 1811 | err = -EINVAL; | | |
| 1812 | goto out; | | |
| 1813 | } | | |
| 1814 | | | |
| 1815 | kobj = kset_find_obj(module_kset, mod->name); | | |
| 1816 | if (kobj) { | | |
| 1817 | pr_err("%s: module is already loaded\n", mod->name); | | |
| 1818 | kobject_put(kobj); | | |
| 1819 | err = -EINVAL; | | |
| 1820 | goto out; | | |
| 1821 | } | | |
| 1822 | | | |
| 1823 | mod->mkobj.mod = mod; | | |
| 1824 | | | |
| 1825 | memset(&mod->mkobj.kobj, 0, sizeof(mod->mkobj.kobj)); | | |
| 1826 | mod->mkobj.kobj.kset = module_kset; | | |
| 1827 | err = kobject_init_and_add(&mod->mkobj.kobj, &module_ktype, NULL, | | |
| 1828 | "%s", mod->name); | | |
| 1829 | if (err) | | |
| 1830 | mod_kobject_put(mod); | | |
| 1831 | | | |
| 1832 | out: | | |
| 1833 | return err; | | |
| 1834 | } | | |
| 1835 | | | |
| 1836 | static int mod_sysfs_setup(struct module *mod, | | |
| 1837 | const struct load_info *info, | | |
| 1838 | struct kernel_param *kparam, | | |
| 1839 | unsigned int num_params) | | |
| 1840 | { | | |
| 1841 | int err; | | |
| 1842 | | | |
| 1843 | err = mod_sysfs_init(mod); | | |
| 1844 | if (err) | | |
| 1845 | goto out; | | |
| 1846 | | | |
| 1847 | mod->holders_dir = kobject_create_and_add("holders", &mod->mkobj.kobj); | | |
| 1848 | if (!mod->holders_dir) { | | |
| 1849 | err = -ENOMEM; | | |
| 1850 | goto out_unreg; | | |
| 1851 | } | | |
| 1852 | | | |
| 1853 | err = module_param_sysfs_setup(mod, kparam, num_params); | | |
| 1854 | if (err) | | |
| 1855 | goto out_unreg_holders; | | |
| 1856 | | | |
| 1857 | err = module_add_modinfo_attrs(mod); | | |
| 1858 | if (err) | | |
| 1859 | goto out_unreg_param; | | |
| 1860 | | | |
| 1861 | err = add_usage_links(mod); | | |
| 1862 | if (err) | | |
| 1863 | goto out_unreg_modinfo_attrs; | | |
| 1864 | | | |
| 1865 | add_sect_attrs(mod, info); | | |
| 1866 | add_notes_attrs(mod, info); | | |
| 1867 | | | |
| 1868 | return 0; | | |
| 1869 | | | |
| 1870 | out_unreg_modinfo_attrs: | | |
| 1871 | module_remove_modinfo_attrs(mod, -1); | | |
| 1872 | out_unreg_param: | | |
| 1873 | module_param_sysfs_remove(mod); | | |
| 1874 | out_unreg_holders: | | |
| 1875 | kobject_put(mod->holders_dir); | | |
| 1876 | out_unreg: | | |
| 1877 | mod_kobject_put(mod); | | |
| 1878 | out: | | |
| 1879 | return err; | | |
| 1880 | } | | |
| 1881 | | | |
| 1882 | static void mod_sysfs_fini(struct module *mod) | | |
| 1883 | { | | |
| 1884 | remove_notes_attrs(mod); | | |
| 1885 | remove_sect_attrs(mod); | | |
| 1886 | mod_kobject_put(mod); | | |
| 1887 | } | | |
| 1888 | | | |
| 1889 | static void init_param_lock(struct module *mod) | | |
| 1890 | { | | |
| 1891 | mutex_init(&mod->param_lock); | | |
| 1892 | } | | |
| 1893 | #else /* !CONFIG_SYSFS */ | | |
| 1894 | | | |
| 1895 | static int mod_sysfs_setup(struct module *mod, | | |
| 1896 | const struct load_info *info, | | |
| 1897 | struct kernel_param *kparam, | | |
| 1898 | unsigned int num_params) | | |
| 1899 | { | | |
| 1900 | return 0; | | |
| 1901 | } | | |
| 1902 | | | |
| 1903 | static void mod_sysfs_fini(struct module *mod) | | |
| 1904 | { | | |
| 1905 | } | | |
| 1906 | | | |
| 1907 | static void module_remove_modinfo_attrs(struct module *mod, int end) | | |
| 1908 | { | | |
| 1909 | } | | |
| 1910 | | | |
| 1911 | static void del_usage_links(struct module *mod) | | |
| 1912 | { | | |
| 1913 | } | | |
| 1914 | | | |
| 1915 | static void init_param_lock(struct module *mod) | | |
| 1916 | { | | |
| 1917 | } | | |
| 1918 | #endif /* CONFIG_SYSFS */ | | |
| 1919 | | | |
| 1920 | static void mod_sysfs_teardown(struct module *mod) | | |
| 1921 | { | | |
| 1922 | del_usage_links(mod); | | |
| 1923 | module_remove_modinfo_attrs(mod, -1); | | |
| 1924 | module_param_sysfs_remove(mod); | | |
| 1925 | kobject_put(mod->mkobj.drivers_dir); | | |
| 1926 | kobject_put(mod->holders_dir); | | |
| 1927 | mod_sysfs_fini(mod); | | |
| 1928 | } | | |
| 1929 | | | |
| 1930 | /* | | |
| 1931 | * LKM RO/NX protection: protect module's text/ro-data | | |
| 1932 | * from modification and any data from execution. | | |
| 1933 | * | | |
| 1934 | * General layout of module is: | | |
| 1935 | * [text] [read-only-data] [ro-after-init] [writable data] | | |
| 1936 | * text_size -----^ ^ ^ ^ | | |
| 1937 | * ro_size ------------------------| | | | | |
| 1938 | * ro_after_init_size -----------------------------| | | | |
| 1939 | * size -----------------------------------------------------------| | | |
| 1940 | * | | |
| 1941 | * These values are always page-aligned (as is base) | | |
| 1942 | */ | | |
| 1943 | | | |
| 1944 | /* | | |
| 1945 | * Since some arches are moving towards PAGE_KERNEL module allocations instead | | |
| 1946 | * of PAGE_KERNEL_EXEC, keep frob_text() and module_enable_x() outside of the | | |
| 1947 | * CONFIG_STRICT_MODULE_RWX block below because they are needed regardless of | | |
| 1948 | * whether we are strict. | | |
| 1949 | */ | | |
| 1950 | #ifdef CONFIG_ARCH_HAS_STRICT_MODULE_RWX | | |
| 1951 | static void frob_text(const struct module_layout *layout, | | |
| 1952 | int (*set_memory)(unsigned long start, int num_pages)) | | |
| 1953 | { | | |
| 1954 | BUG_ON((unsigned long)layout->base & (PAGE_SIZE-1)); | | |
| 1955 | BUG_ON((unsigned long)layout->text_size & (PAGE_SIZE-1)); | | |
| 1956 | set_memory((unsigned long)layout->base, | | |
| 1957 | layout->text_size >> PAGE_SHIFT); | | |
| 1958 | } | | |
| 1959 | | | |
| 1960 | static void module_enable_x(const struct module *mod) | | |
| 1961 | { | | |
| 1962 | frob_text(&mod->core_layout, set_memory_x); | | |
| 1963 | frob_text(&mod->init_layout, set_memory_x); | | |
| 1964 | } | | |
| 1965 | #else /* !CONFIG_ARCH_HAS_STRICT_MODULE_RWX */ | | |
| 1966 | static void module_enable_x(const struct module *mod) { } | | |
| 1967 | #endif /* CONFIG_ARCH_HAS_STRICT_MODULE_RWX */ | | |
| 1968 | | | |
| 1969 | #ifdef CONFIG_STRICT_MODULE_RWX | | |
| 1970 | static void frob_rodata(const struct module_layout *layout, | | |
| 1971 | int (*set_memory)(unsigned long start, int num_pages)) | | |
| 1972 | { | | |
| 1973 | BUG_ON((unsigned long)layout->base & (PAGE_SIZE-1)); | | |
| 1974 | BUG_ON((unsigned long)layout->text_size & (PAGE_SIZE-1)); | | |
| 1975 | BUG_ON((unsigned long)layout->ro_size & (PAGE_SIZE-1)); | | |
| 1976 | set_memory((unsigned long)layout->base + layout->text_size, | | |
| 1977 | (layout->ro_size - layout->text_size) >> PAGE_SHIFT); | | |
| 1978 | } | | |
| 1979 | | | |
| 1980 | static void frob_ro_after_init(const struct module_layout *layout, | | |
| 1981 | int (*set_memory)(unsigned long start, int num_pages)) | | |
| 1982 | { | | |
| 1983 | BUG_ON((unsigned long)layout->base & (PAGE_SIZE-1)); | | |
| 1984 | BUG_ON((unsigned long)layout->ro_size & (PAGE_SIZE-1)); | | |
| 1985 | BUG_ON((unsigned long)layout->ro_after_init_size & (PAGE_SIZE-1)); | | |
| 1986 | set_memory((unsigned long)layout->base + layout->ro_size, | | |
| 1987 | (layout->ro_after_init_size - layout->ro_size) >> PAGE_SHIFT); | | |
| 1988 | } | | |
| 1989 | | | |
| 1990 | static void frob_writable_data(const struct module_layout *layout, | | |
| 1991 | int (*set_memory)(unsigned long start, int num_pages)) | | |
| 1992 | { | | |
| 1993 | BUG_ON((unsigned long)layout->base & (PAGE_SIZE-1)); | | |
| 1994 | BUG_ON((unsigned long)layout->ro_after_init_size & (PAGE_SIZE-1)); | | |
| 1995 | BUG_ON((unsigned long)layout->size & (PAGE_SIZE-1)); | | |
| 1996 | set_memory((unsigned long)layout->base + layout->ro_after_init_size, | | |
| 1997 | (layout->size - layout->ro_after_init_size) >> PAGE_SHIFT); | | |
| 1998 | } | | |
| 1999 | | | |
| 2000 | static void module_enable_ro(const struct module *mod, bool after_init) | | |
| 2001 | { | | |
| 2002 | if (!rodata_enabled) | | |
| 2003 | return; | | |
| 2004 | | | |
| 2005 | set_vm_flush_reset_perms(mod->core_layout.base); | | |
| 2006 | set_vm_flush_reset_perms(mod->init_layout.base); | | |
| 2007 | frob_text(&mod->core_layout, set_memory_ro); | | |
| 2008 | | | |
| 2009 | frob_rodata(&mod->core_layout, set_memory_ro); | | |
| 2010 | frob_text(&mod->init_layout, set_memory_ro); | | |
| 2011 | frob_rodata(&mod->init_layout, set_memory_ro); | | |
| 2012 | | | |
| 2013 | if (after_init) | | |
| 2014 | frob_ro_after_init(&mod->core_layout, set_memory_ro); | | |
| 2015 | } | | |
| 2016 | | | |
| 2017 | static void module_enable_nx(const struct module *mod) | | |
| 2018 | { | | |
| 2019 | frob_rodata(&mod->core_layout, set_memory_nx); | | |
| 2020 | frob_ro_after_init(&mod->core_layout, set_memory_nx); | | |
| 2021 | frob_writable_data(&mod->core_layout, set_memory_nx); | | |
| 2022 | frob_rodata(&mod->init_layout, set_memory_nx); | | |
| 2023 | frob_writable_data(&mod->init_layout, set_memory_nx); | | |
| 2024 | } | | |
| 2025 | | | |
| 2026 | static int module_enforce_rwx_sections(Elf_Ehdr *hdr, Elf_Shdr *sechdrs, | | |
| 2027 | char *secstrings, struct module *mod) | | |
| 2028 | { | | |
| 2029 | const unsigned long shf_wx = SHF_WRITE|SHF_EXECINSTR; | | |
| 2030 | int i; | | |
| 2031 | | | |
| 2032 | for (i = 0; i < hdr->e_shnum; i++) { | | |
| 2033 | if ((sechdrs[i].sh_flags & shf_wx) == shf_wx) { | | |
| 2034 | pr_err("%s: section %s (index %d) has invalid WRITE|EXEC flags\n", | | |
| 2035 | mod->name, secstrings + sechdrs[i].sh_name, i); | | |
| 2036 | return -ENOEXEC; | | |
| 2037 | } | | |
| 2038 | } | | |
| 2039 | | | |
| 2040 | return 0; | | |
| 2041 | } | | |
| 2042 | | | |
| 2043 | #else /* !CONFIG_STRICT_MODULE_RWX */ | | |
| 2044 | static void module_enable_nx(const struct module *mod) { } | | |
| 2045 | static void module_enable_ro(const struct module *mod, bool after_init) {} | | |
| 2046 | static int module_enforce_rwx_sections(Elf_Ehdr *hdr, Elf_Shdr *sechdrs, | | |
| 2047 | char *secstrings, struct module *mod) | | |
| 2048 | { | | |
| 2049 | return 0; | | |
| 2050 | } | | |
| 2051 | #endif /* CONFIG_STRICT_MODULE_RWX */ | | |
| 2052 | | | |
| 2053 | #ifdef CONFIG_LIVEPATCH | | |
| 2054 | /* | | |
| 2055 | * Persist Elf information about a module. Copy the Elf header, | | |
| 2056 | * section header table, section string table, and symtab section | | |
| 2057 | * index from info to mod->klp_info. | | |
| 2058 | */ | | |
| 2059 | static int copy_module_elf(struct module *mod, struct load_info *info) | | |
| 2060 | { | | |
| 2061 | unsigned int size, symndx; | | |
| 2062 | int ret; | | |
| 2063 | | | |
| 2064 | size = sizeof(*mod->klp_info); | | |
| 2065 | mod->klp_info = kmalloc(size, GFP_KERNEL); | | |
| 2066 | if (mod->klp_info == NULL) | | |
| 2067 | return -ENOMEM; | | |
| 2068 | | | |
| 2069 | /* Elf header */ | | |
| 2070 | size = sizeof(mod->klp_info->hdr); | | |
| 2071 | memcpy(&mod->klp_info->hdr, info->hdr, size); | | |
| 2072 | | | |
| 2073 | /* Elf section header table */ | | |
| 2074 | size = sizeof(*info->sechdrs) * info->hdr->e_shnum; | | |
| 2075 | mod->klp_info->sechdrs = kmemdup(info->sechdrs, size, GFP_KERNEL); | | |
| 2076 | if (mod->klp_info->sechdrs == NULL) { | | |
| 2077 | ret = -ENOMEM; | | |
| 2078 | goto free_info; | | |
| 2079 | } | | |
| 2080 | | | |
| 2081 | /* Elf section name string table */ | | |
| 2082 | size = info->sechdrs[info->hdr->e_shstrndx].sh_size; | | |
| 2083 | mod->klp_info->secstrings = kmemdup(info->secstrings, size, GFP_KERNEL); | | |
| 2084 | if (mod->klp_info->secstrings == NULL) { | | |
| 2085 | ret = -ENOMEM; | | |
| 2086 | goto free_sechdrs; | | |
| 2087 | } | | |
| 2088 | | | |
| 2089 | /* Elf symbol section index */ | | |
| 2090 | symndx = info->index.sym; | | |
| 2091 | mod->klp_info->symndx = symndx; | | |
| 2092 | | | |
| 2093 | /* | | |
| 2094 | * For livepatch modules, core_kallsyms.symtab is a complete | | |
| 2095 | * copy of the original symbol table. Adjust sh_addr to point | | |
| 2096 | * to core_kallsyms.symtab since the copy of the symtab in module | | |
| 2097 | * init memory is freed at the end of do_init_module(). | | |
| 2098 | */ | | |
| 2099 | mod->klp_info->sechdrs[symndx].sh_addr = \ | | |
| 2100 | (unsigned long) mod->core_kallsyms.symtab; | | |
| 2101 | | | |
| 2102 | return 0; | | |
| 2103 | | | |
| 2104 | free_sechdrs: | | |
| 2105 | kfree(mod->klp_info->sechdrs); | | |
| 2106 | free_info: | | |
| 2107 | kfree(mod->klp_info); | | |
| 2108 | return ret; | | |
| 2109 | } | | |
| 2110 | | | |
| 2111 | static void free_module_elf(struct module *mod) | | |
| 2112 | { | | |
| 2113 | kfree(mod->klp_info->sechdrs); | | |
| 2114 | kfree(mod->klp_info->secstrings); | | |
| 2115 | kfree(mod->klp_info); | | |
| 2116 | } | | |
| 2117 | #else /* !CONFIG_LIVEPATCH */ | | |
| 2118 | static int copy_module_elf(struct module *mod, struct load_info *info) | | |
| 2119 | { | | |
| 2120 | return 0; | | |
| 2121 | } | | |
| 2122 | | | |
| 2123 | static void free_module_elf(struct module *mod) | | |
| 2124 | { | | |
| 2125 | } | | |
| 2126 | #endif /* CONFIG_LIVEPATCH */ | | |
| 2127 | | | |
| 2128 | void __weak module_memfree(void *module_region) | | |
| 2129 | { | | |
| 2130 | /* | | |
| 2131 | * This memory may be RO, and freeing RO memory in an interrupt is not | | |
| 2132 | * supported by vmalloc. | | |
| 2133 | */ | | |
| 2134 | WARN_ON(in_interrupt()); | | |
| 2135 | vfree(module_region); | | |
| 2136 | } | | |
| 2137 | | | |
| 2138 | void __weak module_arch_cleanup(struct module *mod) | | |
| 2139 | { | | |
| 2140 | } | | |
| 2141 | | | |
| 2142 | void __weak module_arch_freeing_init(struct module *mod) | | |
| 2143 | { | | |
| 2144 | } | | |
| 2145 | | | |
| 2146 | static void cfi_cleanup(struct module *mod); | | |
| 2147 | | | |
| 2148 | /* Free a module, remove from lists, etc. */ | | |
| 2149 | static void free_module(struct module *mod) | | |
| 2150 | { | | |
| 2151 | trace_module_free(mod); | | |
| 2152 | | | |
| 2153 | mod_sysfs_teardown(mod); | | |
| 2154 | | | |
| 2155 | /* | | |
| 2156 | * We leave it in list to prevent duplicate loads, but make sure | | |
| 2157 | * that noone uses it while it's being deconstructed. | | |
| 2158 | */ | | |
| 2159 | mutex_lock(&module_mutex); | | |
| 2160 | mod->state = MODULE_STATE_UNFORMED; | | |
| 2161 | mutex_unlock(&module_mutex); | | |
| 2162 | | | |
| 2163 | /* Remove dynamic debug info */ | | |
| 2164 | ddebug_remove_module(mod->name); | | |
| 2165 | | | |
| 2166 | /* Arch-specific cleanup. */ | | |
| 2167 | module_arch_cleanup(mod); | | |
| 2168 | | | |
| 2169 | /* Module unload stuff */ | | |
| 2170 | module_unload_free(mod); | | |
| 2171 | | | |
| 2172 | /* Free any allocated parameters. */ | | |
| 2173 | destroy_params(mod->kp, mod->num_kp); | | |
| 2174 | | | |
| 2175 | if (is_livepatch_module(mod)) | | |
| 2176 | free_module_elf(mod); | | |
| 2177 | | | |
| 2178 | /* Now we can delete it from the lists */ | | |
| 2179 | mutex_lock(&module_mutex); | | |
| 2180 | /* Unlink carefully: kallsyms could be walking list. */ | | |
| 2181 | list_del_rcu(&mod->list); | | |
| 2182 | mod_tree_remove(mod); | | |
| 2183 | /* Remove this module from bug list, this uses list_del_rcu */ | | |
| 2184 | module_bug_cleanup(mod); | | |
| 2185 | /* Wait for RCU-sched synchronizing before releasing mod->list and buglist. */ | | |
| 2186 | synchronize_rcu(); | | |
| 2187 | mutex_unlock(&module_mutex); | | |
| 2188 | | 1187 | |
| 2189 | /* Clean up CFI for the module. */ | 1188 | /* Clean up CFI for the module. */ |
| 2190 | cfi_cleanup(mod); | 1189 | cfi_cleanup(mod); |
| @@ -2196,10 +1195,13 @@ static void free_module(struct module *mod) · 5 unmodified lines |
| 2196 | percpu_modfree(mod); | 1195 | percpu_modfree(mod); |
| 2197 | | 1196 | |
| 2198 | /* Free lock-classes; relies on the preceding sync_rcu(). */ | 1197 | /* Free lock-classes; relies on the preceding sync_rcu(). */ |
| 2199 | lockdep_free_key_range(mod->core_layout.base, mod->core_layout.size); | 1198 | lockdep_free_key_range(mod->data_layout.base, mod->data_layout.size); |
| 2200 | | 1199 | |
| 2201 | /* Finally, free the core (containing the module structure) */ | 1200 | /* Finally, free the core (containing the module structure) */ |
| 2202 | module_memfree(mod->core_layout.base); | 1201 | module_memfree(mod->core_layout.base); |
| | 1202 | #ifdef CONFIG_ARCH_WANTS_MODULES_DATA_IN_VMALLOC |
| | 1203 | vfree(mod->data_layout.base); |
| | 1204 | #endif |
| 2203 | } | 1205 | } |
| 2204 | | 1206 | |
| 2205 | void *__symbol_get(const char *symbol) | 1207 | void *__symbol_get(const char *symbol) |
| @@ -2387,7 +1389,7 @@ unsigned int __weak arch_mod_section_prepend(struct module *mod, · 181 unmodified lines |
| 2387 | } | 1389 | } |
| 2388 | | 1390 | |
| 2389 | /* Update size with this section: return offset. */ | 1391 | /* Update size with this section: return offset. */ |
| 2390 | static long get_offset(struct module *mod, unsigned int *size, | 1392 | long module_get_offset(struct module *mod, unsigned int *size, |
| 2391 | Elf_Shdr *sechdr, unsigned int section) | 1393 | Elf_Shdr *sechdr, unsigned int section) |
| 2392 | { | 1394 | { |
| 2393 | long ret; | 1395 | long ret; |
| @@ -2437,30 +1439,32 @@ static void layout_sections(struct module *mod, struct load_info *info) · 43 unmodified lines |
| 2437 | for (i = 0; i < info->hdr->e_shnum; ++i) { | 1439 | for (i = 0; i < info->hdr->e_shnum; ++i) { |
| 2438 | Elf_Shdr *s = &info->sechdrs[i]; | 1440 | Elf_Shdr *s = &info->sechdrs[i]; |
| 2439 | const char *sname = info->secstrings + s->sh_name; | 1441 | const char *sname = info->secstrings + s->sh_name; |
| | 1442 | unsigned int *sizep; |
| 2440 | | 1443 | |
| 2441 | if ((s->sh_flags & masks[m][0]) != masks[m][0] | 1444 | if ((s->sh_flags & masks[m][0]) != masks[m][0] |
| 2442 | || (s->sh_flags & masks[m][1]) | 1445 | || (s->sh_flags & masks[m][1]) |
| 2443 | || s->sh_entsize != ~0UL | 1446 | || s->sh_entsize != ~0UL |
| 2444 | || module_init_layout_section(sname)) | 1447 | || module_init_layout_section(sname)) |
| 2445 | continue; | 1448 | continue; |
| 2446 | s->sh_entsize = get_offset(mod, &mod->core_layout.size, s, i); | 1449 | sizep = m ? &mod->data_layout.size : &mod->core_layout.size; |
| | 1450 | s->sh_entsize = module_get_offset(mod, sizep, s, i); |
| 2447 | pr_debug("\t%s\n", sname); | 1451 | pr_debug("\t%s\n", sname); |
| 2448 | } | 1452 | } |
| 2449 | switch (m) { | 1453 | switch (m) { |
| 2450 | case 0: /* executable */ | 1454 | case 0: /* executable */ |
| 2451 | mod->core_layout.size = debug_align(mod->core_layout.size); | 1455 | mod->core_layout.size = strict_align(mod->core_layout.size); |
| 2452 | mod->core_layout.text_size = mod->core_layout.size; | 1456 | mod->core_layout.text_size = mod->core_layout.size; |
| 2453 | break; | 1457 | break; |
| 2454 | case 1: /* RO: text and ro-data */ | 1458 | case 1: /* RO: text and ro-data */ |
| 2455 | mod->core_layout.size = debug_align(mod->core_layout.size); | 1459 | mod->data_layout.size = strict_align(mod->data_layout.size); |
| 2456 | mod->core_layout.ro_size = mod->core_layout.size; | 1460 | mod->data_layout.ro_size = mod->data_layout.size; |
| 2457 | break; | 1461 | break; |
| 2458 | case 2: /* RO after init */ | 1462 | case 2: /* RO after init */ |
| 2459 | mod->core_layout.size = debug_align(mod->core_layout.size); | 1463 | mod->data_layout.size = strict_align(mod->data_layout.size); |
| 2460 | mod->core_layout.ro_after_init_size = mod->core_layout.size; | 1464 | mod->data_layout.ro_after_init_size = mod->data_layout.size; |
| 2461 | break; | 1465 | break; |
| 2462 | case 4: /* whole core */ | 1466 | case 4: /* whole core */ |
| 2463 | mod->core_layout.size = debug_align(mod->core_layout.size); | 1467 | mod->data_layout.size = strict_align(mod->data_layout.size); |
| 2464 | break; | 1468 | break; |
| 2465 | } | 1469 | } |
| 2466 | } | 1470 | } |
| @@ -2476,17 +1480,17 @@ static void layout_sections(struct module *mod, struct load_info *info) · 9 unmodified lines |
| 2476 | || s->sh_entsize != ~0UL | 1480 | || s->sh_entsize != ~0UL |
| 2477 | || !module_init_layout_section(sname)) | 1481 | || !module_init_layout_section(sname)) |
| 2478 | continue; | 1482 | continue; |
| 2479 | s->sh_entsize = (get_offset(mod, &mod->init_layout.size, s, i) | 1483 | s->sh_entsize = (module_get_offset(mod, &mod->init_layout.size, s, i) |
| 2480 | | INIT_OFFSET_MASK); | 1484 | | INIT_OFFSET_MASK); |
| 2481 | pr_debug("\t%s\n", sname); | 1485 | pr_debug("\t%s\n", sname); |
| 2482 | } | 1486 | } |
| 2483 | switch (m) { | 1487 | switch (m) { |
| 2484 | case 0: /* executable */ | 1488 | case 0: /* executable */ |
| 2485 | mod->init_layout.size = debug_align(mod->init_layout.size); | 1489 | mod->init_layout.size = strict_align(mod->init_layout.size); |
| 2486 | mod->init_layout.text_size = mod->init_layout.size; | 1490 | mod->init_layout.text_size = mod->init_layout.size; |
| 2487 | break; | 1491 | break; |
| 2488 | case 1: /* RO: text and ro-data */ | 1492 | case 1: /* RO: text and ro-data */ |
| 2489 | mod->init_layout.size = debug_align(mod->init_layout.size); | 1493 | mod->init_layout.size = strict_align(mod->init_layout.size); |
| 2490 | mod->init_layout.ro_size = mod->init_layout.size; | 1494 | mod->init_layout.ro_size = mod->init_layout.size; |
| 2491 | break; | 1495 | break; |
| 2492 | case 2: | 1496 | case 2: |
| @@ -2497,7 +1501,7 @@ static void layout_sections(struct module *mod, struct load_info *info) · 4 unmodified lines |
| 2497 | mod->init_layout.ro_after_init_size = mod->init_layout.ro_size; | 1501 | mod->init_layout.ro_after_init_size = mod->init_layout.ro_size; |
| 2498 | break; | 1502 | break; |
| 2499 | case 4: /* whole init */ | 1503 | case 4: /* whole init */ |
| 2500 | mod->init_layout.size = debug_align(mod->init_layout.size); | 1504 | mod->init_layout.size = strict_align(mod->init_layout.size); |
| 2501 | break; | 1505 | break; |
| 2502 | } | 1506 | } |
| 2503 | } | 1507 | } |
| @@ -2587,350 +1591,37 @@ static void free_modinfo(struct module *mod) · 83 unmodified lines |
| 2587 | if (attr->free) | 1591 | if (attr->free) |
| 2588 | attr->free(mod); | 1592 | attr->free(mod); |
| 2589 | } | 1593 | } |
| 2590 | } | 1594 | } |
| 2591 | | | |
| 2592 | #ifdef CONFIG_KALLSYMS | | |
| 2593 | | | |
| 2594 | /* Lookup exported symbol in given range of kernel_symbols */ | | |
| 2595 | static const struct kernel_symbol *lookup_exported_symbol(const char *name, | | |
| 2596 | const struct kernel_symbol *start, | | |
| 2597 | const struct kernel_symbol *stop) | | |
| 2598 | { | | |
| 2599 | return bsearch(name, start, stop - start, | | |
| 2600 | sizeof(struct kernel_symbol), cmp_name); | | |
| 2601 | } | | |
| 2602 | | | |
| 2603 | static int is_exported(const char *name, unsigned long value, | | |
| 2604 | const struct module *mod) | | |
| 2605 | { | | |
| 2606 | const struct kernel_symbol *ks; | | |
| 2607 | if (!mod) | | |
| 2608 | ks = lookup_exported_symbol(name, __start___ksymtab, __stop___ksymtab); | | |
| 2609 | else | | |
| 2610 | ks = lookup_exported_symbol(name, mod->syms, mod->syms + mod->num_syms); | | |
| 2611 | | | |
| 2612 | return ks != NULL && kernel_symbol_value(ks) == value; | | |
| 2613 | } | | |
| 2614 | | | |
| 2615 | /* As per nm */ | | |
| 2616 | static char elf_type(const Elf_Sym *sym, const struct load_info *info) | | |
| 2617 | { | | |
| 2618 | const Elf_Shdr *sechdrs = info->sechdrs; | | |
| 2619 | | | |
| 2620 | if (ELF_ST_BIND(sym->st_info) == STB_WEAK) { | | |
| 2621 | if (ELF_ST_TYPE(sym->st_info) == STT_OBJECT) | | |
| 2622 | return 'v'; | | |
| 2623 | else | | |
| 2624 | return 'w'; | | |
| 2625 | } | | |
| 2626 | if (sym->st_shndx == SHN_UNDEF) | | |
| 2627 | return 'U'; | | |
| 2628 | if (sym->st_shndx == SHN_ABS || sym->st_shndx == info->index.pcpu) | | |
| 2629 | return 'a'; | | |
| 2630 | if (sym->st_shndx >= SHN_LORESERVE) | | |
| 2631 | return '?'; | | |
| 2632 | if (sechdrs[sym->st_shndx].sh_flags & SHF_EXECINSTR) | | |
| 2633 | return 't'; | | |
| 2634 | if (sechdrs[sym->st_shndx].sh_flags & SHF_ALLOC | | |
| 2635 | && sechdrs[sym->st_shndx].sh_type != SHT_NOBITS) { | | |
| 2636 | if (!(sechdrs[sym->st_shndx].sh_flags & SHF_WRITE)) | | |
| 2637 | return 'r'; | | |
| 2638 | else if (sechdrs[sym->st_shndx].sh_flags & ARCH_SHF_SMALL) | | |
| 2639 | return 'g'; | | |
| 2640 | else | | |
| 2641 | return 'd'; | | |
| 2642 | } | | |
| 2643 | if (sechdrs[sym->st_shndx].sh_type == SHT_NOBITS) { | | |
| 2644 | if (sechdrs[sym->st_shndx].sh_flags & ARCH_SHF_SMALL) | | |
| 2645 | return 's'; | | |
| 2646 | else | | |
| 2647 | return 'b'; | | |
| 2648 | } | | |
| 2649 | if (strstarts(info->secstrings + sechdrs[sym->st_shndx].sh_name, | | |
| 2650 | ".debug")) { | | |
| 2651 | return 'n'; | | |
| 2652 | } | | |
| 2653 | return '?'; | | |
| 2654 | } | | |
| 2655 | | | |
| 2656 | static bool is_core_symbol(const Elf_Sym *src, const Elf_Shdr *sechdrs, | | |
| 2657 | unsigned int shnum, unsigned int pcpundx) | | |
| 2658 | { | | |
| 2659 | const Elf_Shdr *sec; | | |
| 2660 | | | |
| 2661 | if (src->st_shndx == SHN_UNDEF | | |
| 2662 | || src->st_shndx >= shnum | | |
| 2663 | || !src->st_name) | | |
| 2664 | return false; | | |
| 2665 | | | |
| 2666 | #ifdef CONFIG_KALLSYMS_ALL | | |
| 2667 | if (src->st_shndx == pcpundx) | | |
| 2668 | return true; | | |
| 2669 | #endif | | |
| 2670 | | | |
| 2671 | sec = sechdrs + src->st_shndx; | | |
| 2672 | if (!(sec->sh_flags & SHF_ALLOC) | | |
| 2673 | #ifndef CONFIG_KALLSYMS_ALL | | |
| 2674 | || !(sec->sh_flags & SHF_EXECINSTR) | | |
| 2675 | #endif | | |
| 2676 | || (sec->sh_entsize & INIT_OFFSET_MASK)) | | |
| 2677 | return false; | | |
| 2678 | | | |
| 2679 | return true; | | |
| 2680 | } | | |
| 2681 | | | |
| 2682 | /* | | |
| 2683 | * We only allocate and copy the strings needed by the parts of symtab | | |
| 2684 | * we keep. This is simple, but has the effect of making multiple | | |
| 2685 | * copies of duplicates. We could be more sophisticated, see | | |
| 2686 | * linux-kernel thread starting with | | |
| 2687 | * <73defb5e4bca04a6431392cc341112b1@localhost>. | | |
| 2688 | */ | | |
| 2689 | static void layout_symtab(struct module *mod, struct load_info *info) | | |
| 2690 | { | | |
| 2691 | Elf_Shdr *symsect = info->sechdrs + info->index.sym; | | |
| 2692 | Elf_Shdr *strsect = info->sechdrs + info->index.str; | | |
| 2693 | const Elf_Sym *src; | | |
| 2694 | unsigned int i, nsrc, ndst, strtab_size = 0; | | |
| 2695 | | | |
| 2696 | /* Put symbol section at end of init part of module. */ | | |
| 2697 | symsect->sh_flags |= SHF_ALLOC; | | |
| 2698 | symsect->sh_entsize = get_offset(mod, &mod->init_layout.size, symsect, | | |
| 2699 | info->index.sym) | INIT_OFFSET_MASK; | | |
| 2700 | pr_debug("\t%s\n", info->secstrings + symsect->sh_name); | | |
| 2701 | | | |
| 2702 | src = (void *)info->hdr + symsect->sh_offset; | | |
| 2703 | nsrc = symsect->sh_size / sizeof(*src); | | |
| 2704 | | | |
| 2705 | /* Compute total space required for the core symbols' strtab. */ | | |
| 2706 | for (ndst = i = 0; i < nsrc; i++) { | | |
| 2707 | if (i == 0 || is_livepatch_module(mod) || | | |
| 2708 | is_core_symbol(src+i, info->sechdrs, info->hdr->e_shnum, | | |
| 2709 | info->index.pcpu)) { | | |
| 2710 | strtab_size += strlen(&info->strtab[src[i].st_name])+1; | | |
| 2711 | ndst++; | | |
| 2712 | } | | |
| 2713 | } | | |
| 2714 | | | |
| 2715 | /* Append room for core symbols at end of core part. */ | | |
| 2716 | info->symoffs = ALIGN(mod->core_layout.size, symsect->sh_addralign ?: 1); | | |
| 2717 | info->stroffs = mod->core_layout.size = info->symoffs + ndst * sizeof(Elf_Sym); | | |
| 2718 | mod->core_layout.size += strtab_size; | | |
| 2719 | info->core_typeoffs = mod->core_layout.size; | | |
| 2720 | mod->core_layout.size += ndst * sizeof(char); | | |
| 2721 | mod->core_layout.size = debug_align(mod->core_layout.size); | | |
| 2722 | | | |
| 2723 | /* Put string table section at end of init part of module. */ | | |
| 2724 | strsect->sh_flags |= SHF_ALLOC; | | |
| 2725 | strsect->sh_entsize = get_offset(mod, &mod->init_layout.size, strsect, | | |
| 2726 | info->index.str) | INIT_OFFSET_MASK; | | |
| 2727 | pr_debug("\t%s\n", info->secstrings + strsect->sh_name); | | |
| 2728 | | | |
| 2729 | /* We'll tack temporary mod_kallsyms on the end. */ | | |
| 2730 | mod->init_layout.size = ALIGN(mod->init_layout.size, | | |
| 2731 | __alignof__(struct mod_kallsyms)); | | |
| 2732 | info->mod_kallsyms_init_off = mod->init_layout.size; | | |
| 2733 | mod->init_layout.size += sizeof(struct mod_kallsyms); | | |
| 2734 | info->init_typeoffs = mod->init_layout.size; | | |
| 2735 | mod->init_layout.size += nsrc * sizeof(char); | | |
| 2736 | mod->init_layout.size = debug_align(mod->init_layout.size); | | |
| 2737 | } | | |
| 2738 | | | |
| 2739 | /* | | |
| 2740 | * We use the full symtab and strtab which layout_symtab arranged to | | |
| 2741 | * be appended to the init section. Later we switch to the cut-down | | |
| 2742 | * core-only ones. | | |
| 2743 | */ | | |
| 2744 | static void add_kallsyms(struct module *mod, const struct load_info *info) | | |
| 2745 | { | | |
| 2746 | unsigned int i, ndst; | | |
| 2747 | const Elf_Sym *src; | | |
| 2748 | Elf_Sym *dst; | | |
| 2749 | char *s; | | |
| 2750 | Elf_Shdr *symsec = &info->sechdrs[info->index.sym]; | | |
| 2751 | | | |
| 2752 | /* Set up to point into init section. */ | | |
| 2753 | mod->kallsyms = mod->init_layout.base + info->mod_kallsyms_init_off; | | |
| 2754 | | | |
| 2755 | mod->kallsyms->symtab = (void *)symsec->sh_addr; | | |
| 2756 | mod->kallsyms->num_symtab = symsec->sh_size / sizeof(Elf_Sym); | | |
| 2757 | /* Make sure we get permanent strtab: don't use info->strtab. */ | | |
| 2758 | mod->kallsyms->strtab = (void *)info->sechdrs[info->index.str].sh_addr; | | |
| 2759 | mod->kallsyms->typetab = mod->init_layout.base + info->init_typeoffs; | | |
| 2760 | | | |
| 2761 | /* | | |
| 2762 | * Now populate the cut down core kallsyms for after init | | |
| 2763 | * and set types up while we still have access to sections. | | |
| 2764 | */ | | |
| 2765 | mod->core_kallsyms.symtab = dst = mod->core_layout.base + info->symoffs; | | |
| 2766 | mod->core_kallsyms.strtab = s = mod->core_layout.base + info->stroffs; | | |
| 2767 | mod->core_kallsyms.typetab = mod->core_layout.base + info->core_typeoffs; | | |
| 2768 | src = mod->kallsyms->symtab; | | |
| 2769 | for (ndst = i = 0; i < mod->kallsyms->num_symtab; i++) { | | |
| 2770 | mod->kallsyms->typetab[i] = elf_type(src + i, info); | | |
| 2771 | if (i == 0 || is_livepatch_module(mod) || | | |
| 2772 | is_core_symbol(src+i, info->sechdrs, info->hdr->e_shnum, | | |
| 2773 | info->index.pcpu)) { | | |
| 2774 | mod->core_kallsyms.typetab[ndst] = | | |
| 2775 | mod->kallsyms->typetab[i]; | | |
| 2776 | dst[ndst] = src[i]; | | |
| 2777 | dst[ndst++].st_name = s - mod->core_kallsyms.strtab; | | |
| 2778 | s += strlcpy(s, &mod->kallsyms->strtab[src[i].st_name], | | |
| 2779 | KSYM_NAME_LEN) + 1; | | |
| 2780 | } | | |
| 2781 | } | | |
| 2782 | mod->core_kallsyms.num_symtab = ndst; | | |
| 2783 | } | | |
| 2784 | #else | | |
| 2785 | static inline void layout_symtab(struct module *mod, struct load_info *info) | | |
| 2786 | { | | |
| 2787 | } | | |
| 2788 | | | |
| 2789 | static void add_kallsyms(struct module *mod, const struct load_info *info) | | |
| 2790 | { | | |
| 2791 | } | | |
| 2792 | #endif /* CONFIG_KALLSYMS */ | | |
| 2793 | | | |
| 2794 | #if IS_ENABLED(CONFIG_KALLSYMS) && IS_ENABLED(CONFIG_STACKTRACE_BUILD_ID) | | |
| 2795 | static void init_build_id(struct module *mod, const struct load_info *info) | | |
| 2796 | { | | |
| 2797 | const Elf_Shdr *sechdr; | | |
| 2798 | unsigned int i; | | |
| 2799 | | | |
| 2800 | for (i = 0; i < info->hdr->e_shnum; i++) { | | |
| 2801 | sechdr = &info->sechdrs[i]; | | |
| 2802 | if (!sect_empty(sechdr) && sechdr->sh_type == SHT_NOTE && | | |
| 2803 | !build_id_parse_buf((void *)sechdr->sh_addr, mod->build_id, | | |
| 2804 | sechdr->sh_size)) | | |
| 2805 | break; | | |
| 2806 | } | | |
| 2807 | } | | |
| 2808 | #else | | |
| 2809 | static void init_build_id(struct module *mod, const struct load_info *info) | | |
| 2810 | { | | |
| 2811 | } | | |
| 2812 | #endif | | |
| 2813 | | | |
| 2814 | static void dynamic_debug_setup(struct module *mod, struct _ddebug *debug, unsigned int num) | | |
| 2815 | { | | |
| 2816 | if (!debug) | | |
| 2817 | return; | | |
| 2818 | ddebug_add_module(debug, num, mod->name); | | |
| 2819 | } | | |
| 2820 | | | |
| 2821 | static void dynamic_debug_remove(struct module *mod, struct _ddebug *debug) | | |
| 2822 | { | | |
| 2823 | if (debug) | | |
| 2824 | ddebug_remove_module(mod->name); | | |
| 2825 | } | | |
| 2826 | | | |
| 2827 | void * __weak module_alloc(unsigned long size) | | |
| 2828 | { | | |
| 2829 | return __vmalloc_node_range(size, 1, VMALLOC_START, VMALLOC_END, | | |
| 2830 | GFP_KERNEL, PAGE_KERNEL_EXEC, VM_FLUSH_RESET_PERMS, | | |
| 2831 | NUMA_NO_NODE, __builtin_return_address(0)); | | |
| 2832 | } | | |
| 2833 | | | |
| 2834 | bool __weak module_init_section(const char *name) | | |
| 2835 | { | | |
| 2836 | return strstarts(name, ".init"); | | |
| 2837 | } | | |
| 2838 | | | |
| 2839 | bool __weak module_exit_section(const char *name) | | |
| 2840 | { | | |
| 2841 | return strstarts(name, ".exit"); | | |
| 2842 | } | | |
| 2843 | | | |
| 2844 | #ifdef CONFIG_DEBUG_KMEMLEAK | | |
| 2845 | static void kmemleak_load_module(const struct module *mod, | | |
| 2846 | const struct load_info *info) | | |
| 2847 | { | | |
| 2848 | unsigned int i; | | |
| 2849 | | | |
| 2850 | /* only scan the sections containing data */ | | |
| 2851 | kmemleak_scan_area(mod, sizeof(struct module), GFP_KERNEL); | | |
| 2852 | | | |
| 2853 | for (i = 1; i < info->hdr->e_shnum; i++) { | | |
| 2854 | /* Scan all writable sections that's not executable */ | | |
| 2855 | if (!(info->sechdrs[i].sh_flags & SHF_ALLOC) || | | |
| 2856 | !(info->sechdrs[i].sh_flags & SHF_WRITE) || | | |
| 2857 | (info->sechdrs[i].sh_flags & SHF_EXECINSTR)) | | |
| 2858 | continue; | | |
| 2859 | | | |
| 2860 | kmemleak_scan_area((void *)info->sechdrs[i].sh_addr, | | |
| 2861 | info->sechdrs[i].sh_size, GFP_KERNEL); | | |
| 2862 | } | | |
| 2863 | } | | |
| 2864 | #else | | |
| 2865 | static inline void kmemleak_load_module(const struct module *mod, | | |
| 2866 | const struct load_info *info) | | |
| 2867 | { | | |
| 2868 | } | | |
| 2869 | #endif | | |
| 2870 | | | |
| 2871 | #ifdef CONFIG_MODULE_SIG | | |
| 2872 | static int module_sig_check(struct load_info *info, int flags) | | |
| 2873 | { | | |
| 2874 | int err = -ENODATA; | | |
| 2875 | const unsigned long markerlen = sizeof(MODULE_SIG_STRING) - 1; | | |
| 2876 | const char *reason; | | |
| 2877 | const void *mod = info->hdr; | | |
| 2878 | bool mangled_module = flags & (MODULE_INIT_IGNORE_MODVERSIONS | | | |
| 2879 | MODULE_INIT_IGNORE_VERMAGIC); | | |
| 2880 | /* | | |
| 2881 | * Do not allow mangled modules as a module with version information | | |
| 2882 | * removed is no longer the module that was signed. | | |
| 2883 | */ | | |
| 2884 | if (!mangled_module && | | |
| 2885 | info->len > markerlen && | | |
| 2886 | memcmp(mod + info->len - markerlen, MODULE_SIG_STRING, markerlen) == 0) { | | |
| 2887 | /* We truncate the module to discard the signature */ | | |
| 2888 | info->len -= markerlen; | | |
| 2889 | err = mod_verify_sig(mod, info); | | |
| 2890 | if (!err) { | | |
| 2891 | info->sig_ok = true; | | |
| 2892 | return 0; | | |
| 2893 | } | | |
| 2894 | } | | |
| 2895 | | 1595 | |
| 2896 | /* | 1596 | static void dynamic_debug_setup(struct module *mod, struct _ddebug *debug, unsigned int num) |
| 2897 | * We don't permit modules to be loaded into the trusted kernels | 1597 | { |
| 2898 | * without a valid signature on them, but if we're not enforcing, | 1598 | if (!debug) |
| 2899 | * certain errors are non-fatal. | 1599 | return; |
| 2900 | */ | 1600 | ddebug_add_module(debug, num, mod->name); |
| 2901 | switch (err) { | 1601 | } |
| 2902 | case -ENODATA: | | |
| 2903 | reason = "unsigned module"; | | |
| 2904 | break; | | |
| 2905 | case -ENOPKG: | | |
| 2906 | reason = "module with unsupported crypto"; | | |
| 2907 | break; | | |
| 2908 | case -ENOKEY: | | |
| 2909 | reason = "module with unavailable key"; | | |
| 2910 | break; | | |
| 2911 | | 1602 | |
| 2912 | default: | 1603 | static void dynamic_debug_remove(struct module *mod, struct _ddebug *debug) |
| 2913 | /* | 1604 | { |
| 2914 | * All other errors are fatal, including lack of memory, | 1605 | if (debug) |
| 2915 | * unparseable signatures, and signature check failures -- | 1606 | ddebug_remove_module(mod->name); |
| 2916 | * even if signatures aren't required. | 1607 | } |
| 2917 | */ | | |
| 2918 | return err; | | |
| 2919 | } | | |
| 2920 | | 1608 | |
| 2921 | if (is_module_sig_enforced()) { | 1609 | void * __weak module_alloc(unsigned long size) |
| 2922 | pr_notice("Loading of %s is rejected\n", reason); | 1610 | { |
| 2923 | return -EKEYREJECTED; | 1611 | return __vmalloc_node_range(size, 1, VMALLOC_START, VMALLOC_END, |
| 2924 | } | 1612 | GFP_KERNEL, PAGE_KERNEL_EXEC, VM_FLUSH_RESET_PERMS, |
| | 1613 | NUMA_NO_NODE, __builtin_return_address(0)); |
| | 1614 | } |
| 2925 | | 1615 | |
| 2926 | return security_locked_down(LOCKDOWN_MODULE_SIGNATURE); | 1616 | bool __weak module_init_section(const char *name) |
| | 1617 | { |
| | 1618 | return strstarts(name, ".init"); |
| 2927 | } | 1619 | } |
| 2928 | #else /* !CONFIG_MODULE_SIG */ | 1620 | |
| 2929 | static int module_sig_check(struct load_info *info, int flags) | 1621 | bool __weak module_exit_section(const char *name) |
| 2930 | { | 1622 | { |
| 2931 | return 0; | 1623 | return strstarts(name, ".exit"); |
| 2932 | } | 1624 | } |
| 2933 | #endif /* !CONFIG_MODULE_SIG */ | | |
| 2934 | | 1625 | |
| 2935 | static int validate_section_offset(struct load_info *info, Elf_Shdr *shdr) | 1626 | static int validate_section_offset(struct load_info *info, Elf_Shdr *shdr) |
| 2936 | { | 1627 | { |
| @@ -3025,6 +1716,10 @@ static int elf_validity_check(struct load_info *info) · 88 unmodified lines |
| 3025 | * strings in the section safe. | 1716 | * strings in the section safe. |
| 3026 | */ | 1717 | */ |
| 3027 | info->secstrings = (void *)info->hdr + strhdr->sh_offset; | 1718 | info->secstrings = (void *)info->hdr + strhdr->sh_offset; |
| | 1719 | if (strhdr->sh_size == 0) { |
| | 1720 | pr_err("empty section name table\n"); |
| | 1721 | goto no_exec; |
| | 1722 | } |
| 3028 | if (info->secstrings[strhdr->sh_size - 1] != '\0') { | 1723 | if (info->secstrings[strhdr->sh_size - 1] != '\0') { |
| 3029 | pr_err("ELF Spec violation: section name table isn't null terminated\n"); | 1724 | pr_err("ELF Spec violation: section name table isn't null terminated\n"); |
| 3030 | goto no_exec; | 1725 | goto no_exec; |
| @@ -3099,30 +1794,23 @@ static int copy_chunked_from_user(void *dst, const void __user *usrc, unsigned l · 68 unmodified lines |
| 3099 | return 0; | 1794 | return 0; |
| 3100 | } | 1795 | } |
| 3101 | | 1796 | |
| 3102 | #ifdef CONFIG_LIVEPATCH | | |
| 3103 | static int check_modinfo_livepatch(struct module *mod, struct load_info *info) | 1797 | static int check_modinfo_livepatch(struct module *mod, struct load_info *info) |
| 3104 | { | 1798 | { |
| 3105 | if (get_modinfo(info, "livepatch")) { | 1799 | if (!get_modinfo(info, "livepatch")) |
| 3106 | mod->klp = true; | 1800 | /* Nothing more to do */ |
| | 1801 | return 0; |
| | 1802 | |
| | 1803 | if (set_livepatch_module(mod)) { |
| 3107 | add_taint_module(mod, TAINT_LIVEPATCH, LOCKDEP_STILL_OK); | 1804 | add_taint_module(mod, TAINT_LIVEPATCH, LOCKDEP_STILL_OK); |
| 3108 | pr_notice_once("%s: tainting kernel with TAINT_LIVEPATCH\n", | 1805 | pr_notice_once("%s: tainting kernel with TAINT_LIVEPATCH\n", |
| 3109 | mod->name); | 1806 | mod->name); |
| 3110 | } | 1807 | return 0; |
| 3111 | | | |
| 3112 | return 0; | | |
| 3113 | } | | |
| 3114 | #else /* !CONFIG_LIVEPATCH */ | | |
| 3115 | static int check_modinfo_livepatch(struct module *mod, struct load_info *info) | | |
| 3116 | { | | |
| 3117 | if (get_modinfo(info, "livepatch")) { | | |
| 3118 | pr_err("%s: module is marked as livepatch module, but livepatch support is disabled", | | |
| 3119 | mod->name); | | |
| 3120 | return -ENOEXEC; | | |
| 3121 | } | 1808 | } |
| 3122 | | 1809 | |
| 3123 | return 0; | 1810 | pr_err("%s: module is marked as livepatch module, but livepatch support is disabled", |
| | 1811 | mod->name); |
| | 1812 | return -ENOEXEC; |
| 3124 | } | 1813 | } |
| 3125 | #endif /* CONFIG_LIVEPATCH */ | | |
| 3126 | | 1814 | |
| 3127 | static void check_modinfo_retpoline(struct module *mod, struct load_info *info) | 1815 | static void check_modinfo_retpoline(struct module *mod, struct load_info *info) |
| 3128 | { | 1816 | { |
| @@ -3448,6 +2136,24 @@ static int move_module(struct module *mod, struct load_info *info) · 319 unmodified lines |
| 3448 | } else | 2136 | } else |
| 3449 | mod->init_layout.base = NULL; | 2137 | mod->init_layout.base = NULL; |
| 3450 | | 2138 | |
| | 2139 | #ifdef CONFIG_ARCH_WANTS_MODULES_DATA_IN_VMALLOC |
| | 2140 | /* Do the allocs. */ |
| | 2141 | ptr = vmalloc(mod->data_layout.size); |
| | 2142 | /* |
| | 2143 | * The pointer to this block is stored in the module structure |
| | 2144 | * which is inside the block. Just mark it as not being a |
| | 2145 | * leak. |
| | 2146 | */ |
| | 2147 | kmemleak_not_leak(ptr); |
| | 2148 | if (!ptr) { |
| | 2149 | module_memfree(mod->core_layout.base); |
| | 2150 | module_memfree(mod->init_layout.base); |
| | 2151 | return -ENOMEM; |
| | 2152 | } |
| | 2153 | |
| | 2154 | memset(ptr, 0, mod->data_layout.size); |
| | 2155 | mod->data_layout.base = ptr; |
| | 2156 | #endif |
| 3451 | /* Transfer each section which specifies SHF_ALLOC */ | 2157 | /* Transfer each section which specifies SHF_ALLOC */ |
| 3452 | pr_debug("final section addresses:\n"); | 2158 | pr_debug("final section addresses:\n"); |
| 3453 | for (i = 0; i < info->hdr->e_shnum; i++) { | 2159 | for (i = 0; i < info->hdr->e_shnum; i++) { |
| @@ -3460,6 +2166,8 @@ static int move_module(struct module *mod, struct load_info *info) · 6 unmodified lines |
| 3460 | if (shdr->sh_entsize & INIT_OFFSET_MASK) | 2166 | if (shdr->sh_entsize & INIT_OFFSET_MASK) |
| 3461 | dest = mod->init_layout.base | 2167 | dest = mod->init_layout.base |
| 3462 | + (shdr->sh_entsize & ~INIT_OFFSET_MASK); | 2168 | + (shdr->sh_entsize & ~INIT_OFFSET_MASK); |
| | 2169 | else if (!(shdr->sh_flags & SHF_EXECINSTR)) |
| | 2170 | dest = mod->data_layout.base + shdr->sh_entsize; |
| 3463 | else | 2171 | else |
| 3464 | dest = mod->core_layout.base + shdr->sh_entsize; | 2172 | dest = mod->core_layout.base + shdr->sh_entsize; |
| 3465 | | 2173 | |
| @@ -3621,6 +2329,9 @@ static void module_deallocate(struct module *mod, struct load_info *info) · 155 unmodified lines |
| 3621 | module_arch_freeing_init(mod); | 2329 | module_arch_freeing_init(mod); |
| 3622 | module_memfree(mod->init_layout.base); | 2330 | module_memfree(mod->init_layout.base); |
| 3623 | module_memfree(mod->core_layout.base); | 2331 | module_memfree(mod->core_layout.base); |
| | 2332 | #ifdef CONFIG_ARCH_WANTS_MODULES_DATA_IN_VMALLOC |
| | 2333 | vfree(mod->data_layout.base); |
| | 2334 | #endif |
| 3624 | } | 2335 | } |
| 3625 | | 2336 | |
| 3626 | int __weak module_finalize(const Elf_Ehdr *hdr, | 2337 | int __weak module_finalize(const Elf_Ehdr *hdr, |
| @@ -3871,6 +2582,9 @@ static int complete_formation(struct module *mod, struct load_info *info) · 244 unmodified lines |
| 3871 | /* This relies on module_mutex for list integrity. */ | 2582 | /* This relies on module_mutex for list integrity. */ |
| 3872 | module_bug_finalize(info->hdr, info->sechdrs, mod); | 2583 | module_bug_finalize(info->hdr, info->sechdrs, mod); |
| 3873 | | 2584 | |
| | 2585 | if (module_check_misalignment(mod)) |
| | 2586 | goto out_misaligned; |
| | 2587 | |
| 3874 | module_enable_ro(mod, false); | 2588 | module_enable_ro(mod, false); |
| 3875 | module_enable_nx(mod); | 2589 | module_enable_nx(mod); |
| 3876 | module_enable_x(mod); | 2590 | module_enable_x(mod); |
| @@ -3884,6 +2598,8 @@ static int complete_formation(struct module *mod, struct load_info *info) · 7 unmodified lines |
| 3884 | | 2598 | |
| 3885 | return 0; | 2599 | return 0; |
| 3886 | | 2600 | |
| | 2601 | out_misaligned: |
| | 2602 | err = -EINVAL; |
| 3887 | out: | 2603 | out: |
| 3888 | mutex_unlock(&module_mutex); | 2604 | mutex_unlock(&module_mutex); |
| 3889 | return err; | 2605 | return err; |
| @@ -4150,7 +2866,7 @@ static int load_module(struct load_info *info, const char __user *uargs, · 260 unmodified lines |
| 4150 | mutex_unlock(&module_mutex); | 2866 | mutex_unlock(&module_mutex); |
| 4151 | free_module: | 2867 | free_module: |
| 4152 | /* Free lock-classes; relies on the preceding sync_rcu() */ | 2868 | /* Free lock-classes; relies on the preceding sync_rcu() */ |
| 4153 | lockdep_free_key_range(mod->core_layout.base, mod->core_layout.size); | 2869 | lockdep_free_key_range(mod->data_layout.base, mod->data_layout.size); |
| 4154 | | 2870 | |
| 4155 | module_deallocate(mod, info); | 2871 | module_deallocate(mod, info); |
| 4156 | free_copy: | 2872 | free_copy: |
| @@ -4219,287 +2935,6 @@ static inline int within(unsigned long addr, void *start, unsigned long size) · 62 unmodified lines |
| 4219 | return ((void *)addr >= start && (void *)addr < start + size); | 2935 | return ((void *)addr >= start && (void *)addr < start + size); |
| 4220 | } | 2936 | } |
| 4221 | | 2937 | |
| 4222 | #ifdef CONFIG_KALLSYMS | | |
| 4223 | /* | | |
| 4224 | * This ignores the intensely annoying "mapping symbols" found | | |
| 4225 | * in ARM ELF files: $a, $t and $d. | | |
| 4226 | */ | | |
| 4227 | static inline int is_arm_mapping_symbol(const char *str) | | |
| 4228 | { | | |
| 4229 | if (str[0] == '.' && str[1] == 'L') | | |
| 4230 | return true; | | |
| 4231 | return str[0] == '$' && strchr("axtd", str[1]) | | |
| 4232 | && (str[2] == '\0' || str[2] == '.'); | | |
| 4233 | } | | |
| 4234 | | | |
| 4235 | static const char *kallsyms_symbol_name(struct mod_kallsyms *kallsyms, unsigned int symnum) | | |
| 4236 | { | | |
| 4237 | return kallsyms->strtab + kallsyms->symtab[symnum].st_name; | | |
| 4238 | } | | |
| 4239 | | | |
| 4240 | /* | | |
| 4241 | * Given a module and address, find the corresponding symbol and return its name | | |
| 4242 | * while providing its size and offset if needed. | | |
| 4243 | */ | | |
| 4244 | static const char *find_kallsyms_symbol(struct module *mod, | | |
| 4245 | unsigned long addr, | | |
| 4246 | unsigned long *size, | | |
| 4247 | unsigned long *offset) | | |
| 4248 | { | | |
| 4249 | unsigned int i, best = 0; | | |
| 4250 | unsigned long nextval, bestval; | | |
| 4251 | struct mod_kallsyms *kallsyms = rcu_dereference_sched(mod->kallsyms); | | |
| 4252 | | | |
| 4253 | /* At worse, next value is at end of module */ | | |
| 4254 | if (within_module_init(addr, mod)) | | |
| 4255 | nextval = (unsigned long)mod->init_layout.base+mod->init_layout.text_size; | | |
| 4256 | else | | |
| 4257 | nextval = (unsigned long)mod->core_layout.base+mod->core_layout.text_size; | | |
| 4258 | | | |
| 4259 | bestval = kallsyms_symbol_value(&kallsyms->symtab[best]); | | |
| 4260 | | | |
| 4261 | /* | | |
| 4262 | * Scan for closest preceding symbol, and next symbol. (ELF | | |
| 4263 | * starts real symbols at 1). | | |
| 4264 | */ | | |
| 4265 | for (i = 1; i < kallsyms->num_symtab; i++) { | | |
| 4266 | const Elf_Sym *sym = &kallsyms->symtab[i]; | | |
| 4267 | unsigned long thisval = kallsyms_symbol_value(sym); | | |
| 4268 | | | |
| 4269 | if (sym->st_shndx == SHN_UNDEF) | | |
| 4270 | continue; | | |
| 4271 | | | |
| 4272 | /* | | |
| 4273 | * We ignore unnamed symbols: they're uninformative | | |
| 4274 | * and inserted at a whim. | | |
| 4275 | */ | | |
| 4276 | if (*kallsyms_symbol_name(kallsyms, i) == '\0' | | |
| 4277 | || is_arm_mapping_symbol(kallsyms_symbol_name(kallsyms, i))) | | |
| 4278 | continue; | | |
| 4279 | | | |
| 4280 | if (thisval <= addr && thisval > bestval) { | | |
| 4281 | best = i; | | |
| 4282 | bestval = thisval; | | |
| 4283 | } | | |
| 4284 | if (thisval > addr && thisval < nextval) | | |
| 4285 | nextval = thisval; | | |
| 4286 | } | | |
| 4287 | | | |
| 4288 | if (!best) | | |
| 4289 | return NULL; | | |
| 4290 | | | |
| 4291 | if (size) | | |
| 4292 | *size = nextval - bestval; | | |
| 4293 | if (offset) | | |
| 4294 | *offset = addr - bestval; | | |
| 4295 | | | |
| 4296 | return kallsyms_symbol_name(kallsyms, best); | | |
| 4297 | } | | |
| 4298 | | | |
| 4299 | void * __weak dereference_module_function_descriptor(struct module *mod, | | |
| 4300 | void *ptr) | | |
| 4301 | { | | |
| 4302 | return ptr; | | |
| 4303 | } | | |
| 4304 | | | |
| 4305 | /* | | |
| 4306 | * For kallsyms to ask for address resolution. NULL means not found. Careful | | |
| 4307 | * not to lock to avoid deadlock on oopses, simply disable preemption. | | |
| 4308 | */ | | |
| 4309 | const char *module_address_lookup(unsigned long addr, | | |
| 4310 | unsigned long *size, | | |
| 4311 | unsigned long *offset, | | |
| 4312 | char **modname, | | |
| 4313 | const unsigned char **modbuildid, | | |
| 4314 | char *namebuf) | | |
| 4315 | { | | |
| 4316 | const char *ret = NULL; | | |
| 4317 | struct module *mod; | | |
| 4318 | | | |
| 4319 | preempt_disable(); | | |
| 4320 | mod = __module_address(addr); | | |
| 4321 | if (mod) { | | |
| 4322 | if (modname) | | |
| 4323 | *modname = mod->name; | | |
| 4324 | if (modbuildid) { | | |
| 4325 | #if IS_ENABLED(CONFIG_STACKTRACE_BUILD_ID) | | |
| 4326 | *modbuildid = mod->build_id; | | |
| 4327 | #else | | |
| 4328 | *modbuildid = NULL; | | |
| 4329 | #endif | | |
| 4330 | } | | |
| 4331 | | | |
| 4332 | ret = find_kallsyms_symbol(mod, addr, size, offset); | | |
| 4333 | } | | |
| 4334 | /* Make a copy in here where it's safe */ | | |
| 4335 | if (ret) { | | |
| 4336 | strncpy(namebuf, ret, KSYM_NAME_LEN - 1); | | |
| 4337 | ret = namebuf; | | |
| 4338 | } | | |
| 4339 | preempt_enable(); | | |
| 4340 | | | |
| 4341 | return ret; | | |
| 4342 | } | | |
| 4343 | | | |
| 4344 | int lookup_module_symbol_name(unsigned long addr, char *symname) | | |
| 4345 | { | | |
| 4346 | struct module *mod; | | |
| 4347 | | | |
| 4348 | preempt_disable(); | | |
| 4349 | list_for_each_entry_rcu(mod, &modules, list) { | | |
| 4350 | if (mod->state == MODULE_STATE_UNFORMED) | | |
| 4351 | continue; | | |
| 4352 | if (within_module(addr, mod)) { | | |
| 4353 | const char *sym; | | |
| 4354 | | | |
| 4355 | sym = find_kallsyms_symbol(mod, addr, NULL, NULL); | | |
| 4356 | if (!sym) | | |
| 4357 | goto out; | | |
| 4358 | | | |
| 4359 | strlcpy(symname, sym, KSYM_NAME_LEN); | | |
| 4360 | preempt_enable(); | | |
| 4361 | return 0; | | |
| 4362 | } | | |
| 4363 | } | | |
| 4364 | out: | | |
| 4365 | preempt_enable(); | | |
| 4366 | return -ERANGE; | | |
| 4367 | } | | |
| 4368 | | | |
| 4369 | int lookup_module_symbol_attrs(unsigned long addr, unsigned long *size, | | |
| 4370 | unsigned long *offset, char *modname, char *name) | | |
| 4371 | { | | |
| 4372 | struct module *mod; | | |
| 4373 | | | |
| 4374 | preempt_disable(); | | |
| 4375 | list_for_each_entry_rcu(mod, &modules, list) { | | |
| 4376 | if (mod->state == MODULE_STATE_UNFORMED) | | |
| 4377 | continue; | | |
| 4378 | if (within_module(addr, mod)) { | | |
| 4379 | const char *sym; | | |
| 4380 | | | |
| 4381 | sym = find_kallsyms_symbol(mod, addr, size, offset); | | |
| 4382 | if (!sym) | | |
| 4383 | goto out; | | |
| 4384 | if (modname) | | |
| 4385 | strlcpy(modname, mod->name, MODULE_NAME_LEN); | | |
| 4386 | if (name) | | |
| 4387 | strlcpy(name, sym, KSYM_NAME_LEN); | | |
| 4388 | preempt_enable(); | | |
| 4389 | return 0; | | |
| 4390 | } | | |
| 4391 | } | | |
| 4392 | out: | | |
| 4393 | preempt_enable(); | | |
| 4394 | return -ERANGE; | | |
| 4395 | } | | |
| 4396 | | | |
| 4397 | int module_get_kallsym(unsigned int symnum, unsigned long *value, char *type, | | |
| 4398 | char *name, char *module_name, int *exported) | | |
| 4399 | { | | |
| 4400 | struct module *mod; | | |
| 4401 | | | |
| 4402 | preempt_disable(); | | |
| 4403 | list_for_each_entry_rcu(mod, &modules, list) { | | |
| 4404 | struct mod_kallsyms *kallsyms; | | |
| 4405 | | | |
| 4406 | if (mod->state == MODULE_STATE_UNFORMED) | | |
| 4407 | continue; | | |
| 4408 | kallsyms = rcu_dereference_sched(mod->kallsyms); | | |
| 4409 | if (symnum < kallsyms->num_symtab) { | | |
| 4410 | const Elf_Sym *sym = &kallsyms->symtab[symnum]; | | |
| 4411 | | | |
| 4412 | *value = kallsyms_symbol_value(sym); | | |
| 4413 | *type = kallsyms->typetab[symnum]; | | |
| 4414 | strlcpy(name, kallsyms_symbol_name(kallsyms, symnum), KSYM_NAME_LEN); | | |
| 4415 | strlcpy(module_name, mod->name, MODULE_NAME_LEN); | | |
| 4416 | *exported = is_exported(name, *value, mod); | | |
| 4417 | preempt_enable(); | | |
| 4418 | return 0; | | |
| 4419 | } | | |
| 4420 | symnum -= kallsyms->num_symtab; | | |
| 4421 | } | | |
| 4422 | preempt_enable(); | | |
| 4423 | return -ERANGE; | | |
| 4424 | } | | |
| 4425 | | | |
| 4426 | /* Given a module and name of symbol, find and return the symbol's value */ | | |
| 4427 | static unsigned long find_kallsyms_symbol_value(struct module *mod, const char *name) | | |
| 4428 | { | | |
| 4429 | unsigned int i; | | |
| 4430 | struct mod_kallsyms *kallsyms = rcu_dereference_sched(mod->kallsyms); | | |
| 4431 | | | |
| 4432 | for (i = 0; i < kallsyms->num_symtab; i++) { | | |
| 4433 | const Elf_Sym *sym = &kallsyms->symtab[i]; | | |
| 4434 | | | |
| 4435 | if (strcmp(name, kallsyms_symbol_name(kallsyms, i)) == 0 && | | |
| 4436 | sym->st_shndx != SHN_UNDEF) | | |
| 4437 | return kallsyms_symbol_value(sym); | | |
| 4438 | } | | |
| 4439 | return 0; | | |
| 4440 | } | | |
| 4441 | | | |
| 4442 | /* Look for this name: can be of form module:name. */ | | |
| 4443 | unsigned long module_kallsyms_lookup_name(const char *name) | | |
| 4444 | { | | |
| 4445 | struct module *mod; | | |
| 4446 | char *colon; | | |
| 4447 | unsigned long ret = 0; | | |
| 4448 | | | |
| 4449 | /* Don't lock: we're in enough trouble already. */ | | |
| 4450 | preempt_disable(); | | |
| 4451 | if ((colon = strnchr(name, MODULE_NAME_LEN, ':')) != NULL) { | | |
| 4452 | if ((mod = find_module_all(name, colon - name, false)) != NULL) | | |
| 4453 | ret = find_kallsyms_symbol_value(mod, colon+1); | | |
| 4454 | } else { | | |
| 4455 | list_for_each_entry_rcu(mod, &modules, list) { | | |
| 4456 | if (mod->state == MODULE_STATE_UNFORMED) | | |
| 4457 | continue; | | |
| 4458 | if ((ret = find_kallsyms_symbol_value(mod, name)) != 0) | | |
| 4459 | break; | | |
| 4460 | } | | |
| 4461 | } | | |
| 4462 | preempt_enable(); | | |
| 4463 | return ret; | | |
| 4464 | } | | |
| 4465 | | | |
| 4466 | #ifdef CONFIG_LIVEPATCH | | |
| 4467 | int module_kallsyms_on_each_symbol(int (*fn)(void *, const char *, | | |
| 4468 | struct module *, unsigned long), | | |
| 4469 | void *data) | | |
| 4470 | { | | |
| 4471 | struct module *mod; | | |
| 4472 | unsigned int i; | | |
| 4473 | int ret = 0; | | |
| 4474 | | | |
| 4475 | mutex_lock(&module_mutex); | | |
| 4476 | list_for_each_entry(mod, &modules, list) { | | |
| 4477 | /* We hold module_mutex: no need for rcu_dereference_sched */ | | |
| 4478 | struct mod_kallsyms *kallsyms = mod->kallsyms; | | |
| 4479 | | | |
| 4480 | if (mod->state == MODULE_STATE_UNFORMED) | | |
| 4481 | continue; | | |
| 4482 | for (i = 0; i < kallsyms->num_symtab; i++) { | | |
| 4483 | const Elf_Sym *sym = &kallsyms->symtab[i]; | | |
| 4484 | | | |
| 4485 | if (sym->st_shndx == SHN_UNDEF) | | |
| 4486 | continue; | | |
| 4487 | | | |
| 4488 | ret = fn(data, kallsyms_symbol_name(kallsyms, i), | | |
| 4489 | mod, kallsyms_symbol_value(sym)); | | |
| 4490 | if (ret != 0) | | |
| 4491 | goto out; | | |
| 4492 | | | |
| 4493 | cond_resched(); | | |
| 4494 | } | | |
| 4495 | } | | |
| 4496 | out: | | |
| 4497 | mutex_unlock(&module_mutex); | | |
| 4498 | return ret; | | |
| 4499 | } | | |
| 4500 | #endif /* CONFIG_LIVEPATCH */ | | |
| 4501 | #endif /* CONFIG_KALLSYMS */ | | |
| 4502 | | | |
| 4503 | static void cfi_init(struct module *mod) | 2938 | static void cfi_init(struct module *mod) |
| 4504 | { | 2939 | { |
| 4505 | #ifdef CONFIG_CFI_CLANG | 2940 | #ifdef CONFIG_CFI_CLANG |
| @@ -4523,22 +2958,19 @@ static void cfi_init(struct module *mod) · 17 unmodified lines |
| 4523 | mod->exit = *exit; | 2958 | mod->exit = *exit; |
| 4524 | #endif | 2959 | #endif |
| 4525 | | 2960 | |
| 4526 | cfi_module_add(mod, module_addr_min); | 2961 | cfi_module_add(mod, mod_tree.addr_min); |
| 4527 | #endif | 2962 | #endif |
| 4528 | } | 2963 | } |
| 4529 | | 2964 | |
| 4530 | static void cfi_cleanup(struct module *mod) | 2965 | static void cfi_cleanup(struct module *mod) |
| 4531 | { | 2966 | { |
| 4532 | #ifdef CONFIG_CFI_CLANG | 2967 | #ifdef CONFIG_CFI_CLANG |
| 4533 | cfi_module_remove(mod, module_addr_min); | 2968 | cfi_module_remove(mod, mod_tree.addr_min); |
| 4534 | #endif | 2969 | #endif |
| 4535 | } | 2970 | } |
| 4536 | | 2971 | |
| 4537 | /* Maximum number of characters written by module_flags() */ | | |
| 4538 | #define MODULE_FLAGS_BUF_SIZE (TAINT_FLAGS_COUNT + 4) | | |
| 4539 | | | |
| 4540 | /* Keep in sync with MODULE_FLAGS_BUF_SIZE !!! */ | 2972 | /* Keep in sync with MODULE_FLAGS_BUF_SIZE !!! */ |
| 4541 | static char *module_flags(struct module *mod, char *buf) | 2973 | char *module_flags(struct module *mod, char *buf) |
| 4542 | { | 2974 | { |
| 4543 | int bx = 0; | 2975 | int bx = 0; |
| 4544 | | 2976 | |
| @@ -4547,7 +2979,7 @@ static char *module_flags(struct module *mod, char *buf) · 2 unmodified lines |
| 4547 | mod->state == MODULE_STATE_GOING || | 2979 | mod->state == MODULE_STATE_GOING || |
| 4548 | mod->state == MODULE_STATE_COMING) { | 2980 | mod->state == MODULE_STATE_COMING) { |
| 4549 | buf[bx++] = '('; | 2981 | buf[bx++] = '('; |
| 4550 | bx += module_flags_taint(mod, buf + bx); | 2982 | bx += module_flags_taint(mod->taints, buf + bx); |
| 4551 | /* Show a - for module-is-being-unloaded */ | 2983 | /* Show a - for module-is-being-unloaded */ |
| 4552 | if (mod->state == MODULE_STATE_GOING) | 2984 | if (mod->state == MODULE_STATE_GOING) |
| 4553 | buf[bx++] = '-'; | 2985 | buf[bx++] = '-'; |
| @@ -4561,103 +2993,6 @@ static char *module_flags(struct module *mod, char *buf) · 7 unmodified lines |
| 4561 | return buf; | 2993 | return buf; |
| 4562 | } | 2994 | } |
| 4563 | | 2995 | |
| 4564 | #ifdef CONFIG_PROC_FS | | |
| 4565 | /* Called by the /proc file system to return a list of modules. */ | | |
| 4566 | static void *m_start(struct seq_file *m, loff_t *pos) | | |
| 4567 | { | | |
| 4568 | mutex_lock(&module_mutex); | | |
| 4569 | return seq_list_start(&modules, *pos); | | |
| 4570 | } | | |
| 4571 | | | |
| 4572 | static void *m_next(struct seq_file *m, void *p, loff_t *pos) | | |
| 4573 | { | | |
| 4574 | return seq_list_next(p, &modules, pos); | | |
| 4575 | } | | |
| 4576 | | | |
| 4577 | static void m_stop(struct seq_file *m, void *p) | | |
| 4578 | { | | |
| 4579 | mutex_unlock(&module_mutex); | | |
| 4580 | } | | |
| 4581 | | | |
| 4582 | static int m_show(struct seq_file *m, void *p) | | |
| 4583 | { | | |
| 4584 | struct module *mod = list_entry(p, struct module, list); | | |
| 4585 | char buf[MODULE_FLAGS_BUF_SIZE]; | | |
| 4586 | void *value; | | |
| 4587 | | | |
| 4588 | /* We always ignore unformed modules. */ | | |
| 4589 | if (mod->state == MODULE_STATE_UNFORMED) | | |
| 4590 | return 0; | | |
| 4591 | | | |
| 4592 | seq_printf(m, "%s %u", | | |
| 4593 | mod->name, mod->init_layout.size + mod->core_layout.size); | | |
| 4594 | print_unload_info(m, mod); | | |
| 4595 | | | |
| 4596 | /* Informative for users. */ | | |
| 4597 | seq_printf(m, " %s", | | |
| 4598 | mod->state == MODULE_STATE_GOING ? "Unloading" : | | |
| 4599 | mod->state == MODULE_STATE_COMING ? "Loading" : | | |
| 4600 | "Live"); | | |
| 4601 | /* Used by oprofile and other similar tools. */ | | |
| 4602 | value = m->private ? NULL : mod->core_layout.base; | | |
| 4603 | seq_printf(m, " 0x%px", value); | | |
| 4604 | | | |
| 4605 | /* Taints info */ | | |
| 4606 | if (mod->taints) | | |
| 4607 | seq_printf(m, " %s", module_flags(mod, buf)); | | |
| 4608 | | | |
| 4609 | seq_puts(m, "\n"); | | |
| 4610 | return 0; | | |
| 4611 | } | | |
| 4612 | | | |
| 4613 | /* | | |
| 4614 | * Format: modulename size refcount deps address | | |
| 4615 | * | | |
| 4616 | * Where refcount is a number or -, and deps is a comma-separated list | | |
| 4617 | * of depends or -. | | |
| 4618 | */ | | |
| 4619 | static const struct seq_operations modules_op = { | | |
| 4620 | .start = m_start, | | |
| 4621 | .next = m_next, | | |
| 4622 | .stop = m_stop, | | |
| 4623 | .show = m_show | | |
| 4624 | }; | | |
| 4625 | | | |
| 4626 | /* | | |
| 4627 | * This also sets the "private" pointer to non-NULL if the | | |
| 4628 | * kernel pointers should be hidden (so you can just test | | |
| 4629 | * "m->private" to see if you should keep the values private). | | |
| 4630 | * | | |
| 4631 | * We use the same logic as for /proc/kallsyms. | | |
| 4632 | */ | | |
| 4633 | static int modules_open(struct inode *inode, struct file *file) | | |
| 4634 | { | | |
| 4635 | int err = seq_open(file, &modules_op); | | |
| 4636 | | | |
| 4637 | if (!err) { | | |
| 4638 | struct seq_file *m = file->private_data; | | |
| 4639 | m->private = kallsyms_show_value(file->f_cred) ? NULL : (void *)8ul; | | |
| 4640 | } | | |
| 4641 | | | |
| 4642 | return err; | | |
| 4643 | } | | |
| 4644 | | | |
| 4645 | static const struct proc_ops modules_proc_ops = { | | |
| 4646 | .proc_flags = PROC_ENTRY_PERMANENT, | | |
| 4647 | .proc_open = modules_open, | | |
| 4648 | .proc_read = seq_read, | | |
| 4649 | .proc_lseek = seq_lseek, | | |
| 4650 | .proc_release = seq_release, | | |
| 4651 | }; | | |
| 4652 | | | |
| 4653 | static int __init proc_modules_init(void) | | |
| 4654 | { | | |
| 4655 | proc_create("modules", 0, NULL, &modules_proc_ops); | | |
| 4656 | return 0; | | |
| 4657 | } | | |
| 4658 | module_init(proc_modules_init); | | |
| 4659 | #endif | | |
| 4660 | | | |
| 4661 | /* Given an address, look for it in the module exception tables. */ | 2996 | /* Given an address, look for it in the module exception tables. */ |
| 4662 | const struct exception_table_entry *search_module_extables(unsigned long addr) | 2997 | const struct exception_table_entry *search_module_extables(unsigned long addr) |
| 4663 | { | 2998 | { |
| @@ -4713,13 +3048,20 @@ bool is_module_address(unsigned long addr) · 49 unmodified lines |
| 4713 | struct module *__module_address(unsigned long addr) | 3048 | struct module *__module_address(unsigned long addr) |
| 4714 | { | 3049 | { |
| 4715 | struct module *mod; | 3050 | struct module *mod; |
| | 3051 | struct mod_tree_root *tree; |
| 4716 | | 3052 | |
| 4717 | if (addr < module_addr_min || addr > module_addr_max) | 3053 | if (addr >= mod_tree.addr_min && addr <= mod_tree.addr_max) |
| | 3054 | tree = &mod_tree; |
| | 3055 | #ifdef CONFIG_ARCH_WANTS_MODULES_DATA_IN_VMALLOC |
| | 3056 | else if (addr >= mod_data_tree.addr_min && addr <= mod_data_tree.addr_max) |
| | 3057 | tree = &mod_data_tree; |
| | 3058 | #endif |
| | 3059 | else |
| 4718 | return NULL; | 3060 | return NULL; |
| 4719 | | 3061 | |
| 4720 | module_assert_mutex_or_preempt(); | 3062 | module_assert_mutex_or_preempt(); |
| 4721 | | 3063 | |
| 4722 | mod = mod_find(addr); | 3064 | mod = mod_find(addr, tree); |
| 4723 | if (mod) { | 3065 | if (mod) { |
| 4724 | BUG_ON(!within_module(addr, mod)); | 3066 | BUG_ON(!within_module(addr, mod)); |
| 4725 | if (mod->state == MODULE_STATE_UNFORMED) | 3067 | if (mod->state == MODULE_STATE_UNFORMED) |
| @@ -4780,23 +3122,10 @@ void print_modules(void) · 54 unmodified lines |
| 4780 | continue; | 3122 | continue; |
| 4781 | pr_cont(" %s%s", mod->name, module_flags(mod, buf)); | 3123 | pr_cont(" %s%s", mod->name, module_flags(mod, buf)); |
| 4782 | } | 3124 | } |
| | 3125 | |
| | 3126 | print_unloaded_tainted_modules(); |
| 4783 | preempt_enable(); | 3127 | preempt_enable(); |
| 4784 | if (last_unloaded_module[0]) | 3128 | if (last_unloaded_module[0]) |
| 4785 | pr_cont(" [last unloaded: %s]", last_unloaded_module); | 3129 | pr_cont(" [last unloaded: %s]", last_unloaded_module); |
| 4786 | pr_cont("\n"); | 3130 | pr_cont("\n"); |
| 4787 | } | 3131 | } |
| 4788 | | | |
| 4789 | #ifdef CONFIG_MODVERSIONS | | |
| 4790 | /* | | |
| 4791 | * Generate the signature for all relevant module structures here. | | |
| 4792 | * If these change, we don't want to try to parse the module. | | |
| 4793 | */ | | |
| 4794 | void module_layout(struct module *mod, | | |
| 4795 | struct modversion_info *ver, | | |
| 4796 | struct kernel_param *kp, | | |
| 4797 | struct kernel_symbol *ks, | | |
| 4798 | struct tracepoint * const *tp) | | |
| 4799 | { | | |
| 4800 | } | | |
| 4801 | EXPORT_SYMBOL(module_layout); | | |
| 4802 | #endif | | |