Home | History | Annotate | Line # | Download | only in kern
      1  1.12  riastrad /*	$NetBSD: sys_ptrace.c,v 1.12 2022/07/10 14:07:55 riastradh 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 #include <sys/cdefs.h>
     72  1.12  riastrad __KERNEL_RCSID(0, "$NetBSD: sys_ptrace.c,v 1.12 2022/07/10 14:07:55 riastradh Exp $");
     73   1.1  pgoyette 
     74   1.1  pgoyette #ifdef _KERNEL_OPT
     75   1.1  pgoyette #include "opt_ptrace.h"
     76   1.1  pgoyette #endif
     77   1.1  pgoyette 
     78   1.1  pgoyette #include <sys/param.h>
     79   1.1  pgoyette #include <sys/systm.h>
     80   1.1  pgoyette #include <sys/proc.h>
     81   1.1  pgoyette #include <sys/errno.h>
     82   1.1  pgoyette #include <sys/exec.h>
     83   1.1  pgoyette #include <sys/pax.h>
     84   1.1  pgoyette #include <sys/ptrace.h>
     85   1.1  pgoyette #include <sys/uio.h>
     86   1.1  pgoyette #include <sys/ras.h>
     87   1.1  pgoyette #include <sys/kmem.h>
     88   1.1  pgoyette #include <sys/kauth.h>
     89   1.1  pgoyette #include <sys/mount.h>
     90   1.1  pgoyette #include <sys/syscallargs.h>
     91   1.1  pgoyette #include <sys/syscallvar.h>
     92   1.1  pgoyette #include <sys/syscall.h>
     93   1.8     kamil #include <sys/module.h>
     94   1.1  pgoyette 
     95   1.1  pgoyette #include <uvm/uvm_extern.h>
     96   1.1  pgoyette 
     97   1.1  pgoyette #include <machine/reg.h>
     98   1.1  pgoyette 
     99   1.1  pgoyette /*
    100   1.1  pgoyette  * PTRACE methods
    101   1.1  pgoyette  */
    102   1.1  pgoyette 
    103   1.5  christos static int
    104   1.5  christos ptrace_copyin_piod(struct ptrace_io_desc *piod, const void *addr, size_t len)
    105   1.5  christos {
    106   1.5  christos 	if (len != 0 && sizeof(*piod) != len)
    107   1.5  christos 		return EINVAL;
    108   1.5  christos 
    109   1.5  christos 	return copyin(addr, piod, sizeof(*piod));
    110   1.5  christos }
    111   1.5  christos 
    112   1.5  christos static int
    113   1.5  christos ptrace_copyout_piod(const struct ptrace_io_desc *piod, void *addr, size_t len)
    114   1.5  christos {
    115   1.5  christos 	if (len != 0 && sizeof(*piod) != len)
    116   1.5  christos 		return EINVAL;
    117   1.5  christos 
    118   1.5  christos 	return copyout(piod, addr, sizeof(*piod));
    119   1.5  christos }
    120   1.1  pgoyette 
    121   1.1  pgoyette static int
    122   1.5  christos ptrace_copyin_siginfo(struct ptrace_siginfo *psi, const void *addr, size_t len)
    123   1.1  pgoyette {
    124   1.5  christos 	if (sizeof(*psi) != len)
    125   1.5  christos 		return EINVAL;
    126   1.5  christos 
    127   1.5  christos 	return copyin(addr, psi, sizeof(*psi));
    128   1.1  pgoyette }
    129   1.1  pgoyette 
    130   1.5  christos static int
    131   1.5  christos ptrace_copyout_siginfo(const struct ptrace_siginfo *psi, void *addr, size_t len)
    132   1.1  pgoyette {
    133   1.5  christos 	if (sizeof(*psi) != len)
    134   1.5  christos 		return EINVAL;
    135   1.5  christos 
    136   1.5  christos 	return copyout(psi, addr, sizeof(*psi));
    137   1.1  pgoyette }
    138   1.1  pgoyette 
    139   1.6     kamil static int
    140   1.6     kamil ptrace_copyout_lwpstatus(const struct ptrace_lwpstatus *pls, void *addr,
    141   1.6     kamil     size_t len)
    142   1.6     kamil {
    143   1.6     kamil 
    144   1.6     kamil 	return copyout(pls, addr, len);
    145   1.6     kamil }
    146   1.6     kamil 
    147   1.1  pgoyette static struct ptrace_methods native_ptm = {
    148   1.5  christos 	.ptm_copyin_piod = ptrace_copyin_piod,
    149   1.5  christos 	.ptm_copyout_piod = ptrace_copyout_piod,
    150   1.5  christos 	.ptm_copyin_siginfo = ptrace_copyin_siginfo,
    151   1.5  christos 	.ptm_copyout_siginfo = ptrace_copyout_siginfo,
    152   1.6     kamil 	.ptm_copyout_lwpstatus = ptrace_copyout_lwpstatus,
    153   1.2  pgoyette 	.ptm_doregs = process_doregs,
    154   1.2  pgoyette 	.ptm_dofpregs = process_dofpregs,
    155   1.4     kamil 	.ptm_dodbregs = process_dodbregs,
    156   1.1  pgoyette };
    157   1.1  pgoyette 
    158   1.1  pgoyette static const struct syscall_package ptrace_syscalls[] = {
    159   1.1  pgoyette 	{ SYS_ptrace, 0, (sy_call_t *)sys_ptrace },
    160   1.1  pgoyette 	{ 0, 0, NULL },
    161  1.12  riastrad };
    162   1.1  pgoyette 
    163  1.12  riastrad /*
    164   1.1  pgoyette  * Process debugging system call.
    165  1.12  riastrad  */
    166  1.12  riastrad int
    167   1.1  pgoyette sys_ptrace(struct lwp *l, const struct sys_ptrace_args *uap, register_t *retval)
    168   1.1  pgoyette {
    169  1.12  riastrad 	/* {
    170   1.1  pgoyette 		syscallarg(int) req;
    171   1.1  pgoyette 		syscallarg(pid_t) pid;
    172   1.1  pgoyette 		syscallarg(void *) addr;
    173   1.1  pgoyette 		syscallarg(int) data;
    174   1.1  pgoyette 	} */
    175  1.12  riastrad 
    176   1.1  pgoyette         return do_ptrace(&native_ptm, l, SCARG(uap, req), SCARG(uap, pid),
    177   1.1  pgoyette             SCARG(uap, addr), SCARG(uap, data), retval);
    178   1.1  pgoyette }
    179   1.8     kamil 
    180  1.12  riastrad #define	DEPS	"ptrace_common"
    181   1.8     kamil 
    182   1.8     kamil MODULE(MODULE_CLASS_EXEC, ptrace, DEPS);
    183  1.10  pgoyette 
    184  1.10  pgoyette static int
    185  1.10  pgoyette ptrace_init(void)
    186  1.12  riastrad {
    187  1.10  pgoyette 	int error;
    188  1.10  pgoyette 
    189  1.10  pgoyette 	error = syscall_establish(&emul_netbsd, ptrace_syscalls);
    190  1.10  pgoyette 	return error;
    191  1.12  riastrad }
    192  1.12  riastrad 
    193  1.12  riastrad static int
    194  1.10  pgoyette ptrace_fini(void)
    195  1.10  pgoyette {
    196  1.10  pgoyette 	int error;
    197  1.10  pgoyette 
    198  1.10  pgoyette 	error = syscall_disestablish(&emul_netbsd, ptrace_syscalls);
    199  1.10  pgoyette 	return error;
    200  1.10  pgoyette }
    201  1.10  pgoyette 
    202  1.12  riastrad 
    203   1.8     kamil static int
    204   1.8     kamil ptrace_modcmd(modcmd_t cmd, void *arg)
    205   1.8     kamil {
    206   1.8     kamil 	int error;
    207  1.12  riastrad 
    208   1.8     kamil 	switch (cmd) {
    209  1.12  riastrad 	case MODULE_CMD_INIT:
    210  1.11  pgoyette 		error = ptrace_init();
    211   1.8     kamil 		break;
    212   1.8     kamil 	case MODULE_CMD_FINI:
    213  1.10  pgoyette 		error = ptrace_fini();
    214   1.8     kamil 		break;
    215   1.8     kamil 	default:
    216   1.8     kamil 		error = ENOTTY;
    217   1.8     kamil 		break;
    218   1.8     kamil 	}
    219   1.8     kamil 	return error;
    220   1.8     kamil }
    221