1 /*
2 * Copyright (c) 1993 Jan-Simon Pendry
3 * Copyright (c) 1993
4 * The Regents of the University of California. All rights reserved.
5 *
6 * This code is derived from software contributed to Berkeley by
7 * Jan-Simon Pendry.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 *
33 * @(#)procfs_ctl.c 8.4 (Berkeley) 6/15/94
34 *
35 * From:
36 * $FreeBSD: src/sys/miscfs/procfs/procfs_ctl.c,v 1.20.2.2 2002/01/22 17:22:59 nectar Exp $
37 */
38
39 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/proc.h>
42 #include <sys/caps.h>
43 #include <sys/vnode.h>
44 #include <sys/ptrace.h>
45 #include <sys/signalvar.h>
46 #include <vfs/procfs/procfs.h>
47
48 #include <sys/signal2.h>
49 #include <sys/spinlock2.h>
50
51 #include <vm/vm.h>
52
53 #ifndef FIX_SSTEP
54 #define FIX_SSTEP(lp)
55 #endif
56
57 /*
58 * True iff process (p) is in trace wait state
59 * relative to process (curp)
60 */
61 #define TRACE_WAIT_P(curp, p) \
62 (((p)->p_stat == SSTOP) && \
63 (p)->p_pptr == (curp) && \
64 ((p)->p_flags & P_TRACED))
65
66 #define PROCFS_CTL_ATTACH 1
67 #define PROCFS_CTL_DETACH 2
68 #define PROCFS_CTL_STEP 3
69 #define PROCFS_CTL_RUN 4
70 #define PROCFS_CTL_WAIT 5
71
72 static vfs_namemap_t ctlnames[] = {
73 /* special /proc commands */
74 { "attach", PROCFS_CTL_ATTACH },
75 { "detach", PROCFS_CTL_DETACH },
76 { "step", PROCFS_CTL_STEP },
77 { "run", PROCFS_CTL_RUN },
78 { "wait", PROCFS_CTL_WAIT },
79 { 0 },
80 };
81
82 static vfs_namemap_t signames[] = {
83 /* regular signal names */
84 { "hup", SIGHUP }, { "int", SIGINT },
85 { "quit", SIGQUIT }, { "ill", SIGILL },
86 { "trap", SIGTRAP }, { "abrt", SIGABRT },
87 { "iot", SIGIOT }, { "emt", SIGEMT },
88 { "fpe", SIGFPE }, { "kill", SIGKILL },
89 { "bus", SIGBUS }, { "segv", SIGSEGV },
90 { "sys", SIGSYS }, { "pipe", SIGPIPE },
91 { "alrm", SIGALRM }, { "term", SIGTERM },
92 { "urg", SIGURG }, { "stop", SIGSTOP },
93 { "tstp", SIGTSTP }, { "cont", SIGCONT },
94 { "chld", SIGCHLD }, { "ttin", SIGTTIN },
95 { "ttou", SIGTTOU }, { "io", SIGIO },
96 { "xcpu", SIGXCPU }, { "xfsz", SIGXFSZ },
97 { "vtalrm", SIGVTALRM }, { "prof", SIGPROF },
98 { "winch", SIGWINCH }, { "info", SIGINFO },
99 { "usr1", SIGUSR1 }, { "usr2", SIGUSR2 },
100 { 0 },
101 };
102
103 static int procfs_control (struct proc *curp, struct lwp *lp, int op);
104
105 static int
procfs_control(struct proc * curp,struct lwp * lp,int op)106 procfs_control(struct proc *curp, struct lwp *lp, int op)
107 {
108 struct proc *p = lp->lwp_proc;
109 int error;
110
111 ASSERT_LWKT_TOKEN_HELD(&p->p_token);
112
113 /* Can't trace a process that's currently exec'ing. */
114 if ((p->p_flags & P_INEXEC) != 0)
115 return EAGAIN;
116 /*
117 * Authorization check: rely on normal debugging protection, except
118 * allow processes to disengage debugging on a process onto which
119 * they have previously attached, but no longer have permission to
120 * debug.
121 */
122 if (op != PROCFS_CTL_DETACH) {
123 if (securelevel > 0 && p->p_pid == 1)
124 return (EPERM);
125
126 if (!CHECKIO(curp, p) || p_trespass(curp->p_ucred, p->p_ucred))
127 return (EPERM);
128 }
129
130 /*
131 * Attach - attaches the target process for debugging
132 * by the calling process.
133 */
134 if (op == PROCFS_CTL_ATTACH) {
135 /* check whether already being traced */
136 if (p->p_flags & P_TRACED)
137 return (EBUSY);
138
139 /* can't trace yourself! */
140 if (p->p_pid == curp->p_pid)
141 return (EINVAL);
142
143 /*
144 * Go ahead and set the trace flag.
145 * Save the old parent (it's reset in
146 * _DETACH, and also in kern_exit.c:wait4()
147 * Reparent the process so that the tracing
148 * proc gets to see all the action.
149 * Stop the target.
150 */
151 p->p_flags |= P_TRACED;
152 p->p_xstat = 0; /* XXX ? */
153 if (p->p_pptr != curp) {
154 p->p_oppid = p->p_pptr->p_pid;
155 proc_reparent(p, curp);
156 }
157 proc_stop(p, SSTOP);
158 return (0);
159 }
160
161 /*
162 * Target process must be stopped, owned by (curp) and
163 * be set up for tracing (P_TRACED flag set).
164 * Allow DETACH to take place at any time for sanity.
165 * Allow WAIT any time, of course.
166 */
167 switch (op) {
168 case PROCFS_CTL_DETACH:
169 case PROCFS_CTL_WAIT:
170 break;
171
172 default:
173 if (!TRACE_WAIT_P(curp, p))
174 return (EBUSY);
175 }
176
177
178 #ifdef FIX_SSTEP
179 /*
180 * do single-step fixup if needed
181 */
182 FIX_SSTEP(lp);
183 #endif
184
185 /*
186 * Don't deliver any signal by default.
187 * To continue with a signal, just send
188 * the signal name to the ctl file
189 */
190 p->p_xstat = 0;
191
192 switch (op) {
193 /*
194 * Detach. Cleans up the target process, reparent it if possible
195 * and set it running once more.
196 */
197 case PROCFS_CTL_DETACH:
198 /* if not being traced, then this is a painless no-op */
199 if ((p->p_flags & P_TRACED) == 0)
200 return (0);
201
202 /* not being traced any more */
203 p->p_flags &= ~P_TRACED;
204
205 /* remove pending SIGTRAP, else the process will die */
206 spin_lock(&lp->lwp_spin);
207 lwp_delsig(lp, SIGTRAP, 1);
208 spin_unlock(&lp->lwp_spin);
209
210 /* give process back to original parent */
211 if (p->p_oppid != p->p_pptr->p_pid) {
212 struct proc *pp;
213
214 pp = pfs_pfind(p->p_oppid);
215 if (pp) {
216 proc_reparent(p, pp);
217 pfs_pdone(pp);
218 }
219 }
220
221 p->p_oppid = 0;
222 p->p_flags &= ~P_WAITED; /* XXX ? */
223 wakeup((caddr_t) curp); /* XXX for CTL_WAIT below ? */
224
225 break;
226
227 /*
228 * Step. Let the target process execute a single instruction.
229 */
230 case PROCFS_CTL_STEP:
231 LWPHOLD(lp);
232 error = procfs_sstep(lp);
233 LWPRELE(lp);
234 if (error)
235 return (error);
236 break;
237
238 /*
239 * Run. Let the target process continue running until a breakpoint
240 * or some other trap.
241 */
242 case PROCFS_CTL_RUN:
243 break;
244
245 /*
246 * Wait for the target process to stop.
247 * If the target is not being traced then just wait
248 * to enter
249 */
250 case PROCFS_CTL_WAIT:
251 error = 0;
252 if (p->p_flags & P_TRACED) {
253 while (error == 0 &&
254 p->p_stat != SSTOP &&
255 (p->p_flags & P_TRACED) &&
256 (p->p_pptr == curp))
257 {
258 error = tsleep((caddr_t) p, PCATCH,
259 "procfsx", 0);
260 }
261 if (error == 0 && !TRACE_WAIT_P(curp, p))
262 error = EBUSY;
263 } else {
264 while (error == 0 && p->p_stat != SSTOP) {
265 error = tsleep((caddr_t) p, PCATCH,
266 "procfs", 0);
267 }
268 }
269 return (error);
270
271 default:
272 panic("procfs_control");
273 }
274
275 /*
276 * If the process is in a stopped state, make it runnable again.
277 * Do not set LWP_MP_BREAKTSLEEP - that is, do not break a tsleep
278 * that might be in progress.
279 */
280 if (p->p_stat == SSTOP)
281 proc_unstop(p, SSTOP);
282 return (0);
283 }
284
285 int
procfs_doctl(struct proc * curp,struct lwp * lp,struct pfsnode * pfs,struct uio * uio)286 procfs_doctl(struct proc *curp, struct lwp *lp, struct pfsnode *pfs,
287 struct uio *uio)
288 {
289 struct proc *p = lp->lwp_proc;
290 int xlen;
291 int error;
292 char msg[PROCFS_CTLLEN+1];
293 vfs_namemap_t *nm;
294
295 ASSERT_LWKT_TOKEN_HELD(&p->p_token);
296
297 if (uio->uio_rw != UIO_WRITE)
298 return (EOPNOTSUPP);
299
300 xlen = PROCFS_CTLLEN;
301 error = vfs_getuserstr(uio, msg, &xlen);
302 if (error)
303 return (error);
304
305 /*
306 * Map signal names into signal generation
307 * or debug control. Unknown commands and/or signals
308 * return EOPNOTSUPP.
309 *
310 * Sending a signal while the process is being debugged
311 * also has the side effect of letting the target continue
312 * to run. There is no way to single-step a signal delivery.
313 */
314 error = EOPNOTSUPP;
315
316 nm = vfs_findname(ctlnames, msg, xlen);
317 if (nm) {
318 error = procfs_control(curp, lp, nm->nm_val);
319 } else {
320 nm = vfs_findname(signames, msg, xlen);
321 if (nm) {
322 if (TRACE_WAIT_P(curp, p)) {
323 p->p_xstat = nm->nm_val;
324 #ifdef FIX_SSTEP
325 FIX_SSTEP(lp);
326 #endif
327 /*
328 * Make the process runnable but do not
329 * break its tsleep.
330 */
331 proc_unstop(p, SSTOP);
332 } else {
333 ksignal(p, nm->nm_val);
334 }
335 error = 0;
336 }
337 }
338
339 return (error);
340 }
341