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