Wed Jun 1 21:25:02 2011 UTC ()
kern/42030 - tracking of file descriptors by ktrace/kdump


(alnsn)
diff -r1.314 -r1.315 src/sys/kern/kern_exec.c
diff -r1.156 -r1.157 src/sys/kern/kern_ktrace.c
diff -r1.32 -r1.33 src/sys/kern/kern_stub.c
diff -r1.5 -r1.6 src/sys/kern/subr_exec_fd.c
diff -r1.59 -r1.60 src/sys/sys/filedesc.h
diff -r1.57 -r1.58 src/sys/sys/ktrace.h

cvs diff -r1.314 -r1.315 src/sys/kern/kern_exec.c (expand / switch to unified diff)

--- src/sys/kern/kern_exec.c 2011/04/26 16:36:42 1.314
+++ src/sys/kern/kern_exec.c 2011/06/01 21:24:59 1.315
@@ -1,14 +1,14 @@ @@ -1,14 +1,14 @@
1/* $NetBSD: kern_exec.c,v 1.314 2011/04/26 16:36:42 joerg Exp $ */ 1/* $NetBSD: kern_exec.c,v 1.315 2011/06/01 21:24:59 alnsn Exp $ */
2 2
3/*- 3/*-
4 * Copyright (c) 2008 The NetBSD Foundation, Inc. 4 * Copyright (c) 2008 The NetBSD Foundation, Inc.
5 * All rights reserved. 5 * All rights reserved.
6 * 6 *
7 * Redistribution and use in source and binary forms, with or without 7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions 8 * modification, are permitted provided that the following conditions
9 * are met: 9 * are met:
10 * 1. Redistributions of source code must retain the above copyright 10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer. 11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright 12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the 13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution. 14 * documentation and/or other materials provided with the distribution.
@@ -49,27 +49,27 @@ @@ -49,27 +49,27 @@
49 * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR 49 * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
50 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 50 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
51 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 51 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
52 * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 52 * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
53 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 53 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
54 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 54 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
55 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 55 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
56 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 56 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
57 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 57 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
58 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 58 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
59 */ 59 */
60 60
61#include <sys/cdefs.h> 61#include <sys/cdefs.h>
62__KERNEL_RCSID(0, "$NetBSD: kern_exec.c,v 1.314 2011/04/26 16:36:42 joerg Exp $"); 62__KERNEL_RCSID(0, "$NetBSD: kern_exec.c,v 1.315 2011/06/01 21:24:59 alnsn Exp $");
63 63
64#include "opt_ktrace.h" 64#include "opt_ktrace.h"
65#include "opt_modular.h" 65#include "opt_modular.h"
66#include "opt_syscall_debug.h" 66#include "opt_syscall_debug.h"
67#include "veriexec.h" 67#include "veriexec.h"
68#include "opt_pax.h" 68#include "opt_pax.h"
69#include "opt_sa.h" 69#include "opt_sa.h"
70 70
71#include <sys/param.h> 71#include <sys/param.h>
72#include <sys/systm.h> 72#include <sys/systm.h>
73#include <sys/filedesc.h> 73#include <sys/filedesc.h>
74#include <sys/kernel.h> 74#include <sys/kernel.h>
75#include <sys/proc.h> 75#include <sys/proc.h>
@@ -969,26 +969,30 @@ execve1(struct lwp *l, const char *path, @@ -969,26 +969,30 @@ execve1(struct lwp *l, const char *path,
969 arginfo32.ps_envstr = (vaddr_t)arginfo.ps_envstr; 969 arginfo32.ps_envstr = (vaddr_t)arginfo.ps_envstr;
970 arginfo32.ps_nenvstr = arginfo.ps_nenvstr; 970 arginfo32.ps_nenvstr = arginfo.ps_nenvstr;
971 } 971 }
972 972
973 /* copy out the process's ps_strings structure */ 973 /* copy out the process's ps_strings structure */
974 if ((error = copyout(aip, (void *)p->p_psstrp, ps_strings_sz)) != 0) { 974 if ((error = copyout(aip, (void *)p->p_psstrp, ps_strings_sz)) != 0) {
975 DPRINTF(("%s: ps_strings copyout %p->%p size %zu failed\n", 975 DPRINTF(("%s: ps_strings copyout %p->%p size %zu failed\n",
976 __func__, aip, (void *)p->p_psstrp, ps_strings_sz)); 976 __func__, aip, (void *)p->p_psstrp, ps_strings_sz));
977 goto exec_abort; 977 goto exec_abort;
978 } 978 }
979 979
980 cwdexec(p); 980 cwdexec(p);
981 fd_closeexec(); /* handle close on exec */ 981 fd_closeexec(); /* handle close on exec */
 982
 983 if (__predict_false(ktrace_on))
 984 fd_ktrexecfd();
 985
