[PATCH] proc: Simplify the ownership rules for /proc
[linux-2.6] / fs / binfmt_elf_fdpic.c
1 /* binfmt_elf_fdpic.c: FDPIC ELF binary format
2  *
3  * Copyright (C) 2003, 2004 Red Hat, Inc. All Rights Reserved.
4  * Written by David Howells (dhowells@redhat.com)
5  * Derived from binfmt_elf.c
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version
10  * 2 of the License, or (at your option) any later version.
11  */
12
13 #include <linux/module.h>
14
15 #include <linux/fs.h>
16 #include <linux/stat.h>
17 #include <linux/sched.h>
18 #include <linux/mm.h>
19 #include <linux/mman.h>
20 #include <linux/errno.h>
21 #include <linux/signal.h>
22 #include <linux/binfmts.h>
23 #include <linux/string.h>
24 #include <linux/file.h>
25 #include <linux/fcntl.h>
26 #include <linux/slab.h>
27 #include <linux/highmem.h>
28 #include <linux/personality.h>
29 #include <linux/ptrace.h>
30 #include <linux/init.h>
31 #include <linux/smp_lock.h>
32 #include <linux/elf.h>
33 #include <linux/elf-fdpic.h>
34 #include <linux/elfcore.h>
35
36 #include <asm/uaccess.h>
37 #include <asm/param.h>
38 #include <asm/pgalloc.h>
39
40 typedef char *elf_caddr_t;
41 #ifndef elf_addr_t
42 #define elf_addr_t unsigned long
43 #endif
44
45 #if 0
46 #define kdebug(fmt, ...) printk("FDPIC "fmt"\n" ,##__VA_ARGS__ )
47 #else
48 #define kdebug(fmt, ...) do {} while(0)
49 #endif
50
51 MODULE_LICENSE("GPL");
52
53 static int load_elf_fdpic_binary(struct linux_binprm *bprm, struct pt_regs *regs);
54 //static int load_elf_fdpic_library(struct file *);
55 static int elf_fdpic_fetch_phdrs(struct elf_fdpic_params *params, struct file *file);
56 static int elf_fdpic_map_file(struct elf_fdpic_params *params,
57                               struct file *file,
58                               struct mm_struct *mm,
59                               const char *what);
60
61 static int create_elf_fdpic_tables(struct linux_binprm *bprm,
62                                    struct mm_struct *mm,
63                                    struct elf_fdpic_params *exec_params,
64                                    struct elf_fdpic_params *interp_params);
65
66 #ifndef CONFIG_MMU
67 static int elf_fdpic_transfer_args_to_stack(struct linux_binprm *bprm, unsigned long *_sp);
68 static int elf_fdpic_map_file_constdisp_on_uclinux(struct elf_fdpic_params *params,
69                                                    struct file *file,
70                                                    struct mm_struct *mm);
71 #endif
72
73 static int elf_fdpic_map_file_by_direct_mmap(struct elf_fdpic_params *params,
74                                              struct file *file,
75                                              struct mm_struct *mm);
76
77 static struct linux_binfmt elf_fdpic_format = {
78         .module         = THIS_MODULE,
79         .load_binary    = load_elf_fdpic_binary,
80 //      .load_shlib     = load_elf_fdpic_library,
81 //      .core_dump      = elf_fdpic_core_dump,
82         .min_coredump   = ELF_EXEC_PAGESIZE,
83 };
84
85 static int __init init_elf_fdpic_binfmt(void)  { return register_binfmt(&elf_fdpic_format); }
86 static void __exit exit_elf_fdpic_binfmt(void) { unregister_binfmt(&elf_fdpic_format); }
87
88 module_init(init_elf_fdpic_binfmt)
89 module_exit(exit_elf_fdpic_binfmt)
90
91 static int is_elf_fdpic(struct elfhdr *hdr, struct file *file)
92 {
93         if (memcmp(hdr->e_ident, ELFMAG, SELFMAG) != 0)
94                 return 0;
95         if (hdr->e_type != ET_EXEC && hdr->e_type != ET_DYN)
96                 return 0;
97         if (!elf_check_arch(hdr) || !elf_check_fdpic(hdr))
98                 return 0;
99         if (!file->f_op || !file->f_op->mmap)
100                 return 0;
101         return 1;
102 }
103
104 /*****************************************************************************/
105 /*
106  * read the program headers table into memory
107  */
108 static int elf_fdpic_fetch_phdrs(struct elf_fdpic_params *params, struct file *file)
109 {
110         struct elf32_phdr *phdr;
111         unsigned long size;
112         int retval, loop;
113
114         if (params->hdr.e_phentsize != sizeof(struct elf_phdr))
115                 return -ENOMEM;
116         if (params->hdr.e_phnum > 65536U / sizeof(struct elf_phdr))
117                 return -ENOMEM;
118
119         size = params->hdr.e_phnum * sizeof(struct elf_phdr);
120         params->phdrs = kmalloc(size, GFP_KERNEL);
121         if (!params->phdrs)
122                 return -ENOMEM;
123
124         retval = kernel_read(file, params->hdr.e_phoff, (char *) params->phdrs, size);
125         if (retval < 0)
126                 return retval;
127
128         /* determine stack size for this binary */
129         phdr = params->phdrs;
130         for (loop = 0; loop < params->hdr.e_phnum; loop++, phdr++) {
131                 if (phdr->p_type != PT_GNU_STACK)
132                         continue;
133
134                 if (phdr->p_flags & PF_X)
135                         params->flags |= ELF_FDPIC_FLAG_EXEC_STACK;
136                 else
137                         params->flags |= ELF_FDPIC_FLAG_NOEXEC_STACK;
138
139                 params->stack_size = phdr->p_memsz;
140                 break;
141         }
142
143         return 0;
144 } /* end elf_fdpic_fetch_phdrs() */
145
146 /*****************************************************************************/
147 /*
148  * load an fdpic binary into various bits of memory
149  */
150 static int load_elf_fdpic_binary(struct linux_binprm *bprm, struct pt_regs *regs)
151 {
152         struct elf_fdpic_params exec_params, interp_params;
153         struct elf_phdr *phdr;
154         unsigned long stack_size;
155         struct file *interpreter = NULL; /* to shut gcc up */
156         char *interpreter_name = NULL;
157         int executable_stack;
158         int retval, i;
159
160         memset(&exec_params, 0, sizeof(exec_params));
161         memset(&interp_params, 0, sizeof(interp_params));
162
163         exec_params.hdr = *(struct elfhdr *) bprm->buf;
164         exec_params.flags = ELF_FDPIC_FLAG_PRESENT | ELF_FDPIC_FLAG_EXECUTABLE;
165
166         /* check that this is a binary we know how to deal with */
167         retval = -ENOEXEC;
168         if (!is_elf_fdpic(&exec_params.hdr, bprm->file))
169                 goto error;
170
171         /* read the program header table */
172         retval = elf_fdpic_fetch_phdrs(&exec_params, bprm->file);
173         if (retval < 0)
174                 goto error;
175
176         /* scan for a program header that specifies an interpreter */
177         phdr = exec_params.phdrs;
178
179         for (i = 0; i < exec_params.hdr.e_phnum; i++, phdr++) {
180                 switch (phdr->p_type) {
181                 case PT_INTERP:
182                         retval = -ENOMEM;
183                         if (phdr->p_filesz > PATH_MAX)
184                                 goto error;
185                         retval = -ENOENT;
186                         if (phdr->p_filesz < 2)
187                                 goto error;
188
189                         /* read the name of the interpreter into memory */
190                         interpreter_name = kmalloc(phdr->p_filesz, GFP_KERNEL);
191                         if (!interpreter_name)
192                                 goto error;
193
194                         retval = kernel_read(bprm->file,
195                                              phdr->p_offset,
196                                              interpreter_name,
197                                              phdr->p_filesz);
198                         if (retval < 0)
199                                 goto error;
200
201                         retval = -ENOENT;
202                         if (interpreter_name[phdr->p_filesz - 1] != '\0')
203                                 goto error;
204
205                         kdebug("Using ELF interpreter %s", interpreter_name);
206
207                         /* replace the program with the interpreter */
208                         interpreter = open_exec(interpreter_name);
209                         retval = PTR_ERR(interpreter);
210                         if (IS_ERR(interpreter)) {
211                                 interpreter = NULL;
212                                 goto error;
213                         }
214
215                         retval = kernel_read(interpreter, 0, bprm->buf, BINPRM_BUF_SIZE);
216                         if (retval < 0)
217                                 goto error;
218
219                         interp_params.hdr = *((struct elfhdr *) bprm->buf);
220                         break;
221
222                 case PT_LOAD:
223 #ifdef CONFIG_MMU
224                         if (exec_params.load_addr == 0)
225                                 exec_params.load_addr = phdr->p_vaddr;
226 #endif
227                         break;
228                 }
229
230         }
231
232         if (elf_check_const_displacement(&exec_params.hdr))
233                 exec_params.flags |= ELF_FDPIC_FLAG_CONSTDISP;
234
235         /* perform insanity checks on the interpreter */
236         if (interpreter_name) {
237                 retval = -ELIBBAD;
238                 if (!is_elf_fdpic(&interp_params.hdr, interpreter))
239                         goto error;
240
241                 interp_params.flags = ELF_FDPIC_FLAG_PRESENT;
242
243                 /* read the interpreter's program header table */
244                 retval = elf_fdpic_fetch_phdrs(&interp_params, interpreter);
245                 if (retval < 0)
246                         goto error;
247         }
248
249         stack_size = exec_params.stack_size;
250         if (stack_size < interp_params.stack_size)
251                 stack_size = interp_params.stack_size;
252
253         if (exec_params.flags & ELF_FDPIC_FLAG_EXEC_STACK)
254                 executable_stack = EXSTACK_ENABLE_X;
255         else if (exec_params.flags & ELF_FDPIC_FLAG_NOEXEC_STACK)
256                 executable_stack = EXSTACK_DISABLE_X;
257         else if (interp_params.flags & ELF_FDPIC_FLAG_EXEC_STACK)
258                 executable_stack = EXSTACK_ENABLE_X;
259         else if (interp_params.flags & ELF_FDPIC_FLAG_NOEXEC_STACK)
260                 executable_stack = EXSTACK_DISABLE_X;
261         else
262                 executable_stack = EXSTACK_DEFAULT;
263
264         retval = -ENOEXEC;
265         if (stack_size == 0)
266                 goto error;
267
268         if (elf_check_const_displacement(&interp_params.hdr))
269                 interp_params.flags |= ELF_FDPIC_FLAG_CONSTDISP;
270
271         /* flush all traces of the currently running executable */
272         retval = flush_old_exec(bprm);
273         if (retval)
274                 goto error;
275
276         /* there's now no turning back... the old userspace image is dead,
277          * defunct, deceased, etc. after this point we have to exit via
278          * error_kill */
279         set_personality(PER_LINUX_FDPIC);
280         set_binfmt(&elf_fdpic_format);
281
282         current->mm->start_code = 0;
283         current->mm->end_code = 0;
284         current->mm->start_stack = 0;
285         current->mm->start_data = 0;
286         current->mm->end_data = 0;
287         current->mm->context.exec_fdpic_loadmap = 0;
288         current->mm->context.interp_fdpic_loadmap = 0;
289
290         current->flags &= ~PF_FORKNOEXEC;
291
292 #ifdef CONFIG_MMU
293         elf_fdpic_arch_lay_out_mm(&exec_params,
294                                   &interp_params,
295                                   &current->mm->start_stack,
296                                   &current->mm->start_brk);
297
298         retval = setup_arg_pages(bprm, current->mm->start_stack, executable_stack);
299         if (retval < 0) {
300                 send_sig(SIGKILL, current, 0);
301                 goto error_kill;
302         }
303 #endif
304
305         /* load the executable and interpreter into memory */
306         retval = elf_fdpic_map_file(&exec_params, bprm->file, current->mm, "executable");
307         if (retval < 0)
308                 goto error_kill;
309
310         if (interpreter_name) {
311                 retval = elf_fdpic_map_file(&interp_params, interpreter,
312                                             current->mm, "interpreter");
313                 if (retval < 0) {
314                         printk(KERN_ERR "Unable to load interpreter\n");
315                         goto error_kill;
316                 }
317
318                 allow_write_access(interpreter);
319                 fput(interpreter);
320                 interpreter = NULL;
321         }
322
323 #ifdef CONFIG_MMU
324         if (!current->mm->start_brk)
325                 current->mm->start_brk = current->mm->end_data;
326
327         current->mm->brk = current->mm->start_brk = PAGE_ALIGN(current->mm->start_brk);
328
329 #else
330         /* create a stack and brk area big enough for everyone
331          * - the brk heap starts at the bottom and works up
332          * - the stack starts at the top and works down
333          */
334         stack_size = (stack_size + PAGE_SIZE - 1) & PAGE_MASK;
335         if (stack_size < PAGE_SIZE * 2)
336                 stack_size = PAGE_SIZE * 2;
337
338         down_write(&current->mm->mmap_sem);
339         current->mm->start_brk = do_mmap(NULL,
340                                          0,
341                                          stack_size,
342                                          PROT_READ | PROT_WRITE | PROT_EXEC,
343                                          MAP_PRIVATE | MAP_ANON | MAP_GROWSDOWN,
344                                          0);
345
346         if (IS_ERR((void *) current->mm->start_brk)) {
347                 up_write(&current->mm->mmap_sem);
348                 retval = current->mm->start_brk;
349                 current->mm->start_brk = 0;
350                 goto error_kill;
351         }
352
353         if (do_mremap(current->mm->start_brk,
354                       stack_size,
355                       ksize((char *) current->mm->start_brk),
356                       0, 0
357                       ) == current->mm->start_brk
358             )
359                 stack_size = ksize((char *) current->mm->start_brk);
360         up_write(&current->mm->mmap_sem);
361
362         current->mm->brk = current->mm->start_brk;
363         current->mm->context.end_brk = current->mm->start_brk;
364         current->mm->context.end_brk += (stack_size > PAGE_SIZE) ? (stack_size - PAGE_SIZE) : 0;
365         current->mm->start_stack = current->mm->start_brk + stack_size;
366 #endif
367
368         compute_creds(bprm);
369         current->flags &= ~PF_FORKNOEXEC;
370         if (create_elf_fdpic_tables(bprm, current->mm, &exec_params, &interp_params) < 0)
371                 goto error_kill;
372
373         kdebug("- start_code  %lx",     (long) current->mm->start_code);
374         kdebug("- end_code    %lx",     (long) current->mm->end_code);
375         kdebug("- start_data  %lx",     (long) current->mm->start_data);
376         kdebug("- end_data    %lx",     (long) current->mm->end_data);
377         kdebug("- start_brk   %lx",     (long) current->mm->start_brk);
378         kdebug("- brk         %lx",     (long) current->mm->brk);
379         kdebug("- start_stack %lx",     (long) current->mm->start_stack);
380
381 #ifdef ELF_FDPIC_PLAT_INIT
382         /*
383          * The ABI may specify that certain registers be set up in special
384          * ways (on i386 %edx is the address of a DT_FINI function, for
385          * example.  This macro performs whatever initialization to
386          * the regs structure is required.
387          */
388         ELF_FDPIC_PLAT_INIT(regs,
389                             exec_params.map_addr,
390                             interp_params.map_addr,
391                             interp_params.dynamic_addr ?: exec_params.dynamic_addr
392                             );
393 #endif
394
395         /* everything is now ready... get the userspace context ready to roll */
396         start_thread(regs,
397                      interp_params.entry_addr ?: exec_params.entry_addr,
398                      current->mm->start_stack);
399
400         if (unlikely(current->ptrace & PT_PTRACED)) {
401                 if (current->ptrace & PT_TRACE_EXEC)
402                         ptrace_notify ((PTRACE_EVENT_EXEC << 8) | SIGTRAP);
403                 else
404                         send_sig(SIGTRAP, current, 0);
405         }
406
407         retval = 0;
408
409 error:
410         if (interpreter) {
411                 allow_write_access(interpreter);
412                 fput(interpreter);
413         }
414         kfree(interpreter_name);
415         kfree(exec_params.phdrs);
416         kfree(exec_params.loadmap);
417         kfree(interp_params.phdrs);
418         kfree(interp_params.loadmap);
419         return retval;
420
421         /* unrecoverable error - kill the process */
422  error_kill:
423         send_sig(SIGSEGV, current, 0);
424         goto error;
425
426 } /* end load_elf_fdpic_binary() */
427
428 /*****************************************************************************/
429 /*
430  * present useful information to the program
431  */
432 static int create_elf_fdpic_tables(struct linux_binprm *bprm,
433                                    struct mm_struct *mm,
434                                    struct elf_fdpic_params *exec_params,
435                                    struct elf_fdpic_params *interp_params)
436 {
437         unsigned long sp, csp, nitems;
438         elf_caddr_t __user *argv, *envp;
439         size_t platform_len = 0, len;
440         char *k_platform;
441         char __user *u_platform, *p;
442         long hwcap;
443         int loop;
444
445         /* we're going to shovel a whole load of stuff onto the stack */
446 #ifdef CONFIG_MMU
447         sp = bprm->p;
448 #else
449         sp = mm->start_stack;
450
451         /* stack the program arguments and environment */
452         if (elf_fdpic_transfer_args_to_stack(bprm, &sp) < 0)
453                 return -EFAULT;
454 #endif
455
456         /* get hold of platform and hardware capabilities masks for the machine
457          * we are running on.  In some cases (Sparc), this info is impossible
458          * to get, in others (i386) it is merely difficult.
459          */
460         hwcap = ELF_HWCAP;
461         k_platform = ELF_PLATFORM;
462
463         if (k_platform) {
464                 platform_len = strlen(k_platform) + 1;
465                 sp -= platform_len;
466                 u_platform = (char __user *) sp;
467                 if (__copy_to_user(u_platform, k_platform, platform_len) != 0)
468                         return -EFAULT;
469         }
470
471 #if defined(__i386__) && defined(CONFIG_SMP)
472         /* in some cases (e.g. Hyper-Threading), we want to avoid L1 evictions
473          * by the processes running on the same package. One thing we can do
474          * is to shuffle the initial stack for them.
475          *
476          * the conditionals here are unneeded, but kept in to make the
477          * code behaviour the same as pre change unless we have hyperthreaded
478          * processors. This keeps Mr Marcelo Person happier but should be
479          * removed for 2.5
480          */
481         if (smp_num_siblings > 1)
482                 sp = sp - ((current->pid % 64) << 7);
483 #endif
484
485         sp &= ~7UL;
486
487         /* stack the load map(s) */
488         len = sizeof(struct elf32_fdpic_loadmap);
489         len += sizeof(struct elf32_fdpic_loadseg) * exec_params->loadmap->nsegs;
490         sp = (sp - len) & ~7UL;
491         exec_params->map_addr = sp;
492
493         if (copy_to_user((void __user *) sp, exec_params->loadmap, len) != 0)
494                 return -EFAULT;
495
496         current->mm->context.exec_fdpic_loadmap = (unsigned long) sp;
497
498         if (interp_params->loadmap) {
499                 len = sizeof(struct elf32_fdpic_loadmap);
500                 len += sizeof(struct elf32_fdpic_loadseg) * interp_params->loadmap->nsegs;
501                 sp = (sp - len) & ~7UL;
502                 interp_params->map_addr = sp;
503
504                 if (copy_to_user((void __user *) sp, interp_params->loadmap, len) != 0)
505                         return -EFAULT;
506
507                 current->mm->context.interp_fdpic_loadmap = (unsigned long) sp;
508         }
509
510         /* force 16 byte _final_ alignment here for generality */
511 #define DLINFO_ITEMS 13
512
513         nitems = 1 + DLINFO_ITEMS + (k_platform ? 1 : 0);
514 #ifdef DLINFO_ARCH_ITEMS
515         nitems += DLINFO_ARCH_ITEMS;
516 #endif
517
518         csp = sp;
519         sp -= nitems * 2 * sizeof(unsigned long);
520         sp -= (bprm->envc + 1) * sizeof(char *);        /* envv[] */
521         sp -= (bprm->argc + 1) * sizeof(char *);        /* argv[] */
522         sp -= 1 * sizeof(unsigned long);                /* argc */
523
524         csp -= sp & 15UL;
525         sp -= sp & 15UL;
526
527         /* put the ELF interpreter info on the stack */
528 #define NEW_AUX_ENT(nr, id, val)                                                \
529         do {                                                                    \
530                 struct { unsigned long _id, _val; } __user *ent = (void __user *) csp;  \
531                 __put_user((id), &ent[nr]._id);                                 \
532                 __put_user((val), &ent[nr]._val);                               \
533         } while (0)
534
535         csp -= 2 * sizeof(unsigned long);
536         NEW_AUX_ENT(0, AT_NULL, 0);
537         if (k_platform) {
538                 csp -= 2 * sizeof(unsigned long);
539                 NEW_AUX_ENT(0, AT_PLATFORM, (elf_addr_t)(unsigned long) u_platform);
540         }
541
542         csp -= DLINFO_ITEMS * 2 * sizeof(unsigned long);
543         NEW_AUX_ENT( 0, AT_HWCAP,               hwcap);
544         NEW_AUX_ENT( 1, AT_PAGESZ,              PAGE_SIZE);
545         NEW_AUX_ENT( 2, AT_CLKTCK,              CLOCKS_PER_SEC);
546         NEW_AUX_ENT( 3, AT_PHDR,                exec_params->ph_addr);
547         NEW_AUX_ENT( 4, AT_PHENT,               sizeof(struct elf_phdr));
548         NEW_AUX_ENT( 5, AT_PHNUM,               exec_params->hdr.e_phnum);
549         NEW_AUX_ENT( 6, AT_BASE,                interp_params->elfhdr_addr);
550         NEW_AUX_ENT( 7, AT_FLAGS,               0);
551         NEW_AUX_ENT( 8, AT_ENTRY,               exec_params->entry_addr);
552         NEW_AUX_ENT( 9, AT_UID,                 (elf_addr_t) current->uid);
553         NEW_AUX_ENT(10, AT_EUID,                (elf_addr_t) current->euid);
554         NEW_AUX_ENT(11, AT_GID,                 (elf_addr_t) current->gid);
555         NEW_AUX_ENT(12, AT_EGID,                (elf_addr_t) current->egid);
556
557 #ifdef ARCH_DLINFO
558         /* ARCH_DLINFO must come last so platform specific code can enforce
559          * special alignment requirements on the AUXV if necessary (eg. PPC).
560          */
561         ARCH_DLINFO;
562 #endif
563 #undef NEW_AUX_ENT
564
565         /* allocate room for argv[] and envv[] */
566         csp -= (bprm->envc + 1) * sizeof(elf_caddr_t);
567         envp = (elf_caddr_t __user *) csp;
568         csp -= (bprm->argc + 1) * sizeof(elf_caddr_t);
569         argv = (elf_caddr_t __user *) csp;
570
571         /* stack argc */
572         csp -= sizeof(unsigned long);
573         __put_user(bprm->argc, (unsigned long __user *) csp);
574
575         BUG_ON(csp != sp);
576
577         /* fill in the argv[] array */
578 #ifdef CONFIG_MMU
579         current->mm->arg_start = bprm->p;
580 #else
581         current->mm->arg_start = current->mm->start_stack - (MAX_ARG_PAGES * PAGE_SIZE - bprm->p);
582 #endif
583
584         p = (char __user *) current->mm->arg_start;
585         for (loop = bprm->argc; loop > 0; loop--) {
586                 __put_user((elf_caddr_t) p, argv++);
587                 len = strnlen_user(p, PAGE_SIZE * MAX_ARG_PAGES);
588                 if (!len || len > PAGE_SIZE * MAX_ARG_PAGES)
589                         return -EINVAL;
590                 p += len;
591         }
592         __put_user(NULL, argv);
593         current->mm->arg_end = (unsigned long) p;
594
595         /* fill in the envv[] array */
596         current->mm->env_start = (unsigned long) p;
597         for (loop = bprm->envc; loop > 0; loop--) {
598                 __put_user((elf_caddr_t)(unsigned long) p, envp++);
599                 len = strnlen_user(p, PAGE_SIZE * MAX_ARG_PAGES);
600                 if (!len || len > PAGE_SIZE * MAX_ARG_PAGES)
601                         return -EINVAL;
602                 p += len;
603         }
604         __put_user(NULL, envp);
605         current->mm->env_end = (unsigned long) p;
606
607         mm->start_stack = (unsigned long) sp;
608         return 0;
609 } /* end create_elf_fdpic_tables() */
610
611 /*****************************************************************************/
612 /*
613  * transfer the program arguments and environment from the holding pages onto
614  * the stack
615  */
616 #ifndef CONFIG_MMU
617 static int elf_fdpic_transfer_args_to_stack(struct linux_binprm *bprm, unsigned long *_sp)
618 {
619         unsigned long index, stop, sp;
620         char *src;
621         int ret = 0;
622
623         stop = bprm->p >> PAGE_SHIFT;
624         sp = *_sp;
625
626         for (index = MAX_ARG_PAGES - 1; index >= stop; index--) {
627                 src = kmap(bprm->page[index]);
628                 sp -= PAGE_SIZE;
629                 if (copy_to_user((void *) sp, src, PAGE_SIZE) != 0)
630                         ret = -EFAULT;
631                 kunmap(bprm->page[index]);
632                 if (ret < 0)
633                         goto out;
634         }
635
636         *_sp = (*_sp - (MAX_ARG_PAGES * PAGE_SIZE - bprm->p)) & ~15;
637
638  out:
639         return ret;
640 } /* end elf_fdpic_transfer_args_to_stack() */
641 #endif
642
643 /*****************************************************************************/
644 /*
645  * load the appropriate binary image (executable or interpreter) into memory
646  * - we assume no MMU is available
647  * - if no other PIC bits are set in params->hdr->e_flags
648  *   - we assume that the LOADable segments in the binary are independently relocatable
649  *   - we assume R/O executable segments are shareable
650  * - else
651  *   - we assume the loadable parts of the image to require fixed displacement
652  *   - the image is not shareable
653  */
654 static int elf_fdpic_map_file(struct elf_fdpic_params *params,
655                               struct file *file,
656                               struct mm_struct *mm,
657                               const char *what)
658 {
659         struct elf32_fdpic_loadmap *loadmap;
660 #ifdef CONFIG_MMU
661         struct elf32_fdpic_loadseg *mseg;
662 #endif
663         struct elf32_fdpic_loadseg *seg;
664         struct elf32_phdr *phdr;
665         unsigned long load_addr, stop;
666         unsigned nloads, tmp;
667         size_t size;
668         int loop, ret;
669
670         /* allocate a load map table */
671         nloads = 0;
672         for (loop = 0; loop < params->hdr.e_phnum; loop++)
673                 if (params->phdrs[loop].p_type == PT_LOAD)
674                         nloads++;
675
676         if (nloads == 0)
677                 return -ELIBBAD;
678
679         size = sizeof(*loadmap) + nloads * sizeof(*seg);
680         loadmap = kmalloc(size, GFP_KERNEL);
681         if (!loadmap)
682                 return -ENOMEM;
683
684         params->loadmap = loadmap;
685         memset(loadmap, 0, size);
686
687         loadmap->version = ELF32_FDPIC_LOADMAP_VERSION;
688         loadmap->nsegs = nloads;
689
690         load_addr = params->load_addr;
691         seg = loadmap->segs;
692
693         /* map the requested LOADs into the memory space */
694         switch (params->flags & ELF_FDPIC_FLAG_ARRANGEMENT) {
695         case ELF_FDPIC_FLAG_CONSTDISP:
696         case ELF_FDPIC_FLAG_CONTIGUOUS:
697 #ifndef CONFIG_MMU
698                 ret = elf_fdpic_map_file_constdisp_on_uclinux(params, file, mm);
699                 if (ret < 0)
700                         return ret;
701                 break;
702 #endif
703         default:
704                 ret = elf_fdpic_map_file_by_direct_mmap(params, file, mm);
705                 if (ret < 0)
706                         return ret;
707                 break;
708         }
709
710         /* map the entry point */
711         if (params->hdr.e_entry) {
712                 seg = loadmap->segs;
713                 for (loop = loadmap->nsegs; loop > 0; loop--, seg++) {
714                         if (params->hdr.e_entry >= seg->p_vaddr &&
715                             params->hdr.e_entry < seg->p_vaddr + seg->p_memsz
716                             ) {
717                                 params->entry_addr =
718                                         (params->hdr.e_entry - seg->p_vaddr) + seg->addr;
719                                 break;
720                         }
721                 }
722         }
723
724         /* determine where the program header table has wound up if mapped */
725         stop = params->hdr.e_phoff + params->hdr.e_phnum * sizeof (struct elf_phdr);
726         phdr = params->phdrs;
727
728         for (loop = 0; loop < params->hdr.e_phnum; loop++, phdr++) {
729                 if (phdr->p_type != PT_LOAD)
730                         continue;
731
732                 if (phdr->p_offset > params->hdr.e_phoff ||
733                     phdr->p_offset + phdr->p_filesz < stop)
734                         continue;
735
736                 seg = loadmap->segs;
737                 for (loop = loadmap->nsegs; loop > 0; loop--, seg++) {
738                         if (phdr->p_vaddr >= seg->p_vaddr &&
739                             phdr->p_vaddr + phdr->p_filesz <= seg->p_vaddr + seg->p_memsz
740                             ) {
741                                 params->ph_addr = (phdr->p_vaddr - seg->p_vaddr) + seg->addr +
742                                         params->hdr.e_phoff - phdr->p_offset;
743                                 break;
744                         }
745                 }
746                 break;
747         }
748
749         /* determine where the dynamic section has wound up if there is one */
750         phdr = params->phdrs;
751         for (loop = 0; loop < params->hdr.e_phnum; loop++, phdr++) {
752                 if (phdr->p_type != PT_DYNAMIC)
753                         continue;
754
755                 seg = loadmap->segs;
756                 for (loop = loadmap->nsegs; loop > 0; loop--, seg++) {
757                         if (phdr->p_vaddr >= seg->p_vaddr &&
758                             phdr->p_vaddr + phdr->p_memsz <= seg->p_vaddr + seg->p_memsz
759                             ) {
760                                 params->dynamic_addr = (phdr->p_vaddr - seg->p_vaddr) + seg->addr;
761
762                                 /* check the dynamic section contains at least one item, and that
763                                  * the last item is a NULL entry */
764                                 if (phdr->p_memsz == 0 ||
765                                     phdr->p_memsz % sizeof(Elf32_Dyn) != 0)
766                                         goto dynamic_error;
767
768                                 tmp = phdr->p_memsz / sizeof(Elf32_Dyn);
769                                 if (((Elf32_Dyn *) params->dynamic_addr)[tmp - 1].d_tag != 0)
770                                         goto dynamic_error;
771                                 break;
772                         }
773                 }
774                 break;
775         }
776
777         /* now elide adjacent segments in the load map on MMU linux
778          * - on uClinux the holes between may actually be filled with system stuff or stuff from
779          *   other processes
780          */
781 #ifdef CONFIG_MMU
782         nloads = loadmap->nsegs;
783         mseg = loadmap->segs;
784         seg = mseg + 1;
785         for (loop = 1; loop < nloads; loop++) {
786                 /* see if we have a candidate for merging */
787                 if (seg->p_vaddr - mseg->p_vaddr == seg->addr - mseg->addr) {
788                         load_addr = PAGE_ALIGN(mseg->addr + mseg->p_memsz);
789                         if (load_addr == (seg->addr & PAGE_MASK)) {
790                                 mseg->p_memsz += load_addr - (mseg->addr + mseg->p_memsz);
791                                 mseg->p_memsz += seg->addr & ~PAGE_MASK;
792                                 mseg->p_memsz += seg->p_memsz;
793                                 loadmap->nsegs--;
794                                 continue;
795                         }
796                 }
797
798                 mseg++;
799                 if (mseg != seg)
800                         *mseg = *seg;
801         }
802 #endif
803
804         kdebug("Mapped Object [%s]:", what);
805         kdebug("- elfhdr   : %lx", params->elfhdr_addr);
806         kdebug("- entry    : %lx", params->entry_addr);
807         kdebug("- PHDR[]   : %lx", params->ph_addr);
808         kdebug("- DYNAMIC[]: %lx", params->dynamic_addr);
809         seg = loadmap->segs;
810         for (loop = 0; loop < loadmap->nsegs; loop++, seg++)
811                 kdebug("- LOAD[%d] : %08x-%08x [va=%x ms=%x]",
812                        loop,
813                        seg->addr, seg->addr + seg->p_memsz - 1,
814                        seg->p_vaddr, seg->p_memsz);
815
816         return 0;
817
818  dynamic_error:
819         printk("ELF FDPIC %s with invalid DYNAMIC section (inode=%lu)\n",
820                what, file->f_dentry->d_inode->i_ino);
821         return -ELIBBAD;
822 } /* end elf_fdpic_map_file() */
823
824 /*****************************************************************************/
825 /*
826  * map a file with constant displacement under uClinux
827  */
828 #ifndef CONFIG_MMU
829 static int elf_fdpic_map_file_constdisp_on_uclinux(struct elf_fdpic_params *params,
830                                                    struct file *file,
831                                                    struct mm_struct *mm)
832 {
833         struct elf32_fdpic_loadseg *seg;
834         struct elf32_phdr *phdr;
835         unsigned long load_addr, base = ULONG_MAX, top = 0, maddr = 0, mflags;
836         loff_t fpos;
837         int loop, ret;
838
839         load_addr = params->load_addr;
840         seg = params->loadmap->segs;
841
842         /* determine the bounds of the contiguous overall allocation we must make */
843         phdr = params->phdrs;
844         for (loop = 0; loop < params->hdr.e_phnum; loop++, phdr++) {
845                 if (params->phdrs[loop].p_type != PT_LOAD)
846                         continue;
847
848                 if (base > phdr->p_vaddr)
849                         base = phdr->p_vaddr;
850                 if (top < phdr->p_vaddr + phdr->p_memsz)
851                         top = phdr->p_vaddr + phdr->p_memsz;
852         }
853
854         /* allocate one big anon block for everything */
855         mflags = MAP_PRIVATE;
856         if (params->flags & ELF_FDPIC_FLAG_EXECUTABLE)
857                 mflags |= MAP_EXECUTABLE;
858
859         down_write(&mm->mmap_sem);
860         maddr = do_mmap(NULL, load_addr, top - base,
861                         PROT_READ | PROT_WRITE | PROT_EXEC, mflags, 0);
862         up_write(&mm->mmap_sem);
863         if (IS_ERR((void *) maddr))
864                 return (int) maddr;
865
866         if (load_addr != 0)
867                 load_addr += PAGE_ALIGN(top - base);
868
869         /* and then load the file segments into it */
870         phdr = params->phdrs;
871         for (loop = 0; loop < params->hdr.e_phnum; loop++, phdr++) {
872                 if (params->phdrs[loop].p_type != PT_LOAD)
873                         continue;
874
875                 fpos = phdr->p_offset;
876
877                 seg->addr = maddr + (phdr->p_vaddr - base);
878                 seg->p_vaddr = phdr->p_vaddr;
879                 seg->p_memsz = phdr->p_memsz;
880
881                 ret = file->f_op->read(file, (void *) seg->addr, phdr->p_filesz, &fpos);
882                 if (ret < 0)
883                         return ret;
884
885                 /* map the ELF header address if in this segment */
886                 if (phdr->p_offset == 0)
887                         params->elfhdr_addr = seg->addr;
888
889                 /* clear any space allocated but not loaded */
890                 if (phdr->p_filesz < phdr->p_memsz)
891                         clear_user((void *) (seg->addr + phdr->p_filesz),
892                                    phdr->p_memsz - phdr->p_filesz);
893
894                 if (mm) {
895                         if (phdr->p_flags & PF_X) {
896                                 mm->start_code = seg->addr;
897                                 mm->end_code = seg->addr + phdr->p_memsz;
898                         }
899                         else if (!mm->start_data) {
900                                 mm->start_data = seg->addr;
901 #ifndef CONFIG_MMU
902                                 mm->end_data = seg->addr + phdr->p_memsz;
903 #endif
904                         }
905
906 #ifdef CONFIG_MMU
907                         if (seg->addr + phdr->p_memsz > mm->end_data)
908                                 mm->end_data = seg->addr + phdr->p_memsz;
909 #endif
910                 }
911
912                 seg++;
913         }
914
915         return 0;
916 } /* end elf_fdpic_map_file_constdisp_on_uclinux() */
917 #endif
918
919 /*****************************************************************************/
920 /*
921  * map a binary by direct mmap() of the individual PT_LOAD segments
922  */
923 static int elf_fdpic_map_file_by_direct_mmap(struct elf_fdpic_params *params,
924                                              struct file *file,
925                                              struct mm_struct *mm)
926 {
927         struct elf32_fdpic_loadseg *seg;
928         struct elf32_phdr *phdr;
929         unsigned long load_addr, delta_vaddr;
930         int loop, dvset;
931
932         load_addr = params->load_addr;
933         delta_vaddr = 0;
934         dvset = 0;
935
936         seg = params->loadmap->segs;
937
938         /* deal with each load segment separately */
939         phdr = params->phdrs;
940         for (loop = 0; loop < params->hdr.e_phnum; loop++, phdr++) {
941                 unsigned long maddr, disp, excess, excess1;
942                 int prot = 0, flags;
943
944                 if (phdr->p_type != PT_LOAD)
945                         continue;
946
947                 kdebug("[LOAD] va=%lx of=%lx fs=%lx ms=%lx",
948                        (unsigned long) phdr->p_vaddr,
949                        (unsigned long) phdr->p_offset,
950                        (unsigned long) phdr->p_filesz,
951                        (unsigned long) phdr->p_memsz);
952
953                 /* determine the mapping parameters */
954                 if (phdr->p_flags & PF_R) prot |= PROT_READ;
955                 if (phdr->p_flags & PF_W) prot |= PROT_WRITE;
956                 if (phdr->p_flags & PF_X) prot |= PROT_EXEC;
957
958                 flags = MAP_PRIVATE | MAP_DENYWRITE;
959                 if (params->flags & ELF_FDPIC_FLAG_EXECUTABLE)
960                         flags |= MAP_EXECUTABLE;
961
962                 maddr = 0;
963
964                 switch (params->flags & ELF_FDPIC_FLAG_ARRANGEMENT) {
965                 case ELF_FDPIC_FLAG_INDEPENDENT:
966                         /* PT_LOADs are independently locatable */
967                         break;
968
969                 case ELF_FDPIC_FLAG_HONOURVADDR:
970                         /* the specified virtual address must be honoured */
971                         maddr = phdr->p_vaddr;
972                         flags |= MAP_FIXED;
973                         break;
974
975                 case ELF_FDPIC_FLAG_CONSTDISP:
976                         /* constant displacement
977                          * - can be mapped anywhere, but must be mapped as a unit
978                          */
979                         if (!dvset) {
980                                 maddr = load_addr;
981                                 delta_vaddr = phdr->p_vaddr;
982                                 dvset = 1;
983                         }
984                         else {
985                                 maddr = load_addr + phdr->p_vaddr - delta_vaddr;
986                                 flags |= MAP_FIXED;
987                         }
988                         break;
989
990                 case ELF_FDPIC_FLAG_CONTIGUOUS:
991                         /* contiguity handled later */
992                         break;
993
994                 default:
995                         BUG();
996                 }
997
998                 maddr &= PAGE_MASK;
999
1000                 /* create the mapping */
1001                 disp = phdr->p_vaddr & ~PAGE_MASK;
1002                 down_write(&mm->mmap_sem);
1003                 maddr = do_mmap(file, maddr, phdr->p_memsz + disp, prot, flags,
1004                                 phdr->p_offset - disp);
1005                 up_write(&mm->mmap_sem);
1006
1007                 kdebug("mmap[%d] <file> sz=%lx pr=%x fl=%x of=%lx --> %08lx",
1008                        loop, phdr->p_memsz + disp, prot, flags, phdr->p_offset - disp,
1009                        maddr);
1010
1011                 if (IS_ERR((void *) maddr))
1012                         return (int) maddr;
1013
1014                 if ((params->flags & ELF_FDPIC_FLAG_ARRANGEMENT) == ELF_FDPIC_FLAG_CONTIGUOUS)
1015                         load_addr += PAGE_ALIGN(phdr->p_memsz + disp);
1016
1017                 seg->addr = maddr + disp;
1018                 seg->p_vaddr = phdr->p_vaddr;
1019                 seg->p_memsz = phdr->p_memsz;
1020
1021                 /* map the ELF header address if in this segment */
1022                 if (phdr->p_offset == 0)
1023                         params->elfhdr_addr = seg->addr;
1024
1025                 /* clear the bit between beginning of mapping and beginning of PT_LOAD */
1026                 if (prot & PROT_WRITE && disp > 0) {
1027                         kdebug("clear[%d] ad=%lx sz=%lx", loop, maddr, disp);
1028                         clear_user((void __user *) maddr, disp);
1029                         maddr += disp;
1030                 }
1031
1032                 /* clear any space allocated but not loaded
1033                  * - on uClinux we can just clear the lot
1034                  * - on MMU linux we'll get a SIGBUS beyond the last page
1035                  *   extant in the file
1036                  */
1037                 excess = phdr->p_memsz - phdr->p_filesz;
1038                 excess1 = PAGE_SIZE - ((maddr + phdr->p_filesz) & ~PAGE_MASK);
1039
1040 #ifdef CONFIG_MMU
1041
1042                 if (excess > excess1) {
1043                         unsigned long xaddr = maddr + phdr->p_filesz + excess1;
1044                         unsigned long xmaddr;
1045
1046                         flags |= MAP_FIXED | MAP_ANONYMOUS;
1047                         down_write(&mm->mmap_sem);
1048                         xmaddr = do_mmap(NULL, xaddr, excess - excess1, prot, flags, 0);
1049                         up_write(&mm->mmap_sem);
1050
1051                         kdebug("mmap[%d] <anon>"
1052                                " ad=%lx sz=%lx pr=%x fl=%x of=0 --> %08lx",
1053                                loop, xaddr, excess - excess1, prot, flags, xmaddr);
1054
1055                         if (xmaddr != xaddr)
1056                                 return -ENOMEM;
1057                 }
1058
1059                 if (prot & PROT_WRITE && excess1 > 0) {
1060                         kdebug("clear[%d] ad=%lx sz=%lx",
1061                                loop, maddr + phdr->p_filesz, excess1);
1062                         clear_user((void __user *) maddr + phdr->p_filesz, excess1);
1063                 }
1064
1065 #else
1066                 if (excess > 0) {
1067                         kdebug("clear[%d] ad=%lx sz=%lx",
1068                                loop, maddr + phdr->p_filesz, excess);
1069                         clear_user((void *) maddr + phdr->p_filesz, excess);
1070                 }
1071 #endif
1072
1073                 if (mm) {
1074                         if (phdr->p_flags & PF_X) {
1075                                 mm->start_code = maddr;
1076                                 mm->end_code = maddr + phdr->p_memsz;
1077                         }
1078                         else if (!mm->start_data) {
1079                                 mm->start_data = maddr;
1080                                 mm->end_data = maddr + phdr->p_memsz;
1081                         }
1082                 }
1083
1084                 seg++;
1085         }
1086
1087         return 0;
1088 } /* end elf_fdpic_map_file_by_direct_mmap() */