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