982 execsigs(p); /* reset catched signals */ 986 execsigs(p); /* reset catched signals */
983 987
984 l->l_ctxlink = NULL; /* reset ucontext link */ 988 l->l_ctxlink = NULL; /* reset ucontext link */
985 989
986 990
987 p->p_acflag &= ~AFORK; 991 p->p_acflag &= ~AFORK;
988 mutex_enter(p->p_lock); 992 mutex_enter(p->p_lock);
989 p->p_flag |= PK_EXEC; 993 p->p_flag |= PK_EXEC;
990 mutex_exit(p->p_lock); 994 mutex_exit(p->p_lock);
991 995
992 /* 996 /*
993 * Stop profiling. 997 * Stop profiling.
994 */ 998 */

cvs diff -r1.156 -r1.157 src/sys/kern/kern_ktrace.c (expand / switch to unified diff)

--- src/sys/kern/kern_ktrace.c 2011/04/27 00:00:46 1.156
+++ src/sys/kern/kern_ktrace.c 2011/06/01 21:25:00 1.157
@@ -1,14 +1,14 @@ @@ -1,14 +1,14 @@
1/* $NetBSD: kern_ktrace.c,v 1.156 2011/04/27 00:00:46 joerg Exp $ */ 1/* $NetBSD: kern_ktrace.c,v 1.157 2011/06/01 21:25:00 alnsn Exp $ */
2 2
3/*- 3/*-
4 * Copyright (c) 2006, 2007, 2008 The NetBSD Foundation, Inc. 4 * Copyright (c) 2006, 2007, 2008 The NetBSD Foundation, Inc.
5 * All rights reserved. 5 * All rights reserved.
6 * 6 *
7 * This code is derived from software contributed to The NetBSD Foundation 7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Andrew Doran. 8 * by Andrew Doran.
9 * 9 *
10 * Redistribution and use in source and binary forms, with or without 10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions 11 * modification, are permitted provided that the following conditions
12 * are met: 12 * are met:
13 * 1. Redistributions of source code must retain the above copyright 13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer. 14 * notice, this list of conditions and the following disclaimer.
@@ -51,27 +51,27 @@ @@ -51,27 +51,27 @@
51 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 51 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
52 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 52 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
53 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 53 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
54 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 54 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
55 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 55 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
56 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 56 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
57 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 57 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
58 * SUCH DAMAGE. 58 * SUCH DAMAGE.
59 * 59 *
60 * @(#)kern_ktrace.c 8.5 (Berkeley) 5/14/95 60 * @(#)kern_ktrace.c 8.5 (Berkeley) 5/14/95
61 */ 61 */
62 62
63#include <sys/cdefs.h> 63#include <sys/cdefs.h>
64__KERNEL_RCSID(0, "$NetBSD: kern_ktrace.c,v 1.156 2011/04/27 00:00:46 joerg Exp $"); 64__KERNEL_RCSID(0, "$NetBSD: kern_ktrace.c,v 1.157 2011/06/01 21:25:00 alnsn Exp $");
65 65
66#include <sys/param.h> 66#include <sys/param.h>
67#include <sys/systm.h> 67#include <sys/systm.h>
68#include <sys/proc.h> 68#include <sys/proc.h>
69#include <sys/file.h> 69#include <sys/file.h>
70#include <sys/namei.h> 70#include <sys/namei.h>
71#include <sys/vnode.h> 71#include <sys/vnode.h>
72#include <sys/kernel.h> 72#include <sys/kernel.h>
73#include <sys/kthread.h> 73#include <sys/kthread.h>
74#include <sys/ktrace.h> 74#include <sys/ktrace.h>
75#include <sys/kmem.h> 75#include <sys/kmem.h>
76#include <sys/syslog.h> 76#include <sys/syslog.h>
77#include <sys/filedesc.h> 77#include <sys/filedesc.h>
@@ -663,26 +663,45 @@ ktr_execarg(const void *bf, size_t len) @@ -663,26 +663,45 @@ ktr_execarg(const void *bf, size_t len)
663} 663}
664 664
665void 665void
666ktr_execenv(const void *bf, size_t len) 666ktr_execenv(const void *bf, size_t len)
667{ 667{
668 lwp_t *l = curlwp; 668 lwp_t *l = curlwp;
669 669
670 if (!KTRPOINT(l->l_proc, KTR_EXEC_ENV)) 670 if (!KTRPOINT(l->l_proc, KTR_EXEC_ENV))
671 return; 671 return;
672 672
673 ktr_kmem(l, KTR_EXEC_ENV, bf, len); 673 ktr_kmem(l, KTR_EXEC_ENV, bf, len);
674} 674}
675 675
 676void
 677ktr_execfd(int fd, u_int dtype)
 678{
 679 struct ktrace_entry *kte;
 680 struct ktr_execfd* ktp;
 681
 682 lwp_t *l = curlwp;
 683
 684 if (!KTRPOINT(l->l_proc, KTR_EXEC_FD))
 685 return;
 686
 687 if (ktealloc(&kte, (void *)&ktp, l, KTR_EXEC_FD, sizeof(*ktp)))
 688 return;
 689
 690 ktp->ktr_fd = fd;
 691 ktp->ktr_dtype = dtype;
 692 ktraddentry(l, kte, KTA_WAITOK);
 693}
 694
