DragonFlyBSD Kernel Audit
sys/kern/sys_process.c
← back
  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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
/*
 * Copyright (c) 1994, Sean Eric Fagan
 * 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.
 * 3. All advertising materials mentioning features or use of this software
 *    must display the following acknowledgement:
 *	This product includes software developed by Sean Eric Fagan.
 * 4. The name of the author may not be used to endorse or promote products
 *    derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR 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 AUTHOR 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.
 *
 * $FreeBSD: src/sys/kern/sys_process.c,v 1.51.2.6 2003/01/08 03:06:45 kan Exp $
 */

#include <sys/param.h>
#include <sys/systm.h>
#include <sys/sysmsg.h>
#include <sys/uio.h>
#include <sys/proc.h>
#include <sys/caps.h>
#include <sys/vnode.h>
#include <sys/ptrace.h>
#include <sys/reg.h>
#include <sys/lock.h>

#include <vm/vm.h>
#include <vm/pmap.h>
#include <vm/vm_map.h>
#include <vm/vm_page.h>

#include <vfs/procfs/procfs.h>

#include <sys/thread2.h>
#include <sys/spinlock2.h>


/*
 * Process debugging system call.
 *
 * MPALMOSTSAFE
 */
int
sys_ptrace(struct sysmsg *sysmsg, const struct ptrace_args *uap)
{
	struct proc *p = curproc;

	/*
	 * XXX this obfuscation is to reduce stack usage, but the register
	 * structs may be too large to put on the stack anyway.
	 */
	union {
		struct ptrace_io_desc piod;
		struct dbreg dbreg;
		struct fpreg fpreg;
		struct reg reg;
	} r;
	void *addr;
	int error = 0;

	addr = &r;
	switch (uap->req) {
	case PT_GETREGS:
	case PT_GETFPREGS:
#ifdef PT_GETDBREGS
	case PT_GETDBREGS:
#endif
		break;
	case PT_SETREGS:
		error = copyin(uap->addr, &r.reg, sizeof r.reg);
		break;
	case PT_SETFPREGS:
		error = copyin(uap->addr, &r.fpreg, sizeof r.fpreg);
		break;
#ifdef PT_SETDBREGS
	case PT_SETDBREGS:
		error = copyin(uap->addr, &r.dbreg, sizeof r.dbreg);
		break;
#endif
	case PT_IO:
		error = copyin(uap->addr, &r.piod, sizeof r.piod);
		break;
	default:
		addr = uap->addr;
	}
	if (error)
		return (error);

	error = kern_ptrace(p, uap->req, uap->pid, addr, uap->data,
			&sysmsg->sysmsg_result);
	if (error)
		return (error);

	switch (uap->req) {
	case PT_IO:
		(void)copyout(&r.piod, uap->addr, sizeof r.piod);
		break;
	case PT_GETREGS:
		error = copyout(&r.reg, uap->addr, sizeof r.reg);
		break;
	case PT_GETFPREGS:
		error = copyout(&r.fpreg, uap->addr, sizeof r.fpreg);
		break;
#ifdef PT_GETDBREGS
	case PT_GETDBREGS:
		error = copyout(&r.dbreg, uap->addr, sizeof r.dbreg);
		break;
#endif
	}

	return (error);
}

