1 /* $NetBSD: exec_subr.c,v 1.56 2007/11/26 19:02:00 pooka Exp $ */ 2 3 /* 4 * Copyright (c) 1993, 1994, 1996 Christopher G. Demetriou 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. All advertising materials mentioning features or use of this software 16 * must display the following acknowledgement: 17 * This product includes software developed by Christopher G. Demetriou. 18 * 4. The name of the author may not be used to endorse or promote products 19 * derived from this software without specific prior written permission 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 22 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 23 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 24 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 25 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 26 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 30 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 */ 32 33 #include <sys/cdefs.h> 34 __KERNEL_RCSID(0, "$NetBSD: exec_subr.c,v 1.56 2007/11/26 19:02:00 pooka Exp $"); 35 36 #include "opt_pax.h" 37 38 #include <sys/param.h> 39 #include <sys/systm.h> 40 #include <sys/proc.h> 41 #include <sys/malloc.h> 42 #include <sys/vnode.h> 43 #include <sys/filedesc.h> 44 #include <sys/exec.h> 45 #include <sys/mman.h> 46 #include <sys/resourcevar.h> 47 #include <sys/device.h> 48 49 #ifdef PAX_MPROTECT 50 #include <sys/pax.h> 51 #endif /* PAX_MPROTECT */ 52 53 #include <uvm/uvm.h> 54 55 #define VMCMD_EVCNT_DECL(name) \ 56 static struct evcnt vmcmd_ev_##name = \ 57 EVCNT_INITIALIZER(EVCNT_TYPE_MISC, NULL, "vmcmd", #name); \ 58 EVCNT_ATTACH_STATIC(vmcmd_ev_##name) 59 60 #define VMCMD_EVCNT_INCR(name) \ 61 vmcmd_ev_##name.ev_count++ 62 63 VMCMD_EVCNT_DECL(calls); 64 VMCMD_EVCNT_DECL(extends); 65 VMCMD_EVCNT_DECL(kills); 66 67 /* 68 * new_vmcmd(): 69 * create a new vmcmd structure and fill in its fields based 70 * on function call arguments. make sure objects ref'd by 71 * the vmcmd are 'held'. 72 */ 73 74 void 75 new_vmcmd(struct exec_vmcmd_set *evsp, 76 int (*proc)(struct lwp * l, struct exec_vmcmd *), 77 u_long len, u_long addr, struct vnode *vp, u_long offset, 78 u_int prot, int flags) 79 { 80 struct exec_vmcmd *vcp; 81 82 VMCMD_EVCNT_INCR(calls); 83 84 if (evsp->evs_used >= evsp->evs_cnt) 85 vmcmdset_extend(evsp); 86 vcp = &evsp->evs_cmds[evsp->evs_used++]; 87 vcp->ev_proc = proc; 88 vcp->ev_len = len; 89 vcp->ev_addr = addr; 90 if ((vcp->ev_vp = vp) != NULL) 91 vref(vp); 92 vcp->ev_offset = offset; 93 vcp->ev_prot = prot; 94 vcp->ev_flags = flags; 95 } 96 97 void 98 vmcmdset_extend(struct exec_vmcmd_set *evsp) 99 { 100 struct exec_vmcmd *nvcp; 101 u_int ocnt; 102 103 #ifdef DIAGNOSTIC 104 if (evsp->evs_used < evsp->evs_cnt) 105 panic("vmcmdset_extend: not necessary"); 106 #endif 107 108 /* figure out number of entries in new set */ 109 if ((ocnt = evsp->evs_cnt) != 0) { 110 evsp->evs_cnt += ocnt; 111 VMCMD_EVCNT_INCR(extends); 112 } else 113 evsp->evs_cnt = EXEC_DEFAULT_VMCMD_SETSIZE; 114 115 /* allocate it */ 116 nvcp = malloc(evsp->evs_cnt * sizeof(struct exec_vmcmd), 117 M_EXEC, M_WAITOK); 118 119 /* free the old struct, if there was one, and record the new one */ 120 if (ocnt) { 121 memcpy(nvcp, evsp->evs_cmds, 122 (ocnt * sizeof(struct exec_vmcmd))); 123 free(evsp->evs_cmds, M_EXEC); 124 } 125 evsp->evs_cmds = nvcp; 126 } 127 128 void 129 kill_vmcmds(struct exec_vmcmd_set *evsp) 130 { 131 struct exec_vmcmd *vcp; 132 u_int i; 133 134 VMCMD_EVCNT_INCR(kills); 135 136 if (evsp->evs_cnt == 0) 137 return; 138 139 for (i = 0; i < evsp->evs_used; i++) { 140 vcp = &evsp->evs_cmds[i]; 141 if (vcp->ev_vp != NULL) 142 vrele(vcp->ev_vp); 143 } 144 evsp->evs_used = evsp->evs_cnt = 0; 145 free(evsp->evs_cmds, M_EXEC); 146 } 147 148 /* 149 * vmcmd_map_pagedvn(): 150 * handle vmcmd which specifies that a vnode should be mmap'd. 151 * appropriate for handling demand-paged text and data segments. 152 */ 153 154 int 155 vmcmd_map_pagedvn(struct lwp *l, struct exec_vmcmd *cmd) 156 { 157 struct uvm_object *uobj; 158 struct vnode *vp = cmd->ev_vp; 159 struct proc *p = l->l_proc; 160 int error; 161 vm_prot_t prot, maxprot; 162 163 KASSERT(vp->v_iflag & VI_TEXT); 164 165 /* 166 * map the vnode in using uvm_map. 167 */ 168 169 if (cmd->ev_len == 0) 170 return(0); 171 if (cmd->ev_offset & PAGE_MASK) 172 return(EINVAL); 173 if (cmd->ev_addr & PAGE_MASK) 174 return(EINVAL); 175 if (cmd->ev_len & PAGE_MASK) 176 return(EINVAL); 177 178 prot = cmd->ev_prot; 179 maxprot = UVM_PROT_ALL; 180 #ifdef PAX_MPROTECT 181 pax_mprotect(l, &prot, &maxprot); 182 #endif /* PAX_MPROTECT */ 183 184 /* 185 * check the file system's opinion about mmapping the file 186 */ 187 188 error = VOP_MMAP(vp, prot, p->p_cred); 189 if (error) 190 return error; 191 192 if ((vp->v_vflag & VV_MAPPED) == 0) { 193 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 194 simple_lock(&vp->v_interlock); 195 vp->v_vflag |= VV_MAPPED; 196 vp->v_iflag |= VI_MAPPED; 197 simple_unlock(&vp->v_interlock); 198 VOP_UNLOCK(vp, 0); 199 } 200 201 /* 202 * do the map, reference the object for this map entry 203 */ 204 uobj = &vp->v_uobj; 205 vref(vp); 206 207 error = uvm_map(&p->p_vmspace->vm_map, &cmd->ev_addr, cmd->ev_len, 208 uobj, cmd->ev_offset, 0, 209 UVM_MAPFLAG(prot, maxprot, UVM_INH_COPY, 210 UVM_ADV_NORMAL, UVM_FLAG_COPYONW|UVM_FLAG_FIXED)); 211 if (error) { 212 uobj->pgops->pgo_detach(uobj); 213 } 214 return error; 215 } 216 217 /* 218 * vmcmd_map_readvn(): 219 * handle vmcmd which specifies that a vnode should be read from. 220 * appropriate for non-demand-paged text/data segments, i.e. impure 221 * objects (a la OMAGIC and NMAGIC). 222 */ 223 int 224 vmcmd_map_readvn(struct lwp *l, struct exec_vmcmd *cmd) 225 { 226 struct proc *p = l->l_proc; 227 int error; 228 long diff; 229 230 if (cmd->ev_len == 0) 231 return 0; 232 233 diff = cmd->ev_addr - trunc_page(cmd->ev_addr); 234 cmd->ev_addr -= diff; /* required by uvm_map */ 235 cmd->ev_offset -= diff; 236 cmd->ev_len += diff; 237 238 error = uvm_map(&p->p_vmspace->vm_map, &cmd->ev_addr, 239 round_page(cmd->ev_len), NULL, UVM_UNKNOWN_OFFSET, 0, 240 UVM_MAPFLAG(UVM_PROT_ALL, UVM_PROT_ALL, UVM_INH_COPY, 241 UVM_ADV_NORMAL, 242 UVM_FLAG_FIXED|UVM_FLAG_OVERLAY|UVM_FLAG_COPYONW)); 243 244 if (error) 245 return error; 246 247 return vmcmd_readvn(l, cmd); 248 } 249 250 int 251 vmcmd_readvn(struct lwp *l, struct exec_vmcmd *cmd) 252 { 253 struct proc *p = l->l_proc; 254 int error; 255 vm_prot_t prot, maxprot; 256 257 error = vn_rdwr(UIO_READ, cmd->ev_vp, (void *)cmd->ev_addr, 258 cmd->ev_len, cmd->ev_offset, UIO_USERSPACE, IO_UNIT, 259 l->l_cred, NULL, l); 260 if (error) 261 return error; 262 263 prot = cmd->ev_prot; 264 maxprot = VM_PROT_ALL; 265 #ifdef PAX_MPROTECT 266 pax_mprotect(l, &prot, &maxprot); 267 #endif /* PAX_MPROTECT */ 268 269 #ifdef PMAP_NEED_PROCWR 270 /* 271 * we had to write the process, make sure the pages are synched 272 * with the instruction cache. 273 */ 274 if (prot & VM_PROT_EXECUTE) 275 pmap_procwr(p, cmd->ev_addr, cmd->ev_len); 276 #endif 277 278 /* 279 * we had to map in the area at PROT_ALL so that vn_rdwr() 280 * could write to it. however, the caller seems to want 281 * it mapped read-only, so now we are going to have to call 282 * uvm_map_protect() to fix up the protection. ICK. 283 */ 284 if (maxprot != VM_PROT_ALL) { 285 error = uvm_map_protect(&p->p_vmspace->vm_map, 286 trunc_page(cmd->ev_addr), 287 round_page(cmd->ev_addr + cmd->ev_len), 288 maxprot, true); 289 if (error) 290 return (error); 291 } 292 293 if (prot != maxprot) { 294 error = uvm_map_protect(&p->p_vmspace->vm_map, 295 trunc_page(cmd->ev_addr), 296 round_page(cmd->ev_addr + cmd->ev_len), 297 prot, false); 298 if (error) 299 return (error); 300 } 301 302 return 0; 303 } 304 305 /* 306 * vmcmd_map_zero(): 307 * handle vmcmd which specifies a zero-filled address space region. The 308 * address range must be first allocated, then protected appropriately. 309 */ 310 311 int 312 vmcmd_map_zero(struct lwp *l, struct exec_vmcmd *cmd) 313 { 314 struct proc *p = l->l_proc; 315 int error; 316 long diff; 317 vm_prot_t prot, maxprot; 318 319 diff = cmd->ev_addr - trunc_page(cmd->ev_addr); 320 cmd->ev_addr -= diff; /* required by uvm_map */ 321 cmd->ev_len += diff; 322 323 prot = cmd->ev_prot; 324 maxprot = UVM_PROT_ALL; 325 #ifdef PAX_MPROTECT 326 pax_mprotect(l, &prot, &maxprot); 327 #endif /* PAX_MPROTECT */ 328 329 error = uvm_map(&p->p_vmspace->vm_map, &cmd->ev_addr, 330 round_page(cmd->ev_len), NULL, UVM_UNKNOWN_OFFSET, 0, 331 UVM_MAPFLAG(prot, maxprot, UVM_INH_COPY, 332 UVM_ADV_NORMAL, 333 UVM_FLAG_FIXED|UVM_FLAG_COPYONW)); 334 return error; 335 } 336 337 /* 338 * exec_read_from(): 339 * 340 * Read from vnode into buffer at offset. 341 */ 342 int 343 exec_read_from(struct lwp *l, struct vnode *vp, u_long off, void *bf, 344 size_t size) 345 { 346 int error; 347 size_t resid; 348 349 if ((error = vn_rdwr(UIO_READ, vp, bf, size, off, UIO_SYSSPACE, 350 0, l->l_cred, &resid, NULL)) != 0) 351 return error; 352 /* 353 * See if we got all of it 354 */ 355 if (resid != 0) 356 return ENOEXEC; 357 return 0; 358 } 359 360 /* 361 * exec_setup_stack(): Set up the stack segment for an elf 362 * executable. 363 * 364 * Note that the ep_ssize parameter must be set to be the current stack 365 * limit; this is adjusted in the body of execve() to yield the 366 * appropriate stack segment usage once the argument length is 367 * calculated. 368 * 369 * This function returns an int for uniformity with other (future) formats' 370 * stack setup functions. They might have errors to return. 371 */ 372 373 int 374 exec_setup_stack(struct lwp *l, struct exec_package *epp) 375 { 376 u_long max_stack_size; 377 u_long access_linear_min, access_size; 378 u_long noaccess_linear_min, noaccess_size; 379 380 #ifndef USRSTACK32 381 #define USRSTACK32 (0x00000000ffffffffL&~PGOFSET) 382 #endif 383 384 if (epp->ep_flags & EXEC_32) { 385 epp->ep_minsaddr = USRSTACK32; 386 max_stack_size = MAXSSIZ; 387 } else { 388 epp->ep_minsaddr = USRSTACK; 389 max_stack_size = MAXSSIZ; 390 } 391 epp->ep_maxsaddr = (u_long)STACK_GROW(epp->ep_minsaddr, 392 max_stack_size); 393 epp->ep_ssize = l->l_proc->p_rlimit[RLIMIT_STACK].rlim_cur; 394 395 /* 396 * set up commands for stack. note that this takes *two*, one to 397 * map the part of the stack which we can access, and one to map 398 * the part which we can't. 399 * 400 * arguably, it could be made into one, but that would require the 401 * addition of another mapping proc, which is unnecessary 402 */ 403 access_size = epp->ep_ssize; 404 access_linear_min = (u_long)STACK_ALLOC(epp->ep_minsaddr, access_size); 405 noaccess_size = max_stack_size - access_size; 406 noaccess_linear_min = (u_long)STACK_ALLOC(STACK_GROW(epp->ep_minsaddr, 407 access_size), noaccess_size); 408 if (noaccess_size > 0) { 409 NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero, noaccess_size, 410 noaccess_linear_min, NULL, 0, VM_PROT_NONE); 411 } 412 KASSERT(access_size > 0); 413 NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero, access_size, 414 access_linear_min, NULL, 0, VM_PROT_READ | VM_PROT_WRITE); 415 416 return 0; 417 } 418