676static void 695static void
677ktr_kmem(lwp_t *l, int type, const void *bf, size_t len) 696ktr_kmem(lwp_t *l, int type, const void *bf, size_t len)
678{ 697{
679 struct ktrace_entry *kte; 698 struct ktrace_entry *kte;
680 void *buf; 699 void *buf;
681 700
682 if (ktealloc(&kte, &buf, l, type, len)) 701 if (ktealloc(&kte, &buf, l, type, len))
683 return; 702 return;
684 memcpy(buf, bf, len); 703 memcpy(buf, bf, len);
685 ktraddentry(l, kte, KTA_WAITOK); 704 ktraddentry(l, kte, KTA_WAITOK);
686} 705}
687 706
688static void 707static void

cvs diff -r1.32 -r1.33 src/sys/kern/kern_stub.c (expand / switch to unified diff)

--- src/sys/kern/kern_stub.c 2011/05/31 23:28:53 1.32
+++ src/sys/kern/kern_stub.c 2011/06/01 21:25:01 1.33
@@ -1,14 +1,14 @@ @@ -1,14 +1,14 @@
1/* $NetBSD: kern_stub.c,v 1.32 2011/05/31 23:28:53 dyoung Exp $ */ 1/* $NetBSD: kern_stub.c,v 1.33 2011/06/01 21:25:01 alnsn Exp $ */
2 2
3/*- 3/*-
4 * Copyright (c) 2007, 2008 The NetBSD Foundation, Inc. 4 * Copyright (c) 2007, 2008 The NetBSD Foundation, Inc.
5 * All rights reserved. 5 * All rights reserved.
6 * 6 *
7 * Redistribution and use in source and binary forms, with or without 7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions 8 * modification, are permitted provided that the following conditions
9 * are met: 9 * are met:
10 * 1. Redistributions of source code must retain the above copyright 10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer. 11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright 12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the 13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution. 14 * documentation and/or other materials provided with the distribution.
@@ -52,27 +52,27 @@ @@ -52,27 +52,27 @@
52 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 52 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
53 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 53 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
54 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 54 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
55 * SUCH DAMAGE. 55 * SUCH DAMAGE.
56 * 56 *
57 * @(#)subr_xxx.c 8.3 (Berkeley) 3/29/95 57 * @(#)subr_xxx.c 8.3 (Berkeley) 3/29/95
58 */ 58 */
59 59
60/* 60/*
61 * Stubs for system calls and facilities not included in the system. 61 * Stubs for system calls and facilities not included in the system.
62 */ 62 */
63 63
64#include <sys/cdefs.h> 64#include <sys/cdefs.h>
65__KERNEL_RCSID(0, "$NetBSD: kern_stub.c,v 1.32 2011/05/31 23:28:53 dyoung Exp $"); 65__KERNEL_RCSID(0, "$NetBSD: kern_stub.c,v 1.33 2011/06/01 21:25:01 alnsn Exp $");
66 66
67#include "opt_ptrace.h" 67#include "opt_ptrace.h"
68#include "opt_ktrace.h" 68#include "opt_ktrace.h"
69#include "opt_sa.h" 69#include "opt_sa.h"
70 70
71#include <sys/param.h> 71#include <sys/param.h>
72#include <sys/kernel.h> 72#include <sys/kernel.h>
73#include <sys/proc.h> 73#include <sys/proc.h>
74#include <sys/fstypes.h> 74#include <sys/fstypes.h>
75#include <sys/signalvar.h> 75#include <sys/signalvar.h>
76#include <sys/syscall.h> 76#include <sys/syscall.h>
77#include <sys/ktrace.h> 77#include <sys/ktrace.h>
78#include <sys/intr.h> 78#include <sys/intr.h>
@@ -101,26 +101,27 @@ __weak_alias(ktr_emul,nullop); @@ -101,26 +101,27 @@ __weak_alias(ktr_emul,nullop);
101__weak_alias(ktr_geniov,nullop); 101__weak_alias(ktr_geniov,nullop);
102__weak_alias(ktr_genio,nullop); 102__weak_alias(ktr_genio,nullop);
103__weak_alias(ktr_mibio,nullop); 103__weak_alias(ktr_mibio,nullop);
104__weak_alias(ktr_namei,nullop); 104__weak_alias(ktr_namei,nullop);
105__weak_alias(ktr_namei2,nullop); 105__weak_alias(ktr_namei2,nullop);
106__weak_alias(ktr_psig,nullop); 106__weak_alias(ktr_psig,nullop);
107__weak_alias(ktr_saupcall,nullop); 107__weak_alias(ktr_saupcall,nullop);
108__weak_alias(ktr_syscall,nullop); 108__weak_alias(ktr_syscall,nullop);
109__weak_alias(ktr_sysret,nullop); 109__weak_alias(ktr_sysret,nullop);
110__weak_alias(ktr_kuser,nullop); 110__weak_alias(ktr_kuser,nullop);
111__weak_alias(ktr_mib,nullop); 111__weak_alias(ktr_mib,nullop);
112__weak_alias(ktr_execarg,nullop); 112__weak_alias(ktr_execarg,nullop);
113__weak_alias(ktr_execenv,nullop); 113__weak_alias(ktr_execenv,nullop);
 114__weak_alias(ktr_execfd,nullop);
