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