1 /* $OpenBSD: exec_subr.c,v 1.42 2014/11/18 02:37:31 tedu Exp $ */ 2 /* $NetBSD: exec_subr.c,v 1.9 1994/12/04 03:10:42 mycroft Exp $ */ 3 4 /* 5 * Copyright (c) 1993, 1994 Christopher G. Demetriou 6 * All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 3. All advertising materials mentioning features or use of this software 17 * must display the following acknowledgement: 18 * This product includes software developed by Christopher G. Demetriou. 19 * 4. The name of the author may not be used to endorse or promote products 20 * derived from this software without specific prior written permission 21 * 22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 23 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 24 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 25 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 27 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 31 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 32 */ 33 34 #include <sys/param.h> 35 #include <sys/systm.h> 36 #include <sys/proc.h> 37 #include <sys/malloc.h> 38 #include <sys/vnode.h> 39 #include <sys/filedesc.h> 40 #include <sys/exec.h> 41 #include <sys/mman.h> 42 #include <sys/resourcevar.h> 43 44 #ifdef DEBUG 45 /* 46 * new_vmcmd(): 47 * create a new vmcmd structure and fill in its fields based 48 * on function call arguments. make sure objects ref'd by 49 * the vmcmd are 'held'. 50 * 51 * If not debugging, this is a macro, so it's expanded inline. 52 */ 53 54 void 55 new_vmcmd(struct exec_vmcmd_set *evsp, 56 int (*proc)(struct proc *, struct exec_vmcmd *), u_long len, u_long addr, 57 struct vnode *vp, u_long offset, u_int prot, int flags) 58 { 59 struct exec_vmcmd *vcp; 60 61 if (evsp->evs_used >= evsp->evs_cnt) 62 vmcmdset_extend(evsp); 63 vcp = &evsp->evs_cmds[evsp->evs_used++]; 64 vcp->ev_proc = proc; 65 vcp->ev_len = len; 66 vcp->ev_addr = addr; 67 if ((vcp->ev_vp = vp) != NULL) 68 vref(vp); 69 vcp->ev_offset = offset; 70 vcp->ev_prot = prot; 71 vcp->ev_flags = flags; 72 } 73 #endif /* DEBUG */ 74 75 void 76 vmcmdset_extend(struct exec_vmcmd_set *evsp) 77 { 78 struct exec_vmcmd *nvcp; 79 u_int ocnt; 80 81 #ifdef DIAGNOSTIC 82 if (evsp->evs_used < evsp->evs_cnt) 83 panic("vmcmdset_extend: not necessary"); 84 #endif 85 86 ocnt = evsp->evs_cnt; 87 KASSERT(ocnt > 0); 88 /* figure out number of entries in new set */ 89 evsp->evs_cnt += ocnt; 90 91 /* reallocate the command set */ 92 nvcp = mallocarray(evsp->evs_cnt, sizeof(*nvcp), M_EXEC, 93 M_WAITOK); 94 bcopy(evsp->evs_cmds, nvcp, (ocnt * sizeof(*nvcp))); 95 if (evsp->evs_cmds != evsp->evs_start) 96 free(evsp->evs_cmds, M_EXEC, ocnt * sizeof(*nvcp)); 97 evsp->evs_cmds = nvcp; 98 } 99 100 void 101 kill_vmcmds(struct exec_vmcmd_set *evsp) 102 { 103 struct exec_vmcmd *vcp; 104 int i; 105 106 for (i = 0; i < evsp->evs_used; i++) { 107 vcp = &evsp->evs_cmds[i]; 108 if (vcp->ev_vp != NULLVP) 109 vrele(vcp->ev_vp); 110 } 111 112 /* 113 * Free old vmcmds and reset the array. 114 */ 115 evsp->evs_used = 0; 116 if (evsp->evs_cmds != evsp->evs_start) 117 free(evsp->evs_cmds, M_EXEC, 0); 118 evsp->evs_cmds = evsp->evs_start; 119 evsp->evs_cnt = EXEC_DEFAULT_VMCMD_SETSIZE; 120 } 121 122 int 123 exec_process_vmcmds(struct proc *p, struct exec_package *epp) 124 { 125 struct exec_vmcmd *base_vc = NULL; 126 int error = 0; 127 int i; 128 129 for (i = 0; i < epp->ep_vmcmds.evs_used && !error; i++) { 130 struct exec_vmcmd *vcp; 131 132 vcp = &epp->ep_vmcmds.evs_cmds[i]; 133 134 if (vcp->ev_flags & VMCMD_RELATIVE) { 135 #ifdef DIAGNOSTIC 136 if (base_vc == NULL) 137 panic("exec_process_vmcmds: RELATIVE no base"); 138 #endif 139 vcp->ev_addr += base_vc->ev_addr; 140 } 141 error = (*vcp->ev_proc)(p, vcp); 142 if (vcp->ev_flags & VMCMD_BASE) { 143 base_vc = vcp; 144 } 145 } 146 147 kill_vmcmds(&epp->ep_vmcmds); 148 149 return (error); 150 } 151 152 /* 153 * vmcmd_map_pagedvn(): 154 * handle vmcmd which specifies that a vnode should be mmap'd. 155 * appropriate for handling demand-paged text and data segments. 156 */ 157 158 int 159 vmcmd_map_pagedvn(struct proc *p, struct exec_vmcmd *cmd) 160 { 161 /* 162 * note that if you're going to map part of a process as being 163 * paged from a vnode, that vnode had damn well better be marked as 164 * VTEXT. that's handled in the routine which sets up the vmcmd to 165 * call this routine. 166 */ 167 struct uvm_object *uobj; 168 int error; 169 170 /* 171 * map the vnode in using uvm_map. 172 */ 173 174 if (cmd->ev_len == 0) 175 return (0); 176 if (cmd->ev_offset & PAGE_MASK) 177 return (EINVAL); 178 if (cmd->ev_addr & PAGE_MASK) 179 return (EINVAL); 180 if (cmd->ev_len & PAGE_MASK) 181 return (EINVAL); 182 183 /* 184 * first, attach to the object 185 */ 186 187 uobj = uvn_attach(cmd->ev_vp, PROT_READ | PROT_EXEC); 188 if (uobj == NULL) 189 return (ENOMEM); 190 191 /* 192 * do the map 193 */ 194 195 error = uvm_map(&p->p_vmspace->vm_map, &cmd->ev_addr, cmd->ev_len, 196 uobj, cmd->ev_offset, 0, 197 UVM_MAPFLAG(cmd->ev_prot, PROT_MASK, UVM_INH_COPY, 198 POSIX_MADV_NORMAL, UVM_FLAG_COPYONW|UVM_FLAG_FIXED)); 199 200 /* 201 * check for error 202 */ 203 204 if (error) { 205 /* 206 * error: detach from object 207 */ 208 uobj->pgops->pgo_detach(uobj); 209 } 210 211 return (error); 212 } 213 214 /* 215 * vmcmd_map_readvn(): 216 * handle vmcmd which specifies that a vnode should be read from. 217 * appropriate for non-demand-paged text/data segments, i.e. impure 218 * objects (a la OMAGIC and NMAGIC). 219 */ 220 221 int 222 vmcmd_map_readvn(struct proc *p, struct exec_vmcmd *cmd) 223 { 224 int error; 225 vm_prot_t prot; 226 227 if (cmd->ev_len == 0) 228 return (0); 229 230 prot = cmd->ev_prot; 231 232 cmd->ev_addr = trunc_page(cmd->ev_addr); /* required by uvm_map */ 233 error = uvm_map(&p->p_vmspace->vm_map, &cmd->ev_addr, 234 round_page(cmd->ev_len), NULL, UVM_UNKNOWN_OFFSET, 0, 235 UVM_MAPFLAG(prot | PROT_WRITE, PROT_MASK, UVM_INH_COPY, 236 POSIX_MADV_NORMAL, 237 UVM_FLAG_FIXED|UVM_FLAG_OVERLAY|UVM_FLAG_COPYONW)); 238 239 if (error) 240 return (error); 241 242 error = vn_rdwr(UIO_READ, cmd->ev_vp, (caddr_t)cmd->ev_addr, 243 cmd->ev_len, cmd->ev_offset, UIO_USERSPACE, IO_UNIT, 244 p->p_ucred, NULL, p); 245 if (error) 246 return (error); 247 248 if ((prot & PROT_WRITE) == 0) { 249 /* 250 * we had to map in the area at PROT_WRITE so that vn_rdwr() 251 * could write to it. however, the caller seems to want 252 * it mapped read-only, so now we are going to have to call 253 * uvm_map_protect() to fix up the protection. ICK. 254 */ 255 return (uvm_map_protect(&p->p_vmspace->vm_map, 256 trunc_page(cmd->ev_addr), 257 round_page(cmd->ev_addr + cmd->ev_len), 258 prot, FALSE)); 259 } 260 return (0); 261 } 262 263 /* 264 * vmcmd_map_zero(): 265 * handle vmcmd which specifies a zero-filled address space region. 266 */ 267 268 int 269 vmcmd_map_zero(struct proc *p, struct exec_vmcmd *cmd) 270 { 271 if (cmd->ev_len == 0) 272 return (0); 273 274 cmd->ev_addr = trunc_page(cmd->ev_addr); /* required by uvm_map */ 275 return (uvm_map(&p->p_vmspace->vm_map, &cmd->ev_addr, 276 round_page(cmd->ev_len), NULL, UVM_UNKNOWN_OFFSET, 0, 277 UVM_MAPFLAG(cmd->ev_prot, PROT_MASK, UVM_INH_COPY, 278 POSIX_MADV_NORMAL, UVM_FLAG_FIXED|UVM_FLAG_COPYONW))); 279 } 280 281 /* 282 * vmcmd_randomize(): 283 * handle vmcmd which specifies a randomized address space region. 284 */ 285 286 int 287 vmcmd_randomize(struct proc *p, struct exec_vmcmd *cmd) 288 { 289 char *buf; 290 int error; 291 292 if (cmd->ev_len == 0) 293 return (0); 294 if (cmd->ev_len > 1024) 295 return (EINVAL); 296 297 buf = malloc(cmd->ev_len, M_TEMP, M_WAITOK); 298 arc4random_buf(buf, cmd->ev_len); 299 error = copyout(buf, (void *)cmd->ev_addr, cmd->ev_len); 300 free(buf, M_TEMP, cmd->ev_len); 301 302 return (error); 303 } 304 305 /* 306 * exec_setup_stack(): Set up the stack segment for an executable. 307 * 308 * Note that the ep_ssize parameter must be set to be the current stack 309 * limit; this is adjusted in the body of execve() to yield the 310 * appropriate stack segment usage once the argument length is 311 * calculated. 312 * 313 * This function returns an int for uniformity with other (future) formats' 314 * stack setup functions. They might have errors to return. 315 */ 316 317 int 318 exec_setup_stack(struct proc *p, struct exec_package *epp) 319 { 320 321 #ifdef MACHINE_STACK_GROWS_UP 322 epp->ep_maxsaddr = USRSTACK; 323 epp->ep_minsaddr = USRSTACK + MAXSSIZ; 324 #else 325 epp->ep_maxsaddr = USRSTACK - MAXSSIZ; 326 epp->ep_minsaddr = USRSTACK; 327 #endif 328 epp->ep_ssize = round_page(p->p_rlimit[RLIMIT_STACK].rlim_cur); 329 330 /* 331 * set up commands for stack. note that this takes *two*, one to 332 * map the part of the stack which we can access, and one to map 333 * the part which we can't. 334 * 335 * arguably, it could be made into one, but that would require the 336 * addition of another mapping proc, which is unnecessary 337 * 338 * note that in memory, things assumed to be: 0 ....... ep_maxsaddr 339 * <stack> ep_minsaddr 340 */ 341 #ifdef MACHINE_STACK_GROWS_UP 342 NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero, 343 ((epp->ep_minsaddr - epp->ep_ssize) - epp->ep_maxsaddr), 344 epp->ep_maxsaddr + epp->ep_ssize, NULLVP, 0, PROT_NONE); 345 NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero, epp->ep_ssize, 346 epp->ep_maxsaddr, NULLVP, 0, 347 PROT_READ | PROT_WRITE); 348 #else 349 NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero, 350 ((epp->ep_minsaddr - epp->ep_ssize) - epp->ep_maxsaddr), 351 epp->ep_maxsaddr, NULLVP, 0, PROT_NONE); 352 NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero, epp->ep_ssize, 353 (epp->ep_minsaddr - epp->ep_ssize), NULLVP, 0, 354 PROT_READ | PROT_WRITE); 355 #endif 356 357 return (0); 358 } 359