~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/fs/binfmt_elf_fdpic.c

Version: ~ [ linux-6.12-rc7 ] ~ [ linux-6.11.7 ] ~ [ linux-6.10.14 ] ~ [ linux-6.9.12 ] ~ [ linux-6.8.12 ] ~ [ linux-6.7.12 ] ~ [ linux-6.6.60 ] ~ [ linux-6.5.13 ] ~ [ linux-6.4.16 ] ~ [ linux-6.3.13 ] ~ [ linux-6.2.16 ] ~ [ linux-6.1.116 ] ~ [ linux-6.0.19 ] ~ [ linux-5.19.17 ] ~ [ linux-5.18.19 ] ~ [ linux-5.17.15 ] ~ [ linux-5.16.20 ] ~ [ linux-5.15.171 ] ~ [ linux-5.14.21 ] ~ [ linux-5.13.19 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.229 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.285 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.323 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.336 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.337 ] ~ [ linux-4.4.302 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.12 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

Diff markup

Differences between /fs/binfmt_elf_fdpic.c (Version linux-6.12-rc7) and /fs/binfmt_elf_fdpic.c (Version linux-5.9.16)


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

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

sflogo.php