sys_ptrace.c revision 1.4.12.2 1 1.4.12.2 jdolecek /* $NetBSD: sys_ptrace.c,v 1.4.12.2 2017/12/03 11:38:45 jdolecek Exp $ */
2 1.4.12.2 jdolecek
3 1.4.12.2 jdolecek /*-
4 1.4.12.2 jdolecek * Copyright (c) 2008, 2009 The NetBSD Foundation, Inc.
5 1.4.12.2 jdolecek * All rights reserved.
6 1.4.12.2 jdolecek *
7 1.4.12.2 jdolecek * This code is derived from software contributed to The NetBSD Foundation
8 1.4.12.2 jdolecek * by Andrew Doran.
9 1.4.12.2 jdolecek *
10 1.4.12.2 jdolecek * Redistribution and use in source and binary forms, with or without
11 1.4.12.2 jdolecek * modification, are permitted provided that the following conditions
12 1.4.12.2 jdolecek * are met:
13 1.4.12.2 jdolecek * 1. Redistributions of source code must retain the above copyright
14 1.4.12.2 jdolecek * notice, this list of conditions and the following disclaimer.
15 1.4.12.2 jdolecek * 2. Redistributions in binary form must reproduce the above copyright
16 1.4.12.2 jdolecek * notice, this list of conditions and the following disclaimer in the
17 1.4.12.2 jdolecek * documentation and/or other materials provided with the distribution.
18 1.4.12.2 jdolecek *
19 1.4.12.2 jdolecek * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.4.12.2 jdolecek * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.4.12.2 jdolecek * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.4.12.2 jdolecek * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.4.12.2 jdolecek * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.4.12.2 jdolecek * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.4.12.2 jdolecek * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.4.12.2 jdolecek * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.4.12.2 jdolecek * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.4.12.2 jdolecek * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.4.12.2 jdolecek * POSSIBILITY OF SUCH DAMAGE.
30 1.4.12.2 jdolecek */
31 1.4.12.2 jdolecek
32 1.4.12.2 jdolecek /*-
33 1.4.12.2 jdolecek * Copyright (c) 1982, 1986, 1989, 1993
34 1.4.12.2 jdolecek * The Regents of the University of California. All rights reserved.
35 1.4.12.2 jdolecek * (c) UNIX System Laboratories, Inc.
36 1.4.12.2 jdolecek * All or some portions of this file are derived from material licensed
37 1.4.12.2 jdolecek * to the University of California by American Telephone and Telegraph
38 1.4.12.2 jdolecek * Co. or Unix System Laboratories, Inc. and are reproduced herein with
39 1.4.12.2 jdolecek * the permission of UNIX System Laboratories, Inc.
40 1.4.12.2 jdolecek *
41 1.4.12.2 jdolecek * This code is derived from software contributed to Berkeley by
42 1.4.12.2 jdolecek * Jan-Simon Pendry.
43 1.4.12.2 jdolecek *
44 1.4.12.2 jdolecek * Redistribution and use in source and binary forms, with or without
45 1.4.12.2 jdolecek * modification, are permitted provided that the following conditions
46 1.4.12.2 jdolecek * are met:
47 1.4.12.2 jdolecek * 1. Redistributions of source code must retain the above copyright
48 1.4.12.2 jdolecek * notice, this list of conditions and the following disclaimer.
49 1.4.12.2 jdolecek * 2. Redistributions in binary form must reproduce the above copyright
50 1.4.12.2 jdolecek * notice, this list of conditions and the following disclaimer in the
51 1.4.12.2 jdolecek * documentation and/or other materials provided with the distribution.
52 1.4.12.2 jdolecek * 3. Neither the name of the University nor the names of its contributors
53 1.4.12.2 jdolecek * may be used to endorse or promote products derived from this software
54 1.4.12.2 jdolecek * without specific prior written permission.
55 1.4.12.2 jdolecek *
56 1.4.12.2 jdolecek * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
57 1.4.12.2 jdolecek * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
58 1.4.12.2 jdolecek * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
59 1.4.12.2 jdolecek * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
60 1.4.12.2 jdolecek * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
61 1.4.12.2 jdolecek * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
62 1.4.12.2 jdolecek * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
63 1.4.12.2 jdolecek * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
64 1.4.12.2 jdolecek * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
65 1.4.12.2 jdolecek * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
66 1.4.12.2 jdolecek * SUCH DAMAGE.
67 1.4.12.2 jdolecek *
68 1.4.12.2 jdolecek * from: @(#)sys_process.c 8.1 (Berkeley) 6/10/93
69 1.4.12.2 jdolecek */
70 1.4.12.2 jdolecek
71 1.4.12.2 jdolecek /*-
72 1.4.12.2 jdolecek * Copyright (c) 1993 Jan-Simon Pendry.
73 1.4.12.2 jdolecek * Copyright (c) 1994 Christopher G. Demetriou. All rights reserved.
74 1.4.12.2 jdolecek *
75 1.4.12.2 jdolecek * This code is derived from software contributed to Berkeley by
76 1.4.12.2 jdolecek * Jan-Simon Pendry.
77 1.4.12.2 jdolecek *
78 1.4.12.2 jdolecek * Redistribution and use in source and binary forms, with or without
79 1.4.12.2 jdolecek * modification, are permitted provided that the following conditions
80 1.4.12.2 jdolecek * are met:
81 1.4.12.2 jdolecek * 1. Redistributions of source code must retain the above copyright
82 1.4.12.2 jdolecek * notice, this list of conditions and the following disclaimer.
83 1.4.12.2 jdolecek * 2. Redistributions in binary form must reproduce the above copyright
84 1.4.12.2 jdolecek * notice, this list of conditions and the following disclaimer in the
85 1.4.12.2 jdolecek * documentation and/or other materials provided with the distribution.
86 1.4.12.2 jdolecek * 3. All advertising materials mentioning features or use of this software
87 1.4.12.2 jdolecek * must display the following acknowledgement:
88 1.4.12.2 jdolecek * This product includes software developed by the University of
89 1.4.12.2 jdolecek * California, Berkeley and its contributors.
90 1.4.12.2 jdolecek * 4. Neither the name of the University nor the names of its contributors
91 1.4.12.2 jdolecek * may be used to endorse or promote products derived from this software
92 1.4.12.2 jdolecek * without specific prior written permission.
93 1.4.12.2 jdolecek *
94 1.4.12.2 jdolecek * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
95 1.4.12.2 jdolecek * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
96 1.4.12.2 jdolecek * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
97 1.4.12.2 jdolecek * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
98 1.4.12.2 jdolecek * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
99 1.4.12.2 jdolecek * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
100 1.4.12.2 jdolecek * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
101 1.4.12.2 jdolecek * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
102 1.4.12.2 jdolecek * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
103 1.4.12.2 jdolecek * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
104 1.4.12.2 jdolecek * SUCH DAMAGE.
105 1.4.12.2 jdolecek *
106 1.4.12.2 jdolecek * from: @(#)sys_process.c 8.1 (Berkeley) 6/10/93
107 1.4.12.2 jdolecek */
108 1.4.12.2 jdolecek
109 1.4.12.2 jdolecek /*
110 1.4.12.2 jdolecek * References:
111 1.4.12.2 jdolecek * (1) Bach's "The Design of the UNIX Operating System",
112 1.4.12.2 jdolecek * (2) sys/miscfs/procfs from UCB's 4.4BSD-Lite distribution,
113 1.4.12.2 jdolecek * (3) the "4.4BSD Programmer's Reference Manual" published
114 1.4.12.2 jdolecek * by USENIX and O'Reilly & Associates.
115 1.4.12.2 jdolecek * The 4.4BSD PRM does a reasonably good job of documenting what the various
116 1.4.12.2 jdolecek * ptrace() requests should actually do, and its text is quoted several times
117 1.4.12.2 jdolecek * in this file.
118 1.4.12.2 jdolecek */
119 1.4.12.2 jdolecek
120 1.4.12.2 jdolecek #include <sys/cdefs.h>
121 1.4.12.2 jdolecek __KERNEL_RCSID(0, "$NetBSD: sys_ptrace.c,v 1.4.12.2 2017/12/03 11:38:45 jdolecek Exp $");
122 1.4.12.2 jdolecek
123 1.4.12.2 jdolecek #ifdef _KERNEL_OPT
124 1.4.12.2 jdolecek #include "opt_ptrace.h"
125 1.4.12.2 jdolecek #endif
126 1.4.12.2 jdolecek
127 1.4.12.2 jdolecek #include <sys/param.h>
128 1.4.12.2 jdolecek #include <sys/systm.h>
129 1.4.12.2 jdolecek #include <sys/proc.h>
130 1.4.12.2 jdolecek #include <sys/errno.h>
131 1.4.12.2 jdolecek #include <sys/exec.h>
132 1.4.12.2 jdolecek #include <sys/pax.h>
133 1.4.12.2 jdolecek #include <sys/ptrace.h>
134 1.4.12.2 jdolecek #include <sys/uio.h>
135 1.4.12.2 jdolecek #include <sys/ras.h>
136 1.4.12.2 jdolecek #include <sys/kmem.h>
137 1.4.12.2 jdolecek #include <sys/kauth.h>
138 1.4.12.2 jdolecek #include <sys/mount.h>
139 1.4.12.2 jdolecek #include <sys/syscallargs.h>
140 1.4.12.2 jdolecek #include <sys/syscallvar.h>
141 1.4.12.2 jdolecek #include <sys/syscall.h>
142 1.4.12.2 jdolecek #include <sys/module.h>
143 1.4.12.2 jdolecek
144 1.4.12.2 jdolecek #include <uvm/uvm_extern.h>
145 1.4.12.2 jdolecek
146 1.4.12.2 jdolecek #include <machine/reg.h>
147 1.4.12.2 jdolecek
148 1.4.12.2 jdolecek /*
149 1.4.12.2 jdolecek * PTRACE methods
150 1.4.12.2 jdolecek */
151 1.4.12.2 jdolecek
152 1.4.12.2 jdolecek static int ptrace_copyinpiod(struct ptrace_io_desc *, const void *);
153 1.4.12.2 jdolecek static void ptrace_copyoutpiod(const struct ptrace_io_desc *, void *);
154 1.4.12.2 jdolecek
155 1.4.12.2 jdolecek static int
156 1.4.12.2 jdolecek ptrace_copyinpiod(struct ptrace_io_desc *piod, const void *addr)
157 1.4.12.2 jdolecek {
158 1.4.12.2 jdolecek return copyin(addr, piod, sizeof(*piod));
159 1.4.12.2 jdolecek }
160 1.4.12.2 jdolecek
161 1.4.12.2 jdolecek static void
162 1.4.12.2 jdolecek ptrace_copyoutpiod(const struct ptrace_io_desc *piod, void *addr)
163 1.4.12.2 jdolecek {
164 1.4.12.2 jdolecek (void) copyout(piod, addr, sizeof(*piod));
165 1.4.12.2 jdolecek }
166 1.4.12.2 jdolecek
167 1.4.12.2 jdolecek static struct ptrace_methods native_ptm = {
168 1.4.12.2 jdolecek .ptm_copyinpiod = ptrace_copyinpiod,
169 1.4.12.2 jdolecek .ptm_copyoutpiod = ptrace_copyoutpiod,
170 1.4.12.2 jdolecek .ptm_doregs = process_doregs,
171 1.4.12.2 jdolecek .ptm_dofpregs = process_dofpregs,
172 1.4.12.2 jdolecek .ptm_dodbregs = process_dodbregs,
173 1.4.12.2 jdolecek };
174 1.4.12.2 jdolecek
175 1.4.12.2 jdolecek static const struct syscall_package ptrace_syscalls[] = {
176 1.4.12.2 jdolecek { SYS_ptrace, 0, (sy_call_t *)sys_ptrace },
177 1.4.12.2 jdolecek { 0, 0, NULL },
178 1.4.12.2 jdolecek };
179 1.4.12.2 jdolecek
180 1.4.12.2 jdolecek /*
181 1.4.12.2 jdolecek * Process debugging system call.
182 1.4.12.2 jdolecek */
183 1.4.12.2 jdolecek int
184 1.4.12.2 jdolecek sys_ptrace(struct lwp *l, const struct sys_ptrace_args *uap, register_t *retval)
185 1.4.12.2 jdolecek {
186 1.4.12.2 jdolecek /* {
187 1.4.12.2 jdolecek syscallarg(int) req;
188 1.4.12.2 jdolecek syscallarg(pid_t) pid;
189 1.4.12.2 jdolecek syscallarg(void *) addr;
190 1.4.12.2 jdolecek syscallarg(int) data;
191 1.4.12.2 jdolecek } */
192 1.4.12.2 jdolecek
193 1.4.12.2 jdolecek return do_ptrace(&native_ptm, l, SCARG(uap, req), SCARG(uap, pid),
194 1.4.12.2 jdolecek SCARG(uap, addr), SCARG(uap, data), retval);
195 1.4.12.2 jdolecek }
196 1.4.12.2 jdolecek
197 1.4.12.2 jdolecek #define DEPS "ptrace_common"
198 1.4.12.2 jdolecek
199 1.4.12.2 jdolecek MODULE(MODULE_CLASS_EXEC, ptrace, DEPS);
200 1.4.12.2 jdolecek
201 1.4.12.2 jdolecek static int
202 1.4.12.2 jdolecek ptrace_modcmd(modcmd_t cmd, void *arg)
203 1.4.12.2 jdolecek {
204 1.4.12.2 jdolecek int error;
205 1.4.12.2 jdolecek
206 1.4.12.2 jdolecek switch (cmd) {
207 1.4.12.2 jdolecek case MODULE_CMD_INIT:
208 1.4.12.2 jdolecek error = syscall_establish(&emul_netbsd, ptrace_syscalls);
209 1.4.12.2 jdolecek break;
210 1.4.12.2 jdolecek case MODULE_CMD_FINI:
211 1.4.12.2 jdolecek error = syscall_disestablish(&emul_netbsd, ptrace_syscalls);
212 1.4.12.2 jdolecek break;
213 1.4.12.2 jdolecek default:
214 1.4.12.2 jdolecek error = ENOTTY;
215 1.4.12.2 jdolecek break;
216 1.4.12.2 jdolecek }
217 1.4.12.2 jdolecek return error;
218 1.4.12.2 jdolecek }
219