1 /* 2 * Copyright (c) 1993, David Greenman 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 * SUCH DAMAGE. 25 * 26 * $Id$ 27 */ 28 29 #include "opt_rlimit.h" 30 31 #include <sys/param.h> 32 #include <sys/systm.h> 33 #include <sys/resourcevar.h> 34 #include <sys/exec.h> 35 #include <sys/mman.h> 36 #include <sys/imgact.h> 37 #include <sys/imgact_aout.h> 38 #include <sys/kernel.h> 39 #include <sys/proc.h> 40 #include <sys/sysent.h> 41 #include <sys/vnode.h> 42 43 #include <vm/vm.h> 44 #include <vm/vm_param.h> 45 #include <vm/vm_prot.h> 46 #include <sys/lock.h> 47 #include <vm/pmap.h> 48 #include <vm/vm_map.h> 49 #include <vm/vm_extern.h> 50 51 static int exec_aout_imgact __P((struct image_params *imgp)); 52 53 static int 54 exec_aout_imgact(imgp) 55 struct image_params *imgp; 56 { 57 const struct exec *a_out = (const struct exec *) imgp->image_header; 58 struct vmspace *vmspace = imgp->proc->p_vmspace; 59 vm_offset_t vmaddr; 60 unsigned long virtual_offset; 61 unsigned long file_offset; 62 unsigned long bss_size; 63 int error; 64 65 /* 66 * Linux and *BSD binaries look very much alike, 67 * only the machine id is different: 68 * 0x64 for Linux, 0x86 for *BSD, 0x00 for BSDI. 69 * NetBSD is in network byte order.. ugh. 70 */ 71 if (((a_out->a_magic >> 16) & 0xff) != 0x86 && 72 ((a_out->a_magic >> 16) & 0xff) != 0 && 73 ((((int)ntohl(a_out->a_magic)) >> 16) & 0xff) != 0x86) 74 return -1; 75 76 /* 77 * Set file/virtual offset based on a.out variant. 78 * We do two cases: host byte order and network byte order 79 * (for NetBSD compatibility) 80 */ 81 switch ((int)(a_out->a_magic & 0xffff)) { 82 case ZMAGIC: 83 virtual_offset = 0; 84 if (a_out->a_text) { 85 file_offset = PAGE_SIZE; 86 } else { 87 /* Bill's "screwball mode" */ 88 file_offset = 0; 89 } 90 break; 91 case QMAGIC: 92 virtual_offset = PAGE_SIZE; 93 file_offset = 0; 94 break; 95 default: 96 /* NetBSD compatibility */ 97 switch ((int)(ntohl(a_out->a_magic) & 0xffff)) { 98 case ZMAGIC: 99 case QMAGIC: 100 virtual_offset = PAGE_SIZE; 101 file_offset = 0; 102 break; 103 default: 104 return (-1); 105 } 106 } 107 108 bss_size = roundup(a_out->a_bss, PAGE_SIZE); 109 110 /* 111 * Check various fields in header for validity/bounds. 112 */ 113 if (/* entry point must lay with text region */ 114 a_out->a_entry < virtual_offset || 115 a_out->a_entry >= virtual_offset + a_out->a_text || 116 117 /* text and data size must each be page rounded */ 118 a_out->a_text & PAGE_MASK || a_out->a_data & PAGE_MASK) 119 return (-1); 120 121 /* text + data can't exceed file size */ 122 if (a_out->a_data + a_out->a_text > imgp->attr->va_size) 123 return (EFAULT); 124 125 /* 126 * text/data/bss must not exceed limits 127 */ 128 if (/* text can't exceed maximum text size */ 129 a_out->a_text > MAXTSIZ || 130 131 /* data + bss can't exceed maximum data size */ 132 a_out->a_data + bss_size > MAXDSIZ || 133 134 /* data + bss can't exceed rlimit */ 135 a_out->a_data + bss_size > 136 imgp->proc->p_rlimit[RLIMIT_DATA].rlim_cur) 137 return (ENOMEM); 138 139 /* copy in arguments and/or environment from old process */ 140 error = exec_extract_strings(imgp); 141 if (error) 142 return (error); 143 144 /* 145 * Destroy old process VM and create a new one (with a new stack) 146 */ 147 exec_new_vmspace(imgp); 148 149 /* 150 * Map text/data read/execute 151 */ 152 vmaddr = virtual_offset; 153 error = 154 vm_mmap(&vmspace->vm_map, /* map */ 155 &vmaddr, /* address */ 156 a_out->a_text + a_out->a_data, /* size */ 157 VM_PROT_READ | VM_PROT_EXECUTE, /* protection */ 158 VM_PROT_ALL, /* max protection */ 159 MAP_PRIVATE | MAP_FIXED, /* flags */ 160 (caddr_t)imgp->vp, /* vnode */ 161 file_offset); /* offset */ 162 if (error) 163 return (error); 164 165 /* 166 * allow writing of data 167 */ 168 vm_map_protect(&vmspace->vm_map, 169 vmaddr + a_out->a_text, 170 vmaddr + a_out->a_text + a_out->a_data, 171 VM_PROT_ALL, 172 FALSE); 173 174 if (bss_size != 0) { 175 /* 176 * Allocate demand-zeroed area for uninitialized data 177 * "bss" = 'block started by symbol' - named after the IBM 7090 178 * instruction of the same name. 179 */ 180 vmaddr = virtual_offset + a_out->a_text + a_out->a_data; 181 error = vm_map_find(&vmspace->vm_map, NULL, 0, &vmaddr, bss_size, FALSE, VM_PROT_ALL, VM_PROT_ALL, 0); 182 if (error) 183 return (error); 184 } 185 186 /* Fill in process VM information */ 187 vmspace->vm_tsize = a_out->a_text >> PAGE_SHIFT; 188 vmspace->vm_dsize = (a_out->a_data + bss_size) >> PAGE_SHIFT; 189 vmspace->vm_taddr = (caddr_t) virtual_offset; 190 vmspace->vm_daddr = (caddr_t) virtual_offset + a_out->a_text; 191 192 /* Fill in image_params */ 193 imgp->interpreted = 0; 194 imgp->entry_addr = a_out->a_entry; 195 196 imgp->proc->p_sysent = &aout_sysvec; 197 198 /* Indicate that this file should not be modified */ 199 imgp->vp->v_flag |= VTEXT; 200 201 return (0); 202 } 203 204 /* 205 * Tell kern_execve.c about it, with a little help from the linker. 206 * Since `const' objects end up in the text segment, TEXT_SET is the 207 * correct directive to use. 208 */ 209 static const struct execsw aout_execsw = { exec_aout_imgact, "a.out" }; 210 TEXT_SET(execsw_set, aout_execsw); 211