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