subr_exec_fd.c revision 1.1 1 1.1 pooka /* $NetBSD: subr_exec_fd.c,v 1.1 2008/11/18 13:01:41 pooka 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.1 pooka __KERNEL_RCSID(0, "$NetBSD: subr_exec_fd.c,v 1.1 2008/11/18 13:01:41 pooka 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.1 pooka int fd;
55 1.1 pooka
56 1.1 pooka l = curlwp;
57 1.1 pooka p = l->l_proc;
58 1.1 pooka fdp = p->p_fd;
59 1.1 pooka
60 1.1 pooka cwdunshare(p);
61 1.1 pooka
62 1.1 pooka if (p->p_cwdi->cwdi_edir) {
63 1.1 pooka vrele(p->p_cwdi->cwdi_edir);
64 1.1 pooka }
65 1.1 pooka
66 1.1 pooka if (fdp->fd_refcnt > 1) {
67 1.1 pooka fdp = fd_copy();
68 1.1 pooka fd_free();
69 1.1 pooka p->p_fd = fdp;
70 1.1 pooka l->l_fd = fdp;
71 1.1 pooka }
72 1.1 pooka if (!fdp->fd_exclose) {
73 1.1 pooka return;
74 1.1 pooka }
75 1.1 pooka fdp->fd_exclose = false;
76 1.1 pooka
77 1.1 pooka for (fd = 0; fd <= fdp->fd_lastfile; fd++) {
78 1.1 pooka if ((ff = fdp->fd_ofiles[fd]) == NULL) {
79 1.1 pooka KASSERT(fd >= NDFDFILE);
80 1.1 pooka continue;
81 1.1 pooka }
82 1.1 pooka KASSERT(fd >= NDFDFILE ||
83 1.1 pooka ff == (fdfile_t *)fdp->fd_dfdfile[fd]);
84 1.1 pooka if (ff->ff_file == NULL)
85 1.1 pooka continue;
86 1.1 pooka if (ff->ff_exclose) {
87 1.1 pooka /*
88 1.1 pooka * We need a reference to close the file.
89 1.1 pooka * No other threads can see the fdfile_t at
90 1.1 pooka * this point, so don't bother locking.
91 1.1 pooka */
92 1.1 pooka KASSERT((ff->ff_refcnt & FR_CLOSING) == 0);
93 1.1 pooka ff->ff_refcnt++;
94 1.1 pooka fd_close(fd);
95 1.1 pooka }
96 1.1 pooka }
97 1.1 pooka }
98 1.1 pooka
99 1.1 pooka /*
100 1.1 pooka * It is unsafe for set[ug]id processes to be started with file
101 1.1 pooka * descriptors 0..2 closed, as these descriptors are given implicit
102 1.1 pooka * significance in the Standard C library. fdcheckstd() will create a
103 1.1 pooka * descriptor referencing /dev/null for each of stdin, stdout, and
104 1.1 pooka * stderr that is not already open.
105 1.1 pooka */
106 1.1 pooka #define CHECK_UPTO 3
107 1.1 pooka int
108 1.1 pooka fd_checkstd(void)
109 1.1 pooka {
110 1.1 pooka struct proc *p;
111 1.1 pooka struct nameidata nd;
112 1.1 pooka filedesc_t *fdp;
113 1.1 pooka file_t *fp;
114 1.1 pooka struct proc *pp;
115 1.1 pooka int fd, i, error, flags = FREAD|FWRITE;
116 1.1 pooka char closed[CHECK_UPTO * 3 + 1], which[3 + 1];
117 1.1 pooka
118 1.1 pooka p = curproc;
119 1.1 pooka closed[0] = '\0';
120 1.1 pooka if ((fdp = p->p_fd) == NULL)
121 1.1 pooka return (0);
122 1.1 pooka for (i = 0; i < CHECK_UPTO; i++) {
123 1.1 pooka KASSERT(i >= NDFDFILE ||
124 1.1 pooka fdp->fd_ofiles[i] == (fdfile_t *)fdp->fd_dfdfile[i]);
125 1.1 pooka if (fdp->fd_ofiles[i]->ff_file != NULL)
126 1.1 pooka continue;
127 1.1 pooka snprintf(which, sizeof(which), ",%d", i);
128 1.1 pooka strlcat(closed, which, sizeof(closed));
129 1.1 pooka if ((error = fd_allocfile(&fp, &fd)) != 0)
130 1.1 pooka return (error);
131 1.1 pooka KASSERT(fd < CHECK_UPTO);
132 1.1 pooka NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, "/dev/null");
133 1.1 pooka if ((error = vn_open(&nd, flags, 0)) != 0) {
134 1.1 pooka fd_abort(p, fp, fd);
135 1.1 pooka return (error);
136 1.1 pooka }
137 1.1 pooka fp->f_data = nd.ni_vp;
138 1.1 pooka fp->f_flag = flags;
139 1.1 pooka fp->f_ops = &vnops;
140 1.1 pooka fp->f_type = DTYPE_VNODE;
141 1.1 pooka VOP_UNLOCK(nd.ni_vp, 0);
142 1.1 pooka fd_affix(p, fp, fd);
143 1.1 pooka }
144 1.1 pooka if (closed[0] != '\0') {
145 1.1 pooka mutex_enter(proc_lock);
146 1.1 pooka pp = p->p_pptr;
147 1.1 pooka mutex_enter(pp->p_lock);
148 1.1 pooka log(LOG_WARNING, "set{u,g}id pid %d (%s) "
149 1.1 pooka "was invoked by uid %d ppid %d (%s) "
150 1.1 pooka "with fd %s closed\n",
151 1.1 pooka p->p_pid, p->p_comm, kauth_cred_geteuid(pp->p_cred),
152 1.1 pooka pp->p_pid, pp->p_comm, &closed[1]);
153 1.1 pooka mutex_exit(pp->p_lock);
154 1.1 pooka mutex_exit(proc_lock);
155 1.1 pooka }
156 1.1 pooka return (0);
157 1.1 pooka }
158 1.1 pooka #undef CHECK_UPTO
159