09c434b8a0047 (Thomas Gleixner 2019-05-19 13:08:20 +0100 1) // SPDX-License-Identifier: GPL-2.0-only
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2) /*
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3) * linux/fs/binfmt_aout.c
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 4) *
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 5) * Copyright (C) 1991, 1992, 1996 Linus Torvalds
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 6) */
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 7)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 8) #include <linux/module.h>
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 9)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 10) #include <linux/time.h>
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 11) #include <linux/kernel.h>
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 12) #include <linux/mm.h>
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 13) #include <linux/mman.h>
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 14) #include <linux/a.out.h>
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 15) #include <linux/errno.h>
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 16) #include <linux/signal.h>
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 17) #include <linux/string.h>
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 18) #include <linux/fs.h>
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 19) #include <linux/file.h>
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 20) #include <linux/stat.h>
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 21) #include <linux/fcntl.h>
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 22) #include <linux/ptrace.h>
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 23) #include <linux/user.h>
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 24) #include <linux/binfmts.h>
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 25) #include <linux/personality.h>
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 26) #include <linux/init.h>
088e7af73a962 (Daisuke HATAYAMA 2010-03-05 13:44:06 -0800 27) #include <linux/coredump.h>
5a0e3ad6af866 (Tejun Heo 2010-03-24 17:04:11 +0900 28) #include <linux/slab.h>
68db0cf106786 (Ingo Molnar 2017-02-08 18:51:37 +0100 29) #include <linux/sched/task_stack.h>
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 30)
7c0f6ba682b9c (Linus Torvalds 2016-12-24 11:46:01 -0800 31) #include <linux/uaccess.h>
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 32) #include <asm/cacheflush.h>
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 33)
71613c3b871c5 (Al Viro 2012-10-20 22:00:48 -0400 34) static int load_aout_binary(struct linux_binprm *);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 35) static int load_aout_library(struct file*);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 36)
046d662f48183 (Alex Kelly 2012-10-04 17:15:23 -0700 37) static struct linux_binfmt aout_format = {
046d662f48183 (Alex Kelly 2012-10-04 17:15:23 -0700 38) .module = THIS_MODULE,
046d662f48183 (Alex Kelly 2012-10-04 17:15:23 -0700 39) .load_binary = load_aout_binary,
046d662f48183 (Alex Kelly 2012-10-04 17:15:23 -0700 40) .load_shlib = load_aout_library,
046d662f48183 (Alex Kelly 2012-10-04 17:15:23 -0700 41) };
046d662f48183 (Alex Kelly 2012-10-04 17:15:23 -0700 42)
046d662f48183 (Alex Kelly 2012-10-04 17:15:23 -0700 43) #define BAD_ADDR(x) ((unsigned long)(x) >= TASK_SIZE)
046d662f48183 (Alex Kelly 2012-10-04 17:15:23 -0700 44)
046d662f48183 (Alex Kelly 2012-10-04 17:15:23 -0700 45) static int set_brk(unsigned long start, unsigned long end)
046d662f48183 (Alex Kelly 2012-10-04 17:15:23 -0700 46) {
046d662f48183 (Alex Kelly 2012-10-04 17:15:23 -0700 47) start = PAGE_ALIGN(start);
046d662f48183 (Alex Kelly 2012-10-04 17:15:23 -0700 48) end = PAGE_ALIGN(end);
5d22fc25d4fc8 (Linus Torvalds 2016-05-27 15:57:31 -0700 49) if (end > start)
5d22fc25d4fc8 (Linus Torvalds 2016-05-27 15:57:31 -0700 50) return vm_brk(start, end - start);
046d662f48183 (Alex Kelly 2012-10-04 17:15:23 -0700 51) return 0;
046d662f48183 (Alex Kelly 2012-10-04 17:15:23 -0700 52) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 53)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 54) /*
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 55) * create_aout_tables() parses the env- and arg-strings in new user
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 56) * memory and creates the pointer tables from them, and puts their
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 57) * addresses on the "stack", returning the new stack pointer value.
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 58) */
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 59) static unsigned long __user *create_aout_tables(char __user *p, struct linux_binprm * bprm)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 60) {
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 61) char __user * __user *argv;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 62) char __user * __user *envp;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 63) unsigned long __user *sp;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 64) int argc = bprm->argc;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 65) int envc = bprm->envc;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 66)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 67) sp = (void __user *)((-(unsigned long)sizeof(char *)) & (unsigned long) p);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 68) #ifdef __alpha__
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 69) /* whee.. test-programs are so much fun. */
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 70) put_user(0, --sp);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 71) put_user(0, --sp);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 72) if (bprm->loader) {
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 73) put_user(0, --sp);
17580d7f2f632 (Al Viro 2009-01-03 07:16:23 +0000 74) put_user(1003, --sp);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 75) put_user(bprm->loader, --sp);
17580d7f2f632 (Al Viro 2009-01-03 07:16:23 +0000 76) put_user(1002, --sp);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 77) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 78) put_user(bprm->exec, --sp);
17580d7f2f632 (Al Viro 2009-01-03 07:16:23 +0000 79) put_user(1001, --sp);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 80) #endif
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 81) sp -= envc+1;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 82) envp = (char __user * __user *) sp;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 83) sp -= argc+1;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 84) argv = (char __user * __user *) sp;
17580d7f2f632 (Al Viro 2009-01-03 07:16:23 +0000 85) #ifndef __alpha__
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 86) put_user((unsigned long) envp,--sp);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 87) put_user((unsigned long) argv,--sp);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 88) #endif
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 89) put_user(argc,--sp);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 90) current->mm->arg_start = (unsigned long) p;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 91) while (argc-->0) {
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 92) char c;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 93) put_user(p,argv++);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 94) do {
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 95) get_user(c,p++);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 96) } while (c);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 97) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 98) put_user(NULL,argv);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 99) current->mm->arg_end = current->mm->env_start = (unsigned long) p;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 100) while (envc-->0) {
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 101) char c;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 102) put_user(p,envp++);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 103) do {
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 104) get_user(c,p++);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 105) } while (c);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 106) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 107) put_user(NULL,envp);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 108) current->mm->env_end = (unsigned long) p;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 109) return sp;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 110) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 111)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 112) /*
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 113) * These are the functions used to load a.out style executables and shared
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 114) * libraries. There is no binary dependent code anywhere else.
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 115) */
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 116)
71613c3b871c5 (Al Viro 2012-10-20 22:00:48 -0400 117) static int load_aout_binary(struct linux_binprm * bprm)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 118) {
71613c3b871c5 (Al Viro 2012-10-20 22:00:48 -0400 119) struct pt_regs *regs = current_pt_regs();
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 120) struct exec ex;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 121) unsigned long error;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 122) unsigned long fd_offset;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 123) unsigned long rlim;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 124) int retval;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 125)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 126) ex = *((struct exec *) bprm->buf); /* exec-header */
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 127) if ((N_MAGIC(ex) != ZMAGIC && N_MAGIC(ex) != OMAGIC &&
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 128) N_MAGIC(ex) != QMAGIC && N_MAGIC(ex) != NMAGIC) ||
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 129) N_TRSIZE(ex) || N_DRSIZE(ex) ||
496ad9aa8ef44 (Al Viro 2013-01-23 17:07:38 -0500 130) i_size_read(file_inode(bprm->file)) < ex.a_text+ex.a_data+N_SYMSIZE(ex)+N_TXTOFF(ex)) {
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 131) return -ENOEXEC;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 132) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 133)
8454aeef6fea9 (Eugene Teo 2006-09-29 01:59:33 -0700 134) /*
8454aeef6fea9 (Eugene Teo 2006-09-29 01:59:33 -0700 135) * Requires a mmap handler. This prevents people from using a.out
8454aeef6fea9 (Eugene Teo 2006-09-29 01:59:33 -0700 136) * as part of an exploit attack against /proc-related vulnerabilities.
8454aeef6fea9 (Eugene Teo 2006-09-29 01:59:33 -0700 137) */
72c2d53192004 (Al Viro 2013-09-22 16:27:52 -0400 138) if (!bprm->file->f_op->mmap)
8454aeef6fea9 (Eugene Teo 2006-09-29 01:59:33 -0700 139) return -ENOEXEC;
8454aeef6fea9 (Eugene Teo 2006-09-29 01:59:33 -0700 140)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 141) fd_offset = N_TXTOFF(ex);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 142)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 143) /* Check initial limits. This avoids letting people circumvent
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 144) * size limits imposed on them by creating programs with large
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 145) * arrays in the data or bss.
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 146) */
d554ed895dc8f (Jiri Slaby 2010-03-05 13:42:42 -0800 147) rlim = rlimit(RLIMIT_DATA);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 148) if (rlim >= RLIM_INFINITY)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 149) rlim = ~0;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 150) if (ex.a_data + ex.a_bss > rlim)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 151) return -ENOMEM;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 152)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 153) /* Flush all traces of the currently running executable */
2388777a0a595 (Eric W. Biederman 2020-05-03 07:54:10 -0500 154) retval = begin_new_exec(bprm);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 155) if (retval)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 156) return retval;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 157)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 158) /* OK, This is the point of no return */
17580d7f2f632 (Al Viro 2009-01-03 07:16:23 +0000 159) #ifdef __alpha__
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 160) SET_AOUT_PERSONALITY(bprm, ex);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 161) #else
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 162) set_personality(PER_LINUX);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 163) #endif
221af7f87b974 (Linus Torvalds 2010-01-28 22:14:42 -0800 164) setup_new_exec(bprm);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 165)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 166) current->mm->end_code = ex.a_text +
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 167) (current->mm->start_code = N_TXTADDR(ex));
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 168) current->mm->end_data = ex.a_data +
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 169) (current->mm->start_data = N_DATADDR(ex));
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 170) current->mm->brk = ex.a_bss +
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 171) (current->mm->start_brk = N_BSSADDR(ex));
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 172)
6414fa6a15011 (Al Viro 2012-03-05 06:38:42 +0000 173) retval = setup_arg_pages(bprm, STACK_TOP, EXSTACK_DEFAULT);
19d860a140bea (Al Viro 2014-05-04 20:11:36 -0400 174) if (retval < 0)
6414fa6a15011 (Al Viro 2012-03-05 06:38:42 +0000 175) return retval;
6414fa6a15011 (Al Viro 2012-03-05 06:38:42 +0000 176)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 177)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 178) if (N_MAGIC(ex) == OMAGIC) {
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 179) unsigned long text_addr, map_size;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 180) loff_t pos;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 181)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 182) text_addr = N_TXTADDR(ex);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 183)
fe30af971d896 (Al Viro 2009-01-03 07:16:13 +0000 184) #ifdef __alpha__
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 185) pos = fd_offset;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 186) map_size = ex.a_text+ex.a_data + PAGE_SIZE - 1;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 187) #else
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 188) pos = 32;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 189) map_size = ex.a_text+ex.a_data;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 190) #endif
e4eb1ff61b323 (Linus Torvalds 2012-04-20 15:35:40 -0700 191) error = vm_brk(text_addr & PAGE_MASK, map_size);
5d22fc25d4fc8 (Linus Torvalds 2016-05-27 15:57:31 -0700 192) if (error)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 193) return error;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 194)
3dc20cb282ec0 (Al Viro 2013-04-13 20:31:37 -0400 195) error = read_code(bprm->file, text_addr, pos,
3dc20cb282ec0 (Al Viro 2013-04-13 20:31:37 -0400 196) ex.a_text+ex.a_data);
19d860a140bea (Al Viro 2014-05-04 20:11:36 -0400 197) if ((signed long)error < 0)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 198) return error;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 199) } else {
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 200) if ((ex.a_text & 0xfff || ex.a_data & 0xfff) &&
2e50b6ccdaaf0 (S.Çağlar Onur 2008-04-29 00:59:26 -0700 201) (N_MAGIC(ex) != NMAGIC) && printk_ratelimit())
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 202) {
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 203) printk(KERN_NOTICE "executable not page aligned\n");
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 204) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 205)
2e50b6ccdaaf0 (S.Çağlar Onur 2008-04-29 00:59:26 -0700 206) if ((fd_offset & ~PAGE_MASK) != 0 && printk_ratelimit())
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 207) {
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 208) printk(KERN_WARNING
a455589f181e6 (Al Viro 2014-10-21 20:11:25 -0400 209) "fd_offset is not page aligned. Please convert program: %pD\n",
a455589f181e6 (Al Viro 2014-10-21 20:11:25 -0400 210) bprm->file);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 211) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 212)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 213) if (!bprm->file->f_op->mmap||((fd_offset & ~PAGE_MASK) != 0)) {
864778b154233 (Michal Hocko 2016-05-23 16:25:36 -0700 214) error = vm_brk(N_TXTADDR(ex), ex.a_text+ex.a_data);
5d22fc25d4fc8 (Linus Torvalds 2016-05-27 15:57:31 -0700 215) if (error)
864778b154233 (Michal Hocko 2016-05-23 16:25:36 -0700 216) return error;
864778b154233 (Michal Hocko 2016-05-23 16:25:36 -0700 217)
3dc20cb282ec0 (Al Viro 2013-04-13 20:31:37 -0400 218) read_code(bprm->file, N_TXTADDR(ex), fd_offset,
3dc20cb282ec0 (Al Viro 2013-04-13 20:31:37 -0400 219) ex.a_text + ex.a_data);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 220) goto beyond_if;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 221) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 222)
6be5ceb02e98e (Linus Torvalds 2012-04-20 17:13:58 -0700 223) error = vm_mmap(bprm->file, N_TXTADDR(ex), ex.a_text,
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 224) PROT_READ | PROT_EXEC,
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 225) MAP_FIXED | MAP_PRIVATE | MAP_DENYWRITE | MAP_EXECUTABLE,
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 226) fd_offset);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 227)
19d860a140bea (Al Viro 2014-05-04 20:11:36 -0400 228) if (error != N_TXTADDR(ex))
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 229) return error;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 230)
6be5ceb02e98e (Linus Torvalds 2012-04-20 17:13:58 -0700 231) error = vm_mmap(bprm->file, N_DATADDR(ex), ex.a_data,
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 232) PROT_READ | PROT_WRITE | PROT_EXEC,
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 233) MAP_FIXED | MAP_PRIVATE | MAP_DENYWRITE | MAP_EXECUTABLE,
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 234) fd_offset + ex.a_text);
19d860a140bea (Al Viro 2014-05-04 20:11:36 -0400 235) if (error != N_DATADDR(ex))
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 236) return error;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 237) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 238) beyond_if:
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 239) set_binfmt(&aout_format);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 240)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 241) retval = set_brk(current->mm->start_brk, current->mm->brk);
19d860a140bea (Al Viro 2014-05-04 20:11:36 -0400 242) if (retval < 0)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 243) return retval;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 244)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 245) current->mm->start_stack =
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 246) (unsigned long) create_aout_tables((char __user *) bprm->p, bprm);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 247) #ifdef __alpha__
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 248) regs->gp = ex.a_gpvalue;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 249) #endif
b83838313386f (Kees Cook 2018-04-10 16:34:57 -0700 250) finalize_exec(bprm);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 251) start_thread(regs, ex.a_entry, current->mm->start_stack);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 252) return 0;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 253) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 254)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 255) static int load_aout_library(struct file *file)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 256) {
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 257) struct inode * inode;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 258) unsigned long bss, start_addr, len;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 259) unsigned long error;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 260) int retval;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 261) struct exec ex;
bdd1d2d3d251c (Christoph Hellwig 2017-09-01 17:39:13 +0200 262) loff_t pos = 0;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 263)
496ad9aa8ef44 (Al Viro 2013-01-23 17:07:38 -0500 264) inode = file_inode(file);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 265)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 266) retval = -ENOEXEC;
bdd1d2d3d251c (Christoph Hellwig 2017-09-01 17:39:13 +0200 267) error = kernel_read(file, &ex, sizeof(ex), &pos);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 268) if (error != sizeof(ex))
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 269) goto out;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 270)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 271) /* We come in here for the regular a.out style of shared libraries */
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 272) if ((N_MAGIC(ex) != ZMAGIC && N_MAGIC(ex) != QMAGIC) || N_TRSIZE(ex) ||
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 273) N_DRSIZE(ex) || ((ex.a_entry & 0xfff) && N_MAGIC(ex) == ZMAGIC) ||
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 274) i_size_read(inode) < ex.a_text+ex.a_data+N_SYMSIZE(ex)+N_TXTOFF(ex)) {
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 275) goto out;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 276) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 277)
8454aeef6fea9 (Eugene Teo 2006-09-29 01:59:33 -0700 278) /*
8454aeef6fea9 (Eugene Teo 2006-09-29 01:59:33 -0700 279) * Requires a mmap handler. This prevents people from using a.out
8454aeef6fea9 (Eugene Teo 2006-09-29 01:59:33 -0700 280) * as part of an exploit attack against /proc-related vulnerabilities.
8454aeef6fea9 (Eugene Teo 2006-09-29 01:59:33 -0700 281) */
72c2d53192004 (Al Viro 2013-09-22 16:27:52 -0400 282) if (!file->f_op->mmap)
8454aeef6fea9 (Eugene Teo 2006-09-29 01:59:33 -0700 283) goto out;
8454aeef6fea9 (Eugene Teo 2006-09-29 01:59:33 -0700 284)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 285) if (N_FLAGS(ex))
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 286) goto out;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 287)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 288) /* For QMAGIC, the starting address is 0x20 into the page. We mask
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 289) this off to get the starting address for the page */
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 290)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 291) start_addr = ex.a_entry & 0xfffff000;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 292)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 293) if ((N_TXTOFF(ex) & ~PAGE_MASK) != 0) {
2e50b6ccdaaf0 (S.Çağlar Onur 2008-04-29 00:59:26 -0700 294) if (printk_ratelimit())
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 295) {
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 296) printk(KERN_WARNING
a455589f181e6 (Al Viro 2014-10-21 20:11:25 -0400 297) "N_TXTOFF is not page aligned. Please convert library: %pD\n",
a455589f181e6 (Al Viro 2014-10-21 20:11:25 -0400 298) file);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 299) }
864778b154233 (Michal Hocko 2016-05-23 16:25:36 -0700 300) retval = vm_brk(start_addr, ex.a_text + ex.a_data + ex.a_bss);
5d22fc25d4fc8 (Linus Torvalds 2016-05-27 15:57:31 -0700 301) if (retval)
864778b154233 (Michal Hocko 2016-05-23 16:25:36 -0700 302) goto out;
864778b154233 (Michal Hocko 2016-05-23 16:25:36 -0700 303)
3dc20cb282ec0 (Al Viro 2013-04-13 20:31:37 -0400 304) read_code(file, start_addr, N_TXTOFF(ex),
3dc20cb282ec0 (Al Viro 2013-04-13 20:31:37 -0400 305) ex.a_text + ex.a_data);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 306) retval = 0;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 307) goto out;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 308) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 309) /* Now use mmap to map the library into memory. */
6be5ceb02e98e (Linus Torvalds 2012-04-20 17:13:58 -0700 310) error = vm_mmap(file, start_addr, ex.a_text + ex.a_data,
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 311) PROT_READ | PROT_WRITE | PROT_EXEC,
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 312) MAP_FIXED | MAP_PRIVATE | MAP_DENYWRITE,
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 313) N_TXTOFF(ex));
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 314) retval = error;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 315) if (error != start_addr)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 316) goto out;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 317)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 318) len = PAGE_ALIGN(ex.a_text + ex.a_data);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 319) bss = ex.a_text + ex.a_data + ex.a_bss;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 320) if (bss > len) {
5d22fc25d4fc8 (Linus Torvalds 2016-05-27 15:57:31 -0700 321) retval = vm_brk(start_addr + len, bss - len);
5d22fc25d4fc8 (Linus Torvalds 2016-05-27 15:57:31 -0700 322) if (retval)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 323) goto out;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 324) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 325) retval = 0;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 326) out:
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 327) return retval;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 328) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 329)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 330) static int __init init_aout_binfmt(void)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 331) {
8fc3dc5a3a17a (Al Viro 2012-03-17 03:05:16 -0400 332) register_binfmt(&aout_format);
8fc3dc5a3a17a (Al Viro 2012-03-17 03:05:16 -0400 333) return 0;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 334) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 335)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 336) static void __exit exit_aout_binfmt(void)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 337) {
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 338) unregister_binfmt(&aout_format);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 339) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 340)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 341) core_initcall(init_aout_binfmt);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 342) module_exit(exit_aout_binfmt);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 343) MODULE_LICENSE("GPL");