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