114 115
115__weak_alias(sys_fktrace,sys_nosys); /* Syscalls */ 116__weak_alias(sys_fktrace,sys_nosys); /* Syscalls */
116__weak_alias(sys_ktrace,sys_nosys); 117__weak_alias(sys_ktrace,sys_nosys);
117__weak_alias(sys_utrace,sys_nosys); 118__weak_alias(sys_utrace,sys_nosys);
118 119
119int ktrace_on; /* Misc */ 120int ktrace_on; /* Misc */
120__weak_alias(ktruser,enosys); 121__weak_alias(ktruser,enosys);
121__weak_alias(ktr_point,nullop); 122__weak_alias(ktr_point,nullop);
122#endif /* KTRACE */ 123#endif /* KTRACE */
123 124
124__weak_alias(device_register, voidop); 125__weak_alias(device_register, voidop);
125__weak_alias(device_register_post_config, voidop); 126__weak_alias(device_register_post_config, voidop);
126__weak_alias(spldebug_start, voidop); 127__weak_alias(spldebug_start, voidop);

cvs diff -r1.5 -r1.6 src/sys/kern/subr_exec_fd.c (expand / switch to unified diff)

--- src/sys/kern/subr_exec_fd.c 2011/02/15 15:54:28 1.5
+++ src/sys/kern/subr_exec_fd.c 2011/06/01 21:25:01 1.6
@@ -1,14 +1,14 @@ @@ -1,14 +1,14 @@
1/* $NetBSD: subr_exec_fd.c,v 1.5 2011/02/15 15:54:28 pooka Exp $ */ 1/* $NetBSD: subr_exec_fd.c,v 1.6 2011/06/01 21:25:01 alnsn Exp $ */
2 2
3/*- 3/*-
4 * Copyright (c) 2008 The NetBSD Foundation, Inc. 4 * Copyright (c) 2008 The NetBSD Foundation, Inc.
5 * All rights reserved. 5 * All rights reserved.
6 * 6 *
7 * Redistribution and use in source and binary forms, with or without 7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions 8 * modification, are permitted provided that the following conditions
9 * are met: 9 * are met:
10 * 1. Redistributions of source code must retain the above copyright 10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer. 11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright 12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the 13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution. 14 * documentation and/or other materials provided with the distribution.
@@ -17,35 +17,64 @@ @@ -17,35 +17,64 @@
17 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 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 18 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 19 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 20 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 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 23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 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 25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE. 26 * POSSIBILITY OF SUCH DAMAGE.
27 */ 27 */
28 28
29#include <sys/cdefs.h> 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 $"); 30__KERNEL_RCSID(0, "$NetBSD: subr_exec_fd.c,v 1.6 2011/06/01 21:25:01 alnsn Exp $");
31 31
32#include <sys/param.h> 32#include <sys/param.h>
33#include <sys/file.h> 33#include <sys/file.h>
34#include <sys/filedesc.h> 34#include <sys/filedesc.h>
35#include <sys/mutex.h> 35#include <sys/mutex.h>
36#include <sys/namei.h> 36#include <sys/namei.h>
37#include <sys/syslog.h> 37#include <sys/syslog.h>
38#include <sys/vnode.h> 38#include <sys/vnode.h>
 39#include <sys/ktrace.h>
 40
 41void
 42fd_ktrexecfd(void)
 43{
 44 proc_t *p;
 45 filedesc_t *fdp;
 46 fdfile_t *ff;
 47 lwp_t *l;
 48 fdtab_t *dt;
 49 int fd;
 50
 51 l = curlwp;
 52 p = l->l_proc;
 53 fdp = p->p_fd;
 54 dt = fdp->fd_dt;
 55
 56 for (fd = 0; fd <= fdp->fd_lastfile; fd++) {
 57 if ((ff = dt->dt_ff[fd]) == NULL) {
 58 KASSERT(fd >= NDFDFILE);
 59 continue;
 60 }
 61 KASSERT(fd >= NDFDFILE ||
 62 ff == (fdfile_t *)fdp->fd_dfdfile[fd]);
 63 if (ff->ff_file == NULL)
 64 continue;
 65 ktr_execfd(fd, ff->ff_file->f_type);
 66 }
 67}
