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/slab.h> 25#include <linux/binfmts.h> 26#include <linux/personality.h> 27#include <linux/init.h> 28 29#include <asm/system.h> 30#include <asm/uaccess.h> 31#include <asm/pgalloc.h> 32#include <asm/cacheflush.h> 33#include <asm/user32.h> 34#include <asm/ia32.h> 35 36#undef WARN_OLD 37#undef CORE_DUMP /* probably broken */ 38 39static int load_aout_binary(struct linux_binprm *, struct pt_regs * regs); 40static int load_aout_library(struct file*); 41 42#ifdef CORE_DUMP 43static int aout_core_dump(long signr, struct pt_regs * regs, struct file *file); 44 45/* 46 * fill in the user structure for a core dump.. 47 */ 48static void dump_thread32(struct pt_regs * regs, struct user32 * dump) 49{ 50 u32 fs,gs; 51 52/* changed the size calculations - should hopefully work better. lbt */ 53 dump->magic = CMAGIC; 54 dump->start_code = 0; 55 dump->start_stack = regs->rsp & ~(PAGE_SIZE - 1); 56 dump->u_tsize = ((unsigned long) current->mm->end_code) >> PAGE_SHIFT; 57 dump->u_dsize = ((unsigned long) (current->mm->brk + (PAGE_SIZE-1))) >> PAGE_SHIFT; 58 dump->u_dsize -= dump->u_tsize; 59 dump->u_ssize = 0; 60 dump->u_debugreg[0] = current->thread.debugreg0; 61 dump->u_debugreg[1] = current->thread.debugreg1; 62 dump->u_debugreg[2] = current->thread.debugreg2; 63 dump->u_debugreg[3] = current->thread.debugreg3; 64 dump->u_debugreg[4] = 0; 65 dump->u_debugreg[5] = 0; 66 dump->u_debugreg[6] = current->thread.debugreg6; 67 dump->u_debugreg[7] = current->thread.debugreg7; 68 69 if (dump->start_stack < 0xc0000000) 70 dump->u_ssize = ((unsigned long) (0xc0000000 - dump->start_stack)) >> PAGE_SHIFT; 71 72 dump->regs.ebx = regs->rbx; 73 dump->regs.ecx = regs->rcx; 74 dump->regs.edx = regs->rdx; 75 dump->regs.esi = regs->rsi; 76 dump->regs.edi = regs->rdi; 77 dump->regs.ebp = regs->rbp; 78 dump->regs.eax = regs->rax; 79 dump->regs.ds = current->thread.ds; 80 dump->regs.es = current->thread.es; 81 asm("movl %%fs,%0" : "=r" (fs)); dump->regs.fs = fs; 82 asm("movl %%gs,%0" : "=r" (gs)); dump->regs.gs = gs; 83 dump->regs.orig_eax = regs->orig_rax; 84 dump->regs.eip = regs->rip; 85 dump->regs.cs = regs->cs; 86 dump->regs.eflags = regs->eflags; 87 dump->regs.esp = regs->rsp; 88 dump->regs.ss = regs->ss; 89 90 dump->u_fpvalid = 0; 91} 92 93#endif 94 95static struct linux_binfmt aout_format = { 96 .module = THIS_MODULE, 97 .load_binary = load_aout_binary, 98 .load_shlib = load_aout_library, 99#ifdef CORE_DUMP 100 .core_dump = aout_core_dump, 101#endif 102 .min_coredump = PAGE_SIZE 103}; 104 105static void set_brk(unsigned long start, unsigned long end) 106{ 107 start = PAGE_ALIGN(start); 108 end = PAGE_ALIGN(end); 109 if (end <= start) 110 return; 111 down_write(¤t->mm->mmap_sem); 112 do_brk(start, end - start); 113 up_write(¤t->mm->mmap_sem); 114} 115 116#ifdef CORE_DUMP 117/* 118 * These are the only things you should do on a core-file: use only these 119 * macros to write out all the necessary info. 120 */ 121 122static int dump_write(struct file *file, const void *addr, int nr) 123{ 124 return file->f_op->write(file, addr, nr, &file->f_pos) == nr; 125} 126 127#define DUMP_WRITE(addr, nr) \ 128 if (!dump_write(file, (void *)(addr), (nr))) \ 129 goto end_coredump; 130 131#define DUMP_SEEK(offset) \ 132if (file->f_op->llseek) { \ 133 if (file->f_op->llseek(file,(offset),0) != (offset)) \ 134 goto end_coredump; \ 135} else file->f_pos = (offset) 136 137/* 138 * Routine writes a core dump image in the current directory. 139 * Currently only a stub-function. 140 * 141 * Note that setuid/setgid files won't make a core-dump if the uid/gid 142 * changed due to the set[u|g]id. It's enforced by the "current->mm->dumpable" 143 * field, which also makes sure the core-dumps won't be recursive if the 144 * dumping of the process results in another error.. 145 */ 146 147static int aout_core_dump(long signr, struct pt_regs * regs, struct file *file) 148{ 149 mm_segment_t fs; 150 int has_dumped = 0; 151 unsigned long dump_start, dump_size; 152 struct user32 dump; 153# define START_DATA(u) (u.u_tsize << PAGE_SHIFT) 154# define START_STACK(u) (u.start_stack) 155 156 fs = get_fs(); 157 set_fs(KERNEL_DS); 158 has_dumped = 1; 159 current->flags |= PF_DUMPCORE; 160 strncpy(dump.u_comm, current->comm, sizeof(current->comm)); 161 dump.u_ar0 = (u32)(((unsigned long)(&dump.regs)) - ((unsigned long)(&dump))); 162 dump.signal = signr; 163 dump_thread32(regs, &dump); 164 165/* If the size of the dump file exceeds the rlimit, then see what would happen 166 if we wrote the stack, but not the data area. */ 167 if ((dump.u_dsize+dump.u_ssize+1) * PAGE_SIZE > 168 current->signal->rlim[RLIMIT_CORE].rlim_cur) 169 dump.u_dsize = 0; 170 171/* Make sure we have enough room to write the stack and data areas. */ 172 if ((dump.u_ssize+1) * PAGE_SIZE > 173 current->signal->rlim[RLIMIT_CORE].rlim_cur) 174 dump.u_ssize = 0; 175 176/* make sure we actually have a data and stack area to dump */ 177 set_fs(USER_DS); 178 if (!access_ok(VERIFY_READ, (void *) (unsigned long)START_DATA(dump), dump.u_dsize << PAGE_SHIFT)) 179 dump.u_dsize = 0; 180 if (!access_ok(VERIFY_READ, (void *) (unsigned long)START_STACK(dump), dump.u_ssize << PAGE_SHIFT)) 181 dump.u_ssize = 0; 182 183 set_fs(KERNEL_DS); 184/* struct user */ 185 DUMP_WRITE(&dump,sizeof(dump)); 186/* Now dump all of the user data. Include malloced stuff as well */ 187 DUMP_SEEK(PAGE_SIZE); 188/* now we start writing out the user space info */ 189 set_fs(USER_DS); 190/* Dump the data area */ 191 if (dump.u_dsize != 0) { 192 dump_start = START_DATA(dump); 193 dump_size = dump.u_dsize << PAGE_SHIFT; 194 DUMP_WRITE(dump_start,dump_size); 195 } 196/* Now prepare to dump the stack area */ 197 if (dump.u_ssize != 0) { 198 dump_start = START_STACK(dump); 199 dump_size = dump.u_ssize << PAGE_SHIFT; 200 DUMP_WRITE(dump_start,dump_size); 201 } 202/* Finally dump the task struct. Not be used by gdb, but could be useful */ 203 set_fs(KERNEL_DS); 204 DUMP_WRITE(current,sizeof(*current)); 205end_coredump: 206 set_fs(fs); 207 return has_dumped; 208} 209#endif 210 211/* 212 * create_aout_tables() parses the env- and arg-strings in new user 213 * memory and creates the pointer tables from them, and puts their 214 * addresses on the "stack", returning the new stack pointer value. 215 */ 216static u32 __user *create_aout_tables(char __user *p, struct linux_binprm *bprm) 217{ 218 u32 __user *argv; 219 u32 __user *envp; 220 u32 __user *sp; 221 int argc = bprm->argc; 222 int envc = bprm->envc; 223 224 sp = (u32 __user *) ((-(unsigned long)sizeof(u32)) & (unsigned long) p); 225 sp -= envc+1; 226 envp = sp; 227 sp -= argc+1; 228 argv = sp; 229 put_user((unsigned long) envp,--sp); 230 put_user((unsigned long) argv,--sp); 231 put_user(argc,--sp); 232 current->mm->arg_start = (unsigned long) p; 233 while (argc-->0) { 234 char c; 235 put_user((u32)(unsigned long)p,argv++); 236 do { 237 get_user(c,p++); 238 } while (c); 239 } 240 put_user(0, argv); 241 current->mm->arg_end = current->mm->env_start = (unsigned long) p; 242 while (envc-->0) { 243 char c; 244 put_user((u32)(unsigned long)p,envp++); 245 do { 246 get_user(c,p++); 247 } while (c); 248 } 249 put_user(0, envp); 250 current->mm->env_end = (unsigned long) p; 251 return sp; 252} 253 254/* 255 * These are the functions used to load a.out style executables and shared 256 * libraries. There is no binary dependent code anywhere else. 257 */ 258 259static int load_aout_binary(struct linux_binprm * bprm, struct pt_regs * regs) 260{ 261 struct exec ex; 262 unsigned long error; 263 unsigned long fd_offset; 264 unsigned long rlim; 265 int retval; 266 267 ex = *((struct exec *) bprm->buf); /* exec-header */ 268 if ((N_MAGIC(ex) != ZMAGIC && N_MAGIC(ex) != OMAGIC && 269 N_MAGIC(ex) != QMAGIC && N_MAGIC(ex) != NMAGIC) || 270 N_TRSIZE(ex) || N_DRSIZE(ex) || 271 i_size_read(bprm->file->f_path.dentry->d_inode) < ex.a_text+ex.a_data+N_SYMSIZE(ex)+N_TXTOFF(ex)) { 272 return -ENOEXEC; 273 } 274 275 fd_offset = N_TXTOFF(ex); 276 277 /* Check initial limits. This avoids letting people circumvent 278 * size limits imposed on them by creating programs with large 279 * arrays in the data or bss. 280 */ 281 rlim = current->signal->rlim[RLIMIT_DATA].rlim_cur; 282 if (rlim >= RLIM_INFINITY) 283 rlim = ~0; 284 if (ex.a_data + ex.a_bss > rlim) 285 return -ENOMEM; 286 287 /* Flush all traces of the currently running executable */ 288 retval = flush_old_exec(bprm); 289 if (retval) 290 return retval; 291 292 regs->cs = __USER32_CS; 293 regs->r8 = regs->r9 = regs->r10 = regs->r11 = regs->r12 = 294 regs->r13 = regs->r14 = regs->r15 = 0; 295 296 /* OK, This is the point of no return */ 297 set_personality(PER_LINUX); 298 set_thread_flag(TIF_IA32); 299 clear_thread_flag(TIF_ABI_PENDING); 300 301 current->mm->end_code = ex.a_text + 302 (current->mm->start_code = N_TXTADDR(ex)); 303 current->mm->end_data = ex.a_data + 304 (current->mm->start_data = N_DATADDR(ex)); 305 current->mm->brk = ex.a_bss + 306 (current->mm->start_brk = N_BSSADDR(ex)); 307 current->mm->free_area_cache = TASK_UNMAPPED_BASE; 308 current->mm->cached_hole_size = 0; 309 310 current->mm->mmap = NULL; 311 compute_creds(bprm); 312 current->flags &= ~PF_FORKNOEXEC; 313 314 if (N_MAGIC(ex) == OMAGIC) { 315 unsigned long text_addr, map_size; 316 loff_t pos; 317 318 text_addr = N_TXTADDR(ex); 319 320 pos = 32; 321 map_size = ex.a_text+ex.a_data; 322 323 down_write(¤t->mm->mmap_sem); 324 error = do_brk(text_addr & PAGE_MASK, map_size); 325 up_write(¤t->mm->mmap_sem); 326 327 if (error != (text_addr & PAGE_MASK)) { 328 send_sig(SIGKILL, current, 0); 329 return error; 330 } 331 332 error = bprm->file->f_op->read(bprm->file, 333 (char __user *)text_addr, 334 ex.a_text+ex.a_data, &pos); 335 if ((signed long)error < 0) { 336 send_sig(SIGKILL, current, 0); 337 return error; 338 } 339 340 flush_icache_range(text_addr, text_addr+ex.a_text+ex.a_data); 341 } else { 342#ifdef WARN_OLD 343 static unsigned long error_time, error_time2; 344 if ((ex.a_text & 0xfff || ex.a_data & 0xfff) && 345 (N_MAGIC(ex) != NMAGIC) && (jiffies-error_time2) > 5*HZ) 346 { 347 printk(KERN_NOTICE "executable not page aligned\n"); 348 error_time2 = jiffies; 349 } 350 351 if ((fd_offset & ~PAGE_MASK) != 0 && 352 (jiffies-error_time) > 5*HZ) 353 { 354 printk(KERN_WARNING 355 "fd_offset is not page aligned. Please convert program: %s\n", 356 bprm->file->f_path.dentry->d_name.name); 357 error_time = jiffies; 358 } 359#endif 360 361 if (!bprm->file->f_op->mmap||((fd_offset & ~PAGE_MASK) != 0)) { 362 loff_t pos = fd_offset; 363 down_write(¤t->mm->mmap_sem); 364 do_brk(N_TXTADDR(ex), ex.a_text+ex.a_data); 365 up_write(¤t->mm->mmap_sem); 366 bprm->file->f_op->read(bprm->file, 367 (char __user *)N_TXTADDR(ex), 368 ex.a_text+ex.a_data, &pos); 369 flush_icache_range((unsigned long) N_TXTADDR(ex), 370 (unsigned long) N_TXTADDR(ex) + 371 ex.a_text+ex.a_data); 372 goto beyond_if; 373 } 374 375 down_write(¤t->mm->mmap_sem); 376 error = do_mmap(bprm->file, N_TXTADDR(ex), ex.a_text, 377 PROT_READ | PROT_EXEC, 378 MAP_FIXED | MAP_PRIVATE | MAP_DENYWRITE | MAP_EXECUTABLE | MAP_32BIT, 379 fd_offset); 380 up_write(¤t->mm->mmap_sem); 381 382 if (error != N_TXTADDR(ex)) { 383 send_sig(SIGKILL, current, 0); 384 return error; 385 } 386 387 down_write(¤t->mm->mmap_sem); 388 error = do_mmap(bprm->file, N_DATADDR(ex), ex.a_data, 389 PROT_READ | PROT_WRITE | PROT_EXEC, 390 MAP_FIXED | MAP_PRIVATE | MAP_DENYWRITE | MAP_EXECUTABLE | MAP_32BIT, 391 fd_offset + ex.a_text); 392 up_write(¤t->mm->mmap_sem); 393 if (error != N_DATADDR(ex)) { 394 send_sig(SIGKILL, current, 0); 395 return error; 396 } 397 } 398beyond_if: 399 set_binfmt(&aout_format); 400 401 set_brk(current->mm->start_brk, current->mm->brk); 402 403 retval = ia32_setup_arg_pages(bprm, IA32_STACK_TOP, EXSTACK_DEFAULT); 404 if (retval < 0) { 405 /* Someone check-me: is this error path enough? */ 406 send_sig(SIGKILL, current, 0); 407 return retval; 408 } 409 410 current->mm->start_stack = 411 (unsigned long)create_aout_tables((char __user *)bprm->p, bprm); 412 /* start thread */ 413 asm volatile("movl %0,%%fs" :: "r" (0)); \ 414 asm volatile("movl %0,%%es; movl %0,%%ds": :"r" (__USER32_DS)); 415 load_gs_index(0); 416 (regs)->rip = ex.a_entry; 417 (regs)->rsp = current->mm->start_stack; 418 (regs)->eflags = 0x200; 419 (regs)->cs = __USER32_CS; 420 (regs)->ss = __USER32_DS; 421 set_fs(USER_DS); 422 if (unlikely(current->ptrace & PT_PTRACED)) { 423 if (current->ptrace & PT_TRACE_EXEC) 424 ptrace_notify ((PTRACE_EVENT_EXEC << 8) | SIGTRAP); 425 else 426 send_sig(SIGTRAP, current, 0); 427 } 428 return 0; 429} 430 431static int load_aout_library(struct file *file) 432{ 433 struct inode * inode; 434 unsigned long bss, start_addr, len; 435 unsigned long error; 436 int retval; 437 struct exec ex; 438 439 inode = file->f_path.dentry->d_inode; 440 441 retval = -ENOEXEC; 442 error = kernel_read(file, 0, (char *) &ex, sizeof(ex)); 443 if (error != sizeof(ex)) 444 goto out; 445 446 /* We come in here for the regular a.out style of shared libraries */ 447 if ((N_MAGIC(ex) != ZMAGIC && N_MAGIC(ex) != QMAGIC) || N_TRSIZE(ex) || 448 N_DRSIZE(ex) || ((ex.a_entry & 0xfff) && N_MAGIC(ex) == ZMAGIC) || 449 i_size_read(inode) < ex.a_text+ex.a_data+N_SYMSIZE(ex)+N_TXTOFF(ex)) { 450 goto out; 451 } 452 453 if (N_FLAGS(ex)) 454 goto out; 455 456 /* For QMAGIC, the starting address is 0x20 into the page. We mask 457 this off to get the starting address for the page */ 458 459 start_addr = ex.a_entry & 0xfffff000; 460 461 if ((N_TXTOFF(ex) & ~PAGE_MASK) != 0) { 462 loff_t pos = N_TXTOFF(ex); 463 464#ifdef WARN_OLD 465 static unsigned long error_time; 466 if ((jiffies-error_time) > 5*HZ) 467 { 468 printk(KERN_WARNING 469 "N_TXTOFF is not page aligned. Please convert library: %s\n", 470 file->f_path.dentry->d_name.name); 471 error_time = jiffies; 472 } 473#endif 474 down_write(¤t->mm->mmap_sem); 475 do_brk(start_addr, ex.a_text + ex.a_data + ex.a_bss); 476 up_write(¤t->mm->mmap_sem); 477 478 file->f_op->read(file, (char __user *)start_addr, 479 ex.a_text + ex.a_data, &pos); 480 flush_icache_range((unsigned long) start_addr, 481 (unsigned long) start_addr + ex.a_text + ex.a_data); 482 483 retval = 0; 484 goto out; 485 } 486 /* Now use mmap to map the library into memory. */ 487 down_write(¤t->mm->mmap_sem); 488 error = do_mmap(file, start_addr, ex.a_text + ex.a_data, 489 PROT_READ | PROT_WRITE | PROT_EXEC, 490 MAP_FIXED | MAP_PRIVATE | MAP_DENYWRITE | MAP_32BIT, 491 N_TXTOFF(ex)); 492 up_write(¤t->mm->mmap_sem); 493 retval = error; 494 if (error != start_addr) 495 goto out; 496 497 len = PAGE_ALIGN(ex.a_text + ex.a_data); 498 bss = ex.a_text + ex.a_data + ex.a_bss; 499 if (bss > len) { 500 down_write(¤t->mm->mmap_sem); 501 error = do_brk(start_addr + len, bss - len); 502 up_write(¤t->mm->mmap_sem); 503 retval = error; 504 if (error != start_addr + len) 505 goto out; 506 } 507 retval = 0; 508out: 509 return retval; 510} 511 512static int __init init_aout_binfmt(void) 513{ 514 return register_binfmt(&aout_format); 515} 516 517static void __exit exit_aout_binfmt(void) 518{ 519 unregister_binfmt(&aout_format); 520} 521 522module_init(init_aout_binfmt); 523module_exit(exit_aout_binfmt); 524MODULE_LICENSE("GPL"); 525