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