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