procfs_subr.c revision 1.106.2.1 1 /* $NetBSD: procfs_subr.c,v 1.106.2.1 2017/08/28 17:53:09 skrll Exp $ */
2
3 /*-
4 * Copyright (c) 2006, 2007, 2008 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Andrew Doran.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 /*
33 * Copyright (c) 1993
34 * The Regents of the University of California. All rights reserved.
35 *
36 * This code is derived from software contributed to Berkeley by
37 * Jan-Simon Pendry.
38 *
39 * Redistribution and use in source and binary forms, with or without
40 * modification, are permitted provided that the following conditions
41 * are met:
42 * 1. Redistributions of source code must retain the above copyright
43 * notice, this list of conditions and the following disclaimer.
44 * 2. Redistributions in binary form must reproduce the above copyright
45 * notice, this list of conditions and the following disclaimer in the
46 * documentation and/or other materials provided with the distribution.
47 * 3. Neither the name of the University nor the names of its contributors
48 * may be used to endorse or promote products derived from this software
49 * without specific prior written permission.
50 *
51 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
52 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
53 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
54 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
55 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
56 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
57 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
58 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
59 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
60 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
61 * SUCH DAMAGE.
62 *
63 * @(#)procfs_subr.c 8.6 (Berkeley) 5/14/95
64 */
65
66 /*
67 * Copyright (c) 1994 Christopher G. Demetriou. All rights reserved.
68 * Copyright (c) 1993 Jan-Simon Pendry
69 *
70 * This code is derived from software contributed to Berkeley by
71 * Jan-Simon Pendry.
72 *
73 * Redistribution and use in source and binary forms, with or without
74 * modification, are permitted provided that the following conditions
75 * are met:
76 * 1. Redistributions of source code must retain the above copyright
77 * notice, this list of conditions and the following disclaimer.
78 * 2. Redistributions in binary form must reproduce the above copyright
79 * notice, this list of conditions and the following disclaimer in the
80 * documentation and/or other materials provided with the distribution.
81 * 3. All advertising materials mentioning features or use of this software
82 * must display the following acknowledgement:
83 * This product includes software developed by the University of
84 * California, Berkeley and its contributors.
85 * 4. Neither the name of the University nor the names of its contributors
86 * may be used to endorse or promote products derived from this software
87 * without specific prior written permission.
88 *
89 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
90 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
91 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
92 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
93 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
94 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
95 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
96 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
97 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
98 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
99 * SUCH DAMAGE.
100 *
101 * @(#)procfs_subr.c 8.6 (Berkeley) 5/14/95
102 */
103
104 #include <sys/cdefs.h>
105 __KERNEL_RCSID(0, "$NetBSD: procfs_subr.c,v 1.106.2.1 2017/08/28 17:53:09 skrll Exp $");
106
107 #include <sys/param.h>
108 #include <sys/systm.h>
109 #include <sys/time.h>
110 #include <sys/kernel.h>
111 #include <sys/proc.h>
112 #include <sys/vnode.h>
113 #include <sys/stat.h>
114 #include <sys/file.h>
115 #include <sys/filedesc.h>
116 #include <sys/kauth.h>
117
118 #include <miscfs/procfs/procfs.h>
119
120 /*
121 * Allocate a pfsnode/vnode pair. The vnode is referenced.
122 * The pid, type, and file descriptor uniquely identify a pfsnode.
123 */
124 int
125 procfs_allocvp(struct mount *mp, struct vnode **vpp, pid_t pid,
126 pfstype type, int fd)
127 {
128 struct pfskey key;
129
130 memset(&key, 0, sizeof(key));
131 key.pk_type = type;
132 key.pk_pid = pid;
133 key.pk_fd = fd;
134
135 return vcache_get(mp, &key, sizeof(key), vpp);
136 }
137
138 int
139 procfs_rw(void *v)
140 {
141 struct vop_read_args *ap = v;
142 struct vnode *vp = ap->a_vp;
143 struct uio *uio = ap->a_uio;
144 struct lwp *curl;
145 struct lwp *l;
146 struct pfsnode *pfs = VTOPFS(vp);
147 struct proc *p;
148 int error;
149
150 if (uio->uio_offset < 0)
151 return EINVAL;
152
153 if ((error = procfs_proc_lock(pfs->pfs_pid, &p, ESRCH)) != 0)
154 return error;
155
156 curl = curlwp;
157
158 /*
159 * Do not allow init to be modified while in secure mode; it
160 * could be duped into changing the security level.
161 */
162 #define M2K(m) ((m) == UIO_READ ? KAUTH_REQ_PROCESS_PROCFS_READ : \
163 KAUTH_REQ_PROCESS_PROCFS_WRITE)
164 mutex_enter(p->p_lock);
165 error = kauth_authorize_process(curl->l_cred, KAUTH_PROCESS_PROCFS,
166 p, pfs, KAUTH_ARG(M2K(uio->uio_rw)), NULL);
167 mutex_exit(p->p_lock);
168 if (error) {
169 procfs_proc_unlock(p);
170 return (error);
171 }
172 #undef M2K
173
174 mutex_enter(p->p_lock);
175 LIST_FOREACH(l, &p->p_lwps, l_sibling) {
176 if (l->l_stat != LSZOMB)
177 break;
178 }
179 /* Process is exiting if no-LWPS or all LWPs are LSZOMB */
180 if (l == NULL) {
181 mutex_exit(p->p_lock);
182 procfs_proc_unlock(p);
183 return ESRCH;
184 }
185
186 lwp_addref(l);
187 mutex_exit(p->p_lock);
188
189 switch (pfs->pfs_type) {
190 case PFSnote:
191 case PFSnotepg:
192 error = procfs_donote(curl, p, pfs, uio);
193 break;
194
195 case PFSregs:
196 error = procfs_doregs(curl, l, pfs, uio);
197 break;
198
199 case PFSfpregs:
200 error = procfs_dofpregs(curl, l, pfs, uio);
201 break;
202
203 case PFSctl:
204 error = procfs_doctl(curl, l, pfs, uio);
205 break;
206
207 case PFSstatus:
208 error = procfs_dostatus(curl, l, pfs, uio);
209 break;
210
211 case PFSstat:
212 error = procfs_do_pid_stat(curl, l, pfs, uio);
213 break;
214
215 case PFSmap:
216 error = procfs_domap(curl, p, pfs, uio, 0);
217 break;
218
219 case PFSmaps:
220 error = procfs_domap(curl, p, pfs, uio, 1);
221 break;
222
223 case PFSmem:
224 error = procfs_domem(curl, l, pfs, uio);
225 break;
226
227 case PFScmdline:
228 error = procfs_docmdline(curl, p, pfs, uio);
229 break;
230
231 case PFSmeminfo:
232 error = procfs_domeminfo(curl, p, pfs, uio);
233 break;
234
235 case PFSdevices:
236 error = procfs_dodevices(curl, p, pfs, uio);
237 break;
238
239 case PFScpuinfo:
240 error = procfs_docpuinfo(curl, p, pfs, uio);
241 break;
242
243 case PFScpustat:
244 error = procfs_docpustat(curl, p, pfs, uio);
245 break;
246
247 case PFSloadavg:
248 error = procfs_doloadavg(curl, p, pfs, uio);
249 break;
250
251 case PFSstatm:
252 error = procfs_do_pid_statm(curl, l, pfs, uio);
253 break;
254
255 case PFSfd:
256 error = procfs_dofd(curl, p, pfs, uio);
257 break;
258
259 case PFSuptime:
260 error = procfs_douptime(curl, p, pfs, uio);
261 break;
262
263 case PFSmounts:
264 error = procfs_domounts(curl, p, pfs, uio);
265 break;
266
267 case PFSemul:
268 error = procfs_doemul(curl, p, pfs, uio);
269 break;
270
271 case PFSversion:
272 error = procfs_doversion(curl, p, pfs, uio);
273 break;
274
275 case PFSauxv:
276 error = procfs_doauxv(curl, p, pfs, uio);
277 break;
278
279 #ifdef __HAVE_PROCFS_MACHDEP
280 PROCFS_MACHDEP_NODETYPE_CASES
281 error = procfs_machdep_rw(curl, l, pfs, uio);
282 break;
283 #endif
284
285 default:
286 error = EOPNOTSUPP;
287 break;
288 }
289
290 /*
291 * Release the references that we acquired earlier.
292 */
293 lwp_delref(l);
294 procfs_proc_unlock(p);
295
296 return (error);
297 }
298
299 /*
300 * Get a string from userland into (bf). Strip a trailing
301 * nl character (to allow easy access from the shell).
302 * The buffer should be *buflenp + 1 chars long. vfs_getuserstr
303 * will automatically add a nul char at the end.
304 *
305 * Returns 0 on success or the following errors
306 *
307 * EINVAL: file offset is non-zero.
308 * EMSGSIZE: message is longer than kernel buffer
309 * EFAULT: user i/o buffer is not addressable
310 */
311 int
312 vfs_getuserstr(struct uio *uio, char *bf, int *buflenp)
313 {
314 int xlen;
315 int error;
316
317 if (uio->uio_offset != 0)
318 return (EINVAL);
319
320 xlen = *buflenp;
321
322 /* must be able to read the whole string in one go */
323 if (xlen < uio->uio_resid)
324 return (EMSGSIZE);
325 xlen = uio->uio_resid;
326
327 if ((error = uiomove(bf, xlen, uio)) != 0)
328 return (error);
329
330 /* allow multiple writes without seeks */
331 uio->uio_offset = 0;
332
333 /* cleanup string and remove trailing newline */
334 bf[xlen] = '\0';
335 xlen = strlen(bf);
336 if (xlen > 0 && bf[xlen-1] == '\n')
337 bf[--xlen] = '\0';
338 *buflenp = xlen;
339
340 return (0);
341 }
342
343 const vfs_namemap_t *
344 vfs_findname(const vfs_namemap_t *nm, const char *bf, int buflen)
345 {
346
347 for (; nm->nm_name; nm++)
348 if (memcmp(bf, nm->nm_name, buflen+1) == 0)
349 return (nm);
350
351 return (0);
352 }
353
354 static bool
355 procfs_revoke_selector(void *arg, struct vnode *vp)
356 {
357 struct proc *p = arg;
358 struct pfsnode *pfs;
359
360 KASSERT(mutex_owned(vp->v_interlock));
361
362 pfs = VTOPFS(vp);
363
364 return (pfs != NULL && pfs->pfs_pid == p->p_pid);
365 }
366
367 void
368 procfs_revoke_vnodes(struct proc *p, void *arg)
369 {
370 struct vnode *vp;
371 struct vnode_iterator *marker;
372 struct mount *mp = (struct mount *)arg;
373
374 if (!(p->p_flag & PK_SUGID))
375 return;
376
377 vfs_vnode_iterator_init(mp, &marker);
378
379 while ((vp = vfs_vnode_iterator_next(marker,
380 procfs_revoke_selector, p)) != NULL) {
381 VOP_REVOKE(vp, REVOKEALL);
382 vrele(vp);
383 }
384
385 vfs_vnode_iterator_destroy(marker);
386 }
387
388 int
389 procfs_proc_lock(int pid, struct proc **bunghole, int notfound)
390 {
391 struct proc *tp;
392 int error = 0;
393
394 mutex_enter(proc_lock);
395
396 if (pid == 0)
397 tp = &proc0;
398 else if ((tp = proc_find(pid)) == NULL)
399 error = notfound;
400 if (tp != NULL && !rw_tryenter(&tp->p_reflock, RW_READER))
401 error = EBUSY;
402
403 mutex_exit(proc_lock);
404
405 *bunghole = tp;
406 return error;
407 }
408
409 void
410 procfs_proc_unlock(struct proc *p)
411 {
412
413 rw_exit(&p->p_reflock);
414 }
415
416 int
417 procfs_doemul(struct lwp *curl, struct proc *p,
418 struct pfsnode *pfs, struct uio *uio)
419 {
420 const char *ename = p->p_emul->e_name;
421 return uiomove_frombuf(__UNCONST(ename), strlen(ename), uio);
422 }
423