Home | History | Annotate | Line # | Download | only in kern
subr_exec_fd.c revision 1.4
      1 /*	$NetBSD: subr_exec_fd.c,v 1.4 2010/11/19 06:44:43 dholland 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.4 2010/11/19 06:44:43 dholland 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 pathbuf *pb;
    114 	struct nameidata nd;
    115 	filedesc_t *fdp;
    116 	file_t *fp;
    117 	fdtab_t *dt;
    118 	struct proc *pp;
    119 	int fd, i, error, flags = FREAD|FWRITE;
    120 	char closed[CHECK_UPTO * 3 + 1], which[3 + 1];
    121 
    122 	p = curproc;
    123 	closed[0] = '\0';
    124 	if ((fdp = p->p_fd) == NULL)
    125 		return (0);
    126 	dt = fdp->fd_dt;
    127 	for (i = 0; i < CHECK_UPTO; i++) {
    128 		KASSERT(i >= NDFDFILE ||
    129 		    dt->dt_ff[i] == (fdfile_t *)fdp->fd_dfdfile[i]);
    130 		if (dt->dt_ff[i]->ff_file != NULL)
    131 			continue;
    132 		snprintf(which, sizeof(which), ",%d", i);
    133 		strlcat(closed, which, sizeof(closed));
    134 		if ((error = fd_allocfile(&fp, &fd)) != 0)
    135 			return (error);
    136 		KASSERT(fd < CHECK_UPTO);
    137 		pb = pathbuf_create("/dev/null");
    138 		if (pb == NULL) {
    139 			return ENOMEM;
    140 		}
    141 		NDINIT(&nd, LOOKUP, FOLLOW, pb);
    142 		if ((error = vn_open(&nd, flags, 0)) != 0) {
    143 			pathbuf_destroy(pb);
    144 			fd_abort(p, fp, fd);
    145 			return (error);
    146 		}
    147 		fp->f_data = nd.ni_vp;
    148 		fp->f_flag = flags;
    149 		fp->f_ops = &vnops;
    150 		fp->f_type = DTYPE_VNODE;
    151 		VOP_UNLOCK(nd.ni_vp);
    152 		fd_affix(p, fp, fd);
    153 		pathbuf_destroy(pb);
    154 	}
    155 	if (closed[0] != '\0') {
    156 		mutex_enter(proc_lock);
    157 		pp = p->p_pptr;
    158 		mutex_enter(pp->p_lock);
    159 		log(LOG_WARNING, "set{u,g}id pid %d (%s) "
    160 		    "was invoked by uid %d ppid %d (%s) "
    161 		    "with fd %s closed\n",
    162 		    p->p_pid, p->p_comm, kauth_cred_geteuid(pp->p_cred),
    163 		    pp->p_pid, pp->p_comm, &closed[1]);
    164 		mutex_exit(pp->p_lock);
    165 		mutex_exit(proc_lock);
    166 	}
    167 	return (0);
    168 }
    169 #undef CHECK_UPTO
    170