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