Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * linux/fs/binfmt_elf.c |
| 3 | * |
| 4 | * These are the functions used to load ELF format executables as used |
| 5 | * on SVr4 machines. Information on the format may be found in the book |
| 6 | * "UNIX SYSTEM V RELEASE 4 Programmers Guide: Ansi C and Programming Support |
| 7 | * Tools". |
| 8 | * |
| 9 | * Copyright 1993, 1994: Eric Youngdale (ericy@cais.com). |
| 10 | */ |
| 11 | |
| 12 | #include <linux/module.h> |
| 13 | #include <linux/kernel.h> |
| 14 | #include <linux/fs.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 15 | #include <linux/mm.h> |
| 16 | #include <linux/mman.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | #include <linux/errno.h> |
| 18 | #include <linux/signal.h> |
| 19 | #include <linux/binfmts.h> |
| 20 | #include <linux/string.h> |
| 21 | #include <linux/file.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | #include <linux/slab.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | #include <linux/personality.h> |
| 24 | #include <linux/elfcore.h> |
| 25 | #include <linux/init.h> |
| 26 | #include <linux/highuid.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | #include <linux/compiler.h> |
| 28 | #include <linux/highmem.h> |
| 29 | #include <linux/pagemap.h> |
| 30 | #include <linux/security.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 | #include <linux/random.h> |
Jesper Juhl | f4e5cc2 | 2006-06-23 02:05:35 -0700 | [diff] [blame] | 32 | #include <linux/elf.h> |
Alexey Dobriyan | 7e80d0d | 2007-05-08 00:28:59 -0700 | [diff] [blame] | 33 | #include <linux/utsname.h> |
Daisuke HATAYAMA | 088e7af | 2010-03-05 13:44:06 -0800 | [diff] [blame] | 34 | #include <linux/coredump.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | #include <asm/uaccess.h> |
| 36 | #include <asm/param.h> |
| 37 | #include <asm/page.h> |
| 38 | |
Jesper Juhl | f4e5cc2 | 2006-06-23 02:05:35 -0700 | [diff] [blame] | 39 | static int load_elf_binary(struct linux_binprm *bprm, struct pt_regs *regs); |
| 40 | static int load_elf_library(struct file *); |
Andrew Morton | bb1ad82 | 2008-01-30 13:31:07 +0100 | [diff] [blame] | 41 | static unsigned long elf_map(struct file *, unsigned long, struct elf_phdr *, |
| 42 | int, int, unsigned long); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 | /* |
| 45 | * If we don't support core dumping, then supply a NULL so we |
| 46 | * don't even try. |
| 47 | */ |
Christoph Hellwig | 698ba7b | 2009-12-15 16:47:37 -0800 | [diff] [blame] | 48 | #ifdef CONFIG_ELF_CORE |
Masami Hiramatsu | f6151df | 2009-12-17 15:27:16 -0800 | [diff] [blame] | 49 | static int elf_core_dump(struct coredump_params *cprm); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 50 | #else |
| 51 | #define elf_core_dump NULL |
| 52 | #endif |
| 53 | |
| 54 | #if ELF_EXEC_PAGESIZE > PAGE_SIZE |
Jesper Juhl | f4e5cc2 | 2006-06-23 02:05:35 -0700 | [diff] [blame] | 55 | #define ELF_MIN_ALIGN ELF_EXEC_PAGESIZE |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | #else |
Jesper Juhl | f4e5cc2 | 2006-06-23 02:05:35 -0700 | [diff] [blame] | 57 | #define ELF_MIN_ALIGN PAGE_SIZE |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 58 | #endif |
| 59 | |
| 60 | #ifndef ELF_CORE_EFLAGS |
| 61 | #define ELF_CORE_EFLAGS 0 |
| 62 | #endif |
| 63 | |
| 64 | #define ELF_PAGESTART(_v) ((_v) & ~(unsigned long)(ELF_MIN_ALIGN-1)) |
| 65 | #define ELF_PAGEOFFSET(_v) ((_v) & (ELF_MIN_ALIGN-1)) |
| 66 | #define ELF_PAGEALIGN(_v) (((_v) + ELF_MIN_ALIGN - 1) & ~(ELF_MIN_ALIGN - 1)) |
| 67 | |
| 68 | static struct linux_binfmt elf_format = { |
| 69 | .module = THIS_MODULE, |
| 70 | .load_binary = load_elf_binary, |
| 71 | .load_shlib = load_elf_library, |
| 72 | .core_dump = elf_core_dump, |
Andi Kleen | 9fbbd4d | 2007-02-13 13:26:26 +0100 | [diff] [blame] | 73 | .min_coredump = ELF_EXEC_PAGESIZE, |
| 74 | .hasvdso = 1 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 75 | }; |
| 76 | |
Andrew Morton | d4e3cc3 | 2007-07-21 04:37:32 -0700 | [diff] [blame] | 77 | #define BAD_ADDR(x) ((unsigned long)(x) >= TASK_SIZE) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 78 | |
| 79 | static int set_brk(unsigned long start, unsigned long end) |
| 80 | { |
| 81 | start = ELF_PAGEALIGN(start); |
| 82 | end = ELF_PAGEALIGN(end); |
| 83 | if (end > start) { |
| 84 | unsigned long addr; |
| 85 | down_write(¤t->mm->mmap_sem); |
| 86 | addr = do_brk(start, end - start); |
| 87 | up_write(¤t->mm->mmap_sem); |
| 88 | if (BAD_ADDR(addr)) |
| 89 | return addr; |
| 90 | } |
| 91 | current->mm->start_brk = current->mm->brk = end; |
| 92 | return 0; |
| 93 | } |
| 94 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 95 | /* We need to explicitly zero any fractional pages |
| 96 | after the data section (i.e. bss). This would |
| 97 | contain the junk from the file that should not |
Jesper Juhl | f4e5cc2 | 2006-06-23 02:05:35 -0700 | [diff] [blame] | 98 | be in memory |
| 99 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 100 | static int padzero(unsigned long elf_bss) |
| 101 | { |
| 102 | unsigned long nbyte; |
| 103 | |
| 104 | nbyte = ELF_PAGEOFFSET(elf_bss); |
| 105 | if (nbyte) { |
| 106 | nbyte = ELF_MIN_ALIGN - nbyte; |
| 107 | if (clear_user((void __user *) elf_bss, nbyte)) |
| 108 | return -EFAULT; |
| 109 | } |
| 110 | return 0; |
| 111 | } |
| 112 | |
Ohad Ben-Cohen | 09c6dd3 | 2008-02-03 18:05:15 +0200 | [diff] [blame] | 113 | /* Let's use some macros to make this stack manipulation a little clearer */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 114 | #ifdef CONFIG_STACK_GROWSUP |
| 115 | #define STACK_ADD(sp, items) ((elf_addr_t __user *)(sp) + (items)) |
| 116 | #define STACK_ROUND(sp, items) \ |
| 117 | ((15 + (unsigned long) ((sp) + (items))) &~ 15UL) |
Jesper Juhl | f4e5cc2 | 2006-06-23 02:05:35 -0700 | [diff] [blame] | 118 | #define STACK_ALLOC(sp, len) ({ \ |
| 119 | elf_addr_t __user *old_sp = (elf_addr_t __user *)sp; sp += len; \ |
| 120 | old_sp; }) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 121 | #else |
| 122 | #define STACK_ADD(sp, items) ((elf_addr_t __user *)(sp) - (items)) |
| 123 | #define STACK_ROUND(sp, items) \ |
| 124 | (((unsigned long) (sp - items)) &~ 15UL) |
| 125 | #define STACK_ALLOC(sp, len) ({ sp -= len ; sp; }) |
| 126 | #endif |
| 127 | |
Nathan Lynch | 483fad1 | 2008-07-22 04:48:46 +1000 | [diff] [blame] | 128 | #ifndef ELF_BASE_PLATFORM |
| 129 | /* |
| 130 | * AT_BASE_PLATFORM indicates the "real" hardware/microarchitecture. |
| 131 | * If the arch defines ELF_BASE_PLATFORM (in asm/elf.h), the value |
| 132 | * will be copied to the user stack in the same manner as AT_PLATFORM. |
| 133 | */ |
| 134 | #define ELF_BASE_PLATFORM NULL |
| 135 | #endif |
| 136 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 137 | static int |
Jesper Juhl | f4e5cc2 | 2006-06-23 02:05:35 -0700 | [diff] [blame] | 138 | create_elf_tables(struct linux_binprm *bprm, struct elfhdr *exec, |
Andi Kleen | d20894a | 2008-02-08 04:21:54 -0800 | [diff] [blame] | 139 | unsigned long load_addr, unsigned long interp_load_addr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 140 | { |
| 141 | unsigned long p = bprm->p; |
| 142 | int argc = bprm->argc; |
| 143 | int envc = bprm->envc; |
| 144 | elf_addr_t __user *argv; |
| 145 | elf_addr_t __user *envp; |
| 146 | elf_addr_t __user *sp; |
| 147 | elf_addr_t __user *u_platform; |
Nathan Lynch | 483fad1 | 2008-07-22 04:48:46 +1000 | [diff] [blame] | 148 | elf_addr_t __user *u_base_platform; |
Kees Cook | f06295b | 2009-01-07 18:08:52 -0800 | [diff] [blame] | 149 | elf_addr_t __user *u_rand_bytes; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 150 | const char *k_platform = ELF_PLATFORM; |
Nathan Lynch | 483fad1 | 2008-07-22 04:48:46 +1000 | [diff] [blame] | 151 | const char *k_base_platform = ELF_BASE_PLATFORM; |
Kees Cook | f06295b | 2009-01-07 18:08:52 -0800 | [diff] [blame] | 152 | unsigned char k_rand_bytes[16]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 153 | int items; |
| 154 | elf_addr_t *elf_info; |
| 155 | int ei_index = 0; |
David Howells | 86a264a | 2008-11-14 10:39:18 +1100 | [diff] [blame] | 156 | const struct cred *cred = current_cred(); |
Ollie Wild | b6a2fea | 2007-07-19 01:48:16 -0700 | [diff] [blame] | 157 | struct vm_area_struct *vma; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 158 | |
| 159 | /* |
Franck Bui-Huu | d68c9d6 | 2007-10-16 23:30:24 -0700 | [diff] [blame] | 160 | * In some cases (e.g. Hyper-Threading), we want to avoid L1 |
| 161 | * evictions by the processes running on the same package. One |
| 162 | * thing we can do is to shuffle the initial stack for them. |
| 163 | */ |
| 164 | |
| 165 | p = arch_align_stack(p); |
| 166 | |
| 167 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 168 | * If this architecture has a platform capability string, copy it |
| 169 | * to userspace. In some cases (Sparc), this info is impossible |
| 170 | * for userspace to get any other way, in others (i386) it is |
| 171 | * merely difficult. |
| 172 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 173 | u_platform = NULL; |
| 174 | if (k_platform) { |
| 175 | size_t len = strlen(k_platform) + 1; |
| 176 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 177 | u_platform = (elf_addr_t __user *)STACK_ALLOC(p, len); |
| 178 | if (__copy_to_user(u_platform, k_platform, len)) |
| 179 | return -EFAULT; |
| 180 | } |
| 181 | |
Nathan Lynch | 483fad1 | 2008-07-22 04:48:46 +1000 | [diff] [blame] | 182 | /* |
| 183 | * If this architecture has a "base" platform capability |
| 184 | * string, copy it to userspace. |
| 185 | */ |
| 186 | u_base_platform = NULL; |
| 187 | if (k_base_platform) { |
| 188 | size_t len = strlen(k_base_platform) + 1; |
| 189 | |
| 190 | u_base_platform = (elf_addr_t __user *)STACK_ALLOC(p, len); |
| 191 | if (__copy_to_user(u_base_platform, k_base_platform, len)) |
| 192 | return -EFAULT; |
| 193 | } |
| 194 | |
Kees Cook | f06295b | 2009-01-07 18:08:52 -0800 | [diff] [blame] | 195 | /* |
| 196 | * Generate 16 random bytes for userspace PRNG seeding. |
| 197 | */ |
| 198 | get_random_bytes(k_rand_bytes, sizeof(k_rand_bytes)); |
| 199 | u_rand_bytes = (elf_addr_t __user *) |
| 200 | STACK_ALLOC(p, sizeof(k_rand_bytes)); |
| 201 | if (__copy_to_user(u_rand_bytes, k_rand_bytes, sizeof(k_rand_bytes))) |
| 202 | return -EFAULT; |
| 203 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 204 | /* Create the ELF interpreter info */ |
Jesper Juhl | 785d557 | 2006-06-23 02:05:35 -0700 | [diff] [blame] | 205 | elf_info = (elf_addr_t *)current->mm->saved_auxv; |
Olaf Hering | 4f9a58d | 2007-10-16 23:30:12 -0700 | [diff] [blame] | 206 | /* update AT_VECTOR_SIZE_BASE if the number of NEW_AUX_ENT() changes */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 207 | #define NEW_AUX_ENT(id, val) \ |
Jesper Juhl | f4e5cc2 | 2006-06-23 02:05:35 -0700 | [diff] [blame] | 208 | do { \ |
Jesper Juhl | 785d557 | 2006-06-23 02:05:35 -0700 | [diff] [blame] | 209 | elf_info[ei_index++] = id; \ |
| 210 | elf_info[ei_index++] = val; \ |
Jesper Juhl | f4e5cc2 | 2006-06-23 02:05:35 -0700 | [diff] [blame] | 211 | } while (0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 212 | |
| 213 | #ifdef ARCH_DLINFO |
| 214 | /* |
| 215 | * ARCH_DLINFO must come first so PPC can do its special alignment of |
| 216 | * AUXV. |
Olaf Hering | 4f9a58d | 2007-10-16 23:30:12 -0700 | [diff] [blame] | 217 | * update AT_VECTOR_SIZE_ARCH if the number of NEW_AUX_ENT() in |
| 218 | * ARCH_DLINFO changes |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 219 | */ |
| 220 | ARCH_DLINFO; |
| 221 | #endif |
| 222 | NEW_AUX_ENT(AT_HWCAP, ELF_HWCAP); |
| 223 | NEW_AUX_ENT(AT_PAGESZ, ELF_EXEC_PAGESIZE); |
| 224 | NEW_AUX_ENT(AT_CLKTCK, CLOCKS_PER_SEC); |
| 225 | NEW_AUX_ENT(AT_PHDR, load_addr + exec->e_phoff); |
Jesper Juhl | f4e5cc2 | 2006-06-23 02:05:35 -0700 | [diff] [blame] | 226 | NEW_AUX_ENT(AT_PHENT, sizeof(struct elf_phdr)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 227 | NEW_AUX_ENT(AT_PHNUM, exec->e_phnum); |
| 228 | NEW_AUX_ENT(AT_BASE, interp_load_addr); |
| 229 | NEW_AUX_ENT(AT_FLAGS, 0); |
| 230 | NEW_AUX_ENT(AT_ENTRY, exec->e_entry); |
David Howells | 86a264a | 2008-11-14 10:39:18 +1100 | [diff] [blame] | 231 | NEW_AUX_ENT(AT_UID, cred->uid); |
| 232 | NEW_AUX_ENT(AT_EUID, cred->euid); |
| 233 | NEW_AUX_ENT(AT_GID, cred->gid); |
| 234 | NEW_AUX_ENT(AT_EGID, cred->egid); |
Jesper Juhl | 785d557 | 2006-06-23 02:05:35 -0700 | [diff] [blame] | 235 | NEW_AUX_ENT(AT_SECURE, security_bprm_secureexec(bprm)); |
Kees Cook | f06295b | 2009-01-07 18:08:52 -0800 | [diff] [blame] | 236 | NEW_AUX_ENT(AT_RANDOM, (elf_addr_t)(unsigned long)u_rand_bytes); |
John Reiser | 6519108 | 2008-07-21 14:21:32 -0700 | [diff] [blame] | 237 | NEW_AUX_ENT(AT_EXECFN, bprm->exec); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 238 | if (k_platform) { |
Jesper Juhl | f4e5cc2 | 2006-06-23 02:05:35 -0700 | [diff] [blame] | 239 | NEW_AUX_ENT(AT_PLATFORM, |
Jesper Juhl | 785d557 | 2006-06-23 02:05:35 -0700 | [diff] [blame] | 240 | (elf_addr_t)(unsigned long)u_platform); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 241 | } |
Nathan Lynch | 483fad1 | 2008-07-22 04:48:46 +1000 | [diff] [blame] | 242 | if (k_base_platform) { |
| 243 | NEW_AUX_ENT(AT_BASE_PLATFORM, |
| 244 | (elf_addr_t)(unsigned long)u_base_platform); |
| 245 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 246 | if (bprm->interp_flags & BINPRM_FLAGS_EXECFD) { |
Jesper Juhl | 785d557 | 2006-06-23 02:05:35 -0700 | [diff] [blame] | 247 | NEW_AUX_ENT(AT_EXECFD, bprm->interp_data); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 248 | } |
| 249 | #undef NEW_AUX_ENT |
| 250 | /* AT_NULL is zero; clear the rest too */ |
| 251 | memset(&elf_info[ei_index], 0, |
| 252 | sizeof current->mm->saved_auxv - ei_index * sizeof elf_info[0]); |
| 253 | |
| 254 | /* And advance past the AT_NULL entry. */ |
| 255 | ei_index += 2; |
| 256 | |
| 257 | sp = STACK_ADD(p, ei_index); |
| 258 | |
Andi Kleen | d20894a | 2008-02-08 04:21:54 -0800 | [diff] [blame] | 259 | items = (argc + 1) + (envc + 1) + 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 260 | bprm->p = STACK_ROUND(sp, items); |
| 261 | |
| 262 | /* Point sp at the lowest address on the stack */ |
| 263 | #ifdef CONFIG_STACK_GROWSUP |
| 264 | sp = (elf_addr_t __user *)bprm->p - items - ei_index; |
Jesper Juhl | f4e5cc2 | 2006-06-23 02:05:35 -0700 | [diff] [blame] | 265 | bprm->exec = (unsigned long)sp; /* XXX: PARISC HACK */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 266 | #else |
| 267 | sp = (elf_addr_t __user *)bprm->p; |
| 268 | #endif |
| 269 | |
Ollie Wild | b6a2fea | 2007-07-19 01:48:16 -0700 | [diff] [blame] | 270 | |
| 271 | /* |
| 272 | * Grow the stack manually; some architectures have a limit on how |
| 273 | * far ahead a user-space access may be in order to grow the stack. |
| 274 | */ |
| 275 | vma = find_extend_vma(current->mm, bprm->p); |
| 276 | if (!vma) |
| 277 | return -EFAULT; |
| 278 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 279 | /* Now, let's put argc (and argv, envp if appropriate) on the stack */ |
| 280 | if (__put_user(argc, sp++)) |
| 281 | return -EFAULT; |
Andi Kleen | d20894a | 2008-02-08 04:21:54 -0800 | [diff] [blame] | 282 | argv = sp; |
| 283 | envp = argv + argc + 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 284 | |
| 285 | /* Populate argv and envp */ |
Greg Kroah-Hartman | a84a505 | 2005-05-11 00:10:44 -0700 | [diff] [blame] | 286 | p = current->mm->arg_end = current->mm->arg_start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 287 | while (argc-- > 0) { |
| 288 | size_t len; |
Heiko Carstens | 841d5fb | 2006-12-06 20:36:35 -0800 | [diff] [blame] | 289 | if (__put_user((elf_addr_t)p, argv++)) |
| 290 | return -EFAULT; |
Ollie Wild | b6a2fea | 2007-07-19 01:48:16 -0700 | [diff] [blame] | 291 | len = strnlen_user((void __user *)p, MAX_ARG_STRLEN); |
| 292 | if (!len || len > MAX_ARG_STRLEN) |
WANG Cong | 23c4971 | 2008-05-08 21:52:33 +0800 | [diff] [blame] | 293 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 294 | p += len; |
| 295 | } |
| 296 | if (__put_user(0, argv)) |
| 297 | return -EFAULT; |
| 298 | current->mm->arg_end = current->mm->env_start = p; |
| 299 | while (envc-- > 0) { |
| 300 | size_t len; |
Heiko Carstens | 841d5fb | 2006-12-06 20:36:35 -0800 | [diff] [blame] | 301 | if (__put_user((elf_addr_t)p, envp++)) |
| 302 | return -EFAULT; |
Ollie Wild | b6a2fea | 2007-07-19 01:48:16 -0700 | [diff] [blame] | 303 | len = strnlen_user((void __user *)p, MAX_ARG_STRLEN); |
| 304 | if (!len || len > MAX_ARG_STRLEN) |
WANG Cong | 23c4971 | 2008-05-08 21:52:33 +0800 | [diff] [blame] | 305 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 306 | p += len; |
| 307 | } |
| 308 | if (__put_user(0, envp)) |
| 309 | return -EFAULT; |
| 310 | current->mm->env_end = p; |
| 311 | |
| 312 | /* Put the elf_info on the stack in the right place. */ |
| 313 | sp = (elf_addr_t __user *)envp + 1; |
| 314 | if (copy_to_user(sp, elf_info, ei_index * sizeof(elf_addr_t))) |
| 315 | return -EFAULT; |
| 316 | return 0; |
| 317 | } |
| 318 | |
| 319 | #ifndef elf_map |
| 320 | |
| 321 | static unsigned long elf_map(struct file *filep, unsigned long addr, |
Jiri Kosina | cc503c1 | 2008-01-30 13:31:07 +0100 | [diff] [blame] | 322 | struct elf_phdr *eppnt, int prot, int type, |
| 323 | unsigned long total_size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 324 | { |
| 325 | unsigned long map_addr; |
Jiri Kosina | cc503c1 | 2008-01-30 13:31:07 +0100 | [diff] [blame] | 326 | unsigned long size = eppnt->p_filesz + ELF_PAGEOFFSET(eppnt->p_vaddr); |
| 327 | unsigned long off = eppnt->p_offset - ELF_PAGEOFFSET(eppnt->p_vaddr); |
| 328 | addr = ELF_PAGESTART(addr); |
| 329 | size = ELF_PAGEALIGN(size); |
Jan Kratochvil | 60bfba7 | 2007-07-15 23:40:06 -0700 | [diff] [blame] | 330 | |
Andrew Morton | d4e3cc3 | 2007-07-21 04:37:32 -0700 | [diff] [blame] | 331 | /* mmap() will return -EINVAL if given a zero size, but a |
| 332 | * segment with zero filesize is perfectly valid */ |
Jiri Kosina | cc503c1 | 2008-01-30 13:31:07 +0100 | [diff] [blame] | 333 | if (!size) |
| 334 | return addr; |
| 335 | |
| 336 | down_write(¤t->mm->mmap_sem); |
| 337 | /* |
| 338 | * total_size is the size of the ELF (interpreter) image. |
| 339 | * The _first_ mmap needs to know the full size, otherwise |
| 340 | * randomization might put this image into an overlapping |
| 341 | * position with the ELF binary image. (since size < total_size) |
| 342 | * So we first map the 'big' image - and unmap the remainder at |
| 343 | * the end. (which unmap is needed for ELF images with holes.) |
| 344 | */ |
| 345 | if (total_size) { |
| 346 | total_size = ELF_PAGEALIGN(total_size); |
| 347 | map_addr = do_mmap(filep, addr, total_size, prot, type, off); |
| 348 | if (!BAD_ADDR(map_addr)) |
| 349 | do_munmap(current->mm, map_addr+size, total_size-size); |
| 350 | } else |
| 351 | map_addr = do_mmap(filep, addr, size, prot, type, off); |
| 352 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 353 | up_write(¤t->mm->mmap_sem); |
| 354 | return(map_addr); |
| 355 | } |
| 356 | |
| 357 | #endif /* !elf_map */ |
| 358 | |
Jiri Kosina | cc503c1 | 2008-01-30 13:31:07 +0100 | [diff] [blame] | 359 | static unsigned long total_mapping_size(struct elf_phdr *cmds, int nr) |
| 360 | { |
| 361 | int i, first_idx = -1, last_idx = -1; |
| 362 | |
| 363 | for (i = 0; i < nr; i++) { |
| 364 | if (cmds[i].p_type == PT_LOAD) { |
| 365 | last_idx = i; |
| 366 | if (first_idx == -1) |
| 367 | first_idx = i; |
| 368 | } |
| 369 | } |
| 370 | if (first_idx == -1) |
| 371 | return 0; |
| 372 | |
| 373 | return cmds[last_idx].p_vaddr + cmds[last_idx].p_memsz - |
| 374 | ELF_PAGESTART(cmds[first_idx].p_vaddr); |
| 375 | } |
| 376 | |
| 377 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 378 | /* This is much more generalized than the library routine read function, |
| 379 | so we keep this separate. Technically the library read function |
| 380 | is only provided so that we can read a.out libraries that have |
| 381 | an ELF header */ |
| 382 | |
Jesper Juhl | f4e5cc2 | 2006-06-23 02:05:35 -0700 | [diff] [blame] | 383 | static unsigned long load_elf_interp(struct elfhdr *interp_elf_ex, |
Jiri Kosina | cc503c1 | 2008-01-30 13:31:07 +0100 | [diff] [blame] | 384 | struct file *interpreter, unsigned long *interp_map_addr, |
| 385 | unsigned long no_base) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 386 | { |
| 387 | struct elf_phdr *elf_phdata; |
| 388 | struct elf_phdr *eppnt; |
| 389 | unsigned long load_addr = 0; |
| 390 | int load_addr_set = 0; |
| 391 | unsigned long last_bss = 0, elf_bss = 0; |
| 392 | unsigned long error = ~0UL; |
Jiri Kosina | cc503c1 | 2008-01-30 13:31:07 +0100 | [diff] [blame] | 393 | unsigned long total_size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 394 | int retval, i, size; |
| 395 | |
| 396 | /* First of all, some simple consistency checks */ |
| 397 | if (interp_elf_ex->e_type != ET_EXEC && |
| 398 | interp_elf_ex->e_type != ET_DYN) |
| 399 | goto out; |
| 400 | if (!elf_check_arch(interp_elf_ex)) |
| 401 | goto out; |
| 402 | if (!interpreter->f_op || !interpreter->f_op->mmap) |
| 403 | goto out; |
| 404 | |
| 405 | /* |
| 406 | * If the size of this structure has changed, then punt, since |
| 407 | * we will be doing the wrong thing. |
| 408 | */ |
| 409 | if (interp_elf_ex->e_phentsize != sizeof(struct elf_phdr)) |
| 410 | goto out; |
| 411 | if (interp_elf_ex->e_phnum < 1 || |
| 412 | interp_elf_ex->e_phnum > 65536U / sizeof(struct elf_phdr)) |
| 413 | goto out; |
| 414 | |
| 415 | /* Now read in all of the header information */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 416 | size = sizeof(struct elf_phdr) * interp_elf_ex->e_phnum; |
| 417 | if (size > ELF_MIN_ALIGN) |
| 418 | goto out; |
Jesper Juhl | f4e5cc2 | 2006-06-23 02:05:35 -0700 | [diff] [blame] | 419 | elf_phdata = kmalloc(size, GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 420 | if (!elf_phdata) |
| 421 | goto out; |
| 422 | |
Jesper Juhl | f4e5cc2 | 2006-06-23 02:05:35 -0700 | [diff] [blame] | 423 | retval = kernel_read(interpreter, interp_elf_ex->e_phoff, |
| 424 | (char *)elf_phdata,size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 425 | error = -EIO; |
| 426 | if (retval != size) { |
| 427 | if (retval < 0) |
| 428 | error = retval; |
| 429 | goto out_close; |
| 430 | } |
| 431 | |
Jiri Kosina | cc503c1 | 2008-01-30 13:31:07 +0100 | [diff] [blame] | 432 | total_size = total_mapping_size(elf_phdata, interp_elf_ex->e_phnum); |
| 433 | if (!total_size) { |
| 434 | error = -EINVAL; |
| 435 | goto out_close; |
| 436 | } |
| 437 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 438 | eppnt = elf_phdata; |
Jesper Juhl | f4e5cc2 | 2006-06-23 02:05:35 -0700 | [diff] [blame] | 439 | for (i = 0; i < interp_elf_ex->e_phnum; i++, eppnt++) { |
| 440 | if (eppnt->p_type == PT_LOAD) { |
| 441 | int elf_type = MAP_PRIVATE | MAP_DENYWRITE; |
| 442 | int elf_prot = 0; |
| 443 | unsigned long vaddr = 0; |
| 444 | unsigned long k, map_addr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 445 | |
Jesper Juhl | f4e5cc2 | 2006-06-23 02:05:35 -0700 | [diff] [blame] | 446 | if (eppnt->p_flags & PF_R) |
| 447 | elf_prot = PROT_READ; |
| 448 | if (eppnt->p_flags & PF_W) |
| 449 | elf_prot |= PROT_WRITE; |
| 450 | if (eppnt->p_flags & PF_X) |
| 451 | elf_prot |= PROT_EXEC; |
| 452 | vaddr = eppnt->p_vaddr; |
| 453 | if (interp_elf_ex->e_type == ET_EXEC || load_addr_set) |
| 454 | elf_type |= MAP_FIXED; |
Jiri Kosina | cc503c1 | 2008-01-30 13:31:07 +0100 | [diff] [blame] | 455 | else if (no_base && interp_elf_ex->e_type == ET_DYN) |
| 456 | load_addr = -vaddr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 457 | |
Jesper Juhl | f4e5cc2 | 2006-06-23 02:05:35 -0700 | [diff] [blame] | 458 | map_addr = elf_map(interpreter, load_addr + vaddr, |
Andrew Morton | bb1ad82 | 2008-01-30 13:31:07 +0100 | [diff] [blame] | 459 | eppnt, elf_prot, elf_type, total_size); |
Jiri Kosina | cc503c1 | 2008-01-30 13:31:07 +0100 | [diff] [blame] | 460 | total_size = 0; |
| 461 | if (!*interp_map_addr) |
| 462 | *interp_map_addr = map_addr; |
Jesper Juhl | f4e5cc2 | 2006-06-23 02:05:35 -0700 | [diff] [blame] | 463 | error = map_addr; |
| 464 | if (BAD_ADDR(map_addr)) |
| 465 | goto out_close; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 466 | |
Jesper Juhl | f4e5cc2 | 2006-06-23 02:05:35 -0700 | [diff] [blame] | 467 | if (!load_addr_set && |
| 468 | interp_elf_ex->e_type == ET_DYN) { |
| 469 | load_addr = map_addr - ELF_PAGESTART(vaddr); |
| 470 | load_addr_set = 1; |
| 471 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 472 | |
Jesper Juhl | f4e5cc2 | 2006-06-23 02:05:35 -0700 | [diff] [blame] | 473 | /* |
| 474 | * Check to see if the section's size will overflow the |
| 475 | * allowed task size. Note that p_filesz must always be |
| 476 | * <= p_memsize so it's only necessary to check p_memsz. |
| 477 | */ |
| 478 | k = load_addr + eppnt->p_vaddr; |
Chuck Ebbert | ce51059 | 2006-07-03 00:24:14 -0700 | [diff] [blame] | 479 | if (BAD_ADDR(k) || |
Jesper Juhl | f4e5cc2 | 2006-06-23 02:05:35 -0700 | [diff] [blame] | 480 | eppnt->p_filesz > eppnt->p_memsz || |
| 481 | eppnt->p_memsz > TASK_SIZE || |
| 482 | TASK_SIZE - eppnt->p_memsz < k) { |
| 483 | error = -ENOMEM; |
| 484 | goto out_close; |
| 485 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 486 | |
Jesper Juhl | f4e5cc2 | 2006-06-23 02:05:35 -0700 | [diff] [blame] | 487 | /* |
| 488 | * Find the end of the file mapping for this phdr, and |
| 489 | * keep track of the largest address we see for this. |
| 490 | */ |
| 491 | k = load_addr + eppnt->p_vaddr + eppnt->p_filesz; |
| 492 | if (k > elf_bss) |
| 493 | elf_bss = k; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 494 | |
Jesper Juhl | f4e5cc2 | 2006-06-23 02:05:35 -0700 | [diff] [blame] | 495 | /* |
| 496 | * Do the same thing for the memory mapping - between |
| 497 | * elf_bss and last_bss is the bss section. |
| 498 | */ |
| 499 | k = load_addr + eppnt->p_memsz + eppnt->p_vaddr; |
| 500 | if (k > last_bss) |
| 501 | last_bss = k; |
| 502 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 503 | } |
| 504 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 505 | if (last_bss > elf_bss) { |
Roland McGrath | 752015d | 2009-09-08 19:49:40 -0700 | [diff] [blame] | 506 | /* |
| 507 | * Now fill out the bss section. First pad the last page up |
| 508 | * to the page boundary, and then perform a mmap to make sure |
| 509 | * that there are zero-mapped pages up to and including the |
| 510 | * last bss page. |
| 511 | */ |
| 512 | if (padzero(elf_bss)) { |
| 513 | error = -EFAULT; |
| 514 | goto out_close; |
| 515 | } |
| 516 | |
| 517 | /* What we have mapped so far */ |
| 518 | elf_bss = ELF_PAGESTART(elf_bss + ELF_MIN_ALIGN - 1); |
| 519 | |
| 520 | /* Map the last of the bss segment */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 521 | down_write(¤t->mm->mmap_sem); |
| 522 | error = do_brk(elf_bss, last_bss - elf_bss); |
| 523 | up_write(¤t->mm->mmap_sem); |
| 524 | if (BAD_ADDR(error)) |
| 525 | goto out_close; |
| 526 | } |
| 527 | |
Jiri Kosina | cc503c1 | 2008-01-30 13:31:07 +0100 | [diff] [blame] | 528 | error = load_addr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 529 | |
| 530 | out_close: |
| 531 | kfree(elf_phdata); |
| 532 | out: |
| 533 | return error; |
| 534 | } |
| 535 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 536 | /* |
| 537 | * These are the functions used to load ELF style executables and shared |
| 538 | * libraries. There is no binary dependent code anywhere else. |
| 539 | */ |
| 540 | |
| 541 | #define INTERPRETER_NONE 0 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 542 | #define INTERPRETER_ELF 2 |
| 543 | |
Andi Kleen | 913bd90 | 2006-03-25 16:29:09 +0100 | [diff] [blame] | 544 | #ifndef STACK_RND_MASK |
James Bottomley | d1cabd6 | 2007-03-16 13:38:35 -0800 | [diff] [blame] | 545 | #define STACK_RND_MASK (0x7ff >> (PAGE_SHIFT - 12)) /* 8MB of VA */ |
Andi Kleen | 913bd90 | 2006-03-25 16:29:09 +0100 | [diff] [blame] | 546 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 547 | |
| 548 | static unsigned long randomize_stack_top(unsigned long stack_top) |
| 549 | { |
| 550 | unsigned int random_variable = 0; |
| 551 | |
Andi Kleen | c16b63e0 | 2006-09-26 10:52:28 +0200 | [diff] [blame] | 552 | if ((current->flags & PF_RANDOMIZE) && |
| 553 | !(current->personality & ADDR_NO_RANDOMIZE)) { |
Andi Kleen | 913bd90 | 2006-03-25 16:29:09 +0100 | [diff] [blame] | 554 | random_variable = get_random_int() & STACK_RND_MASK; |
| 555 | random_variable <<= PAGE_SHIFT; |
| 556 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 557 | #ifdef CONFIG_STACK_GROWSUP |
Andi Kleen | 913bd90 | 2006-03-25 16:29:09 +0100 | [diff] [blame] | 558 | return PAGE_ALIGN(stack_top) + random_variable; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 559 | #else |
Andi Kleen | 913bd90 | 2006-03-25 16:29:09 +0100 | [diff] [blame] | 560 | return PAGE_ALIGN(stack_top) - random_variable; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 561 | #endif |
| 562 | } |
| 563 | |
Jesper Juhl | f4e5cc2 | 2006-06-23 02:05:35 -0700 | [diff] [blame] | 564 | static int load_elf_binary(struct linux_binprm *bprm, struct pt_regs *regs) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 565 | { |
| 566 | struct file *interpreter = NULL; /* to shut gcc up */ |
| 567 | unsigned long load_addr = 0, load_bias = 0; |
| 568 | int load_addr_set = 0; |
| 569 | char * elf_interpreter = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 570 | unsigned long error; |
Jesper Juhl | f4e5cc2 | 2006-06-23 02:05:35 -0700 | [diff] [blame] | 571 | struct elf_phdr *elf_ppnt, *elf_phdata; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 572 | unsigned long elf_bss, elf_brk; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 573 | int retval, i; |
| 574 | unsigned int size; |
Jiri Kosina | cc503c1 | 2008-01-30 13:31:07 +0100 | [diff] [blame] | 575 | unsigned long elf_entry; |
| 576 | unsigned long interp_load_addr = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 577 | unsigned long start_code, end_code, start_data, end_data; |
| 578 | unsigned long reloc_func_desc = 0; |
David Rientjes | 8de61e6 | 2006-12-06 20:40:16 -0800 | [diff] [blame] | 579 | int executable_stack = EXSTACK_DEFAULT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 580 | unsigned long def_flags = 0; |
| 581 | struct { |
| 582 | struct elfhdr elf_ex; |
| 583 | struct elfhdr interp_elf_ex; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 584 | } *loc; |
| 585 | |
| 586 | loc = kmalloc(sizeof(*loc), GFP_KERNEL); |
| 587 | if (!loc) { |
| 588 | retval = -ENOMEM; |
| 589 | goto out_ret; |
| 590 | } |
| 591 | |
| 592 | /* Get the exec-header */ |
Jesper Juhl | f4e5cc2 | 2006-06-23 02:05:35 -0700 | [diff] [blame] | 593 | loc->elf_ex = *((struct elfhdr *)bprm->buf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 594 | |
| 595 | retval = -ENOEXEC; |
| 596 | /* First of all, some simple consistency checks */ |
| 597 | if (memcmp(loc->elf_ex.e_ident, ELFMAG, SELFMAG) != 0) |
| 598 | goto out; |
| 599 | |
| 600 | if (loc->elf_ex.e_type != ET_EXEC && loc->elf_ex.e_type != ET_DYN) |
| 601 | goto out; |
| 602 | if (!elf_check_arch(&loc->elf_ex)) |
| 603 | goto out; |
| 604 | if (!bprm->file->f_op||!bprm->file->f_op->mmap) |
| 605 | goto out; |
| 606 | |
| 607 | /* Now read in all of the header information */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 608 | if (loc->elf_ex.e_phentsize != sizeof(struct elf_phdr)) |
| 609 | goto out; |
| 610 | if (loc->elf_ex.e_phnum < 1 || |
| 611 | loc->elf_ex.e_phnum > 65536U / sizeof(struct elf_phdr)) |
| 612 | goto out; |
| 613 | size = loc->elf_ex.e_phnum * sizeof(struct elf_phdr); |
| 614 | retval = -ENOMEM; |
Jesper Juhl | f4e5cc2 | 2006-06-23 02:05:35 -0700 | [diff] [blame] | 615 | elf_phdata = kmalloc(size, GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 616 | if (!elf_phdata) |
| 617 | goto out; |
| 618 | |
Jesper Juhl | f4e5cc2 | 2006-06-23 02:05:35 -0700 | [diff] [blame] | 619 | retval = kernel_read(bprm->file, loc->elf_ex.e_phoff, |
| 620 | (char *)elf_phdata, size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 621 | if (retval != size) { |
| 622 | if (retval >= 0) |
| 623 | retval = -EIO; |
| 624 | goto out_free_ph; |
| 625 | } |
| 626 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 627 | elf_ppnt = elf_phdata; |
| 628 | elf_bss = 0; |
| 629 | elf_brk = 0; |
| 630 | |
| 631 | start_code = ~0UL; |
| 632 | end_code = 0; |
| 633 | start_data = 0; |
| 634 | end_data = 0; |
| 635 | |
| 636 | for (i = 0; i < loc->elf_ex.e_phnum; i++) { |
| 637 | if (elf_ppnt->p_type == PT_INTERP) { |
| 638 | /* This is the program interpreter used for |
| 639 | * shared libraries - for now assume that this |
| 640 | * is an a.out format binary |
| 641 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 642 | retval = -ENOEXEC; |
| 643 | if (elf_ppnt->p_filesz > PATH_MAX || |
| 644 | elf_ppnt->p_filesz < 2) |
Al Viro | e7b9b55 | 2009-03-29 16:31:16 -0400 | [diff] [blame] | 645 | goto out_free_ph; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 646 | |
| 647 | retval = -ENOMEM; |
Jesper Juhl | 792db3a | 2006-01-09 20:54:45 -0800 | [diff] [blame] | 648 | elf_interpreter = kmalloc(elf_ppnt->p_filesz, |
Jesper Juhl | f4e5cc2 | 2006-06-23 02:05:35 -0700 | [diff] [blame] | 649 | GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 650 | if (!elf_interpreter) |
Al Viro | e7b9b55 | 2009-03-29 16:31:16 -0400 | [diff] [blame] | 651 | goto out_free_ph; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 652 | |
| 653 | retval = kernel_read(bprm->file, elf_ppnt->p_offset, |
Jesper Juhl | f4e5cc2 | 2006-06-23 02:05:35 -0700 | [diff] [blame] | 654 | elf_interpreter, |
| 655 | elf_ppnt->p_filesz); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 656 | if (retval != elf_ppnt->p_filesz) { |
| 657 | if (retval >= 0) |
| 658 | retval = -EIO; |
| 659 | goto out_free_interp; |
| 660 | } |
| 661 | /* make sure path is NULL terminated */ |
| 662 | retval = -ENOEXEC; |
| 663 | if (elf_interpreter[elf_ppnt->p_filesz - 1] != '\0') |
| 664 | goto out_free_interp; |
| 665 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 666 | interpreter = open_exec(elf_interpreter); |
| 667 | retval = PTR_ERR(interpreter); |
| 668 | if (IS_ERR(interpreter)) |
| 669 | goto out_free_interp; |
Alexey Dobriyan | 1fb8449 | 2007-01-26 00:57:16 -0800 | [diff] [blame] | 670 | |
| 671 | /* |
| 672 | * If the binary is not readable then enforce |
| 673 | * mm->dumpable = 0 regardless of the interpreter's |
| 674 | * permissions. |
| 675 | */ |
| 676 | if (file_permission(interpreter, MAY_READ) < 0) |
| 677 | bprm->interp_flags |= BINPRM_FLAGS_ENFORCE_NONDUMP; |
| 678 | |
Jesper Juhl | f4e5cc2 | 2006-06-23 02:05:35 -0700 | [diff] [blame] | 679 | retval = kernel_read(interpreter, 0, bprm->buf, |
| 680 | BINPRM_BUF_SIZE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 681 | if (retval != BINPRM_BUF_SIZE) { |
| 682 | if (retval >= 0) |
| 683 | retval = -EIO; |
| 684 | goto out_free_dentry; |
| 685 | } |
| 686 | |
| 687 | /* Get the exec headers */ |
Jesper Juhl | f4e5cc2 | 2006-06-23 02:05:35 -0700 | [diff] [blame] | 688 | loc->interp_elf_ex = *((struct elfhdr *)bprm->buf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 689 | break; |
| 690 | } |
| 691 | elf_ppnt++; |
| 692 | } |
| 693 | |
| 694 | elf_ppnt = elf_phdata; |
| 695 | for (i = 0; i < loc->elf_ex.e_phnum; i++, elf_ppnt++) |
| 696 | if (elf_ppnt->p_type == PT_GNU_STACK) { |
| 697 | if (elf_ppnt->p_flags & PF_X) |
| 698 | executable_stack = EXSTACK_ENABLE_X; |
| 699 | else |
| 700 | executable_stack = EXSTACK_DISABLE_X; |
| 701 | break; |
| 702 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 703 | |
| 704 | /* Some simple consistency checks for the interpreter */ |
| 705 | if (elf_interpreter) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 706 | retval = -ELIBBAD; |
Andi Kleen | d20894a | 2008-02-08 04:21:54 -0800 | [diff] [blame] | 707 | /* Not an ELF interpreter */ |
| 708 | if (memcmp(loc->interp_elf_ex.e_ident, ELFMAG, SELFMAG) != 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 709 | goto out_free_dentry; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 710 | /* Verify the interpreter has a valid arch */ |
Andi Kleen | d20894a | 2008-02-08 04:21:54 -0800 | [diff] [blame] | 711 | if (!elf_check_arch(&loc->interp_elf_ex)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 712 | goto out_free_dentry; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 713 | } |
| 714 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 715 | /* Flush all traces of the currently running executable */ |
| 716 | retval = flush_old_exec(bprm); |
| 717 | if (retval) |
| 718 | goto out_free_dentry; |
| 719 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 720 | /* OK, This is the point of no return */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 721 | current->flags &= ~PF_FORKNOEXEC; |
| 722 | current->mm->def_flags = def_flags; |
| 723 | |
| 724 | /* Do this immediately, since STACK_TOP as used in setup_arg_pages |
| 725 | may depend on the personality. */ |
Martin Schwidefsky | 0b59268 | 2008-10-16 15:39:57 +0200 | [diff] [blame] | 726 | SET_PERSONALITY(loc->elf_ex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 727 | if (elf_read_implies_exec(loc->elf_ex, executable_stack)) |
| 728 | current->personality |= READ_IMPLIES_EXEC; |
| 729 | |
Jesper Juhl | f4e5cc2 | 2006-06-23 02:05:35 -0700 | [diff] [blame] | 730 | if (!(current->personality & ADDR_NO_RANDOMIZE) && randomize_va_space) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 731 | current->flags |= PF_RANDOMIZE; |
Linus Torvalds | 221af7f | 2010-01-28 22:14:42 -0800 | [diff] [blame] | 732 | |
| 733 | setup_new_exec(bprm); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 734 | |
| 735 | /* Do this so that we can load the interpreter, if need be. We will |
| 736 | change some of these later */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 737 | current->mm->free_area_cache = current->mm->mmap_base; |
Wolfgang Wander | 1363c3c | 2005-06-21 17:14:49 -0700 | [diff] [blame] | 738 | current->mm->cached_hole_size = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 739 | retval = setup_arg_pages(bprm, randomize_stack_top(STACK_TOP), |
| 740 | executable_stack); |
| 741 | if (retval < 0) { |
| 742 | send_sig(SIGKILL, current, 0); |
| 743 | goto out_free_dentry; |
| 744 | } |
| 745 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 746 | current->mm->start_stack = bprm->p; |
| 747 | |
André Goddard Rosa | af901ca | 2009-11-14 13:09:05 -0200 | [diff] [blame] | 748 | /* Now we do a little grungy work by mmapping the ELF image into |
Jiri Kosina | cc503c1 | 2008-01-30 13:31:07 +0100 | [diff] [blame] | 749 | the correct location in memory. */ |
Jesper Juhl | f4e5cc2 | 2006-06-23 02:05:35 -0700 | [diff] [blame] | 750 | for(i = 0, elf_ppnt = elf_phdata; |
| 751 | i < loc->elf_ex.e_phnum; i++, elf_ppnt++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 752 | int elf_prot = 0, elf_flags; |
| 753 | unsigned long k, vaddr; |
| 754 | |
| 755 | if (elf_ppnt->p_type != PT_LOAD) |
| 756 | continue; |
| 757 | |
| 758 | if (unlikely (elf_brk > elf_bss)) { |
| 759 | unsigned long nbyte; |
| 760 | |
| 761 | /* There was a PT_LOAD segment with p_memsz > p_filesz |
| 762 | before this one. Map anonymous pages, if needed, |
| 763 | and clear the area. */ |
| 764 | retval = set_brk (elf_bss + load_bias, |
| 765 | elf_brk + load_bias); |
| 766 | if (retval) { |
| 767 | send_sig(SIGKILL, current, 0); |
| 768 | goto out_free_dentry; |
| 769 | } |
| 770 | nbyte = ELF_PAGEOFFSET(elf_bss); |
| 771 | if (nbyte) { |
| 772 | nbyte = ELF_MIN_ALIGN - nbyte; |
| 773 | if (nbyte > elf_brk - elf_bss) |
| 774 | nbyte = elf_brk - elf_bss; |
| 775 | if (clear_user((void __user *)elf_bss + |
| 776 | load_bias, nbyte)) { |
| 777 | /* |
| 778 | * This bss-zeroing can fail if the ELF |
Jesper Juhl | f4e5cc2 | 2006-06-23 02:05:35 -0700 | [diff] [blame] | 779 | * file specifies odd protections. So |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 780 | * we don't check the return value |
| 781 | */ |
| 782 | } |
| 783 | } |
| 784 | } |
| 785 | |
Jesper Juhl | f4e5cc2 | 2006-06-23 02:05:35 -0700 | [diff] [blame] | 786 | if (elf_ppnt->p_flags & PF_R) |
| 787 | elf_prot |= PROT_READ; |
| 788 | if (elf_ppnt->p_flags & PF_W) |
| 789 | elf_prot |= PROT_WRITE; |
| 790 | if (elf_ppnt->p_flags & PF_X) |
| 791 | elf_prot |= PROT_EXEC; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 792 | |
Jesper Juhl | f4e5cc2 | 2006-06-23 02:05:35 -0700 | [diff] [blame] | 793 | elf_flags = MAP_PRIVATE | MAP_DENYWRITE | MAP_EXECUTABLE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 794 | |
| 795 | vaddr = elf_ppnt->p_vaddr; |
| 796 | if (loc->elf_ex.e_type == ET_EXEC || load_addr_set) { |
| 797 | elf_flags |= MAP_FIXED; |
| 798 | } else if (loc->elf_ex.e_type == ET_DYN) { |
Jesper Juhl | f4e5cc2 | 2006-06-23 02:05:35 -0700 | [diff] [blame] | 799 | /* Try and get dynamic programs out of the way of the |
| 800 | * default mmap base, as well as whatever program they |
| 801 | * might try to exec. This is because the brk will |
| 802 | * follow the loader, and is not movable. */ |
Jiri Kosina | cc503c1 | 2008-01-30 13:31:07 +0100 | [diff] [blame] | 803 | #ifdef CONFIG_X86 |
| 804 | load_bias = 0; |
| 805 | #else |
Linus Torvalds | 90cb28e | 2007-01-06 13:28:21 -0800 | [diff] [blame] | 806 | load_bias = ELF_PAGESTART(ELF_ET_DYN_BASE - vaddr); |
Jiri Kosina | cc503c1 | 2008-01-30 13:31:07 +0100 | [diff] [blame] | 807 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 808 | } |
| 809 | |
Jesper Juhl | f4e5cc2 | 2006-06-23 02:05:35 -0700 | [diff] [blame] | 810 | error = elf_map(bprm->file, load_bias + vaddr, elf_ppnt, |
Andrew Morton | bb1ad82 | 2008-01-30 13:31:07 +0100 | [diff] [blame] | 811 | elf_prot, elf_flags, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 812 | if (BAD_ADDR(error)) { |
| 813 | send_sig(SIGKILL, current, 0); |
Alexey Kuznetsov | b140f251 | 2007-05-08 00:31:57 -0700 | [diff] [blame] | 814 | retval = IS_ERR((void *)error) ? |
| 815 | PTR_ERR((void*)error) : -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 816 | goto out_free_dentry; |
| 817 | } |
| 818 | |
| 819 | if (!load_addr_set) { |
| 820 | load_addr_set = 1; |
| 821 | load_addr = (elf_ppnt->p_vaddr - elf_ppnt->p_offset); |
| 822 | if (loc->elf_ex.e_type == ET_DYN) { |
| 823 | load_bias += error - |
| 824 | ELF_PAGESTART(load_bias + vaddr); |
| 825 | load_addr += load_bias; |
| 826 | reloc_func_desc = load_bias; |
| 827 | } |
| 828 | } |
| 829 | k = elf_ppnt->p_vaddr; |
Jesper Juhl | f4e5cc2 | 2006-06-23 02:05:35 -0700 | [diff] [blame] | 830 | if (k < start_code) |
| 831 | start_code = k; |
| 832 | if (start_data < k) |
| 833 | start_data = k; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 834 | |
| 835 | /* |
| 836 | * Check to see if the section's size will overflow the |
| 837 | * allowed task size. Note that p_filesz must always be |
| 838 | * <= p_memsz so it is only necessary to check p_memsz. |
| 839 | */ |
Chuck Ebbert | ce51059 | 2006-07-03 00:24:14 -0700 | [diff] [blame] | 840 | if (BAD_ADDR(k) || elf_ppnt->p_filesz > elf_ppnt->p_memsz || |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 841 | elf_ppnt->p_memsz > TASK_SIZE || |
| 842 | TASK_SIZE - elf_ppnt->p_memsz < k) { |
Jesper Juhl | f4e5cc2 | 2006-06-23 02:05:35 -0700 | [diff] [blame] | 843 | /* set_brk can never work. Avoid overflows. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 844 | send_sig(SIGKILL, current, 0); |
Alexey Kuznetsov | b140f251 | 2007-05-08 00:31:57 -0700 | [diff] [blame] | 845 | retval = -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 846 | goto out_free_dentry; |
| 847 | } |
| 848 | |
| 849 | k = elf_ppnt->p_vaddr + elf_ppnt->p_filesz; |
| 850 | |
| 851 | if (k > elf_bss) |
| 852 | elf_bss = k; |
| 853 | if ((elf_ppnt->p_flags & PF_X) && end_code < k) |
| 854 | end_code = k; |
| 855 | if (end_data < k) |
| 856 | end_data = k; |
| 857 | k = elf_ppnt->p_vaddr + elf_ppnt->p_memsz; |
| 858 | if (k > elf_brk) |
| 859 | elf_brk = k; |
| 860 | } |
| 861 | |
| 862 | loc->elf_ex.e_entry += load_bias; |
| 863 | elf_bss += load_bias; |
| 864 | elf_brk += load_bias; |
| 865 | start_code += load_bias; |
| 866 | end_code += load_bias; |
| 867 | start_data += load_bias; |
| 868 | end_data += load_bias; |
| 869 | |
| 870 | /* Calling set_brk effectively mmaps the pages that we need |
| 871 | * for the bss and break sections. We must do this before |
| 872 | * mapping in the interpreter, to make sure it doesn't wind |
| 873 | * up getting placed where the bss needs to go. |
| 874 | */ |
| 875 | retval = set_brk(elf_bss, elf_brk); |
| 876 | if (retval) { |
| 877 | send_sig(SIGKILL, current, 0); |
| 878 | goto out_free_dentry; |
| 879 | } |
akpm@osdl.org | 6de5051 | 2005-10-11 08:29:08 -0700 | [diff] [blame] | 880 | if (likely(elf_bss != elf_brk) && unlikely(padzero(elf_bss))) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 881 | send_sig(SIGSEGV, current, 0); |
| 882 | retval = -EFAULT; /* Nobody gets to see this, but.. */ |
| 883 | goto out_free_dentry; |
| 884 | } |
| 885 | |
| 886 | if (elf_interpreter) { |
Andi Kleen | d20894a | 2008-02-08 04:21:54 -0800 | [diff] [blame] | 887 | unsigned long uninitialized_var(interp_map_addr); |
Jiri Kosina | cc503c1 | 2008-01-30 13:31:07 +0100 | [diff] [blame] | 888 | |
Andi Kleen | d20894a | 2008-02-08 04:21:54 -0800 | [diff] [blame] | 889 | elf_entry = load_elf_interp(&loc->interp_elf_ex, |
| 890 | interpreter, |
| 891 | &interp_map_addr, |
| 892 | load_bias); |
| 893 | if (!IS_ERR((void *)elf_entry)) { |
| 894 | /* |
| 895 | * load_elf_interp() returns relocation |
| 896 | * adjustment |
| 897 | */ |
| 898 | interp_load_addr = elf_entry; |
| 899 | elf_entry += loc->interp_elf_ex.e_entry; |
Jiri Kosina | cc503c1 | 2008-01-30 13:31:07 +0100 | [diff] [blame] | 900 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 901 | if (BAD_ADDR(elf_entry)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 902 | force_sig(SIGSEGV, current); |
Chuck Ebbert | ce51059 | 2006-07-03 00:24:14 -0700 | [diff] [blame] | 903 | retval = IS_ERR((void *)elf_entry) ? |
| 904 | (int)elf_entry : -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 905 | goto out_free_dentry; |
| 906 | } |
| 907 | reloc_func_desc = interp_load_addr; |
| 908 | |
| 909 | allow_write_access(interpreter); |
| 910 | fput(interpreter); |
| 911 | kfree(elf_interpreter); |
| 912 | } else { |
| 913 | elf_entry = loc->elf_ex.e_entry; |
Suresh Siddha | 5342fba | 2006-02-26 04:18:28 +0100 | [diff] [blame] | 914 | if (BAD_ADDR(elf_entry)) { |
Chuck Ebbert | ce51059 | 2006-07-03 00:24:14 -0700 | [diff] [blame] | 915 | force_sig(SIGSEGV, current); |
| 916 | retval = -EINVAL; |
Suresh Siddha | 5342fba | 2006-02-26 04:18:28 +0100 | [diff] [blame] | 917 | goto out_free_dentry; |
| 918 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 919 | } |
| 920 | |
| 921 | kfree(elf_phdata); |
| 922 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 923 | set_binfmt(&elf_format); |
| 924 | |
Benjamin Herrenschmidt | 547ee84 | 2005-04-16 15:24:35 -0700 | [diff] [blame] | 925 | #ifdef ARCH_HAS_SETUP_ADDITIONAL_PAGES |
Martin Schwidefsky | fc5243d | 2008-12-25 13:38:35 +0100 | [diff] [blame] | 926 | retval = arch_setup_additional_pages(bprm, !!elf_interpreter); |
Benjamin Herrenschmidt | 547ee84 | 2005-04-16 15:24:35 -0700 | [diff] [blame] | 927 | if (retval < 0) { |
| 928 | send_sig(SIGKILL, current, 0); |
Roland McGrath | 18c8baf | 2005-04-28 15:17:19 -0700 | [diff] [blame] | 929 | goto out; |
Benjamin Herrenschmidt | 547ee84 | 2005-04-16 15:24:35 -0700 | [diff] [blame] | 930 | } |
| 931 | #endif /* ARCH_HAS_SETUP_ADDITIONAL_PAGES */ |
| 932 | |
David Howells | a6f76f2 | 2008-11-14 10:39:24 +1100 | [diff] [blame] | 933 | install_exec_creds(bprm); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 934 | current->flags &= ~PF_FORKNOEXEC; |
Ollie Wild | b6a2fea | 2007-07-19 01:48:16 -0700 | [diff] [blame] | 935 | retval = create_elf_tables(bprm, &loc->elf_ex, |
Jesper Juhl | f4e5cc2 | 2006-06-23 02:05:35 -0700 | [diff] [blame] | 936 | load_addr, interp_load_addr); |
Ollie Wild | b6a2fea | 2007-07-19 01:48:16 -0700 | [diff] [blame] | 937 | if (retval < 0) { |
| 938 | send_sig(SIGKILL, current, 0); |
| 939 | goto out; |
| 940 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 941 | /* N.B. passed_fileno might not be initialized? */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 942 | current->mm->end_code = end_code; |
| 943 | current->mm->start_code = start_code; |
| 944 | current->mm->start_data = start_data; |
| 945 | current->mm->end_data = end_data; |
| 946 | current->mm->start_stack = bprm->p; |
| 947 | |
Jiri Kosina | c1d171a | 2008-01-30 13:30:40 +0100 | [diff] [blame] | 948 | #ifdef arch_randomize_brk |
Ingo Molnar | 32a9323 | 2008-02-06 22:39:44 +0100 | [diff] [blame] | 949 | if ((current->flags & PF_RANDOMIZE) && (randomize_va_space > 1)) |
Jiri Kosina | c1d171a | 2008-01-30 13:30:40 +0100 | [diff] [blame] | 950 | current->mm->brk = current->mm->start_brk = |
| 951 | arch_randomize_brk(current->mm); |
| 952 | #endif |
| 953 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 954 | if (current->personality & MMAP_PAGE_ZERO) { |
| 955 | /* Why this, you ask??? Well SVr4 maps page 0 as read-only, |
| 956 | and some applications "depend" upon this behavior. |
| 957 | Since we do not have the power to recompile these, we |
Jesper Juhl | f4e5cc2 | 2006-06-23 02:05:35 -0700 | [diff] [blame] | 958 | emulate the SVr4 behavior. Sigh. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 959 | down_write(¤t->mm->mmap_sem); |
| 960 | error = do_mmap(NULL, 0, PAGE_SIZE, PROT_READ | PROT_EXEC, |
| 961 | MAP_FIXED | MAP_PRIVATE, 0); |
| 962 | up_write(¤t->mm->mmap_sem); |
| 963 | } |
| 964 | |
| 965 | #ifdef ELF_PLAT_INIT |
| 966 | /* |
| 967 | * The ABI may specify that certain registers be set up in special |
| 968 | * ways (on i386 %edx is the address of a DT_FINI function, for |
| 969 | * example. In addition, it may also specify (eg, PowerPC64 ELF) |
| 970 | * that the e_entry field is the address of the function descriptor |
| 971 | * for the startup routine, rather than the address of the startup |
| 972 | * routine itself. This macro performs whatever initialization to |
| 973 | * the regs structure is required as well as any relocations to the |
| 974 | * function descriptor entries when executing dynamically links apps. |
| 975 | */ |
| 976 | ELF_PLAT_INIT(regs, reloc_func_desc); |
| 977 | #endif |
| 978 | |
| 979 | start_thread(regs, elf_entry, bprm->p); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 980 | retval = 0; |
| 981 | out: |
| 982 | kfree(loc); |
| 983 | out_ret: |
| 984 | return retval; |
| 985 | |
| 986 | /* error cleanup */ |
| 987 | out_free_dentry: |
| 988 | allow_write_access(interpreter); |
| 989 | if (interpreter) |
| 990 | fput(interpreter); |
| 991 | out_free_interp: |
Jesper Juhl | f99d49a | 2005-11-07 01:01:34 -0800 | [diff] [blame] | 992 | kfree(elf_interpreter); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 993 | out_free_ph: |
| 994 | kfree(elf_phdata); |
| 995 | goto out; |
| 996 | } |
| 997 | |
| 998 | /* This is really simpleminded and specialized - we are loading an |
| 999 | a.out library that is given an ELF header. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1000 | static int load_elf_library(struct file *file) |
| 1001 | { |
| 1002 | struct elf_phdr *elf_phdata; |
| 1003 | struct elf_phdr *eppnt; |
| 1004 | unsigned long elf_bss, bss, len; |
| 1005 | int retval, error, i, j; |
| 1006 | struct elfhdr elf_ex; |
| 1007 | |
| 1008 | error = -ENOEXEC; |
Jesper Juhl | f4e5cc2 | 2006-06-23 02:05:35 -0700 | [diff] [blame] | 1009 | retval = kernel_read(file, 0, (char *)&elf_ex, sizeof(elf_ex)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1010 | if (retval != sizeof(elf_ex)) |
| 1011 | goto out; |
| 1012 | |
| 1013 | if (memcmp(elf_ex.e_ident, ELFMAG, SELFMAG) != 0) |
| 1014 | goto out; |
| 1015 | |
| 1016 | /* First of all, some simple consistency checks */ |
| 1017 | if (elf_ex.e_type != ET_EXEC || elf_ex.e_phnum > 2 || |
Jesper Juhl | f4e5cc2 | 2006-06-23 02:05:35 -0700 | [diff] [blame] | 1018 | !elf_check_arch(&elf_ex) || !file->f_op || !file->f_op->mmap) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1019 | goto out; |
| 1020 | |
| 1021 | /* Now read in all of the header information */ |
| 1022 | |
| 1023 | j = sizeof(struct elf_phdr) * elf_ex.e_phnum; |
| 1024 | /* j < ELF_MIN_ALIGN because elf_ex.e_phnum <= 2 */ |
| 1025 | |
| 1026 | error = -ENOMEM; |
| 1027 | elf_phdata = kmalloc(j, GFP_KERNEL); |
| 1028 | if (!elf_phdata) |
| 1029 | goto out; |
| 1030 | |
| 1031 | eppnt = elf_phdata; |
| 1032 | error = -ENOEXEC; |
| 1033 | retval = kernel_read(file, elf_ex.e_phoff, (char *)eppnt, j); |
| 1034 | if (retval != j) |
| 1035 | goto out_free_ph; |
| 1036 | |
| 1037 | for (j = 0, i = 0; i<elf_ex.e_phnum; i++) |
| 1038 | if ((eppnt + i)->p_type == PT_LOAD) |
| 1039 | j++; |
| 1040 | if (j != 1) |
| 1041 | goto out_free_ph; |
| 1042 | |
| 1043 | while (eppnt->p_type != PT_LOAD) |
| 1044 | eppnt++; |
| 1045 | |
| 1046 | /* Now use mmap to map the library into memory. */ |
| 1047 | down_write(¤t->mm->mmap_sem); |
| 1048 | error = do_mmap(file, |
| 1049 | ELF_PAGESTART(eppnt->p_vaddr), |
| 1050 | (eppnt->p_filesz + |
| 1051 | ELF_PAGEOFFSET(eppnt->p_vaddr)), |
| 1052 | PROT_READ | PROT_WRITE | PROT_EXEC, |
| 1053 | MAP_FIXED | MAP_PRIVATE | MAP_DENYWRITE, |
| 1054 | (eppnt->p_offset - |
| 1055 | ELF_PAGEOFFSET(eppnt->p_vaddr))); |
| 1056 | up_write(¤t->mm->mmap_sem); |
| 1057 | if (error != ELF_PAGESTART(eppnt->p_vaddr)) |
| 1058 | goto out_free_ph; |
| 1059 | |
| 1060 | elf_bss = eppnt->p_vaddr + eppnt->p_filesz; |
| 1061 | if (padzero(elf_bss)) { |
| 1062 | error = -EFAULT; |
| 1063 | goto out_free_ph; |
| 1064 | } |
| 1065 | |
Jesper Juhl | f4e5cc2 | 2006-06-23 02:05:35 -0700 | [diff] [blame] | 1066 | len = ELF_PAGESTART(eppnt->p_filesz + eppnt->p_vaddr + |
| 1067 | ELF_MIN_ALIGN - 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1068 | bss = eppnt->p_memsz + eppnt->p_vaddr; |
| 1069 | if (bss > len) { |
| 1070 | down_write(¤t->mm->mmap_sem); |
| 1071 | do_brk(len, bss - len); |
| 1072 | up_write(¤t->mm->mmap_sem); |
| 1073 | } |
| 1074 | error = 0; |
| 1075 | |
| 1076 | out_free_ph: |
| 1077 | kfree(elf_phdata); |
| 1078 | out: |
| 1079 | return error; |
| 1080 | } |
| 1081 | |
Christoph Hellwig | 698ba7b | 2009-12-15 16:47:37 -0800 | [diff] [blame] | 1082 | #ifdef CONFIG_ELF_CORE |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1083 | /* |
| 1084 | * ELF core dumper |
| 1085 | * |
| 1086 | * Modelled on fs/exec.c:aout_core_dump() |
| 1087 | * Jeremy Fitzhardinge <jeremy@sw.oz.au> |
| 1088 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1089 | |
| 1090 | /* |
Roland McGrath | 82df397 | 2007-10-16 23:27:02 -0700 | [diff] [blame] | 1091 | * Decide what to dump of a segment, part, all or none. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1092 | */ |
Roland McGrath | 82df397 | 2007-10-16 23:27:02 -0700 | [diff] [blame] | 1093 | static unsigned long vma_dump_size(struct vm_area_struct *vma, |
| 1094 | unsigned long mm_flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1095 | { |
KOSAKI Motohiro | e575f11 | 2008-10-18 20:27:08 -0700 | [diff] [blame] | 1096 | #define FILTER(type) (mm_flags & (1UL << MMF_DUMP_##type)) |
| 1097 | |
Roland McGrath | e5b97dd | 2007-01-26 00:56:48 -0800 | [diff] [blame] | 1098 | /* The vma can be set up to tell us the answer directly. */ |
| 1099 | if (vma->vm_flags & VM_ALWAYSDUMP) |
Roland McGrath | 82df397 | 2007-10-16 23:27:02 -0700 | [diff] [blame] | 1100 | goto whole; |
Roland McGrath | e5b97dd | 2007-01-26 00:56:48 -0800 | [diff] [blame] | 1101 | |
KOSAKI Motohiro | e575f11 | 2008-10-18 20:27:08 -0700 | [diff] [blame] | 1102 | /* Hugetlb memory check */ |
| 1103 | if (vma->vm_flags & VM_HUGETLB) { |
| 1104 | if ((vma->vm_flags & VM_SHARED) && FILTER(HUGETLB_SHARED)) |
| 1105 | goto whole; |
| 1106 | if (!(vma->vm_flags & VM_SHARED) && FILTER(HUGETLB_PRIVATE)) |
| 1107 | goto whole; |
| 1108 | } |
| 1109 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1110 | /* Do not dump I/O mapped devices or special mappings */ |
| 1111 | if (vma->vm_flags & (VM_IO | VM_RESERVED)) |
| 1112 | return 0; |
| 1113 | |
Kawai, Hidehiro | a1b59e8 | 2007-07-19 01:48:29 -0700 | [diff] [blame] | 1114 | /* By default, dump shared memory if mapped from an anonymous file. */ |
| 1115 | if (vma->vm_flags & VM_SHARED) { |
Roland McGrath | 82df397 | 2007-10-16 23:27:02 -0700 | [diff] [blame] | 1116 | if (vma->vm_file->f_path.dentry->d_inode->i_nlink == 0 ? |
| 1117 | FILTER(ANON_SHARED) : FILTER(MAPPED_SHARED)) |
| 1118 | goto whole; |
| 1119 | return 0; |
Kawai, Hidehiro | a1b59e8 | 2007-07-19 01:48:29 -0700 | [diff] [blame] | 1120 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1121 | |
Roland McGrath | 82df397 | 2007-10-16 23:27:02 -0700 | [diff] [blame] | 1122 | /* Dump segments that have been written to. */ |
| 1123 | if (vma->anon_vma && FILTER(ANON_PRIVATE)) |
| 1124 | goto whole; |
| 1125 | if (vma->vm_file == NULL) |
| 1126 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1127 | |
Roland McGrath | 82df397 | 2007-10-16 23:27:02 -0700 | [diff] [blame] | 1128 | if (FILTER(MAPPED_PRIVATE)) |
| 1129 | goto whole; |
| 1130 | |
| 1131 | /* |
| 1132 | * If this looks like the beginning of a DSO or executable mapping, |
| 1133 | * check for an ELF header. If we find one, dump the first page to |
| 1134 | * aid in determining what was mapped here. |
| 1135 | */ |
Roland McGrath | 92dc07b | 2009-02-06 17:34:07 -0800 | [diff] [blame] | 1136 | if (FILTER(ELF_HEADERS) && |
| 1137 | vma->vm_pgoff == 0 && (vma->vm_flags & VM_READ)) { |
Roland McGrath | 82df397 | 2007-10-16 23:27:02 -0700 | [diff] [blame] | 1138 | u32 __user *header = (u32 __user *) vma->vm_start; |
| 1139 | u32 word; |
Roland McGrath | 92dc07b | 2009-02-06 17:34:07 -0800 | [diff] [blame] | 1140 | mm_segment_t fs = get_fs(); |
Roland McGrath | 82df397 | 2007-10-16 23:27:02 -0700 | [diff] [blame] | 1141 | /* |
| 1142 | * Doing it this way gets the constant folded by GCC. |
| 1143 | */ |
| 1144 | union { |
| 1145 | u32 cmp; |
| 1146 | char elfmag[SELFMAG]; |
| 1147 | } magic; |
| 1148 | BUILD_BUG_ON(SELFMAG != sizeof word); |
| 1149 | magic.elfmag[EI_MAG0] = ELFMAG0; |
| 1150 | magic.elfmag[EI_MAG1] = ELFMAG1; |
| 1151 | magic.elfmag[EI_MAG2] = ELFMAG2; |
| 1152 | magic.elfmag[EI_MAG3] = ELFMAG3; |
Roland McGrath | 92dc07b | 2009-02-06 17:34:07 -0800 | [diff] [blame] | 1153 | /* |
| 1154 | * Switch to the user "segment" for get_user(), |
| 1155 | * then put back what elf_core_dump() had in place. |
| 1156 | */ |
| 1157 | set_fs(USER_DS); |
| 1158 | if (unlikely(get_user(word, header))) |
| 1159 | word = 0; |
| 1160 | set_fs(fs); |
| 1161 | if (word == magic.cmp) |
Roland McGrath | 82df397 | 2007-10-16 23:27:02 -0700 | [diff] [blame] | 1162 | return PAGE_SIZE; |
| 1163 | } |
| 1164 | |
| 1165 | #undef FILTER |
| 1166 | |
| 1167 | return 0; |
| 1168 | |
| 1169 | whole: |
| 1170 | return vma->vm_end - vma->vm_start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1171 | } |
| 1172 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1173 | /* An ELF note in memory */ |
| 1174 | struct memelfnote |
| 1175 | { |
| 1176 | const char *name; |
| 1177 | int type; |
| 1178 | unsigned int datasz; |
| 1179 | void *data; |
| 1180 | }; |
| 1181 | |
| 1182 | static int notesize(struct memelfnote *en) |
| 1183 | { |
| 1184 | int sz; |
| 1185 | |
| 1186 | sz = sizeof(struct elf_note); |
| 1187 | sz += roundup(strlen(en->name) + 1, 4); |
| 1188 | sz += roundup(en->datasz, 4); |
| 1189 | |
| 1190 | return sz; |
| 1191 | } |
| 1192 | |
Andi Kleen | d025c9d | 2006-09-30 23:29:28 -0700 | [diff] [blame] | 1193 | #define DUMP_WRITE(addr, nr, foffset) \ |
| 1194 | do { if (!dump_write(file, (addr), (nr))) return 0; *foffset += (nr); } while(0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1195 | |
Andi Kleen | d025c9d | 2006-09-30 23:29:28 -0700 | [diff] [blame] | 1196 | static int alignfile(struct file *file, loff_t *foffset) |
| 1197 | { |
Petr Vandrovec | a7a0d86 | 2006-10-13 18:42:07 +0200 | [diff] [blame] | 1198 | static const char buf[4] = { 0, }; |
Andi Kleen | d025c9d | 2006-09-30 23:29:28 -0700 | [diff] [blame] | 1199 | DUMP_WRITE(buf, roundup(*foffset, 4) - *foffset, foffset); |
| 1200 | return 1; |
| 1201 | } |
| 1202 | |
| 1203 | static int writenote(struct memelfnote *men, struct file *file, |
| 1204 | loff_t *foffset) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1205 | { |
| 1206 | struct elf_note en; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1207 | en.n_namesz = strlen(men->name) + 1; |
| 1208 | en.n_descsz = men->datasz; |
| 1209 | en.n_type = men->type; |
| 1210 | |
Andi Kleen | d025c9d | 2006-09-30 23:29:28 -0700 | [diff] [blame] | 1211 | DUMP_WRITE(&en, sizeof(en), foffset); |
| 1212 | DUMP_WRITE(men->name, en.n_namesz, foffset); |
| 1213 | if (!alignfile(file, foffset)) |
| 1214 | return 0; |
| 1215 | DUMP_WRITE(men->data, men->datasz, foffset); |
| 1216 | if (!alignfile(file, foffset)) |
| 1217 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1218 | |
| 1219 | return 1; |
| 1220 | } |
| 1221 | #undef DUMP_WRITE |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1222 | |
Roland McGrath | 3aba481 | 2008-01-30 13:31:44 +0100 | [diff] [blame] | 1223 | static void fill_elf_header(struct elfhdr *elf, int segs, |
| 1224 | u16 machine, u32 flags, u8 osabi) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1225 | { |
Cyrill Gorcunov | 6970c8e | 2008-04-29 01:01:18 -0700 | [diff] [blame] | 1226 | memset(elf, 0, sizeof(*elf)); |
| 1227 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1228 | memcpy(elf->e_ident, ELFMAG, SELFMAG); |
| 1229 | elf->e_ident[EI_CLASS] = ELF_CLASS; |
| 1230 | elf->e_ident[EI_DATA] = ELF_DATA; |
| 1231 | elf->e_ident[EI_VERSION] = EV_CURRENT; |
| 1232 | elf->e_ident[EI_OSABI] = ELF_OSABI; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1233 | |
| 1234 | elf->e_type = ET_CORE; |
Roland McGrath | 3aba481 | 2008-01-30 13:31:44 +0100 | [diff] [blame] | 1235 | elf->e_machine = machine; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1236 | elf->e_version = EV_CURRENT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1237 | elf->e_phoff = sizeof(struct elfhdr); |
Roland McGrath | 3aba481 | 2008-01-30 13:31:44 +0100 | [diff] [blame] | 1238 | elf->e_flags = flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1239 | elf->e_ehsize = sizeof(struct elfhdr); |
| 1240 | elf->e_phentsize = sizeof(struct elf_phdr); |
| 1241 | elf->e_phnum = segs; |
Cyrill Gorcunov | 6970c8e | 2008-04-29 01:01:18 -0700 | [diff] [blame] | 1242 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1243 | return; |
| 1244 | } |
| 1245 | |
Andrew Morton | 8d6b5eee | 2006-09-25 23:32:04 -0700 | [diff] [blame] | 1246 | static void fill_elf_note_phdr(struct elf_phdr *phdr, int sz, loff_t offset) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1247 | { |
| 1248 | phdr->p_type = PT_NOTE; |
| 1249 | phdr->p_offset = offset; |
| 1250 | phdr->p_vaddr = 0; |
| 1251 | phdr->p_paddr = 0; |
| 1252 | phdr->p_filesz = sz; |
| 1253 | phdr->p_memsz = 0; |
| 1254 | phdr->p_flags = 0; |
| 1255 | phdr->p_align = 0; |
| 1256 | return; |
| 1257 | } |
| 1258 | |
| 1259 | static void fill_note(struct memelfnote *note, const char *name, int type, |
| 1260 | unsigned int sz, void *data) |
| 1261 | { |
| 1262 | note->name = name; |
| 1263 | note->type = type; |
| 1264 | note->datasz = sz; |
| 1265 | note->data = data; |
| 1266 | return; |
| 1267 | } |
| 1268 | |
| 1269 | /* |
Jesper Juhl | f4e5cc2 | 2006-06-23 02:05:35 -0700 | [diff] [blame] | 1270 | * fill up all the fields in prstatus from the given task struct, except |
| 1271 | * registers which need to be filled up separately. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1272 | */ |
| 1273 | static void fill_prstatus(struct elf_prstatus *prstatus, |
Jesper Juhl | f4e5cc2 | 2006-06-23 02:05:35 -0700 | [diff] [blame] | 1274 | struct task_struct *p, long signr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1275 | { |
| 1276 | prstatus->pr_info.si_signo = prstatus->pr_cursig = signr; |
| 1277 | prstatus->pr_sigpend = p->pending.signal.sig[0]; |
| 1278 | prstatus->pr_sighold = p->blocked.sig[0]; |
Oleg Nesterov | 3b34fc5 | 2009-06-17 16:27:38 -0700 | [diff] [blame] | 1279 | rcu_read_lock(); |
| 1280 | prstatus->pr_ppid = task_pid_vnr(rcu_dereference(p->real_parent)); |
| 1281 | rcu_read_unlock(); |
Pavel Emelyanov | b488893 | 2007-10-18 23:40:14 -0700 | [diff] [blame] | 1282 | prstatus->pr_pid = task_pid_vnr(p); |
Pavel Emelyanov | b488893 | 2007-10-18 23:40:14 -0700 | [diff] [blame] | 1283 | prstatus->pr_pgrp = task_pgrp_vnr(p); |
| 1284 | prstatus->pr_sid = task_session_vnr(p); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1285 | if (thread_group_leader(p)) { |
Frank Mayhar | f06febc | 2008-09-12 09:54:39 -0700 | [diff] [blame] | 1286 | struct task_cputime cputime; |
| 1287 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1288 | /* |
Frank Mayhar | f06febc | 2008-09-12 09:54:39 -0700 | [diff] [blame] | 1289 | * This is the record for the group leader. It shows the |
| 1290 | * group-wide total, not its individual thread total. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1291 | */ |
Frank Mayhar | f06febc | 2008-09-12 09:54:39 -0700 | [diff] [blame] | 1292 | thread_group_cputime(p, &cputime); |
| 1293 | cputime_to_timeval(cputime.utime, &prstatus->pr_utime); |
| 1294 | cputime_to_timeval(cputime.stime, &prstatus->pr_stime); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1295 | } else { |
| 1296 | cputime_to_timeval(p->utime, &prstatus->pr_utime); |
| 1297 | cputime_to_timeval(p->stime, &prstatus->pr_stime); |
| 1298 | } |
| 1299 | cputime_to_timeval(p->signal->cutime, &prstatus->pr_cutime); |
| 1300 | cputime_to_timeval(p->signal->cstime, &prstatus->pr_cstime); |
| 1301 | } |
| 1302 | |
| 1303 | static int fill_psinfo(struct elf_prpsinfo *psinfo, struct task_struct *p, |
| 1304 | struct mm_struct *mm) |
| 1305 | { |
David Howells | c69e8d9 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 1306 | const struct cred *cred; |
Greg Kroah-Hartman | a84a505 | 2005-05-11 00:10:44 -0700 | [diff] [blame] | 1307 | unsigned int i, len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1308 | |
| 1309 | /* first copy the parameters from user space */ |
| 1310 | memset(psinfo, 0, sizeof(struct elf_prpsinfo)); |
| 1311 | |
| 1312 | len = mm->arg_end - mm->arg_start; |
| 1313 | if (len >= ELF_PRARGSZ) |
| 1314 | len = ELF_PRARGSZ-1; |
| 1315 | if (copy_from_user(&psinfo->pr_psargs, |
| 1316 | (const char __user *)mm->arg_start, len)) |
| 1317 | return -EFAULT; |
| 1318 | for(i = 0; i < len; i++) |
| 1319 | if (psinfo->pr_psargs[i] == 0) |
| 1320 | psinfo->pr_psargs[i] = ' '; |
| 1321 | psinfo->pr_psargs[len] = 0; |
| 1322 | |
Oleg Nesterov | 3b34fc5 | 2009-06-17 16:27:38 -0700 | [diff] [blame] | 1323 | rcu_read_lock(); |
| 1324 | psinfo->pr_ppid = task_pid_vnr(rcu_dereference(p->real_parent)); |
| 1325 | rcu_read_unlock(); |
Pavel Emelyanov | b488893 | 2007-10-18 23:40:14 -0700 | [diff] [blame] | 1326 | psinfo->pr_pid = task_pid_vnr(p); |
Pavel Emelyanov | b488893 | 2007-10-18 23:40:14 -0700 | [diff] [blame] | 1327 | psinfo->pr_pgrp = task_pgrp_vnr(p); |
| 1328 | psinfo->pr_sid = task_session_vnr(p); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1329 | |
| 1330 | i = p->state ? ffz(~p->state) + 1 : 0; |
| 1331 | psinfo->pr_state = i; |
Carsten Otte | 5514854 | 2006-03-25 03:08:22 -0800 | [diff] [blame] | 1332 | psinfo->pr_sname = (i > 5) ? '.' : "RSDTZW"[i]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1333 | psinfo->pr_zomb = psinfo->pr_sname == 'Z'; |
| 1334 | psinfo->pr_nice = task_nice(p); |
| 1335 | psinfo->pr_flag = p->flags; |
David Howells | c69e8d9 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 1336 | rcu_read_lock(); |
| 1337 | cred = __task_cred(p); |
| 1338 | SET_UID(psinfo->pr_uid, cred->uid); |
| 1339 | SET_GID(psinfo->pr_gid, cred->gid); |
| 1340 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1341 | strncpy(psinfo->pr_fname, p->comm, sizeof(psinfo->pr_fname)); |
| 1342 | |
| 1343 | return 0; |
| 1344 | } |
| 1345 | |
Roland McGrath | 3aba481 | 2008-01-30 13:31:44 +0100 | [diff] [blame] | 1346 | static void fill_auxv_note(struct memelfnote *note, struct mm_struct *mm) |
| 1347 | { |
| 1348 | elf_addr_t *auxv = (elf_addr_t *) mm->saved_auxv; |
| 1349 | int i = 0; |
| 1350 | do |
| 1351 | i += 2; |
| 1352 | while (auxv[i - 2] != AT_NULL); |
| 1353 | fill_note(note, "CORE", NT_AUXV, i * sizeof(elf_addr_t), auxv); |
| 1354 | } |
| 1355 | |
Roland McGrath | 4206d3a | 2008-01-30 13:31:45 +0100 | [diff] [blame] | 1356 | #ifdef CORE_DUMP_USE_REGSET |
| 1357 | #include <linux/regset.h> |
| 1358 | |
| 1359 | struct elf_thread_core_info { |
| 1360 | struct elf_thread_core_info *next; |
| 1361 | struct task_struct *task; |
| 1362 | struct elf_prstatus prstatus; |
| 1363 | struct memelfnote notes[0]; |
| 1364 | }; |
| 1365 | |
| 1366 | struct elf_note_info { |
| 1367 | struct elf_thread_core_info *thread; |
| 1368 | struct memelfnote psinfo; |
| 1369 | struct memelfnote auxv; |
| 1370 | size_t size; |
| 1371 | int thread_notes; |
| 1372 | }; |
| 1373 | |
Roland McGrath | d31472b | 2008-03-04 14:28:30 -0800 | [diff] [blame] | 1374 | /* |
| 1375 | * When a regset has a writeback hook, we call it on each thread before |
| 1376 | * dumping user memory. On register window machines, this makes sure the |
| 1377 | * user memory backing the register data is up to date before we read it. |
| 1378 | */ |
| 1379 | static void do_thread_regset_writeback(struct task_struct *task, |
| 1380 | const struct user_regset *regset) |
| 1381 | { |
| 1382 | if (regset->writeback) |
| 1383 | regset->writeback(task, regset, 1); |
| 1384 | } |
| 1385 | |
Roland McGrath | 4206d3a | 2008-01-30 13:31:45 +0100 | [diff] [blame] | 1386 | static int fill_thread_core_info(struct elf_thread_core_info *t, |
| 1387 | const struct user_regset_view *view, |
| 1388 | long signr, size_t *total) |
| 1389 | { |
| 1390 | unsigned int i; |
| 1391 | |
| 1392 | /* |
| 1393 | * NT_PRSTATUS is the one special case, because the regset data |
| 1394 | * goes into the pr_reg field inside the note contents, rather |
| 1395 | * than being the whole note contents. We fill the reset in here. |
| 1396 | * We assume that regset 0 is NT_PRSTATUS. |
| 1397 | */ |
| 1398 | fill_prstatus(&t->prstatus, t->task, signr); |
| 1399 | (void) view->regsets[0].get(t->task, &view->regsets[0], |
| 1400 | 0, sizeof(t->prstatus.pr_reg), |
| 1401 | &t->prstatus.pr_reg, NULL); |
| 1402 | |
| 1403 | fill_note(&t->notes[0], "CORE", NT_PRSTATUS, |
| 1404 | sizeof(t->prstatus), &t->prstatus); |
| 1405 | *total += notesize(&t->notes[0]); |
| 1406 | |
Roland McGrath | d31472b | 2008-03-04 14:28:30 -0800 | [diff] [blame] | 1407 | do_thread_regset_writeback(t->task, &view->regsets[0]); |
| 1408 | |
Roland McGrath | 4206d3a | 2008-01-30 13:31:45 +0100 | [diff] [blame] | 1409 | /* |
| 1410 | * Each other regset might generate a note too. For each regset |
| 1411 | * that has no core_note_type or is inactive, we leave t->notes[i] |
| 1412 | * all zero and we'll know to skip writing it later. |
| 1413 | */ |
| 1414 | for (i = 1; i < view->n; ++i) { |
| 1415 | const struct user_regset *regset = &view->regsets[i]; |
Roland McGrath | d31472b | 2008-03-04 14:28:30 -0800 | [diff] [blame] | 1416 | do_thread_regset_writeback(t->task, regset); |
Roland McGrath | 4206d3a | 2008-01-30 13:31:45 +0100 | [diff] [blame] | 1417 | if (regset->core_note_type && |
| 1418 | (!regset->active || regset->active(t->task, regset))) { |
| 1419 | int ret; |
| 1420 | size_t size = regset->n * regset->size; |
| 1421 | void *data = kmalloc(size, GFP_KERNEL); |
| 1422 | if (unlikely(!data)) |
| 1423 | return 0; |
| 1424 | ret = regset->get(t->task, regset, |
| 1425 | 0, size, data, NULL); |
| 1426 | if (unlikely(ret)) |
| 1427 | kfree(data); |
| 1428 | else { |
| 1429 | if (regset->core_note_type != NT_PRFPREG) |
| 1430 | fill_note(&t->notes[i], "LINUX", |
| 1431 | regset->core_note_type, |
| 1432 | size, data); |
| 1433 | else { |
| 1434 | t->prstatus.pr_fpvalid = 1; |
| 1435 | fill_note(&t->notes[i], "CORE", |
| 1436 | NT_PRFPREG, size, data); |
| 1437 | } |
| 1438 | *total += notesize(&t->notes[i]); |
| 1439 | } |
| 1440 | } |
| 1441 | } |
| 1442 | |
| 1443 | return 1; |
| 1444 | } |
| 1445 | |
| 1446 | static int fill_note_info(struct elfhdr *elf, int phdrs, |
| 1447 | struct elf_note_info *info, |
| 1448 | long signr, struct pt_regs *regs) |
| 1449 | { |
| 1450 | struct task_struct *dump_task = current; |
| 1451 | const struct user_regset_view *view = task_user_regset_view(dump_task); |
| 1452 | struct elf_thread_core_info *t; |
| 1453 | struct elf_prpsinfo *psinfo; |
Oleg Nesterov | 8391444 | 2008-07-25 01:47:45 -0700 | [diff] [blame] | 1454 | struct core_thread *ct; |
Roland McGrath | 4206d3a | 2008-01-30 13:31:45 +0100 | [diff] [blame] | 1455 | unsigned int i; |
| 1456 | |
| 1457 | info->size = 0; |
| 1458 | info->thread = NULL; |
| 1459 | |
| 1460 | psinfo = kmalloc(sizeof(*psinfo), GFP_KERNEL); |
Roland McGrath | 4206d3a | 2008-01-30 13:31:45 +0100 | [diff] [blame] | 1461 | if (psinfo == NULL) |
| 1462 | return 0; |
| 1463 | |
Amerigo Wang | e2dbe12 | 2009-07-01 01:06:26 -0400 | [diff] [blame] | 1464 | fill_note(&info->psinfo, "CORE", NT_PRPSINFO, sizeof(*psinfo), psinfo); |
| 1465 | |
Roland McGrath | 4206d3a | 2008-01-30 13:31:45 +0100 | [diff] [blame] | 1466 | /* |
| 1467 | * Figure out how many notes we're going to need for each thread. |
| 1468 | */ |
| 1469 | info->thread_notes = 0; |
| 1470 | for (i = 0; i < view->n; ++i) |
| 1471 | if (view->regsets[i].core_note_type != 0) |
| 1472 | ++info->thread_notes; |
| 1473 | |
| 1474 | /* |
| 1475 | * Sanity check. We rely on regset 0 being in NT_PRSTATUS, |
| 1476 | * since it is our one special case. |
| 1477 | */ |
| 1478 | if (unlikely(info->thread_notes == 0) || |
| 1479 | unlikely(view->regsets[0].core_note_type != NT_PRSTATUS)) { |
| 1480 | WARN_ON(1); |
| 1481 | return 0; |
| 1482 | } |
| 1483 | |
| 1484 | /* |
| 1485 | * Initialize the ELF file header. |
| 1486 | */ |
| 1487 | fill_elf_header(elf, phdrs, |
| 1488 | view->e_machine, view->e_flags, view->ei_osabi); |
| 1489 | |
| 1490 | /* |
| 1491 | * Allocate a structure for each thread. |
| 1492 | */ |
Oleg Nesterov | 8391444 | 2008-07-25 01:47:45 -0700 | [diff] [blame] | 1493 | for (ct = &dump_task->mm->core_state->dumper; ct; ct = ct->next) { |
| 1494 | t = kzalloc(offsetof(struct elf_thread_core_info, |
| 1495 | notes[info->thread_notes]), |
| 1496 | GFP_KERNEL); |
| 1497 | if (unlikely(!t)) |
| 1498 | return 0; |
Oleg Nesterov | 24d5288 | 2008-07-25 01:47:40 -0700 | [diff] [blame] | 1499 | |
Oleg Nesterov | 8391444 | 2008-07-25 01:47:45 -0700 | [diff] [blame] | 1500 | t->task = ct->task; |
| 1501 | if (ct->task == dump_task || !info->thread) { |
| 1502 | t->next = info->thread; |
| 1503 | info->thread = t; |
| 1504 | } else { |
| 1505 | /* |
| 1506 | * Make sure to keep the original task at |
| 1507 | * the head of the list. |
| 1508 | */ |
| 1509 | t->next = info->thread->next; |
| 1510 | info->thread->next = t; |
Roland McGrath | 4206d3a | 2008-01-30 13:31:45 +0100 | [diff] [blame] | 1511 | } |
Oleg Nesterov | 8391444 | 2008-07-25 01:47:45 -0700 | [diff] [blame] | 1512 | } |
Roland McGrath | 4206d3a | 2008-01-30 13:31:45 +0100 | [diff] [blame] | 1513 | |
| 1514 | /* |
| 1515 | * Now fill in each thread's information. |
| 1516 | */ |
| 1517 | for (t = info->thread; t != NULL; t = t->next) |
| 1518 | if (!fill_thread_core_info(t, view, signr, &info->size)) |
| 1519 | return 0; |
| 1520 | |
| 1521 | /* |
| 1522 | * Fill in the two process-wide notes. |
| 1523 | */ |
| 1524 | fill_psinfo(psinfo, dump_task->group_leader, dump_task->mm); |
| 1525 | info->size += notesize(&info->psinfo); |
| 1526 | |
| 1527 | fill_auxv_note(&info->auxv, current->mm); |
| 1528 | info->size += notesize(&info->auxv); |
| 1529 | |
| 1530 | return 1; |
| 1531 | } |
| 1532 | |
| 1533 | static size_t get_note_info_size(struct elf_note_info *info) |
| 1534 | { |
| 1535 | return info->size; |
| 1536 | } |
| 1537 | |
| 1538 | /* |
| 1539 | * Write all the notes for each thread. When writing the first thread, the |
| 1540 | * process-wide notes are interleaved after the first thread-specific note. |
| 1541 | */ |
| 1542 | static int write_note_info(struct elf_note_info *info, |
| 1543 | struct file *file, loff_t *foffset) |
| 1544 | { |
| 1545 | bool first = 1; |
| 1546 | struct elf_thread_core_info *t = info->thread; |
| 1547 | |
| 1548 | do { |
| 1549 | int i; |
| 1550 | |
| 1551 | if (!writenote(&t->notes[0], file, foffset)) |
| 1552 | return 0; |
| 1553 | |
| 1554 | if (first && !writenote(&info->psinfo, file, foffset)) |
| 1555 | return 0; |
| 1556 | if (first && !writenote(&info->auxv, file, foffset)) |
| 1557 | return 0; |
| 1558 | |
| 1559 | for (i = 1; i < info->thread_notes; ++i) |
| 1560 | if (t->notes[i].data && |
| 1561 | !writenote(&t->notes[i], file, foffset)) |
| 1562 | return 0; |
| 1563 | |
| 1564 | first = 0; |
| 1565 | t = t->next; |
| 1566 | } while (t); |
| 1567 | |
| 1568 | return 1; |
| 1569 | } |
| 1570 | |
| 1571 | static void free_note_info(struct elf_note_info *info) |
| 1572 | { |
| 1573 | struct elf_thread_core_info *threads = info->thread; |
| 1574 | while (threads) { |
| 1575 | unsigned int i; |
| 1576 | struct elf_thread_core_info *t = threads; |
| 1577 | threads = t->next; |
| 1578 | WARN_ON(t->notes[0].data && t->notes[0].data != &t->prstatus); |
| 1579 | for (i = 1; i < info->thread_notes; ++i) |
| 1580 | kfree(t->notes[i].data); |
| 1581 | kfree(t); |
| 1582 | } |
| 1583 | kfree(info->psinfo.data); |
| 1584 | } |
| 1585 | |
| 1586 | #else |
| 1587 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1588 | /* Here is the structure in which status of each thread is captured. */ |
| 1589 | struct elf_thread_status |
| 1590 | { |
| 1591 | struct list_head list; |
| 1592 | struct elf_prstatus prstatus; /* NT_PRSTATUS */ |
| 1593 | elf_fpregset_t fpu; /* NT_PRFPREG */ |
| 1594 | struct task_struct *thread; |
| 1595 | #ifdef ELF_CORE_COPY_XFPREGS |
Mark Nelson | 5b20cd8 | 2007-10-16 23:25:39 -0700 | [diff] [blame] | 1596 | elf_fpxregset_t xfpu; /* ELF_CORE_XFPREG_TYPE */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1597 | #endif |
| 1598 | struct memelfnote notes[3]; |
| 1599 | int num_notes; |
| 1600 | }; |
| 1601 | |
| 1602 | /* |
| 1603 | * In order to add the specific thread information for the elf file format, |
Jesper Juhl | f4e5cc2 | 2006-06-23 02:05:35 -0700 | [diff] [blame] | 1604 | * we need to keep a linked list of every threads pr_status and then create |
| 1605 | * a single section for them in the final core file. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1606 | */ |
| 1607 | static int elf_dump_thread_status(long signr, struct elf_thread_status *t) |
| 1608 | { |
| 1609 | int sz = 0; |
| 1610 | struct task_struct *p = t->thread; |
| 1611 | t->num_notes = 0; |
| 1612 | |
| 1613 | fill_prstatus(&t->prstatus, p, signr); |
| 1614 | elf_core_copy_task_regs(p, &t->prstatus.pr_reg); |
| 1615 | |
Jesper Juhl | f4e5cc2 | 2006-06-23 02:05:35 -0700 | [diff] [blame] | 1616 | fill_note(&t->notes[0], "CORE", NT_PRSTATUS, sizeof(t->prstatus), |
| 1617 | &(t->prstatus)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1618 | t->num_notes++; |
| 1619 | sz += notesize(&t->notes[0]); |
| 1620 | |
Jesper Juhl | f4e5cc2 | 2006-06-23 02:05:35 -0700 | [diff] [blame] | 1621 | if ((t->prstatus.pr_fpvalid = elf_core_copy_task_fpregs(p, NULL, |
| 1622 | &t->fpu))) { |
| 1623 | fill_note(&t->notes[1], "CORE", NT_PRFPREG, sizeof(t->fpu), |
| 1624 | &(t->fpu)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1625 | t->num_notes++; |
| 1626 | sz += notesize(&t->notes[1]); |
| 1627 | } |
| 1628 | |
| 1629 | #ifdef ELF_CORE_COPY_XFPREGS |
| 1630 | if (elf_core_copy_task_xfpregs(p, &t->xfpu)) { |
Mark Nelson | 5b20cd8 | 2007-10-16 23:25:39 -0700 | [diff] [blame] | 1631 | fill_note(&t->notes[2], "LINUX", ELF_CORE_XFPREG_TYPE, |
| 1632 | sizeof(t->xfpu), &t->xfpu); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1633 | t->num_notes++; |
| 1634 | sz += notesize(&t->notes[2]); |
| 1635 | } |
| 1636 | #endif |
| 1637 | return sz; |
| 1638 | } |
| 1639 | |
Roland McGrath | 3aba481 | 2008-01-30 13:31:44 +0100 | [diff] [blame] | 1640 | struct elf_note_info { |
| 1641 | struct memelfnote *notes; |
| 1642 | struct elf_prstatus *prstatus; /* NT_PRSTATUS */ |
| 1643 | struct elf_prpsinfo *psinfo; /* NT_PRPSINFO */ |
| 1644 | struct list_head thread_list; |
| 1645 | elf_fpregset_t *fpu; |
| 1646 | #ifdef ELF_CORE_COPY_XFPREGS |
| 1647 | elf_fpxregset_t *xfpu; |
| 1648 | #endif |
| 1649 | int thread_status_size; |
| 1650 | int numnote; |
| 1651 | }; |
| 1652 | |
Amerigo Wang | 0cf062d | 2009-09-23 15:57:05 -0700 | [diff] [blame] | 1653 | static int elf_note_info_init(struct elf_note_info *info) |
Roland McGrath | 3aba481 | 2008-01-30 13:31:44 +0100 | [diff] [blame] | 1654 | { |
Amerigo Wang | 0cf062d | 2009-09-23 15:57:05 -0700 | [diff] [blame] | 1655 | memset(info, 0, sizeof(*info)); |
Roland McGrath | 3aba481 | 2008-01-30 13:31:44 +0100 | [diff] [blame] | 1656 | INIT_LIST_HEAD(&info->thread_list); |
| 1657 | |
Amerigo Wang | 0cf062d | 2009-09-23 15:57:05 -0700 | [diff] [blame] | 1658 | /* Allocate space for six ELF notes */ |
| 1659 | info->notes = kmalloc(6 * sizeof(struct memelfnote), GFP_KERNEL); |
Roland McGrath | 3aba481 | 2008-01-30 13:31:44 +0100 | [diff] [blame] | 1660 | if (!info->notes) |
| 1661 | return 0; |
| 1662 | info->psinfo = kmalloc(sizeof(*info->psinfo), GFP_KERNEL); |
| 1663 | if (!info->psinfo) |
Amerigo Wang | 0cf062d | 2009-09-23 15:57:05 -0700 | [diff] [blame] | 1664 | goto notes_free; |
Roland McGrath | 3aba481 | 2008-01-30 13:31:44 +0100 | [diff] [blame] | 1665 | info->prstatus = kmalloc(sizeof(*info->prstatus), GFP_KERNEL); |
| 1666 | if (!info->prstatus) |
Amerigo Wang | 0cf062d | 2009-09-23 15:57:05 -0700 | [diff] [blame] | 1667 | goto psinfo_free; |
Roland McGrath | 3aba481 | 2008-01-30 13:31:44 +0100 | [diff] [blame] | 1668 | info->fpu = kmalloc(sizeof(*info->fpu), GFP_KERNEL); |
| 1669 | if (!info->fpu) |
Amerigo Wang | 0cf062d | 2009-09-23 15:57:05 -0700 | [diff] [blame] | 1670 | goto prstatus_free; |
Roland McGrath | 3aba481 | 2008-01-30 13:31:44 +0100 | [diff] [blame] | 1671 | #ifdef ELF_CORE_COPY_XFPREGS |
| 1672 | info->xfpu = kmalloc(sizeof(*info->xfpu), GFP_KERNEL); |
| 1673 | if (!info->xfpu) |
Amerigo Wang | 0cf062d | 2009-09-23 15:57:05 -0700 | [diff] [blame] | 1674 | goto fpu_free; |
Roland McGrath | 3aba481 | 2008-01-30 13:31:44 +0100 | [diff] [blame] | 1675 | #endif |
Amerigo Wang | 0cf062d | 2009-09-23 15:57:05 -0700 | [diff] [blame] | 1676 | return 1; |
| 1677 | #ifdef ELF_CORE_COPY_XFPREGS |
| 1678 | fpu_free: |
| 1679 | kfree(info->fpu); |
| 1680 | #endif |
| 1681 | prstatus_free: |
| 1682 | kfree(info->prstatus); |
| 1683 | psinfo_free: |
| 1684 | kfree(info->psinfo); |
| 1685 | notes_free: |
| 1686 | kfree(info->notes); |
| 1687 | return 0; |
| 1688 | } |
Roland McGrath | 3aba481 | 2008-01-30 13:31:44 +0100 | [diff] [blame] | 1689 | |
Amerigo Wang | 0cf062d | 2009-09-23 15:57:05 -0700 | [diff] [blame] | 1690 | static int fill_note_info(struct elfhdr *elf, int phdrs, |
| 1691 | struct elf_note_info *info, |
| 1692 | long signr, struct pt_regs *regs) |
| 1693 | { |
| 1694 | struct list_head *t; |
| 1695 | |
| 1696 | if (!elf_note_info_init(info)) |
| 1697 | return 0; |
| 1698 | |
Roland McGrath | 3aba481 | 2008-01-30 13:31:44 +0100 | [diff] [blame] | 1699 | if (signr) { |
Oleg Nesterov | 8391444 | 2008-07-25 01:47:45 -0700 | [diff] [blame] | 1700 | struct core_thread *ct; |
WANG Cong | 4220b7f | 2008-04-29 01:01:18 -0700 | [diff] [blame] | 1701 | struct elf_thread_status *ets; |
Oleg Nesterov | 24d5288 | 2008-07-25 01:47:40 -0700 | [diff] [blame] | 1702 | |
Oleg Nesterov | 8391444 | 2008-07-25 01:47:45 -0700 | [diff] [blame] | 1703 | for (ct = current->mm->core_state->dumper.next; |
| 1704 | ct; ct = ct->next) { |
| 1705 | ets = kzalloc(sizeof(*ets), GFP_KERNEL); |
| 1706 | if (!ets) |
| 1707 | return 0; |
| 1708 | |
| 1709 | ets->thread = ct->task; |
| 1710 | list_add(&ets->list, &info->thread_list); |
| 1711 | } |
| 1712 | |
Roland McGrath | 3aba481 | 2008-01-30 13:31:44 +0100 | [diff] [blame] | 1713 | list_for_each(t, &info->thread_list) { |
Roland McGrath | 3aba481 | 2008-01-30 13:31:44 +0100 | [diff] [blame] | 1714 | int sz; |
| 1715 | |
WANG Cong | 4220b7f | 2008-04-29 01:01:18 -0700 | [diff] [blame] | 1716 | ets = list_entry(t, struct elf_thread_status, list); |
| 1717 | sz = elf_dump_thread_status(signr, ets); |
Roland McGrath | 3aba481 | 2008-01-30 13:31:44 +0100 | [diff] [blame] | 1718 | info->thread_status_size += sz; |
| 1719 | } |
| 1720 | } |
| 1721 | /* now collect the dump for the current */ |
| 1722 | memset(info->prstatus, 0, sizeof(*info->prstatus)); |
| 1723 | fill_prstatus(info->prstatus, current, signr); |
| 1724 | elf_core_copy_regs(&info->prstatus->pr_reg, regs); |
| 1725 | |
| 1726 | /* Set up header */ |
| 1727 | fill_elf_header(elf, phdrs, ELF_ARCH, ELF_CORE_EFLAGS, ELF_OSABI); |
| 1728 | |
| 1729 | /* |
| 1730 | * Set up the notes in similar form to SVR4 core dumps made |
| 1731 | * with info from their /proc. |
| 1732 | */ |
| 1733 | |
| 1734 | fill_note(info->notes + 0, "CORE", NT_PRSTATUS, |
| 1735 | sizeof(*info->prstatus), info->prstatus); |
| 1736 | fill_psinfo(info->psinfo, current->group_leader, current->mm); |
| 1737 | fill_note(info->notes + 1, "CORE", NT_PRPSINFO, |
| 1738 | sizeof(*info->psinfo), info->psinfo); |
| 1739 | |
| 1740 | info->numnote = 2; |
| 1741 | |
| 1742 | fill_auxv_note(&info->notes[info->numnote++], current->mm); |
| 1743 | |
| 1744 | /* Try to dump the FPU. */ |
| 1745 | info->prstatus->pr_fpvalid = elf_core_copy_task_fpregs(current, regs, |
| 1746 | info->fpu); |
| 1747 | if (info->prstatus->pr_fpvalid) |
| 1748 | fill_note(info->notes + info->numnote++, |
| 1749 | "CORE", NT_PRFPREG, sizeof(*info->fpu), info->fpu); |
| 1750 | #ifdef ELF_CORE_COPY_XFPREGS |
| 1751 | if (elf_core_copy_task_xfpregs(current, info->xfpu)) |
| 1752 | fill_note(info->notes + info->numnote++, |
| 1753 | "LINUX", ELF_CORE_XFPREG_TYPE, |
| 1754 | sizeof(*info->xfpu), info->xfpu); |
| 1755 | #endif |
| 1756 | |
| 1757 | return 1; |
Roland McGrath | 3aba481 | 2008-01-30 13:31:44 +0100 | [diff] [blame] | 1758 | } |
| 1759 | |
| 1760 | static size_t get_note_info_size(struct elf_note_info *info) |
| 1761 | { |
| 1762 | int sz = 0; |
| 1763 | int i; |
| 1764 | |
| 1765 | for (i = 0; i < info->numnote; i++) |
| 1766 | sz += notesize(info->notes + i); |
| 1767 | |
| 1768 | sz += info->thread_status_size; |
| 1769 | |
| 1770 | return sz; |
| 1771 | } |
| 1772 | |
| 1773 | static int write_note_info(struct elf_note_info *info, |
| 1774 | struct file *file, loff_t *foffset) |
| 1775 | { |
| 1776 | int i; |
| 1777 | struct list_head *t; |
| 1778 | |
| 1779 | for (i = 0; i < info->numnote; i++) |
| 1780 | if (!writenote(info->notes + i, file, foffset)) |
| 1781 | return 0; |
| 1782 | |
| 1783 | /* write out the thread status notes section */ |
| 1784 | list_for_each(t, &info->thread_list) { |
| 1785 | struct elf_thread_status *tmp = |
| 1786 | list_entry(t, struct elf_thread_status, list); |
| 1787 | |
| 1788 | for (i = 0; i < tmp->num_notes; i++) |
| 1789 | if (!writenote(&tmp->notes[i], file, foffset)) |
| 1790 | return 0; |
| 1791 | } |
| 1792 | |
| 1793 | return 1; |
| 1794 | } |
| 1795 | |
| 1796 | static void free_note_info(struct elf_note_info *info) |
| 1797 | { |
| 1798 | while (!list_empty(&info->thread_list)) { |
| 1799 | struct list_head *tmp = info->thread_list.next; |
| 1800 | list_del(tmp); |
| 1801 | kfree(list_entry(tmp, struct elf_thread_status, list)); |
| 1802 | } |
| 1803 | |
| 1804 | kfree(info->prstatus); |
| 1805 | kfree(info->psinfo); |
| 1806 | kfree(info->notes); |
| 1807 | kfree(info->fpu); |
| 1808 | #ifdef ELF_CORE_COPY_XFPREGS |
| 1809 | kfree(info->xfpu); |
| 1810 | #endif |
| 1811 | } |
| 1812 | |
Roland McGrath | 4206d3a | 2008-01-30 13:31:45 +0100 | [diff] [blame] | 1813 | #endif |
| 1814 | |
Roland McGrath | f47aef5 | 2007-01-26 00:56:49 -0800 | [diff] [blame] | 1815 | static struct vm_area_struct *first_vma(struct task_struct *tsk, |
| 1816 | struct vm_area_struct *gate_vma) |
| 1817 | { |
| 1818 | struct vm_area_struct *ret = tsk->mm->mmap; |
| 1819 | |
| 1820 | if (ret) |
| 1821 | return ret; |
| 1822 | return gate_vma; |
| 1823 | } |
| 1824 | /* |
| 1825 | * Helper function for iterating across a vma list. It ensures that the caller |
| 1826 | * will visit `gate_vma' prior to terminating the search. |
| 1827 | */ |
| 1828 | static struct vm_area_struct *next_vma(struct vm_area_struct *this_vma, |
| 1829 | struct vm_area_struct *gate_vma) |
| 1830 | { |
| 1831 | struct vm_area_struct *ret; |
| 1832 | |
| 1833 | ret = this_vma->vm_next; |
| 1834 | if (ret) |
| 1835 | return ret; |
| 1836 | if (this_vma == gate_vma) |
| 1837 | return NULL; |
| 1838 | return gate_vma; |
| 1839 | } |
| 1840 | |
Daisuke HATAYAMA | 8d9032b | 2010-03-05 13:44:10 -0800 | [diff] [blame] | 1841 | static void fill_extnum_info(struct elfhdr *elf, struct elf_shdr *shdr4extnum, |
| 1842 | elf_addr_t e_shoff, int segs) |
| 1843 | { |
| 1844 | elf->e_shoff = e_shoff; |
| 1845 | elf->e_shentsize = sizeof(*shdr4extnum); |
| 1846 | elf->e_shnum = 1; |
| 1847 | elf->e_shstrndx = SHN_UNDEF; |
| 1848 | |
| 1849 | memset(shdr4extnum, 0, sizeof(*shdr4extnum)); |
| 1850 | |
| 1851 | shdr4extnum->sh_type = SHT_NULL; |
| 1852 | shdr4extnum->sh_size = elf->e_shnum; |
| 1853 | shdr4extnum->sh_link = elf->e_shstrndx; |
| 1854 | shdr4extnum->sh_info = segs; |
| 1855 | } |
| 1856 | |
| 1857 | static size_t elf_core_vma_data_size(struct vm_area_struct *gate_vma, |
| 1858 | unsigned long mm_flags) |
| 1859 | { |
| 1860 | struct vm_area_struct *vma; |
| 1861 | size_t size = 0; |
| 1862 | |
| 1863 | for (vma = first_vma(current, gate_vma); vma != NULL; |
| 1864 | vma = next_vma(vma, gate_vma)) |
| 1865 | size += vma_dump_size(vma, mm_flags); |
| 1866 | return size; |
| 1867 | } |
| 1868 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1869 | /* |
| 1870 | * Actual dumper |
| 1871 | * |
| 1872 | * This is a two-pass process; first we find the offsets of the bits, |
| 1873 | * and then they are actually written out. If we run out of core limit |
| 1874 | * we just truncate. |
| 1875 | */ |
Masami Hiramatsu | f6151df | 2009-12-17 15:27:16 -0800 | [diff] [blame] | 1876 | static int elf_core_dump(struct coredump_params *cprm) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1877 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1878 | int has_dumped = 0; |
| 1879 | mm_segment_t fs; |
| 1880 | int segs; |
| 1881 | size_t size = 0; |
Roland McGrath | f47aef5 | 2007-01-26 00:56:49 -0800 | [diff] [blame] | 1882 | struct vm_area_struct *vma, *gate_vma; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1883 | struct elfhdr *elf = NULL; |
Andi Kleen | d025c9d | 2006-09-30 23:29:28 -0700 | [diff] [blame] | 1884 | loff_t offset = 0, dataoff, foffset; |
Roland McGrath | 3aba481 | 2008-01-30 13:31:44 +0100 | [diff] [blame] | 1885 | struct elf_note_info info; |
Daisuke HATAYAMA | 93eb211 | 2010-03-05 13:44:09 -0800 | [diff] [blame] | 1886 | struct elf_phdr *phdr4note = NULL; |
Daisuke HATAYAMA | 8d9032b | 2010-03-05 13:44:10 -0800 | [diff] [blame] | 1887 | struct elf_shdr *shdr4extnum = NULL; |
| 1888 | Elf_Half e_phnum; |
| 1889 | elf_addr_t e_shoff; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1890 | |
| 1891 | /* |
| 1892 | * We no longer stop all VM operations. |
| 1893 | * |
Jesper Juhl | f4e5cc2 | 2006-06-23 02:05:35 -0700 | [diff] [blame] | 1894 | * This is because those proceses that could possibly change map_count |
| 1895 | * or the mmap / vma pages are now blocked in do_exit on current |
| 1896 | * finishing this core dump. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1897 | * |
| 1898 | * Only ptrace can touch these memory addresses, but it doesn't change |
Jesper Juhl | f4e5cc2 | 2006-06-23 02:05:35 -0700 | [diff] [blame] | 1899 | * the map_count or the pages allocated. So no possibility of crashing |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1900 | * exists while dumping the mm->vm_next areas to the core file. |
| 1901 | */ |
| 1902 | |
| 1903 | /* alloc memory for large data structures: too large to be on stack */ |
| 1904 | elf = kmalloc(sizeof(*elf), GFP_KERNEL); |
| 1905 | if (!elf) |
WANG Cong | 5f71955 | 2008-05-06 12:45:35 +0800 | [diff] [blame] | 1906 | goto out; |
KAMEZAWA Hiroyuki | 341c87b | 2009-06-30 11:41:23 -0700 | [diff] [blame] | 1907 | /* |
| 1908 | * The number of segs are recored into ELF header as 16bit value. |
| 1909 | * Please check DEFAULT_MAX_MAP_COUNT definition when you modify here. |
| 1910 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1911 | segs = current->mm->map_count; |
Daisuke HATAYAMA | 1fcccba | 2010-03-05 13:44:07 -0800 | [diff] [blame] | 1912 | segs += elf_core_extra_phdrs(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1913 | |
Roland McGrath | f47aef5 | 2007-01-26 00:56:49 -0800 | [diff] [blame] | 1914 | gate_vma = get_gate_vma(current); |
| 1915 | if (gate_vma != NULL) |
| 1916 | segs++; |
| 1917 | |
Daisuke HATAYAMA | 8d9032b | 2010-03-05 13:44:10 -0800 | [diff] [blame] | 1918 | /* for notes section */ |
| 1919 | segs++; |
| 1920 | |
| 1921 | /* If segs > PN_XNUM(0xffff), then e_phnum overflows. To avoid |
| 1922 | * this, kernel supports extended numbering. Have a look at |
| 1923 | * include/linux/elf.h for further information. */ |
| 1924 | e_phnum = segs > PN_XNUM ? PN_XNUM : segs; |
| 1925 | |
Roland McGrath | 3aba481 | 2008-01-30 13:31:44 +0100 | [diff] [blame] | 1926 | /* |
| 1927 | * Collect all the non-memory information about the process for the |
| 1928 | * notes. This also sets up the file header. |
| 1929 | */ |
Daisuke HATAYAMA | 8d9032b | 2010-03-05 13:44:10 -0800 | [diff] [blame] | 1930 | if (!fill_note_info(elf, e_phnum, &info, cprm->signr, cprm->regs)) |
Roland McGrath | 3aba481 | 2008-01-30 13:31:44 +0100 | [diff] [blame] | 1931 | goto cleanup; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1932 | |
| 1933 | has_dumped = 1; |
| 1934 | current->flags |= PF_DUMPCORE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1935 | |
| 1936 | fs = get_fs(); |
| 1937 | set_fs(KERNEL_DS); |
| 1938 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1939 | offset += sizeof(*elf); /* Elf header */ |
Daisuke HATAYAMA | 8d9032b | 2010-03-05 13:44:10 -0800 | [diff] [blame] | 1940 | offset += segs * sizeof(struct elf_phdr); /* Program headers */ |
Petr Vandrovec | a7a0d86 | 2006-10-13 18:42:07 +0200 | [diff] [blame] | 1941 | foffset = offset; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1942 | |
| 1943 | /* Write notes phdr entry */ |
| 1944 | { |
Roland McGrath | 3aba481 | 2008-01-30 13:31:44 +0100 | [diff] [blame] | 1945 | size_t sz = get_note_info_size(&info); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1946 | |
Michael Ellerman | e550149 | 2007-09-19 14:38:12 +1000 | [diff] [blame] | 1947 | sz += elf_coredump_extra_notes_size(); |
Dwayne Grant McConnell | bf1ab97 | 2006-11-23 00:46:37 +0100 | [diff] [blame] | 1948 | |
Daisuke HATAYAMA | 93eb211 | 2010-03-05 13:44:09 -0800 | [diff] [blame] | 1949 | phdr4note = kmalloc(sizeof(*phdr4note), GFP_KERNEL); |
| 1950 | if (!phdr4note) |
Daisuke HATAYAMA | 088e7af | 2010-03-05 13:44:06 -0800 | [diff] [blame] | 1951 | goto end_coredump; |
Daisuke HATAYAMA | 93eb211 | 2010-03-05 13:44:09 -0800 | [diff] [blame] | 1952 | |
| 1953 | fill_elf_note_phdr(phdr4note, sz, offset); |
| 1954 | offset += sz; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1955 | } |
| 1956 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1957 | dataoff = offset = roundup(offset, ELF_EXEC_PAGESIZE); |
| 1958 | |
Masami Hiramatsu | 30736a4 | 2010-03-05 13:44:12 -0800 | [diff] [blame] | 1959 | offset += elf_core_vma_data_size(gate_vma, cprm->mm_flags); |
Daisuke HATAYAMA | 8d9032b | 2010-03-05 13:44:10 -0800 | [diff] [blame] | 1960 | offset += elf_core_extra_data_size(); |
| 1961 | e_shoff = offset; |
| 1962 | |
| 1963 | if (e_phnum == PN_XNUM) { |
| 1964 | shdr4extnum = kmalloc(sizeof(*shdr4extnum), GFP_KERNEL); |
| 1965 | if (!shdr4extnum) |
| 1966 | goto end_coredump; |
| 1967 | fill_extnum_info(elf, shdr4extnum, e_shoff, segs); |
| 1968 | } |
| 1969 | |
| 1970 | offset = dataoff; |
| 1971 | |
Daisuke HATAYAMA | 93eb211 | 2010-03-05 13:44:09 -0800 | [diff] [blame] | 1972 | size += sizeof(*elf); |
| 1973 | if (size > cprm->limit || !dump_write(cprm->file, elf, sizeof(*elf))) |
| 1974 | goto end_coredump; |
| 1975 | |
| 1976 | size += sizeof(*phdr4note); |
| 1977 | if (size > cprm->limit |
| 1978 | || !dump_write(cprm->file, phdr4note, sizeof(*phdr4note))) |
| 1979 | goto end_coredump; |
| 1980 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1981 | /* Write program headers for segments dump */ |
Roland McGrath | f47aef5 | 2007-01-26 00:56:49 -0800 | [diff] [blame] | 1982 | for (vma = first_vma(current, gate_vma); vma != NULL; |
| 1983 | vma = next_vma(vma, gate_vma)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1984 | struct elf_phdr phdr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1985 | |
| 1986 | phdr.p_type = PT_LOAD; |
| 1987 | phdr.p_offset = offset; |
| 1988 | phdr.p_vaddr = vma->vm_start; |
| 1989 | phdr.p_paddr = 0; |
Masami Hiramatsu | 30736a4 | 2010-03-05 13:44:12 -0800 | [diff] [blame] | 1990 | phdr.p_filesz = vma_dump_size(vma, cprm->mm_flags); |
Roland McGrath | 82df397 | 2007-10-16 23:27:02 -0700 | [diff] [blame] | 1991 | phdr.p_memsz = vma->vm_end - vma->vm_start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1992 | offset += phdr.p_filesz; |
| 1993 | phdr.p_flags = vma->vm_flags & VM_READ ? PF_R : 0; |
Jesper Juhl | f4e5cc2 | 2006-06-23 02:05:35 -0700 | [diff] [blame] | 1994 | if (vma->vm_flags & VM_WRITE) |
| 1995 | phdr.p_flags |= PF_W; |
| 1996 | if (vma->vm_flags & VM_EXEC) |
| 1997 | phdr.p_flags |= PF_X; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1998 | phdr.p_align = ELF_EXEC_PAGESIZE; |
| 1999 | |
Daisuke HATAYAMA | 088e7af | 2010-03-05 13:44:06 -0800 | [diff] [blame] | 2000 | size += sizeof(phdr); |
| 2001 | if (size > cprm->limit |
| 2002 | || !dump_write(cprm->file, &phdr, sizeof(phdr))) |
| 2003 | goto end_coredump; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2004 | } |
| 2005 | |
Daisuke HATAYAMA | 1fcccba | 2010-03-05 13:44:07 -0800 | [diff] [blame] | 2006 | if (!elf_core_write_extra_phdrs(cprm->file, offset, &size, cprm->limit)) |
| 2007 | goto end_coredump; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2008 | |
| 2009 | /* write out the notes section */ |
Masami Hiramatsu | f6151df | 2009-12-17 15:27:16 -0800 | [diff] [blame] | 2010 | if (!write_note_info(&info, cprm->file, &foffset)) |
Roland McGrath | 3aba481 | 2008-01-30 13:31:44 +0100 | [diff] [blame] | 2011 | goto end_coredump; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2012 | |
Masami Hiramatsu | f6151df | 2009-12-17 15:27:16 -0800 | [diff] [blame] | 2013 | if (elf_coredump_extra_notes_write(cprm->file, &foffset)) |
Michael Ellerman | e550149 | 2007-09-19 14:38:12 +1000 | [diff] [blame] | 2014 | goto end_coredump; |
Dwayne Grant McConnell | bf1ab97 | 2006-11-23 00:46:37 +0100 | [diff] [blame] | 2015 | |
Andi Kleen | d025c9d | 2006-09-30 23:29:28 -0700 | [diff] [blame] | 2016 | /* Align to page */ |
Masami Hiramatsu | f6151df | 2009-12-17 15:27:16 -0800 | [diff] [blame] | 2017 | if (!dump_seek(cprm->file, dataoff - foffset)) |
Hugh Dickins | f3e8fcc | 2009-09-21 17:03:25 -0700 | [diff] [blame] | 2018 | goto end_coredump; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2019 | |
Roland McGrath | f47aef5 | 2007-01-26 00:56:49 -0800 | [diff] [blame] | 2020 | for (vma = first_vma(current, gate_vma); vma != NULL; |
| 2021 | vma = next_vma(vma, gate_vma)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2022 | unsigned long addr; |
Roland McGrath | 82df397 | 2007-10-16 23:27:02 -0700 | [diff] [blame] | 2023 | unsigned long end; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2024 | |
Masami Hiramatsu | 30736a4 | 2010-03-05 13:44:12 -0800 | [diff] [blame] | 2025 | end = vma->vm_start + vma_dump_size(vma, cprm->mm_flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2026 | |
Roland McGrath | 82df397 | 2007-10-16 23:27:02 -0700 | [diff] [blame] | 2027 | for (addr = vma->vm_start; addr < end; addr += PAGE_SIZE) { |
Jesper Juhl | f4e5cc2 | 2006-06-23 02:05:35 -0700 | [diff] [blame] | 2028 | struct page *page; |
Hugh Dickins | f3e8fcc | 2009-09-21 17:03:25 -0700 | [diff] [blame] | 2029 | int stop; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2030 | |
Hugh Dickins | f3e8fcc | 2009-09-21 17:03:25 -0700 | [diff] [blame] | 2031 | page = get_dump_page(addr); |
| 2032 | if (page) { |
| 2033 | void *kaddr = kmap(page); |
Masami Hiramatsu | f6151df | 2009-12-17 15:27:16 -0800 | [diff] [blame] | 2034 | stop = ((size += PAGE_SIZE) > cprm->limit) || |
| 2035 | !dump_write(cprm->file, kaddr, |
| 2036 | PAGE_SIZE); |
Hugh Dickins | f3e8fcc | 2009-09-21 17:03:25 -0700 | [diff] [blame] | 2037 | kunmap(page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2038 | page_cache_release(page); |
Hugh Dickins | f3e8fcc | 2009-09-21 17:03:25 -0700 | [diff] [blame] | 2039 | } else |
Masami Hiramatsu | f6151df | 2009-12-17 15:27:16 -0800 | [diff] [blame] | 2040 | stop = !dump_seek(cprm->file, PAGE_SIZE); |
Hugh Dickins | f3e8fcc | 2009-09-21 17:03:25 -0700 | [diff] [blame] | 2041 | if (stop) |
| 2042 | goto end_coredump; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2043 | } |
| 2044 | } |
| 2045 | |
Daisuke HATAYAMA | 1fcccba | 2010-03-05 13:44:07 -0800 | [diff] [blame] | 2046 | if (!elf_core_write_extra_data(cprm->file, &size, cprm->limit)) |
| 2047 | goto end_coredump; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2048 | |
Daisuke HATAYAMA | 8d9032b | 2010-03-05 13:44:10 -0800 | [diff] [blame] | 2049 | if (e_phnum == PN_XNUM) { |
| 2050 | size += sizeof(*shdr4extnum); |
| 2051 | if (size > cprm->limit |
| 2052 | || !dump_write(cprm->file, shdr4extnum, |
| 2053 | sizeof(*shdr4extnum))) |
| 2054 | goto end_coredump; |
| 2055 | } |
| 2056 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2057 | end_coredump: |
| 2058 | set_fs(fs); |
| 2059 | |
| 2060 | cleanup: |
Roland McGrath | 3aba481 | 2008-01-30 13:31:44 +0100 | [diff] [blame] | 2061 | free_note_info(&info); |
Daisuke HATAYAMA | 8d9032b | 2010-03-05 13:44:10 -0800 | [diff] [blame] | 2062 | kfree(shdr4extnum); |
Daisuke HATAYAMA | 93eb211 | 2010-03-05 13:44:09 -0800 | [diff] [blame] | 2063 | kfree(phdr4note); |
WANG Cong | 5f71955 | 2008-05-06 12:45:35 +0800 | [diff] [blame] | 2064 | kfree(elf); |
| 2065 | out: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2066 | return has_dumped; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2067 | } |
| 2068 | |
Christoph Hellwig | 698ba7b | 2009-12-15 16:47:37 -0800 | [diff] [blame] | 2069 | #endif /* CONFIG_ELF_CORE */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2070 | |
| 2071 | static int __init init_elf_binfmt(void) |
| 2072 | { |
| 2073 | return register_binfmt(&elf_format); |
| 2074 | } |
| 2075 | |
| 2076 | static void __exit exit_elf_binfmt(void) |
| 2077 | { |
| 2078 | /* Remove the COFF and ELF loaders. */ |
| 2079 | unregister_binfmt(&elf_format); |
| 2080 | } |
| 2081 | |
| 2082 | core_initcall(init_elf_binfmt); |
| 2083 | module_exit(exit_elf_binfmt); |
| 2084 | MODULE_LICENSE("GPL"); |