imgact_aout.c revision 103086
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 * $FreeBSD: head/sys/kern/imgact_aout.c 103086 2002-09-07 22:31:44Z peter $ 27 */ 28 29#include <sys/param.h> 30#include <sys/exec.h> 31#include <sys/fcntl.h> 32#include <sys/imgact.h> 33#include <sys/imgact_aout.h> 34#include <sys/kernel.h> 35#include <sys/lock.h> 36#include <sys/mutex.h> 37#include <sys/namei.h> 38#include <sys/pioctl.h> 39#include <sys/proc.h> 40#include <sys/resourcevar.h> 41#include <sys/systm.h> 42#include <sys/signalvar.h> 43#include <sys/stat.h> 44#include <sys/sysent.h> 45#include <sys/syscall.h> 46#include <sys/vnode.h> 47#include <sys/user.h> 48 49#include <machine/md_var.h> 50#include <machine/frame.h> 51 52#include <vm/vm.h> 53#include <vm/vm_param.h> 54#include <vm/pmap.h> 55#include <vm/vm_map.h> 56#include <vm/vm_object.h> 57 58static int exec_aout_imgact(struct image_params *imgp); 59static int aout_fixup(register_t **stack_base, struct image_params *imgp); 60 61struct sysentvec aout_sysvec = { 62 SYS_MAXSYSCALL, 63 sysent, 64 0, 65 0, 66 NULL, 67 0, 68 NULL, 69 NULL, 70 aout_fixup, 71 sendsig, 72 sigcode, 73 &szsigcode, 74 NULL, 75 "FreeBSD a.out", 76 aout_coredump, 77 NULL, 78 MINSIGSTKSZ, 79 PAGE_SIZE, 80 VM_MIN_ADDRESS, 81 VM_MAXUSER_ADDRESS, 82 USRSTACK, 83 PS_STRINGS, 84 VM_PROT_ALL, 85 exec_copyout_strings, 86 exec_setregs 87}; 88 89static int 90aout_fixup(stack_base, imgp) 91 register_t **stack_base; 92 struct image_params *imgp; 93{ 94 95 return (suword(--(*stack_base), imgp->argc)); 96} 97 98static int 99exec_aout_imgact(imgp) 100 struct image_params *imgp; 101{ 102 const struct exec *a_out = (const struct exec *) imgp->image_header; 103 struct vmspace *vmspace; 104 struct vnode *vp; 105 vm_map_t map; 106 vm_object_t object; 107 vm_offset_t text_end, data_end; 108 unsigned long virtual_offset; 109 unsigned long file_offset; 110 unsigned long bss_size; 111 int error; 112 113 GIANT_REQUIRED; 114 115 /* 116 * Linux and *BSD binaries look very much alike, 117 * only the machine id is different: 118 * 0x64 for Linux, 0x86 for *BSD, 0x00 for BSDI. 119 * NetBSD is in network byte order.. ugh. 120 */ 121 if (((a_out->a_magic >> 16) & 0xff) != 0x86 && 122 ((a_out->a_magic >> 16) & 0xff) != 0 && 123 ((((int)ntohl(a_out->a_magic)) >> 16) & 0xff) != 0x86) 124 return -1; 125 126 /* 127 * Set file/virtual offset based on a.out variant. 128 * We do two cases: host byte order and network byte order 129 * (for NetBSD compatibility) 130 */ 131 switch ((int)(a_out->a_magic & 0xffff)) { 132 case ZMAGIC: 133 virtual_offset = 0; 134 if (a_out->a_text) { 135 file_offset = PAGE_SIZE; 136 } else { 137 /* Bill's "screwball mode" */ 138 file_offset = 0; 139 } 140 break; 141 case QMAGIC: 142 virtual_offset = PAGE_SIZE; 143 file_offset = 0; 144 /* Pass PS_STRINGS for BSD/OS binaries only. */ 145 if (N_GETMID(*a_out) == MID_ZERO) 146 imgp->ps_strings = PS_STRINGS; 147 break; 148 default: 149 /* NetBSD compatibility */ 150 switch ((int)(ntohl(a_out->a_magic) & 0xffff)) { 151 case ZMAGIC: 152 case QMAGIC: 153 virtual_offset = PAGE_SIZE; 154 file_offset = 0; 155 break; 156 default: 157 return (-1); 158 } 159 } 160 161 bss_size = roundup(a_out->a_bss, PAGE_SIZE); 162 163 /* 164 * Check various fields in header for validity/bounds. 165 */ 166 if (/* entry point must lay with text region */ 167 a_out->a_entry < virtual_offset || 168 a_out->a_entry >= virtual_offset + a_out->a_text || 169 170 /* text and data size must each be page rounded */ 171 a_out->a_text & PAGE_MASK || a_out->a_data & PAGE_MASK) 172 return (-1); 173 174 /* text + data can't exceed file size */ 175 if (a_out->a_data + a_out->a_text > imgp->attr->va_size) 176 return (EFAULT); 177 178 /* 179 * text/data/bss must not exceed limits 180 */ 181 mtx_assert(&Giant, MA_OWNED); 182 if (/* text can't exceed maximum text size */ 183 a_out->a_text > maxtsiz || 184 185 /* data + bss can't exceed rlimit */ 186 a_out->a_data + bss_size > 187 imgp->proc->p_rlimit[RLIMIT_DATA].rlim_cur) 188 return (ENOMEM); 189 190 /* copy in arguments and/or environment from old process */ 191 error = exec_extract_strings(imgp); 192 if (error) 193 return (error); 194 195 /* 196 * Destroy old process VM and create a new one (with a new stack) 197 */ 198 exec_new_vmspace(imgp, VM_MIN_ADDRESS, VM_MAXUSER_ADDRESS, USRSTACK); 199 200 /* 201 * The vm space can be changed by exec_new_vmspace 202 */ 203 vmspace = imgp->proc->p_vmspace; 204 205 vp = imgp->vp; 206 object = imgp->object; 207 map = &vmspace->vm_map; 208 vm_map_lock(map); 209 vm_object_reference(object); 210 211 text_end = virtual_offset + a_out->a_text; 212 error = vm_map_insert(map, object, 213 file_offset, 214 virtual_offset, text_end, 215 VM_PROT_READ | VM_PROT_EXECUTE, VM_PROT_ALL, 216 MAP_COPY_ON_WRITE | MAP_PREFAULT); 217 if (error) { 218 vm_map_unlock(map); 219 return (error); 220 } 221 data_end = text_end + a_out->a_data; 222 if (a_out->a_data) { 223 vm_object_reference(object); 224 error = vm_map_insert(map, object, 225 file_offset + a_out->a_text, 226 text_end, data_end, 227 VM_PROT_ALL, VM_PROT_ALL, 228 MAP_COPY_ON_WRITE | MAP_PREFAULT); 229 if (error) { 230 vm_map_unlock(map); 231 return (error); 232 } 233 } 234 235 if (bss_size) { 236 error = vm_map_insert(map, NULL, 0, 237 data_end, data_end + bss_size, 238 VM_PROT_ALL, VM_PROT_ALL, 0); 239 if (error) { 240 vm_map_unlock(map); 241 return (error); 242 } 243 } 244 vm_map_unlock(map); 245 246 /* Fill in process VM information */ 247 vmspace->vm_tsize = a_out->a_text >> PAGE_SHIFT; 248 vmspace->vm_dsize = (a_out->a_data + bss_size) >> PAGE_SHIFT; 249 vmspace->vm_taddr = (caddr_t) (uintptr_t) virtual_offset; 250 vmspace->vm_daddr = (caddr_t) (uintptr_t) 251 (virtual_offset + a_out->a_text); 252 253 /* Fill in image_params */ 254 imgp->interpreted = 0; 255 imgp->entry_addr = a_out->a_entry; 256 257 imgp->proc->p_sysent = &aout_sysvec; 258 259 return (0); 260} 261 262/* 263 * Dump core, into a file named as described in the comments for 264 * expand_name(), unless the process was setuid/setgid. 265 */ 266int 267aout_coredump(td, vp, limit) 268 register struct thread *td; 269 register struct vnode *vp; 270 off_t limit; 271{ 272 struct proc *p = td->td_proc; 273 register struct ucred *cred = td->td_ucred; 274 register struct vmspace *vm = p->p_vmspace; 275 char *tempuser; 276 int error; 277 278 if (ctob((uarea_pages + kstack_pages) 279 + vm->vm_dsize + vm->vm_ssize) >= limit) 280 return (EFAULT); 281 tempuser = malloc(ctob(uarea_pages + kstack_pages), M_TEMP, 282 M_WAITOK | M_ZERO); 283 if (tempuser == NULL) 284 return (ENOMEM); 285 PROC_LOCK(p); 286 fill_kinfo_proc(p, &p->p_uarea->u_kproc); 287 PROC_UNLOCK(p); 288 bcopy(p->p_uarea, tempuser, sizeof(struct user)); 289 bcopy(td->td_frame, 290 tempuser + ctob(uarea_pages) + 291 ((caddr_t)td->td_frame - (caddr_t)td->td_kstack), 292 sizeof(struct trapframe)); 293 error = vn_rdwr(UIO_WRITE, vp, (caddr_t)tempuser, 294 ctob(uarea_pages + kstack_pages), 295 (off_t)0, UIO_SYSSPACE, IO_UNIT, cred, NOCRED, 296 (int *)NULL, td); 297 free(tempuser, M_TEMP); 298 if (error == 0) 299 error = vn_rdwr(UIO_WRITE, vp, vm->vm_daddr, 300 (int)ctob(vm->vm_dsize), 301 (off_t)ctob(uarea_pages + kstack_pages), UIO_USERSPACE, 302 IO_UNIT | IO_DIRECT, cred, NOCRED, (int *) NULL, td); 303 if (error == 0) 304 error = vn_rdwr_inchunks(UIO_WRITE, vp, 305 (caddr_t)trunc_page(USRSTACK - ctob(vm->vm_ssize)), 306 round_page(ctob(vm->vm_ssize)), 307 (off_t)ctob(uarea_pages + kstack_pages) + 308 ctob(vm->vm_dsize), UIO_USERSPACE, 309 IO_UNIT | IO_DIRECT, cred, NOCRED, (int *) NULL, td); 310 return (error); 311} 312 313/* 314 * Tell kern_execve.c about it, with a little help from the linker. 315 */ 316static struct execsw aout_execsw = { exec_aout_imgact, "a.out" }; 317EXEC_SET(aout, aout_execsw); 318