Home | History | Annotate | Line # | Download | only in kern
subr_exec_fd.c revision 1.9
      1  1.9  riastrad /*	$NetBSD: subr_exec_fd.c,v 1.9 2020/02/01 02:23:04 riastradh 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 #include <sys/cdefs.h>
     30  1.9  riastrad __KERNEL_RCSID(0, "$NetBSD: subr_exec_fd.c,v 1.9 2020/02/01 02:23:04 riastradh Exp $");
     31  1.1     pooka 
     32  1.1     pooka #include <sys/param.h>
     33  1.9  riastrad #include <sys/atomic.h>
     34  1.1     pooka #include <sys/file.h>
     35  1.1     pooka #include <sys/filedesc.h>
     36  1.1     pooka #include <sys/mutex.h>
     37  1.1     pooka #include <sys/namei.h>
     38  1.1     pooka #include <sys/syslog.h>
     39  1.1     pooka #include <sys/vnode.h>
     40  1.6     alnsn #include <sys/ktrace.h>
     41  1.6     alnsn 
     42  1.6     alnsn void
     43  1.6     alnsn fd_ktrexecfd(void)
     44  1.6     alnsn {
     45  1.6     alnsn 	proc_t *p;
     46  1.6     alnsn 	filedesc_t *fdp;
     47  1.6     alnsn 	fdfile_t *ff;
     48  1.6     alnsn 	lwp_t *l;
     49  1.6     alnsn 	fdtab_t *dt;
     50  1.6     alnsn 	int fd;
     51  1.6     alnsn 
     52  1.6     alnsn 	l = curlwp;
     53  1.6     alnsn 	p = l->l_proc;
     54  1.6     alnsn 	fdp = p->p_fd;
     55  1.9  riastrad 	dt = atomic_load_consume(&fdp->fd_dt);
     56  1.6     alnsn 
     57  1.6     alnsn 	for (fd = 0; fd <= fdp->fd_lastfile; fd++) {
     58  1.6     alnsn 		if ((ff = dt->dt_ff[fd]) == NULL) {
     59  1.6     alnsn 			KASSERT(fd >= NDFDFILE);
     60  1.6     alnsn 			continue;
     61  1.6     alnsn 		}
     62  1.6     alnsn 		KASSERT(fd >= NDFDFILE ||
     63  1.6     alnsn 		    ff == (fdfile_t *)fdp->fd_dfdfile[fd]);
     64  1.6     alnsn 		if (ff->ff_file == NULL)
     65  1.6     alnsn 			continue;
     66  1.6     alnsn 		ktr_execfd(fd, ff->ff_file->f_type);
     67  1.6     alnsn 	}
     68  1.6     alnsn }
     69  1.1     pooka 
     70  1.1     pooka /*
     71  1.1     pooka  * It is unsafe for set[ug]id processes to be started with file
     72  1.1     pooka  * descriptors 0..2 closed, as these descriptors are given implicit
     73  1.8      maya  * significance in the Standard C library.  fd_checkstd() will create a
     74  1.1     pooka  * descriptor referencing /dev/null for each of stdin, stdout, and
     75  1.1     pooka  * stderr that is not already open.
     76  1.1     pooka  */
     77  1.1     pooka #define CHECK_UPTO 3
     78  1.1     pooka int
     79  1.1     pooka fd_checkstd(void)
     80  1.1     pooka {
     81  1.1     pooka 	struct proc *p;
     82  1.4  dholland 	struct pathbuf *pb;
     83  1.1     pooka 	struct nameidata nd;
     84  1.1     pooka 	filedesc_t *fdp;
     85  1.1     pooka 	file_t *fp;
     86  1.2        ad 	fdtab_t *dt;
     87  1.1     pooka 	struct proc *pp;
     88  1.1     pooka 	int fd, i, error, flags = FREAD|FWRITE;
     89  1.1     pooka 	char closed[CHECK_UPTO * 3 + 1], which[3 + 1];
     90  1.1     pooka 
     91  1.1     pooka 	p = curproc;
     92  1.1     pooka 	closed[0] = '\0';
     93  1.1     pooka 	if ((fdp = p->p_fd) == NULL)
     94  1.1     pooka 		return (0);
     95  1.9  riastrad 	dt = atomic_load_consume(&fdp->fd_dt);
     96  1.1     pooka 	for (i = 0; i < CHECK_UPTO; i++) {
     97  1.1     pooka 		KASSERT(i >= NDFDFILE ||
     98  1.2        ad 		    dt->dt_ff[i] == (fdfile_t *)fdp->fd_dfdfile[i]);
     99  1.2        ad 		if (dt->dt_ff[i]->ff_file != NULL)
    100  1.1     pooka 			continue;
    101  1.1     pooka 		snprintf(which, sizeof(which), ",%d", i);
    102  1.1     pooka 		strlcat(closed, which, sizeof(closed));
    103  1.1     pooka 		if ((error = fd_allocfile(&fp, &fd)) != 0)
    104  1.1     pooka 			return (error);
    105  1.1     pooka 		KASSERT(fd < CHECK_UPTO);
    106  1.4  dholland 		pb = pathbuf_create("/dev/null");
    107  1.4  dholland 		if (pb == NULL) {
    108  1.4  dholland 			return ENOMEM;
    109  1.4  dholland 		}
    110  1.4  dholland 		NDINIT(&nd, LOOKUP, FOLLOW, pb);
    111  1.1     pooka 		if ((error = vn_open(&nd, flags, 0)) != 0) {
    112  1.4  dholland 			pathbuf_destroy(pb);
    113  1.1     pooka 			fd_abort(p, fp, fd);
    114  1.1     pooka 			return (error);
    115  1.1     pooka 		}
    116  1.7      matt 		fp->f_type = DTYPE_VNODE;
    117  1.7      matt 		fp->f_vnode = nd.ni_vp;
    118  1.1     pooka 		fp->f_flag = flags;
    119  1.1     pooka 		fp->f_ops = &vnops;
    120  1.3   hannken 		VOP_UNLOCK(nd.ni_vp);
    121  1.1     pooka 		fd_affix(p, fp, fd);
    122  1.4  dholland 		pathbuf_destroy(pb);
    123  1.1     pooka 	}
    124  1.1     pooka 	if (closed[0] != '\0') {
    125  1.1     pooka 		mutex_enter(proc_lock);
    126  1.1     pooka 		pp = p->p_pptr;
    127  1.1     pooka 		mutex_enter(pp->p_lock);
    128  1.1     pooka 		log(LOG_WARNING, "set{u,g}id pid %d (%s) "
    129  1.1     pooka 		    "was invoked by uid %d ppid %d (%s) "
    130  1.1     pooka 		    "with fd %s closed\n",
    131  1.1     pooka 		    p->p_pid, p->p_comm, kauth_cred_geteuid(pp->p_cred),
    132  1.1     pooka 		    pp->p_pid, pp->p_comm, &closed[1]);
    133  1.1     pooka 		mutex_exit(pp->p_lock);
    134  1.1     pooka 		mutex_exit(proc_lock);
    135  1.1     pooka 	}
    136  1.1     pooka 	return (0);
    137  1.1     pooka }
    138  1.1     pooka #undef CHECK_UPTO
    139