1 | /* $NetBSD: kern_ktrace_vfs.c,v 1.2 2014/09/05 09:20:59 matt Exp $ */ |
2 | |
3 | /*- |
4 | * Copyright (c) 2006, 2007, 2008 The NetBSD Foundation, Inc. |
5 | * All rights reserved. |
6 | * |
7 | * This code is derived from software contributed to The NetBSD Foundation |
8 | * by Andrew Doran. |
9 | * |
10 | * Redistribution and use in source and binary forms, with or without |
11 | * modification, are permitted provided that the following conditions |
12 | * are met: |
13 | * 1. Redistributions of source code must retain the above copyright |
14 | * notice, this list of conditions and the following disclaimer. |
15 | * 2. Redistributions in binary form must reproduce the above copyright |
16 | * notice, this list of conditions and the following disclaimer in the |
17 | * documentation and/or other materials provided with the distribution. |
18 | * |
19 | * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS |
20 | * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED |
21 | * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
22 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS |
23 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
24 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
25 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
26 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
27 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
28 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
29 | * POSSIBILITY OF SUCH DAMAGE. |
30 | */ |
31 | |
32 | /* |
33 | * Copyright (c) 1989, 1993 |
34 | * The Regents of the University of California. All rights reserved. |
35 | * |
36 | * Redistribution and use in source and binary forms, with or without |
37 | * modification, are permitted provided that the following conditions |
38 | * are met: |
39 | * 1. Redistributions of source code must retain the above copyright |
40 | * notice, this list of conditions and the following disclaimer. |
41 | * 2. Redistributions in binary form must reproduce the above copyright |
42 | * notice, this list of conditions and the following disclaimer in the |
43 | * documentation and/or other materials provided with the distribution. |
44 | * 3. Neither the name of the University nor the names of its contributors |
45 | * may be used to endorse or promote products derived from this software |
46 | * without specific prior written permission. |
47 | * |
48 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND |
49 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
50 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
51 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE |
52 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
53 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
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 |
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 |
58 | * SUCH DAMAGE. |
59 | * |
60 | * @(#)kern_ktrace.c 8.5 (Berkeley) 5/14/95 |
61 | */ |
62 | |
63 | #include <sys/cdefs.h> |
64 | __KERNEL_RCSID(0, "$NetBSD: kern_ktrace_vfs.c,v 1.2 2014/09/05 09:20:59 matt Exp $" ); |
65 | |
66 | #include <sys/param.h> |
67 | #include <sys/systm.h> |
68 | #include <sys/file.h> |
69 | #include <sys/filedesc.h> |
70 | #include <sys/namei.h> |
71 | #include <sys/vnode.h> |
72 | #include <sys/kernel.h> |
73 | #include <sys/ktrace.h> |
74 | #include <sys/kauth.h> |
75 | |
76 | #include <sys/mount.h> |
77 | #include <sys/syscallargs.h> |
78 | |
79 | /* |
80 | * ktrace system call, the part of the ktrace framework that |
81 | * explicitly interacts with VFS |
82 | */ |
83 | /* ARGSUSED */ |
84 | int |
85 | sys_ktrace(struct lwp *l, const struct sys_ktrace_args *uap, register_t *retval) |
86 | { |
87 | /* { |
88 | syscallarg(const char *) fname; |
89 | syscallarg(int) ops; |
90 | syscallarg(int) facs; |
91 | syscallarg(int) pid; |
92 | } */ |
93 | struct vnode *vp = NULL; |
94 | file_t *fp = NULL; |
95 | struct pathbuf *pb; |
96 | struct nameidata nd; |
97 | int error = 0; |
98 | int fd; |
99 | |
100 | if (ktrenter(l)) |
101 | return EAGAIN; |
102 | |
103 | if (KTROP(SCARG(uap, ops)) != KTROP_CLEAR) { |
104 | /* |
105 | * an operation which requires a file argument. |
106 | */ |
107 | error = pathbuf_copyin(SCARG(uap, fname), &pb); |
108 | if (error) { |
109 | ktrexit(l); |
110 | return (error); |
111 | } |
112 | NDINIT(&nd, LOOKUP, FOLLOW, pb); |
113 | if ((error = vn_open(&nd, FREAD|FWRITE, 0)) != 0) { |
114 | pathbuf_destroy(pb); |
115 | ktrexit(l); |
116 | return (error); |
117 | } |
118 | vp = nd.ni_vp; |
119 | pathbuf_destroy(pb); |
120 | VOP_UNLOCK(vp); |
121 | if (vp->v_type != VREG) { |
122 | vn_close(vp, FREAD|FWRITE, l->l_cred); |
123 | ktrexit(l); |
124 | return (EACCES); |
125 | } |
126 | /* |
127 | * This uses up a file descriptor slot in the |
128 | * tracing process for the duration of this syscall. |
129 | * This is not expected to be a problem. |
130 | */ |
131 | if ((error = fd_allocfile(&fp, &fd)) != 0) { |
132 | vn_close(vp, FWRITE, l->l_cred); |
133 | ktrexit(l); |
134 | return error; |
135 | } |
136 | fp->f_flag = FWRITE; |
137 | fp->f_type = DTYPE_VNODE; |
138 | fp->f_ops = &vnops; |
139 | fp->f_vnode = vp; |
140 | vp = NULL; |
141 | } |
142 | error = ktrace_common(l, SCARG(uap, ops), SCARG(uap, facs), |
143 | SCARG(uap, pid), &fp); |
144 | if (KTROP(SCARG(uap, ops)) != KTROP_CLEAR) |
145 | fd_abort(curproc, fp, fd); |
146 | return (error); |
147 | } |
148 | |