39 68
40/* 69/*
41 * It is unsafe for set[ug]id processes to be started with file 70 * It is unsafe for set[ug]id processes to be started with file
42 * descriptors 0..2 closed, as these descriptors are given implicit 71 * descriptors 0..2 closed, as these descriptors are given implicit
43 * significance in the Standard C library. fdcheckstd() will create a 72 * significance in the Standard C library. fdcheckstd() will create a
44 * descriptor referencing /dev/null for each of stdin, stdout, and 73 * descriptor referencing /dev/null for each of stdin, stdout, and
45 * stderr that is not already open. 74 * stderr that is not already open.
46 */ 75 */
47#define CHECK_UPTO 3 76#define CHECK_UPTO 3
48int 77int
49fd_checkstd(void) 78fd_checkstd(void)
50{ 79{
51 struct proc *p; 80 struct proc *p;

cvs diff -r1.59 -r1.60 src/sys/sys/filedesc.h (expand / switch to unified diff)

--- src/sys/sys/filedesc.h 2011/04/10 15:45:33 1.59
+++ src/sys/sys/filedesc.h 2011/06/01 21:25:02 1.60
@@ -1,14 +1,14 @@ @@ -1,14 +1,14 @@
1/* $NetBSD: filedesc.h,v 1.59 2011/04/10 15:45:33 christos Exp $ */ 1/* $NetBSD: filedesc.h,v 1.60 2011/06/01 21:25:02 alnsn Exp $ */
2 2
3/*- 3/*-
4 * Copyright (c) 2008 The NetBSD Foundation, Inc. 4 * Copyright (c) 2008 The NetBSD Foundation, Inc.
5 * All rights reserved. 5 * All rights reserved.
6 * 6 *
7 * Redistribution and use in source and binary forms, with or without 7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions 8 * modification, are permitted provided that the following conditions
9 * are met: 9 * are met:
10 * 1. Redistributions of source code must retain the above copyright 10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer. 11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright 12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the 13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution. 14 * documentation and/or other materials provided with the distribution.
@@ -183,26 +183,27 @@ struct proc; @@ -183,26 +183,27 @@ struct proc;
183void fd_sys_init(void); 183void fd_sys_init(void);
184int fd_dupopen(int, int *, int, int); 184int fd_dupopen(int, int *, int, int);
185int fd_alloc(struct proc *, int, int *); 185int fd_alloc(struct proc *, int, int *);
186void fd_tryexpand(struct proc *); 186void fd_tryexpand(struct proc *);
187int fd_allocfile(file_t **, int *); 187int fd_allocfile(file_t **, int *);
188void fd_affix(struct proc *, file_t *, unsigned); 188void fd_affix(struct proc *, file_t *, unsigned);
189void fd_abort(struct proc *, file_t *, unsigned); 189void fd_abort(struct proc *, file_t *, unsigned);
190filedesc_t *fd_copy(void); 190filedesc_t *fd_copy(void);
191filedesc_t *fd_init(filedesc_t *); 191filedesc_t *fd_init(filedesc_t *);
192void fd_share(proc_t *); 192void fd_share(proc_t *);
193void fd_hold(lwp_t *); 193void fd_hold(lwp_t *);
194void fd_free(void); 194void fd_free(void);
195void fd_closeexec(void); 195void fd_closeexec(void);
 196void fd_ktrexecfd(void);
196int fd_checkstd(void); 197int fd_checkstd(void);
197file_t *fd_getfile(unsigned); 198file_t *fd_getfile(unsigned);
198file_t *fd_getfile2(proc_t *, unsigned); 199file_t *fd_getfile2(proc_t *, unsigned);
199void fd_putfile(unsigned); 200void fd_putfile(unsigned);
200int fd_getvnode(unsigned, file_t **); 201int fd_getvnode(unsigned, file_t **);
201int fd_getsock(unsigned, struct socket **); 202int fd_getsock(unsigned, struct socket **);
202void fd_putvnode(unsigned); 203void fd_putvnode(unsigned);
203void fd_putsock(unsigned); 204void fd_putsock(unsigned);
204int fd_close(unsigned); 205int fd_close(unsigned);
205int fd_dup(file_t *, int, int *, bool); 206int fd_dup(file_t *, int, int *, bool);
206int fd_dup2(file_t *, unsigned); 207int fd_dup2(file_t *, unsigned);
207int fd_clone(file_t *, unsigned, int, const struct fileops *, void *); 208int fd_clone(file_t *, unsigned, int, const struct fileops *, void *);
208void fd_set_exclose(struct lwp *, int, bool); 209void fd_set_exclose(struct lwp *, int, bool);

cvs diff -r1.57 -r1.58 src/sys/sys/ktrace.h (expand / switch to unified diff)

--- src/sys/sys/ktrace.h 2011/04/27 00:00:46 1.57
+++ src/sys/sys/ktrace.h 2011/06/01 21:25:02 1.58
@@ -1,14 +1,14 @@ @@ -1,14 +1,14 @@
1/* $NetBSD: ktrace.h,v 1.57 2011/04/27 00:00:46 joerg Exp $ */ 1/* $NetBSD: ktrace.h,v 1.58 2011/06/01 21:25:02 alnsn Exp $ */
2 2
3/* 3/*
4 * Copyright (c) 1988, 1993 4 * Copyright (c) 1988, 1993
5 * The Regents of the University of California. All rights reserved. 5 * The Regents of the University of California. All rights reserved.
6 * 6 *
7 * Redistribution and use in source and binary forms, with or without 7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions 8 * modification, are permitted provided that the following conditions
9 * are met: 9 * are met:
10 * 1. Redistributions of source code must retain the above copyright 10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer. 11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright 12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the 13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution. 14 * documentation and/or other materials provided with the distribution.
@@ -207,43 +207,52 @@ struct ktr_saupcall { @@ -207,43 +207,52 @@ struct ktr_saupcall {
207 void *ktr_sas; 207 void *ktr_sas;
208 void *ktr_ap; 208 void *ktr_ap;
209 /* 209 /*
210 * followed by nevent sa_t's from sas[] 210 * followed by nevent sa_t's from sas[]
211 */ 211 */
212}; 212};
213 213
214/* 214/*
215 * KTR_MIB - MIB name and data 215 * KTR_MIB - MIB name and data
216 */ 216 */
217#define KTR_MIB 14 217#define KTR_MIB 14
218 /* Record contains MIB name */ 218 /* Record contains MIB name */
219 219
 220/*
 221 * KTR_EXEC_FD - Opened file descriptor from exec
 222 */
 223#define KTR_EXEC_FD 15
 224struct ktr_execfd {
 225 int ktr_fd;
 226 u_int ktr_dtype; /* one of DTYPE_* constants */
 227};
