a.out: remove core dumping support
[muen/linux.git] / arch / x86 / ia32 / ia32_aout.c
1 /*
2  *  a.out loader for x86-64
3  *
4  *  Copyright (C) 1991, 1992, 1996  Linus Torvalds
5  *  Hacked together by Andi Kleen
6  */
7
8 #include <linux/module.h>
9
10 #include <linux/time.h>
11 #include <linux/kernel.h>
12 #include <linux/mm.h>
13 #include <linux/mman.h>
14 #include <linux/a.out.h>
15 #include <linux/errno.h>
16 #include <linux/signal.h>
17 #include <linux/string.h>
18 #include <linux/fs.h>
19 #include <linux/file.h>
20 #include <linux/stat.h>
21 #include <linux/fcntl.h>
22 #include <linux/ptrace.h>
23 #include <linux/user.h>
24 #include <linux/binfmts.h>
25 #include <linux/personality.h>
26 #include <linux/init.h>
27 #include <linux/jiffies.h>
28 #include <linux/perf_event.h>
29 #include <linux/sched/task_stack.h>
30
31 #include <linux/uaccess.h>
32 #include <asm/pgalloc.h>
33 #include <asm/cacheflush.h>
34 #include <asm/user32.h>
35 #include <asm/ia32.h>
36
37 #undef WARN_OLD
38
39 static int load_aout_binary(struct linux_binprm *);
40 static int load_aout_library(struct file *);
41
42 static struct linux_binfmt aout_format = {
43         .module         = THIS_MODULE,
44         .load_binary    = load_aout_binary,
45         .load_shlib     = load_aout_library,
46 };
47
48 static int set_brk(unsigned long start, unsigned long end)
49 {
50         start = PAGE_ALIGN(start);
51         end = PAGE_ALIGN(end);
52         if (end <= start)
53                 return 0;
54         return vm_brk(start, end - start);
55 }
56
57
58 /*
59  * create_aout_tables() parses the env- and arg-strings in new user
60  * memory and creates the pointer tables from them, and puts their
61  * addresses on the "stack", returning the new stack pointer value.
62  */
63 static u32 __user *create_aout_tables(char __user *p, struct linux_binprm *bprm)
64 {
65         u32 __user *argv, *envp, *sp;
66         int argc = bprm->argc, envc = bprm->envc;
67
68         sp = (u32 __user *) ((-(unsigned long)sizeof(u32)) & (unsigned long) p);
69         sp -= envc+1;
70         envp = sp;
71         sp -= argc+1;
72         argv = sp;
73         put_user((unsigned long) envp, --sp);
74         put_user((unsigned long) argv, --sp);
75         put_user(argc, --sp);
76         current->mm->arg_start = (unsigned long) p;
77         while (argc-- > 0) {
78                 char c;
79
80                 put_user((u32)(unsigned long)p, argv++);
81                 do {
82                         get_user(c, p++);
83                 } while (c);
84         }
85         put_user(0, argv);
86         current->mm->arg_end = current->mm->env_start = (unsigned long) p;
87         while (envc-- > 0) {
88                 char c;
89
90                 put_user((u32)(unsigned long)p, envp++);
91                 do {
92                         get_user(c, p++);
93                 } while (c);
94         }
95         put_user(0, envp);
96         current->mm->env_end = (unsigned long) p;
97         return sp;
98 }
99
100 /*
101  * These are the functions used to load a.out style executables and shared
102  * libraries.  There is no binary dependent code anywhere else.
103  */
104 static int load_aout_binary(struct linux_binprm *bprm)
105 {
106         unsigned long error, fd_offset, rlim;
107         struct pt_regs *regs = current_pt_regs();
108         struct exec ex;
109         int retval;
110
111         ex = *((struct exec *) bprm->buf);              /* exec-header */
112         if ((N_MAGIC(ex) != ZMAGIC && N_MAGIC(ex) != OMAGIC &&
113              N_MAGIC(ex) != QMAGIC && N_MAGIC(ex) != NMAGIC) ||
114             N_TRSIZE(ex) || N_DRSIZE(ex) ||
115             i_size_read(file_inode(bprm->file)) <
116             ex.a_text+ex.a_data+N_SYMSIZE(ex)+N_TXTOFF(ex)) {
117                 return -ENOEXEC;
118         }
119
120         fd_offset = N_TXTOFF(ex);
121
122         /* Check initial limits. This avoids letting people circumvent
123          * size limits imposed on them by creating programs with large
124          * arrays in the data or bss.
125          */
126         rlim = rlimit(RLIMIT_DATA);
127         if (rlim >= RLIM_INFINITY)
128                 rlim = ~0;
129         if (ex.a_data + ex.a_bss > rlim)
130                 return -ENOMEM;
131
132         /* Flush all traces of the currently running executable */
133         retval = flush_old_exec(bprm);
134         if (retval)
135                 return retval;
136
137         /* OK, This is the point of no return */
138         set_personality(PER_LINUX);
139         set_personality_ia32(false);
140
141         setup_new_exec(bprm);
142
143         regs->cs = __USER32_CS;
144         regs->r8 = regs->r9 = regs->r10 = regs->r11 = regs->r12 =
145                 regs->r13 = regs->r14 = regs->r15 = 0;
146
147         current->mm->end_code = ex.a_text +
148                 (current->mm->start_code = N_TXTADDR(ex));
149         current->mm->end_data = ex.a_data +
150                 (current->mm->start_data = N_DATADDR(ex));
151         current->mm->brk = ex.a_bss +
152                 (current->mm->start_brk = N_BSSADDR(ex));
153
154         retval = setup_arg_pages(bprm, IA32_STACK_TOP, EXSTACK_DEFAULT);
155         if (retval < 0)
156                 return retval;
157
158         install_exec_creds(bprm);
159
160         if (N_MAGIC(ex) == OMAGIC) {
161                 unsigned long text_addr, map_size;
162
163                 text_addr = N_TXTADDR(ex);
164                 map_size = ex.a_text+ex.a_data;
165
166                 error = vm_brk(text_addr & PAGE_MASK, map_size);
167
168                 if (error)
169                         return error;
170
171                 error = read_code(bprm->file, text_addr, 32,
172                                   ex.a_text + ex.a_data);
173                 if ((signed long)error < 0)
174                         return error;
175         } else {
176 #ifdef WARN_OLD
177                 static unsigned long error_time, error_time2;
178                 if ((ex.a_text & 0xfff || ex.a_data & 0xfff) &&
179                     (N_MAGIC(ex) != NMAGIC) &&
180                                 time_after(jiffies, error_time2 + 5*HZ)) {
181                         printk(KERN_NOTICE "executable not page aligned\n");
182                         error_time2 = jiffies;
183                 }
184
185                 if ((fd_offset & ~PAGE_MASK) != 0 &&
186                             time_after(jiffies, error_time + 5*HZ)) {
187                         printk(KERN_WARNING
188                                "fd_offset is not page aligned. Please convert "
189                                "program: %pD\n",
190                                bprm->file);
191                         error_time = jiffies;
192                 }
193 #endif
194
195                 if (!bprm->file->f_op->mmap || (fd_offset & ~PAGE_MASK) != 0) {
196                         error = vm_brk(N_TXTADDR(ex), ex.a_text+ex.a_data);
197                         if (error)
198                                 return error;
199
200                         read_code(bprm->file, N_TXTADDR(ex), fd_offset,
201                                         ex.a_text+ex.a_data);
202                         goto beyond_if;
203                 }
204
205                 error = vm_mmap(bprm->file, N_TXTADDR(ex), ex.a_text,
206                                 PROT_READ | PROT_EXEC,
207                                 MAP_FIXED | MAP_PRIVATE | MAP_DENYWRITE |
208                                 MAP_EXECUTABLE | MAP_32BIT,
209                                 fd_offset);
210
211                 if (error != N_TXTADDR(ex))
212                         return error;
213
214                 error = vm_mmap(bprm->file, N_DATADDR(ex), ex.a_data,
215                                 PROT_READ | PROT_WRITE | PROT_EXEC,
216                                 MAP_FIXED | MAP_PRIVATE | MAP_DENYWRITE |
217                                 MAP_EXECUTABLE | MAP_32BIT,
218                                 fd_offset + ex.a_text);
219                 if (error != N_DATADDR(ex))
220                         return error;
221         }
222
223 beyond_if:
224         error = set_brk(current->mm->start_brk, current->mm->brk);
225         if (error)
226                 return error;
227
228         set_binfmt(&aout_format);
229
230         current->mm->start_stack =
231                 (unsigned long)create_aout_tables((char __user *)bprm->p, bprm);
232         /* start thread */
233         loadsegment(fs, 0);
234         loadsegment(ds, __USER32_DS);
235         loadsegment(es, __USER32_DS);
236         load_gs_index(0);
237         (regs)->ip = ex.a_entry;
238         (regs)->sp = current->mm->start_stack;
239         (regs)->flags = 0x200;
240         (regs)->cs = __USER32_CS;
241         (regs)->ss = __USER32_DS;
242         regs->r8 = regs->r9 = regs->r10 = regs->r11 =
243         regs->r12 = regs->r13 = regs->r14 = regs->r15 = 0;
244         set_fs(USER_DS);
245         return 0;
246 }
247
248 static int load_aout_library(struct file *file)
249 {
250         unsigned long bss, start_addr, len, error;
251         int retval;
252         struct exec ex;
253         loff_t pos = 0;
254
255         retval = -ENOEXEC;
256         error = kernel_read(file, &ex, sizeof(ex), &pos);
257         if (error != sizeof(ex))
258                 goto out;
259
260         /* We come in here for the regular a.out style of shared libraries */
261         if ((N_MAGIC(ex) != ZMAGIC && N_MAGIC(ex) != QMAGIC) || N_TRSIZE(ex) ||
262             N_DRSIZE(ex) || ((ex.a_entry & 0xfff) && N_MAGIC(ex) == ZMAGIC) ||
263             i_size_read(file_inode(file)) <
264             ex.a_text+ex.a_data+N_SYMSIZE(ex)+N_TXTOFF(ex)) {
265                 goto out;
266         }
267
268         if (N_FLAGS(ex))
269                 goto out;
270
271         /* For  QMAGIC, the starting address is 0x20 into the page.  We mask
272            this off to get the starting address for the page */
273
274         start_addr =  ex.a_entry & 0xfffff000;
275
276         if ((N_TXTOFF(ex) & ~PAGE_MASK) != 0) {
277 #ifdef WARN_OLD
278                 static unsigned long error_time;
279                 if (time_after(jiffies, error_time + 5*HZ)) {
280                         printk(KERN_WARNING
281                                "N_TXTOFF is not page aligned. Please convert "
282                                "library: %pD\n",
283                                file);
284                         error_time = jiffies;
285                 }
286 #endif
287                 retval = vm_brk(start_addr, ex.a_text + ex.a_data + ex.a_bss);
288                 if (retval)
289                         goto out;
290
291                 read_code(file, start_addr, N_TXTOFF(ex),
292                           ex.a_text + ex.a_data);
293                 retval = 0;
294                 goto out;
295         }
296         /* Now use mmap to map the library into memory. */
297         error = vm_mmap(file, start_addr, ex.a_text + ex.a_data,
298                         PROT_READ | PROT_WRITE | PROT_EXEC,
299                         MAP_FIXED | MAP_PRIVATE | MAP_DENYWRITE | MAP_32BIT,
300                         N_TXTOFF(ex));
301         retval = error;
302         if (error != start_addr)
303                 goto out;
304
305         len = PAGE_ALIGN(ex.a_text + ex.a_data);
306         bss = ex.a_text + ex.a_data + ex.a_bss;
307         if (bss > len) {
308                 retval = vm_brk(start_addr + len, bss - len);
309                 if (retval)
310                         goto out;
311         }
312         retval = 0;
313 out:
314         return retval;
315 }
316
317 static int __init init_aout_binfmt(void)
318 {
319         register_binfmt(&aout_format);
320         return 0;
321 }
322
323 static void __exit exit_aout_binfmt(void)
324 {
325         unregister_binfmt(&aout_format);
326 }
327
328 module_init(init_aout_binfmt);
329 module_exit(exit_aout_binfmt);
330 MODULE_LICENSE("GPL");