1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
|
/* $NetBSD: sys_process_lwpstatus.c,v 1.4 2022/07/10 17:47:58 riastradh Exp $ */
/*-
* Copyright (c) 2019 The NetBSD Foundation, Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: sys_process_lwpstatus.c,v 1.4 2022/07/10 17:47:58 riastradh Exp $");
#ifdef _KERNEL_OPT
#include "opt_ptrace.h"
#include "opt_ktrace.h"
#include "opt_pax.h"
#include "opt_compat_netbsd32.h"
#endif
#if defined(__HAVE_COMPAT_NETBSD32) && !defined(COMPAT_NETBSD32) \
&& !defined(_RUMPKERNEL)
#define COMPAT_NETBSD32
#endif
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/errno.h>
#include <sys/lwp.h>
#include <sys/proc.h>
#include <sys/ptrace.h>
#ifndef PTRACE_REGS_ALIGN
#define PTRACE_REGS_ALIGN /* nothing */
#endif
void
ptrace_read_lwpstatus(struct lwp *l, struct ptrace_lwpstatus *pls)
{
pls->pl_lwpid = l->l_lid;
memcpy(&pls->pl_sigmask, &l->l_sigmask, sizeof(pls->pl_sigmask));
memcpy(&pls->pl_sigpend, &l->l_sigpend.sp_set, sizeof(pls->pl_sigpend));
if (l->l_name == NULL)
memset(&pls->pl_name, 0, PL_LNAMELEN);
else {
KASSERT(strlen(l->l_name) < PL_LNAMELEN);
strncpy(pls->pl_name, l->l_name, PL_LNAMELEN);
}
#ifdef PTRACE_LWP_GETPRIVATE
pls->pl_private = (void *)(intptr_t)PTRACE_LWP_GETPRIVATE(l);
#else
pls->pl_private = l->l_private;
#endif
}
void
process_read_lwpstatus(struct lwp *l, struct ptrace_lwpstatus *pls)
{
ptrace_read_lwpstatus(l, pls);
}
int
ptrace_update_lwp(struct proc *t, struct lwp **lt, lwpid_t lid)
{
if (lid == 0 || lid == (*lt)->l_lid || t->p_nlwps == 1)
return 0;
mutex_enter(t->p_lock);
lwp_delref2(*lt);
*lt = lwp_find(t, lid);
if (*lt == NULL) {
mutex_exit(t->p_lock);
return ESRCH;
}
if ((*lt)->l_flag & LW_SYSTEM) {
mutex_exit(t->p_lock);
*lt = NULL;
return EINVAL;
}
lwp_addref(*lt);
mutex_exit(t->p_lock);
return 0;
}
int
process_validfpregs(struct lwp *l)
{
#if defined(PT_SETFPREGS) || defined(PT_GETFPREGS)
return (l->l_flag & LW_SYSTEM) == 0;
#else
return 0;
#endif
}
int
process_validregs(struct lwp *l)
{
#if defined(PT_SETREGS) || defined(PT_GETREGS)
return (l->l_flag & LW_SYSTEM) == 0;
#else
return 0;
#endif
}
int
process_validdbregs(struct lwp *l)
{
#if defined(PT_SETDBREGS) || defined(PT_GETDBREGS)
return (l->l_flag & LW_SYSTEM) == 0;
#else
return 0;
#endif
}
#ifdef PT_REGISTERS
static int
proc_regio(struct lwp *l, struct uio *uio, size_t ks, ptrace_regrfunc_t r,
ptrace_regwfunc_t w)
{
char buf[1024] PTRACE_REGS_ALIGN;
int error;
char *kv;
size_t kl;
if (ks > sizeof(buf))
return E2BIG;
if (uio->uio_offset < 0 || uio->uio_offset > (off_t)ks)
return EINVAL;
kv = buf + uio->uio_offset;
kl = ks - uio->uio_offset;
if (kl > uio->uio_resid)
kl = uio->uio_resid;
error = (*r)(l, buf, &ks);
if (error == 0)
error = uiomove(kv, kl, uio);
if (error == 0 && uio->uio_rw == UIO_WRITE) {
if (l->l_stat != LSSTOP)
error = EBUSY;
else
error = (*w)(l, buf, ks);
}
uio->uio_offset = 0;
return error;
}
#endif
int
process_doregs(struct lwp *curl /*tracer*/,
struct lwp *l /*traced*/,
struct uio *uio)
{
#if defined(PT_GETREGS) || defined(PT_SETREGS)
size_t s;
ptrace_regrfunc_t r;
ptrace_regwfunc_t w;
#ifdef COMPAT_NETBSD32
const bool pk32 = (curl->l_proc->p_flag & PK_32) != 0;
if (__predict_false(pk32)) {
if ((l->l_proc->p_flag & PK_32) == 0) {
// 32 bit tracer can't trace 64 bit process
return EINVAL;
}
s = sizeof(process_reg32);
r = __FPTRCAST(ptrace_regrfunc_t, process_read_regs32);
w = __FPTRCAST(ptrace_regwfunc_t, process_write_regs32);
} else
#endif
{
s = sizeof(struct reg);
r = __FPTRCAST(ptrace_regrfunc_t, process_read_regs);
w = __FPTRCAST(ptrace_regwfunc_t, process_write_regs);
}
return proc_regio(l, uio, s, r, w);
#else
return EINVAL;
#endif
}
int
process_dofpregs(struct lwp *curl /*tracer*/,
struct lwp *l /*traced*/,
struct uio *uio)
{
#if defined(PT_GETFPREGS) || defined(PT_SETFPREGS)
size_t s;
ptrace_regrfunc_t r;
ptrace_regwfunc_t w;
#ifdef COMPAT_NETBSD32
const bool pk32 = (curl->l_proc->p_flag & PK_32) != 0;
if (__predict_false(pk32)) {
if ((l->l_proc->p_flag & PK_32) == 0) {
// 32 bit tracer can't trace 64 bit process
return EINVAL;
}
s = sizeof(process_fpreg32);
r = (ptrace_regrfunc_t)process_read_fpregs32;
w = (ptrace_regwfunc_t)process_write_fpregs32;
} else
#endif
{
s = sizeof(struct fpreg);
r = (ptrace_regrfunc_t)process_read_fpregs;
w = (ptrace_regwfunc_t)process_write_fpregs;
}
return proc_regio(l, uio, s, r, w);
#else
return EINVAL;
#endif
}
int
process_dodbregs(struct lwp *curl /*tracer*/,
struct lwp *l /*traced*/,
struct uio *uio)
{
#if defined(PT_GETDBREGS) || defined(PT_SETDBREGS)
size_t s;
ptrace_regrfunc_t r;
ptrace_regwfunc_t w;
#ifdef COMPAT_NETBSD32
const bool pk32 = (curl->l_proc->p_flag & PK_32) != 0;
if (__predict_false(pk32)) {
if ((l->l_proc->p_flag & PK_32) == 0) {
// 32 bit tracer can't trace 64 bit process
return EINVAL;
}
s = sizeof(process_dbreg32);
r = (ptrace_regrfunc_t)process_read_dbregs32;
w = (ptrace_regwfunc_t)process_write_dbregs32;
} else
#endif
{
s = sizeof(struct dbreg);
r = (ptrace_regrfunc_t)process_read_dbregs;
w = (ptrace_regwfunc_t)process_write_dbregs;
}
return proc_regio(l, uio, s, r, w);
#else
return EINVAL;
#endif
}
|