220 228
221/* 229/*
222 * kernel trace points (in p_traceflag) 230 * kernel trace points (in p_traceflag)
223 */ 231 */
224#define KTRFAC_MASK 0x00ffffff 232#define KTRFAC_MASK 0x00ffffff
225#define KTRFAC_SYSCALL (1<<KTR_SYSCALL) 233#define KTRFAC_SYSCALL (1<<KTR_SYSCALL)
226#define KTRFAC_SYSRET (1<<KTR_SYSRET) 234#define KTRFAC_SYSRET (1<<KTR_SYSRET)
227#define KTRFAC_NAMEI (1<<KTR_NAMEI) 235#define KTRFAC_NAMEI (1<<KTR_NAMEI)
228#define KTRFAC_GENIO (1<<KTR_GENIO) 236#define KTRFAC_GENIO (1<<KTR_GENIO)
229#define KTRFAC_PSIG (1<<KTR_PSIG) 237#define KTRFAC_PSIG (1<<KTR_PSIG)
230#define KTRFAC_CSW (1<<KTR_CSW) 238#define KTRFAC_CSW (1<<KTR_CSW)
231#define KTRFAC_EMUL (1<<KTR_EMUL) 239#define KTRFAC_EMUL (1<<KTR_EMUL)
232#define KTRFAC_USER (1<<KTR_USER) 240#define KTRFAC_USER (1<<KTR_USER)
233#define KTRFAC_EXEC_ARG (1<<KTR_EXEC_ARG) 241#define KTRFAC_EXEC_ARG (1<<KTR_EXEC_ARG)
234#define KTRFAC_EXEC_ENV (1<<KTR_EXEC_ENV) 242#define KTRFAC_EXEC_ENV (1<<KTR_EXEC_ENV)
235#define KTRFAC_SAUPCALL (1<<KTR_SAUPCALL) 243#define KTRFAC_SAUPCALL (1<<KTR_SAUPCALL)
236#define KTRFAC_MIB (1<<KTR_MIB) 244#define KTRFAC_MIB (1<<KTR_MIB)
 245#define KTRFAC_EXEC_FD (1<<KTR_EXEC_FD)
