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