1 /* $NetBSD: subr_exec_fd.c,v 1.3 2010/06/24 13:03:11 hannken Exp $ */ 2 3 /*- 4 * Copyright (c) 2008 The NetBSD Foundation, Inc. 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 * 16 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 17 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 18 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 19 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 20 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 26 * POSSIBILITY OF SUCH DAMAGE. 27 */ 28 29 /* 30 * File descriptor related subroutines for exec. 31 */ 32 33 #include <sys/cdefs.h> 34 __KERNEL_RCSID(0, "$NetBSD: subr_exec_fd.c,v 1.3 2010/06/24 13:03:11 hannken Exp $"); 35 36 #include <sys/param.h> 37 #include <sys/file.h> 38 #include <sys/filedesc.h> 39 #include <sys/mutex.h> 40 #include <sys/namei.h> 41 #include <sys/syslog.h> 42 #include <sys/vnode.h> 43 44 /* 45 * Close open files on exec. 46 */ 47 void 48 fd_closeexec(void) 49 { 50 proc_t *p; 51 filedesc_t *fdp; 52 fdfile_t *ff; 53 lwp_t *l; 54 fdtab_t *dt; 55 int fd; 56 57 l = curlwp; 58 p = l->l_proc; 59 fdp = p->p_fd; 60 61 cwdunshare(p); 62 63 if (p->p_cwdi->cwdi_edir) { 64 vrele(p->p_cwdi->cwdi_edir); 65 } 66 67 if (fdp->fd_refcnt > 1) { 68 fdp = fd_copy(); 69 fd_free(); 70 p->p_fd = fdp; 71 l->l_fd = fdp; 72 } 73 if (!fdp->fd_exclose) { 74 return; 75 } 76 fdp->fd_exclose = false; 77 dt = fdp->fd_dt; 78 79 for (fd = 0; fd <= fdp->fd_lastfile; fd++) { 80 if ((ff = dt->dt_ff[fd]) == NULL) { 81 KASSERT(fd >= NDFDFILE); 82 continue; 83 } 84 KASSERT(fd >= NDFDFILE || 85 ff == (fdfile_t *)fdp->fd_dfdfile[fd]); 86 if (ff->ff_file == NULL) 87 continue; 88 if (ff->ff_exclose) { 89 /* 90 * We need a reference to close the file. 91 * No other threads can see the fdfile_t at 92 * this point, so don't bother locking. 93 */ 94 KASSERT((ff->ff_refcnt & FR_CLOSING) == 0); 95 ff->ff_refcnt++; 96 fd_close(fd); 97 } 98 } 99 } 100 101 /* 102 * It is unsafe for set[ug]id processes to be started with file 103 * descriptors 0..2 closed, as these descriptors are given implicit 104 * significance in the Standard C library. fdcheckstd() will create a 105 * descriptor referencing /dev/null for each of stdin, stdout, and 106 * stderr that is not already open. 107 */ 108 #define CHECK_UPTO 3 109 int 110 fd_checkstd(void) 111 { 112 struct proc *p; 113 struct nameidata nd; 114 filedesc_t *fdp; 115 file_t *fp; 116 fdtab_t *dt; 117 struct proc *pp; 118 int fd, i, error, flags = FREAD|FWRITE; 119 char closed[CHECK_UPTO * 3 + 1], which[3 + 1]; 120 121 p = curproc; 122 closed[0] = '\0'; 123 if ((fdp = p->p_fd) == NULL) 124 return (0); 125 dt = fdp->fd_dt; 126 for (i = 0; i < CHECK_UPTO; i++) { 127 KASSERT(i >= NDFDFILE || 128 dt->dt_ff[i] == (fdfile_t *)fdp->fd_dfdfile[i]); 129 if (dt->dt_ff[i]->ff_file != NULL) 130 continue; 131 snprintf(which, sizeof(which), ",%d", i); 132 strlcat(closed, which, sizeof(closed)); 133 if ((error = fd_allocfile(&fp, &fd)) != 0) 134 return (error); 135 KASSERT(fd < CHECK_UPTO); 136 NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, "/dev/null"); 137 if ((error = vn_open(&nd, flags, 0)) != 0) { 138 fd_abort(p, fp, fd); 139 return (error); 140 } 141 fp->f_data = nd.ni_vp; 142 fp->f_flag = flags; 143 fp->f_ops = &vnops; 144 fp->f_type = DTYPE_VNODE; 145 VOP_UNLOCK(nd.ni_vp); 146 fd_affix(p, fp, fd); 147 } 148 if (closed[0] != '\0') { 149 mutex_enter(proc_lock); 150 pp = p->p_pptr; 151 mutex_enter(pp->p_lock); 152 log(LOG_WARNING, "set{u,g}id pid %d (%s) " 153 "was invoked by uid %d ppid %d (%s) " 154 "with fd %s closed\n", 155 p->p_pid, p->p_comm, kauth_cred_geteuid(pp->p_cred), 156 pp->p_pid, pp->p_comm, &closed[1]); 157 mutex_exit(pp->p_lock); 158 mutex_exit(proc_lock); 159 } 160 return (0); 161 } 162 #undef CHECK_UPTO 163