1*e1ae44b8Sriastradh /* $NetBSD: sys_ptrace.c,v 1.12 2022/07/10 14:07:55 riastradh Exp $ */
2a60b9909Spgoyette
3a60b9909Spgoyette /*-
4a60b9909Spgoyette * Copyright (c) 2008, 2009 The NetBSD Foundation, Inc.
5a60b9909Spgoyette * All rights reserved.
6a60b9909Spgoyette *
7a60b9909Spgoyette * This code is derived from software contributed to The NetBSD Foundation
8a60b9909Spgoyette * by Andrew Doran.
9a60b9909Spgoyette *
10a60b9909Spgoyette * Redistribution and use in source and binary forms, with or without
11a60b9909Spgoyette * modification, are permitted provided that the following conditions
12a60b9909Spgoyette * are met:
13a60b9909Spgoyette * 1. Redistributions of source code must retain the above copyright
14a60b9909Spgoyette * notice, this list of conditions and the following disclaimer.
15a60b9909Spgoyette * 2. Redistributions in binary form must reproduce the above copyright
16a60b9909Spgoyette * notice, this list of conditions and the following disclaimer in the
17a60b9909Spgoyette * documentation and/or other materials provided with the distribution.
18a60b9909Spgoyette *
19a60b9909Spgoyette * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20a60b9909Spgoyette * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21a60b9909Spgoyette * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22a60b9909Spgoyette * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23a60b9909Spgoyette * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24a60b9909Spgoyette * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25a60b9909Spgoyette * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26a60b9909Spgoyette * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27a60b9909Spgoyette * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28a60b9909Spgoyette * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29a60b9909Spgoyette * POSSIBILITY OF SUCH DAMAGE.
30a60b9909Spgoyette */
31a60b9909Spgoyette
32a60b9909Spgoyette /*-
33a60b9909Spgoyette * Copyright (c) 1982, 1986, 1989, 1993
34a60b9909Spgoyette * The Regents of the University of California. All rights reserved.
35a60b9909Spgoyette * (c) UNIX System Laboratories, Inc.
36a60b9909Spgoyette * All or some portions of this file are derived from material licensed
37a60b9909Spgoyette * to the University of California by American Telephone and Telegraph
38a60b9909Spgoyette * Co. or Unix System Laboratories, Inc. and are reproduced herein with
39a60b9909Spgoyette * the permission of UNIX System Laboratories, Inc.
40a60b9909Spgoyette *
41a60b9909Spgoyette * This code is derived from software contributed to Berkeley by
42a60b9909Spgoyette * Jan-Simon Pendry.
43a60b9909Spgoyette *
44a60b9909Spgoyette * Redistribution and use in source and binary forms, with or without
45a60b9909Spgoyette * modification, are permitted provided that the following conditions
46a60b9909Spgoyette * are met:
47a60b9909Spgoyette * 1. Redistributions of source code must retain the above copyright
48a60b9909Spgoyette * notice, this list of conditions and the following disclaimer.
49a60b9909Spgoyette * 2. Redistributions in binary form must reproduce the above copyright
50a60b9909Spgoyette * notice, this list of conditions and the following disclaimer in the
51a60b9909Spgoyette * documentation and/or other materials provided with the distribution.
52a60b9909Spgoyette * 3. Neither the name of the University nor the names of its contributors
53a60b9909Spgoyette * may be used to endorse or promote products derived from this software
54a60b9909Spgoyette * without specific prior written permission.
55a60b9909Spgoyette *
56a60b9909Spgoyette * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
57a60b9909Spgoyette * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
58a60b9909Spgoyette * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
59a60b9909Spgoyette * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
60a60b9909Spgoyette * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
61a60b9909Spgoyette * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
62a60b9909Spgoyette * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
63a60b9909Spgoyette * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
64a60b9909Spgoyette * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
65a60b9909Spgoyette * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
66a60b9909Spgoyette * SUCH DAMAGE.
67a60b9909Spgoyette *
68a60b9909Spgoyette * from: @(#)sys_process.c 8.1 (Berkeley) 6/10/93
69a60b9909Spgoyette */
70a60b9909Spgoyette
71a60b9909Spgoyette #include <sys/cdefs.h>
72*e1ae44b8Sriastradh __KERNEL_RCSID(0, "$NetBSD: sys_ptrace.c,v 1.12 2022/07/10 14:07:55 riastradh Exp $");
73a60b9909Spgoyette
74a60b9909Spgoyette #ifdef _KERNEL_OPT
75a60b9909Spgoyette #include "opt_ptrace.h"
76a60b9909Spgoyette #endif
77a60b9909Spgoyette
78a60b9909Spgoyette #include <sys/param.h>
79a60b9909Spgoyette #include <sys/systm.h>
80a60b9909Spgoyette #include <sys/proc.h>
81a60b9909Spgoyette #include <sys/errno.h>
82a60b9909Spgoyette #include <sys/exec.h>
83a60b9909Spgoyette #include <sys/pax.h>
84a60b9909Spgoyette #include <sys/ptrace.h>
85a60b9909Spgoyette #include <sys/uio.h>
86a60b9909Spgoyette #include <sys/ras.h>
87a60b9909Spgoyette #include <sys/kmem.h>
88a60b9909Spgoyette #include <sys/kauth.h>
89a60b9909Spgoyette #include <sys/mount.h>
90a60b9909Spgoyette #include <sys/syscallargs.h>
91a60b9909Spgoyette #include <sys/syscallvar.h>
92a60b9909Spgoyette #include <sys/syscall.h>
93484b4a02Skamil #include <sys/module.h>
94a60b9909Spgoyette
95a60b9909Spgoyette #include <uvm/uvm_extern.h>
96a60b9909Spgoyette
97a60b9909Spgoyette #include <machine/reg.h>
98a60b9909Spgoyette
99a60b9909Spgoyette /*
100a60b9909Spgoyette * PTRACE methods
101a60b9909Spgoyette */
102a60b9909Spgoyette
103a60b9909Spgoyette static int
ptrace_copyin_piod(struct ptrace_io_desc * piod,const void * addr,size_t len)104f818d5c4Schristos ptrace_copyin_piod(struct ptrace_io_desc *piod, const void *addr, size_t len)
105a60b9909Spgoyette {
106f818d5c4Schristos if (len != 0 && sizeof(*piod) != len)
107f818d5c4Schristos return EINVAL;
108f818d5c4Schristos
109a60b9909Spgoyette return copyin(addr, piod, sizeof(*piod));
110a60b9909Spgoyette }
111a60b9909Spgoyette
112f818d5c4Schristos static int
ptrace_copyout_piod(const struct ptrace_io_desc * piod,void * addr,size_t len)113f818d5c4Schristos ptrace_copyout_piod(const struct ptrace_io_desc *piod, void *addr, size_t len)
114a60b9909Spgoyette {
115f818d5c4Schristos if (len != 0 && sizeof(*piod) != len)
116f818d5c4Schristos return EINVAL;
117f818d5c4Schristos
118f818d5c4Schristos return copyout(piod, addr, sizeof(*piod));
119f818d5c4Schristos }
120f818d5c4Schristos
121f818d5c4Schristos static int
ptrace_copyin_siginfo(struct ptrace_siginfo * psi,const void * addr,size_t len)122f818d5c4Schristos ptrace_copyin_siginfo(struct ptrace_siginfo *psi, const void *addr, size_t len)
123f818d5c4Schristos {
124f818d5c4Schristos if (sizeof(*psi) != len)
125f818d5c4Schristos return EINVAL;
126f818d5c4Schristos
127f818d5c4Schristos return copyin(addr, psi, sizeof(*psi));
128f818d5c4Schristos }
129f818d5c4Schristos
130f818d5c4Schristos static int
ptrace_copyout_siginfo(const struct ptrace_siginfo * psi,void * addr,size_t len)131f818d5c4Schristos ptrace_copyout_siginfo(const struct ptrace_siginfo *psi, void *addr, size_t len)
132f818d5c4Schristos {
133f818d5c4Schristos if (sizeof(*psi) != len)
134f818d5c4Schristos return EINVAL;
135f818d5c4Schristos
136f818d5c4Schristos return copyout(psi, addr, sizeof(*psi));
137a60b9909Spgoyette }
138a60b9909Spgoyette
1394f79a484Skamil static int
ptrace_copyout_lwpstatus(const struct ptrace_lwpstatus * pls,void * addr,size_t len)1404f79a484Skamil ptrace_copyout_lwpstatus(const struct ptrace_lwpstatus *pls, void *addr,
1414f79a484Skamil size_t len)
1424f79a484Skamil {
1434f79a484Skamil
1444f79a484Skamil return copyout(pls, addr, len);
1454f79a484Skamil }
1464f79a484Skamil
147a60b9909Spgoyette static struct ptrace_methods native_ptm = {
148f818d5c4Schristos .ptm_copyin_piod = ptrace_copyin_piod,
149f818d5c4Schristos .ptm_copyout_piod = ptrace_copyout_piod,
150f818d5c4Schristos .ptm_copyin_siginfo = ptrace_copyin_siginfo,
151f818d5c4Schristos .ptm_copyout_siginfo = ptrace_copyout_siginfo,
1524f79a484Skamil .ptm_copyout_lwpstatus = ptrace_copyout_lwpstatus,
15318cd37a8Spgoyette .ptm_doregs = process_doregs,
15418cd37a8Spgoyette .ptm_dofpregs = process_dofpregs,
155988eb7edSkamil .ptm_dodbregs = process_dodbregs,
156a60b9909Spgoyette };
157a60b9909Spgoyette
158a60b9909Spgoyette static const struct syscall_package ptrace_syscalls[] = {
159a60b9909Spgoyette { SYS_ptrace, 0, (sy_call_t *)sys_ptrace },
160a60b9909Spgoyette { 0, 0, NULL },
161a60b9909Spgoyette };
162a60b9909Spgoyette
163a60b9909Spgoyette /*
164a60b9909Spgoyette * Process debugging system call.
165a60b9909Spgoyette */
166a60b9909Spgoyette int
sys_ptrace(struct lwp * l,const struct sys_ptrace_args * uap,register_t * retval)167a60b9909Spgoyette sys_ptrace(struct lwp *l, const struct sys_ptrace_args *uap, register_t *retval)
168a60b9909Spgoyette {
169a60b9909Spgoyette /* {
170a60b9909Spgoyette syscallarg(int) req;
171a60b9909Spgoyette syscallarg(pid_t) pid;
172a60b9909Spgoyette syscallarg(void *) addr;
173a60b9909Spgoyette syscallarg(int) data;
174a60b9909Spgoyette } */
175a60b9909Spgoyette
176a60b9909Spgoyette return do_ptrace(&native_ptm, l, SCARG(uap, req), SCARG(uap, pid),
177a60b9909Spgoyette SCARG(uap, addr), SCARG(uap, data), retval);
178a60b9909Spgoyette }
179484b4a02Skamil
180484b4a02Skamil #define DEPS "ptrace_common"
181484b4a02Skamil
182484b4a02Skamil MODULE(MODULE_CLASS_EXEC, ptrace, DEPS);
183484b4a02Skamil
184484b4a02Skamil static int
ptrace_init(void)185c3233fe3Spgoyette ptrace_init(void)
186c3233fe3Spgoyette {
187c3233fe3Spgoyette int error;
188c3233fe3Spgoyette
189c3233fe3Spgoyette error = syscall_establish(&emul_netbsd, ptrace_syscalls);
190c3233fe3Spgoyette return error;
191c3233fe3Spgoyette }
192c3233fe3Spgoyette
193c3233fe3Spgoyette static int
ptrace_fini(void)194c3233fe3Spgoyette ptrace_fini(void)
195c3233fe3Spgoyette {
196c3233fe3Spgoyette int error;
197c3233fe3Spgoyette
198c3233fe3Spgoyette error = syscall_disestablish(&emul_netbsd, ptrace_syscalls);
199c3233fe3Spgoyette return error;
200c3233fe3Spgoyette }
201c3233fe3Spgoyette
202c3233fe3Spgoyette
203c3233fe3Spgoyette static int
ptrace_modcmd(modcmd_t cmd,void * arg)204484b4a02Skamil ptrace_modcmd(modcmd_t cmd, void *arg)
205484b4a02Skamil {
206484b4a02Skamil int error;
207484b4a02Skamil
208484b4a02Skamil switch (cmd) {
209484b4a02Skamil case MODULE_CMD_INIT:
210c8be1c47Spgoyette error = ptrace_init();
211484b4a02Skamil break;
212484b4a02Skamil case MODULE_CMD_FINI:
213c3233fe3Spgoyette error = ptrace_fini();
214484b4a02Skamil break;
215484b4a02Skamil default:
216484b4a02Skamil error = ENOTTY;
217484b4a02Skamil break;
218484b4a02Skamil }
219484b4a02Skamil return error;
220484b4a02Skamil }
221