^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-or-later
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) Copyright (C) 2002 Richard Henderson
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) Copyright (C) 2001 Rusty Russell, 2002, 2010 Rusty Russell IBM.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #define INCLUDE_VERMAGIC
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <linux/export.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/extable.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/moduleloader.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/module_signature.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/trace_events.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <linux/kallsyms.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include <linux/file.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include <linux/fs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #include <linux/sysfs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) #include <linux/kernel_read_file.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) #include <linux/vmalloc.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) #include <linux/elf.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) #include <linux/proc_fs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) #include <linux/security.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) #include <linux/seq_file.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) #include <linux/syscalls.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) #include <linux/fcntl.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) #include <linux/rcupdate.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) #include <linux/capability.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) #include <linux/cpu.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) #include <linux/moduleparam.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) #include <linux/errno.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) #include <linux/err.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) #include <linux/vermagic.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) #include <linux/notifier.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) #include <linux/sched.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) #include <linux/device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) #include <linux/string.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) #include <linux/mutex.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) #include <linux/rculist.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) #include <linux/uaccess.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) #include <asm/cacheflush.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) #include <linux/set_memory.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) #include <asm/mmu_context.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) #include <linux/license.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) #include <asm/sections.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) #include <linux/tracepoint.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) #include <linux/ftrace.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) #include <linux/livepatch.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) #include <linux/async.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) #include <linux/percpu.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) #include <linux/kmemleak.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) #include <linux/jump_label.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) #include <linux/pfn.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) #include <linux/bsearch.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) #include <linux/dynamic_debug.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) #include <linux/audit.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) #include <uapi/linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) #include "module-internal.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) #define CREATE_TRACE_POINTS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) #include <trace/events/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) #undef CREATE_TRACE_POINTS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) #include <trace/hooks/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) #include <trace/hooks/memory.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) #ifndef ARCH_SHF_SMALL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) #define ARCH_SHF_SMALL 0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) * Modules' sections will be aligned on page boundaries
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) * to ensure complete separation of code and data, but
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) * only when CONFIG_ARCH_HAS_STRICT_MODULE_RWX=y
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) #ifdef CONFIG_ARCH_HAS_STRICT_MODULE_RWX
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) # define debug_align(X) ALIGN(X, PAGE_SIZE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) # define debug_align(X) (X)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) /* If this is set, the section belongs in the init part of the module */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) #define INIT_OFFSET_MASK (1UL << (BITS_PER_LONG-1))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) * Mutex protects:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) * 1) List of modules (also safely readable with preempt_disable),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) * 2) module_use links,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) * 3) module_addr_min/module_addr_max.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) * (delete and add uses RCU list operations). */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) DEFINE_MUTEX(module_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) static LIST_HEAD(modules);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) /* Work queue for freeing init sections in success case */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) static void do_free_init(struct work_struct *w);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) static DECLARE_WORK(init_free_wq, do_free_init);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) static LLIST_HEAD(init_free_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) #ifdef CONFIG_MODULES_TREE_LOOKUP
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) * Use a latched RB-tree for __module_address(); this allows us to use
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) * RCU-sched lookups of the address from any context.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) * This is conditional on PERF_EVENTS || TRACING because those can really hit
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) * __module_address() hard by doing a lot of stack unwinding; potentially from
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) * NMI context.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) static __always_inline unsigned long __mod_tree_val(struct latch_tree_node *n)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) struct module_layout *layout = container_of(n, struct module_layout, mtn.node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) return (unsigned long)layout->base;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) static __always_inline unsigned long __mod_tree_size(struct latch_tree_node *n)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) struct module_layout *layout = container_of(n, struct module_layout, mtn.node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) return (unsigned long)layout->size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) static __always_inline bool
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) mod_tree_less(struct latch_tree_node *a, struct latch_tree_node *b)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) return __mod_tree_val(a) < __mod_tree_val(b);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) static __always_inline int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) mod_tree_comp(void *key, struct latch_tree_node *n)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) unsigned long val = (unsigned long)key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) unsigned long start, end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) start = __mod_tree_val(n);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) if (val < start)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) end = start + __mod_tree_size(n);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) if (val >= end)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) static const struct latch_tree_ops mod_tree_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) .less = mod_tree_less,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) .comp = mod_tree_comp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) static struct mod_tree_root {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) struct latch_tree_root root;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) unsigned long addr_min;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) unsigned long addr_max;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) } mod_tree __cacheline_aligned = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) .addr_min = -1UL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) #define module_addr_min mod_tree.addr_min
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) #define module_addr_max mod_tree.addr_max
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) static noinline void __mod_tree_insert(struct mod_tree_node *node)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) latch_tree_insert(&node->node, &mod_tree.root, &mod_tree_ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) static void __mod_tree_remove(struct mod_tree_node *node)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) latch_tree_erase(&node->node, &mod_tree.root, &mod_tree_ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) * These modifications: insert, remove_init and remove; are serialized by the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) * module_mutex.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) static void mod_tree_insert(struct module *mod)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) mod->core_layout.mtn.mod = mod;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) mod->init_layout.mtn.mod = mod;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) __mod_tree_insert(&mod->core_layout.mtn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) if (mod->init_layout.size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) __mod_tree_insert(&mod->init_layout.mtn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) static void mod_tree_remove_init(struct module *mod)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) if (mod->init_layout.size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) __mod_tree_remove(&mod->init_layout.mtn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) static void mod_tree_remove(struct module *mod)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) __mod_tree_remove(&mod->core_layout.mtn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) mod_tree_remove_init(mod);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) static struct module *mod_find(unsigned long addr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) struct latch_tree_node *ltn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) ltn = latch_tree_find((void *)addr, &mod_tree.root, &mod_tree_ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) if (!ltn)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) return container_of(ltn, struct mod_tree_node, node)->mod;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) #else /* MODULES_TREE_LOOKUP */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) static unsigned long module_addr_min = -1UL, module_addr_max = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) static void mod_tree_insert(struct module *mod) { }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) static void mod_tree_remove_init(struct module *mod) { }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) static void mod_tree_remove(struct module *mod) { }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) static struct module *mod_find(unsigned long addr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) struct module *mod;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) list_for_each_entry_rcu(mod, &modules, list,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) lockdep_is_held(&module_mutex)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) if (within_module(addr, mod))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) return mod;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) #endif /* MODULES_TREE_LOOKUP */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) * Bounds of module text, for speeding up __module_address.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) * Protected by module_mutex.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) static void __mod_update_bounds(void *base, unsigned int size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) unsigned long min = (unsigned long)base;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) unsigned long max = min + size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) if (min < module_addr_min)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) module_addr_min = min;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) if (max > module_addr_max)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) module_addr_max = max;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) static void mod_update_bounds(struct module *mod)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) __mod_update_bounds(mod->core_layout.base, mod->core_layout.size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) if (mod->init_layout.size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) __mod_update_bounds(mod->init_layout.base, mod->init_layout.size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) #ifdef CONFIG_KGDB_KDB
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) struct list_head *kdb_modules = &modules; /* kdb needs the list of modules */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) #endif /* CONFIG_KGDB_KDB */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) static void module_assert_mutex(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) lockdep_assert_held(&module_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) static void module_assert_mutex_or_preempt(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) #ifdef CONFIG_LOCKDEP
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) if (unlikely(!debug_locks))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) WARN_ON_ONCE(!rcu_read_lock_sched_held() &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) !lockdep_is_held(&module_mutex));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) #ifdef CONFIG_MODULE_SIG
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) static bool sig_enforce = IS_ENABLED(CONFIG_MODULE_SIG_FORCE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) module_param(sig_enforce, bool_enable_only, 0644);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) void set_module_sig_enforced(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) sig_enforce = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) #define sig_enforce false
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) * Export sig_enforce kernel cmdline parameter to allow other subsystems rely
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) * on that instead of directly to CONFIG_MODULE_SIG_FORCE config.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) bool is_module_sig_enforced(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) return sig_enforce;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) EXPORT_SYMBOL(is_module_sig_enforced);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) /* Block module loading/unloading? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) int modules_disabled = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) core_param(nomodule, modules_disabled, bint, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) /* Waiting for a module to finish initializing? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) static DECLARE_WAIT_QUEUE_HEAD(module_wq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) static BLOCKING_NOTIFIER_HEAD(module_notify_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) int register_module_notifier(struct notifier_block *nb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) return blocking_notifier_chain_register(&module_notify_list, nb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) EXPORT_SYMBOL(register_module_notifier);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) int unregister_module_notifier(struct notifier_block *nb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) return blocking_notifier_chain_unregister(&module_notify_list, nb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) EXPORT_SYMBOL(unregister_module_notifier);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) * We require a truly strong try_module_get(): 0 means success.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) * Otherwise an error is returned due to ongoing or failed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) * initialization etc.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) static inline int strong_try_module_get(struct module *mod)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) BUG_ON(mod && mod->state == MODULE_STATE_UNFORMED);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) if (mod && mod->state == MODULE_STATE_COMING)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) return -EBUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) if (try_module_get(mod))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) return -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) static inline void add_taint_module(struct module *mod, unsigned flag,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) enum lockdep_ok lockdep_ok)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) add_taint(flag, lockdep_ok);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) set_bit(flag, &mod->taints);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) * A thread that wants to hold a reference to a module only while it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) * is running can call this to safely exit. nfsd and lockd use this.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) void __noreturn __module_put_and_exit(struct module *mod, long code)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) module_put(mod);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) do_exit(code);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) EXPORT_SYMBOL(__module_put_and_exit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) /* Find a module section: 0 means not found. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) static unsigned int find_sec(const struct load_info *info, const char *name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) unsigned int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) for (i = 1; i < info->hdr->e_shnum; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) Elf_Shdr *shdr = &info->sechdrs[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) /* Alloc bit cleared means "ignore it." */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) if ((shdr->sh_flags & SHF_ALLOC)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) && strcmp(info->secstrings + shdr->sh_name, name) == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) return i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) /* Find a module section, or NULL. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) static void *section_addr(const struct load_info *info, const char *name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) /* Section 0 has sh_addr 0. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) return (void *)info->sechdrs[find_sec(info, name)].sh_addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) /* Find a module section, or NULL. Fill in number of "objects" in section. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) static void *section_objs(const struct load_info *info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) const char *name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) size_t object_size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) unsigned int *num)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) unsigned int sec = find_sec(info, name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) /* Section 0 has sh_addr 0 and sh_size 0. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) *num = info->sechdrs[sec].sh_size / object_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) return (void *)info->sechdrs[sec].sh_addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) /* Provided by the linker */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) extern const struct kernel_symbol __start___ksymtab[];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) extern const struct kernel_symbol __stop___ksymtab[];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) extern const struct kernel_symbol __start___ksymtab_gpl[];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) extern const struct kernel_symbol __stop___ksymtab_gpl[];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) extern const struct kernel_symbol __start___ksymtab_gpl_future[];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) extern const struct kernel_symbol __stop___ksymtab_gpl_future[];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) extern const s32 __start___kcrctab[];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) extern const s32 __start___kcrctab_gpl[];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) extern const s32 __start___kcrctab_gpl_future[];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) #ifdef CONFIG_UNUSED_SYMBOLS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) extern const struct kernel_symbol __start___ksymtab_unused[];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) extern const struct kernel_symbol __stop___ksymtab_unused[];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) extern const struct kernel_symbol __start___ksymtab_unused_gpl[];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) extern const struct kernel_symbol __stop___ksymtab_unused_gpl[];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) extern const s32 __start___kcrctab_unused[];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) extern const s32 __start___kcrctab_unused_gpl[];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) #ifndef CONFIG_MODVERSIONS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) #define symversion(base, idx) NULL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) #define symversion(base, idx) ((base != NULL) ? ((base) + (idx)) : NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) static bool each_symbol_in_section(const struct symsearch *arr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) unsigned int arrsize,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) struct module *owner,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) bool (*fn)(const struct symsearch *syms,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) struct module *owner,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) void *data),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) void *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) unsigned int j;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) for (j = 0; j < arrsize; j++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) if (fn(&arr[j], owner, data))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) /* Returns true as soon as fn returns true, otherwise false. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) static bool each_symbol_section(bool (*fn)(const struct symsearch *arr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) struct module *owner,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) void *data),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) void *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) struct module *mod;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) static const struct symsearch arr[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) { __start___ksymtab, __stop___ksymtab, __start___kcrctab,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) NOT_GPL_ONLY, false },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) { __start___ksymtab_gpl, __stop___ksymtab_gpl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) __start___kcrctab_gpl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) GPL_ONLY, false },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) { __start___ksymtab_gpl_future, __stop___ksymtab_gpl_future,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) __start___kcrctab_gpl_future,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) WILL_BE_GPL_ONLY, false },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) #ifdef CONFIG_UNUSED_SYMBOLS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) { __start___ksymtab_unused, __stop___ksymtab_unused,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) __start___kcrctab_unused,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) NOT_GPL_ONLY, true },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) { __start___ksymtab_unused_gpl, __stop___ksymtab_unused_gpl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) __start___kcrctab_unused_gpl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) GPL_ONLY, true },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) module_assert_mutex_or_preempt();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) if (each_symbol_in_section(arr, ARRAY_SIZE(arr), NULL, fn, data))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) list_for_each_entry_rcu(mod, &modules, list,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) lockdep_is_held(&module_mutex)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) struct symsearch arr[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) { mod->syms, mod->syms + mod->num_syms, mod->crcs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) NOT_GPL_ONLY, false },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) { mod->gpl_syms, mod->gpl_syms + mod->num_gpl_syms,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) mod->gpl_crcs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) GPL_ONLY, false },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) { mod->gpl_future_syms,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) mod->gpl_future_syms + mod->num_gpl_future_syms,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) mod->gpl_future_crcs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) WILL_BE_GPL_ONLY, false },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) #ifdef CONFIG_UNUSED_SYMBOLS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) { mod->unused_syms,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) mod->unused_syms + mod->num_unused_syms,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) mod->unused_crcs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) NOT_GPL_ONLY, true },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) { mod->unused_gpl_syms,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) mod->unused_gpl_syms + mod->num_unused_gpl_syms,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) mod->unused_gpl_crcs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) GPL_ONLY, true },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) if (mod->state == MODULE_STATE_UNFORMED)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) if (each_symbol_in_section(arr, ARRAY_SIZE(arr), mod, fn, data))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) struct find_symbol_arg {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) /* Input */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) const char *name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) bool gplok;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) bool warn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) /* Output */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) struct module *owner;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) const s32 *crc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) const struct kernel_symbol *sym;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) enum mod_license license;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) static bool check_exported_symbol(const struct symsearch *syms,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) struct module *owner,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) unsigned int symnum, void *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) struct find_symbol_arg *fsa = data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) if (!fsa->gplok) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) if (syms->license == GPL_ONLY)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) if (syms->license == WILL_BE_GPL_ONLY && fsa->warn) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) pr_warn("Symbol %s is being used by a non-GPL module, "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) "which will not be allowed in the future\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) fsa->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) #ifdef CONFIG_UNUSED_SYMBOLS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) if (syms->unused && fsa->warn) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) pr_warn("Symbol %s is marked as UNUSED, however this module is "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) "using it.\n", fsa->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) pr_warn("This symbol will go away in the future.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) pr_warn("Please evaluate if this is the right api to use and "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) "if it really is, submit a report to the linux kernel "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) "mailing list together with submitting your code for "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) "inclusion.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) fsa->owner = owner;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) fsa->crc = symversion(syms->crcs, symnum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) fsa->sym = &syms->start[symnum];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) fsa->license = syms->license;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545) static unsigned long kernel_symbol_value(const struct kernel_symbol *sym)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) #ifdef CONFIG_HAVE_ARCH_PREL32_RELOCATIONS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) return (unsigned long)offset_to_ptr(&sym->value_offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) return sym->value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554) static const char *kernel_symbol_name(const struct kernel_symbol *sym)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) #ifdef CONFIG_HAVE_ARCH_PREL32_RELOCATIONS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557) return offset_to_ptr(&sym->name_offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559) return sym->name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563) static const char *kernel_symbol_namespace(const struct kernel_symbol *sym)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565) #ifdef CONFIG_HAVE_ARCH_PREL32_RELOCATIONS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566) if (!sym->namespace_offset)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568) return offset_to_ptr(&sym->namespace_offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570) return sym->namespace;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574) static int cmp_name(const void *name, const void *sym)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576) return strcmp(name, kernel_symbol_name(sym));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579) static bool find_exported_symbol_in_section(const struct symsearch *syms,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580) struct module *owner,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581) void *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583) struct find_symbol_arg *fsa = data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584) struct kernel_symbol *sym;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586) sym = bsearch(fsa->name, syms->start, syms->stop - syms->start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587) sizeof(struct kernel_symbol), cmp_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 589) if (sym != NULL && check_exported_symbol(syms, owner,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 590) sym - syms->start, data))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 591) return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 592)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 593) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 594) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 595)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 596) /* Find an exported symbol and return it, along with, (optional) crc and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 597) * (optional) module which owns it. Needs preempt disabled or module_mutex. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 598) static const struct kernel_symbol *find_symbol(const char *name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 599) struct module **owner,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 600) const s32 **crc,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 601) enum mod_license *license,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 602) bool gplok,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 603) bool warn)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 604) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 605) struct find_symbol_arg fsa;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 606)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 607) fsa.name = name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 608) fsa.gplok = gplok;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 609) fsa.warn = warn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 610)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 611) if (each_symbol_section(find_exported_symbol_in_section, &fsa)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 612) if (owner)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 613) *owner = fsa.owner;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 614) if (crc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 615) *crc = fsa.crc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 616) if (license)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 617) *license = fsa.license;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 618) return fsa.sym;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 619) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 620)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 621) pr_debug("Failed to find symbol %s\n", name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 622) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 623) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 624)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 625) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 626) * Search for module by name: must hold module_mutex (or preempt disabled
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 627) * for read-only access).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 628) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 629) static struct module *find_module_all(const char *name, size_t len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 630) bool even_unformed)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 631) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 632) struct module *mod;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 633)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 634) module_assert_mutex_or_preempt();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 635)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 636) list_for_each_entry_rcu(mod, &modules, list,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 637) lockdep_is_held(&module_mutex)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 638) if (!even_unformed && mod->state == MODULE_STATE_UNFORMED)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 639) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 640) if (strlen(mod->name) == len && !memcmp(mod->name, name, len))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 641) return mod;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 642) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 643) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 644) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 645)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 646) struct module *find_module(const char *name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 647) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 648) module_assert_mutex();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 649) return find_module_all(name, strlen(name), false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 650) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 651)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 652) #ifdef CONFIG_SMP
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 653)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 654) static inline void __percpu *mod_percpu(struct module *mod)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 655) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 656) return mod->percpu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 657) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 658)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 659) static int percpu_modalloc(struct module *mod, struct load_info *info)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 660) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 661) Elf_Shdr *pcpusec = &info->sechdrs[info->index.pcpu];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 662) unsigned long align = pcpusec->sh_addralign;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 663)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 664) if (!pcpusec->sh_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 665) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 666)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 667) if (align > PAGE_SIZE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 668) pr_warn("%s: per-cpu alignment %li > %li\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 669) mod->name, align, PAGE_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 670) align = PAGE_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 671) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 672)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 673) mod->percpu = __alloc_reserved_percpu(pcpusec->sh_size, align);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 674) if (!mod->percpu) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 675) pr_warn("%s: Could not allocate %lu bytes percpu data\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 676) mod->name, (unsigned long)pcpusec->sh_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 677) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 678) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 679) mod->percpu_size = pcpusec->sh_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 680) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 681) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 682)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 683) static void percpu_modfree(struct module *mod)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 684) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 685) free_percpu(mod->percpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 686) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 687)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 688) static unsigned int find_pcpusec(struct load_info *info)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 689) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 690) return find_sec(info, ".data..percpu");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 691) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 692)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 693) static void percpu_modcopy(struct module *mod,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 694) const void *from, unsigned long size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 695) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 696) int cpu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 697)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 698) for_each_possible_cpu(cpu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 699) memcpy(per_cpu_ptr(mod->percpu, cpu), from, size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 700) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 701)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 702) bool __is_module_percpu_address(unsigned long addr, unsigned long *can_addr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 703) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 704) struct module *mod;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 705) unsigned int cpu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 706)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 707) preempt_disable();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 708)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 709) list_for_each_entry_rcu(mod, &modules, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 710) if (mod->state == MODULE_STATE_UNFORMED)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 711) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 712) if (!mod->percpu_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 713) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 714) for_each_possible_cpu(cpu) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 715) void *start = per_cpu_ptr(mod->percpu, cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 716) void *va = (void *)addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 717)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 718) if (va >= start && va < start + mod->percpu_size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 719) if (can_addr) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 720) *can_addr = (unsigned long) (va - start);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 721) *can_addr += (unsigned long)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 722) per_cpu_ptr(mod->percpu,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 723) get_boot_cpu_id());
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 724) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 725) preempt_enable();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 726) return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 727) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 728) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 729) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 730)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 731) preempt_enable();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 732) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 733) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 734)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 735) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 736) * is_module_percpu_address - test whether address is from module static percpu
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 737) * @addr: address to test
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 738) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 739) * Test whether @addr belongs to module static percpu area.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 740) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 741) * RETURNS:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 742) * %true if @addr is from module static percpu area
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 743) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 744) bool is_module_percpu_address(unsigned long addr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 745) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 746) return __is_module_percpu_address(addr, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 747) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 748)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 749) #else /* ... !CONFIG_SMP */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 750)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 751) static inline void __percpu *mod_percpu(struct module *mod)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 752) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 753) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 754) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 755) static int percpu_modalloc(struct module *mod, struct load_info *info)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 756) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 757) /* UP modules shouldn't have this section: ENOMEM isn't quite right */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 758) if (info->sechdrs[info->index.pcpu].sh_size != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 759) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 760) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 761) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 762) static inline void percpu_modfree(struct module *mod)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 763) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 764) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 765) static unsigned int find_pcpusec(struct load_info *info)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 766) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 767) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 768) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 769) static inline void percpu_modcopy(struct module *mod,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 770) const void *from, unsigned long size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 771) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 772) /* pcpusec should be 0, and size of that section should be 0. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 773) BUG_ON(size != 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 774) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 775) bool is_module_percpu_address(unsigned long addr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 776) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 777) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 778) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 779)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 780) bool __is_module_percpu_address(unsigned long addr, unsigned long *can_addr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 781) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 782) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 783) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 784)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 785) #endif /* CONFIG_SMP */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 786)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 787) #define MODINFO_ATTR(field) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 788) static void setup_modinfo_##field(struct module *mod, const char *s) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 789) { \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 790) mod->field = kstrdup(s, GFP_KERNEL); \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 791) } \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 792) static ssize_t show_modinfo_##field(struct module_attribute *mattr, \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 793) struct module_kobject *mk, char *buffer) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 794) { \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 795) return scnprintf(buffer, PAGE_SIZE, "%s\n", mk->mod->field); \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 796) } \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 797) static int modinfo_##field##_exists(struct module *mod) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 798) { \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 799) return mod->field != NULL; \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 800) } \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 801) static void free_modinfo_##field(struct module *mod) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 802) { \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 803) kfree(mod->field); \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 804) mod->field = NULL; \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 805) } \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 806) static struct module_attribute modinfo_##field = { \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 807) .attr = { .name = __stringify(field), .mode = 0444 }, \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 808) .show = show_modinfo_##field, \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 809) .setup = setup_modinfo_##field, \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 810) .test = modinfo_##field##_exists, \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 811) .free = free_modinfo_##field, \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 812) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 813)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 814) MODINFO_ATTR(version);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 815) MODINFO_ATTR(srcversion);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 816) MODINFO_ATTR(scmversion);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 817)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 818) static char last_unloaded_module[MODULE_NAME_LEN+1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 819)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 820) #ifdef CONFIG_MODULE_UNLOAD
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 821)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 822) EXPORT_TRACEPOINT_SYMBOL(module_get);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 823)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 824) /* MODULE_REF_BASE is the base reference count by kmodule loader. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 825) #define MODULE_REF_BASE 1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 826)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 827) /* Init the unload section of the module. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 828) static int module_unload_init(struct module *mod)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 829) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 830) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 831) * Initialize reference counter to MODULE_REF_BASE.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 832) * refcnt == 0 means module is going.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 833) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 834) atomic_set(&mod->refcnt, MODULE_REF_BASE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 835)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 836) INIT_LIST_HEAD(&mod->source_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 837) INIT_LIST_HEAD(&mod->target_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 838)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 839) /* Hold reference count during initialization. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 840) atomic_inc(&mod->refcnt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 841)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 842) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 843) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 844)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 845) /* Does a already use b? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 846) static int already_uses(struct module *a, struct module *b)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 847) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 848) struct module_use *use;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 849)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 850) list_for_each_entry(use, &b->source_list, source_list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 851) if (use->source == a) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 852) pr_debug("%s uses %s!\n", a->name, b->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 853) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 854) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 855) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 856) pr_debug("%s does not use %s!\n", a->name, b->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 857) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 858) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 859)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 860) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 861) * Module a uses b
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 862) * - we add 'a' as a "source", 'b' as a "target" of module use
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 863) * - the module_use is added to the list of 'b' sources (so
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 864) * 'b' can walk the list to see who sourced them), and of 'a'
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 865) * targets (so 'a' can see what modules it targets).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 866) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 867) static int add_module_usage(struct module *a, struct module *b)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 868) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 869) struct module_use *use;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 870)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 871) pr_debug("Allocating new usage for %s.\n", a->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 872) use = kmalloc(sizeof(*use), GFP_ATOMIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 873) if (!use)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 874) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 875)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 876) use->source = a;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 877) use->target = b;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 878) list_add(&use->source_list, &b->source_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 879) list_add(&use->target_list, &a->target_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 880) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 881) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 882)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 883) /* Module a uses b: caller needs module_mutex() */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 884) static int ref_module(struct module *a, struct module *b)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 885) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 886) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 887)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 888) if (b == NULL || already_uses(a, b))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 889) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 890)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 891) /* If module isn't available, we fail. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 892) err = strong_try_module_get(b);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 893) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 894) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 895)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 896) err = add_module_usage(a, b);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 897) if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 898) module_put(b);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 899) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 900) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 901) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 902) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 903)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 904) /* Clear the unload stuff of the module. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 905) static void module_unload_free(struct module *mod)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 906) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 907) struct module_use *use, *tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 908)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 909) mutex_lock(&module_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 910) list_for_each_entry_safe(use, tmp, &mod->target_list, target_list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 911) struct module *i = use->target;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 912) pr_debug("%s unusing %s\n", mod->name, i->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 913) module_put(i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 914) list_del(&use->source_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 915) list_del(&use->target_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 916) kfree(use);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 917) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 918) mutex_unlock(&module_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 919) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 920)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 921) #ifdef CONFIG_MODULE_FORCE_UNLOAD
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 922) static inline int try_force_unload(unsigned int flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 923) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 924) int ret = (flags & O_TRUNC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 925) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 926) add_taint(TAINT_FORCED_RMMOD, LOCKDEP_NOW_UNRELIABLE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 927) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 928) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 929) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 930) static inline int try_force_unload(unsigned int flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 931) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 932) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 933) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 934) #endif /* CONFIG_MODULE_FORCE_UNLOAD */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 935)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 936) /* Try to release refcount of module, 0 means success. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 937) static int try_release_module_ref(struct module *mod)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 938) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 939) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 940)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 941) /* Try to decrement refcnt which we set at loading */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 942) ret = atomic_sub_return(MODULE_REF_BASE, &mod->refcnt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 943) BUG_ON(ret < 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 944) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 945) /* Someone can put this right now, recover with checking */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 946) ret = atomic_add_unless(&mod->refcnt, MODULE_REF_BASE, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 947)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 948) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 949) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 950)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 951) static int try_stop_module(struct module *mod, int flags, int *forced)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 952) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 953) /* If it's not unused, quit unless we're forcing. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 954) if (try_release_module_ref(mod) != 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 955) *forced = try_force_unload(flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 956) if (!(*forced))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 957) return -EWOULDBLOCK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 958) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 959)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 960) /* Mark it as dying. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 961) mod->state = MODULE_STATE_GOING;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 962)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 963) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 964) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 965)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 966) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 967) * module_refcount - return the refcount or -1 if unloading
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 968) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 969) * @mod: the module we're checking
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 970) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 971) * Returns:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 972) * -1 if the module is in the process of unloading
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 973) * otherwise the number of references in the kernel to the module
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 974) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 975) int module_refcount(struct module *mod)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 976) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 977) return atomic_read(&mod->refcnt) - MODULE_REF_BASE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 978) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 979) EXPORT_SYMBOL(module_refcount);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 980)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 981) /* This exists whether we can unload or not */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 982) static void free_module(struct module *mod);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 983)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 984) SYSCALL_DEFINE2(delete_module, const char __user *, name_user,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 985) unsigned int, flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 986) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 987) struct module *mod;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 988) char name[MODULE_NAME_LEN];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 989) int ret, forced = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 990)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 991) if (!capable(CAP_SYS_MODULE) || modules_disabled)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 992) return -EPERM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 993)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 994) if (strncpy_from_user(name, name_user, MODULE_NAME_LEN-1) < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 995) return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 996) name[MODULE_NAME_LEN-1] = '\0';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 997)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 998) audit_log_kern_module(name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 999)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1000) if (mutex_lock_interruptible(&module_mutex) != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1001) return -EINTR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1002)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1003) mod = find_module(name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1004) if (!mod) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1005) ret = -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1006) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1007) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1008)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1009) if (!list_empty(&mod->source_list)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1010) /* Other modules depend on us: get rid of them first. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1011) ret = -EWOULDBLOCK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1012) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1013) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1014)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1015) /* Doing init or already dying? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1016) if (mod->state != MODULE_STATE_LIVE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1017) /* FIXME: if (force), slam module count damn the torpedoes */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1018) pr_debug("%s already dying\n", mod->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1019) ret = -EBUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1020) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1021) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1022)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1023) /* If it has an init func, it must have an exit func to unload */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1024) if (mod->init && !mod->exit) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1025) forced = try_force_unload(flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1026) if (!forced) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1027) /* This module can't be removed */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1028) ret = -EBUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1029) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1030) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1031) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1032)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1033) /* Stop the machine so refcounts can't move and disable module. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1034) ret = try_stop_module(mod, flags, &forced);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1035) if (ret != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1036) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1037)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1038) mutex_unlock(&module_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1039) /* Final destruction now no one is using it. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1040) if (mod->exit != NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1041) mod->exit();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1042) blocking_notifier_call_chain(&module_notify_list,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1043) MODULE_STATE_GOING, mod);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1044) klp_module_going(mod);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1045) ftrace_release_mod(mod);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1046)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1047) async_synchronize_full();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1048)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1049) /* Store the name of the last unloaded module for diagnostic purposes */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1050) strlcpy(last_unloaded_module, mod->name, sizeof(last_unloaded_module));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1051)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1052) free_module(mod);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1053) /* someone could wait for the module in add_unformed_module() */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1054) wake_up_all(&module_wq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1055) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1056) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1057) mutex_unlock(&module_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1058) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1059) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1060)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1061) static inline void print_unload_info(struct seq_file *m, struct module *mod)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1062) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1063) struct module_use *use;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1064) int printed_something = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1065)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1066) seq_printf(m, " %i ", module_refcount(mod));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1067)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1068) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1069) * Always include a trailing , so userspace can differentiate
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1070) * between this and the old multi-field proc format.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1071) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1072) list_for_each_entry(use, &mod->source_list, source_list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1073) printed_something = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1074) seq_printf(m, "%s,", use->source->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1075) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1076)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1077) if (mod->init != NULL && mod->exit == NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1078) printed_something = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1079) seq_puts(m, "[permanent],");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1080) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1081)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1082) if (!printed_something)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1083) seq_puts(m, "-");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1084) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1085)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1086) void __symbol_put(const char *symbol)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1087) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1088) struct module *owner;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1089)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1090) preempt_disable();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1091) if (!find_symbol(symbol, &owner, NULL, NULL, true, false))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1092) BUG();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1093) module_put(owner);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1094) preempt_enable();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1095) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1096) EXPORT_SYMBOL(__symbol_put);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1097)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1098) /* Note this assumes addr is a function, which it currently always is. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1099) void symbol_put_addr(void *addr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1100) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1101) struct module *modaddr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1102) unsigned long a = (unsigned long)dereference_function_descriptor(addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1103)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1104) if (core_kernel_text(a))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1105) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1106)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1107) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1108) * Even though we hold a reference on the module; we still need to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1109) * disable preemption in order to safely traverse the data structure.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1110) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1111) preempt_disable();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1112) modaddr = __module_text_address(a);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1113) BUG_ON(!modaddr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1114) module_put(modaddr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1115) preempt_enable();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1116) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1117) EXPORT_SYMBOL_GPL(symbol_put_addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1118)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1119) static ssize_t show_refcnt(struct module_attribute *mattr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1120) struct module_kobject *mk, char *buffer)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1121) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1122) return sprintf(buffer, "%i\n", module_refcount(mk->mod));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1123) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1124)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1125) static struct module_attribute modinfo_refcnt =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1126) __ATTR(refcnt, 0444, show_refcnt, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1127)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1128) void __module_get(struct module *module)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1129) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1130) if (module) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1131) preempt_disable();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1132) atomic_inc(&module->refcnt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1133) trace_module_get(module, _RET_IP_);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1134) preempt_enable();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1135) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1136) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1137) EXPORT_SYMBOL(__module_get);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1138)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1139) bool try_module_get(struct module *module)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1140) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1141) bool ret = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1142)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1143) if (module) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1144) preempt_disable();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1145) /* Note: here, we can fail to get a reference */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1146) if (likely(module_is_live(module) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1147) atomic_inc_not_zero(&module->refcnt) != 0))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1148) trace_module_get(module, _RET_IP_);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1149) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1150) ret = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1151)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1152) preempt_enable();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1153) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1154) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1155) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1156) EXPORT_SYMBOL(try_module_get);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1157)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1158) void module_put(struct module *module)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1159) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1160) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1161)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1162) if (module) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1163) preempt_disable();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1164) ret = atomic_dec_if_positive(&module->refcnt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1165) WARN_ON(ret < 0); /* Failed to put refcount */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1166) trace_module_put(module, _RET_IP_);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1167) preempt_enable();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1168) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1169) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1170) EXPORT_SYMBOL(module_put);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1171)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1172) #else /* !CONFIG_MODULE_UNLOAD */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1173) static inline void print_unload_info(struct seq_file *m, struct module *mod)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1174) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1175) /* We don't know the usage count, or what modules are using. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1176) seq_puts(m, " - -");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1177) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1178)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1179) static inline void module_unload_free(struct module *mod)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1180) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1181) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1182)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1183) static int ref_module(struct module *a, struct module *b)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1184) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1185) return strong_try_module_get(b);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1186) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1187)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1188) static inline int module_unload_init(struct module *mod)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1189) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1190) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1191) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1192) #endif /* CONFIG_MODULE_UNLOAD */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1193)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1194) static size_t module_flags_taint(struct module *mod, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1195) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1196) size_t l = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1197) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1198)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1199) for (i = 0; i < TAINT_FLAGS_COUNT; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1200) if (taint_flags[i].module && test_bit(i, &mod->taints))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1201) buf[l++] = taint_flags[i].c_true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1202) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1203)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1204) return l;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1205) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1206)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1207) static ssize_t show_initstate(struct module_attribute *mattr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1208) struct module_kobject *mk, char *buffer)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1209) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1210) const char *state = "unknown";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1211)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1212) switch (mk->mod->state) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1213) case MODULE_STATE_LIVE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1214) state = "live";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1215) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1216) case MODULE_STATE_COMING:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1217) state = "coming";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1218) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1219) case MODULE_STATE_GOING:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1220) state = "going";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1221) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1222) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1223) BUG();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1224) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1225) return sprintf(buffer, "%s\n", state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1226) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1227)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1228) static struct module_attribute modinfo_initstate =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1229) __ATTR(initstate, 0444, show_initstate, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1230)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1231) static ssize_t store_uevent(struct module_attribute *mattr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1232) struct module_kobject *mk,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1233) const char *buffer, size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1234) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1235) int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1236)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1237) rc = kobject_synth_uevent(&mk->kobj, buffer, count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1238) return rc ? rc : count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1239) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1240)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1241) struct module_attribute module_uevent =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1242) __ATTR(uevent, 0200, NULL, store_uevent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1243)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1244) static ssize_t show_coresize(struct module_attribute *mattr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1245) struct module_kobject *mk, char *buffer)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1246) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1247) return sprintf(buffer, "%u\n", mk->mod->core_layout.size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1248) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1249)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1250) static struct module_attribute modinfo_coresize =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1251) __ATTR(coresize, 0444, show_coresize, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1252)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1253) static ssize_t show_initsize(struct module_attribute *mattr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1254) struct module_kobject *mk, char *buffer)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1255) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1256) return sprintf(buffer, "%u\n", mk->mod->init_layout.size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1257) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1258)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1259) static struct module_attribute modinfo_initsize =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1260) __ATTR(initsize, 0444, show_initsize, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1261)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1262) static ssize_t show_taint(struct module_attribute *mattr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1263) struct module_kobject *mk, char *buffer)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1264) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1265) size_t l;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1266)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1267) l = module_flags_taint(mk->mod, buffer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1268) buffer[l++] = '\n';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1269) return l;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1270) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1271)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1272) static struct module_attribute modinfo_taint =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1273) __ATTR(taint, 0444, show_taint, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1274)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1275) static struct module_attribute *modinfo_attrs[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1276) &module_uevent,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1277) &modinfo_version,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1278) &modinfo_srcversion,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1279) &modinfo_scmversion,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1280) &modinfo_initstate,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1281) &modinfo_coresize,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1282) &modinfo_initsize,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1283) &modinfo_taint,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1284) #ifdef CONFIG_MODULE_UNLOAD
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1285) &modinfo_refcnt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1286) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1287) NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1288) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1289)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1290) static const char vermagic[] = VERMAGIC_STRING;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1291)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1292) static int try_to_force_load(struct module *mod, const char *reason)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1293) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1294) #ifdef CONFIG_MODULE_FORCE_LOAD
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1295) if (!test_taint(TAINT_FORCED_MODULE))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1296) pr_warn("%s: %s: kernel tainted.\n", mod->name, reason);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1297) add_taint_module(mod, TAINT_FORCED_MODULE, LOCKDEP_NOW_UNRELIABLE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1298) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1299) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1300) return -ENOEXEC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1301) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1302) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1303)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1304) #ifdef CONFIG_MODVERSIONS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1305)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1306) static u32 resolve_rel_crc(const s32 *crc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1307) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1308) return *(u32 *)((void *)crc + *crc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1309) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1310)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1311) static int check_version(const struct load_info *info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1312) const char *symname,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1313) struct module *mod,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1314) const s32 *crc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1315) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1316) Elf_Shdr *sechdrs = info->sechdrs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1317) unsigned int versindex = info->index.vers;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1318) unsigned int i, num_versions;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1319) struct modversion_info *versions;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1320)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1321) /* Exporting module didn't supply crcs? OK, we're already tainted. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1322) if (!crc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1323) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1324)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1325) /* No versions at all? modprobe --force does this. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1326) if (versindex == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1327) return try_to_force_load(mod, symname) == 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1328)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1329) versions = (void *) sechdrs[versindex].sh_addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1330) num_versions = sechdrs[versindex].sh_size
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1331) / sizeof(struct modversion_info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1332)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1333) for (i = 0; i < num_versions; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1334) u32 crcval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1335)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1336) if (strcmp(versions[i].name, symname) != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1337) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1338)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1339) if (IS_ENABLED(CONFIG_MODULE_REL_CRCS))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1340) crcval = resolve_rel_crc(crc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1341) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1342) crcval = *crc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1343) if (versions[i].crc == crcval)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1344) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1345) pr_debug("Found checksum %X vs module %lX\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1346) crcval, versions[i].crc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1347) goto bad_version;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1348) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1349)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1350) /* Broken toolchain. Warn once, then let it go.. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1351) pr_warn_once("%s: no symbol version for %s\n", info->name, symname);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1352) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1353)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1354) bad_version:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1355) pr_warn("%s: disagrees about version of symbol %s\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1356) info->name, symname);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1357) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1358) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1359)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1360) static inline int check_modstruct_version(const struct load_info *info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1361) struct module *mod)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1362) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1363) const s32 *crc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1364)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1365) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1366) * Since this should be found in kernel (which can't be removed), no
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1367) * locking is necessary -- use preempt_disable() to placate lockdep.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1368) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1369) preempt_disable();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1370) if (!find_symbol("module_layout", NULL, &crc, NULL, true, false)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1371) preempt_enable();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1372) BUG();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1373) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1374) preempt_enable();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1375) return check_version(info, "module_layout", mod, crc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1376) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1377)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1378) /* First part is kernel version, which we ignore if module has crcs. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1379) static inline int same_magic(const char *amagic, const char *bmagic,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1380) bool has_crcs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1381) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1382) if (has_crcs) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1383) amagic += strcspn(amagic, " ");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1384) bmagic += strcspn(bmagic, " ");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1385) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1386) return strcmp(amagic, bmagic) == 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1387) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1388) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1389) static inline int check_version(const struct load_info *info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1390) const char *symname,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1391) struct module *mod,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1392) const s32 *crc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1393) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1394) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1395) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1396)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1397) static inline int check_modstruct_version(const struct load_info *info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1398) struct module *mod)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1399) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1400) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1401) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1402)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1403) static inline int same_magic(const char *amagic, const char *bmagic,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1404) bool has_crcs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1405) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1406) return strcmp(amagic, bmagic) == 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1407) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1408) #endif /* CONFIG_MODVERSIONS */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1409)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1410) static char *get_modinfo(const struct load_info *info, const char *tag);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1411) static char *get_next_modinfo(const struct load_info *info, const char *tag,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1412) char *prev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1413)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1414) static int verify_namespace_is_imported(const struct load_info *info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1415) const struct kernel_symbol *sym,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1416) struct module *mod)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1417) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1418) const char *namespace;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1419) char *imported_namespace;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1420)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1421) namespace = kernel_symbol_namespace(sym);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1422) if (namespace && namespace[0]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1423) imported_namespace = get_modinfo(info, "import_ns");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1424) while (imported_namespace) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1425) if (strcmp(namespace, imported_namespace) == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1426) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1427) imported_namespace = get_next_modinfo(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1428) info, "import_ns", imported_namespace);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1429) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1430) #ifdef CONFIG_MODULE_ALLOW_MISSING_NAMESPACE_IMPORTS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1431) pr_warn(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1432) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1433) pr_err(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1434) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1435) "%s: module uses symbol (%s) from namespace %s, but does not import it.\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1436) mod->name, kernel_symbol_name(sym), namespace);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1437) #ifndef CONFIG_MODULE_ALLOW_MISSING_NAMESPACE_IMPORTS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1438) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1439) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1440) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1441) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1442) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1443)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1444) static bool inherit_taint(struct module *mod, struct module *owner)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1445) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1446) if (!owner || !test_bit(TAINT_PROPRIETARY_MODULE, &owner->taints))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1447) return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1448)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1449) if (mod->using_gplonly_symbols) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1450) pr_err("%s: module using GPL-only symbols uses symbols from proprietary module %s.\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1451) mod->name, owner->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1452) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1453) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1454)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1455) if (!test_bit(TAINT_PROPRIETARY_MODULE, &mod->taints)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1456) pr_warn("%s: module uses symbols from proprietary module %s, inheriting taint.\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1457) mod->name, owner->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1458) set_bit(TAINT_PROPRIETARY_MODULE, &mod->taints);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1459) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1460) return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1461) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1462)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1463) /* Resolve a symbol for this module. I.e. if we find one, record usage. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1464) static const struct kernel_symbol *resolve_symbol(struct module *mod,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1465) const struct load_info *info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1466) const char *name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1467) char ownername[])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1468) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1469) struct module *owner;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1470) const struct kernel_symbol *sym;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1471) const s32 *crc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1472) enum mod_license license;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1473) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1474)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1475) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1476) * The module_mutex should not be a heavily contended lock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1477) * if we get the occasional sleep here, we'll go an extra iteration
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1478) * in the wait_event_interruptible(), which is harmless.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1479) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1480) sched_annotate_sleep();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1481) mutex_lock(&module_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1482) sym = find_symbol(name, &owner, &crc, &license,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1483) !(mod->taints & (1 << TAINT_PROPRIETARY_MODULE)), true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1484) if (!sym)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1485) goto unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1486)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1487) if (license == GPL_ONLY)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1488) mod->using_gplonly_symbols = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1489)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1490) if (!inherit_taint(mod, owner)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1491) sym = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1492) goto getname;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1493) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1494)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1495) if (!check_version(info, name, mod, crc)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1496) sym = ERR_PTR(-EINVAL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1497) goto getname;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1498) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1499)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1500) err = verify_namespace_is_imported(info, sym, mod);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1501) if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1502) sym = ERR_PTR(err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1503) goto getname;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1504) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1505)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1506) err = ref_module(mod, owner);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1507) if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1508) sym = ERR_PTR(err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1509) goto getname;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1510) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1511)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1512) getname:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1513) /* We must make copy under the lock if we failed to get ref. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1514) strncpy(ownername, module_name(owner), MODULE_NAME_LEN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1515) unlock:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1516) mutex_unlock(&module_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1517) return sym;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1518) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1519)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1520) static const struct kernel_symbol *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1521) resolve_symbol_wait(struct module *mod,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1522) const struct load_info *info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1523) const char *name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1524) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1525) const struct kernel_symbol *ksym;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1526) char owner[MODULE_NAME_LEN];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1527)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1528) if (wait_event_interruptible_timeout(module_wq,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1529) !IS_ERR(ksym = resolve_symbol(mod, info, name, owner))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1530) || PTR_ERR(ksym) != -EBUSY,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1531) 30 * HZ) <= 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1532) pr_warn("%s: gave up waiting for init of module %s.\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1533) mod->name, owner);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1534) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1535) return ksym;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1536) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1537)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1538) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1539) * /sys/module/foo/sections stuff
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1540) * J. Corbet <corbet@lwn.net>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1541) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1542) #ifdef CONFIG_SYSFS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1543)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1544) #ifdef CONFIG_KALLSYMS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1545) static inline bool sect_empty(const Elf_Shdr *sect)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1546) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1547) return !(sect->sh_flags & SHF_ALLOC) || sect->sh_size == 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1548) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1549)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1550) struct module_sect_attr {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1551) struct bin_attribute battr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1552) unsigned long address;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1553) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1554)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1555) struct module_sect_attrs {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1556) struct attribute_group grp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1557) unsigned int nsections;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1558) struct module_sect_attr attrs[];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1559) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1560)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1561) #define MODULE_SECT_READ_SIZE (3 /* "0x", "\n" */ + (BITS_PER_LONG / 4))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1562) static ssize_t module_sect_read(struct file *file, struct kobject *kobj,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1563) struct bin_attribute *battr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1564) char *buf, loff_t pos, size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1565) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1566) struct module_sect_attr *sattr =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1567) container_of(battr, struct module_sect_attr, battr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1568) char bounce[MODULE_SECT_READ_SIZE + 1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1569) size_t wrote;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1570)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1571) if (pos != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1572) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1573)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1574) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1575) * Since we're a binary read handler, we must account for the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1576) * trailing NUL byte that sprintf will write: if "buf" is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1577) * too small to hold the NUL, or the NUL is exactly the last
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1578) * byte, the read will look like it got truncated by one byte.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1579) * Since there is no way to ask sprintf nicely to not write
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1580) * the NUL, we have to use a bounce buffer.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1581) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1582) wrote = scnprintf(bounce, sizeof(bounce), "0x%px\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1583) kallsyms_show_value(file->f_cred)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1584) ? (void *)sattr->address : NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1585) count = min(count, wrote);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1586) memcpy(buf, bounce, count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1587)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1588) return count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1589) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1590)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1591) static void free_sect_attrs(struct module_sect_attrs *sect_attrs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1592) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1593) unsigned int section;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1594)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1595) for (section = 0; section < sect_attrs->nsections; section++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1596) kfree(sect_attrs->attrs[section].battr.attr.name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1597) kfree(sect_attrs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1598) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1599)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1600) static void add_sect_attrs(struct module *mod, const struct load_info *info)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1601) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1602) unsigned int nloaded = 0, i, size[2];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1603) struct module_sect_attrs *sect_attrs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1604) struct module_sect_attr *sattr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1605) struct bin_attribute **gattr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1606)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1607) /* Count loaded sections and allocate structures */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1608) for (i = 0; i < info->hdr->e_shnum; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1609) if (!sect_empty(&info->sechdrs[i]))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1610) nloaded++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1611) size[0] = ALIGN(struct_size(sect_attrs, attrs, nloaded),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1612) sizeof(sect_attrs->grp.bin_attrs[0]));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1613) size[1] = (nloaded + 1) * sizeof(sect_attrs->grp.bin_attrs[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1614) sect_attrs = kzalloc(size[0] + size[1], GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1615) if (sect_attrs == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1616) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1617)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1618) /* Setup section attributes. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1619) sect_attrs->grp.name = "sections";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1620) sect_attrs->grp.bin_attrs = (void *)sect_attrs + size[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1621)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1622) sect_attrs->nsections = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1623) sattr = §_attrs->attrs[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1624) gattr = §_attrs->grp.bin_attrs[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1625) for (i = 0; i < info->hdr->e_shnum; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1626) Elf_Shdr *sec = &info->sechdrs[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1627) if (sect_empty(sec))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1628) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1629) sysfs_bin_attr_init(&sattr->battr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1630) sattr->address = sec->sh_addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1631) sattr->battr.attr.name =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1632) kstrdup(info->secstrings + sec->sh_name, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1633) if (sattr->battr.attr.name == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1634) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1635) sect_attrs->nsections++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1636) sattr->battr.read = module_sect_read;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1637) sattr->battr.size = MODULE_SECT_READ_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1638) sattr->battr.attr.mode = 0400;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1639) *(gattr++) = &(sattr++)->battr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1640) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1641) *gattr = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1642)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1643) if (sysfs_create_group(&mod->mkobj.kobj, §_attrs->grp))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1644) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1645)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1646) mod->sect_attrs = sect_attrs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1647) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1648) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1649) free_sect_attrs(sect_attrs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1650) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1651)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1652) static void remove_sect_attrs(struct module *mod)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1653) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1654) if (mod->sect_attrs) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1655) sysfs_remove_group(&mod->mkobj.kobj,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1656) &mod->sect_attrs->grp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1657) /* We are positive that no one is using any sect attrs
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1658) * at this point. Deallocate immediately. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1659) free_sect_attrs(mod->sect_attrs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1660) mod->sect_attrs = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1661) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1662) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1663)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1664) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1665) * /sys/module/foo/notes/.section.name gives contents of SHT_NOTE sections.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1666) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1667)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1668) struct module_notes_attrs {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1669) struct kobject *dir;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1670) unsigned int notes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1671) struct bin_attribute attrs[];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1672) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1673)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1674) static ssize_t module_notes_read(struct file *filp, struct kobject *kobj,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1675) struct bin_attribute *bin_attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1676) char *buf, loff_t pos, size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1677) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1678) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1679) * The caller checked the pos and count against our size.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1680) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1681) memcpy(buf, bin_attr->private + pos, count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1682) return count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1683) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1684)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1685) static void free_notes_attrs(struct module_notes_attrs *notes_attrs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1686) unsigned int i)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1687) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1688) if (notes_attrs->dir) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1689) while (i-- > 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1690) sysfs_remove_bin_file(notes_attrs->dir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1691) ¬es_attrs->attrs[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1692) kobject_put(notes_attrs->dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1693) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1694) kfree(notes_attrs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1695) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1696)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1697) static void add_notes_attrs(struct module *mod, const struct load_info *info)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1698) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1699) unsigned int notes, loaded, i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1700) struct module_notes_attrs *notes_attrs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1701) struct bin_attribute *nattr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1702)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1703) /* failed to create section attributes, so can't create notes */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1704) if (!mod->sect_attrs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1705) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1706)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1707) /* Count notes sections and allocate structures. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1708) notes = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1709) for (i = 0; i < info->hdr->e_shnum; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1710) if (!sect_empty(&info->sechdrs[i]) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1711) (info->sechdrs[i].sh_type == SHT_NOTE))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1712) ++notes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1713)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1714) if (notes == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1715) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1716)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1717) notes_attrs = kzalloc(struct_size(notes_attrs, attrs, notes),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1718) GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1719) if (notes_attrs == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1720) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1721)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1722) notes_attrs->notes = notes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1723) nattr = ¬es_attrs->attrs[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1724) for (loaded = i = 0; i < info->hdr->e_shnum; ++i) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1725) if (sect_empty(&info->sechdrs[i]))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1726) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1727) if (info->sechdrs[i].sh_type == SHT_NOTE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1728) sysfs_bin_attr_init(nattr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1729) nattr->attr.name = mod->sect_attrs->attrs[loaded].battr.attr.name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1730) nattr->attr.mode = S_IRUGO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1731) nattr->size = info->sechdrs[i].sh_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1732) nattr->private = (void *) info->sechdrs[i].sh_addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1733) nattr->read = module_notes_read;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1734) ++nattr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1735) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1736) ++loaded;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1737) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1738)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1739) notes_attrs->dir = kobject_create_and_add("notes", &mod->mkobj.kobj);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1740) if (!notes_attrs->dir)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1741) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1742)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1743) for (i = 0; i < notes; ++i)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1744) if (sysfs_create_bin_file(notes_attrs->dir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1745) ¬es_attrs->attrs[i]))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1746) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1747)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1748) mod->notes_attrs = notes_attrs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1749) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1750)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1751) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1752) free_notes_attrs(notes_attrs, i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1753) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1754)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1755) static void remove_notes_attrs(struct module *mod)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1756) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1757) if (mod->notes_attrs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1758) free_notes_attrs(mod->notes_attrs, mod->notes_attrs->notes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1759) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1760)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1761) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1762)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1763) static inline void add_sect_attrs(struct module *mod,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1764) const struct load_info *info)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1765) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1766) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1767)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1768) static inline void remove_sect_attrs(struct module *mod)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1769) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1770) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1771)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1772) static inline void add_notes_attrs(struct module *mod,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1773) const struct load_info *info)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1774) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1775) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1776)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1777) static inline void remove_notes_attrs(struct module *mod)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1778) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1779) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1780) #endif /* CONFIG_KALLSYMS */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1781)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1782) static void del_usage_links(struct module *mod)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1783) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1784) #ifdef CONFIG_MODULE_UNLOAD
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1785) struct module_use *use;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1786)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1787) mutex_lock(&module_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1788) list_for_each_entry(use, &mod->target_list, target_list)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1789) sysfs_remove_link(use->target->holders_dir, mod->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1790) mutex_unlock(&module_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1791) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1792) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1793)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1794) static int add_usage_links(struct module *mod)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1795) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1796) int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1797) #ifdef CONFIG_MODULE_UNLOAD
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1798) struct module_use *use;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1799)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1800) mutex_lock(&module_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1801) list_for_each_entry(use, &mod->target_list, target_list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1802) ret = sysfs_create_link(use->target->holders_dir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1803) &mod->mkobj.kobj, mod->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1804) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1805) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1806) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1807) mutex_unlock(&module_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1808) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1809) del_usage_links(mod);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1810) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1811) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1812) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1813)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1814) static void module_remove_modinfo_attrs(struct module *mod, int end);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1815)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1816) static int module_add_modinfo_attrs(struct module *mod)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1817) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1818) struct module_attribute *attr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1819) struct module_attribute *temp_attr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1820) int error = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1821) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1822)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1823) mod->modinfo_attrs = kzalloc((sizeof(struct module_attribute) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1824) (ARRAY_SIZE(modinfo_attrs) + 1)),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1825) GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1826) if (!mod->modinfo_attrs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1827) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1828)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1829) temp_attr = mod->modinfo_attrs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1830) for (i = 0; (attr = modinfo_attrs[i]); i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1831) if (!attr->test || attr->test(mod)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1832) memcpy(temp_attr, attr, sizeof(*temp_attr));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1833) sysfs_attr_init(&temp_attr->attr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1834) error = sysfs_create_file(&mod->mkobj.kobj,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1835) &temp_attr->attr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1836) if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1837) goto error_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1838) ++temp_attr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1839) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1840) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1841)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1842) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1843)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1844) error_out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1845) if (i > 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1846) module_remove_modinfo_attrs(mod, --i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1847) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1848) kfree(mod->modinfo_attrs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1849) return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1850) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1851)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1852) static void module_remove_modinfo_attrs(struct module *mod, int end)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1853) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1854) struct module_attribute *attr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1855) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1856)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1857) for (i = 0; (attr = &mod->modinfo_attrs[i]); i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1858) if (end >= 0 && i > end)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1859) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1860) /* pick a field to test for end of list */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1861) if (!attr->attr.name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1862) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1863) sysfs_remove_file(&mod->mkobj.kobj, &attr->attr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1864) if (attr->free)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1865) attr->free(mod);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1866) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1867) kfree(mod->modinfo_attrs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1868) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1869)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1870) static void mod_kobject_put(struct module *mod)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1871) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1872) DECLARE_COMPLETION_ONSTACK(c);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1873) mod->mkobj.kobj_completion = &c;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1874) kobject_put(&mod->mkobj.kobj);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1875) wait_for_completion(&c);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1876) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1877)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1878) static int mod_sysfs_init(struct module *mod)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1879) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1880) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1881) struct kobject *kobj;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1882)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1883) if (!module_sysfs_initialized) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1884) pr_err("%s: module sysfs not initialized\n", mod->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1885) err = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1886) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1887) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1888)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1889) kobj = kset_find_obj(module_kset, mod->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1890) if (kobj) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1891) pr_err("%s: module is already loaded\n", mod->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1892) kobject_put(kobj);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1893) err = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1894) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1895) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1896)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1897) mod->mkobj.mod = mod;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1898)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1899) memset(&mod->mkobj.kobj, 0, sizeof(mod->mkobj.kobj));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1900) mod->mkobj.kobj.kset = module_kset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1901) err = kobject_init_and_add(&mod->mkobj.kobj, &module_ktype, NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1902) "%s", mod->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1903) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1904) mod_kobject_put(mod);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1905)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1906) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1907) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1908) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1909)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1910) static int mod_sysfs_setup(struct module *mod,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1911) const struct load_info *info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1912) struct kernel_param *kparam,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1913) unsigned int num_params)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1914) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1915) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1916)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1917) err = mod_sysfs_init(mod);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1918) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1919) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1920)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1921) mod->holders_dir = kobject_create_and_add("holders", &mod->mkobj.kobj);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1922) if (!mod->holders_dir) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1923) err = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1924) goto out_unreg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1925) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1926)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1927) err = module_param_sysfs_setup(mod, kparam, num_params);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1928) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1929) goto out_unreg_holders;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1930)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1931) err = module_add_modinfo_attrs(mod);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1932) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1933) goto out_unreg_param;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1934)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1935) err = add_usage_links(mod);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1936) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1937) goto out_unreg_modinfo_attrs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1938)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1939) add_sect_attrs(mod, info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1940) add_notes_attrs(mod, info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1941)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1942) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1943)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1944) out_unreg_modinfo_attrs:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1945) module_remove_modinfo_attrs(mod, -1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1946) out_unreg_param:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1947) module_param_sysfs_remove(mod);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1948) out_unreg_holders:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1949) kobject_put(mod->holders_dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1950) out_unreg:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1951) mod_kobject_put(mod);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1952) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1953) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1954) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1955)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1956) static void mod_sysfs_fini(struct module *mod)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1957) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1958) remove_notes_attrs(mod);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1959) remove_sect_attrs(mod);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1960) mod_kobject_put(mod);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1961) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1962)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1963) static void init_param_lock(struct module *mod)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1964) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1965) mutex_init(&mod->param_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1966) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1967) #else /* !CONFIG_SYSFS */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1968)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1969) static int mod_sysfs_setup(struct module *mod,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1970) const struct load_info *info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1971) struct kernel_param *kparam,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1972) unsigned int num_params)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1973) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1974) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1975) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1976)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1977) static void mod_sysfs_fini(struct module *mod)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1978) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1979) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1980)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1981) static void module_remove_modinfo_attrs(struct module *mod, int end)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1982) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1983) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1984)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1985) static void del_usage_links(struct module *mod)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1986) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1987) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1988)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1989) static void init_param_lock(struct module *mod)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1990) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1991) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1992) #endif /* CONFIG_SYSFS */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1993)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1994) static void mod_sysfs_teardown(struct module *mod)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1995) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1996) del_usage_links(mod);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1997) module_remove_modinfo_attrs(mod, -1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1998) module_param_sysfs_remove(mod);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1999) kobject_put(mod->mkobj.drivers_dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2000) kobject_put(mod->holders_dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2001) mod_sysfs_fini(mod);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2002) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2003)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2004) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2005) * LKM RO/NX protection: protect module's text/ro-data
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2006) * from modification and any data from execution.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2007) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2008) * General layout of module is:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2009) * [text] [read-only-data] [ro-after-init] [writable data]
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2010) * text_size -----^ ^ ^ ^
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2011) * ro_size ------------------------| | |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2012) * ro_after_init_size -----------------------------| |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2013) * size -----------------------------------------------------------|
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2014) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2015) * These values are always page-aligned (as is base)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2016) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2017)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2018) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2019) * Since some arches are moving towards PAGE_KERNEL module allocations instead
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2020) * of PAGE_KERNEL_EXEC, keep frob_text() and module_enable_x() outside of the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2021) * CONFIG_STRICT_MODULE_RWX block below because they are needed regardless of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2022) * whether we are strict.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2023) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2024) #ifdef CONFIG_ARCH_HAS_STRICT_MODULE_RWX
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2025) static void frob_text(const struct module_layout *layout,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2026) int (*set_memory)(unsigned long start, int num_pages))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2027) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2028) BUG_ON((unsigned long)layout->base & (PAGE_SIZE-1));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2029) BUG_ON((unsigned long)layout->text_size & (PAGE_SIZE-1));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2030) set_memory((unsigned long)layout->base,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2031) layout->text_size >> PAGE_SHIFT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2032) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2033)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2034) static void module_enable_x(const struct module *mod)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2035) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2036) frob_text(&mod->core_layout, set_memory_x);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2037) frob_text(&mod->init_layout, set_memory_x);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2038) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2039) #else /* !CONFIG_ARCH_HAS_STRICT_MODULE_RWX */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2040) static void module_enable_x(const struct module *mod) { }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2041) #endif /* CONFIG_ARCH_HAS_STRICT_MODULE_RWX */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2042)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2043) #ifdef CONFIG_STRICT_MODULE_RWX
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2044) static void frob_rodata(const struct module_layout *layout,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2045) int (*set_memory)(unsigned long start, int num_pages))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2046) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2047) BUG_ON((unsigned long)layout->base & (PAGE_SIZE-1));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2048) BUG_ON((unsigned long)layout->text_size & (PAGE_SIZE-1));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2049) BUG_ON((unsigned long)layout->ro_size & (PAGE_SIZE-1));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2050) set_memory((unsigned long)layout->base + layout->text_size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2051) (layout->ro_size - layout->text_size) >> PAGE_SHIFT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2052) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2053)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2054) static void frob_ro_after_init(const struct module_layout *layout,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2055) int (*set_memory)(unsigned long start, int num_pages))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2056) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2057) BUG_ON((unsigned long)layout->base & (PAGE_SIZE-1));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2058) BUG_ON((unsigned long)layout->ro_size & (PAGE_SIZE-1));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2059) BUG_ON((unsigned long)layout->ro_after_init_size & (PAGE_SIZE-1));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2060) set_memory((unsigned long)layout->base + layout->ro_size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2061) (layout->ro_after_init_size - layout->ro_size) >> PAGE_SHIFT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2062) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2063)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2064) static void frob_writable_data(const struct module_layout *layout,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2065) int (*set_memory)(unsigned long start, int num_pages))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2066) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2067) BUG_ON((unsigned long)layout->base & (PAGE_SIZE-1));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2068) BUG_ON((unsigned long)layout->ro_after_init_size & (PAGE_SIZE-1));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2069) BUG_ON((unsigned long)layout->size & (PAGE_SIZE-1));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2070) set_memory((unsigned long)layout->base + layout->ro_after_init_size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2071) (layout->size - layout->ro_after_init_size) >> PAGE_SHIFT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2072) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2073)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2074) static void module_enable_ro(const struct module *mod, bool after_init)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2075) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2076) if (!rodata_enabled)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2077) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2078)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2079) set_vm_flush_reset_perms(mod->core_layout.base);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2080) set_vm_flush_reset_perms(mod->init_layout.base);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2081) frob_text(&mod->core_layout, set_memory_ro);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2082)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2083) frob_rodata(&mod->core_layout, set_memory_ro);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2084) frob_text(&mod->init_layout, set_memory_ro);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2085) frob_rodata(&mod->init_layout, set_memory_ro);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2086)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2087) if (after_init)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2088) frob_ro_after_init(&mod->core_layout, set_memory_ro);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2089) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2090)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2091) static void module_enable_nx(const struct module *mod)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2092) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2093) frob_rodata(&mod->core_layout, set_memory_nx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2094) frob_ro_after_init(&mod->core_layout, set_memory_nx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2095) frob_writable_data(&mod->core_layout, set_memory_nx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2096) frob_rodata(&mod->init_layout, set_memory_nx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2097) frob_writable_data(&mod->init_layout, set_memory_nx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2098) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2099)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2100) static int module_enforce_rwx_sections(Elf_Ehdr *hdr, Elf_Shdr *sechdrs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2101) char *secstrings, struct module *mod)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2102) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2103) const unsigned long shf_wx = SHF_WRITE|SHF_EXECINSTR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2104) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2105)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2106) for (i = 0; i < hdr->e_shnum; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2107) if ((sechdrs[i].sh_flags & shf_wx) == shf_wx) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2108) pr_err("%s: section %s (index %d) has invalid WRITE|EXEC flags\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2109) mod->name, secstrings + sechdrs[i].sh_name, i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2110) return -ENOEXEC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2111) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2112) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2113)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2114) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2115) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2116)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2117) #else /* !CONFIG_STRICT_MODULE_RWX */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2118) static void module_enable_nx(const struct module *mod) { }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2119) static void module_enable_ro(const struct module *mod, bool after_init) {}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2120) static int module_enforce_rwx_sections(Elf_Ehdr *hdr, Elf_Shdr *sechdrs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2121) char *secstrings, struct module *mod)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2122) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2123) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2124) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2125) #endif /* CONFIG_STRICT_MODULE_RWX */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2126)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2127) #ifdef CONFIG_LIVEPATCH
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2128) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2129) * Persist Elf information about a module. Copy the Elf header,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2130) * section header table, section string table, and symtab section
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2131) * index from info to mod->klp_info.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2132) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2133) static int copy_module_elf(struct module *mod, struct load_info *info)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2134) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2135) unsigned int size, symndx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2136) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2137)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2138) size = sizeof(*mod->klp_info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2139) mod->klp_info = kmalloc(size, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2140) if (mod->klp_info == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2141) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2142)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2143) /* Elf header */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2144) size = sizeof(mod->klp_info->hdr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2145) memcpy(&mod->klp_info->hdr, info->hdr, size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2146)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2147) /* Elf section header table */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2148) size = sizeof(*info->sechdrs) * info->hdr->e_shnum;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2149) mod->klp_info->sechdrs = kmemdup(info->sechdrs, size, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2150) if (mod->klp_info->sechdrs == NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2151) ret = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2152) goto free_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2153) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2154)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2155) /* Elf section name string table */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2156) size = info->sechdrs[info->hdr->e_shstrndx].sh_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2157) mod->klp_info->secstrings = kmemdup(info->secstrings, size, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2158) if (mod->klp_info->secstrings == NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2159) ret = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2160) goto free_sechdrs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2161) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2162)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2163) /* Elf symbol section index */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2164) symndx = info->index.sym;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2165) mod->klp_info->symndx = symndx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2166)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2167) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2168) * For livepatch modules, core_kallsyms.symtab is a complete
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2169) * copy of the original symbol table. Adjust sh_addr to point
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2170) * to core_kallsyms.symtab since the copy of the symtab in module
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2171) * init memory is freed at the end of do_init_module().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2172) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2173) mod->klp_info->sechdrs[symndx].sh_addr = \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2174) (unsigned long) mod->core_kallsyms.symtab;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2175)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2176) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2177)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2178) free_sechdrs:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2179) kfree(mod->klp_info->sechdrs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2180) free_info:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2181) kfree(mod->klp_info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2182) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2183) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2184)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2185) static void free_module_elf(struct module *mod)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2186) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2187) kfree(mod->klp_info->sechdrs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2188) kfree(mod->klp_info->secstrings);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2189) kfree(mod->klp_info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2190) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2191) #else /* !CONFIG_LIVEPATCH */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2192) static int copy_module_elf(struct module *mod, struct load_info *info)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2193) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2194) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2195) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2196)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2197) static void free_module_elf(struct module *mod)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2198) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2199) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2200) #endif /* CONFIG_LIVEPATCH */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2201)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2202) void __weak module_memfree(void *module_region)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2203) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2204) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2205) * This memory may be RO, and freeing RO memory in an interrupt is not
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2206) * supported by vmalloc.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2207) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2208) WARN_ON(in_interrupt());
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2209) vfree(module_region);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2210) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2211)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2212) void __weak module_arch_cleanup(struct module *mod)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2213) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2214) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2215)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2216) void __weak module_arch_freeing_init(struct module *mod)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2217) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2218) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2219)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2220) static void cfi_cleanup(struct module *mod);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2221)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2222) /* Free a module, remove from lists, etc. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2223) static void free_module(struct module *mod)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2224) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2225) trace_module_free(mod);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2226)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2227) mod_sysfs_teardown(mod);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2228)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2229) /* We leave it in list to prevent duplicate loads, but make sure
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2230) * that noone uses it while it's being deconstructed. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2231) mutex_lock(&module_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2232) mod->state = MODULE_STATE_UNFORMED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2233) mutex_unlock(&module_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2234)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2235) /* Remove dynamic debug info */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2236) ddebug_remove_module(mod->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2237)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2238) /* Arch-specific cleanup. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2239) module_arch_cleanup(mod);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2240)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2241) /* Module unload stuff */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2242) module_unload_free(mod);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2243)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2244) /* Free any allocated parameters. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2245) destroy_params(mod->kp, mod->num_kp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2246)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2247) if (is_livepatch_module(mod))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2248) free_module_elf(mod);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2249)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2250) /* Now we can delete it from the lists */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2251) mutex_lock(&module_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2252) /* Unlink carefully: kallsyms could be walking list. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2253) list_del_rcu(&mod->list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2254) mod_tree_remove(mod);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2255) /* Remove this module from bug list, this uses list_del_rcu */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2256) module_bug_cleanup(mod);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2257) /* Wait for RCU-sched synchronizing before releasing mod->list and buglist. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2258) synchronize_rcu();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2259) mutex_unlock(&module_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2260)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2261) /* Clean up CFI for the module. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2262) cfi_cleanup(mod);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2263)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2264) /* This may be empty, but that's OK */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2265) module_arch_freeing_init(mod);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2266) trace_android_vh_set_memory_rw((unsigned long)mod->init_layout.base,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2267) (mod->init_layout.size)>>PAGE_SHIFT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2268) trace_android_vh_set_memory_nx((unsigned long)mod->init_layout.base,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2269) (mod->init_layout.size)>>PAGE_SHIFT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2270) module_memfree(mod->init_layout.base);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2271) kfree(mod->args);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2272) percpu_modfree(mod);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2273)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2274) /* Free lock-classes; relies on the preceding sync_rcu(). */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2275) lockdep_free_key_range(mod->core_layout.base, mod->core_layout.size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2276)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2277) /* Finally, free the core (containing the module structure) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2278) trace_android_vh_set_memory_rw((unsigned long)mod->core_layout.base,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2279) (mod->core_layout.size)>>PAGE_SHIFT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2280) trace_android_vh_set_memory_nx((unsigned long)mod->core_layout.base,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2281) (mod->core_layout.size)>>PAGE_SHIFT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2282) module_memfree(mod->core_layout.base);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2283) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2284)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2285) void *__symbol_get(const char *symbol)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2286) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2287) struct module *owner;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2288) const struct kernel_symbol *sym;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2289)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2290) preempt_disable();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2291) sym = find_symbol(symbol, &owner, NULL, NULL, true, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2292) if (sym && strong_try_module_get(owner))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2293) sym = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2294) preempt_enable();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2295)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2296) return sym ? (void *)kernel_symbol_value(sym) : NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2297) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2298) EXPORT_SYMBOL_GPL(__symbol_get);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2299)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2300) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2301) * Ensure that an exported symbol [global namespace] does not already exist
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2302) * in the kernel or in some other module's exported symbol table.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2303) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2304) * You must hold the module_mutex.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2305) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2306) static int verify_exported_symbols(struct module *mod)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2307) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2308) unsigned int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2309) struct module *owner;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2310) const struct kernel_symbol *s;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2311) struct {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2312) const struct kernel_symbol *sym;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2313) unsigned int num;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2314) } arr[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2315) { mod->syms, mod->num_syms },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2316) { mod->gpl_syms, mod->num_gpl_syms },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2317) { mod->gpl_future_syms, mod->num_gpl_future_syms },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2318) #ifdef CONFIG_UNUSED_SYMBOLS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2319) { mod->unused_syms, mod->num_unused_syms },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2320) { mod->unused_gpl_syms, mod->num_unused_gpl_syms },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2321) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2322) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2323)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2324) for (i = 0; i < ARRAY_SIZE(arr); i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2325) for (s = arr[i].sym; s < arr[i].sym + arr[i].num; s++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2326) if (find_symbol(kernel_symbol_name(s), &owner, NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2327) NULL, true, false)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2328) pr_err("%s: exports duplicate symbol %s"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2329) " (owned by %s)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2330) mod->name, kernel_symbol_name(s),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2331) module_name(owner));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2332) return -ENOEXEC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2333) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2334) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2335) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2336) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2337) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2338)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2339) static bool ignore_undef_symbol(Elf_Half emachine, const char *name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2340) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2341) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2342) * On x86, PIC code and Clang non-PIC code may have call foo@PLT. GNU as
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2343) * before 2.37 produces an unreferenced _GLOBAL_OFFSET_TABLE_ on x86-64.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2344) * i386 has a similar problem but may not deserve a fix.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2345) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2346) * If we ever have to ignore many symbols, consider refactoring the code to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2347) * only warn if referenced by a relocation.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2348) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2349) if (emachine == EM_386 || emachine == EM_X86_64)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2350) return !strcmp(name, "_GLOBAL_OFFSET_TABLE_");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2351) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2352) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2353)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2354) /* Change all symbols so that st_value encodes the pointer directly. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2355) static int simplify_symbols(struct module *mod, const struct load_info *info)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2356) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2357) Elf_Shdr *symsec = &info->sechdrs[info->index.sym];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2358) Elf_Sym *sym = (void *)symsec->sh_addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2359) unsigned long secbase;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2360) unsigned int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2361) int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2362) const struct kernel_symbol *ksym;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2363)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2364) for (i = 1; i < symsec->sh_size / sizeof(Elf_Sym); i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2365) const char *name = info->strtab + sym[i].st_name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2366)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2367) switch (sym[i].st_shndx) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2368) case SHN_COMMON:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2369) /* Ignore common symbols */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2370) if (!strncmp(name, "__gnu_lto", 9))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2371) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2372)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2373) /* We compiled with -fno-common. These are not
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2374) supposed to happen. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2375) pr_debug("Common symbol: %s\n", name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2376) pr_warn("%s: please compile with -fno-common\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2377) mod->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2378) ret = -ENOEXEC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2379) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2380)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2381) case SHN_ABS:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2382) /* Don't need to do anything */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2383) pr_debug("Absolute symbol: 0x%08lx\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2384) (long)sym[i].st_value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2385) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2386)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2387) case SHN_LIVEPATCH:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2388) /* Livepatch symbols are resolved by livepatch */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2389) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2390)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2391) case SHN_UNDEF:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2392) ksym = resolve_symbol_wait(mod, info, name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2393) /* Ok if resolved. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2394) if (ksym && !IS_ERR(ksym)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2395) sym[i].st_value = kernel_symbol_value(ksym);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2396) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2397) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2398)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2399) /* Ok if weak or ignored. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2400) if (!ksym &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2401) (ELF_ST_BIND(sym[i].st_info) == STB_WEAK ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2402) ignore_undef_symbol(info->hdr->e_machine, name)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2403) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2404)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2405) ret = PTR_ERR(ksym) ?: -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2406) pr_warn("%s: Unknown symbol %s (err %d)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2407) mod->name, name, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2408) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2409)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2410) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2411) /* Divert to percpu allocation if a percpu var. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2412) if (sym[i].st_shndx == info->index.pcpu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2413) secbase = (unsigned long)mod_percpu(mod);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2414) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2415) secbase = info->sechdrs[sym[i].st_shndx].sh_addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2416) sym[i].st_value += secbase;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2417) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2418) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2419) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2420)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2421) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2422) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2423)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2424) static int apply_relocations(struct module *mod, const struct load_info *info)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2425) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2426) unsigned int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2427) int err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2428)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2429) /* Now do relocations. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2430) for (i = 1; i < info->hdr->e_shnum; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2431) unsigned int infosec = info->sechdrs[i].sh_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2432)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2433) /* Not a valid relocation section? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2434) if (infosec >= info->hdr->e_shnum)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2435) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2436)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2437) /* Don't bother with non-allocated sections */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2438) if (!(info->sechdrs[infosec].sh_flags & SHF_ALLOC))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2439) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2440)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2441) if (info->sechdrs[i].sh_flags & SHF_RELA_LIVEPATCH)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2442) err = klp_apply_section_relocs(mod, info->sechdrs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2443) info->secstrings,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2444) info->strtab,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2445) info->index.sym, i,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2446) NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2447) else if (info->sechdrs[i].sh_type == SHT_REL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2448) err = apply_relocate(info->sechdrs, info->strtab,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2449) info->index.sym, i, mod);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2450) else if (info->sechdrs[i].sh_type == SHT_RELA)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2451) err = apply_relocate_add(info->sechdrs, info->strtab,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2452) info->index.sym, i, mod);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2453) if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2454) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2455) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2456) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2457) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2458)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2459) /* Additional bytes needed by arch in front of individual sections */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2460) unsigned int __weak arch_mod_section_prepend(struct module *mod,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2461) unsigned int section)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2462) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2463) /* default implementation just returns zero */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2464) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2465) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2466)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2467) /* Update size with this section: return offset. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2468) static long get_offset(struct module *mod, unsigned int *size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2469) Elf_Shdr *sechdr, unsigned int section)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2470) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2471) long ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2472)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2473) *size += arch_mod_section_prepend(mod, section);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2474) ret = ALIGN(*size, sechdr->sh_addralign ?: 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2475) *size = ret + sechdr->sh_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2476) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2477) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2478)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2479) /* Lay out the SHF_ALLOC sections in a way not dissimilar to how ld
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2480) might -- code, read-only data, read-write data, small data. Tally
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2481) sizes, and place the offsets into sh_entsize fields: high bit means it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2482) belongs in init. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2483) static void layout_sections(struct module *mod, struct load_info *info)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2484) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2485) static unsigned long const masks[][2] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2486) /* NOTE: all executable code must be the first section
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2487) * in this array; otherwise modify the text_size
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2488) * finder in the two loops below */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2489) { SHF_EXECINSTR | SHF_ALLOC, ARCH_SHF_SMALL },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2490) { SHF_ALLOC, SHF_WRITE | ARCH_SHF_SMALL },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2491) { SHF_RO_AFTER_INIT | SHF_ALLOC, ARCH_SHF_SMALL },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2492) { SHF_WRITE | SHF_ALLOC, ARCH_SHF_SMALL },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2493) { ARCH_SHF_SMALL | SHF_ALLOC, 0 }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2494) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2495) unsigned int m, i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2496)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2497) for (i = 0; i < info->hdr->e_shnum; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2498) info->sechdrs[i].sh_entsize = ~0UL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2499)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2500) pr_debug("Core section allocation order:\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2501) for (m = 0; m < ARRAY_SIZE(masks); ++m) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2502) for (i = 0; i < info->hdr->e_shnum; ++i) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2503) Elf_Shdr *s = &info->sechdrs[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2504) const char *sname = info->secstrings + s->sh_name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2505)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2506) if ((s->sh_flags & masks[m][0]) != masks[m][0]
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2507) || (s->sh_flags & masks[m][1])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2508) || s->sh_entsize != ~0UL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2509) || module_init_section(sname))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2510) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2511) s->sh_entsize = get_offset(mod, &mod->core_layout.size, s, i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2512) pr_debug("\t%s\n", sname);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2513) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2514) switch (m) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2515) case 0: /* executable */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2516) mod->core_layout.size = debug_align(mod->core_layout.size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2517) mod->core_layout.text_size = mod->core_layout.size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2518) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2519) case 1: /* RO: text and ro-data */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2520) mod->core_layout.size = debug_align(mod->core_layout.size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2521) mod->core_layout.ro_size = mod->core_layout.size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2522) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2523) case 2: /* RO after init */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2524) mod->core_layout.size = debug_align(mod->core_layout.size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2525) mod->core_layout.ro_after_init_size = mod->core_layout.size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2526) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2527) case 4: /* whole core */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2528) mod->core_layout.size = debug_align(mod->core_layout.size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2529) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2530) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2531) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2532)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2533) pr_debug("Init section allocation order:\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2534) for (m = 0; m < ARRAY_SIZE(masks); ++m) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2535) for (i = 0; i < info->hdr->e_shnum; ++i) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2536) Elf_Shdr *s = &info->sechdrs[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2537) const char *sname = info->secstrings + s->sh_name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2538)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2539) if ((s->sh_flags & masks[m][0]) != masks[m][0]
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2540) || (s->sh_flags & masks[m][1])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2541) || s->sh_entsize != ~0UL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2542) || !module_init_section(sname))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2543) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2544) s->sh_entsize = (get_offset(mod, &mod->init_layout.size, s, i)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2545) | INIT_OFFSET_MASK);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2546) pr_debug("\t%s\n", sname);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2547) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2548) switch (m) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2549) case 0: /* executable */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2550) mod->init_layout.size = debug_align(mod->init_layout.size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2551) mod->init_layout.text_size = mod->init_layout.size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2552) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2553) case 1: /* RO: text and ro-data */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2554) mod->init_layout.size = debug_align(mod->init_layout.size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2555) mod->init_layout.ro_size = mod->init_layout.size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2556) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2557) case 2:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2558) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2559) * RO after init doesn't apply to init_layout (only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2560) * core_layout), so it just takes the value of ro_size.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2561) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2562) mod->init_layout.ro_after_init_size = mod->init_layout.ro_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2563) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2564) case 4: /* whole init */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2565) mod->init_layout.size = debug_align(mod->init_layout.size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2566) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2567) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2568) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2569) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2570)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2571) static void set_license(struct module *mod, const char *license)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2572) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2573) if (!license)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2574) license = "unspecified";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2575)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2576) if (!license_is_gpl_compatible(license)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2577) if (!test_taint(TAINT_PROPRIETARY_MODULE))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2578) pr_warn("%s: module license '%s' taints kernel.\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2579) mod->name, license);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2580) add_taint_module(mod, TAINT_PROPRIETARY_MODULE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2581) LOCKDEP_NOW_UNRELIABLE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2582) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2583) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2584)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2585) /* Parse tag=value strings from .modinfo section */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2586) static char *next_string(char *string, unsigned long *secsize)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2587) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2588) /* Skip non-zero chars */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2589) while (string[0]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2590) string++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2591) if ((*secsize)-- <= 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2592) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2593) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2594)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2595) /* Skip any zero padding. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2596) while (!string[0]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2597) string++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2598) if ((*secsize)-- <= 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2599) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2600) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2601) return string;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2602) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2603)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2604) static char *get_next_modinfo(const struct load_info *info, const char *tag,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2605) char *prev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2606) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2607) char *p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2608) unsigned int taglen = strlen(tag);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2609) Elf_Shdr *infosec = &info->sechdrs[info->index.info];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2610) unsigned long size = infosec->sh_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2611)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2612) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2613) * get_modinfo() calls made before rewrite_section_headers()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2614) * must use sh_offset, as sh_addr isn't set!
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2615) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2616) char *modinfo = (char *)info->hdr + infosec->sh_offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2617)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2618) if (prev) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2619) size -= prev - modinfo;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2620) modinfo = next_string(prev, &size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2621) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2622)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2623) for (p = modinfo; p; p = next_string(p, &size)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2624) if (strncmp(p, tag, taglen) == 0 && p[taglen] == '=')
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2625) return p + taglen + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2626) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2627) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2628) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2629)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2630) static char *get_modinfo(const struct load_info *info, const char *tag)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2631) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2632) return get_next_modinfo(info, tag, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2633) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2634)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2635) static void setup_modinfo(struct module *mod, struct load_info *info)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2636) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2637) struct module_attribute *attr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2638) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2639)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2640) for (i = 0; (attr = modinfo_attrs[i]); i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2641) if (attr->setup)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2642) attr->setup(mod, get_modinfo(info, attr->attr.name));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2643) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2644) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2645)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2646) static void free_modinfo(struct module *mod)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2647) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2648) struct module_attribute *attr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2649) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2650)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2651) for (i = 0; (attr = modinfo_attrs[i]); i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2652) if (attr->free)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2653) attr->free(mod);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2654) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2655) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2656)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2657) #ifdef CONFIG_KALLSYMS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2658)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2659) /* Lookup exported symbol in given range of kernel_symbols */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2660) static const struct kernel_symbol *lookup_exported_symbol(const char *name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2661) const struct kernel_symbol *start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2662) const struct kernel_symbol *stop)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2663) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2664) return bsearch(name, start, stop - start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2665) sizeof(struct kernel_symbol), cmp_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2666) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2667)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2668) static int is_exported(const char *name, unsigned long value,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2669) const struct module *mod)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2670) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2671) const struct kernel_symbol *ks;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2672) if (!mod)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2673) ks = lookup_exported_symbol(name, __start___ksymtab, __stop___ksymtab);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2674) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2675) ks = lookup_exported_symbol(name, mod->syms, mod->syms + mod->num_syms);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2676)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2677) return ks != NULL && kernel_symbol_value(ks) == value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2678) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2679)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2680) /* As per nm */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2681) static char elf_type(const Elf_Sym *sym, const struct load_info *info)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2682) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2683) const Elf_Shdr *sechdrs = info->sechdrs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2684)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2685) if (ELF_ST_BIND(sym->st_info) == STB_WEAK) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2686) if (ELF_ST_TYPE(sym->st_info) == STT_OBJECT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2687) return 'v';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2688) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2689) return 'w';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2690) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2691) if (sym->st_shndx == SHN_UNDEF)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2692) return 'U';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2693) if (sym->st_shndx == SHN_ABS || sym->st_shndx == info->index.pcpu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2694) return 'a';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2695) if (sym->st_shndx >= SHN_LORESERVE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2696) return '?';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2697) if (sechdrs[sym->st_shndx].sh_flags & SHF_EXECINSTR)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2698) return 't';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2699) if (sechdrs[sym->st_shndx].sh_flags & SHF_ALLOC
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2700) && sechdrs[sym->st_shndx].sh_type != SHT_NOBITS) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2701) if (!(sechdrs[sym->st_shndx].sh_flags & SHF_WRITE))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2702) return 'r';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2703) else if (sechdrs[sym->st_shndx].sh_flags & ARCH_SHF_SMALL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2704) return 'g';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2705) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2706) return 'd';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2707) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2708) if (sechdrs[sym->st_shndx].sh_type == SHT_NOBITS) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2709) if (sechdrs[sym->st_shndx].sh_flags & ARCH_SHF_SMALL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2710) return 's';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2711) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2712) return 'b';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2713) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2714) if (strstarts(info->secstrings + sechdrs[sym->st_shndx].sh_name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2715) ".debug")) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2716) return 'n';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2717) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2718) return '?';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2719) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2720)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2721) static bool is_core_symbol(const Elf_Sym *src, const Elf_Shdr *sechdrs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2722) unsigned int shnum, unsigned int pcpundx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2723) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2724) const Elf_Shdr *sec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2725)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2726) if (src->st_shndx == SHN_UNDEF
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2727) || src->st_shndx >= shnum
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2728) || !src->st_name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2729) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2730)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2731) #ifdef CONFIG_KALLSYMS_ALL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2732) if (src->st_shndx == pcpundx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2733) return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2734) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2735)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2736) sec = sechdrs + src->st_shndx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2737) if (!(sec->sh_flags & SHF_ALLOC)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2738) #ifndef CONFIG_KALLSYMS_ALL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2739) || !(sec->sh_flags & SHF_EXECINSTR)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2740) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2741) || (sec->sh_entsize & INIT_OFFSET_MASK))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2742) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2743)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2744) return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2745) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2746)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2747) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2748) * We only allocate and copy the strings needed by the parts of symtab
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2749) * we keep. This is simple, but has the effect of making multiple
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2750) * copies of duplicates. We could be more sophisticated, see
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2751) * linux-kernel thread starting with
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2752) * <73defb5e4bca04a6431392cc341112b1@localhost>.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2753) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2754) static void layout_symtab(struct module *mod, struct load_info *info)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2755) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2756) Elf_Shdr *symsect = info->sechdrs + info->index.sym;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2757) Elf_Shdr *strsect = info->sechdrs + info->index.str;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2758) const Elf_Sym *src;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2759) unsigned int i, nsrc, ndst, strtab_size = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2760)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2761) /* Put symbol section at end of init part of module. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2762) symsect->sh_flags |= SHF_ALLOC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2763) symsect->sh_entsize = get_offset(mod, &mod->init_layout.size, symsect,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2764) info->index.sym) | INIT_OFFSET_MASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2765) pr_debug("\t%s\n", info->secstrings + symsect->sh_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2766)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2767) src = (void *)info->hdr + symsect->sh_offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2768) nsrc = symsect->sh_size / sizeof(*src);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2769)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2770) /* Compute total space required for the core symbols' strtab. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2771) for (ndst = i = 0; i < nsrc; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2772) if (i == 0 || is_livepatch_module(mod) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2773) is_core_symbol(src+i, info->sechdrs, info->hdr->e_shnum,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2774) info->index.pcpu)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2775) strtab_size += strlen(&info->strtab[src[i].st_name])+1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2776) ndst++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2777) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2778) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2779)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2780) /* Append room for core symbols at end of core part. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2781) info->symoffs = ALIGN(mod->core_layout.size, symsect->sh_addralign ?: 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2782) info->stroffs = mod->core_layout.size = info->symoffs + ndst * sizeof(Elf_Sym);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2783) mod->core_layout.size += strtab_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2784) info->core_typeoffs = mod->core_layout.size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2785) mod->core_layout.size += ndst * sizeof(char);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2786) mod->core_layout.size = debug_align(mod->core_layout.size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2787)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2788) /* Put string table section at end of init part of module. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2789) strsect->sh_flags |= SHF_ALLOC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2790) strsect->sh_entsize = get_offset(mod, &mod->init_layout.size, strsect,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2791) info->index.str) | INIT_OFFSET_MASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2792) pr_debug("\t%s\n", info->secstrings + strsect->sh_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2793)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2794) /* We'll tack temporary mod_kallsyms on the end. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2795) mod->init_layout.size = ALIGN(mod->init_layout.size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2796) __alignof__(struct mod_kallsyms));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2797) info->mod_kallsyms_init_off = mod->init_layout.size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2798) mod->init_layout.size += sizeof(struct mod_kallsyms);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2799) info->init_typeoffs = mod->init_layout.size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2800) mod->init_layout.size += nsrc * sizeof(char);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2801) mod->init_layout.size = debug_align(mod->init_layout.size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2802) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2803)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2804) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2805) * We use the full symtab and strtab which layout_symtab arranged to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2806) * be appended to the init section. Later we switch to the cut-down
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2807) * core-only ones.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2808) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2809) static void add_kallsyms(struct module *mod, const struct load_info *info)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2810) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2811) unsigned int i, ndst;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2812) const Elf_Sym *src;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2813) Elf_Sym *dst;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2814) char *s;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2815) Elf_Shdr *symsec = &info->sechdrs[info->index.sym];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2816)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2817) /* Set up to point into init section. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2818) mod->kallsyms = mod->init_layout.base + info->mod_kallsyms_init_off;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2819)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2820) mod->kallsyms->symtab = (void *)symsec->sh_addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2821) mod->kallsyms->num_symtab = symsec->sh_size / sizeof(Elf_Sym);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2822) /* Make sure we get permanent strtab: don't use info->strtab. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2823) mod->kallsyms->strtab = (void *)info->sechdrs[info->index.str].sh_addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2824) mod->kallsyms->typetab = mod->init_layout.base + info->init_typeoffs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2825)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2826) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2827) * Now populate the cut down core kallsyms for after init
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2828) * and set types up while we still have access to sections.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2829) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2830) mod->core_kallsyms.symtab = dst = mod->core_layout.base + info->symoffs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2831) mod->core_kallsyms.strtab = s = mod->core_layout.base + info->stroffs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2832) mod->core_kallsyms.typetab = mod->core_layout.base + info->core_typeoffs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2833) src = mod->kallsyms->symtab;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2834) for (ndst = i = 0; i < mod->kallsyms->num_symtab; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2835) mod->kallsyms->typetab[i] = elf_type(src + i, info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2836) if (i == 0 || is_livepatch_module(mod) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2837) is_core_symbol(src+i, info->sechdrs, info->hdr->e_shnum,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2838) info->index.pcpu)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2839) mod->core_kallsyms.typetab[ndst] =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2840) mod->kallsyms->typetab[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2841) dst[ndst] = src[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2842) dst[ndst++].st_name = s - mod->core_kallsyms.strtab;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2843) s += strlcpy(s, &mod->kallsyms->strtab[src[i].st_name],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2844) KSYM_NAME_LEN) + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2845) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2846) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2847) mod->core_kallsyms.num_symtab = ndst;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2848) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2849) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2850) static inline void layout_symtab(struct module *mod, struct load_info *info)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2851) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2852) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2853)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2854) static void add_kallsyms(struct module *mod, const struct load_info *info)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2855) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2856) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2857) #endif /* CONFIG_KALLSYMS */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2858)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2859) static void dynamic_debug_setup(struct module *mod, struct _ddebug *debug, unsigned int num)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2860) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2861) if (!debug)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2862) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2863) ddebug_add_module(debug, num, mod->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2864) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2865)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2866) static void dynamic_debug_remove(struct module *mod, struct _ddebug *debug)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2867) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2868) if (debug)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2869) ddebug_remove_module(mod->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2870) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2871)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2872) void * __weak module_alloc(unsigned long size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2873) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2874) return __vmalloc_node_range(size, 1, VMALLOC_START, VMALLOC_END,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2875) GFP_KERNEL, PAGE_KERNEL_EXEC, VM_FLUSH_RESET_PERMS,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2876) NUMA_NO_NODE, __builtin_return_address(0));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2877) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2878)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2879) bool __weak module_init_section(const char *name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2880) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2881) return strstarts(name, ".init");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2882) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2883)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2884) bool __weak module_exit_section(const char *name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2885) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2886) return strstarts(name, ".exit");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2887) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2888)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2889) #ifdef CONFIG_DEBUG_KMEMLEAK
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2890) static void kmemleak_load_module(const struct module *mod,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2891) const struct load_info *info)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2892) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2893) unsigned int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2894)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2895) /* only scan the sections containing data */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2896) kmemleak_scan_area(mod, sizeof(struct module), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2897)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2898) for (i = 1; i < info->hdr->e_shnum; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2899) /* Scan all writable sections that's not executable */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2900) if (!(info->sechdrs[i].sh_flags & SHF_ALLOC) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2901) !(info->sechdrs[i].sh_flags & SHF_WRITE) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2902) (info->sechdrs[i].sh_flags & SHF_EXECINSTR))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2903) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2904)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2905) kmemleak_scan_area((void *)info->sechdrs[i].sh_addr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2906) info->sechdrs[i].sh_size, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2907) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2908) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2909) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2910) static inline void kmemleak_load_module(const struct module *mod,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2911) const struct load_info *info)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2912) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2913) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2914) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2915)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2916) #ifdef CONFIG_MODULE_SIG
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2917) static int module_sig_check(struct load_info *info, int flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2918) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2919) int err = -ENODATA;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2920) const unsigned long markerlen = sizeof(MODULE_SIG_STRING) - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2921) const char *reason;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2922) const void *mod = info->hdr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2923)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2924) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2925) * Require flags == 0, as a module with version information
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2926) * removed is no longer the module that was signed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2927) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2928) if (flags == 0 &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2929) info->len > markerlen &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2930) memcmp(mod + info->len - markerlen, MODULE_SIG_STRING, markerlen) == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2931) /* We truncate the module to discard the signature */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2932) info->len -= markerlen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2933) err = mod_verify_sig(mod, info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2934) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2935)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2936) switch (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2937) case 0:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2938) info->sig_ok = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2939) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2940)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2941) /* We don't permit modules to be loaded into trusted kernels
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2942) * without a valid signature on them, but if we're not
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2943) * enforcing, certain errors are non-fatal.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2944) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2945) case -ENODATA:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2946) reason = "unsigned module";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2947) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2948) case -ENOPKG:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2949) reason = "module with unsupported crypto";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2950) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2951) case -ENOKEY:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2952) reason = "module with unavailable key";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2953) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2954)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2955) /* All other errors are fatal, including nomem, unparseable
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2956) * signatures and signature check failures - even if signatures
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2957) * aren't required.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2958) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2959) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2960) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2961) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2962)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2963) if (is_module_sig_enforced()) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2964) pr_notice("Loading of %s is rejected\n", reason);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2965) return -EKEYREJECTED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2966) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2967)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2968) return security_locked_down(LOCKDOWN_MODULE_SIGNATURE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2969) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2970) #else /* !CONFIG_MODULE_SIG */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2971) static int module_sig_check(struct load_info *info, int flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2972) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2973) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2974) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2975) #endif /* !CONFIG_MODULE_SIG */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2976)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2977) static int validate_section_offset(struct load_info *info, Elf_Shdr *shdr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2978) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2979) unsigned long secend;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2980)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2981) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2982) * Check for both overflow and offset/size being
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2983) * too large.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2984) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2985) secend = shdr->sh_offset + shdr->sh_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2986) if (secend < shdr->sh_offset || secend > info->len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2987) return -ENOEXEC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2988)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2989) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2990) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2991)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2992) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2993) * Sanity checks against invalid binaries, wrong arch, weird elf version.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2994) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2995) * Also do basic validity checks against section offsets and sizes, the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2996) * section name string table, and the indices used for it (sh_name).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2997) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2998) static int elf_validity_check(struct load_info *info)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2999) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3000) unsigned int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3001) Elf_Shdr *shdr, *strhdr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3002) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3003)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3004) if (info->len < sizeof(*(info->hdr)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3005) return -ENOEXEC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3006)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3007) if (memcmp(info->hdr->e_ident, ELFMAG, SELFMAG) != 0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3008) || info->hdr->e_type != ET_REL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3009) || !elf_check_arch(info->hdr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3010) || info->hdr->e_shentsize != sizeof(Elf_Shdr))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3011) return -ENOEXEC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3012)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3013) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3014) * e_shnum is 16 bits, and sizeof(Elf_Shdr) is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3015) * known and small. So e_shnum * sizeof(Elf_Shdr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3016) * will not overflow unsigned long on any platform.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3017) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3018) if (info->hdr->e_shoff >= info->len
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3019) || (info->hdr->e_shnum * sizeof(Elf_Shdr) >
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3020) info->len - info->hdr->e_shoff))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3021) return -ENOEXEC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3022)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3023) info->sechdrs = (void *)info->hdr + info->hdr->e_shoff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3024)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3025) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3026) * Verify if the section name table index is valid.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3027) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3028) if (info->hdr->e_shstrndx == SHN_UNDEF
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3029) || info->hdr->e_shstrndx >= info->hdr->e_shnum)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3030) return -ENOEXEC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3031)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3032) strhdr = &info->sechdrs[info->hdr->e_shstrndx];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3033) err = validate_section_offset(info, strhdr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3034) if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3035) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3036)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3037) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3038) * The section name table must be NUL-terminated, as required
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3039) * by the spec. This makes strcmp and pr_* calls that access
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3040) * strings in the section safe.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3041) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3042) info->secstrings = (void *)info->hdr + strhdr->sh_offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3043) if (info->secstrings[strhdr->sh_size - 1] != '\0')
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3044) return -ENOEXEC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3045)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3046) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3047) * The code assumes that section 0 has a length of zero and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3048) * an addr of zero, so check for it.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3049) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3050) if (info->sechdrs[0].sh_type != SHT_NULL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3051) || info->sechdrs[0].sh_size != 0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3052) || info->sechdrs[0].sh_addr != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3053) return -ENOEXEC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3054)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3055) for (i = 1; i < info->hdr->e_shnum; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3056) shdr = &info->sechdrs[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3057) switch (shdr->sh_type) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3058) case SHT_NULL:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3059) case SHT_NOBITS:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3060) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3061) case SHT_SYMTAB:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3062) if (shdr->sh_link == SHN_UNDEF
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3063) || shdr->sh_link >= info->hdr->e_shnum)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3064) return -ENOEXEC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3065) fallthrough;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3066) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3067) err = validate_section_offset(info, shdr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3068) if (err < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3069) pr_err("Invalid ELF section in module (section %u type %u)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3070) i, shdr->sh_type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3071) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3072) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3073)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3074) if (shdr->sh_flags & SHF_ALLOC) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3075) if (shdr->sh_name >= strhdr->sh_size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3076) pr_err("Invalid ELF section name in module (section %u type %u)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3077) i, shdr->sh_type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3078) return -ENOEXEC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3079) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3080) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3081) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3082) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3083) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3084)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3085) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3086) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3087)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3088) #define COPY_CHUNK_SIZE (16*PAGE_SIZE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3089)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3090) static int copy_chunked_from_user(void *dst, const void __user *usrc, unsigned long len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3091) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3092) do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3093) unsigned long n = min(len, COPY_CHUNK_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3094)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3095) if (copy_from_user(dst, usrc, n) != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3096) return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3097) cond_resched();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3098) dst += n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3099) usrc += n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3100) len -= n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3101) } while (len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3102) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3103) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3104)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3105) #ifdef CONFIG_LIVEPATCH
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3106) static int check_modinfo_livepatch(struct module *mod, struct load_info *info)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3107) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3108) if (get_modinfo(info, "livepatch")) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3109) mod->klp = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3110) add_taint_module(mod, TAINT_LIVEPATCH, LOCKDEP_STILL_OK);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3111) pr_notice_once("%s: tainting kernel with TAINT_LIVEPATCH\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3112) mod->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3113) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3114)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3115) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3116) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3117) #else /* !CONFIG_LIVEPATCH */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3118) static int check_modinfo_livepatch(struct module *mod, struct load_info *info)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3119) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3120) if (get_modinfo(info, "livepatch")) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3121) pr_err("%s: module is marked as livepatch module, but livepatch support is disabled",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3122) mod->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3123) return -ENOEXEC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3124) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3125)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3126) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3127) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3128) #endif /* CONFIG_LIVEPATCH */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3129)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3130) static void check_modinfo_retpoline(struct module *mod, struct load_info *info)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3131) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3132) if (retpoline_module_ok(get_modinfo(info, "retpoline")))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3133) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3134)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3135) pr_warn("%s: loading module not compiled with retpoline compiler.\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3136) mod->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3137) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3138)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3139) /* Sets info->hdr and info->len. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3140) static int copy_module_from_user(const void __user *umod, unsigned long len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3141) struct load_info *info)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3142) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3143) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3144)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3145) info->len = len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3146) if (info->len < sizeof(*(info->hdr)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3147) return -ENOEXEC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3148)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3149) err = security_kernel_load_data(LOADING_MODULE, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3150) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3151) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3152)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3153) /* Suck in entire file: we'll want most of it. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3154) info->hdr = __vmalloc(info->len, GFP_KERNEL | __GFP_NOWARN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3155) if (!info->hdr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3156) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3157)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3158) if (copy_chunked_from_user(info->hdr, umod, info->len) != 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3159) err = -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3160) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3161) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3162)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3163) err = security_kernel_post_load_data((char *)info->hdr, info->len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3164) LOADING_MODULE, "init_module");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3165) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3166) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3167) vfree(info->hdr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3168)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3169) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3170) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3171)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3172) static void free_copy(struct load_info *info)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3173) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3174) vfree(info->hdr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3175) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3176)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3177) static int rewrite_section_headers(struct load_info *info, int flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3178) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3179) unsigned int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3180)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3181) /* This should always be true, but let's be sure. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3182) info->sechdrs[0].sh_addr = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3183)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3184) for (i = 1; i < info->hdr->e_shnum; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3185) Elf_Shdr *shdr = &info->sechdrs[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3186)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3187) /* Mark all sections sh_addr with their address in the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3188) temporary image. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3189) shdr->sh_addr = (size_t)info->hdr + shdr->sh_offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3190)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3191) #ifndef CONFIG_MODULE_UNLOAD
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3192) /* Don't load .exit sections */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3193) if (module_exit_section(info->secstrings+shdr->sh_name))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3194) shdr->sh_flags &= ~(unsigned long)SHF_ALLOC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3195) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3196) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3197)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3198) /* Track but don't keep modinfo and version sections. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3199) info->sechdrs[info->index.vers].sh_flags &= ~(unsigned long)SHF_ALLOC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3200) info->sechdrs[info->index.info].sh_flags &= ~(unsigned long)SHF_ALLOC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3201)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3202) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3203) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3204)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3205) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3206) * Set up our basic convenience variables (pointers to section headers,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3207) * search for module section index etc), and do some basic section
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3208) * verification.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3209) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3210) * Set info->mod to the temporary copy of the module in info->hdr. The final one
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3211) * will be allocated in move_module().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3212) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3213) static int setup_load_info(struct load_info *info, int flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3214) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3215) unsigned int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3216)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3217) /* Try to find a name early so we can log errors with a module name */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3218) info->index.info = find_sec(info, ".modinfo");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3219) if (info->index.info)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3220) info->name = get_modinfo(info, "name");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3221)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3222) /* Find internal symbols and strings. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3223) for (i = 1; i < info->hdr->e_shnum; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3224) if (info->sechdrs[i].sh_type == SHT_SYMTAB) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3225) info->index.sym = i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3226) info->index.str = info->sechdrs[i].sh_link;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3227) info->strtab = (char *)info->hdr
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3228) + info->sechdrs[info->index.str].sh_offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3229) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3230) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3231) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3232)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3233) if (info->index.sym == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3234) pr_warn("%s: module has no symbols (stripped?)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3235) info->name ?: "(missing .modinfo section or name field)");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3236) return -ENOEXEC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3237) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3238)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3239) info->index.mod = find_sec(info, ".gnu.linkonce.this_module");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3240) if (!info->index.mod) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3241) pr_warn("%s: No module found in object\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3242) info->name ?: "(missing .modinfo section or name field)");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3243) return -ENOEXEC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3244) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3245) /* This is temporary: point mod into copy of data. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3246) info->mod = (void *)info->hdr + info->sechdrs[info->index.mod].sh_offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3247)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3248) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3249) * If we didn't load the .modinfo 'name' field earlier, fall back to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3250) * on-disk struct mod 'name' field.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3251) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3252) if (!info->name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3253) info->name = info->mod->name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3254)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3255) if (flags & MODULE_INIT_IGNORE_MODVERSIONS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3256) info->index.vers = 0; /* Pretend no __versions section! */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3257) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3258) info->index.vers = find_sec(info, "__versions");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3259)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3260) info->index.pcpu = find_pcpusec(info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3261)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3262) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3263) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3264)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3265) static int check_modinfo(struct module *mod, struct load_info *info, int flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3266) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3267) const char *modmagic = get_modinfo(info, "vermagic");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3268) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3269)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3270) if (flags & MODULE_INIT_IGNORE_VERMAGIC)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3271) modmagic = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3272)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3273) /* This is allowed: modprobe --force will invalidate it. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3274) if (!modmagic) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3275) err = try_to_force_load(mod, "bad vermagic");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3276) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3277) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3278) } else if (!same_magic(modmagic, vermagic, info->index.vers)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3279) pr_err("%s: version magic '%s' should be '%s'\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3280) info->name, modmagic, vermagic);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3281) return -ENOEXEC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3282) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3283)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3284) if (!get_modinfo(info, "intree")) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3285) if (!test_taint(TAINT_OOT_MODULE))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3286) pr_warn("%s: loading out-of-tree module taints kernel.\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3287) mod->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3288) add_taint_module(mod, TAINT_OOT_MODULE, LOCKDEP_STILL_OK);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3289) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3290)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3291) check_modinfo_retpoline(mod, info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3292)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3293) if (get_modinfo(info, "staging")) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3294) add_taint_module(mod, TAINT_CRAP, LOCKDEP_STILL_OK);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3295) pr_warn("%s: module is from the staging directory, the quality "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3296) "is unknown, you have been warned.\n", mod->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3297) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3298)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3299) err = check_modinfo_livepatch(mod, info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3300) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3301) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3302)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3303) /* Set up license info based on the info section */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3304) set_license(mod, get_modinfo(info, "license"));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3305)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3306) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3307) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3308)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3309) static int find_module_sections(struct module *mod, struct load_info *info)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3310) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3311) mod->kp = section_objs(info, "__param",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3312) sizeof(*mod->kp), &mod->num_kp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3313) mod->syms = section_objs(info, "__ksymtab",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3314) sizeof(*mod->syms), &mod->num_syms);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3315) mod->crcs = section_addr(info, "__kcrctab");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3316) mod->gpl_syms = section_objs(info, "__ksymtab_gpl",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3317) sizeof(*mod->gpl_syms),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3318) &mod->num_gpl_syms);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3319) mod->gpl_crcs = section_addr(info, "__kcrctab_gpl");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3320) mod->gpl_future_syms = section_objs(info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3321) "__ksymtab_gpl_future",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3322) sizeof(*mod->gpl_future_syms),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3323) &mod->num_gpl_future_syms);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3324) mod->gpl_future_crcs = section_addr(info, "__kcrctab_gpl_future");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3325)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3326) #ifdef CONFIG_UNUSED_SYMBOLS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3327) mod->unused_syms = section_objs(info, "__ksymtab_unused",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3328) sizeof(*mod->unused_syms),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3329) &mod->num_unused_syms);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3330) mod->unused_crcs = section_addr(info, "__kcrctab_unused");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3331) mod->unused_gpl_syms = section_objs(info, "__ksymtab_unused_gpl",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3332) sizeof(*mod->unused_gpl_syms),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3333) &mod->num_unused_gpl_syms);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3334) mod->unused_gpl_crcs = section_addr(info, "__kcrctab_unused_gpl");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3335) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3336) #ifdef CONFIG_CONSTRUCTORS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3337) mod->ctors = section_objs(info, ".ctors",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3338) sizeof(*mod->ctors), &mod->num_ctors);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3339) if (!mod->ctors)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3340) mod->ctors = section_objs(info, ".init_array",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3341) sizeof(*mod->ctors), &mod->num_ctors);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3342) else if (find_sec(info, ".init_array")) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3343) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3344) * This shouldn't happen with same compiler and binutils
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3345) * building all parts of the module.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3346) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3347) pr_warn("%s: has both .ctors and .init_array.\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3348) mod->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3349) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3350) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3351) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3352)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3353) mod->noinstr_text_start = section_objs(info, ".noinstr.text", 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3354) &mod->noinstr_text_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3355)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3356) #ifdef CONFIG_TRACEPOINTS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3357) mod->tracepoints_ptrs = section_objs(info, "__tracepoints_ptrs",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3358) sizeof(*mod->tracepoints_ptrs),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3359) &mod->num_tracepoints);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3360) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3361) #ifdef CONFIG_TREE_SRCU
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3362) mod->srcu_struct_ptrs = section_objs(info, "___srcu_struct_ptrs",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3363) sizeof(*mod->srcu_struct_ptrs),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3364) &mod->num_srcu_structs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3365) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3366) #ifdef CONFIG_BPF_EVENTS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3367) mod->bpf_raw_events = section_objs(info, "__bpf_raw_tp_map",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3368) sizeof(*mod->bpf_raw_events),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3369) &mod->num_bpf_raw_events);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3370) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3371) #ifdef CONFIG_JUMP_LABEL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3372) mod->jump_entries = section_objs(info, "__jump_table",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3373) sizeof(*mod->jump_entries),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3374) &mod->num_jump_entries);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3375) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3376) #ifdef CONFIG_EVENT_TRACING
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3377) mod->trace_events = section_objs(info, "_ftrace_events",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3378) sizeof(*mod->trace_events),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3379) &mod->num_trace_events);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3380) mod->trace_evals = section_objs(info, "_ftrace_eval_map",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3381) sizeof(*mod->trace_evals),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3382) &mod->num_trace_evals);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3383) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3384) #ifdef CONFIG_TRACING
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3385) mod->trace_bprintk_fmt_start = section_objs(info, "__trace_printk_fmt",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3386) sizeof(*mod->trace_bprintk_fmt_start),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3387) &mod->num_trace_bprintk_fmt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3388) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3389) #ifdef CONFIG_FTRACE_MCOUNT_RECORD
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3390) /* sechdrs[0].sh_size is always zero */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3391) mod->ftrace_callsites = section_objs(info, FTRACE_CALLSITE_SECTION,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3392) sizeof(*mod->ftrace_callsites),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3393) &mod->num_ftrace_callsites);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3394) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3395) #ifdef CONFIG_FUNCTION_ERROR_INJECTION
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3396) mod->ei_funcs = section_objs(info, "_error_injection_whitelist",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3397) sizeof(*mod->ei_funcs),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3398) &mod->num_ei_funcs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3399) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3400) #ifdef CONFIG_KPROBES
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3401) mod->kprobes_text_start = section_objs(info, ".kprobes.text", 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3402) &mod->kprobes_text_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3403) mod->kprobe_blacklist = section_objs(info, "_kprobe_blacklist",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3404) sizeof(unsigned long),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3405) &mod->num_kprobe_blacklist);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3406) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3407) #ifdef CONFIG_HAVE_STATIC_CALL_INLINE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3408) mod->static_call_sites = section_objs(info, ".static_call_sites",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3409) sizeof(*mod->static_call_sites),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3410) &mod->num_static_call_sites);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3411) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3412) mod->extable = section_objs(info, "__ex_table",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3413) sizeof(*mod->extable), &mod->num_exentries);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3414)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3415) if (section_addr(info, "__obsparm"))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3416) pr_warn("%s: Ignoring obsolete parameters\n", mod->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3417)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3418) info->debug = section_objs(info, "__dyndbg",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3419) sizeof(*info->debug), &info->num_debug);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3420)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3421) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3422) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3423)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3424) static int move_module(struct module *mod, struct load_info *info)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3425) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3426) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3427) void *ptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3428)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3429) /* Do the allocs. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3430) ptr = module_alloc(mod->core_layout.size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3431) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3432) * The pointer to this block is stored in the module structure
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3433) * which is inside the block. Just mark it as not being a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3434) * leak.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3435) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3436) kmemleak_not_leak(ptr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3437) if (!ptr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3438) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3439)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3440) memset(ptr, 0, mod->core_layout.size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3441) mod->core_layout.base = ptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3442)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3443) if (mod->init_layout.size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3444) ptr = module_alloc(mod->init_layout.size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3445) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3446) * The pointer to this block is stored in the module structure
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3447) * which is inside the block. This block doesn't need to be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3448) * scanned as it contains data and code that will be freed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3449) * after the module is initialized.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3450) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3451) kmemleak_ignore(ptr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3452) if (!ptr) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3453) module_memfree(mod->core_layout.base);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3454) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3455) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3456) memset(ptr, 0, mod->init_layout.size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3457) mod->init_layout.base = ptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3458) } else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3459) mod->init_layout.base = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3460)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3461) /* Transfer each section which specifies SHF_ALLOC */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3462) pr_debug("final section addresses:\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3463) for (i = 0; i < info->hdr->e_shnum; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3464) void *dest;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3465) Elf_Shdr *shdr = &info->sechdrs[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3466)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3467) if (!(shdr->sh_flags & SHF_ALLOC))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3468) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3469)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3470) if (shdr->sh_entsize & INIT_OFFSET_MASK)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3471) dest = mod->init_layout.base
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3472) + (shdr->sh_entsize & ~INIT_OFFSET_MASK);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3473) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3474) dest = mod->core_layout.base + shdr->sh_entsize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3475)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3476) if (shdr->sh_type != SHT_NOBITS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3477) memcpy(dest, (void *)shdr->sh_addr, shdr->sh_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3478) /* Update sh_addr to point to copy in image. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3479) shdr->sh_addr = (unsigned long)dest;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3480) pr_debug("\t0x%lx %s\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3481) (long)shdr->sh_addr, info->secstrings + shdr->sh_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3482) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3483)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3484) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3485) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3486)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3487) static int check_module_license_and_versions(struct module *mod)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3488) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3489) int prev_taint = test_taint(TAINT_PROPRIETARY_MODULE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3490)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3491) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3492) * ndiswrapper is under GPL by itself, but loads proprietary modules.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3493) * Don't use add_taint_module(), as it would prevent ndiswrapper from
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3494) * using GPL-only symbols it needs.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3495) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3496) if (strcmp(mod->name, "ndiswrapper") == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3497) add_taint(TAINT_PROPRIETARY_MODULE, LOCKDEP_NOW_UNRELIABLE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3498)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3499) /* driverloader was caught wrongly pretending to be under GPL */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3500) if (strcmp(mod->name, "driverloader") == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3501) add_taint_module(mod, TAINT_PROPRIETARY_MODULE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3502) LOCKDEP_NOW_UNRELIABLE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3503)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3504) /* lve claims to be GPL but upstream won't provide source */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3505) if (strcmp(mod->name, "lve") == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3506) add_taint_module(mod, TAINT_PROPRIETARY_MODULE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3507) LOCKDEP_NOW_UNRELIABLE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3508)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3509) if (!prev_taint && test_taint(TAINT_PROPRIETARY_MODULE))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3510) pr_warn("%s: module license taints kernel.\n", mod->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3511)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3512) #ifdef CONFIG_MODVERSIONS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3513) if ((mod->num_syms && !mod->crcs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3514) || (mod->num_gpl_syms && !mod->gpl_crcs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3515) || (mod->num_gpl_future_syms && !mod->gpl_future_crcs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3516) #ifdef CONFIG_UNUSED_SYMBOLS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3517) || (mod->num_unused_syms && !mod->unused_crcs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3518) || (mod->num_unused_gpl_syms && !mod->unused_gpl_crcs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3519) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3520) ) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3521) return try_to_force_load(mod,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3522) "no versions for exported symbols");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3523) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3524) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3525) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3526) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3527)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3528) static void flush_module_icache(const struct module *mod)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3529) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3530) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3531) * Flush the instruction cache, since we've played with text.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3532) * Do it before processing of module parameters, so the module
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3533) * can provide parameter accessor functions of its own.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3534) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3535) if (mod->init_layout.base)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3536) flush_icache_range((unsigned long)mod->init_layout.base,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3537) (unsigned long)mod->init_layout.base
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3538) + mod->init_layout.size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3539) flush_icache_range((unsigned long)mod->core_layout.base,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3540) (unsigned long)mod->core_layout.base + mod->core_layout.size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3541) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3542)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3543) int __weak module_frob_arch_sections(Elf_Ehdr *hdr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3544) Elf_Shdr *sechdrs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3545) char *secstrings,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3546) struct module *mod)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3547) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3548) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3549) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3550)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3551) /* module_blacklist is a comma-separated list of module names */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3552) static char *module_blacklist;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3553) static bool blacklisted(const char *module_name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3554) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3555) const char *p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3556) size_t len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3557)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3558) if (!module_blacklist)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3559) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3560)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3561) for (p = module_blacklist; *p; p += len) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3562) len = strcspn(p, ",");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3563) if (strlen(module_name) == len && !memcmp(module_name, p, len))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3564) return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3565) if (p[len] == ',')
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3566) len++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3567) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3568) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3569) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3570) core_param(module_blacklist, module_blacklist, charp, 0400);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3571)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3572) static struct module *layout_and_allocate(struct load_info *info, int flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3573) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3574) struct module *mod;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3575) unsigned int ndx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3576) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3577)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3578) err = check_modinfo(info->mod, info, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3579) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3580) return ERR_PTR(err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3581)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3582) /* Allow arches to frob section contents and sizes. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3583) err = module_frob_arch_sections(info->hdr, info->sechdrs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3584) info->secstrings, info->mod);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3585) if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3586) return ERR_PTR(err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3587)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3588) err = module_enforce_rwx_sections(info->hdr, info->sechdrs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3589) info->secstrings, info->mod);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3590) if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3591) return ERR_PTR(err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3592)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3593) /* We will do a special allocation for per-cpu sections later. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3594) info->sechdrs[info->index.pcpu].sh_flags &= ~(unsigned long)SHF_ALLOC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3595)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3596) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3597) * Mark ro_after_init section with SHF_RO_AFTER_INIT so that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3598) * layout_sections() can put it in the right place.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3599) * Note: ro_after_init sections also have SHF_{WRITE,ALLOC} set.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3600) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3601) ndx = find_sec(info, ".data..ro_after_init");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3602) if (ndx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3603) info->sechdrs[ndx].sh_flags |= SHF_RO_AFTER_INIT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3604) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3605) * Mark the __jump_table section as ro_after_init as well: these data
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3606) * structures are never modified, with the exception of entries that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3607) * refer to code in the __init section, which are annotated as such
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3608) * at module load time.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3609) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3610) ndx = find_sec(info, "__jump_table");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3611) if (ndx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3612) info->sechdrs[ndx].sh_flags |= SHF_RO_AFTER_INIT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3613)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3614) /* Determine total sizes, and put offsets in sh_entsize. For now
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3615) this is done generically; there doesn't appear to be any
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3616) special cases for the architectures. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3617) layout_sections(info->mod, info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3618) layout_symtab(info->mod, info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3619)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3620) /* Allocate and move to the final place */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3621) err = move_module(info->mod, info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3622) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3623) return ERR_PTR(err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3624)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3625) /* Module has been copied to its final place now: return it. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3626) mod = (void *)info->sechdrs[info->index.mod].sh_addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3627) kmemleak_load_module(mod, info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3628) return mod;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3629) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3630)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3631) /* mod is no longer valid after this! */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3632) static void module_deallocate(struct module *mod, struct load_info *info)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3633) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3634) percpu_modfree(mod);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3635) module_arch_freeing_init(mod);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3636) trace_android_vh_set_memory_rw((unsigned long)mod->init_layout.base,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3637) (mod->init_layout.size)>>PAGE_SHIFT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3638) trace_android_vh_set_memory_nx((unsigned long)mod->init_layout.base,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3639) (mod->init_layout.size)>>PAGE_SHIFT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3640) module_memfree(mod->init_layout.base);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3641) trace_android_vh_set_memory_rw((unsigned long)mod->core_layout.base,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3642) (mod->core_layout.size)>>PAGE_SHIFT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3643) trace_android_vh_set_memory_nx((unsigned long)mod->core_layout.base,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3644) (mod->core_layout.size)>>PAGE_SHIFT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3645) module_memfree(mod->core_layout.base);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3646) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3647)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3648) int __weak module_finalize(const Elf_Ehdr *hdr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3649) const Elf_Shdr *sechdrs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3650) struct module *me)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3651) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3652) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3653) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3654)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3655) static int post_relocation(struct module *mod, const struct load_info *info)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3656) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3657) /* Sort exception table now relocations are done. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3658) sort_extable(mod->extable, mod->extable + mod->num_exentries);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3659)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3660) /* Copy relocated percpu area over. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3661) percpu_modcopy(mod, (void *)info->sechdrs[info->index.pcpu].sh_addr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3662) info->sechdrs[info->index.pcpu].sh_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3663)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3664) /* Setup kallsyms-specific fields. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3665) add_kallsyms(mod, info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3666)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3667) /* Arch-specific module finalizing. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3668) return module_finalize(info->hdr, info->sechdrs, mod);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3669) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3670)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3671) /* Is this module of this name done loading? No locks held. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3672) static bool finished_loading(const char *name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3673) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3674) struct module *mod;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3675) bool ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3676)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3677) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3678) * The module_mutex should not be a heavily contended lock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3679) * if we get the occasional sleep here, we'll go an extra iteration
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3680) * in the wait_event_interruptible(), which is harmless.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3681) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3682) sched_annotate_sleep();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3683) mutex_lock(&module_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3684) mod = find_module_all(name, strlen(name), true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3685) ret = !mod || mod->state == MODULE_STATE_LIVE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3686) mutex_unlock(&module_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3687)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3688) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3689) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3690)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3691) /* Call module constructors. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3692) static void do_mod_ctors(struct module *mod)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3693) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3694) #ifdef CONFIG_CONSTRUCTORS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3695) unsigned long i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3696)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3697) for (i = 0; i < mod->num_ctors; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3698) mod->ctors[i]();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3699) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3700) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3701)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3702) /* For freeing module_init on success, in case kallsyms traversing */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3703) struct mod_initfree {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3704) struct llist_node node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3705) void *module_init;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3706) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3707)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3708) static void do_free_init(struct work_struct *w)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3709) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3710) struct llist_node *pos, *n, *list;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3711) struct mod_initfree *initfree;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3712)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3713) list = llist_del_all(&init_free_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3714)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3715) synchronize_rcu();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3716)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3717) llist_for_each_safe(pos, n, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3718) initfree = container_of(pos, struct mod_initfree, node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3719) module_memfree(initfree->module_init);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3720) kfree(initfree);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3721) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3722) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3723)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3724) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3725) * This is where the real work happens.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3726) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3727) * Keep it uninlined to provide a reliable breakpoint target, e.g. for the gdb
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3728) * helper command 'lx-symbols'.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3729) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3730) static noinline int do_init_module(struct module *mod)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3731) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3732) int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3733) struct mod_initfree *freeinit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3734)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3735) freeinit = kmalloc(sizeof(*freeinit), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3736) if (!freeinit) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3737) ret = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3738) goto fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3739) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3740) freeinit->module_init = mod->init_layout.base;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3741)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3742) do_mod_ctors(mod);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3743) /* Start the module */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3744) if (mod->init != NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3745) ret = do_one_initcall(mod->init);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3746) if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3747) goto fail_free_freeinit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3748) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3749) if (ret > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3750) pr_warn("%s: '%s'->init suspiciously returned %d, it should "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3751) "follow 0/-E convention\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3752) "%s: loading module anyway...\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3753) __func__, mod->name, ret, __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3754) dump_stack();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3755) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3756)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3757) /* Now it's a first class citizen! */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3758) mod->state = MODULE_STATE_LIVE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3759) blocking_notifier_call_chain(&module_notify_list,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3760) MODULE_STATE_LIVE, mod);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3761)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3762) /* Delay uevent until module has finished its init routine */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3763) kobject_uevent(&mod->mkobj.kobj, KOBJ_ADD);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3764)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3765) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3766) * We need to finish all async code before the module init sequence
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3767) * is done. This has potential to deadlock if synchronous module
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3768) * loading is requested from async (which is not allowed!).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3769) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3770) * See commit 0fdff3ec6d87 ("async, kmod: warn on synchronous
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3771) * request_module() from async workers") for more details.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3772) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3773) if (!mod->async_probe_requested)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3774) async_synchronize_full();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3775)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3776) ftrace_free_mem(mod, mod->init_layout.base, mod->init_layout.base +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3777) mod->init_layout.size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3778) mutex_lock(&module_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3779) /* Drop initial reference. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3780) module_put(mod);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3781) trim_init_extable(mod);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3782) #ifdef CONFIG_KALLSYMS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3783) /* Switch to core kallsyms now init is done: kallsyms may be walking! */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3784) rcu_assign_pointer(mod->kallsyms, &mod->core_kallsyms);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3785) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3786) module_enable_ro(mod, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3787) trace_android_vh_set_module_permit_after_init(mod);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3788) mod_tree_remove_init(mod);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3789) module_arch_freeing_init(mod);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3790) trace_android_vh_set_memory_rw((unsigned long)mod->init_layout.base,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3791) (mod->init_layout.size)>>PAGE_SHIFT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3792) trace_android_vh_set_memory_nx((unsigned long)mod->init_layout.base,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3793) (mod->init_layout.size)>>PAGE_SHIFT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3794) mod->init_layout.base = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3795) mod->init_layout.size = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3796) mod->init_layout.ro_size = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3797) mod->init_layout.ro_after_init_size = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3798) mod->init_layout.text_size = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3799) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3800) * We want to free module_init, but be aware that kallsyms may be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3801) * walking this with preempt disabled. In all the failure paths, we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3802) * call synchronize_rcu(), but we don't want to slow down the success
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3803) * path. module_memfree() cannot be called in an interrupt, so do the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3804) * work and call synchronize_rcu() in a work queue.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3805) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3806) * Note that module_alloc() on most architectures creates W+X page
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3807) * mappings which won't be cleaned up until do_free_init() runs. Any
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3808) * code such as mark_rodata_ro() which depends on those mappings to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3809) * be cleaned up needs to sync with the queued work - ie
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3810) * rcu_barrier()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3811) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3812) if (llist_add(&freeinit->node, &init_free_list))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3813) schedule_work(&init_free_wq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3814)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3815) mutex_unlock(&module_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3816) wake_up_all(&module_wq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3817)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3818) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3819)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3820) fail_free_freeinit:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3821) kfree(freeinit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3822) fail:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3823) /* Try to protect us from buggy refcounters. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3824) mod->state = MODULE_STATE_GOING;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3825) synchronize_rcu();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3826) module_put(mod);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3827) blocking_notifier_call_chain(&module_notify_list,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3828) MODULE_STATE_GOING, mod);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3829) klp_module_going(mod);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3830) ftrace_release_mod(mod);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3831) free_module(mod);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3832) wake_up_all(&module_wq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3833) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3834) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3835)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3836) static int may_init_module(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3837) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3838) if (!capable(CAP_SYS_MODULE) || modules_disabled)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3839) return -EPERM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3840)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3841) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3842) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3843)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3844) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3845) * We try to place it in the list now to make sure it's unique before
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3846) * we dedicate too many resources. In particular, temporary percpu
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3847) * memory exhaustion.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3848) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3849) static int add_unformed_module(struct module *mod)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3850) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3851) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3852) struct module *old;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3853)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3854) mod->state = MODULE_STATE_UNFORMED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3855)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3856) again:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3857) mutex_lock(&module_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3858) old = find_module_all(mod->name, strlen(mod->name), true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3859) if (old != NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3860) if (old->state != MODULE_STATE_LIVE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3861) /* Wait in case it fails to load. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3862) mutex_unlock(&module_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3863) err = wait_event_interruptible(module_wq,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3864) finished_loading(mod->name));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3865) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3866) goto out_unlocked;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3867) goto again;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3868) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3869) err = -EEXIST;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3870) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3871) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3872) mod_update_bounds(mod);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3873) list_add_rcu(&mod->list, &modules);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3874) mod_tree_insert(mod);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3875) err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3876)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3877) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3878) mutex_unlock(&module_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3879) out_unlocked:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3880) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3881) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3882)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3883) static int complete_formation(struct module *mod, struct load_info *info)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3884) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3885) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3886)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3887) mutex_lock(&module_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3888)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3889) /* Find duplicate symbols (must be called under lock). */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3890) err = verify_exported_symbols(mod);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3891) if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3892) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3893)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3894) /* This relies on module_mutex for list integrity. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3895) module_bug_finalize(info->hdr, info->sechdrs, mod);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3896)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3897) module_enable_ro(mod, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3898) module_enable_nx(mod);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3899) module_enable_x(mod);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3900) trace_android_vh_set_module_permit_before_init(mod);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3901)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3902) /* Mark state as coming so strong_try_module_get() ignores us,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3903) * but kallsyms etc. can see us. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3904) mod->state = MODULE_STATE_COMING;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3905) mutex_unlock(&module_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3906)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3907) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3908)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3909) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3910) mutex_unlock(&module_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3911) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3912) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3913)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3914) static int prepare_coming_module(struct module *mod)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3915) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3916) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3917)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3918) ftrace_module_enable(mod);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3919) err = klp_module_coming(mod);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3920) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3921) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3922)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3923) err = blocking_notifier_call_chain_robust(&module_notify_list,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3924) MODULE_STATE_COMING, MODULE_STATE_GOING, mod);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3925) err = notifier_to_errno(err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3926) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3927) klp_module_going(mod);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3928)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3929) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3930) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3931)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3932) static int unknown_module_param_cb(char *param, char *val, const char *modname,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3933) void *arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3934) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3935) struct module *mod = arg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3936) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3937)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3938) if (strcmp(param, "async_probe") == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3939) mod->async_probe_requested = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3940) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3941) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3942)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3943) /* Check for magic 'dyndbg' arg */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3944) ret = ddebug_dyndbg_module_param_cb(param, val, modname);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3945) if (ret != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3946) pr_warn("%s: unknown parameter '%s' ignored\n", modname, param);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3947) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3948) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3949)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3950) static void cfi_init(struct module *mod);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3951)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3952) /* Allocate and load the module: note that size of section 0 is always
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3953) zero, and we rely on this for optional sections. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3954) static int load_module(struct load_info *info, const char __user *uargs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3955) int flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3956) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3957) struct module *mod;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3958) long err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3959) char *after_dashes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3960)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3961) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3962) * Do the signature check (if any) first. All that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3963) * the signature check needs is info->len, it does
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3964) * not need any of the section info. That can be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3965) * set up later. This will minimize the chances
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3966) * of a corrupt module causing problems before
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3967) * we even get to the signature check.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3968) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3969) * The check will also adjust info->len by stripping
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3970) * off the sig length at the end of the module, making
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3971) * checks against info->len more correct.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3972) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3973) err = module_sig_check(info, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3974) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3975) goto free_copy;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3976)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3977) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3978) * Do basic sanity checks against the ELF header and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3979) * sections.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3980) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3981) err = elf_validity_check(info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3982) if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3983) pr_err("Module has invalid ELF structures\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3984) goto free_copy;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3985) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3986)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3987) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3988) * Everything checks out, so set up the section info
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3989) * in the info structure.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3990) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3991) err = setup_load_info(info, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3992) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3993) goto free_copy;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3994)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3995) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3996) * Now that we know we have the correct module name, check
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3997) * if it's blacklisted.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3998) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3999) if (blacklisted(info->name)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4000) err = -EPERM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4001) pr_err("Module %s is blacklisted\n", info->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4002) goto free_copy;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4003) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4004)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4005) err = rewrite_section_headers(info, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4006) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4007) goto free_copy;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4008)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4009) /* Check module struct version now, before we try to use module. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4010) if (!check_modstruct_version(info, info->mod)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4011) err = -ENOEXEC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4012) goto free_copy;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4013) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4014)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4015) /* Figure out module layout, and allocate all the memory. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4016) mod = layout_and_allocate(info, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4017) if (IS_ERR(mod)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4018) err = PTR_ERR(mod);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4019) goto free_copy;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4020) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4021)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4022) audit_log_kern_module(mod->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4023)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4024) /* Reserve our place in the list. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4025) err = add_unformed_module(mod);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4026) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4027) goto free_module;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4028)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4029) #ifdef CONFIG_MODULE_SIG
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4030) mod->sig_ok = info->sig_ok;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4031) if (!mod->sig_ok) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4032) pr_notice_once("%s: module verification failed: signature "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4033) "and/or required key missing - tainting "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4034) "kernel\n", mod->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4035) add_taint_module(mod, TAINT_UNSIGNED_MODULE, LOCKDEP_STILL_OK);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4036) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4037) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4038)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4039) /* To avoid stressing percpu allocator, do this once we're unique. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4040) err = percpu_modalloc(mod, info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4041) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4042) goto unlink_mod;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4043)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4044) /* Now module is in final location, initialize linked lists, etc. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4045) err = module_unload_init(mod);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4046) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4047) goto unlink_mod;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4048)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4049) init_param_lock(mod);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4050)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4051) /* Now we've got everything in the final locations, we can
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4052) * find optional sections. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4053) err = find_module_sections(mod, info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4054) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4055) goto free_unload;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4056)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4057) err = check_module_license_and_versions(mod);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4058) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4059) goto free_unload;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4060)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4061) /* Set up MODINFO_ATTR fields */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4062) setup_modinfo(mod, info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4063)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4064) /* Fix up syms, so that st_value is a pointer to location. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4065) err = simplify_symbols(mod, info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4066) if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4067) goto free_modinfo;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4068)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4069) err = apply_relocations(mod, info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4070) if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4071) goto free_modinfo;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4072)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4073) err = post_relocation(mod, info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4074) if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4075) goto free_modinfo;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4076)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4077) flush_module_icache(mod);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4078)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4079) /* Setup CFI for the module. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4080) cfi_init(mod);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4081)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4082) /* Now copy in args */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4083) mod->args = strndup_user(uargs, ~0UL >> 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4084) if (IS_ERR(mod->args)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4085) err = PTR_ERR(mod->args);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4086) goto free_arch_cleanup;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4087) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4088)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4089) dynamic_debug_setup(mod, info->debug, info->num_debug);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4090)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4091) /* Ftrace init must be called in the MODULE_STATE_UNFORMED state */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4092) ftrace_module_init(mod);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4093)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4094) /* Finally it's fully formed, ready to start executing. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4095) err = complete_formation(mod, info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4096) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4097) goto ddebug_cleanup;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4098)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4099) err = prepare_coming_module(mod);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4100) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4101) goto bug_cleanup;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4102)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4103) /* Module is ready to execute: parsing args may do that. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4104) after_dashes = parse_args(mod->name, mod->args, mod->kp, mod->num_kp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4105) -32768, 32767, mod,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4106) unknown_module_param_cb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4107) if (IS_ERR(after_dashes)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4108) err = PTR_ERR(after_dashes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4109) goto coming_cleanup;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4110) } else if (after_dashes) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4111) pr_warn("%s: parameters '%s' after `--' ignored\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4112) mod->name, after_dashes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4113) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4114)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4115) /* Link in to sysfs. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4116) err = mod_sysfs_setup(mod, info, mod->kp, mod->num_kp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4117) if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4118) goto coming_cleanup;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4119)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4120) if (is_livepatch_module(mod)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4121) err = copy_module_elf(mod, info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4122) if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4123) goto sysfs_cleanup;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4124) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4125)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4126) /* Get rid of temporary copy. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4127) free_copy(info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4128)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4129) /* Done! */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4130) trace_module_load(mod);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4131)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4132) return do_init_module(mod);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4133)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4134) sysfs_cleanup:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4135) mod_sysfs_teardown(mod);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4136) coming_cleanup:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4137) mod->state = MODULE_STATE_GOING;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4138) destroy_params(mod->kp, mod->num_kp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4139) blocking_notifier_call_chain(&module_notify_list,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4140) MODULE_STATE_GOING, mod);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4141) klp_module_going(mod);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4142) bug_cleanup:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4143) mod->state = MODULE_STATE_GOING;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4144) /* module_bug_cleanup needs module_mutex protection */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4145) mutex_lock(&module_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4146) module_bug_cleanup(mod);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4147) mutex_unlock(&module_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4148)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4149) ddebug_cleanup:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4150) ftrace_release_mod(mod);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4151) dynamic_debug_remove(mod, info->debug);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4152) synchronize_rcu();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4153) kfree(mod->args);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4154) free_arch_cleanup:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4155) cfi_cleanup(mod);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4156) module_arch_cleanup(mod);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4157) free_modinfo:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4158) free_modinfo(mod);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4159) free_unload:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4160) module_unload_free(mod);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4161) unlink_mod:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4162) mutex_lock(&module_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4163) /* Unlink carefully: kallsyms could be walking list. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4164) list_del_rcu(&mod->list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4165) mod_tree_remove(mod);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4166) wake_up_all(&module_wq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4167) /* Wait for RCU-sched synchronizing before releasing mod->list. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4168) synchronize_rcu();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4169) mutex_unlock(&module_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4170) free_module:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4171) /* Free lock-classes; relies on the preceding sync_rcu() */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4172) lockdep_free_key_range(mod->core_layout.base, mod->core_layout.size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4173)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4174) module_deallocate(mod, info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4175) free_copy:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4176) free_copy(info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4177) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4178) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4179)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4180) SYSCALL_DEFINE3(init_module, void __user *, umod,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4181) unsigned long, len, const char __user *, uargs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4182) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4183) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4184) struct load_info info = { };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4185)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4186) err = may_init_module();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4187) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4188) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4189)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4190) pr_debug("init_module: umod=%p, len=%lu, uargs=%p\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4191) umod, len, uargs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4192)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4193) err = copy_module_from_user(umod, len, &info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4194) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4195) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4196)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4197) return load_module(&info, uargs, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4198) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4199)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4200) SYSCALL_DEFINE3(finit_module, int, fd, const char __user *, uargs, int, flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4201) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4202) struct load_info info = { };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4203) void *hdr = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4204) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4205)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4206) err = may_init_module();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4207) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4208) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4209)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4210) pr_debug("finit_module: fd=%d, uargs=%p, flags=%i\n", fd, uargs, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4211)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4212) if (flags & ~(MODULE_INIT_IGNORE_MODVERSIONS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4213) |MODULE_INIT_IGNORE_VERMAGIC))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4214) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4215)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4216) err = kernel_read_file_from_fd(fd, 0, &hdr, INT_MAX, NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4217) READING_MODULE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4218) if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4219) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4220) info.hdr = hdr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4221) info.len = err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4222)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4223) return load_module(&info, uargs, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4224) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4225)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4226) static inline int within(unsigned long addr, void *start, unsigned long size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4227) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4228) return ((void *)addr >= start && (void *)addr < start + size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4229) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4230)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4231) #ifdef CONFIG_KALLSYMS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4232) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4233) * This ignores the intensely annoying "mapping symbols" found
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4234) * in ARM ELF files: $a, $t and $d.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4235) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4236) static inline int is_arm_mapping_symbol(const char *str)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4237) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4238) if (str[0] == '.' && str[1] == 'L')
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4239) return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4240) return str[0] == '$' && strchr("axtd", str[1])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4241) && (str[2] == '\0' || str[2] == '.');
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4242) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4243)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4244) static inline int is_cfi_typeid_symbol(const char *str)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4245) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4246) return !strncmp(str, "__typeid__", 10);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4247) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4248)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4249) static const char *kallsyms_symbol_name(struct mod_kallsyms *kallsyms, unsigned int symnum)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4250) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4251) return kallsyms->strtab + kallsyms->symtab[symnum].st_name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4252) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4253)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4254) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4255) * Given a module and address, find the corresponding symbol and return its name
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4256) * while providing its size and offset if needed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4257) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4258) static const char *find_kallsyms_symbol(struct module *mod,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4259) unsigned long addr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4260) unsigned long *size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4261) unsigned long *offset)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4262) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4263) unsigned int i, best = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4264) unsigned long nextval, bestval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4265) struct mod_kallsyms *kallsyms = rcu_dereference_sched(mod->kallsyms);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4266)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4267) /* At worse, next value is at end of module */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4268) if (within_module_init(addr, mod))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4269) nextval = (unsigned long)mod->init_layout.base+mod->init_layout.text_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4270) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4271) nextval = (unsigned long)mod->core_layout.base+mod->core_layout.text_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4272)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4273) bestval = kallsyms_symbol_value(&kallsyms->symtab[best]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4274)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4275) /* Scan for closest preceding symbol, and next symbol. (ELF
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4276) starts real symbols at 1). */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4277) for (i = 1; i < kallsyms->num_symtab; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4278) const Elf_Sym *sym = &kallsyms->symtab[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4279) unsigned long thisval = kallsyms_symbol_value(sym);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4280)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4281) if (sym->st_shndx == SHN_UNDEF)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4282) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4283)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4284) /* We ignore unnamed symbols: they're uninformative
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4285) * and inserted at a whim. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4286) if (*kallsyms_symbol_name(kallsyms, i) == '\0'
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4287) || is_arm_mapping_symbol(kallsyms_symbol_name(kallsyms, i))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4288) || is_cfi_typeid_symbol(kallsyms_symbol_name(kallsyms, i)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4289) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4290)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4291) if (thisval <= addr && thisval > bestval) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4292) best = i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4293) bestval = thisval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4294) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4295) if (thisval > addr && thisval < nextval)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4296) nextval = thisval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4297) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4298)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4299) if (!best)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4300) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4301)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4302) if (size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4303) *size = nextval - bestval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4304) if (offset)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4305) *offset = addr - bestval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4306)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4307) return kallsyms_symbol_name(kallsyms, best);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4308) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4309)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4310) void * __weak dereference_module_function_descriptor(struct module *mod,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4311) void *ptr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4312) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4313) return ptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4314) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4315)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4316) /* For kallsyms to ask for address resolution. NULL means not found. Careful
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4317) * not to lock to avoid deadlock on oopses, simply disable preemption. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4318) const char *module_address_lookup(unsigned long addr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4319) unsigned long *size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4320) unsigned long *offset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4321) char **modname,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4322) char *namebuf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4323) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4324) const char *ret = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4325) struct module *mod;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4326)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4327) preempt_disable();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4328) mod = __module_address(addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4329) if (mod) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4330) if (modname)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4331) *modname = mod->name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4332)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4333) ret = find_kallsyms_symbol(mod, addr, size, offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4334) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4335) /* Make a copy in here where it's safe */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4336) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4337) strncpy(namebuf, ret, KSYM_NAME_LEN - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4338) ret = namebuf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4339) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4340) preempt_enable();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4341)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4342) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4343) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4344)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4345) int lookup_module_symbol_name(unsigned long addr, char *symname)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4346) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4347) struct module *mod;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4348)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4349) preempt_disable();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4350) list_for_each_entry_rcu(mod, &modules, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4351) if (mod->state == MODULE_STATE_UNFORMED)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4352) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4353) if (within_module(addr, mod)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4354) const char *sym;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4355)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4356) sym = find_kallsyms_symbol(mod, addr, NULL, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4357) if (!sym)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4358) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4359)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4360) strlcpy(symname, sym, KSYM_NAME_LEN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4361) preempt_enable();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4362) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4363) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4364) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4365) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4366) preempt_enable();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4367) return -ERANGE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4368) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4369)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4370) int lookup_module_symbol_attrs(unsigned long addr, unsigned long *size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4371) unsigned long *offset, char *modname, char *name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4372) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4373) struct module *mod;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4374)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4375) preempt_disable();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4376) list_for_each_entry_rcu(mod, &modules, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4377) if (mod->state == MODULE_STATE_UNFORMED)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4378) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4379) if (within_module(addr, mod)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4380) const char *sym;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4381)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4382) sym = find_kallsyms_symbol(mod, addr, size, offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4383) if (!sym)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4384) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4385) if (modname)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4386) strlcpy(modname, mod->name, MODULE_NAME_LEN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4387) if (name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4388) strlcpy(name, sym, KSYM_NAME_LEN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4389) preempt_enable();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4390) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4391) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4392) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4393) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4394) preempt_enable();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4395) return -ERANGE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4396) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4397)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4398) int module_get_kallsym(unsigned int symnum, unsigned long *value, char *type,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4399) char *name, char *module_name, int *exported)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4400) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4401) struct module *mod;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4402)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4403) preempt_disable();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4404) list_for_each_entry_rcu(mod, &modules, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4405) struct mod_kallsyms *kallsyms;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4406)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4407) if (mod->state == MODULE_STATE_UNFORMED)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4408) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4409) kallsyms = rcu_dereference_sched(mod->kallsyms);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4410) if (symnum < kallsyms->num_symtab) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4411) const Elf_Sym *sym = &kallsyms->symtab[symnum];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4412)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4413) *value = kallsyms_symbol_value(sym);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4414) *type = kallsyms->typetab[symnum];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4415) strlcpy(name, kallsyms_symbol_name(kallsyms, symnum), KSYM_NAME_LEN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4416) strlcpy(module_name, mod->name, MODULE_NAME_LEN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4417) *exported = is_exported(name, *value, mod);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4418) preempt_enable();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4419) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4420) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4421) symnum -= kallsyms->num_symtab;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4422) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4423) preempt_enable();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4424) return -ERANGE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4425) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4426)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4427) /* Given a module and name of symbol, find and return the symbol's value */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4428) static unsigned long find_kallsyms_symbol_value(struct module *mod, const char *name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4429) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4430) unsigned int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4431) struct mod_kallsyms *kallsyms = rcu_dereference_sched(mod->kallsyms);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4432)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4433) for (i = 0; i < kallsyms->num_symtab; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4434) const Elf_Sym *sym = &kallsyms->symtab[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4435)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4436) if (strcmp(name, kallsyms_symbol_name(kallsyms, i)) == 0 &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4437) sym->st_shndx != SHN_UNDEF)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4438) return kallsyms_symbol_value(sym);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4439) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4440) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4441) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4442)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4443) /* Look for this name: can be of form module:name. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4444) unsigned long module_kallsyms_lookup_name(const char *name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4445) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4446) struct module *mod;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4447) char *colon;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4448) unsigned long ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4449)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4450) /* Don't lock: we're in enough trouble already. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4451) preempt_disable();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4452) if ((colon = strnchr(name, MODULE_NAME_LEN, ':')) != NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4453) if ((mod = find_module_all(name, colon - name, false)) != NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4454) ret = find_kallsyms_symbol_value(mod, colon+1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4455) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4456) list_for_each_entry_rcu(mod, &modules, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4457) if (mod->state == MODULE_STATE_UNFORMED)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4458) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4459) if ((ret = find_kallsyms_symbol_value(mod, name)) != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4460) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4461) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4462) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4463) preempt_enable();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4464) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4465) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4466)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4467) int module_kallsyms_on_each_symbol(int (*fn)(void *, const char *,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4468) struct module *, unsigned long),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4469) void *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4470) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4471) struct module *mod;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4472) unsigned int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4473) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4474)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4475) module_assert_mutex();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4476)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4477) list_for_each_entry(mod, &modules, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4478) /* We hold module_mutex: no need for rcu_dereference_sched */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4479) struct mod_kallsyms *kallsyms = mod->kallsyms;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4480)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4481) if (mod->state == MODULE_STATE_UNFORMED)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4482) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4483) for (i = 0; i < kallsyms->num_symtab; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4484) const Elf_Sym *sym = &kallsyms->symtab[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4485)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4486) if (sym->st_shndx == SHN_UNDEF)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4487) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4488)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4489) ret = fn(data, kallsyms_symbol_name(kallsyms, i),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4490) mod, kallsyms_symbol_value(sym));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4491) if (ret != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4492) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4493) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4494) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4495) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4496) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4497) #endif /* CONFIG_KALLSYMS */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4498)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4499) static void cfi_init(struct module *mod)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4500) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4501) #ifdef CONFIG_CFI_CLANG
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4502) initcall_t *init;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4503) exitcall_t *exit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4504)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4505) rcu_read_lock_sched();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4506) mod->cfi_check = (cfi_check_fn)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4507) find_kallsyms_symbol_value(mod, "__cfi_check");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4508) init = (initcall_t *)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4509) find_kallsyms_symbol_value(mod, "__cfi_jt_init_module");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4510) exit = (exitcall_t *)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4511) find_kallsyms_symbol_value(mod, "__cfi_jt_cleanup_module");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4512) rcu_read_unlock_sched();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4513)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4514) /* Fix init/exit functions to point to the CFI jump table */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4515) if (init) mod->init = *init;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4516) if (exit) mod->exit = *exit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4517)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4518) cfi_module_add(mod, module_addr_min);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4519) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4520) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4521)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4522) static void cfi_cleanup(struct module *mod)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4523) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4524) #ifdef CONFIG_CFI_CLANG
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4525) cfi_module_remove(mod, module_addr_min);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4526) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4527) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4528)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4529) /* Maximum number of characters written by module_flags() */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4530) #define MODULE_FLAGS_BUF_SIZE (TAINT_FLAGS_COUNT + 4)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4531)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4532) /* Keep in sync with MODULE_FLAGS_BUF_SIZE !!! */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4533) static char *module_flags(struct module *mod, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4534) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4535) int bx = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4536)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4537) BUG_ON(mod->state == MODULE_STATE_UNFORMED);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4538) if (mod->taints ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4539) mod->state == MODULE_STATE_GOING ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4540) mod->state == MODULE_STATE_COMING) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4541) buf[bx++] = '(';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4542) bx += module_flags_taint(mod, buf + bx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4543) /* Show a - for module-is-being-unloaded */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4544) if (mod->state == MODULE_STATE_GOING)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4545) buf[bx++] = '-';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4546) /* Show a + for module-is-being-loaded */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4547) if (mod->state == MODULE_STATE_COMING)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4548) buf[bx++] = '+';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4549) buf[bx++] = ')';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4550) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4551) buf[bx] = '\0';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4552)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4553) return buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4554) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4555)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4556) #ifdef CONFIG_PROC_FS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4557) /* Called by the /proc file system to return a list of modules. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4558) static void *m_start(struct seq_file *m, loff_t *pos)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4559) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4560) mutex_lock(&module_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4561) return seq_list_start(&modules, *pos);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4562) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4563)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4564) static void *m_next(struct seq_file *m, void *p, loff_t *pos)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4565) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4566) return seq_list_next(p, &modules, pos);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4567) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4568)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4569) static void m_stop(struct seq_file *m, void *p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4570) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4571) mutex_unlock(&module_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4572) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4573)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4574) static int m_show(struct seq_file *m, void *p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4575) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4576) struct module *mod = list_entry(p, struct module, list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4577) char buf[MODULE_FLAGS_BUF_SIZE];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4578) void *value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4579)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4580) /* We always ignore unformed modules. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4581) if (mod->state == MODULE_STATE_UNFORMED)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4582) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4583)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4584) seq_printf(m, "%s %u",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4585) mod->name, mod->init_layout.size + mod->core_layout.size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4586) print_unload_info(m, mod);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4587)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4588) /* Informative for users. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4589) seq_printf(m, " %s",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4590) mod->state == MODULE_STATE_GOING ? "Unloading" :
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4591) mod->state == MODULE_STATE_COMING ? "Loading" :
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4592) "Live");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4593) /* Used by oprofile and other similar tools. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4594) value = m->private ? NULL : mod->core_layout.base;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4595) seq_printf(m, " 0x%px", value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4596)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4597) /* Taints info */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4598) if (mod->taints)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4599) seq_printf(m, " %s", module_flags(mod, buf));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4600)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4601) seq_puts(m, "\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4602) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4603) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4604)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4605) /* Format: modulename size refcount deps address
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4606)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4607) Where refcount is a number or -, and deps is a comma-separated list
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4608) of depends or -.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4609) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4610) static const struct seq_operations modules_op = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4611) .start = m_start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4612) .next = m_next,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4613) .stop = m_stop,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4614) .show = m_show
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4615) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4616)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4617) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4618) * This also sets the "private" pointer to non-NULL if the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4619) * kernel pointers should be hidden (so you can just test
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4620) * "m->private" to see if you should keep the values private).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4621) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4622) * We use the same logic as for /proc/kallsyms.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4623) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4624) static int modules_open(struct inode *inode, struct file *file)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4625) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4626) int err = seq_open(file, &modules_op);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4627)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4628) if (!err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4629) struct seq_file *m = file->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4630) m->private = kallsyms_show_value(file->f_cred) ? NULL : (void *)8ul;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4631) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4632)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4633) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4634) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4635)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4636) static const struct proc_ops modules_proc_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4637) .proc_flags = PROC_ENTRY_PERMANENT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4638) .proc_open = modules_open,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4639) .proc_read = seq_read,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4640) .proc_lseek = seq_lseek,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4641) .proc_release = seq_release,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4642) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4643)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4644) static int __init proc_modules_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4645) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4646) proc_create("modules", 0, NULL, &modules_proc_ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4647) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4648) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4649) module_init(proc_modules_init);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4650) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4651)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4652) /* Given an address, look for it in the module exception tables. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4653) const struct exception_table_entry *search_module_extables(unsigned long addr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4654) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4655) const struct exception_table_entry *e = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4656) struct module *mod;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4657)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4658) preempt_disable();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4659) mod = __module_address(addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4660) if (!mod)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4661) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4662)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4663) if (!mod->num_exentries)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4664) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4665)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4666) e = search_extable(mod->extable,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4667) mod->num_exentries,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4668) addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4669) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4670) preempt_enable();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4671)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4672) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4673) * Now, if we found one, we are running inside it now, hence
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4674) * we cannot unload the module, hence no refcnt needed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4675) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4676) return e;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4677) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4678)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4679) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4680) * is_module_address - is this address inside a module?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4681) * @addr: the address to check.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4682) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4683) * See is_module_text_address() if you simply want to see if the address
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4684) * is code (not data).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4685) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4686) bool is_module_address(unsigned long addr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4687) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4688) bool ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4689)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4690) preempt_disable();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4691) ret = __module_address(addr) != NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4692) preempt_enable();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4693)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4694) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4695) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4696)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4697) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4698) * __module_address - get the module which contains an address.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4699) * @addr: the address.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4700) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4701) * Must be called with preempt disabled or module mutex held so that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4702) * module doesn't get freed during this.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4703) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4704) struct module *__module_address(unsigned long addr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4705) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4706) struct module *mod;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4707)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4708) if (addr < module_addr_min || addr > module_addr_max)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4709) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4710)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4711) module_assert_mutex_or_preempt();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4712)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4713) mod = mod_find(addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4714) if (mod) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4715) BUG_ON(!within_module(addr, mod));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4716) if (mod->state == MODULE_STATE_UNFORMED)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4717) mod = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4718) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4719) return mod;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4720) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4721)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4722) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4723) * is_module_text_address - is this address inside module code?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4724) * @addr: the address to check.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4725) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4726) * See is_module_address() if you simply want to see if the address is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4727) * anywhere in a module. See kernel_text_address() for testing if an
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4728) * address corresponds to kernel or module code.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4729) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4730) bool is_module_text_address(unsigned long addr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4731) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4732) bool ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4733)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4734) preempt_disable();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4735) ret = __module_text_address(addr) != NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4736) preempt_enable();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4737)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4738) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4739) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4740)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4741) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4742) * __module_text_address - get the module whose code contains an address.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4743) * @addr: the address.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4744) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4745) * Must be called with preempt disabled or module mutex held so that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4746) * module doesn't get freed during this.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4747) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4748) struct module *__module_text_address(unsigned long addr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4749) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4750) struct module *mod = __module_address(addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4751) if (mod) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4752) /* Make sure it's within the text section. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4753) if (!within(addr, mod->init_layout.base, mod->init_layout.text_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4754) && !within(addr, mod->core_layout.base, mod->core_layout.text_size))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4755) mod = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4756) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4757) return mod;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4758) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4759)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4760) /* Don't grab lock, we're oopsing. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4761) void print_modules(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4762) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4763) struct module *mod;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4764) char buf[MODULE_FLAGS_BUF_SIZE];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4765)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4766) printk(KERN_DEFAULT "Modules linked in:");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4767) /* Most callers should already have preempt disabled, but make sure */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4768) preempt_disable();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4769) list_for_each_entry_rcu(mod, &modules, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4770) if (mod->state == MODULE_STATE_UNFORMED)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4771) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4772) pr_cont(" %s%s", mod->name, module_flags(mod, buf));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4773) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4774) preempt_enable();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4775) if (last_unloaded_module[0])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4776) pr_cont(" [last unloaded: %s]", last_unloaded_module);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4777) pr_cont("\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4778) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4779)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4780) #ifdef CONFIG_ANDROID_DEBUG_SYMBOLS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4781) void android_debug_for_each_module(int (*fn)(const char *mod_name, void *mod_addr, void *data),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4782) void *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4783) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4784) struct module *module;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4785)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4786) preempt_disable();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4787) list_for_each_entry_rcu(module, &modules, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4788) if (fn(module->name, module->core_layout.base, data))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4789) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4790) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4791) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4792) preempt_enable();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4793) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4794) EXPORT_SYMBOL_GPL(android_debug_for_each_module);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4795) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4796)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4797) #ifdef CONFIG_MODVERSIONS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4798) /* Generate the signature for all relevant module structures here.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4799) * If these change, we don't want to try to parse the module. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4800) void module_layout(struct module *mod,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4801) struct modversion_info *ver,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4802) struct kernel_param *kp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4803) struct kernel_symbol *ks,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4804) struct tracepoint * const *tp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4805) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4806) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4807) EXPORT_SYMBOL(module_layout);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4808) #endif