Home | History | Annotate | Line # | Download | only in kern
sys_ptrace.c revision 1.1
      1  1.1  pgoyette /*	$NetBSD: sys_ptrace.c,v 1.1 2016/11/02 00:11:59 pgoyette Exp $	*/
      2  1.1  pgoyette 
      3  1.1  pgoyette /*-
      4  1.1  pgoyette  * Copyright (c) 2008, 2009 The NetBSD Foundation, Inc.
      5  1.1  pgoyette  * All rights reserved.
      6  1.1  pgoyette  *
      7  1.1  pgoyette  * This code is derived from software contributed to The NetBSD Foundation
      8  1.1  pgoyette  * by Andrew Doran.
      9  1.1  pgoyette  *
     10  1.1  pgoyette  * Redistribution and use in source and binary forms, with or without
     11  1.1  pgoyette  * modification, are permitted provided that the following conditions
     12  1.1  pgoyette  * are met:
     13  1.1  pgoyette  * 1. Redistributions of source code must retain the above copyright
     14  1.1  pgoyette  *    notice, this list of conditions and the following disclaimer.
     15  1.1  pgoyette  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.1  pgoyette  *    notice, this list of conditions and the following disclaimer in the
     17  1.1  pgoyette  *    documentation and/or other materials provided with the distribution.
     18  1.1  pgoyette  *
     19  1.1  pgoyette  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  1.1  pgoyette  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  1.1  pgoyette  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  1.1  pgoyette  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  1.1  pgoyette  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  1.1  pgoyette  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  1.1  pgoyette  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  1.1  pgoyette  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  1.1  pgoyette  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  1.1  pgoyette  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  1.1  pgoyette  * POSSIBILITY OF SUCH DAMAGE.
     30  1.1  pgoyette  */
     31  1.1  pgoyette 
     32  1.1  pgoyette /*-
     33  1.1  pgoyette  * Copyright (c) 1982, 1986, 1989, 1993
     34  1.1  pgoyette  *	The Regents of the University of California.  All rights reserved.
     35  1.1  pgoyette  * (c) UNIX System Laboratories, Inc.
     36  1.1  pgoyette  * All or some portions of this file are derived from material licensed
     37  1.1  pgoyette  * to the University of California by American Telephone and Telegraph
     38  1.1  pgoyette  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
     39  1.1  pgoyette  * the permission of UNIX System Laboratories, Inc.
     40  1.1  pgoyette  *
     41  1.1  pgoyette  * This code is derived from software contributed to Berkeley by
     42  1.1  pgoyette  * Jan-Simon Pendry.
     43  1.1  pgoyette  *
     44  1.1  pgoyette  * Redistribution and use in source and binary forms, with or without
     45  1.1  pgoyette  * modification, are permitted provided that the following conditions
     46  1.1  pgoyette  * are met:
     47  1.1  pgoyette  * 1. Redistributions of source code must retain the above copyright
     48  1.1  pgoyette  *    notice, this list of conditions and the following disclaimer.
     49  1.1  pgoyette  * 2. Redistributions in binary form must reproduce the above copyright
     50  1.1  pgoyette  *    notice, this list of conditions and the following disclaimer in the
     51  1.1  pgoyette  *    documentation and/or other materials provided with the distribution.
     52  1.1  pgoyette  * 3. Neither the name of the University nor the names of its contributors
     53  1.1  pgoyette  *    may be used to endorse or promote products derived from this software
     54  1.1  pgoyette  *    without specific prior written permission.
     55  1.1  pgoyette  *
     56  1.1  pgoyette  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     57  1.1  pgoyette  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     58  1.1  pgoyette  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     59  1.1  pgoyette  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     60  1.1  pgoyette  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     61  1.1  pgoyette  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     62  1.1  pgoyette  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     63  1.1  pgoyette  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     64  1.1  pgoyette  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     65  1.1  pgoyette  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     66  1.1  pgoyette  * SUCH DAMAGE.
     67  1.1  pgoyette  *
     68  1.1  pgoyette  *	from: @(#)sys_process.c	8.1 (Berkeley) 6/10/93
     69  1.1  pgoyette  */
     70  1.1  pgoyette 
     71  1.1  pgoyette /*-
     72  1.1  pgoyette  * Copyright (c) 1993 Jan-Simon Pendry.
     73  1.1  pgoyette  * Copyright (c) 1994 Christopher G. Demetriou.  All rights reserved.
     74  1.1  pgoyette  *
     75  1.1  pgoyette  * This code is derived from software contributed to Berkeley by
     76  1.1  pgoyette  * Jan-Simon Pendry.
     77  1.1  pgoyette  *
     78  1.1  pgoyette  * Redistribution and use in source and binary forms, with or without
     79  1.1  pgoyette  * modification, are permitted provided that the following conditions
     80  1.1  pgoyette  * are met:
     81  1.1  pgoyette  * 1. Redistributions of source code must retain the above copyright
     82  1.1  pgoyette  *    notice, this list of conditions and the following disclaimer.
     83  1.1  pgoyette  * 2. Redistributions in binary form must reproduce the above copyright
     84  1.1  pgoyette  *    notice, this list of conditions and the following disclaimer in the
     85  1.1  pgoyette  *    documentation and/or other materials provided with the distribution.
     86  1.1  pgoyette  * 3. All advertising materials mentioning features or use of this software
     87  1.1  pgoyette  *    must display the following acknowledgement:
     88  1.1  pgoyette  *	This product includes software developed by the University of
     89  1.1  pgoyette  *	California, Berkeley and its contributors.
     90  1.1  pgoyette  * 4. Neither the name of the University nor the names of its contributors
     91  1.1  pgoyette  *    may be used to endorse or promote products derived from this software
     92  1.1  pgoyette  *    without specific prior written permission.
     93  1.1  pgoyette  *
     94  1.1  pgoyette  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     95  1.1  pgoyette  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     96  1.1  pgoyette  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     97  1.1  pgoyette  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     98  1.1  pgoyette  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     99  1.1  pgoyette  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
    100  1.1  pgoyette  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
    101  1.1  pgoyette  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
    102  1.1  pgoyette  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
    103  1.1  pgoyette  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
    104  1.1  pgoyette  * SUCH DAMAGE.
    105  1.1  pgoyette  *
    106  1.1  pgoyette  *	from: @(#)sys_process.c	8.1 (Berkeley) 6/10/93
    107  1.1  pgoyette  */
    108  1.1  pgoyette 
    109  1.1  pgoyette /*
    110  1.1  pgoyette  * References:
    111  1.1  pgoyette  *	(1) Bach's "The Design of the UNIX Operating System",
    112  1.1  pgoyette  *	(2) sys/miscfs/procfs from UCB's 4.4BSD-Lite distribution,
    113  1.1  pgoyette  *	(3) the "4.4BSD Programmer's Reference Manual" published
    114  1.1  pgoyette  *		by USENIX and O'Reilly & Associates.
    115  1.1  pgoyette  * The 4.4BSD PRM does a reasonably good job of documenting what the various
    116  1.1  pgoyette  * ptrace() requests should actually do, and its text is quoted several times
    117  1.1  pgoyette  * in this file.
    118  1.1  pgoyette  */
    119  1.1  pgoyette 
    120  1.1  pgoyette #include <sys/cdefs.h>
    121  1.1  pgoyette __KERNEL_RCSID(0, "$NetBSD: sys_ptrace.c,v 1.1 2016/11/02 00:11:59 pgoyette Exp $");
    122  1.1  pgoyette 
    123  1.1  pgoyette #ifdef _KERNEL_OPT
    124  1.1  pgoyette #include "opt_ptrace.h"
    125  1.1  pgoyette #endif
    126  1.1  pgoyette 
    127  1.1  pgoyette #include <sys/param.h>
    128  1.1  pgoyette #include <sys/systm.h>
    129  1.1  pgoyette #include <sys/proc.h>
    130  1.1  pgoyette #include <sys/errno.h>
    131  1.1  pgoyette #include <sys/exec.h>
    132  1.1  pgoyette #include <sys/pax.h>
    133  1.1  pgoyette #include <sys/ptrace.h>
    134  1.1  pgoyette #include <sys/uio.h>
    135  1.1  pgoyette #include <sys/ras.h>
    136  1.1  pgoyette #include <sys/kmem.h>
    137  1.1  pgoyette #include <sys/kauth.h>
    138  1.1  pgoyette #include <sys/mount.h>
    139  1.1  pgoyette #include <sys/syscallargs.h>
    140  1.1  pgoyette #include <sys/syscallvar.h>
    141  1.1  pgoyette #include <sys/syscall.h>
    142  1.1  pgoyette #include <sys/module.h>
    143  1.1  pgoyette 
    144  1.1  pgoyette #include <uvm/uvm_extern.h>
    145  1.1  pgoyette 
    146  1.1  pgoyette #include <machine/reg.h>
    147  1.1  pgoyette 
    148  1.1  pgoyette /*
    149  1.1  pgoyette  * PTRACE methods
    150  1.1  pgoyette  */
    151  1.1  pgoyette 
    152  1.1  pgoyette static int ptrace_copyinpiod(struct ptrace_io_desc *, const void *);
    153  1.1  pgoyette static void ptrace_copyoutpiod(const struct ptrace_io_desc *, void *);
    154  1.1  pgoyette static int ptrace_doregs(struct lwp *, struct lwp *, struct uio *);
    155  1.1  pgoyette static int ptrace_dofpregs(struct lwp *, struct lwp *, struct uio *);
    156  1.1  pgoyette 
    157  1.1  pgoyette static int
    158  1.1  pgoyette ptrace_copyinpiod(struct ptrace_io_desc *piod, const void *addr)
    159  1.1  pgoyette {
    160  1.1  pgoyette 	return copyin(addr, piod, sizeof(*piod));
    161  1.1  pgoyette }
    162  1.1  pgoyette 
    163  1.1  pgoyette static void
    164  1.1  pgoyette ptrace_copyoutpiod(const struct ptrace_io_desc *piod, void *addr)
    165  1.1  pgoyette {
    166  1.1  pgoyette 	(void) copyout(piod, addr, sizeof(*piod));
    167  1.1  pgoyette }
    168  1.1  pgoyette 
    169  1.1  pgoyette int
    170  1.1  pgoyette ptrace_doregs(struct lwp *curl /*tracer*/,
    171  1.1  pgoyette     struct lwp *l /*traced*/,
    172  1.1  pgoyette     struct uio *uio)
    173  1.1  pgoyette {
    174  1.1  pgoyette #if defined(PT_GETREGS) || defined(PT_SETREGS)
    175  1.1  pgoyette 	int error;
    176  1.1  pgoyette 	struct reg r;
    177  1.1  pgoyette 	char *kv;
    178  1.1  pgoyette 	int kl;
    179  1.1  pgoyette 
    180  1.1  pgoyette 	if (uio->uio_offset < 0 || uio->uio_offset > (off_t)sizeof(r))
    181  1.1  pgoyette 		return EINVAL;
    182  1.1  pgoyette 
    183  1.1  pgoyette 	kl = sizeof(r);
    184  1.1  pgoyette 	kv = (char *)&r;
    185  1.1  pgoyette 
    186  1.1  pgoyette 	kv += uio->uio_offset;
    187  1.1  pgoyette 	kl -= uio->uio_offset;
    188  1.1  pgoyette 	if ((size_t)kl > uio->uio_resid)
    189  1.1  pgoyette 		kl = uio->uio_resid;
    190  1.1  pgoyette 
    191  1.1  pgoyette 	error = process_read_regs(l, &r);
    192  1.1  pgoyette 	if (error == 0)
    193  1.1  pgoyette 		error = uiomove(kv, kl, uio);
    194  1.1  pgoyette 	if (error == 0 && uio->uio_rw == UIO_WRITE) {
    195  1.1  pgoyette 		if (l->l_stat != LSSTOP)
    196  1.1  pgoyette 			error = EBUSY;
    197  1.1  pgoyette 		else
    198  1.1  pgoyette 			error = process_write_regs(l, &r);
    199  1.1  pgoyette 	}
    200  1.1  pgoyette 
    201  1.1  pgoyette 	uio->uio_offset = 0;
    202  1.1  pgoyette 	return error;
    203  1.1  pgoyette #else
    204  1.1  pgoyette 	return EINVAL;
    205  1.1  pgoyette #endif
    206  1.1  pgoyette }
    207  1.1  pgoyette 
    208  1.1  pgoyette int
    209  1.1  pgoyette ptrace_dofpregs(struct lwp *curl /*tracer*/,
    210  1.1  pgoyette     struct lwp *l /*traced*/,
    211  1.1  pgoyette     struct uio *uio)
    212  1.1  pgoyette {
    213  1.1  pgoyette #if defined(PT_GETFPREGS) || defined(PT_SETFPREGS)
    214  1.1  pgoyette 	int error;
    215  1.1  pgoyette 	struct fpreg r;
    216  1.1  pgoyette 	char *kv;
    217  1.1  pgoyette 	size_t kl;
    218  1.1  pgoyette 
    219  1.1  pgoyette 	if (uio->uio_offset < 0 || uio->uio_offset > (off_t)sizeof(r))
    220  1.1  pgoyette 		return EINVAL;
    221  1.1  pgoyette 
    222  1.1  pgoyette 	kl = sizeof(r);
    223  1.1  pgoyette 	kv = (char *)&r;
    224  1.1  pgoyette 
    225  1.1  pgoyette 	kv += uio->uio_offset;
    226  1.1  pgoyette 	kl -= uio->uio_offset;
    227  1.1  pgoyette 	if (kl > uio->uio_resid)
    228  1.1  pgoyette 		kl = uio->uio_resid;
    229  1.1  pgoyette 
    230  1.1  pgoyette 	error = process_read_fpregs(l, &r, &kl);
    231  1.1  pgoyette 	if (error == 0)
    232  1.1  pgoyette 		error = uiomove(kv, kl, uio);
    233  1.1  pgoyette 	if (error == 0 && uio->uio_rw == UIO_WRITE) {
    234  1.1  pgoyette 		if (l->l_stat != LSSTOP)
    235  1.1  pgoyette 			error = EBUSY;
    236  1.1  pgoyette 		else
    237  1.1  pgoyette 			error = process_write_fpregs(l, &r, kl);
    238  1.1  pgoyette 	}
    239  1.1  pgoyette 	uio->uio_offset = 0;
    240  1.1  pgoyette 	return error;
    241  1.1  pgoyette #else
    242  1.1  pgoyette 	return EINVAL;
    243  1.1  pgoyette #endif
    244  1.1  pgoyette }
    245  1.1  pgoyette 
    246  1.1  pgoyette static struct ptrace_methods native_ptm = {
    247  1.1  pgoyette 	.ptm_copyinpiod = ptrace_copyinpiod,
    248  1.1  pgoyette 	.ptm_copyoutpiod = ptrace_copyoutpiod,
    249  1.1  pgoyette 	.ptm_doregs = ptrace_doregs,
    250  1.1  pgoyette 	.ptm_dofpregs = ptrace_dofpregs,
    251  1.1  pgoyette };
    252  1.1  pgoyette 
    253  1.1  pgoyette static const struct syscall_package ptrace_syscalls[] = {
    254  1.1  pgoyette 	{ SYS_ptrace, 0, (sy_call_t *)sys_ptrace },
    255  1.1  pgoyette 	{ 0, 0, NULL },
    256  1.1  pgoyette };
    257  1.1  pgoyette 
    258  1.1  pgoyette /*
    259  1.1  pgoyette  * Process debugging system call.
    260  1.1  pgoyette  */
    261  1.1  pgoyette int
    262  1.1  pgoyette sys_ptrace(struct lwp *l, const struct sys_ptrace_args *uap, register_t *retval)
    263  1.1  pgoyette {
    264  1.1  pgoyette 	/* {
    265  1.1  pgoyette 		syscallarg(int) req;
    266  1.1  pgoyette 		syscallarg(pid_t) pid;
    267  1.1  pgoyette 		syscallarg(void *) addr;
    268  1.1  pgoyette 		syscallarg(int) data;
    269  1.1  pgoyette 	} */
    270  1.1  pgoyette 
    271  1.1  pgoyette         return do_ptrace(&native_ptm, l, SCARG(uap, req), SCARG(uap, pid),
    272  1.1  pgoyette             SCARG(uap, addr), SCARG(uap, data), retval);
    273  1.1  pgoyette }
    274  1.1  pgoyette 
    275  1.1  pgoyette #define	DEPS	"ptrace_common"
    276  1.1  pgoyette 
    277  1.1  pgoyette MODULE(MODULE_CLASS_EXEC, ptrace, DEPS);
    278  1.1  pgoyette 
    279  1.1  pgoyette static int
    280  1.1  pgoyette ptrace_modcmd(modcmd_t cmd, void *arg)
    281  1.1  pgoyette {
    282  1.1  pgoyette 	int error;
    283  1.1  pgoyette 
    284  1.1  pgoyette 	switch (cmd) {
    285  1.1  pgoyette 	case MODULE_CMD_INIT:
    286  1.1  pgoyette 		error = syscall_establish(&emul_netbsd, ptrace_syscalls);
    287  1.1  pgoyette 		break;
    288  1.1  pgoyette 	case MODULE_CMD_FINI:
    289  1.1  pgoyette 		error = syscall_disestablish(&emul_netbsd, ptrace_syscalls);
    290  1.1  pgoyette 		break;
    291  1.1  pgoyette 	default:
    292  1.1  pgoyette 		error = ENOTTY;
    293  1.1  pgoyette 		break;
    294  1.1  pgoyette 	}
    295  1.1  pgoyette 	return error;
    296  1.1  pgoyette }
    297