procfs_mem.c revision 1.27.2.5 1 /* $NetBSD: procfs_mem.c,v 1.27.2.5 2002/04/01 21:38:17 nathanw Exp $ */
2
3 /*
4 * Copyright (c) 1993 Jan-Simon Pendry
5 * Copyright (c) 1993 Sean Eric Fagan
6 * Copyright (c) 1993
7 * The Regents of the University of California. All rights reserved.
8 *
9 * This code is derived from software contributed to Berkeley by
10 * Jan-Simon Pendry and Sean Eric Fagan.
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. All advertising materials mentioning features or use of this software
21 * must display the following acknowledgement:
22 * This product includes software developed by the University of
23 * California, Berkeley and its contributors.
24 * 4. Neither the name of the University nor the names of its contributors
25 * may be used to endorse or promote products derived from this software
26 * without specific prior written permission.
27 *
28 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
29 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
32 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
37 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 * SUCH DAMAGE.
39 *
40 * @(#)procfs_mem.c 8.5 (Berkeley) 6/15/94
41 */
42
43 /*
44 * This is a lightly hacked and merged version
45 * of sef's pread/pwrite functions
46 */
47
48 #include <sys/cdefs.h>
49 __KERNEL_RCSID(0, "$NetBSD: procfs_mem.c,v 1.27.2.5 2002/04/01 21:38:17 nathanw Exp $");
50
51 #include <sys/param.h>
52 #include <sys/systm.h>
53 #include <sys/time.h>
54 #include <sys/kernel.h>
55 #include <sys/lwp.h>
56 #include <sys/proc.h>
57 #include <sys/vnode.h>
58
59 #include <miscfs/procfs/procfs.h>
60
61 #include <uvm/uvm_extern.h>
62
63 #define ISSET(t, f) ((t) & (f))
64
65 /*
66 * Copy data in and out of the target process.
67 * We do this by mapping the process's page into
68 * the kernel and then doing a uiomove direct
69 * from the kernel address space.
70 */
71 int
72 procfs_domem(curp, p, pfs, uio)
73 struct proc *curp; /* tracer */
74 struct proc *p; /* traced */
75 struct pfsnode *pfs;
76 struct uio *uio;
77 {
78 int error;
79
80 size_t len;
81 #ifdef PMAP_NEED_PROCWR
82 vaddr_t addr;
83 #endif
84
85 len = uio->uio_resid;
86
87 if (len == 0)
88 return (0);
89
90 #ifdef PMAP_NEED_PROCWR
91 addr = uio->uio_offset;
92 #endif
93
94 if ((error = procfs_checkioperm(curp, p)) != 0)
95 return (error);
96
97 /* XXXCDC: how should locking work here? */
98 if ((p->p_flag & P_WEXIT) || (p->p_vmspace->vm_refcnt < 1))
99 return(EFAULT);
100 p->p_vmspace->vm_refcnt++; /* XXX */
101 error = uvm_io(&p->p_vmspace->vm_map, uio);
102 uvmspace_free(p->p_vmspace);
103
104 #ifdef PMAP_NEED_PROCWR
105 if (uio->uio_rw == UIO_WRITE)
106 pmap_procwr(p, addr, len);
107 #endif
108 return (error);
109 }
110
111 /*
112 * Ensure that a process has permission to perform I/O on another.
113 * Arguments:
114 * p The process wishing to do the I/O (the tracer).
115 * t The process who's memory/registers will be read/written.
116 */
117 int
118 procfs_checkioperm(p, t)
119 struct proc *p, *t;
120 {
121 int error;
122
123 /*
124 * You cannot attach to a processes mem/regs if:
125 *
126 * (1) It is currently exec'ing
127 */
128 if (ISSET(t->p_flag, P_INEXEC))
129 return (EAGAIN);
130
131 /*
132 * (2) it's not owned by you, or is set-id on exec
133 * (unless you're root), or...
134 */
135 if ((t->p_cred->p_ruid != p->p_cred->p_ruid ||
136 ISSET(t->p_flag, P_SUGID)) &&
137 (error = suser(p->p_ucred, &p->p_acflag)) != 0)
138 return (error);
139
140 /*
141 * (3) ...it's init, which controls the security level
142 * of the entire system, and the system was not
143 * compiled with permanetly insecure mode turned on.
144 */
145 if (t == initproc && securelevel > -1)
146 return (EPERM);
147
148 /*
149 * (4) the tracer is chrooted, and its root directory is
150 * not at or above the root directory of the tracee
151 */
152 if (!proc_isunder(t, p))
153 return (EPERM);
154
155 return (0);
156 }
157