int
kern_ptrace(struct proc *curp, int req, pid_t pid, void *addr,
	    int data, int *res)
{
	struct proc *p, *pp;
	struct lwp *lp;
	struct iovec iov;
	struct uio uio;
	struct ptrace_io_desc *piod;
	int error = 0;
	int write, tmp;
	int t;

	write = 0;
	if (req == PT_TRACE_ME) {
		p = curp;
		PHOLD(p);
	} else {
		if ((p = pfind(pid)) == NULL)
			return ESRCH;
	}
	if (!PRISON_CHECK(curp->p_ucred, p->p_ucred)) {
		PRELE(p);
		return (ESRCH);
	}
	if (p->p_flags & P_SYSTEM) {
		PRELE(p);
		return EINVAL;
	}

	lwkt_gettoken(&p->p_token);
	/* Can't trace a process that's currently exec'ing. */
	if ((p->p_flags & P_INEXEC) != 0) {
		lwkt_reltoken(&p->p_token);
		PRELE(p);
		return EAGAIN;
	}

	/*
	 * Permissions check
	 */
	switch (req) {
	case PT_TRACE_ME:
		/* Always legal. */
		break;

	case PT_ATTACH:
		/* Self */
		if (p->p_pid == curp->p_pid) {
			lwkt_reltoken(&p->p_token);
			PRELE(p);
			return EINVAL;
		}

		/* Already traced */
		if (p->p_flags & P_TRACED) {
			lwkt_reltoken(&p->p_token);
			PRELE(p);
			return EBUSY;
		}

		if (curp->p_flags & P_TRACED)
			for (pp = curp->p_pptr; pp != NULL; pp = pp->p_pptr)
				if (pp == p) {
					lwkt_reltoken(&p->p_token);
					PRELE(p);
					return (EINVAL);
				}

		/* not owned by you, has done setuid (unless you're root) */
		if ((p->p_ucred->cr_ruid != curp->p_ucred->cr_ruid) ||
		     (p->p_flags & P_SUGID)) {
			error = caps_priv_check(curp->p_ucred,
						SYSCAP_RESTRICTEDROOT);
			if (error) {
				lwkt_reltoken(&p->p_token);
				PRELE(p);
				return error;
			}
		}

		/* can't trace init when securelevel > 0 */
		if (securelevel > 0 && p->p_pid == 1) {
			lwkt_reltoken(&p->p_token);
			PRELE(p);
			return EPERM;
		}

		/* OK */
		break;

	case PT_READ_I:
	case PT_READ_D:
	case PT_WRITE_I:
	case PT_WRITE_D:
	case PT_IO:
	case PT_CONTINUE:
	case PT_KILL:
	case PT_STEP:
	case PT_DETACH:
#ifdef PT_GETREGS
	case PT_GETREGS:
#endif
#ifdef PT_SETREGS
	case PT_SETREGS:
#endif
#ifdef PT_GETFPREGS
	case PT_GETFPREGS:
#endif
#ifdef PT_SETFPREGS
	case PT_SETFPREGS:
#endif
#ifdef PT_GETDBREGS
	case PT_GETDBREGS:
#endif
#ifdef PT_SETDBREGS
	case PT_SETDBREGS:
#endif
		/* not being traced... */
		if ((p->p_flags & P_TRACED) == 0) {
			lwkt_reltoken(&p->p_token);
			PRELE(p);
			return EPERM;
		}

		/* not being traced by YOU */
		if (p->p_pptr != curp) {
			lwkt_reltoken(&p->p_token);
			PRELE(p);
			return EBUSY;
		}

		/* not currently stopped */
		if (p->p_stat != SSTOP ||
		    (p->p_flags & P_WAITED) == 0) {
			lwkt_reltoken(&p->p_token);
			PRELE(p);
			return EBUSY;
		}

		/* OK */
		break;

	default:
		lwkt_reltoken(&p->p_token);
		PRELE(p);
		return EINVAL;
	}

	/* XXX lwp */
	lp = FIRST_LWP_IN_PROC(p);
	if (lp == NULL) {
		lwkt_reltoken(&p->p_token);
		PRELE(p);
		return EINVAL;
	}

#ifdef FIX_SSTEP
	/*
	 * Single step fixup ala procfs
	 */
	FIX_SSTEP(lp);
#endif

	/*
	 * Actually do the requests
	 */

	*res = 0;

	switch (req) {
	case PT_TRACE_ME:
		/* set my trace flag and "owner" so it can read/write me */
		p->p_flags |= P_TRACED;
		p->p_oppid = p->p_pptr->p_pid;
		lwkt_reltoken(&p->p_token);
		PRELE(p);
		return 0;

	case PT_ATTACH:
		/* security check done above */
		p->p_flags |= P_TRACED;
		p->p_oppid = p->p_pptr->p_pid;
		proc_reparent(p, curp);
		data = SIGSTOP;
		goto sendsig;	/* in PT_CONTINUE below */

	case PT_STEP:
	case PT_CONTINUE:
	case PT_DETACH:
		/* Zero means do not send any signal */
		if (data < 0 || data >= _SIG_MAXSIG) {
			lwkt_reltoken(&p->p_token);
			PRELE(p);
			return EINVAL;
		}

		LWPHOLD(lp);

		if (req == PT_STEP) {
			if ((error = ptrace_single_step (lp))) {
				LWPRELE(lp);
				lwkt_reltoken(&p->p_token);
				PRELE(p);
				return error;
			}
		}

		if (addr != (void *)1) {
			if ((error = ptrace_set_pc (lp, (u_long)addr))) {
				LWPRELE(lp);
				lwkt_reltoken(&p->p_token);
				PRELE(p);
				return error;
			}
		}
		LWPRELE(lp);

		if (req == PT_DETACH) {
			/* reset process parent */
			if (p->p_oppid != p->p_pptr->p_pid) {
				struct proc *pp;

				pp = pfind(p->p_oppid);
				if (pp) {
					proc_reparent(p, pp);
					PRELE(pp);
				}
			}

			p->p_flags &= ~(P_TRACED | P_WAITED);
			p->p_oppid = 0;

			/* should we send SIGCHLD? */
		}

	sendsig:
		/*
		 * Deliver or queue signal.  If the process is stopped
		 * force it to be SACTIVE again.
		 */
		crit_enter();
		if (p->p_stat == SSTOP) {
			p->p_xstat = data;
			proc_unstop(p, SSTOP);
		} else if (data) {
			ksignal(p, data);
		}
		crit_exit();
		lwkt_reltoken(&p->p_token);
		PRELE(p);
		return 0;

	case PT_WRITE_I:
	case PT_WRITE_D:
		write = 1;
		/* fallthrough */
	case PT_READ_I:
	case PT_READ_D:
		/*
		 * NOTE! uio_offset represents the offset in the target
		 * process.  The iov is in the current process (the guy
		 * making the ptrace call) so uio_td must be the current
		 * process (though for a SYSSPACE transfer it doesn't
		 * really matter).
		 */
		tmp = 0;
		/* write = 0 set above */
		iov.iov_base = write ? (caddr_t)&data : (caddr_t)&tmp;
		iov.iov_len = sizeof(int);
		uio.uio_iov = &iov;
		uio.uio_iovcnt = 1;
		uio.uio_offset = (off_t)(uintptr_t)addr;
		uio.uio_resid = sizeof(int);
		uio.uio_segflg = UIO_SYSSPACE;
		uio.uio_rw = write ? UIO_WRITE : UIO_READ;
		uio.uio_td = curthread;
		error = procfs_domem(curp, lp, NULL, &uio);
		if (uio.uio_resid != 0) {
			/*
			 * XXX procfs_domem() doesn't currently return ENOSPC,
			 * so I think write() can bogusly return 0.
			 * XXX what happens for short writes?  We don't want
			 * to write partial data.
			 * XXX procfs_domem() returns EPERM for other invalid
			 * addresses.  Convert this to EINVAL.  Does this
			 * clobber returns of EPERM for other reasons?
			 */
			if (error == 0 || error == ENOSPC || error == EPERM)
				error = EINVAL;	/* EOF */
		}
		if (!write)
			*res = tmp;
		lwkt_reltoken(&p->p_token);
		PRELE(p);
		return (error);

	case PT_IO:
		/*
		 * NOTE! uio_offset represents the offset in the target
		 * process.  The iov is in the current process (the guy
		 * making the ptrace call) so uio_td must be the current
		 * process.
		 */
		piod = addr;
		iov.iov_base = piod->piod_addr;
		iov.iov_len = piod->piod_len;
		uio.uio_iov = &iov;
		uio.uio_iovcnt = 1;
		uio.uio_offset = (off_t)(uintptr_t)piod->piod_offs;
		uio.uio_resid = piod->piod_len;
		uio.uio_segflg = UIO_USERSPACE;
		uio.uio_td = curthread;
		switch (piod->piod_op) {
		case PIOD_READ_D:
		case PIOD_READ_I:
			uio.uio_rw = UIO_READ;
			break;
		case PIOD_WRITE_D:
		case PIOD_WRITE_I:
			uio.uio_rw = UIO_WRITE;
			break;
		default:
			lwkt_reltoken(&p->p_token);
			PRELE(p);
			return (EINVAL);
		}
		error = procfs_domem(curp, lp, NULL, &uio);
		piod->piod_len -= uio.uio_resid;
		lwkt_reltoken(&p->p_token);
		PRELE(p);
		return (error);

	case PT_KILL:
		data = SIGKILL;
		goto sendsig;	/* in PT_CONTINUE above */

#ifdef PT_SETREGS
	case PT_SETREGS:
		write = 1;
		/* fallthrough */
#endif /* PT_SETREGS */
#ifdef PT_GETREGS
	case PT_GETREGS:
		/* write = 0 above */
#endif /* PT_SETREGS */
#if defined(PT_SETREGS) || defined(PT_GETREGS)
		if (!procfs_validregs(lp)) {
			lwkt_reltoken(&p->p_token);
			PRELE(p);
			return EINVAL;
		} else {
			iov.iov_base = addr;
			iov.iov_len = sizeof(struct reg);
			uio.uio_iov = &iov;
			uio.uio_iovcnt = 1;
			uio.uio_offset = 0;
			uio.uio_resid = sizeof(struct reg);
			uio.uio_segflg = UIO_SYSSPACE;
			uio.uio_rw = write ? UIO_WRITE : UIO_READ;
			uio.uio_td = curthread;
			t = procfs_doregs(curp, lp, NULL, &uio);
			lwkt_reltoken(&p->p_token);
			PRELE(p);
			return t;
		}
#endif /* defined(PT_SETREGS) || defined(PT_GETREGS) */

#ifdef PT_SETFPREGS
	case PT_SETFPREGS:
		write = 1;
		/* fallthrough */
#endif /* PT_SETFPREGS */
#ifdef PT_GETFPREGS
	case PT_GETFPREGS:
		/* write = 0 above */
#endif /* PT_SETFPREGS */
#if defined(PT_SETFPREGS) || defined(PT_GETFPREGS)
		if (!procfs_validfpregs(lp)) {
			lwkt_reltoken(&p->p_token);
			PRELE(p);
			return EINVAL;
		} else {
			iov.iov_base = addr;
			iov.iov_len = sizeof(struct fpreg);
			uio.uio_iov = &iov;
			uio.uio_iovcnt = 1;
			uio.uio_offset = 0;
			uio.uio_resid = sizeof(struct fpreg);
			uio.uio_segflg = UIO_SYSSPACE;
			uio.uio_rw = write ? UIO_WRITE : UIO_READ;
			uio.uio_td = curthread;
			t = procfs_dofpregs(curp, lp, NULL, &uio);
			lwkt_reltoken(&p->p_token);
			PRELE(p);
			return t;
		}
#endif /* defined(PT_SETFPREGS) || defined(PT_GETFPREGS) */

#ifdef PT_SETDBREGS
	case PT_SETDBREGS:
		write = 1;
		/* fallthrough */
#endif /* PT_SETDBREGS */
#ifdef PT_GETDBREGS
	case PT_GETDBREGS:
		/* write = 0 above */
#endif /* PT_SETDBREGS */
#if defined(PT_SETDBREGS) || defined(PT_GETDBREGS)
		if (!procfs_validdbregs(lp)) {
			lwkt_reltoken(&p->p_token);
			PRELE(p);
			return EINVAL;
		} else {
			iov.iov_base = addr;
			iov.iov_len = sizeof(struct dbreg);
			uio.uio_iov = &iov;
			uio.uio_iovcnt = 1;
			uio.uio_offset = 0;
			uio.uio_resid = sizeof(struct dbreg);
			uio.uio_segflg = UIO_SYSSPACE;
			uio.uio_rw = write ? UIO_WRITE : UIO_READ;
			uio.uio_td = curthread;
			t = procfs_dodbregs(curp, lp, NULL, &uio);
			lwkt_reltoken(&p->p_token);
			PRELE(p);
			return t;
		}
#endif /* defined(PT_SETDBREGS) || defined(PT_GETDBREGS) */

	default:
		break;
	}

	lwkt_reltoken(&p->p_token);
	PRELE(p);

	return 0;
}

int
trace_req(struct proc *p)
{
	return 1;
}

/*
 * stopevent()
 *
 * Stop a process because of a procfs event.  Stay stopped until p->p_step
 * is cleared (cleared by PIOCCONT in procfs).
 *
 * MPSAFE
 */
void
stopevent(struct proc *p, unsigned int event, unsigned int val) 
{
	/*
	 * Set event info.  Recheck p_stops in case we are
	 * racing a close() on procfs.
	 */
	spin_lock(&p->p_spin);
	if ((p->p_stops & event) == 0) {
		spin_unlock(&p->p_spin);
		return;
	}
	p->p_xstat = val;
	p->p_stype = event;
	p->p_step = 1;
	tsleep_interlock(&p->p_step, 0);
	spin_unlock(&p->p_spin);

	/*
	 * Wakeup any PIOCWAITing procs and wait for p_step to
	 * be cleared.
	 */
	for (;;) {
		wakeup(&p->p_stype);
		tsleep(&p->p_step, PINTERLOCKED, "stopevent", 0);
		spin_lock(&p->p_spin);
		if (p->p_step == 0) {
			spin_unlock(&p->p_spin);
			break;
		}
		tsleep_interlock(&p->p_step, 0);
		spin_unlock(&p->p_spin);
	}
}