procfs_mem.c revision 1.24 1 /* $NetBSD: procfs_mem.c,v 1.24 2000/06/26 14:21:15 mrg 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/param.h>
49 #include <sys/systm.h>
50 #include <sys/time.h>
51 #include <sys/kernel.h>
52 #include <sys/proc.h>
53 #include <sys/vnode.h>
54
55 #include <miscfs/procfs/procfs.h>
56
57 #include <vm/vm.h>
58
59 #include <uvm/uvm_extern.h>
60
61 #define ISSET(t, f) ((t) & (f))
62
63 /*
64 * Copy data in and out of the target process.
65 * We do this by mapping the process's page into
66 * the kernel and then doing a uiomove direct
67 * from the kernel address space.
68 */
69 int
70 procfs_domem(curp, p, pfs, uio)
71 struct proc *curp; /* tracer */
72 struct proc *p; /* traced */
73 struct pfsnode *pfs;
74 struct uio *uio;
75 {
76 int error;
77
78 size_t len;
79 vaddr_t addr;
80
81 len = uio->uio_resid;
82
83 if (len == 0)
84 return (0);
85
86 addr = uio->uio_offset;
87
88 if ((error = procfs_checkioperm(curp, p)) != 0)
89 return (error);
90
91 /* XXXCDC: how should locking work here? */
92 if ((p->p_flag & P_WEXIT) || (p->p_vmspace->vm_refcnt < 1))
93 return(EFAULT);
94 PHOLD(p);
95 p->p_vmspace->vm_refcnt++; /* XXX */
96 error = uvm_io(&p->p_vmspace->vm_map, uio);
97 PRELE(p);
98 uvmspace_free(p->p_vmspace);
99
100 #ifdef PMAP_NEED_PROCWR
101 if (uio->uio_rw == UIO_WRITE)
102 pmap_procwr(p, addr, len);
103 #endif
104 return (error);
105 }
106
107 /*
108 * Given process (p), find the vnode from which
109 * it's text segment is being executed.
110 *
111 * It would be nice to grab this information from
112 * the VM system, however, there is no sure-fire
113 * way of doing that. Instead, fork(), exec() and
114 * wait() all maintain the p_textvp field in the
115 * process proc structure which contains a held
116 * reference to the exec'ed vnode.
117 */
118 struct vnode *
119 procfs_findtextvp(p)
120 struct proc *p;
121 {
122
123 return (p->p_textvp);
124 }
125
126 /*
127 * Ensure that a process has permission to perform I/O on another.
128 * Arguments:
129 * p The process wishing to do the I/O (the tracer).
130 * t The process who's memory/registers will be read/written.
131 */
132 int
133 procfs_checkioperm(p, t)
134 struct proc *p, *t;
135 {
136 int error;
137
138 /*
139 * You cannot attach to a processes mem/regs if:
140 *
141 * (1) it's not owned by you, or is set-id on exec
142 * (unless you're root), or...
143 */
144 if ((t->p_cred->p_ruid != p->p_cred->p_ruid ||
145 ISSET(t->p_flag, P_SUGID)) &&
146 (error = suser(p->p_ucred, &p->p_acflag)) != 0)
147 return (error);
148
149 /*
150 * (2) ...it's init, which controls the security level
151 * of the entire system, and the system was not
152 * compiled with permanetly insecure mode turned on.
153 */
154 if (t == initproc && securelevel > -1)
155 return (EPERM);
156
157 /*
158 * (3) the tracer is chrooted, and its root directory is
159 * not at or above the root directory of the tracee
160 */
161
162 if (!proc_isunder(t, p))
163 return EPERM;
164
165 return (0);
166 }
167
168 #ifdef probably_never
169 /*
170 * Given process (p), find the vnode from which
171 * it's text segment is being mapped.
172 *
173 * (This is here, rather than in procfs_subr in order
174 * to keep all the VM related code in one place.)
175 */
176 struct vnode *
177 procfs_findtextvp(p)
178 struct proc *p;
179 {
180 int error;
181 vm_object_t object;
182 vaddr_t pageno; /* page number */
183
184 /* find a vnode pager for the user address space */
185
186 for (pageno = VM_MIN_ADDRESS;
187 pageno < VM_MAXUSER_ADDRESS;
188 pageno += PAGE_SIZE) {
189 vm_map_t map;
190 vm_map_entry_t out_entry;
191 vm_prot_t out_prot;
192 boolean_t wired, single_use;
193 vaddr_t off;
194
195 map = &p->p_vmspace->vm_map;
196 error = vm_map_lookup(&map, pageno,
197 VM_PROT_READ,
198 &out_entry, &object, &off, &out_prot,
199 &wired, &single_use);
200
201 if (!error) {
202 vm_pager_t pager;
203
204 printf("procfs: found vm object\n");
205 vm_map_lookup_done(map, out_entry);
206 printf("procfs: vm object = %p\n", object);
207
208 /*
209 * At this point, assuming no errors, object
210 * is the VM object mapping UVA (pageno).
211 * Ensure it has a vnode pager, then grab
212 * the vnode from that pager's handle.
213 */
214
215 pager = object->pager;
216 printf("procfs: pager = %p\n", pager);
217 if (pager)
218 printf("procfs: found pager, type = %d\n",
219 pager->pg_type);
220 if (pager && pager->pg_type == PG_VNODE) {
221 struct vnode *vp;
222
223 vp = (struct vnode *) pager->pg_handle;
224 printf("procfs: vp = %p\n", vp);
225 return (vp);
226 }
227 }
228 }
229
230 printf("procfs: text object not found\n");
231 return (0);
232 }
233 #endif /* probably_never */
234