xref: /netbsd-src/sys/kern/subr_exec_fd.c (revision cb861154c176d3dcc8ff846f449e3c16a5f5edb5)
1 /*	$NetBSD: subr_exec_fd.c,v 1.5 2011/02/15 15:54:28 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 #include <sys/cdefs.h>
30 __KERNEL_RCSID(0, "$NetBSD: subr_exec_fd.c,v 1.5 2011/02/15 15:54:28 pooka 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 
40 /*
41  * It is unsafe for set[ug]id processes to be started with file
42  * descriptors 0..2 closed, as these descriptors are given implicit
43  * significance in the Standard C library.  fdcheckstd() will create a
44  * descriptor referencing /dev/null for each of stdin, stdout, and
45  * stderr that is not already open.
46  */
47 #define CHECK_UPTO 3
48 int
49 fd_checkstd(void)
50 {
51 	struct proc *p;
52 	struct pathbuf *pb;
53 	struct nameidata nd;
54 	filedesc_t *fdp;
55 	file_t *fp;
56 	fdtab_t *dt;
57 	struct proc *pp;
58 	int fd, i, error, flags = FREAD|FWRITE;
59 	char closed[CHECK_UPTO * 3 + 1], which[3 + 1];
60 
61 	p = curproc;
62 	closed[0] = '\0';
63 	if ((fdp = p->p_fd) == NULL)
64 		return (0);
65 	dt = fdp->fd_dt;
66 	for (i = 0; i < CHECK_UPTO; i++) {
67 		KASSERT(i >= NDFDFILE ||
68 		    dt->dt_ff[i] == (fdfile_t *)fdp->fd_dfdfile[i]);
69 		if (dt->dt_ff[i]->ff_file != NULL)
70 			continue;
71 		snprintf(which, sizeof(which), ",%d", i);
72 		strlcat(closed, which, sizeof(closed));
73 		if ((error = fd_allocfile(&fp, &fd)) != 0)
74 			return (error);
75 		KASSERT(fd < CHECK_UPTO);
76 		pb = pathbuf_create("/dev/null");
77 		if (pb == NULL) {
78 			return ENOMEM;
79 		}
80 		NDINIT(&nd, LOOKUP, FOLLOW, pb);
81 		if ((error = vn_open(&nd, flags, 0)) != 0) {
82 			pathbuf_destroy(pb);
83 			fd_abort(p, fp, fd);
84 			return (error);
85 		}
86 		fp->f_data = nd.ni_vp;
87 		fp->f_flag = flags;
88 		fp->f_ops = &vnops;
89 		fp->f_type = DTYPE_VNODE;
90 		VOP_UNLOCK(nd.ni_vp);
91 		fd_affix(p, fp, fd);
92 		pathbuf_destroy(pb);
93 	}
94 	if (closed[0] != '\0') {
95 		mutex_enter(proc_lock);
96 		pp = p->p_pptr;
97 		mutex_enter(pp->p_lock);
98 		log(LOG_WARNING, "set{u,g}id pid %d (%s) "
99 		    "was invoked by uid %d ppid %d (%s) "
100 		    "with fd %s closed\n",
101 		    p->p_pid, p->p_comm, kauth_cred_geteuid(pp->p_cred),
102 		    pp->p_pid, pp->p_comm, &closed[1]);
103 		mutex_exit(pp->p_lock);
104 		mutex_exit(proc_lock);
105 	}
106 	return (0);
107 }
108 #undef CHECK_UPTO
109