0001
0002
0003
0004
0005
0006
0007
0008 #include <linux/elf.h>
0009 #include <linux/compiler.h>
0010 #include <linux/module.h>
0011 #include <linux/mutex.h>
0012 #include <linux/rculist.h>
0013 #include <linux/rcupdate.h>
0014 #include <linux/mm.h>
0015
0016 #ifndef ARCH_SHF_SMALL
0017 #define ARCH_SHF_SMALL 0
0018 #endif
0019
0020
0021 #define INIT_OFFSET_MASK (1UL << (BITS_PER_LONG - 1))
0022
0023 #define MODULE_FLAGS_BUF_SIZE (TAINT_FLAGS_COUNT + 4)
0024
0025 #ifndef CONFIG_ARCH_WANTS_MODULES_DATA_IN_VMALLOC
0026 #define data_layout core_layout
0027 #endif
0028
0029
0030
0031
0032
0033
0034 static inline unsigned int strict_align(unsigned int size)
0035 {
0036 if (IS_ENABLED(CONFIG_STRICT_MODULE_RWX))
0037 return PAGE_ALIGN(size);
0038 else
0039 return size;
0040 }
0041
0042 extern struct mutex module_mutex;
0043 extern struct list_head modules;
0044
0045 extern struct module_attribute *modinfo_attrs[];
0046 extern size_t modinfo_attrs_count;
0047
0048
0049 extern const struct kernel_symbol __start___ksymtab[];
0050 extern const struct kernel_symbol __stop___ksymtab[];
0051 extern const struct kernel_symbol __start___ksymtab_gpl[];
0052 extern const struct kernel_symbol __stop___ksymtab_gpl[];
0053 extern const s32 __start___kcrctab[];
0054 extern const s32 __start___kcrctab_gpl[];
0055
0056 struct load_info {
0057 const char *name;
0058
0059 struct module *mod;
0060 Elf_Ehdr *hdr;
0061 unsigned long len;
0062 Elf_Shdr *sechdrs;
0063 char *secstrings, *strtab;
0064 unsigned long symoffs, stroffs, init_typeoffs, core_typeoffs;
0065 struct _ddebug *debug;
0066 unsigned int num_debug;
0067 bool sig_ok;
0068 #ifdef CONFIG_KALLSYMS
0069 unsigned long mod_kallsyms_init_off;
0070 #endif
0071 #ifdef CONFIG_MODULE_DECOMPRESS
0072 struct page **pages;
0073 unsigned int max_pages;
0074 unsigned int used_pages;
0075 #endif
0076 struct {
0077 unsigned int sym, str, mod, vers, info, pcpu;
0078 } index;
0079 };
0080
0081 enum mod_license {
0082 NOT_GPL_ONLY,
0083 GPL_ONLY,
0084 };
0085
0086 struct find_symbol_arg {
0087
0088 const char *name;
0089 bool gplok;
0090 bool warn;
0091
0092
0093 struct module *owner;
0094 const s32 *crc;
0095 const struct kernel_symbol *sym;
0096 enum mod_license license;
0097 };
0098
0099 int mod_verify_sig(const void *mod, struct load_info *info);
0100 int try_to_force_load(struct module *mod, const char *reason);
0101 bool find_symbol(struct find_symbol_arg *fsa);
0102 struct module *find_module_all(const char *name, size_t len, bool even_unformed);
0103 int cmp_name(const void *name, const void *sym);
0104 long module_get_offset(struct module *mod, unsigned int *size, Elf_Shdr *sechdr,
0105 unsigned int section);
0106 char *module_flags(struct module *mod, char *buf, bool show_state);
0107 size_t module_flags_taint(unsigned long taints, char *buf);
0108
0109 static inline void module_assert_mutex_or_preempt(void)
0110 {
0111 #ifdef CONFIG_LOCKDEP
0112 if (unlikely(!debug_locks))
0113 return;
0114
0115 WARN_ON_ONCE(!rcu_read_lock_sched_held() &&
0116 !lockdep_is_held(&module_mutex));
0117 #endif
0118 }
0119
0120 static inline unsigned long kernel_symbol_value(const struct kernel_symbol *sym)
0121 {
0122 #ifdef CONFIG_HAVE_ARCH_PREL32_RELOCATIONS
0123 return (unsigned long)offset_to_ptr(&sym->value_offset);
0124 #else
0125 return sym->value;
0126 #endif
0127 }
0128
0129 #ifdef CONFIG_LIVEPATCH
0130 int copy_module_elf(struct module *mod, struct load_info *info);
0131 void free_module_elf(struct module *mod);
0132 #else
0133 static inline int copy_module_elf(struct module *mod, struct load_info *info)
0134 {
0135 return 0;
0136 }
0137
0138 static inline void free_module_elf(struct module *mod) { }
0139 #endif
0140
0141 static inline bool set_livepatch_module(struct module *mod)
0142 {
0143 #ifdef CONFIG_LIVEPATCH
0144 mod->klp = true;
0145 return true;
0146 #else
0147 return false;
0148 #endif
0149 }
0150
0151 #ifdef CONFIG_MODULE_UNLOAD_TAINT_TRACKING
0152 struct mod_unload_taint {
0153 struct list_head list;
0154 char name[MODULE_NAME_LEN];
0155 unsigned long taints;
0156 u64 count;
0157 };
0158
0159 int try_add_tainted_module(struct module *mod);
0160 void print_unloaded_tainted_modules(void);
0161 #else
0162 static inline int try_add_tainted_module(struct module *mod)
0163 {
0164 return 0;
0165 }
0166
0167 static inline void print_unloaded_tainted_modules(void)
0168 {
0169 }
0170 #endif
0171
0172 #ifdef CONFIG_MODULE_DECOMPRESS
0173 int module_decompress(struct load_info *info, const void *buf, size_t size);
0174 void module_decompress_cleanup(struct load_info *info);
0175 #else
0176 static inline int module_decompress(struct load_info *info,
0177 const void *buf, size_t size)
0178 {
0179 return -EOPNOTSUPP;
0180 }
0181
0182 static inline void module_decompress_cleanup(struct load_info *info)
0183 {
0184 }
0185 #endif
0186
0187 struct mod_tree_root {
0188 #ifdef CONFIG_MODULES_TREE_LOOKUP
0189 struct latch_tree_root root;
0190 #endif
0191 unsigned long addr_min;
0192 unsigned long addr_max;
0193 };
0194
0195 extern struct mod_tree_root mod_tree;
0196 extern struct mod_tree_root mod_data_tree;
0197
0198 #ifdef CONFIG_MODULES_TREE_LOOKUP
0199 void mod_tree_insert(struct module *mod);
0200 void mod_tree_remove_init(struct module *mod);
0201 void mod_tree_remove(struct module *mod);
0202 struct module *mod_find(unsigned long addr, struct mod_tree_root *tree);
0203 #else
0204
0205 static inline void mod_tree_insert(struct module *mod) { }
0206 static inline void mod_tree_remove_init(struct module *mod) { }
0207 static inline void mod_tree_remove(struct module *mod) { }
0208 static inline struct module *mod_find(unsigned long addr, struct mod_tree_root *tree)
0209 {
0210 struct module *mod;
0211
0212 list_for_each_entry_rcu(mod, &modules, list,
0213 lockdep_is_held(&module_mutex)) {
0214 if (within_module(addr, mod))
0215 return mod;
0216 }
0217
0218 return NULL;
0219 }
0220 #endif
0221
0222 void module_enable_ro(const struct module *mod, bool after_init);
0223 void module_enable_nx(const struct module *mod);
0224 void module_enable_x(const struct module *mod);
0225 int module_enforce_rwx_sections(Elf_Ehdr *hdr, Elf_Shdr *sechdrs,
0226 char *secstrings, struct module *mod);
0227 bool module_check_misalignment(const struct module *mod);
0228
0229 #ifdef CONFIG_MODULE_SIG
0230 int module_sig_check(struct load_info *info, int flags);
0231 #else
0232 static inline int module_sig_check(struct load_info *info, int flags)
0233 {
0234 return 0;
0235 }
0236 #endif
0237
0238 #ifdef CONFIG_DEBUG_KMEMLEAK
0239 void kmemleak_load_module(const struct module *mod, const struct load_info *info);
0240 #else
0241 static inline void kmemleak_load_module(const struct module *mod,
0242 const struct load_info *info) { }
0243 #endif
0244
0245 #ifdef CONFIG_KALLSYMS
0246 void init_build_id(struct module *mod, const struct load_info *info);
0247 void layout_symtab(struct module *mod, struct load_info *info);
0248 void add_kallsyms(struct module *mod, const struct load_info *info);
0249 unsigned long find_kallsyms_symbol_value(struct module *mod, const char *name);
0250
0251 static inline bool sect_empty(const Elf_Shdr *sect)
0252 {
0253 return !(sect->sh_flags & SHF_ALLOC) || sect->sh_size == 0;
0254 }
0255 #else
0256 static inline void init_build_id(struct module *mod, const struct load_info *info) { }
0257 static inline void layout_symtab(struct module *mod, struct load_info *info) { }
0258 static inline void add_kallsyms(struct module *mod, const struct load_info *info) { }
0259 #endif
0260
0261 #ifdef CONFIG_SYSFS
0262 int mod_sysfs_setup(struct module *mod, const struct load_info *info,
0263 struct kernel_param *kparam, unsigned int num_params);
0264 void mod_sysfs_teardown(struct module *mod);
0265 void init_param_lock(struct module *mod);
0266 #else
0267 static inline int mod_sysfs_setup(struct module *mod,
0268 const struct load_info *info,
0269 struct kernel_param *kparam,
0270 unsigned int num_params)
0271 {
0272 return 0;
0273 }
0274
0275 static inline void mod_sysfs_teardown(struct module *mod) { }
0276 static inline void init_param_lock(struct module *mod) { }
0277 #endif
0278
0279 #ifdef CONFIG_MODVERSIONS
0280 int check_version(const struct load_info *info,
0281 const char *symname, struct module *mod, const s32 *crc);
0282 void module_layout(struct module *mod, struct modversion_info *ver, struct kernel_param *kp,
0283 struct kernel_symbol *ks, struct tracepoint * const *tp);
0284 int check_modstruct_version(const struct load_info *info, struct module *mod);
0285 int same_magic(const char *amagic, const char *bmagic, bool has_crcs);
0286 #else
0287 static inline int check_version(const struct load_info *info,
0288 const char *symname,
0289 struct module *mod,
0290 const s32 *crc)
0291 {
0292 return 1;
0293 }
0294
0295 static inline int check_modstruct_version(const struct load_info *info,
0296 struct module *mod)
0297 {
0298 return 1;
0299 }
0300
0301 static inline int same_magic(const char *amagic, const char *bmagic, bool has_crcs)
0302 {
0303 return strcmp(amagic, bmagic) == 0;
0304 }
0305 #endif