cachepc-linux

Fork of AMDESE/linux with modifications for CachePC side-channel attack
git clone https://git.sinitax.com/sinitax/cachepc-linux
Log | Files | Refs | README | LICENSE | sfeed.txt

internal.h (8724B)


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