1 /* $NetBSD: subr_exec_fd.c,v 1.1 2008/11/18 13:01:41 pooka Exp $ */
4 * Copyright (c) 2008 The NetBSD Foundation, Inc.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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.
30 * File descriptor related subroutines for exec.
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 $");
36 #include <sys/param.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>
45 * Close open files on exec.
63 if (p
->p_cwdi
->cwdi_edir
) {
64 vrele(p
->p_cwdi
->cwdi_edir
);
67 if (fdp
->fd_refcnt
> 1) {
73 if (!fdp
->fd_exclose
) {
76 fdp
->fd_exclose
= false;
79 for (fd
= 0; fd
<= fdp
->fd_lastfile
; fd
++) {
80 if ((ff
= dt
->dt_ff
[fd
]) == NULL
) {
81 KASSERT(fd
>= NDFDFILE
);
84 KASSERT(fd
>= NDFDFILE
||
85 ff
== (fdfile_t
*)fdp
->fd_dfdfile
[fd
]);
86 if (ff
->ff_file
== NULL
)
90 * We need a reference to close the file.
91 * No other threads can see the fdfile_t at
92 * this point, so don't bother locking.
94 KASSERT((ff
->ff_refcnt
& FR_CLOSING
) == 0);
102 * It is unsafe for set[ug]id processes to be started with file
103 * descriptors 0..2 closed, as these descriptors are given implicit
104 * significance in the Standard C library. fdcheckstd() will create a
105 * descriptor referencing /dev/null for each of stdin, stdout, and
106 * stderr that is not already open.
118 int fd
, i
, error
, flags
= FREAD
|FWRITE
;
119 char closed
[CHECK_UPTO
* 3 + 1], which
[3 + 1];
123 if ((fdp
= p
->p_fd
) == NULL
)
126 for (i
= 0; i
< CHECK_UPTO
; i
++) {
127 KASSERT(i
>= NDFDFILE
||
128 dt
->dt_ff
[i
] == (fdfile_t
*)fdp
->fd_dfdfile
[i
]);
129 if (dt
->dt_ff
[i
]->ff_file
!= NULL
)
131 snprintf(which
, sizeof(which
), ",%d", i
);
132 strlcat(closed
, which
, sizeof(closed
));
133 if ((error
= fd_allocfile(&fp
, &fd
)) != 0)
135 KASSERT(fd
< CHECK_UPTO
);
136 NDINIT(&nd
, LOOKUP
, FOLLOW
, UIO_SYSSPACE
, "/dev/null");
137 if ((error
= vn_open(&nd
, flags
, 0)) != 0) {
141 fp
->f_data
= nd
.ni_vp
;
144 fp
->f_type
= DTYPE_VNODE
;
145 VOP_UNLOCK(nd
.ni_vp
, 0);
148 if (closed
[0] != '\0') {
149 mutex_enter(proc_lock
);
151 mutex_enter(pp
->p_lock
);
152 log(LOG_WARNING
, "set{u,g}id pid %d (%s) "
153 "was invoked by uid %d ppid %d (%s) "
154 "with fd %s closed\n",
155 p
->p_pid
, p
->p_comm
, kauth_cred_geteuid(pp
->p_cred
),
156 pp
->p_pid
, pp
->p_comm
, &closed
[1]);
157 mutex_exit(pp
->p_lock
);
158 mutex_exit(proc_lock
);