^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) /* binfmt_elf_fdpic.c: FDPIC ELF binary format
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) * Copyright (C) 2003, 2004, 2006 Red Hat, Inc. All Rights Reserved.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Written by David Howells (dhowells@redhat.com)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) * Derived from binfmt_elf.c
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/fs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/stat.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/sched.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/sched/coredump.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <linux/sched/task_stack.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <linux/sched/cputime.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include <linux/mm.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include <linux/mman.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #include <linux/errno.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) #include <linux/signal.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) #include <linux/binfmts.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) #include <linux/string.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) #include <linux/file.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) #include <linux/fcntl.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) #include <linux/pagemap.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) #include <linux/security.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) #include <linux/highmem.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) #include <linux/highuid.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) #include <linux/personality.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) #include <linux/ptrace.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) #include <linux/elf.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) #include <linux/elf-fdpic.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) #include <linux/elfcore.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) #include <linux/coredump.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) #include <linux/dax.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) #include <linux/regset.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) #include <linux/uaccess.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) #include <asm/param.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) typedef char *elf_caddr_t;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) #if 0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) #define kdebug(fmt, ...) printk("FDPIC "fmt"\n" ,##__VA_ARGS__ )
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) #define kdebug(fmt, ...) do {} while(0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) #if 0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) #define kdcore(fmt, ...) printk("FDPIC "fmt"\n" ,##__VA_ARGS__ )
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) #define kdcore(fmt, ...) do {} while(0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) MODULE_LICENSE("GPL");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) static int load_elf_fdpic_binary(struct linux_binprm *);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) static int elf_fdpic_fetch_phdrs(struct elf_fdpic_params *, struct file *);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) static int elf_fdpic_map_file(struct elf_fdpic_params *, struct file *,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) struct mm_struct *, const char *);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) static int create_elf_fdpic_tables(struct linux_binprm *, struct mm_struct *,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) struct elf_fdpic_params *,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) struct elf_fdpic_params *);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) #ifndef CONFIG_MMU
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) static int elf_fdpic_map_file_constdisp_on_uclinux(struct elf_fdpic_params *,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) struct file *,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) struct mm_struct *);
^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) static int elf_fdpic_map_file_by_direct_mmap(struct elf_fdpic_params *,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) struct file *, struct mm_struct *);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) #ifdef CONFIG_ELF_CORE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) static int elf_fdpic_core_dump(struct coredump_params *cprm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) static struct linux_binfmt elf_fdpic_format = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) .module = THIS_MODULE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) .load_binary = load_elf_fdpic_binary,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) #ifdef CONFIG_ELF_CORE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) .core_dump = elf_fdpic_core_dump,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) .min_coredump = ELF_EXEC_PAGESIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) static int __init init_elf_fdpic_binfmt(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) register_binfmt(&elf_fdpic_format);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) static void __exit exit_elf_fdpic_binfmt(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) unregister_binfmt(&elf_fdpic_format);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) core_initcall(init_elf_fdpic_binfmt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) module_exit(exit_elf_fdpic_binfmt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) static int is_elf(struct elfhdr *hdr, struct file *file)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) if (memcmp(hdr->e_ident, ELFMAG, SELFMAG) != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) if (hdr->e_type != ET_EXEC && hdr->e_type != ET_DYN)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) if (!elf_check_arch(hdr))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) if (!file->f_op->mmap)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) #ifndef elf_check_fdpic
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) #define elf_check_fdpic(x) 0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) #ifndef elf_check_const_displacement
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) #define elf_check_const_displacement(x) 0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) static int is_constdisp(struct elfhdr *hdr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) if (!elf_check_fdpic(hdr))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) if (elf_check_const_displacement(hdr))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) /*****************************************************************************/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) * read the program headers table into memory
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) static int elf_fdpic_fetch_phdrs(struct elf_fdpic_params *params,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) struct file *file)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) struct elf32_phdr *phdr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) unsigned long size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) int retval, loop;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) loff_t pos = params->hdr.e_phoff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) if (params->hdr.e_phentsize != sizeof(struct elf_phdr))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) if (params->hdr.e_phnum > 65536U / sizeof(struct elf_phdr))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) size = params->hdr.e_phnum * sizeof(struct elf_phdr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) params->phdrs = kmalloc(size, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) if (!params->phdrs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) retval = kernel_read(file, params->phdrs, size, &pos);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) if (unlikely(retval != size))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) return retval < 0 ? retval : -ENOEXEC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) /* determine stack size for this binary */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) phdr = params->phdrs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) for (loop = 0; loop < params->hdr.e_phnum; loop++, phdr++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) if (phdr->p_type != PT_GNU_STACK)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) if (phdr->p_flags & PF_X)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) params->flags |= ELF_FDPIC_FLAG_EXEC_STACK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) params->flags |= ELF_FDPIC_FLAG_NOEXEC_STACK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) params->stack_size = phdr->p_memsz;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) /*****************************************************************************/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) * load an fdpic binary into various bits of memory
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) static int load_elf_fdpic_binary(struct linux_binprm *bprm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) struct elf_fdpic_params exec_params, interp_params;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) struct pt_regs *regs = current_pt_regs();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) struct elf_phdr *phdr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) unsigned long stack_size, entryaddr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) #ifdef ELF_FDPIC_PLAT_INIT
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) unsigned long dynaddr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) #ifndef CONFIG_MMU
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) unsigned long stack_prot;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) struct file *interpreter = NULL; /* to shut gcc up */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) char *interpreter_name = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) int executable_stack;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) int retval, i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) loff_t pos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) kdebug("____ LOAD %d ____", current->pid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) memset(&exec_params, 0, sizeof(exec_params));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) memset(&interp_params, 0, sizeof(interp_params));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) exec_params.hdr = *(struct elfhdr *) bprm->buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) exec_params.flags = ELF_FDPIC_FLAG_PRESENT | ELF_FDPIC_FLAG_EXECUTABLE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) /* check that this is a binary we know how to deal with */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) retval = -ENOEXEC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) if (!is_elf(&exec_params.hdr, bprm->file))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) if (!elf_check_fdpic(&exec_params.hdr)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) #ifdef CONFIG_MMU
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) /* binfmt_elf handles non-fdpic elf except on nommu */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) /* nommu can only load ET_DYN (PIE) ELF */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) if (exec_params.hdr.e_type != ET_DYN)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) /* read the program header table */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) retval = elf_fdpic_fetch_phdrs(&exec_params, bprm->file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) if (retval < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) /* scan for a program header that specifies an interpreter */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) phdr = exec_params.phdrs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) for (i = 0; i < exec_params.hdr.e_phnum; i++, phdr++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) switch (phdr->p_type) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) case PT_INTERP:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) retval = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) if (phdr->p_filesz > PATH_MAX)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) retval = -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) if (phdr->p_filesz < 2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) /* read the name of the interpreter into memory */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) interpreter_name = kmalloc(phdr->p_filesz, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) if (!interpreter_name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) pos = phdr->p_offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) retval = kernel_read(bprm->file, interpreter_name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) phdr->p_filesz, &pos);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) if (unlikely(retval != phdr->p_filesz)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) if (retval >= 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) retval = -ENOEXEC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) retval = -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) if (interpreter_name[phdr->p_filesz - 1] != '\0')
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) kdebug("Using ELF interpreter %s", interpreter_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) /* replace the program with the interpreter */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) interpreter = open_exec(interpreter_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) retval = PTR_ERR(interpreter);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) if (IS_ERR(interpreter)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) interpreter = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) * If the binary is not readable then enforce
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) * mm->dumpable = 0 regardless of the interpreter's
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) * permissions.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) would_dump(bprm, interpreter);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) pos = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) retval = kernel_read(interpreter, bprm->buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) BINPRM_BUF_SIZE, &pos);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) if (unlikely(retval != BINPRM_BUF_SIZE)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) if (retval >= 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) retval = -ENOEXEC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) interp_params.hdr = *((struct elfhdr *) bprm->buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) case PT_LOAD:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) #ifdef CONFIG_MMU
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) if (exec_params.load_addr == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) exec_params.load_addr = phdr->p_vaddr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) if (is_constdisp(&exec_params.hdr))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) exec_params.flags |= ELF_FDPIC_FLAG_CONSTDISP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) /* perform insanity checks on the interpreter */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) if (interpreter_name) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) retval = -ELIBBAD;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) if (!is_elf(&interp_params.hdr, interpreter))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) interp_params.flags = ELF_FDPIC_FLAG_PRESENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) /* read the interpreter's program header table */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) retval = elf_fdpic_fetch_phdrs(&interp_params, interpreter);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) if (retval < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) stack_size = exec_params.stack_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) if (exec_params.flags & ELF_FDPIC_FLAG_EXEC_STACK)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) executable_stack = EXSTACK_ENABLE_X;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) else if (exec_params.flags & ELF_FDPIC_FLAG_NOEXEC_STACK)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) executable_stack = EXSTACK_DISABLE_X;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) executable_stack = EXSTACK_DEFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) if (stack_size == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) stack_size = interp_params.stack_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) if (interp_params.flags & ELF_FDPIC_FLAG_EXEC_STACK)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) executable_stack = EXSTACK_ENABLE_X;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) else if (interp_params.flags & ELF_FDPIC_FLAG_NOEXEC_STACK)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) executable_stack = EXSTACK_DISABLE_X;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) executable_stack = EXSTACK_DEFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) retval = -ENOEXEC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) if (stack_size == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) stack_size = 131072UL; /* same as exec.c's default commit */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) if (is_constdisp(&interp_params.hdr))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) interp_params.flags |= ELF_FDPIC_FLAG_CONSTDISP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) /* flush all traces of the currently running executable */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) retval = begin_new_exec(bprm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) if (retval)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) /* there's now no turning back... the old userspace image is dead,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) * defunct, deceased, etc.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) if (elf_check_fdpic(&exec_params.hdr))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) set_personality(PER_LINUX_FDPIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) set_personality(PER_LINUX);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) if (elf_read_implies_exec(&exec_params.hdr, executable_stack))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) current->personality |= READ_IMPLIES_EXEC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) setup_new_exec(bprm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) set_binfmt(&elf_fdpic_format);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) current->mm->start_code = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) current->mm->end_code = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) current->mm->start_stack = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) current->mm->start_data = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) current->mm->end_data = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) current->mm->context.exec_fdpic_loadmap = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) current->mm->context.interp_fdpic_loadmap = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) #ifdef CONFIG_MMU
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) elf_fdpic_arch_lay_out_mm(&exec_params,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) &interp_params,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) ¤t->mm->start_stack,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) ¤t->mm->start_brk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) retval = setup_arg_pages(bprm, current->mm->start_stack,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) executable_stack);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) if (retval < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) #ifdef ARCH_HAS_SETUP_ADDITIONAL_PAGES
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) retval = arch_setup_additional_pages(bprm, !!interpreter_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) if (retval < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) /* load the executable and interpreter into memory */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) retval = elf_fdpic_map_file(&exec_params, bprm->file, current->mm,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) "executable");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) if (retval < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) if (interpreter_name) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) retval = elf_fdpic_map_file(&interp_params, interpreter,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) current->mm, "interpreter");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) if (retval < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) printk(KERN_ERR "Unable to load interpreter\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) allow_write_access(interpreter);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) fput(interpreter);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) interpreter = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) #ifdef CONFIG_MMU
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) if (!current->mm->start_brk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) current->mm->start_brk = current->mm->end_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) current->mm->brk = current->mm->start_brk =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) PAGE_ALIGN(current->mm->start_brk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) /* create a stack area and zero-size brk area */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) stack_size = (stack_size + PAGE_SIZE - 1) & PAGE_MASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) if (stack_size < PAGE_SIZE * 2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) stack_size = PAGE_SIZE * 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) stack_prot = PROT_READ | PROT_WRITE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) if (executable_stack == EXSTACK_ENABLE_X ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) (executable_stack == EXSTACK_DEFAULT && VM_STACK_FLAGS & VM_EXEC))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) stack_prot |= PROT_EXEC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) current->mm->start_brk = vm_mmap(NULL, 0, stack_size, stack_prot,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) MAP_PRIVATE | MAP_ANONYMOUS |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) MAP_UNINITIALIZED | MAP_GROWSDOWN,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) if (IS_ERR_VALUE(current->mm->start_brk)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) retval = current->mm->start_brk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) current->mm->start_brk = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) current->mm->brk = current->mm->start_brk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) current->mm->context.end_brk = current->mm->start_brk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) current->mm->start_stack = current->mm->start_brk + stack_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) if (create_elf_fdpic_tables(bprm, current->mm,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) &exec_params, &interp_params) < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) kdebug("- start_code %lx", current->mm->start_code);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) kdebug("- end_code %lx", current->mm->end_code);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) kdebug("- start_data %lx", current->mm->start_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) kdebug("- end_data %lx", current->mm->end_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) kdebug("- start_brk %lx", current->mm->start_brk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) kdebug("- brk %lx", current->mm->brk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) kdebug("- start_stack %lx", current->mm->start_stack);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) #ifdef ELF_FDPIC_PLAT_INIT
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) * The ABI may specify that certain registers be set up in special
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) * ways (on i386 %edx is the address of a DT_FINI function, for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) * example. This macro performs whatever initialization to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) * the regs structure is required.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) dynaddr = interp_params.dynamic_addr ?: exec_params.dynamic_addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) ELF_FDPIC_PLAT_INIT(regs, exec_params.map_addr, interp_params.map_addr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) dynaddr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) finalize_exec(bprm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) /* everything is now ready... get the userspace context ready to roll */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) entryaddr = interp_params.entry_addr ?: exec_params.entry_addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) start_thread(regs, entryaddr, current->mm->start_stack);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) retval = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) error:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) if (interpreter) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) allow_write_access(interpreter);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) fput(interpreter);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) kfree(interpreter_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) kfree(exec_params.phdrs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) kfree(exec_params.loadmap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) kfree(interp_params.phdrs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) kfree(interp_params.loadmap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) return retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) /*****************************************************************************/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) #ifndef ELF_BASE_PLATFORM
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) * AT_BASE_PLATFORM indicates the "real" hardware/microarchitecture.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) * If the arch defines ELF_BASE_PLATFORM (in asm/elf.h), the value
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) * will be copied to the user stack in the same manner as AT_PLATFORM.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) #define ELF_BASE_PLATFORM NULL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) * present useful information to the program by shovelling it onto the new
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) * process's stack
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) static int create_elf_fdpic_tables(struct linux_binprm *bprm,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) struct mm_struct *mm,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) struct elf_fdpic_params *exec_params,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) struct elf_fdpic_params *interp_params)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) const struct cred *cred = current_cred();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) unsigned long sp, csp, nitems;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) elf_caddr_t __user *argv, *envp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) size_t platform_len = 0, len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) char *k_platform, *k_base_platform;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) char __user *u_platform, *u_base_platform, *p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) int loop;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) int nr; /* reset for each csp adjustment */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) #ifdef CONFIG_MMU
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) /* In some cases (e.g. Hyper-Threading), we want to avoid L1 evictions
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) * by the processes running on the same package. One thing we can do is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) * to shuffle the initial stack for them, so we give the architecture
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) * an opportunity to do so here.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) sp = arch_align_stack(bprm->p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) sp = mm->start_stack;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) /* stack the program arguments and environment */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) if (transfer_args_to_stack(bprm, &sp) < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) sp &= ~15;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) * If this architecture has a platform capability string, copy it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) * to userspace. In some cases (Sparc), this info is impossible
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) * for userspace to get any other way, in others (i386) it is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) * merely difficult.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) k_platform = ELF_PLATFORM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) u_platform = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) if (k_platform) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) platform_len = strlen(k_platform) + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) sp -= platform_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) u_platform = (char __user *) sp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) if (copy_to_user(u_platform, k_platform, platform_len) != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) * If this architecture has a "base" platform capability
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545) * string, copy it to userspace.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) k_base_platform = ELF_BASE_PLATFORM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) u_base_platform = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) if (k_base_platform) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551) platform_len = strlen(k_base_platform) + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552) sp -= platform_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553) u_base_platform = (char __user *) sp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554) if (copy_to_user(u_base_platform, k_base_platform, platform_len) != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555) return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558) sp &= ~7UL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560) /* stack the load map(s) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561) len = sizeof(struct elf32_fdpic_loadmap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562) len += sizeof(struct elf32_fdpic_loadseg) * exec_params->loadmap->nsegs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563) sp = (sp - len) & ~7UL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564) exec_params->map_addr = sp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566) if (copy_to_user((void __user *) sp, exec_params->loadmap, len) != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567) return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569) current->mm->context.exec_fdpic_loadmap = (unsigned long) sp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571) if (interp_params->loadmap) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572) len = sizeof(struct elf32_fdpic_loadmap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573) len += sizeof(struct elf32_fdpic_loadseg) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574) interp_params->loadmap->nsegs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575) sp = (sp - len) & ~7UL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576) interp_params->map_addr = sp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578) if (copy_to_user((void __user *) sp, interp_params->loadmap,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579) len) != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580) return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582) current->mm->context.interp_fdpic_loadmap = (unsigned long) sp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585) /* force 16 byte _final_ alignment here for generality */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586) #define DLINFO_ITEMS 15
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588) nitems = 1 + DLINFO_ITEMS + (k_platform ? 1 : 0) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 589) (k_base_platform ? 1 : 0) + AT_VECTOR_SIZE_ARCH;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 590)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 591) if (bprm->have_execfd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 592) nitems++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 593)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 594) csp = sp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 595) sp -= nitems * 2 * sizeof(unsigned long);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 596) sp -= (bprm->envc + 1) * sizeof(char *); /* envv[] */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 597) sp -= (bprm->argc + 1) * sizeof(char *); /* argv[] */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 598) sp -= 1 * sizeof(unsigned long); /* argc */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 599)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 600) csp -= sp & 15UL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 601) sp -= sp & 15UL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 602)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 603) /* put the ELF interpreter info on the stack */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 604) #define NEW_AUX_ENT(id, val) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 605) do { \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 606) struct { unsigned long _id, _val; } __user *ent, v; \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 607) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 608) ent = (void __user *) csp; \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 609) v._id = (id); \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 610) v._val = (val); \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 611) if (copy_to_user(ent + nr, &v, sizeof(v))) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 612) return -EFAULT; \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 613) nr++; \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 614) } while (0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 615)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 616) nr = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 617) csp -= 2 * sizeof(unsigned long);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 618) NEW_AUX_ENT(AT_NULL, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 619) if (k_platform) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 620) nr = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 621) csp -= 2 * sizeof(unsigned long);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 622) NEW_AUX_ENT(AT_PLATFORM,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 623) (elf_addr_t) (unsigned long) u_platform);
^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) if (k_base_platform) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 627) nr = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 628) csp -= 2 * sizeof(unsigned long);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 629) NEW_AUX_ENT(AT_BASE_PLATFORM,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 630) (elf_addr_t) (unsigned long) u_base_platform);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 631) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 632)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 633) if (bprm->have_execfd) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 634) nr = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 635) csp -= 2 * sizeof(unsigned long);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 636) NEW_AUX_ENT(AT_EXECFD, bprm->execfd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 637) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 638)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 639) nr = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 640) csp -= DLINFO_ITEMS * 2 * sizeof(unsigned long);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 641) NEW_AUX_ENT(AT_HWCAP, ELF_HWCAP);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 642) #ifdef ELF_HWCAP2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 643) NEW_AUX_ENT(AT_HWCAP2, ELF_HWCAP2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 644) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 645) NEW_AUX_ENT(AT_PAGESZ, PAGE_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 646) NEW_AUX_ENT(AT_CLKTCK, CLOCKS_PER_SEC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 647) NEW_AUX_ENT(AT_PHDR, exec_params->ph_addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 648) NEW_AUX_ENT(AT_PHENT, sizeof(struct elf_phdr));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 649) NEW_AUX_ENT(AT_PHNUM, exec_params->hdr.e_phnum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 650) NEW_AUX_ENT(AT_BASE, interp_params->elfhdr_addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 651) NEW_AUX_ENT(AT_FLAGS, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 652) NEW_AUX_ENT(AT_ENTRY, exec_params->entry_addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 653) NEW_AUX_ENT(AT_UID, (elf_addr_t) from_kuid_munged(cred->user_ns, cred->uid));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 654) NEW_AUX_ENT(AT_EUID, (elf_addr_t) from_kuid_munged(cred->user_ns, cred->euid));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 655) NEW_AUX_ENT(AT_GID, (elf_addr_t) from_kgid_munged(cred->user_ns, cred->gid));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 656) NEW_AUX_ENT(AT_EGID, (elf_addr_t) from_kgid_munged(cred->user_ns, cred->egid));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 657) NEW_AUX_ENT(AT_SECURE, bprm->secureexec);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 658) NEW_AUX_ENT(AT_EXECFN, bprm->exec);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 659)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 660) #ifdef ARCH_DLINFO
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 661) nr = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 662) csp -= AT_VECTOR_SIZE_ARCH * 2 * sizeof(unsigned long);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 663)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 664) /* ARCH_DLINFO must come last so platform specific code can enforce
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 665) * special alignment requirements on the AUXV if necessary (eg. PPC).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 666) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 667) ARCH_DLINFO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 668) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 669) #undef NEW_AUX_ENT
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 670)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 671) /* allocate room for argv[] and envv[] */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 672) csp -= (bprm->envc + 1) * sizeof(elf_caddr_t);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 673) envp = (elf_caddr_t __user *) csp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 674) csp -= (bprm->argc + 1) * sizeof(elf_caddr_t);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 675) argv = (elf_caddr_t __user *) csp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 676)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 677) /* stack argc */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 678) csp -= sizeof(unsigned long);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 679) if (put_user(bprm->argc, (unsigned long __user *) csp))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 680) return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 681)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 682) BUG_ON(csp != sp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 683)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 684) /* fill in the argv[] array */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 685) #ifdef CONFIG_MMU
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 686) current->mm->arg_start = bprm->p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 687) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 688) current->mm->arg_start = current->mm->start_stack -
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 689) (MAX_ARG_PAGES * PAGE_SIZE - bprm->p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 690) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 691)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 692) p = (char __user *) current->mm->arg_start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 693) for (loop = bprm->argc; loop > 0; loop--) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 694) if (put_user((elf_caddr_t) p, argv++))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 695) return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 696) len = strnlen_user(p, MAX_ARG_STRLEN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 697) if (!len || len > MAX_ARG_STRLEN)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 698) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 699) p += len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 700) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 701) if (put_user(NULL, argv))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 702) return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 703) current->mm->arg_end = (unsigned long) p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 704)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 705) /* fill in the envv[] array */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 706) current->mm->env_start = (unsigned long) p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 707) for (loop = bprm->envc; loop > 0; loop--) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 708) if (put_user((elf_caddr_t)(unsigned long) p, envp++))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 709) return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 710) len = strnlen_user(p, MAX_ARG_STRLEN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 711) if (!len || len > MAX_ARG_STRLEN)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 712) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 713) p += len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 714) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 715) if (put_user(NULL, envp))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 716) return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 717) current->mm->env_end = (unsigned long) p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 718)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 719) mm->start_stack = (unsigned long) sp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 720) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 721) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 722)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 723) /*****************************************************************************/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 724) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 725) * load the appropriate binary image (executable or interpreter) into memory
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 726) * - we assume no MMU is available
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 727) * - if no other PIC bits are set in params->hdr->e_flags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 728) * - we assume that the LOADable segments in the binary are independently relocatable
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 729) * - we assume R/O executable segments are shareable
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 730) * - else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 731) * - we assume the loadable parts of the image to require fixed displacement
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 732) * - the image is not shareable
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 733) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 734) static int elf_fdpic_map_file(struct elf_fdpic_params *params,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 735) struct file *file,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 736) struct mm_struct *mm,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 737) const char *what)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 738) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 739) struct elf32_fdpic_loadmap *loadmap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 740) #ifdef CONFIG_MMU
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 741) struct elf32_fdpic_loadseg *mseg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 742) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 743) struct elf32_fdpic_loadseg *seg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 744) struct elf32_phdr *phdr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 745) unsigned long load_addr, stop;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 746) unsigned nloads, tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 747) size_t size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 748) int loop, ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 749)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 750) /* allocate a load map table */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 751) nloads = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 752) for (loop = 0; loop < params->hdr.e_phnum; loop++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 753) if (params->phdrs[loop].p_type == PT_LOAD)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 754) nloads++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 755)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 756) if (nloads == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 757) return -ELIBBAD;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 758)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 759) size = sizeof(*loadmap) + nloads * sizeof(*seg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 760) loadmap = kzalloc(size, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 761) if (!loadmap)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 762) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 763)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 764) params->loadmap = loadmap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 765)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 766) loadmap->version = ELF32_FDPIC_LOADMAP_VERSION;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 767) loadmap->nsegs = nloads;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 768)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 769) load_addr = params->load_addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 770) seg = loadmap->segs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 771)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 772) /* map the requested LOADs into the memory space */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 773) switch (params->flags & ELF_FDPIC_FLAG_ARRANGEMENT) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 774) case ELF_FDPIC_FLAG_CONSTDISP:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 775) case ELF_FDPIC_FLAG_CONTIGUOUS:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 776) #ifndef CONFIG_MMU
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 777) ret = elf_fdpic_map_file_constdisp_on_uclinux(params, file, mm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 778) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 779) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 780) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 781) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 782) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 783) ret = elf_fdpic_map_file_by_direct_mmap(params, file, mm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 784) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 785) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 786) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 787) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 788)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 789) /* map the entry point */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 790) if (params->hdr.e_entry) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 791) seg = loadmap->segs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 792) for (loop = loadmap->nsegs; loop > 0; loop--, seg++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 793) if (params->hdr.e_entry >= seg->p_vaddr &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 794) params->hdr.e_entry < seg->p_vaddr + seg->p_memsz) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 795) params->entry_addr =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 796) (params->hdr.e_entry - seg->p_vaddr) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 797) seg->addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 798) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 799) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 800) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 801) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 802)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 803) /* determine where the program header table has wound up if mapped */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 804) stop = params->hdr.e_phoff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 805) stop += params->hdr.e_phnum * sizeof (struct elf_phdr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 806) phdr = params->phdrs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 807)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 808) for (loop = 0; loop < params->hdr.e_phnum; loop++, phdr++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 809) if (phdr->p_type != PT_LOAD)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 810) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 811)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 812) if (phdr->p_offset > params->hdr.e_phoff ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 813) phdr->p_offset + phdr->p_filesz < stop)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 814) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 815)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 816) seg = loadmap->segs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 817) for (loop = loadmap->nsegs; loop > 0; loop--, seg++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 818) if (phdr->p_vaddr >= seg->p_vaddr &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 819) phdr->p_vaddr + phdr->p_filesz <=
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 820) seg->p_vaddr + seg->p_memsz) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 821) params->ph_addr =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 822) (phdr->p_vaddr - seg->p_vaddr) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 823) seg->addr +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 824) params->hdr.e_phoff - phdr->p_offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 825) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 826) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 827) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 828) break;
^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) /* determine where the dynamic section has wound up if there is one */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 832) phdr = params->phdrs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 833) for (loop = 0; loop < params->hdr.e_phnum; loop++, phdr++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 834) if (phdr->p_type != PT_DYNAMIC)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 835) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 836)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 837) seg = loadmap->segs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 838) for (loop = loadmap->nsegs; loop > 0; loop--, seg++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 839) if (phdr->p_vaddr >= seg->p_vaddr &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 840) phdr->p_vaddr + phdr->p_memsz <=
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 841) seg->p_vaddr + seg->p_memsz) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 842) Elf32_Dyn __user *dyn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 843) Elf32_Sword d_tag;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 844)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 845) params->dynamic_addr =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 846) (phdr->p_vaddr - seg->p_vaddr) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 847) seg->addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 848)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 849) /* check the dynamic section contains at least
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 850) * one item, and that the last item is a NULL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 851) * entry */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 852) if (phdr->p_memsz == 0 ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 853) phdr->p_memsz % sizeof(Elf32_Dyn) != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 854) goto dynamic_error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 855)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 856) tmp = phdr->p_memsz / sizeof(Elf32_Dyn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 857) dyn = (Elf32_Dyn __user *)params->dynamic_addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 858) if (get_user(d_tag, &dyn[tmp - 1].d_tag) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 859) d_tag != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 860) goto dynamic_error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 861) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 862) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 863) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 864) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 865) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 866)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 867) /* now elide adjacent segments in the load map on MMU linux
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 868) * - on uClinux the holes between may actually be filled with system
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 869) * stuff or stuff from other processes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 870) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 871) #ifdef CONFIG_MMU
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 872) nloads = loadmap->nsegs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 873) mseg = loadmap->segs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 874) seg = mseg + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 875) for (loop = 1; loop < nloads; loop++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 876) /* see if we have a candidate for merging */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 877) if (seg->p_vaddr - mseg->p_vaddr == seg->addr - mseg->addr) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 878) load_addr = PAGE_ALIGN(mseg->addr + mseg->p_memsz);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 879) if (load_addr == (seg->addr & PAGE_MASK)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 880) mseg->p_memsz +=
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 881) load_addr -
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 882) (mseg->addr + mseg->p_memsz);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 883) mseg->p_memsz += seg->addr & ~PAGE_MASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 884) mseg->p_memsz += seg->p_memsz;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 885) loadmap->nsegs--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 886) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 887) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 888) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 889)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 890) mseg++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 891) if (mseg != seg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 892) *mseg = *seg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 893) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 894) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 895)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 896) kdebug("Mapped Object [%s]:", what);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 897) kdebug("- elfhdr : %lx", params->elfhdr_addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 898) kdebug("- entry : %lx", params->entry_addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 899) kdebug("- PHDR[] : %lx", params->ph_addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 900) kdebug("- DYNAMIC[]: %lx", params->dynamic_addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 901) seg = loadmap->segs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 902) for (loop = 0; loop < loadmap->nsegs; loop++, seg++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 903) kdebug("- LOAD[%d] : %08x-%08x [va=%x ms=%x]",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 904) loop,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 905) seg->addr, seg->addr + seg->p_memsz - 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 906) seg->p_vaddr, seg->p_memsz);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 907)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 908) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 909)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 910) dynamic_error:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 911) printk("ELF FDPIC %s with invalid DYNAMIC section (inode=%lu)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 912) what, file_inode(file)->i_ino);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 913) return -ELIBBAD;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 914) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 915)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 916) /*****************************************************************************/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 917) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 918) * map a file with constant displacement under uClinux
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 919) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 920) #ifndef CONFIG_MMU
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 921) static int elf_fdpic_map_file_constdisp_on_uclinux(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 922) struct elf_fdpic_params *params,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 923) struct file *file,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 924) struct mm_struct *mm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 925) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 926) struct elf32_fdpic_loadseg *seg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 927) struct elf32_phdr *phdr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 928) unsigned long load_addr, base = ULONG_MAX, top = 0, maddr = 0, mflags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 929) int loop, ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 930)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 931) load_addr = params->load_addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 932) seg = params->loadmap->segs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 933)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 934) /* determine the bounds of the contiguous overall allocation we must
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 935) * make */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 936) phdr = params->phdrs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 937) for (loop = 0; loop < params->hdr.e_phnum; loop++, phdr++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 938) if (params->phdrs[loop].p_type != PT_LOAD)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 939) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 940)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 941) if (base > phdr->p_vaddr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 942) base = phdr->p_vaddr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 943) if (top < phdr->p_vaddr + phdr->p_memsz)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 944) top = phdr->p_vaddr + phdr->p_memsz;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 945) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 946)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 947) /* allocate one big anon block for everything */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 948) mflags = MAP_PRIVATE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 949) if (params->flags & ELF_FDPIC_FLAG_EXECUTABLE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 950) mflags |= MAP_EXECUTABLE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 951)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 952) maddr = vm_mmap(NULL, load_addr, top - base,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 953) PROT_READ | PROT_WRITE | PROT_EXEC, mflags, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 954) if (IS_ERR_VALUE(maddr))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 955) return (int) maddr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 956)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 957) if (load_addr != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 958) load_addr += PAGE_ALIGN(top - base);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 959)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 960) /* and then load the file segments into it */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 961) phdr = params->phdrs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 962) for (loop = 0; loop < params->hdr.e_phnum; loop++, phdr++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 963) if (params->phdrs[loop].p_type != PT_LOAD)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 964) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 965)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 966) seg->addr = maddr + (phdr->p_vaddr - base);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 967) seg->p_vaddr = phdr->p_vaddr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 968) seg->p_memsz = phdr->p_memsz;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 969)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 970) ret = read_code(file, seg->addr, phdr->p_offset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 971) phdr->p_filesz);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 972) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 973) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 974)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 975) /* map the ELF header address if in this segment */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 976) if (phdr->p_offset == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 977) params->elfhdr_addr = seg->addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 978)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 979) /* clear any space allocated but not loaded */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 980) if (phdr->p_filesz < phdr->p_memsz) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 981) if (clear_user((void *) (seg->addr + phdr->p_filesz),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 982) phdr->p_memsz - phdr->p_filesz))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 983) return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 984) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 985)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 986) if (mm) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 987) if (phdr->p_flags & PF_X) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 988) if (!mm->start_code) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 989) mm->start_code = seg->addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 990) mm->end_code = seg->addr +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 991) phdr->p_memsz;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 992) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 993) } else if (!mm->start_data) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 994) mm->start_data = seg->addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 995) mm->end_data = seg->addr + phdr->p_memsz;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 996) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 997) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 998)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 999) seg++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1000) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1001)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1002) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1003) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1004) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1005)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1006) /*****************************************************************************/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1007) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1008) * map a binary by direct mmap() of the individual PT_LOAD segments
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1009) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1010) static int elf_fdpic_map_file_by_direct_mmap(struct elf_fdpic_params *params,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1011) struct file *file,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1012) struct mm_struct *mm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1013) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1014) struct elf32_fdpic_loadseg *seg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1015) struct elf32_phdr *phdr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1016) unsigned long load_addr, delta_vaddr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1017) int loop, dvset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1018)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1019) load_addr = params->load_addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1020) delta_vaddr = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1021) dvset = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1022)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1023) seg = params->loadmap->segs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1024)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1025) /* deal with each load segment separately */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1026) phdr = params->phdrs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1027) for (loop = 0; loop < params->hdr.e_phnum; loop++, phdr++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1028) unsigned long maddr, disp, excess, excess1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1029) int prot = 0, flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1030)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1031) if (phdr->p_type != PT_LOAD)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1032) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1033)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1034) kdebug("[LOAD] va=%lx of=%lx fs=%lx ms=%lx",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1035) (unsigned long) phdr->p_vaddr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1036) (unsigned long) phdr->p_offset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1037) (unsigned long) phdr->p_filesz,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1038) (unsigned long) phdr->p_memsz);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1039)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1040) /* determine the mapping parameters */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1041) if (phdr->p_flags & PF_R) prot |= PROT_READ;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1042) if (phdr->p_flags & PF_W) prot |= PROT_WRITE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1043) if (phdr->p_flags & PF_X) prot |= PROT_EXEC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1044)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1045) flags = MAP_PRIVATE | MAP_DENYWRITE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1046) if (params->flags & ELF_FDPIC_FLAG_EXECUTABLE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1047) flags |= MAP_EXECUTABLE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1048)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1049) maddr = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1050)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1051) switch (params->flags & ELF_FDPIC_FLAG_ARRANGEMENT) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1052) case ELF_FDPIC_FLAG_INDEPENDENT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1053) /* PT_LOADs are independently locatable */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1054) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1055)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1056) case ELF_FDPIC_FLAG_HONOURVADDR:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1057) /* the specified virtual address must be honoured */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1058) maddr = phdr->p_vaddr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1059) flags |= MAP_FIXED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1060) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1061)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1062) case ELF_FDPIC_FLAG_CONSTDISP:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1063) /* constant displacement
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1064) * - can be mapped anywhere, but must be mapped as a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1065) * unit
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1066) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1067) if (!dvset) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1068) maddr = load_addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1069) delta_vaddr = phdr->p_vaddr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1070) dvset = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1071) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1072) maddr = load_addr + phdr->p_vaddr - delta_vaddr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1073) flags |= MAP_FIXED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1074) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1075) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1076)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1077) case ELF_FDPIC_FLAG_CONTIGUOUS:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1078) /* contiguity handled later */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1079) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1080)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1081) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1082) BUG();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1083) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1084)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1085) maddr &= PAGE_MASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1086)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1087) /* create the mapping */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1088) disp = phdr->p_vaddr & ~PAGE_MASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1089) maddr = vm_mmap(file, maddr, phdr->p_memsz + disp, prot, flags,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1090) phdr->p_offset - disp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1091)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1092) kdebug("mmap[%d] <file> sz=%lx pr=%x fl=%x of=%lx --> %08lx",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1093) loop, phdr->p_memsz + disp, prot, flags,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1094) phdr->p_offset - disp, maddr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1095)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1096) if (IS_ERR_VALUE(maddr))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1097) return (int) maddr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1098)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1099) if ((params->flags & ELF_FDPIC_FLAG_ARRANGEMENT) ==
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1100) ELF_FDPIC_FLAG_CONTIGUOUS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1101) load_addr += PAGE_ALIGN(phdr->p_memsz + disp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1102)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1103) seg->addr = maddr + disp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1104) seg->p_vaddr = phdr->p_vaddr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1105) seg->p_memsz = phdr->p_memsz;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1106)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1107) /* map the ELF header address if in this segment */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1108) if (phdr->p_offset == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1109) params->elfhdr_addr = seg->addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1110)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1111) /* clear the bit between beginning of mapping and beginning of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1112) * PT_LOAD */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1113) if (prot & PROT_WRITE && disp > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1114) kdebug("clear[%d] ad=%lx sz=%lx", loop, maddr, disp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1115) if (clear_user((void __user *) maddr, disp))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1116) return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1117) maddr += disp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1118) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1119)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1120) /* clear any space allocated but not loaded
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1121) * - on uClinux we can just clear the lot
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1122) * - on MMU linux we'll get a SIGBUS beyond the last page
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1123) * extant in the file
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1124) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1125) excess = phdr->p_memsz - phdr->p_filesz;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1126) excess1 = PAGE_SIZE - ((maddr + phdr->p_filesz) & ~PAGE_MASK);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1127)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1128) #ifdef CONFIG_MMU
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1129) if (excess > excess1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1130) unsigned long xaddr = maddr + phdr->p_filesz + excess1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1131) unsigned long xmaddr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1132)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1133) flags |= MAP_FIXED | MAP_ANONYMOUS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1134) xmaddr = vm_mmap(NULL, xaddr, excess - excess1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1135) prot, flags, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1136)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1137) kdebug("mmap[%d] <anon>"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1138) " ad=%lx sz=%lx pr=%x fl=%x of=0 --> %08lx",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1139) loop, xaddr, excess - excess1, prot, flags,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1140) xmaddr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1141)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1142) if (xmaddr != xaddr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1143) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1144) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1145)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1146) if (prot & PROT_WRITE && excess1 > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1147) kdebug("clear[%d] ad=%lx sz=%lx",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1148) loop, maddr + phdr->p_filesz, excess1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1149) if (clear_user((void __user *) maddr + phdr->p_filesz,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1150) excess1))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1151) return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1152) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1153)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1154) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1155) if (excess > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1156) kdebug("clear[%d] ad=%lx sz=%lx",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1157) loop, maddr + phdr->p_filesz, excess);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1158) if (clear_user((void *) maddr + phdr->p_filesz, excess))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1159) return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1160) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1161) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1162)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1163) if (mm) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1164) if (phdr->p_flags & PF_X) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1165) if (!mm->start_code) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1166) mm->start_code = maddr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1167) mm->end_code = maddr + phdr->p_memsz;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1168) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1169) } else if (!mm->start_data) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1170) mm->start_data = maddr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1171) mm->end_data = maddr + phdr->p_memsz;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1172) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1173) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1174)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1175) seg++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1176) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1177)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1178) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1179) }
^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) * ELF-FDPIC core dumper
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1184) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1185) * Modelled on fs/exec.c:aout_core_dump()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1186) * Jeremy Fitzhardinge <jeremy@sw.oz.au>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1187) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1188) * Modelled on fs/binfmt_elf.c core dumper
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1189) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1190) #ifdef CONFIG_ELF_CORE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1191)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1192) struct elf_prstatus_fdpic
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1193) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1194) struct elf_siginfo pr_info; /* Info associated with signal */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1195) short pr_cursig; /* Current signal */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1196) unsigned long pr_sigpend; /* Set of pending signals */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1197) unsigned long pr_sighold; /* Set of held signals */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1198) pid_t pr_pid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1199) pid_t pr_ppid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1200) pid_t pr_pgrp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1201) pid_t pr_sid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1202) struct __kernel_old_timeval pr_utime; /* User time */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1203) struct __kernel_old_timeval pr_stime; /* System time */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1204) struct __kernel_old_timeval pr_cutime; /* Cumulative user time */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1205) struct __kernel_old_timeval pr_cstime; /* Cumulative system time */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1206) elf_gregset_t pr_reg; /* GP registers */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1207) /* When using FDPIC, the loadmap addresses need to be communicated
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1208) * to GDB in order for GDB to do the necessary relocations. The
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1209) * fields (below) used to communicate this information are placed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1210) * immediately after ``pr_reg'', so that the loadmap addresses may
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1211) * be viewed as part of the register set if so desired.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1212) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1213) unsigned long pr_exec_fdpic_loadmap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1214) unsigned long pr_interp_fdpic_loadmap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1215) int pr_fpvalid; /* True if math co-processor being used. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1216) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1217)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1218) /* An ELF note in memory */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1219) struct memelfnote
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1220) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1221) const char *name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1222) int type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1223) unsigned int datasz;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1224) void *data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1225) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1226)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1227) static int notesize(struct memelfnote *en)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1228) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1229) int sz;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1230)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1231) sz = sizeof(struct elf_note);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1232) sz += roundup(strlen(en->name) + 1, 4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1233) sz += roundup(en->datasz, 4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1234)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1235) return sz;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1236) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1237)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1238) /* #define DEBUG */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1239)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1240) static int writenote(struct memelfnote *men, struct coredump_params *cprm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1241) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1242) struct elf_note en;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1243) en.n_namesz = strlen(men->name) + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1244) en.n_descsz = men->datasz;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1245) en.n_type = men->type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1246)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1247) return dump_emit(cprm, &en, sizeof(en)) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1248) dump_emit(cprm, men->name, en.n_namesz) && dump_align(cprm, 4) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1249) dump_emit(cprm, men->data, men->datasz) && dump_align(cprm, 4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1250) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1251)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1252) static inline void fill_elf_fdpic_header(struct elfhdr *elf, int segs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1253) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1254) memcpy(elf->e_ident, ELFMAG, SELFMAG);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1255) elf->e_ident[EI_CLASS] = ELF_CLASS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1256) elf->e_ident[EI_DATA] = ELF_DATA;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1257) elf->e_ident[EI_VERSION] = EV_CURRENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1258) elf->e_ident[EI_OSABI] = ELF_OSABI;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1259) memset(elf->e_ident+EI_PAD, 0, EI_NIDENT-EI_PAD);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1260)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1261) elf->e_type = ET_CORE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1262) elf->e_machine = ELF_ARCH;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1263) elf->e_version = EV_CURRENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1264) elf->e_entry = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1265) elf->e_phoff = sizeof(struct elfhdr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1266) elf->e_shoff = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1267) elf->e_flags = ELF_FDPIC_CORE_EFLAGS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1268) elf->e_ehsize = sizeof(struct elfhdr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1269) elf->e_phentsize = sizeof(struct elf_phdr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1270) elf->e_phnum = segs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1271) elf->e_shentsize = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1272) elf->e_shnum = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1273) elf->e_shstrndx = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1274) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1275) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1276)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1277) static inline void fill_elf_note_phdr(struct elf_phdr *phdr, int sz, loff_t offset)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1278) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1279) phdr->p_type = PT_NOTE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1280) phdr->p_offset = offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1281) phdr->p_vaddr = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1282) phdr->p_paddr = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1283) phdr->p_filesz = sz;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1284) phdr->p_memsz = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1285) phdr->p_flags = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1286) phdr->p_align = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1287) return;
^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 inline void fill_note(struct memelfnote *note, const char *name, int type,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1291) unsigned int sz, void *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1292) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1293) note->name = name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1294) note->type = type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1295) note->datasz = sz;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1296) note->data = data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1297) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1298) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1299)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1300) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1301) * fill up all the fields in prstatus from the given task struct, except
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1302) * registers which need to be filled up separately.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1303) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1304) static void fill_prstatus(struct elf_prstatus_fdpic *prstatus,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1305) struct task_struct *p, long signr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1306) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1307) prstatus->pr_info.si_signo = prstatus->pr_cursig = signr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1308) prstatus->pr_sigpend = p->pending.signal.sig[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1309) prstatus->pr_sighold = p->blocked.sig[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1310) rcu_read_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1311) prstatus->pr_ppid = task_pid_vnr(rcu_dereference(p->real_parent));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1312) rcu_read_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1313) prstatus->pr_pid = task_pid_vnr(p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1314) prstatus->pr_pgrp = task_pgrp_vnr(p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1315) prstatus->pr_sid = task_session_vnr(p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1316) if (thread_group_leader(p)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1317) struct task_cputime cputime;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1318)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1319) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1320) * This is the record for the group leader. It shows the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1321) * group-wide total, not its individual thread total.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1322) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1323) thread_group_cputime(p, &cputime);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1324) prstatus->pr_utime = ns_to_kernel_old_timeval(cputime.utime);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1325) prstatus->pr_stime = ns_to_kernel_old_timeval(cputime.stime);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1326) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1327) u64 utime, stime;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1328)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1329) task_cputime(p, &utime, &stime);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1330) prstatus->pr_utime = ns_to_kernel_old_timeval(utime);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1331) prstatus->pr_stime = ns_to_kernel_old_timeval(stime);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1332) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1333) prstatus->pr_cutime = ns_to_kernel_old_timeval(p->signal->cutime);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1334) prstatus->pr_cstime = ns_to_kernel_old_timeval(p->signal->cstime);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1335)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1336) prstatus->pr_exec_fdpic_loadmap = p->mm->context.exec_fdpic_loadmap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1337) prstatus->pr_interp_fdpic_loadmap = p->mm->context.interp_fdpic_loadmap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1338) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1339)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1340) static int fill_psinfo(struct elf_prpsinfo *psinfo, struct task_struct *p,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1341) struct mm_struct *mm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1342) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1343) const struct cred *cred;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1344) unsigned int i, len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1345)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1346) /* first copy the parameters from user space */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1347) memset(psinfo, 0, sizeof(struct elf_prpsinfo));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1348)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1349) len = mm->arg_end - mm->arg_start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1350) if (len >= ELF_PRARGSZ)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1351) len = ELF_PRARGSZ - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1352) if (copy_from_user(&psinfo->pr_psargs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1353) (const char __user *) mm->arg_start, len))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1354) return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1355) for (i = 0; i < len; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1356) if (psinfo->pr_psargs[i] == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1357) psinfo->pr_psargs[i] = ' ';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1358) psinfo->pr_psargs[len] = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1359)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1360) rcu_read_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1361) psinfo->pr_ppid = task_pid_vnr(rcu_dereference(p->real_parent));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1362) rcu_read_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1363) psinfo->pr_pid = task_pid_vnr(p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1364) psinfo->pr_pgrp = task_pgrp_vnr(p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1365) psinfo->pr_sid = task_session_vnr(p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1366)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1367) i = p->state ? ffz(~p->state) + 1 : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1368) psinfo->pr_state = i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1369) psinfo->pr_sname = (i > 5) ? '.' : "RSDTZW"[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1370) psinfo->pr_zomb = psinfo->pr_sname == 'Z';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1371) psinfo->pr_nice = task_nice(p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1372) psinfo->pr_flag = p->flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1373) rcu_read_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1374) cred = __task_cred(p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1375) SET_UID(psinfo->pr_uid, from_kuid_munged(cred->user_ns, cred->uid));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1376) SET_GID(psinfo->pr_gid, from_kgid_munged(cred->user_ns, cred->gid));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1377) rcu_read_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1378) strncpy(psinfo->pr_fname, p->comm, sizeof(psinfo->pr_fname));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1379)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1380) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1381) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1382)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1383) /* Here is the structure in which status of each thread is captured. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1384) struct elf_thread_status
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1385) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1386) struct elf_thread_status *next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1387) struct elf_prstatus_fdpic prstatus; /* NT_PRSTATUS */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1388) elf_fpregset_t fpu; /* NT_PRFPREG */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1389) struct memelfnote notes[2];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1390) int num_notes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1391) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1392)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1393) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1394) * In order to add the specific thread information for the elf file format,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1395) * we need to keep a linked list of every thread's pr_status and then create
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1396) * a single section for them in the final core file.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1397) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1398) static struct elf_thread_status *elf_dump_thread_status(long signr, struct task_struct *p, int *sz)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1399) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1400) const struct user_regset_view *view = task_user_regset_view(p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1401) struct elf_thread_status *t;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1402) int i, ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1403)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1404) t = kzalloc(sizeof(struct elf_thread_status), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1405) if (!t)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1406) return t;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1407)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1408) fill_prstatus(&t->prstatus, p, signr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1409) regset_get(p, &view->regsets[0],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1410) sizeof(t->prstatus.pr_reg), &t->prstatus.pr_reg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1411)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1412) fill_note(&t->notes[0], "CORE", NT_PRSTATUS, sizeof(t->prstatus),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1413) &t->prstatus);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1414) t->num_notes++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1415) *sz += notesize(&t->notes[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1416)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1417) for (i = 1; i < view->n; ++i) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1418) const struct user_regset *regset = &view->regsets[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1419) if (regset->core_note_type != NT_PRFPREG)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1420) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1421) if (regset->active && regset->active(p, regset) <= 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1422) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1423) ret = regset_get(p, regset, sizeof(t->fpu), &t->fpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1424) if (ret >= 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1425) t->prstatus.pr_fpvalid = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1426) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1427) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1428)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1429) if (t->prstatus.pr_fpvalid) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1430) fill_note(&t->notes[1], "CORE", NT_PRFPREG, sizeof(t->fpu),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1431) &t->fpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1432) t->num_notes++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1433) *sz += notesize(&t->notes[1]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1434) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1435) return t;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1436) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1437)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1438) static void fill_extnum_info(struct elfhdr *elf, struct elf_shdr *shdr4extnum,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1439) elf_addr_t e_shoff, int segs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1440) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1441) elf->e_shoff = e_shoff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1442) elf->e_shentsize = sizeof(*shdr4extnum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1443) elf->e_shnum = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1444) elf->e_shstrndx = SHN_UNDEF;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1445)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1446) memset(shdr4extnum, 0, sizeof(*shdr4extnum));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1447)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1448) shdr4extnum->sh_type = SHT_NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1449) shdr4extnum->sh_size = elf->e_shnum;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1450) shdr4extnum->sh_link = elf->e_shstrndx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1451) shdr4extnum->sh_info = segs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1452) }
^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) * dump the segments for an MMU process
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1456) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1457) static bool elf_fdpic_dump_segments(struct coredump_params *cprm,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1458) struct core_vma_metadata *vma_meta,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1459) int vma_count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1460) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1461) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1462)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1463) for (i = 0; i < vma_count; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1464) struct core_vma_metadata *meta = vma_meta + i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1465)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1466) if (!dump_user_range(cprm, meta->start, meta->dump_size))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1467) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1468) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1469) return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1470) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1471)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1472) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1473) * Actual dumper
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1474) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1475) * This is a two-pass process; first we find the offsets of the bits,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1476) * and then they are actually written out. If we run out of core limit
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1477) * we just truncate.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1478) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1479) static int elf_fdpic_core_dump(struct coredump_params *cprm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1480) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1481) int has_dumped = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1482) int vma_count, segs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1483) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1484) struct elfhdr *elf = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1485) loff_t offset = 0, dataoff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1486) struct memelfnote psinfo_note, auxv_note;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1487) struct elf_prpsinfo *psinfo = NULL; /* NT_PRPSINFO */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1488) struct elf_thread_status *thread_list = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1489) int thread_status_size = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1490) elf_addr_t *auxv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1491) struct elf_phdr *phdr4note = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1492) struct elf_shdr *shdr4extnum = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1493) Elf_Half e_phnum;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1494) elf_addr_t e_shoff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1495) struct core_thread *ct;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1496) struct elf_thread_status *tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1497) struct core_vma_metadata *vma_meta = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1498) size_t vma_data_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1499)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1500) /* alloc memory for large data structures: too large to be on stack */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1501) elf = kmalloc(sizeof(*elf), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1502) if (!elf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1503) goto end_coredump;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1504) psinfo = kmalloc(sizeof(*psinfo), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1505) if (!psinfo)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1506) goto end_coredump;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1507)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1508) if (dump_vma_snapshot(cprm, &vma_count, &vma_meta, &vma_data_size))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1509) goto end_coredump;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1510)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1511) for (ct = current->mm->core_state->dumper.next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1512) ct; ct = ct->next) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1513) tmp = elf_dump_thread_status(cprm->siginfo->si_signo,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1514) ct->task, &thread_status_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1515) if (!tmp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1516) goto end_coredump;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1517)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1518) tmp->next = thread_list;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1519) thread_list = tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1520) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1521)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1522) /* now collect the dump for the current */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1523) tmp = elf_dump_thread_status(cprm->siginfo->si_signo,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1524) current, &thread_status_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1525) if (!tmp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1526) goto end_coredump;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1527) tmp->next = thread_list;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1528) thread_list = tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1529)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1530) segs = vma_count + elf_core_extra_phdrs();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1531)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1532) /* for notes section */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1533) segs++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1534)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1535) /* If segs > PN_XNUM(0xffff), then e_phnum overflows. To avoid
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1536) * this, kernel supports extended numbering. Have a look at
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1537) * include/linux/elf.h for further information. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1538) e_phnum = segs > PN_XNUM ? PN_XNUM : segs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1539)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1540) /* Set up header */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1541) fill_elf_fdpic_header(elf, e_phnum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1542)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1543) has_dumped = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1544) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1545) * Set up the notes in similar form to SVR4 core dumps made
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1546) * with info from their /proc.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1547) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1548)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1549) fill_psinfo(psinfo, current->group_leader, current->mm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1550) fill_note(&psinfo_note, "CORE", NT_PRPSINFO, sizeof(*psinfo), psinfo);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1551) thread_status_size += notesize(&psinfo_note);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1552)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1553) auxv = (elf_addr_t *) current->mm->saved_auxv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1554) i = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1555) do
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1556) i += 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1557) while (auxv[i - 2] != AT_NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1558) fill_note(&auxv_note, "CORE", NT_AUXV, i * sizeof(elf_addr_t), auxv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1559) thread_status_size += notesize(&auxv_note);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1560)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1561) offset = sizeof(*elf); /* Elf header */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1562) offset += segs * sizeof(struct elf_phdr); /* Program headers */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1563)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1564) /* Write notes phdr entry */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1565) phdr4note = kmalloc(sizeof(*phdr4note), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1566) if (!phdr4note)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1567) goto end_coredump;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1568)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1569) fill_elf_note_phdr(phdr4note, thread_status_size, offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1570) offset += thread_status_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1571)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1572) /* Page-align dumped data */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1573) dataoff = offset = roundup(offset, ELF_EXEC_PAGESIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1574)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1575) offset += vma_data_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1576) offset += elf_core_extra_data_size();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1577) e_shoff = offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1578)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1579) if (e_phnum == PN_XNUM) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1580) shdr4extnum = kmalloc(sizeof(*shdr4extnum), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1581) if (!shdr4extnum)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1582) goto end_coredump;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1583) fill_extnum_info(elf, shdr4extnum, e_shoff, segs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1584) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1585)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1586) offset = dataoff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1587)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1588) if (!dump_emit(cprm, elf, sizeof(*elf)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1589) goto end_coredump;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1590)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1591) if (!dump_emit(cprm, phdr4note, sizeof(*phdr4note)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1592) goto end_coredump;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1593)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1594) /* write program headers for segments dump */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1595) for (i = 0; i < vma_count; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1596) struct core_vma_metadata *meta = vma_meta + i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1597) struct elf_phdr phdr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1598) size_t sz;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1599)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1600) sz = meta->end - meta->start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1601)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1602) phdr.p_type = PT_LOAD;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1603) phdr.p_offset = offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1604) phdr.p_vaddr = meta->start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1605) phdr.p_paddr = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1606) phdr.p_filesz = meta->dump_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1607) phdr.p_memsz = sz;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1608) offset += phdr.p_filesz;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1609) phdr.p_flags = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1610) if (meta->flags & VM_READ)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1611) phdr.p_flags |= PF_R;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1612) if (meta->flags & VM_WRITE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1613) phdr.p_flags |= PF_W;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1614) if (meta->flags & VM_EXEC)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1615) phdr.p_flags |= PF_X;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1616) phdr.p_align = ELF_EXEC_PAGESIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1617)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1618) if (!dump_emit(cprm, &phdr, sizeof(phdr)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1619) goto end_coredump;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1620) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1621)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1622) if (!elf_core_write_extra_phdrs(cprm, offset))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1623) goto end_coredump;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1624)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1625) /* write out the notes section */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1626) if (!writenote(thread_list->notes, cprm))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1627) goto end_coredump;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1628) if (!writenote(&psinfo_note, cprm))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1629) goto end_coredump;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1630) if (!writenote(&auxv_note, cprm))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1631) goto end_coredump;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1632) for (i = 1; i < thread_list->num_notes; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1633) if (!writenote(thread_list->notes + i, cprm))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1634) goto end_coredump;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1635)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1636) /* write out the thread status notes section */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1637) for (tmp = thread_list->next; tmp; tmp = tmp->next) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1638) for (i = 0; i < tmp->num_notes; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1639) if (!writenote(&tmp->notes[i], cprm))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1640) goto end_coredump;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1641) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1642)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1643) if (!dump_skip(cprm, dataoff - cprm->pos))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1644) goto end_coredump;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1645)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1646) if (!elf_fdpic_dump_segments(cprm, vma_meta, vma_count))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1647) goto end_coredump;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1648)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1649) if (!elf_core_write_extra_data(cprm))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1650) goto end_coredump;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1651)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1652) if (e_phnum == PN_XNUM) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1653) if (!dump_emit(cprm, shdr4extnum, sizeof(*shdr4extnum)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1654) goto end_coredump;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1655) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1656)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1657) if (cprm->file->f_pos != offset) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1658) /* Sanity check */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1659) printk(KERN_WARNING
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1660) "elf_core_dump: file->f_pos (%lld) != offset (%lld)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1661) cprm->file->f_pos, offset);
^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) end_coredump:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1665) while (thread_list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1666) tmp = thread_list;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1667) thread_list = thread_list->next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1668) kfree(tmp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1669) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1670) kvfree(vma_meta);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1671) kfree(phdr4note);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1672) kfree(elf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1673) kfree(psinfo);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1674) kfree(shdr4extnum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1675) return has_dumped;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1676) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1677)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1678) #endif /* CONFIG_ELF_CORE */