237/* 246/*
238 * trace flags (also in p_traceflags) 247 * trace flags (also in p_traceflags)
239 */ 248 */
240#define KTRFAC_PERSISTENT 0x80000000 /* persistent trace across sugid 249#define KTRFAC_PERSISTENT 0x80000000 /* persistent trace across sugid
241 exec (exclusive) */ 250 exec (exclusive) */
242#define KTRFAC_INHERIT 0x40000000 /* pass trace flags to children */ 251#define KTRFAC_INHERIT 0x40000000 /* pass trace flags to children */
243#define KTRFAC_TRC_EMUL 0x10000000 /* ktrace KTR_EMUL before next trace */ 252#define KTRFAC_TRC_EMUL 0x10000000 /* ktrace KTR_EMUL before next trace */
244#define KTRFAC_VER_MASK 0x0f000000 /* record version mask */ 253#define KTRFAC_VER_MASK 0x0f000000 /* record version mask */
245#define KTRFAC_VER_SHIFT 24 /* record version shift */ 254#define KTRFAC_VER_SHIFT 24 /* record version shift */
246 255
247#define KTRFAC_VERSION(tf) (((tf) & KTRFAC_VER_MASK) >> KTRFAC_VER_SHIFT) 256#define KTRFAC_VERSION(tf) (((tf) & KTRFAC_VER_MASK) >> KTRFAC_VER_SHIFT)
248 257
249#define KTRFACv0 (0 << KTRFAC_VER_SHIFT) 258#define KTRFACv0 (0 << KTRFAC_VER_SHIFT)
@@ -276,26 +285,27 @@ void ktr_csw(int, int); @@ -276,26 +285,27 @@ void ktr_csw(int, int);
276void ktr_emul(void); 285void ktr_emul(void);
277void ktr_geniov(int, enum uio_rw, struct iovec *, size_t, int); 286void ktr_geniov(int, enum uio_rw, struct iovec *, size_t, int);
278void ktr_genio(int, enum uio_rw, const void *, size_t, int); 287void ktr_genio(int, enum uio_rw, const void *, size_t, int);
279void ktr_mibio(int, enum uio_rw, const void *, size_t, int); 288void ktr_mibio(int, enum uio_rw, const void *, size_t, int);
280void ktr_namei(const char *, size_t); 289void ktr_namei(const char *, size_t);
281void ktr_namei2(const char *, size_t, const char *, size_t); 290void ktr_namei2(const char *, size_t, const char *, size_t);
282void ktr_psig(int, sig_t, const sigset_t *, const ksiginfo_t *); 291void ktr_psig(int, sig_t, const sigset_t *, const ksiginfo_t *);
283void ktr_syscall(register_t, const register_t [], int); 292void ktr_syscall(register_t, const register_t [], int);
284void ktr_sysret(register_t, int, register_t *); 293void ktr_sysret(register_t, int, register_t *);
285void ktr_kuser(const char *, void *, size_t); 294void ktr_kuser(const char *, void *, size_t);
286void ktr_mib(const int *a , u_int b); 295void ktr_mib(const int *a , u_int b);
287void ktr_execarg(const void *, size_t); 296void ktr_execarg(const void *, size_t);
288void ktr_execenv(const void *, size_t); 297void ktr_execenv(const void *, size_t);
 298void ktr_execfd(int, u_int);
