1/* $NetBSD: sys_process.c,v 1.175 2016/11/02 00:11:59 pgoyette Exp $ */
2
3/*-
4 * Copyright (c) 2008, 2009 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) 1982, 1986, 1989, 1993
34 * The Regents of the University of California. All rights reserved.
35 * (c) UNIX System Laboratories, Inc.
36 * All or some portions of this file are derived from material licensed
37 * to the University of California by American Telephone and Telegraph
38 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
39 * the permission of UNIX System Laboratories, Inc.
40 *
41 * This code is derived from software contributed to Berkeley by
42 * Jan-Simon Pendry.
43 *
44 * Redistribution and use in source and binary forms, with or without
45 * modification, are permitted provided that the following conditions
46 * are met:
47 * 1. Redistributions of source code must retain the above copyright
48 * notice, this list of conditions and the following disclaimer.
49 * 2. Redistributions in binary form must reproduce the above copyright
50 * notice, this list of conditions and the following disclaimer in the
51 * documentation and/or other materials provided with the distribution.
52 * 3. Neither the name of the University nor the names of its contributors
53 * may be used to endorse or promote products derived from this software
54 * without specific prior written permission.
55 *
56 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
57 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
58 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
59 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
60 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
61 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
62 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
63 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
64 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
65 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
66 * SUCH DAMAGE.
67 *
68 * from: @(#)sys_process.c 8.1 (Berkeley) 6/10/93
69 */
70
71/*-
72 * Copyright (c) 1993 Jan-Simon Pendry.
73 * Copyright (c) 1994 Christopher G. Demetriou. All rights reserved.
74 *
75 * This code is derived from software contributed to Berkeley by
76 * Jan-Simon Pendry.
77 *
78 * Redistribution and use in source and binary forms, with or without
79 * modification, are permitted provided that the following conditions
80 * are met:
81 * 1. Redistributions of source code must retain the above copyright
82 * notice, this list of conditions and the following disclaimer.
83 * 2. Redistributions in binary form must reproduce the above copyright
84 * notice, this list of conditions and the following disclaimer in the
85 * documentation and/or other materials provided with the distribution.
86 * 3. All advertising materials mentioning features or use of this software
87 * must display the following acknowledgement:
88 * This product includes software developed by the University of
89 * California, Berkeley and its contributors.
90 * 4. Neither the name of the University nor the names of its contributors
91 * may be used to endorse or promote products derived from this software
92 * without specific prior written permission.
93 *
94 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
95 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
96 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
97 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
98 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
99 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
100 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
101 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
102 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
103 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
104 * SUCH DAMAGE.
105 *
106 * from: @(#)sys_process.c 8.1 (Berkeley) 6/10/93
107 */
108
109/*
110 * References:
111 * (1) Bach's "The Design of the UNIX Operating System",
112 * (2) sys/miscfs/procfs from UCB's 4.4BSD-Lite distribution,
113 * (3) the "4.4BSD Programmer's Reference Manual" published
114 * by USENIX and O'Reilly & Associates.
115 * The 4.4BSD PRM does a reasonably good job of documenting what the various
116 * ptrace() requests should actually do, and its text is quoted several times
117 * in this file.
118 */
119
120#include <sys/cdefs.h>
121__KERNEL_RCSID(0, "$NetBSD: sys_process.c,v 1.175 2016/11/02 00:11:59 pgoyette Exp $");
122
123#ifdef _KERNEL_OPT
124#include "opt_ptrace.h"
125#include "opt_ktrace.h"
126#include "opt_pax.h"
127#endif
128
129#include <sys/param.h>
130#include <sys/systm.h>
131#include <sys/proc.h>
132#include <sys/errno.h>
133#include <sys/exec.h>
134#include <sys/pax.h>
135#include <sys/ptrace.h>
136#include <sys/uio.h>
137#include <sys/ras.h>
138#include <sys/kmem.h>
139#include <sys/kauth.h>
140#include <sys/mount.h>
141#include <sys/syscallargs.h>
142
143#include <uvm/uvm_extern.h>
144
145#include <machine/reg.h>
146
147#if defined(KTRACE) || defined(PTRACE_HOOKS)
148int
149process_domem(struct lwp *curl /*tracer*/,
150 struct lwp *l /*traced*/,
151 struct uio *uio)
152{
153 struct proc *p = l->l_proc; /* traced */
154 struct vmspace *vm;
155 int error;
156
157 size_t len;
158#ifdef PMAP_NEED_PROCWR
159 vaddr_t addr;
160#endif
161
162 error = 0;
163 len = uio->uio_resid;
164
165 if (len == 0)
166 return 0;
167
168#ifdef PMAP_NEED_PROCWR
169 addr = uio->uio_offset;
170#endif
171
172 vm = p->p_vmspace;
173
174 mutex_enter(&vm->vm_map.misc_lock);
175 if ((l->l_flag & LW_WEXIT) || vm->vm_refcnt < 1)
176 error = EFAULT;
177 if (error == 0)
178 p->p_vmspace->vm_refcnt++; /* XXX */
179 mutex_exit(&vm->vm_map.misc_lock);
180 if (error != 0)
181 return error;
182 error = uvm_io(&vm->vm_map, uio, pax_mprotect_prot(l));
183 uvmspace_free(vm);
184
185#ifdef PMAP_NEED_PROCWR
186 if (error == 0 && uio->uio_rw == UIO_WRITE)
187 pmap_procwr(p, addr, len);
188#endif
189 return error;
190}
191
192void
193process_stoptrace(void)
194{
195 struct lwp *l = curlwp;
196 struct proc *p = l->l_proc, *pp;
197
198 mutex_enter(proc_lock);
199 mutex_enter(p->p_lock);
200 pp = p->p_pptr;
201 if (pp->p_pid == 1) {
202 CLR(p->p_slflag, PSL_SYSCALL); /* XXXSMP */
203 mutex_exit(p->p_lock);
204 mutex_exit(proc_lock);
205 return;
206 }
207
208 p->p_xsig = SIGTRAP;
209 proc_stop(p, 1, SIGSTOP);
210 mutex_exit(proc_lock);
211
212 if (sigispending(l, 0)) {
213 lwp_lock(l);
214 l->l_flag |= LW_PENDSIG;
215 lwp_unlock(l);
216 }
217 mutex_exit(p->p_lock);
218}
219#endif /* KTRACE || PTRACE_HOOKS */
220
221/*
222 * Dummy routine so that ptrace_common module will fail to load if this
223 * routine is not defined.
224 */
225#if defined(PTRACE_HOOKS)
226void
227ptrace_hooks(void)
228{
229
230}
231#endif
232