289void ktr_saupcall(struct lwp *, int, int, int, void *, void *, void *); 299void ktr_saupcall(struct lwp *, int, int, int, void *, void *, void *);
290 300
291static inline bool 301static inline bool
292ktrpoint(int fac) 302ktrpoint(int fac)
293{ 303{
294 return __predict_false(ktrace_on) && __predict_false(ktr_point(1 << fac)); 304 return __predict_false(ktrace_on) && __predict_false(ktr_point(1 << fac));
295} 305}
296 306
297static inline void 307static inline void
298ktrcsw(int a, int b) 308ktrcsw(int a, int b)
299{ 309{
300 if (__predict_false(ktrace_on)) 310 if (__predict_false(ktrace_on))
301 ktr_csw(a, b); 311 ktr_csw(a, b);
@@ -383,22 +393,29 @@ ktrexecarg(const void *a, size_t b) @@ -383,22 +393,29 @@ ktrexecarg(const void *a, size_t b)
383{ 393{
384 if (__predict_false(ktrace_on)) 394 if (__predict_false(ktrace_on))
385 ktr_execarg(a, b); 395 ktr_execarg(a, b);
386} 396}
387 397
388static inline void 398static inline void
389ktrexecenv(const void *a, size_t b) 399ktrexecenv(const void *a, size_t b)
390{ 400{
391 if (__predict_false(ktrace_on)) 401 if (__predict_false(ktrace_on))
392 ktr_execenv(a, b); 402 ktr_execenv(a, b);
393} 403}
394 404
395static inline void 405static inline void
 406ktrexecfd(int fd, u_int dtype)
 407{
 408 if (__predict_false(ktrace_on))
 409 ktr_execfd(fd, dtype);
 410}
 411
 412static inline void
396ktrsaupcall(struct lwp *a, int b, int c, int d, void *e, void *f, void *g) 413ktrsaupcall(struct lwp *a, int b, int c, int d, void *e, void *f, void *g)
397{ 414{
398 if (__predict_false(ktrace_on)) 415 if (__predict_false(ktrace_on))
399 ktr_saupcall(a, b, c, d, e, f, g); 416 ktr_saupcall(a, b, c, d, e, f, g);
400} 417}
401 418
402#endif /* !_KERNEL */ 419#endif /* !_KERNEL */
403 420
404#endif /* _SYS_KTRACE_H_ */ 421#endif /* _SYS_KTRACE_H_ */