procfs_vnops.c revision 1.231 1 /* $NetBSD: procfs_vnops.c,v 1.231 2024/05/12 17:22:29 christos Exp $ */
2
3 /*-
4 * Copyright (c) 2006, 2007, 2008, 2020 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, 1995
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_vnops.c 8.18 (Berkeley) 5/21/95
64 */
65
66 /*
67 * Copyright (c) 1993 Jan-Simon Pendry
68 *
69 * This code is derived from software contributed to Berkeley by
70 * Jan-Simon Pendry.
71 *
72 * Redistribution and use in source and binary forms, with or without
73 * modification, are permitted provided that the following conditions
74 * are met:
75 * 1. Redistributions of source code must retain the above copyright
76 * notice, this list of conditions and the following disclaimer.
77 * 2. Redistributions in binary form must reproduce the above copyright
78 * notice, this list of conditions and the following disclaimer in the
79 * documentation and/or other materials provided with the distribution.
80 * 3. All advertising materials mentioning features or use of this software
81 * must display the following acknowledgement:
82 * This product includes software developed by the University of
83 * California, Berkeley and its contributors.
84 * 4. Neither the name of the University nor the names of its contributors
85 * may be used to endorse or promote products derived from this software
86 * without specific prior written permission.
87 *
88 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
89 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
90 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
91 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
92 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
93 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
94 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
95 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
96 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
97 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
98 * SUCH DAMAGE.
99 *
100 * @(#)procfs_vnops.c 8.18 (Berkeley) 5/21/95
101 */
102
103 /*
104 * procfs vnode interface
105 */
106
107 #include <sys/cdefs.h>
108 __KERNEL_RCSID(0, "$NetBSD: procfs_vnops.c,v 1.231 2024/05/12 17:22:29 christos Exp $");
109
110 #include <sys/param.h>
111 #include <sys/atomic.h>
112 #include <sys/systm.h>
113 #include <sys/time.h>
114 #include <sys/kernel.h>
115 #include <sys/file.h>
116 #include <sys/filedesc.h>
117 #include <sys/proc.h>
118 #include <sys/vnode.h>
119 #include <sys/namei.h>
120 #include <sys/malloc.h>
121 #include <sys/mount.h>
122 #include <sys/dirent.h>
123 #include <sys/resourcevar.h>
124 #include <sys/stat.h>
125 #include <sys/ptrace.h>
126 #include <sys/kauth.h>
127 #include <sys/exec.h>
128
129 #include <uvm/uvm_extern.h> /* for PAGE_SIZE */
130
131 #include <machine/reg.h>
132
133 #include <miscfs/genfs/genfs.h>
134 #include <miscfs/procfs/procfs.h>
135
136 /*
137 * Vnode Operations.
138 *
139 */
140
141 static int procfs_validfile_linux(struct lwp *, struct mount *);
142 static int procfs_root_readdir_callback(struct proc *, void *);
143 static void procfs_dir(pfstype, struct lwp *, struct proc *, char **, char *,
144 size_t);
145
146 /*
147 * This is a list of the valid names in the
148 * process-specific sub-directories. It is
149 * used in procfs_lookup and procfs_readdir
150 */
151 static const struct proc_target {
152 u_char pt_type;
153 u_char pt_namlen;
154 const char *pt_name;
155 pfstype pt_pfstype;
156 int (*pt_valid)(struct lwp *, struct mount *);
157 } proc_targets[] = {
158 #define N(s) sizeof(s)-1, s
159 /* name type validp */
160 { DT_DIR, N("."), PFSproc, NULL },
161 { DT_DIR, N(".."), PFSroot, NULL },
162 { DT_DIR, N("fd"), PFSfd, NULL },
163 { DT_DIR, N("task"), PFStask, procfs_validfile_linux },
164 { DT_LNK, N("cwd"), PFScwd, NULL },
165 { DT_REG, N("emul"), PFSemul, NULL },
166 { DT_LNK, N("root"), PFSchroot, NULL },
167 { DT_REG, N("auxv"), PFSauxv, procfs_validauxv },
168 { DT_REG, N("cmdline"), PFScmdline, NULL },
169 { DT_REG, N("environ"), PFSenviron, NULL },
170 { DT_LNK, N("exe"), PFSexe, procfs_validfile },
171 { DT_REG, N("file"), PFSfile, procfs_validfile },
172 { DT_REG, N("fpregs"), PFSfpregs, procfs_validfpregs },
173 { DT_REG, N("limit"), PFSlimit, NULL },
174 { DT_REG, N("limits"), PFSlimits, procfs_validfile_linux },
175 { DT_REG, N("map"), PFSmap, procfs_validmap },
176 { DT_REG, N("maps"), PFSmaps, procfs_validmap },
177 { DT_REG, N("mem"), PFSmem, NULL },
178 { DT_REG, N("note"), PFSnote, NULL },
179 { DT_REG, N("notepg"), PFSnotepg, NULL },
180 { DT_REG, N("regs"), PFSregs, procfs_validregs },
181 { DT_REG, N("stat"), PFSstat, procfs_validfile_linux },
182 { DT_REG, N("statm"), PFSstatm, procfs_validfile_linux },
183 { DT_REG, N("status"), PFSstatus, NULL },
184 #ifdef __HAVE_PROCFS_MACHDEP
185 PROCFS_MACHDEP_NODETYPE_DEFNS
186 #endif
187 #undef N
188 };
189 static const int nproc_targets = sizeof(proc_targets) / sizeof(proc_targets[0]);
190
191 /*
192 * List of files in the root directory. Note: the validate function will
193 * be called with p == NULL for these ones.
194 */
195 static const struct proc_target proc_root_targets[] = {
196 #define N(s) sizeof(s)-1, s
197 /* name type validp */
198 { DT_REG, N("meminfo"), PFSmeminfo, procfs_validfile_linux },
199 { DT_REG, N("cpuinfo"), PFScpuinfo, procfs_validfile_linux },
200 { DT_REG, N("uptime"), PFSuptime, procfs_validfile_linux },
201 { DT_REG, N("mounts"), PFSmounts, procfs_validfile_linux },
202 { DT_REG, N("devices"), PFSdevices, procfs_validfile_linux },
203 { DT_REG, N("stat"), PFScpustat, procfs_validfile_linux },
204 { DT_REG, N("loadavg"), PFSloadavg, procfs_validfile_linux },
205 { DT_REG, N("version"), PFSversion, procfs_validfile_linux },
206 #undef N
207 };
208 static const int nproc_root_targets =
209 sizeof(proc_root_targets) / sizeof(proc_root_targets[0]);
210
211 int procfs_lookup(void *);
212 int procfs_open(void *);
213 int procfs_close(void *);
214 int procfs_access(void *);
215 int procfs_getattr(void *);
216 int procfs_setattr(void *);
217 int procfs_readdir(void *);
218 int procfs_readlink(void *);
219 int procfs_inactive(void *);
220 int procfs_reclaim(void *);
221 int procfs_print(void *);
222 int procfs_pathconf(void *);
223 int procfs_getpages(void *);
224
225 static uint8_t fttodt(file_t *);
226 static int atoi(const char *, size_t);
227
228 /*
229 * procfs vnode operations.
230 */
231 int (**procfs_vnodeop_p)(void *);
232 const struct vnodeopv_entry_desc procfs_vnodeop_entries[] = {
233 { &vop_default_desc, vn_default_error },
234 { &vop_parsepath_desc, genfs_parsepath }, /* parsepath */
235 { &vop_lookup_desc, procfs_lookup }, /* lookup */
236 { &vop_create_desc, genfs_eopnotsupp }, /* create */
237 { &vop_mknod_desc, genfs_eopnotsupp }, /* mknod */
238 { &vop_open_desc, procfs_open }, /* open */
239 { &vop_close_desc, procfs_close }, /* close */
240 { &vop_access_desc, procfs_access }, /* access */
241 { &vop_accessx_desc, genfs_accessx }, /* accessx */
242 { &vop_getattr_desc, procfs_getattr }, /* getattr */
243 { &vop_setattr_desc, procfs_setattr }, /* setattr */
244 { &vop_read_desc, procfs_rw }, /* read */
245 { &vop_write_desc, procfs_rw }, /* write */
246 { &vop_fallocate_desc, genfs_eopnotsupp }, /* fallocate */
247 { &vop_fdiscard_desc, genfs_eopnotsupp }, /* fdiscard */
248 { &vop_fcntl_desc, genfs_fcntl }, /* fcntl */
249 { &vop_ioctl_desc, genfs_enoioctl }, /* ioctl */
250 { &vop_poll_desc, genfs_poll }, /* poll */
251 { &vop_kqfilter_desc, genfs_kqfilter }, /* kqfilter */
252 { &vop_revoke_desc, genfs_revoke }, /* revoke */
253 { &vop_fsync_desc, genfs_nullop }, /* fsync */
254 { &vop_seek_desc, genfs_nullop }, /* seek */
255 { &vop_remove_desc, genfs_eopnotsupp }, /* remove */
256 { &vop_link_desc, genfs_erofs_link }, /* link */
257 { &vop_rename_desc, genfs_eopnotsupp }, /* rename */
258 { &vop_mkdir_desc, genfs_eopnotsupp }, /* mkdir */
259 { &vop_rmdir_desc, genfs_eopnotsupp }, /* rmdir */
260 { &vop_symlink_desc, genfs_erofs_symlink }, /* symlink */
261 { &vop_readdir_desc, procfs_readdir }, /* readdir */
262 { &vop_readlink_desc, procfs_readlink }, /* readlink */
263 { &vop_abortop_desc, genfs_abortop }, /* abortop */
264 { &vop_inactive_desc, procfs_inactive }, /* inactive */
265 { &vop_reclaim_desc, procfs_reclaim }, /* reclaim */
266 { &vop_lock_desc, genfs_lock }, /* lock */
267 { &vop_unlock_desc, genfs_unlock }, /* unlock */
268 { &vop_bmap_desc, genfs_eopnotsupp }, /* bmap */
269 { &vop_strategy_desc, genfs_badop }, /* strategy */
270 { &vop_print_desc, procfs_print }, /* print */
271 { &vop_islocked_desc, genfs_islocked }, /* islocked */
272 { &vop_pathconf_desc, procfs_pathconf }, /* pathconf */
273 { &vop_advlock_desc, genfs_einval }, /* advlock */
274 { &vop_getpages_desc, procfs_getpages }, /* getpages */
275 { &vop_putpages_desc, genfs_null_putpages }, /* putpages */
276 { NULL, NULL }
277 };
278 const struct vnodeopv_desc procfs_vnodeop_opv_desc =
279 { &procfs_vnodeop_p, procfs_vnodeop_entries };
280 /*
281 * set things up for doing i/o on
282 * the pfsnode (vp). (vp) is locked
283 * on entry, and should be left locked
284 * on exit.
285 *
286 * for procfs we don't need to do anything
287 * in particular for i/o. all that is done
288 * is to support exclusive open on process
289 * memory images.
290 */
291 int
292 procfs_open(void *v)
293 {
294 struct vop_open_args /* {
295 struct vnode *a_vp;
296 int a_mode;
297 kauth_cred_t a_cred;
298 } */ *ap = v;
299 struct vnode *vp = ap->a_vp;
300 struct pfsnode *pfs = VTOPFS(vp);
301 struct lwp *l1;
302 struct proc *p2;
303 int error;
304
305 if ((error =
306 procfs_proc_lock(vp->v_mount, pfs->pfs_pid, &p2, ENOENT)) != 0)
307 return error;
308
309 l1 = curlwp; /* tracer */
310
311 #define M2K(m) (((m) & FREAD) && ((m) & FWRITE) ? \
312 KAUTH_REQ_PROCESS_PROCFS_RW : \
313 (m) & FWRITE ? KAUTH_REQ_PROCESS_PROCFS_WRITE : \
314 KAUTH_REQ_PROCESS_PROCFS_READ)
315
316 mutex_enter(p2->p_lock);
317 error = kauth_authorize_process(l1->l_cred, KAUTH_PROCESS_PROCFS,
318 p2, pfs, KAUTH_ARG(M2K(ap->a_mode)), NULL);
319 mutex_exit(p2->p_lock);
320 if (error) {
321 procfs_proc_unlock(p2);
322 return (error);
323 }
324
325 #undef M2K
326
327 switch (pfs->pfs_type) {
328 case PFSmem:
329 if (((pfs->pfs_flags & FWRITE) && (ap->a_mode & O_EXCL)) ||
330 ((pfs->pfs_flags & O_EXCL) && (ap->a_mode & FWRITE))) {
331 error = EBUSY;
332 break;
333 }
334
335 if (!proc_isunder(p2, l1)) {
336 error = EPERM;
337 break;
338 }
339
340 if (ap->a_mode & FWRITE)
341 pfs->pfs_flags = ap->a_mode & (FWRITE|O_EXCL);
342
343 break;
344
345 case PFSregs:
346 case PFSfpregs:
347 if (!proc_isunder(p2, l1)) {
348 error = EPERM;
349 break;
350 }
351 break;
352
353 default:
354 break;
355 }
356
357 procfs_proc_unlock(p2);
358 return (error);
359 }
360
361 /*
362 * close the pfsnode (vp) after doing i/o.
363 * (vp) is not locked on entry or exit.
364 *
365 * nothing to do for procfs other than undo
366 * any exclusive open flag (see _open above).
367 */
368 int
369 procfs_close(void *v)
370 {
371 struct vop_close_args /* {
372 struct vnode *a_vp;
373 int a_fflag;
374 kauth_cred_t a_cred;
375 } */ *ap = v;
376 struct pfsnode *pfs = VTOPFS(ap->a_vp);
377
378 switch (pfs->pfs_type) {
379 case PFSmem:
380 if ((ap->a_fflag & FWRITE) && (pfs->pfs_flags & O_EXCL))
381 pfs->pfs_flags &= ~(FWRITE|O_EXCL);
382 break;
383
384 default:
385 break;
386 }
387
388 return (0);
389 }
390
391 /*
392 * _inactive is called when the pfsnode
393 * is vrele'd and the reference count goes
394 * to zero. (vp) will be on the vnode free
395 * list, so to get it back vget() must be
396 * used.
397 *
398 * (vp) is locked on entry, but must be unlocked on exit.
399 */
400 int
401 procfs_inactive(void *v)
402 {
403 struct vop_inactive_v2_args /* {
404 struct vnode *a_vp;
405 bool *a_recycle;
406 } */ *ap = v;
407 struct vnode *vp = ap->a_vp;
408 struct pfsnode *pfs = VTOPFS(vp);
409
410 mutex_enter(&proc_lock);
411 *ap->a_recycle = (procfs_proc_find(vp->v_mount, pfs->pfs_pid) == NULL);
412 mutex_exit(&proc_lock);
413
414 return (0);
415 }
416
417 /*
418 * _reclaim is called when getnewvnode()
419 * wants to make use of an entry on the vnode
420 * free list. at this time the filesystem needs
421 * to free any private data and remove the node
422 * from any private lists.
423 */
424 int
425 procfs_reclaim(void *v)
426 {
427 struct vop_reclaim_v2_args /* {
428 struct vnode *a_vp;
429 } */ *ap = v;
430 struct vnode *vp = ap->a_vp;
431 struct pfsnode *pfs = VTOPFS(vp);
432
433 VOP_UNLOCK(vp);
434
435 /*
436 * To interlock with procfs_revoke_vnodes().
437 */
438 mutex_enter(vp->v_interlock);
439 vp->v_data = NULL;
440 mutex_exit(vp->v_interlock);
441 procfs_hashrem(pfs);
442 kmem_free(pfs, sizeof(*pfs));
443 return 0;
444 }
445
446 /*
447 * Return POSIX pathconf information applicable to special devices.
448 */
449 int
450 procfs_pathconf(void *v)
451 {
452 struct vop_pathconf_args /* {
453 struct vnode *a_vp;
454 int a_name;
455 register_t *a_retval;
456 } */ *ap = v;
457
458 switch (ap->a_name) {
459 case _PC_LINK_MAX:
460 *ap->a_retval = LINK_MAX;
461 return (0);
462 case _PC_MAX_CANON:
463 *ap->a_retval = MAX_CANON;
464 return (0);
465 case _PC_MAX_INPUT:
466 *ap->a_retval = MAX_INPUT;
467 return (0);
468 case _PC_PIPE_BUF:
469 *ap->a_retval = PIPE_BUF;
470 return (0);
471 case _PC_CHOWN_RESTRICTED:
472 *ap->a_retval = 1;
473 return (0);
474 case _PC_VDISABLE:
475 *ap->a_retval = _POSIX_VDISABLE;
476 return (0);
477 case _PC_SYNC_IO:
478 *ap->a_retval = 1;
479 return (0);
480 default:
481 return genfs_pathconf(ap);
482 }
483 /* NOTREACHED */
484 }
485
486 /*
487 * _print is used for debugging.
488 * just print a readable description
489 * of (vp).
490 */
491 int
492 procfs_print(void *v)
493 {
494 struct vop_print_args /* {
495 struct vnode *a_vp;
496 } */ *ap = v;
497 struct pfsnode *pfs = VTOPFS(ap->a_vp);
498
499 printf("tag VT_PROCFS, type %d, pid %d, mode %x, flags %lx\n",
500 pfs->pfs_type, pfs->pfs_pid, pfs->pfs_mode, pfs->pfs_flags);
501 return 0;
502 }
503
504 /*
505 * Works out the path to the target process's current
506 * working directory or chroot. If the caller is in a chroot and
507 * can't "reach" the target's cwd or root (or some other error
508 * occurs), a "/" is returned for the path.
509 */
510 static void
511 procfs_dir(pfstype t, struct lwp *caller, struct proc *target, char **bpp,
512 char *path, size_t len)
513 {
514 struct cwdinfo *cwdi;
515 struct vnode *vp, *rvp;
516 char *bp;
517
518 /*
519 * Lock target cwdi and take a reference to the vnode
520 * we are interested in to prevent it from disappearing
521 * before getcwd_common() below.
522 */
523 rw_enter(&target->p_cwdi->cwdi_lock, RW_READER);
524 switch (t) {
525 case PFScwd:
526 vp = target->p_cwdi->cwdi_cdir;
527 break;
528 case PFSchroot:
529 vp = target->p_cwdi->cwdi_rdir;
530 break;
531 default:
532 rw_exit(&target->p_cwdi->cwdi_lock);
533 return;
534 }
535 if (vp != NULL)
536 vref(vp);
537 rw_exit(&target->p_cwdi->cwdi_lock);
538
539 cwdi = caller->l_proc->p_cwdi;
540 rw_enter(&cwdi->cwdi_lock, RW_READER);
541
542 rvp = cwdi->cwdi_rdir;
543 bp = bpp ? *bpp : NULL;
544
545 /*
546 * XXX: this horrible kludge avoids locking panics when
547 * attempting to lookup links that point to within procfs
548 */
549 if (vp != NULL && vp->v_tag == VT_PROCFS) {
550 if (bpp) {
551 *--bp = '/';
552 *bpp = bp;
553 }
554 vrele(vp);
555 rw_exit(&cwdi->cwdi_lock);
556 return;
557 }
558
559 if (rvp == NULL)
560 rvp = rootvnode;
561 if (vp == NULL || getcwd_common(vp, rvp, bp ? &bp : NULL, path,
562 len / 2, 0, caller) != 0) {
563 if (bpp) {
564 bp = *bpp;
565 *--bp = '/';
566 }
567 }
568
569 if (bpp)
570 *bpp = bp;
571
572 if (vp != NULL)
573 vrele(vp);
574 rw_exit(&cwdi->cwdi_lock);
575 }
576
577 /*
578 * Invent attributes for pfsnode (vp) and store
579 * them in (vap).
580 * Directories lengths are returned as zero since
581 * any real length would require the genuine size
582 * to be computed, and nothing cares anyway.
583 *
584 * this is relatively minimal for procfs.
585 */
586 int
587 procfs_getattr(void *v)
588 {
589 struct vop_getattr_args /* {
590 struct vnode *a_vp;
591 struct vattr *a_vap;
592 kauth_cred_t a_cred;
593 } */ *ap = v;
594 struct vnode *vp = ap->a_vp;
595 struct pfsnode *pfs = VTOPFS(vp);
596 struct vattr *vap = ap->a_vap;
597 struct proc *procp;
598 char *path, *bp, bf[16];
599 int error;
600
601 /* first check the process still exists */
602 switch (pfs->pfs_type) {
603 case PFSroot:
604 case PFScurproc:
605 case PFSself:
606 procp = NULL;
607 break;
608
609 default:
610 error =
611 procfs_proc_lock(vp->v_mount, pfs->pfs_pid, &procp, ENOENT);
612 if (error != 0)
613 return (error);
614 break;
615 }
616
617 switch (pfs->pfs_type) {
618 case PFStask:
619 if (pfs->pfs_fd == -1) {
620 path = NULL;
621 break;
622 }
623 /*FALLTHROUGH*/
624 case PFScwd:
625 case PFSchroot:
626 path = malloc(MAXPATHLEN + 4, M_TEMP, M_WAITOK);
627 if (path == NULL && procp != NULL) {
628 procfs_proc_unlock(procp);
629 return (ENOMEM);
630 }
631 break;
632
633 default:
634 path = NULL;
635 break;
636 }
637
638 if (procp != NULL) {
639 mutex_enter(procp->p_lock);
640 error = kauth_authorize_process(kauth_cred_get(),
641 KAUTH_PROCESS_CANSEE, procp,
642 KAUTH_ARG(KAUTH_REQ_PROCESS_CANSEE_ENTRY), NULL, NULL);
643 mutex_exit(procp->p_lock);
644 if (error != 0) {
645 procfs_proc_unlock(procp);
646 if (path != NULL)
647 free(path, M_TEMP);
648 return (ENOENT);
649 }
650 }
651
652 error = 0;
653
654 /* start by zeroing out the attributes */
655 vattr_null(vap);
656
657 /* next do all the common fields */
658 vap->va_type = ap->a_vp->v_type;
659 vap->va_mode = pfs->pfs_mode;
660 vap->va_fileid = pfs->pfs_fileno;
661 vap->va_flags = 0;
662 vap->va_blocksize = PAGE_SIZE;
663
664 /*
665 * Make all times be current TOD.
666 *
667 * It would be possible to get the process start
668 * time from the p_stats structure, but there's
669 * no "file creation" time stamp anyway, and the
670 * p_stats structure is not addressable if u. gets
671 * swapped out for that process.
672 */
673 getnanotime(&vap->va_ctime);
674 vap->va_atime = vap->va_mtime = vap->va_ctime;
675 if (procp)
676 TIMEVAL_TO_TIMESPEC(&procp->p_stats->p_start,
677 &vap->va_birthtime);
678 else
679 getnanotime(&vap->va_birthtime);
680
681 switch (pfs->pfs_type) {
682 case PFSmem:
683 case PFSregs:
684 case PFSfpregs:
685 #if defined(__HAVE_PROCFS_MACHDEP) && defined(PROCFS_MACHDEP_PROTECT_CASES)
686 PROCFS_MACHDEP_PROTECT_CASES
687 #endif
688 /*
689 * If the process has exercised some setuid or setgid
690 * privilege, then rip away read/write permission so
691 * that only root can gain access.
692 */
693 if (procp->p_flag & PK_SUGID)
694 vap->va_mode &= ~(S_IRUSR|S_IWUSR);
695 /* FALLTHROUGH */
696 case PFSstatus:
697 case PFSstat:
698 case PFSnote:
699 case PFSnotepg:
700 case PFScmdline:
701 case PFSenviron:
702 case PFSemul:
703 case PFSstatm:
704
705 case PFSmap:
706 case PFSmaps:
707 case PFSlimit:
708 case PFSlimits:
709 case PFSauxv:
710 vap->va_nlink = 1;
711 vap->va_uid = kauth_cred_geteuid(procp->p_cred);
712 vap->va_gid = kauth_cred_getegid(procp->p_cred);
713 break;
714 case PFScwd:
715 case PFSchroot:
716 case PFSmeminfo:
717 case PFSdevices:
718 case PFScpuinfo:
719 case PFSuptime:
720 case PFSmounts:
721 case PFScpustat:
722 case PFSloadavg:
723 case PFSversion:
724 case PFSexe:
725 case PFSself:
726 case PFScurproc:
727 case PFSroot:
728 vap->va_nlink = 1;
729 vap->va_uid = vap->va_gid = 0;
730 break;
731
732 case PFSproc:
733 case PFStask:
734 case PFSfile:
735 case PFSfd:
736 break;
737
738 default:
739 panic("%s: %d/1", __func__, pfs->pfs_type);
740 }
741
742 /*
743 * now do the object specific fields
744 *
745 * The size could be set from struct reg, but it's hardly
746 * worth the trouble, and it puts some (potentially) machine
747 * dependent data into this machine-independent code. If it
748 * becomes important then this function should break out into
749 * a per-file stat function in the corresponding .c file.
750 */
751
752 switch (pfs->pfs_type) {
753 case PFSroot:
754 vap->va_bytes = vap->va_size = DEV_BSIZE;
755 break;
756
757 case PFSself:
758 case PFScurproc:
759 vap->va_bytes = vap->va_size =
760 snprintf(bf, sizeof(bf), "%ld", (long)curproc->p_pid);
761 break;
762 case PFStask:
763 if (pfs->pfs_fd != -1) {
764 vap->va_nlink = 1;
765 vap->va_uid = 0;
766 vap->va_gid = 0;
767 vap->va_bytes = vap->va_size =
768 snprintf(bf, sizeof(bf), "..");
769 break;
770 }
771 /*FALLTHROUGH*/
772 case PFSfd:
773 if (pfs->pfs_fd != -1) {
774 file_t *fp;
775
776 fp = fd_getfile2(procp, pfs->pfs_fd);
777 if (fp == NULL) {
778 error = EBADF;
779 break;
780 }
781 vap->va_nlink = 1;
782 vap->va_uid = kauth_cred_geteuid(fp->f_cred);
783 vap->va_gid = kauth_cred_getegid(fp->f_cred);
784 switch (fp->f_type) {
785 case DTYPE_VNODE:
786 vap->va_bytes = vap->va_size =
787 fp->f_vnode->v_size;
788 break;
789 default:
790 vap->va_bytes = vap->va_size = 0;
791 break;
792 }
793 closef(fp);
794 break;
795 }
796 /*FALLTHROUGH*/
797 case PFSproc:
798 vap->va_nlink = 2;
799 vap->va_uid = kauth_cred_geteuid(procp->p_cred);
800 vap->va_gid = kauth_cred_getegid(procp->p_cred);
801 vap->va_bytes = vap->va_size = DEV_BSIZE;
802 break;
803
804 case PFSfile:
805 error = EOPNOTSUPP;
806 break;
807
808 case PFSmem:
809 vap->va_bytes = vap->va_size =
810 ctob(procp->p_vmspace->vm_tsize +
811 procp->p_vmspace->vm_dsize +
812 procp->p_vmspace->vm_ssize);
813 break;
814
815 case PFSauxv:
816 vap->va_bytes = vap->va_size = procp->p_execsw->es_arglen;
817 break;
818
819 #if defined(PT_GETREGS) || defined(PT_SETREGS)
820 case PFSregs:
821 vap->va_bytes = vap->va_size = sizeof(struct reg);
822 break;
823 #endif
824
825 #if defined(PT_GETFPREGS) || defined(PT_SETFPREGS)
826 case PFSfpregs:
827 vap->va_bytes = vap->va_size = sizeof(struct fpreg);
828 break;
829 #endif
830
831 case PFSstatus:
832 case PFSstat:
833 case PFSnote:
834 case PFSnotepg:
835 case PFScmdline:
836 case PFSenviron:
837 case PFSmeminfo:
838 case PFSdevices:
839 case PFScpuinfo:
840 case PFSuptime:
841 case PFSmounts:
842 case PFScpustat:
843 case PFSloadavg:
844 case PFSstatm:
845 case PFSversion:
846 vap->va_bytes = vap->va_size = 0;
847 break;
848 case PFSlimit:
849 case PFSlimits:
850 case PFSmap:
851 case PFSmaps:
852 /*
853 * Advise a larger blocksize for the map files, so that
854 * they may be read in one pass.
855 */
856 vap->va_blocksize = 4 * PAGE_SIZE;
857 vap->va_bytes = vap->va_size = 0;
858 break;
859
860 case PFScwd:
861 case PFSchroot:
862 bp = path + MAXPATHLEN;
863 *--bp = '\0';
864 procfs_dir(pfs->pfs_type, curlwp, procp, &bp, path,
865 MAXPATHLEN);
866 vap->va_bytes = vap->va_size = strlen(bp);
867 break;
868
869 case PFSexe:
870 vap->va_bytes = vap->va_size = strlen(procp->p_path);
871 break;
872
873 case PFSemul:
874 vap->va_bytes = vap->va_size = strlen(procp->p_emul->e_name);
875 break;
876
877 #ifdef __HAVE_PROCFS_MACHDEP
878 PROCFS_MACHDEP_NODETYPE_CASES
879 error = procfs_machdep_getattr(ap->a_vp, vap, procp);
880 break;
881 #endif
882
883 default:
884 panic("%s: %d/2", __func__, pfs->pfs_type);
885 }
886
887 if (procp != NULL)
888 procfs_proc_unlock(procp);
889 if (path != NULL)
890 free(path, M_TEMP);
891
892 return (error);
893 }
894
895 /*ARGSUSED*/
896 int
897 procfs_setattr(void *v)
898 {
899 /*
900 * just fake out attribute setting
901 * it's not good to generate an error
902 * return, otherwise things like creat()
903 * will fail when they try to set the
904 * file length to 0. worse, this means
905 * that echo $note > /proc/$pid/note will fail.
906 */
907
908 return (0);
909 }
910
911 /*
912 * implement access checking.
913 *
914 * actually, the check for super-user is slightly
915 * broken since it will allow read access to write-only
916 * objects. this doesn't cause any particular trouble
917 * but does mean that the i/o entry points need to check
918 * that the operation really does make sense.
919 */
920 int
921 procfs_access(void *v)
922 {
923 struct vop_access_args /* {
924 struct vnode *a_vp;
925 accmode_t a_accmode;
926 kauth_cred_t a_cred;
927 } */ *ap = v;
928 struct vattr va;
929 int error;
930
931 if ((error = VOP_GETATTR(ap->a_vp, &va, ap->a_cred)) != 0)
932 return (error);
933
934 return kauth_authorize_vnode(ap->a_cred,
935 KAUTH_ACCESS_ACTION(ap->a_accmode, ap->a_vp->v_type, va.va_mode),
936 ap->a_vp, NULL, genfs_can_access(ap->a_vp, ap->a_cred,
937 va.va_uid, va.va_gid, va.va_mode, NULL, ap->a_accmode));
938 }
939
940 /*
941 * lookup. this is incredibly complicated in the
942 * general case, however for most pseudo-filesystems
943 * very little needs to be done.
944 *
945 * Locking isn't hard here, just poorly documented.
946 *
947 * If we're looking up ".", just vref the parent & return it.
948 *
949 * If we're looking up "..", unlock the parent, and lock "..". If everything
950 * went ok, and we're on the last component and the caller requested the
951 * parent locked, try to re-lock the parent. We do this to prevent lock
952 * races.
953 *
954 * For anything else, get the needed node. Then unlock the parent if not
955 * the last component or not LOCKPARENT (i.e. if we wouldn't re-lock the
956 * parent in the .. case).
957 *
958 * We try to exit with the parent locked in error cases.
959 */
960 int
961 procfs_lookup(void *v)
962 {
963 struct vop_lookup_v2_args /* {
964 struct vnode * a_dvp;
965 struct vnode ** a_vpp;
966 struct componentname * a_cnp;
967 } */ *ap = v;
968 struct componentname *cnp = ap->a_cnp;
969 struct vnode **vpp = ap->a_vpp;
970 struct vnode *dvp = ap->a_dvp;
971 const char *pname = cnp->cn_nameptr;
972 const struct proc_target *pt = NULL;
973 struct vnode *fvp;
974 pid_t pid, vnpid;
975 struct pfsnode *pfs;
976 struct proc *p = NULL;
977 struct lwp *plwp;
978 int i, error;
979 pfstype type;
980
981 *vpp = NULL;
982
983 if ((error = VOP_ACCESS(dvp, VEXEC, cnp->cn_cred)) != 0)
984 return (error);
985
986 if (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)
987 return (EROFS);
988
989 if (cnp->cn_namelen == 1 && *pname == '.') {
990 *vpp = dvp;
991 vref(dvp);
992 return (0);
993 }
994
995 pfs = VTOPFS(dvp);
996 switch (pfs->pfs_type) {
997 case PFSroot:
998 /*
999 * Shouldn't get here with .. in the root node.
1000 */
1001 if (cnp->cn_flags & ISDOTDOT)
1002 return (EIO);
1003
1004 for (i = 0; i < nproc_root_targets; i++) {
1005 pt = &proc_root_targets[i];
1006 /*
1007 * check for node match. proc is always NULL here,
1008 * so call pt_valid with constant NULL lwp.
1009 */
1010 if (cnp->cn_namelen == pt->pt_namlen &&
1011 memcmp(pt->pt_name, pname, cnp->cn_namelen) == 0 &&
1012 (pt->pt_valid == NULL ||
1013 (*pt->pt_valid)(NULL, dvp->v_mount)))
1014 break;
1015 }
1016
1017 if (i != nproc_root_targets) {
1018 error = procfs_allocvp(dvp->v_mount, vpp, 0,
1019 pt->pt_pfstype, -1);
1020 return (error);
1021 }
1022
1023 if (CNEQ(cnp, "curproc", 7)) {
1024 pid = curproc->p_pid;
1025 vnpid = 0;
1026 type = PFScurproc;
1027 } else if (CNEQ(cnp, "self", 4)) {
1028 pid = curproc->p_pid;
1029 vnpid = 0;
1030 type = PFSself;
1031 } else {
1032 pid = (pid_t)atoi(pname, cnp->cn_namelen);
1033 vnpid = pid;
1034 type = PFSproc;
1035 }
1036
1037 if (procfs_proc_lock(dvp->v_mount, pid, &p, ESRCH) != 0)
1038 break;
1039 error = procfs_allocvp(dvp->v_mount, vpp, vnpid, type, -1);
1040 procfs_proc_unlock(p);
1041 return (error);
1042
1043 case PFSproc:
1044 if (cnp->cn_flags & ISDOTDOT) {
1045 error = procfs_allocvp(dvp->v_mount, vpp, 0, PFSroot,
1046 -1);
1047 return (error);
1048 }
1049
1050 if (procfs_proc_lock(dvp->v_mount, pfs->pfs_pid, &p,
1051 ESRCH) != 0)
1052 break;
1053
1054 mutex_enter(p->p_lock);
1055 LIST_FOREACH(plwp, &p->p_lwps, l_sibling) {
1056 if (plwp->l_stat != LSZOMB)
1057 break;
1058 }
1059 /* Process is exiting if no-LWPS or all LWPs are LSZOMB */
1060 if (plwp == NULL) {
1061 mutex_exit(p->p_lock);
1062 procfs_proc_unlock(p);
1063 return ESRCH;
1064 }
1065
1066 lwp_addref(plwp);
1067 mutex_exit(p->p_lock);
1068
1069 for (pt = proc_targets, i = 0; i < nproc_targets; pt++, i++) {
1070 int found;
1071
1072 found = cnp->cn_namelen == pt->pt_namlen &&
1073 memcmp(pt->pt_name, pname, cnp->cn_namelen) == 0 &&
1074 (pt->pt_valid == NULL
1075 || (*pt->pt_valid)(plwp, dvp->v_mount));
1076 if (found)
1077 break;
1078 }
1079 lwp_delref(plwp);
1080
1081 if (i == nproc_targets) {
1082 procfs_proc_unlock(p);
1083 break;
1084 }
1085 if (pt->pt_pfstype == PFSfile) {
1086 fvp = p->p_textvp;
1087 /* We already checked that it exists. */
1088 vref(fvp);
1089 procfs_proc_unlock(p);
1090 *vpp = fvp;
1091 return (0);
1092 }
1093
1094 error = procfs_allocvp(dvp->v_mount, vpp, pfs->pfs_pid,
1095 pt->pt_pfstype, -1);
1096 procfs_proc_unlock(p);
1097 return (error);
1098
1099 case PFSfd: {
1100 int fd;
1101 file_t *fp;
1102
1103 if ((error = procfs_proc_lock(dvp->v_mount, pfs->pfs_pid, &p,
1104 ENOENT)) != 0)
1105 return error;
1106
1107 if (cnp->cn_flags & ISDOTDOT) {
1108 error = procfs_allocvp(dvp->v_mount, vpp, pfs->pfs_pid,
1109 PFSproc, -1);
1110 procfs_proc_unlock(p);
1111 return (error);
1112 }
1113 fd = atoi(pname, cnp->cn_namelen);
1114
1115 fp = fd_getfile2(p, fd);
1116 if (fp == NULL) {
1117 procfs_proc_unlock(p);
1118 return ENOENT;
1119 }
1120 fvp = fp->f_vnode;
1121
1122 /* Don't show directories */
1123 if (fp->f_type == DTYPE_VNODE && fvp->v_type != VDIR &&
1124 !procfs_proc_is_linux_compat()) {
1125 vref(fvp);
1126 closef(fp);
1127 procfs_proc_unlock(p);
1128 *vpp = fvp;
1129 return 0;
1130 }
1131
1132 closef(fp);
1133 error = procfs_allocvp(dvp->v_mount, vpp, pfs->pfs_pid,
1134 PFSfd, fd);
1135 procfs_proc_unlock(p);
1136 return error;
1137 }
1138 case PFStask: {
1139 int xpid;
1140
1141 if ((error = procfs_proc_lock(dvp->v_mount, pfs->pfs_pid, &p,
1142 ENOENT)) != 0)
1143 return error;
1144
1145 if (cnp->cn_flags & ISDOTDOT) {
1146 error = procfs_allocvp(dvp->v_mount, vpp, pfs->pfs_pid,
1147 PFSproc, -1);
1148 procfs_proc_unlock(p);
1149 return (error);
1150 }
1151 xpid = atoi(pname, cnp->cn_namelen);
1152
1153 if (xpid != pfs->pfs_pid) {
1154 procfs_proc_unlock(p);
1155 return ENOENT;
1156 }
1157 error = procfs_allocvp(dvp->v_mount, vpp, pfs->pfs_pid,
1158 PFStask, 0);
1159 procfs_proc_unlock(p);
1160 return error;
1161 }
1162 default:
1163 return (ENOTDIR);
1164 }
1165
1166 return (cnp->cn_nameiop == LOOKUP ? ENOENT : EROFS);
1167 }
1168
1169 int
1170 procfs_validfile(struct lwp *l, struct mount *mp)
1171 {
1172 return l != NULL && l->l_proc != NULL && l->l_proc->p_textvp != NULL;
1173 }
1174
1175 static int
1176 procfs_validfile_linux(struct lwp *l, struct mount *mp)
1177 {
1178 return procfs_use_linux_compat(mp) &&
1179 (l == NULL || l->l_proc == NULL || procfs_validfile(l, mp));
1180 }
1181
1182 struct procfs_root_readdir_ctx {
1183 struct uio *uiop;
1184 off_t *cookies;
1185 int ncookies;
1186 off_t off;
1187 off_t startoff;
1188 int error;
1189 };
1190
1191 static int
1192 procfs_root_readdir_callback(struct proc *p, void *arg)
1193 {
1194 struct procfs_root_readdir_ctx *ctxp = arg;
1195 struct dirent d;
1196 struct uio *uiop;
1197 int error;
1198
1199 uiop = ctxp->uiop;
1200 if (uiop->uio_resid < UIO_MX)
1201 return -1; /* no space */
1202
1203 if (kauth_authorize_process(kauth_cred_get(),
1204 KAUTH_PROCESS_CANSEE, p,
1205 KAUTH_ARG(KAUTH_REQ_PROCESS_CANSEE_ENTRY), NULL, NULL) != 0)
1206 return 0;
1207
1208 if (ctxp->off < ctxp->startoff) {
1209 ctxp->off++;
1210 return 0;
1211 }
1212
1213 memset(&d, 0, UIO_MX);
1214 d.d_reclen = UIO_MX;
1215 d.d_fileno = PROCFS_FILENO(p->p_pid, PFSproc, -1);
1216 d.d_namlen = snprintf(d.d_name,
1217 UIO_MX - offsetof(struct dirent, d_name), "%ld", (long)p->p_pid);
1218 d.d_type = DT_DIR;
1219
1220 mutex_exit(&proc_lock);
1221 error = uiomove(&d, UIO_MX, uiop);
1222 mutex_enter(&proc_lock);
1223 if (error) {
1224 ctxp->error = error;
1225 return -1;
1226 }
1227
1228 ctxp->ncookies++;
1229 if (ctxp->cookies)
1230 *(ctxp->cookies)++ = ctxp->off + 1;
1231 ctxp->off++;
1232
1233 return 0;
1234 }
1235
1236 /*
1237 * readdir returns directory entries from pfsnode (vp).
1238 *
1239 * the strategy here with procfs is to generate a single
1240 * directory entry at a time (struct dirent) and then
1241 * copy that out to userland using uiomove. a more efficient
1242 * though more complex implementation, would try to minimize
1243 * the number of calls to uiomove(). for procfs, this is
1244 * hardly worth the added code complexity.
1245 *
1246 * this should just be done through read()
1247 */
1248 int
1249 procfs_readdir(void *v)
1250 {
1251 struct vop_readdir_args /* {
1252 struct vnode *a_vp;
1253 struct uio *a_uio;
1254 kauth_cred_t a_cred;
1255 int *a_eofflag;
1256 off_t **a_cookies;
1257 int *a_ncookies;
1258 } */ *ap = v;
1259 struct uio *uio = ap->a_uio;
1260 struct dirent d;
1261 struct pfsnode *pfs;
1262 off_t i;
1263 int error;
1264 off_t *cookies = NULL;
1265 int ncookies;
1266 struct vnode *vp;
1267 const struct proc_target *pt;
1268 struct procfs_root_readdir_ctx ctx;
1269 struct proc *p = NULL;
1270 struct lwp *l;
1271 int nfd;
1272 int nc = 0;
1273
1274 vp = ap->a_vp;
1275 pfs = VTOPFS(vp);
1276
1277 if (uio->uio_resid < UIO_MX)
1278 return (EINVAL);
1279 if (uio->uio_offset < 0)
1280 return (EINVAL);
1281
1282 error = 0;
1283 i = uio->uio_offset;
1284 memset(&d, 0, UIO_MX);
1285 d.d_reclen = UIO_MX;
1286 ncookies = uio->uio_resid / UIO_MX;
1287
1288 switch (pfs->pfs_type) {
1289 /*
1290 * this is for the process-specific sub-directories.
1291 * all that is needed to is copy out all the entries
1292 * from the procent[] table (top of this file).
1293 */
1294 case PFSproc: {
1295
1296 if (i >= nproc_targets)
1297 return 0;
1298
1299 if (procfs_proc_lock(vp->v_mount, pfs->pfs_pid, &p, ESRCH) != 0)
1300 break;
1301
1302 if (ap->a_ncookies) {
1303 ncookies = uimin(ncookies, (nproc_targets - i));
1304 cookies = malloc(ncookies * sizeof (off_t),
1305 M_TEMP, M_WAITOK);
1306 *ap->a_cookies = cookies;
1307 }
1308
1309 for (pt = &proc_targets[i];
1310 uio->uio_resid >= UIO_MX && i < nproc_targets; pt++, i++) {
1311 if (pt->pt_valid) {
1312 /* XXXSMP LWP can disappear */
1313 mutex_enter(p->p_lock);
1314 l = LIST_FIRST(&p->p_lwps);
1315 KASSERT(l != NULL);
1316 mutex_exit(p->p_lock);
1317 if ((*pt->pt_valid)(l, vp->v_mount) == 0)
1318 continue;
1319 }
1320
1321 d.d_fileno = PROCFS_FILENO(pfs->pfs_pid,
1322 pt->pt_pfstype, -1);
1323 d.d_namlen = pt->pt_namlen;
1324 memcpy(d.d_name, pt->pt_name, pt->pt_namlen + 1);
1325 d.d_type = pt->pt_type;
1326
1327 if ((error = uiomove(&d, UIO_MX, uio)) != 0)
1328 break;
1329 if (cookies)
1330 *cookies++ = i + 1;
1331 }
1332
1333 procfs_proc_unlock(p);
1334 break;
1335 }
1336 case PFSfd: {
1337 file_t *fp;
1338 int lim;
1339
1340 if ((error = procfs_proc_lock(vp->v_mount, pfs->pfs_pid, &p,
1341 ESRCH)) != 0)
1342 return error;
1343
1344 /* XXX Should this be by file as well? */
1345 if (kauth_authorize_process(kauth_cred_get(),
1346 KAUTH_PROCESS_CANSEE, p,
1347 KAUTH_ARG(KAUTH_REQ_PROCESS_CANSEE_OPENFILES), NULL,
1348 NULL) != 0) {
1349 procfs_proc_unlock(p);
1350 return ESRCH;
1351 }
1352
1353 nfd = atomic_load_consume(&p->p_fd->fd_dt)->dt_nfiles;
1354
1355 lim = uimin((int)p->p_rlimit[RLIMIT_NOFILE].rlim_cur, maxfiles);
1356 if (i >= lim) {
1357 procfs_proc_unlock(p);
1358 return 0;
1359 }
1360
1361 if (ap->a_ncookies) {
1362 ncookies = uimin(ncookies, (nfd + 2 - i));
1363 cookies = malloc(ncookies * sizeof (off_t),
1364 M_TEMP, M_WAITOK);
1365 *ap->a_cookies = cookies;
1366 }
1367
1368 for (; i < 2 && uio->uio_resid >= UIO_MX; i++) {
1369 pt = &proc_targets[i];
1370 d.d_namlen = pt->pt_namlen;
1371 d.d_fileno = PROCFS_FILENO(pfs->pfs_pid,
1372 pt->pt_pfstype, -1);
1373 (void)memcpy(d.d_name, pt->pt_name, pt->pt_namlen + 1);
1374 d.d_type = pt->pt_type;
1375 if ((error = uiomove(&d, UIO_MX, uio)) != 0)
1376 break;
1377 if (cookies)
1378 *cookies++ = i + 1;
1379 nc++;
1380 }
1381 if (error)
1382 goto out;
1383 for (; uio->uio_resid >= UIO_MX && i < nfd; i++) {
1384 /* check the descriptor exists */
1385 if ((fp = fd_getfile2(p, i - 2)) == NULL)
1386 continue;
1387 closef(fp);
1388
1389 d.d_fileno = PROCFS_FILENO(pfs->pfs_pid, PFSfd, i - 2);
1390 d.d_namlen = snprintf(d.d_name, sizeof(d.d_name),
1391 "%lld", (long long)(i - 2));
1392 d.d_type = fttodt(fp);
1393 if ((error = uiomove(&d, UIO_MX, uio)) != 0)
1394 break;
1395 if (cookies)
1396 *cookies++ = i + 1;
1397 nc++;
1398 }
1399 goto out;
1400 }
1401 case PFStask: {
1402
1403 if ((error = procfs_proc_lock(vp->v_mount, pfs->pfs_pid, &p,
1404 ESRCH)) != 0)
1405 return error;
1406
1407 nfd = 3; /* ., .., pid */
1408
1409 if (ap->a_ncookies) {
1410 ncookies = uimin(ncookies, (nfd + 2 - i));
1411 cookies = malloc(ncookies * sizeof (off_t),
1412 M_TEMP, M_WAITOK);
1413 *ap->a_cookies = cookies;
1414 }
1415
1416 for (; i < 2 && uio->uio_resid >= UIO_MX; i++) {
1417 pt = &proc_targets[i];
1418 d.d_namlen = pt->pt_namlen;
1419 d.d_fileno = PROCFS_FILENO(pfs->pfs_pid,
1420 pt->pt_pfstype, -1);
1421 (void)memcpy(d.d_name, pt->pt_name, pt->pt_namlen + 1);
1422 d.d_type = pt->pt_type;
1423 if ((error = uiomove(&d, UIO_MX, uio)) != 0)
1424 break;
1425 if (cookies)
1426 *cookies++ = i + 1;
1427 nc++;
1428 }
1429 if (error)
1430 goto out;
1431 for (; uio->uio_resid >= UIO_MX && i < nfd; i++) {
1432 /* check the descriptor exists */
1433 d.d_fileno = PROCFS_FILENO(pfs->pfs_pid, PFStask,
1434 i - 2);
1435 d.d_namlen = snprintf(d.d_name, sizeof(d.d_name),
1436 "%ld", (long)pfs->pfs_pid);
1437 d.d_type = DT_LNK;
1438 if ((error = uiomove(&d, UIO_MX, uio)) != 0)
1439 break;
1440 if (cookies)
1441 *cookies++ = i + 1;
1442 nc++;
1443 }
1444 goto out;
1445 }
1446
1447 /*
1448 * this is for the root of the procfs filesystem
1449 * what is needed are special entries for "curproc"
1450 * and "self" followed by an entry for each process
1451 * on allproc.
1452 */
1453
1454 case PFSroot: {
1455
1456 if (ap->a_ncookies) {
1457 /*
1458 * XXX Potentially allocating too much space here,
1459 * but I'm lazy. This loop needs some work.
1460 */
1461 cookies = malloc(ncookies * sizeof (off_t),
1462 M_TEMP, M_WAITOK);
1463 *ap->a_cookies = cookies;
1464 }
1465
1466 /* 0 ... 3 are static entries. */
1467 for (; i <= 3 && uio->uio_resid >= UIO_MX; i++) {
1468 switch (i) {
1469 case 0: /* `.' */
1470 case 1: /* `..' */
1471 d.d_fileno = PROCFS_FILENO(0, PFSroot, -1);
1472 d.d_namlen = i + 1;
1473 memcpy(d.d_name, "..", d.d_namlen);
1474 d.d_name[i + 1] = '\0';
1475 d.d_type = DT_DIR;
1476 break;
1477
1478 case 2:
1479 d.d_fileno = PROCFS_FILENO(0, PFScurproc, -1);
1480 d.d_namlen = sizeof("curproc") - 1;
1481 memcpy(d.d_name, "curproc", sizeof("curproc"));
1482 d.d_type = DT_LNK;
1483 break;
1484
1485 case 3:
1486 d.d_fileno = PROCFS_FILENO(0, PFSself, -1);
1487 d.d_namlen = sizeof("self") - 1;
1488 memcpy(d.d_name, "self", sizeof("self"));
1489 d.d_type = DT_LNK;
1490 break;
1491 }
1492
1493 if ((error = uiomove(&d, UIO_MX, uio)) != 0)
1494 break;
1495 nc++;
1496 if (cookies)
1497 *cookies++ = i + 1;
1498 }
1499 if (error)
1500 break;
1501 /* 4 ... are process entries. */
1502 ctx.uiop = uio;
1503 ctx.error = 0;
1504 ctx.off = 4;
1505 ctx.startoff = i;
1506 ctx.cookies = cookies;
1507 ctx.ncookies = nc;
1508 proclist_foreach_call(&allproc,
1509 procfs_root_readdir_callback, &ctx);
1510 cookies = ctx.cookies;
1511 nc = ctx.ncookies;
1512 error = ctx.error;
1513 if (error)
1514 break;
1515
1516 /* misc entries. */
1517 if (i < ctx.off)
1518 i = ctx.off;
1519 if (i >= ctx.off + nproc_root_targets)
1520 break;
1521 error = procfs_proc_lock(vp->v_mount, pfs->pfs_pid, &p, ESRCH);
1522 if (error)
1523 break;
1524 for (pt = &proc_root_targets[i - ctx.off];
1525 uio->uio_resid >= UIO_MX &&
1526 pt < &proc_root_targets[nproc_root_targets];
1527 pt++, i++) {
1528 if (pt->pt_valid &&
1529 (*pt->pt_valid)(NULL, vp->v_mount) == 0)
1530 continue;
1531 if (kauth_authorize_process(kauth_cred_get(),
1532 KAUTH_PROCESS_CANSEE, p,
1533 KAUTH_ARG(KAUTH_REQ_PROCESS_CANSEE_ENTRY),
1534 NULL, NULL) != 0)
1535 continue;
1536 d.d_fileno = PROCFS_FILENO(0, pt->pt_pfstype, -1);
1537 d.d_namlen = pt->pt_namlen;
1538 memcpy(d.d_name, pt->pt_name, pt->pt_namlen + 1);
1539 d.d_type = pt->pt_type;
1540
1541 if ((error = uiomove(&d, UIO_MX, uio)) != 0)
1542 break;
1543 nc++;
1544 if (cookies)
1545 *cookies++ = i + 1;
1546 }
1547 out:
1548 KASSERT(p != NULL);
1549 ncookies = nc;
1550 procfs_proc_unlock(p);
1551 break;
1552 }
1553
1554 default:
1555 error = ENOTDIR;
1556 break;
1557 }
1558
1559 if (ap->a_ncookies) {
1560 if (error) {
1561 if (cookies)
1562 free(*ap->a_cookies, M_TEMP);
1563 *ap->a_ncookies = 0;
1564 *ap->a_cookies = NULL;
1565 } else
1566 *ap->a_ncookies = ncookies;
1567 }
1568 uio->uio_offset = i;
1569 return (error);
1570 }
1571
1572 /*
1573 * readlink reads the link of `curproc' and others
1574 */
1575 int
1576 procfs_readlink(void *v)
1577 {
1578 struct vop_readlink_args *ap = v;
1579 char bf[16]; /* should be enough */
1580 char *bp = bf;
1581 char *path = NULL;
1582 int len = 0;
1583 int error = 0;
1584 struct vnode *vp = ap->a_vp;
1585 struct pfsnode *pfs = VTOPFS(vp);
1586 struct proc *pown = NULL;
1587
1588 if (pfs->pfs_fileno == PROCFS_FILENO(0, PFScurproc, -1))
1589 len = snprintf(bf, sizeof(bf), "%ld", (long)curproc->p_pid);
1590 else if (pfs->pfs_fileno == PROCFS_FILENO(0, PFSself, -1))
1591 len = snprintf(bf, sizeof(bf), "%s", "curproc");
1592 else if (pfs->pfs_fileno == PROCFS_FILENO(pfs->pfs_pid, PFStask, 0))
1593 len = snprintf(bf, sizeof(bf), "..");
1594 else if (pfs->pfs_fileno == PROCFS_FILENO(pfs->pfs_pid, PFSexe, -1)) {
1595 if ((error = procfs_proc_lock(vp->v_mount, pfs->pfs_pid, &pown,
1596 ESRCH)) != 0)
1597 return error;
1598 bp = pown->p_path;
1599 len = strlen(bp);
1600 } else if (pfs->pfs_fileno == PROCFS_FILENO(pfs->pfs_pid, PFScwd, -1) ||
1601 pfs->pfs_fileno == PROCFS_FILENO(pfs->pfs_pid, PFSchroot, -1)) {
1602 if ((error = procfs_proc_lock(vp->v_mount, pfs->pfs_pid, &pown,
1603 ESRCH)) != 0)
1604 return error;
1605 path = malloc(MAXPATHLEN + 4, M_TEMP, M_WAITOK);
1606 if (path == NULL) {
1607 procfs_proc_unlock(pown);
1608 return (ENOMEM);
1609 }
1610 bp = path + MAXPATHLEN;
1611 *--bp = '\0';
1612 procfs_dir(PROCFS_TYPE(pfs->pfs_fileno), curlwp, pown,
1613 &bp, path, MAXPATHLEN);
1614 len = strlen(bp);
1615 } else {
1616 file_t *fp;
1617 struct vnode *vxp;
1618
1619 if ((error = procfs_proc_lock(vp->v_mount, pfs->pfs_pid, &pown,
1620 ESRCH)) != 0)
1621 return error;
1622
1623 fp = fd_getfile2(pown, pfs->pfs_fd);
1624 if (fp == NULL) {
1625 procfs_proc_unlock(pown);
1626 return EBADF;
1627 }
1628
1629 switch (fp->f_type) {
1630 case DTYPE_VNODE:
1631 vxp = fp->f_vnode;
1632 if (vxp->v_type != VDIR &&
1633 !procfs_proc_is_linux_compat()) {
1634 error = EINVAL;
1635 break;
1636 }
1637 if ((path = malloc(MAXPATHLEN, M_TEMP, M_WAITOK))
1638 == NULL) {
1639 error = ENOMEM;
1640 break;
1641 }
1642 bp = path + MAXPATHLEN;
1643 *--bp = '\0';
1644
1645 /*
1646 * XXX: kludge to avoid locking against ourselves
1647 * in getcwd()
1648 */
1649 if (vxp->v_tag == VT_PROCFS) {
1650 *--bp = '/';
1651 } else {
1652 rw_enter(&curproc->p_cwdi->cwdi_lock,
1653 RW_READER);
1654 vp = curproc->p_cwdi->cwdi_rdir;
1655 if (vp == NULL)
1656 vp = rootvnode;
1657 error = getcwd_common(vxp, vp, &bp, path,
1658 MAXPATHLEN / 2, 0, curlwp);
1659 rw_exit(&curproc->p_cwdi->cwdi_lock);
1660 }
1661 if (error)
1662 break;
1663 len = strlen(bp);
1664 break;
1665
1666 case DTYPE_MISC:
1667 len = snprintf(bf, sizeof(bf), "%s", "[misc]");
1668 break;
1669
1670 case DTYPE_KQUEUE:
1671 len = snprintf(bf, sizeof(bf), "%s", "[kqueue]");
1672 break;
1673
1674 case DTYPE_SEM:
1675 len = snprintf(bf, sizeof(bf), "%s", "[ksem]");
1676 break;
1677
1678 default:
1679 error = EINVAL;
1680 break;
1681 }
1682 closef(fp);
1683 }
1684
1685 if (error == 0)
1686 error = uiomove(bp, len, ap->a_uio);
1687 if (pown)
1688 procfs_proc_unlock(pown);
1689 if (path)
1690 free(path, M_TEMP);
1691 return error;
1692 }
1693
1694 int
1695 procfs_getpages(void *v)
1696 {
1697 struct vop_getpages_args /* {
1698 struct vnode *a_vp;
1699 voff_t a_offset;
1700 struct vm_page **a_m;
1701 int *a_count;
1702 int a_centeridx;
1703 vm_prot_t a_access_type;
1704 int a_advice;
1705 int a_flags;
1706 } */ *ap = v;
1707
1708 if ((ap->a_flags & PGO_LOCKED) == 0)
1709 rw_exit(ap->a_vp->v_uobj.vmobjlock);
1710
1711 return (EFAULT);
1712 }
1713
1714 /*
1715 * convert decimal ascii to int
1716 */
1717 static int
1718 atoi(const char *b, size_t len)
1719 {
1720 int p = 0;
1721
1722 while (len--) {
1723 char c = *b++;
1724 if (c < '0' || c > '9')
1725 return -1;
1726 p = 10 * p + (c - '0');
1727 }
1728
1729 return p;
1730 }
1731
1732 /**
1733 * convert DTYPE_XXX to corresponding DT_XXX
1734 * matching what procfs_loadvnode() does.
1735 */
1736 static uint8_t
1737 fttodt(file_t *fp)
1738 {
1739 switch (fp->f_type) {
1740 case DTYPE_VNODE:
1741 switch (fp->f_vnode->v_type) {
1742 case VREG: return DT_REG;
1743 case VDIR: return DT_LNK; /* symlink */
1744 case VBLK: return DT_BLK;
1745 case VCHR: return DT_CHR;
1746 case VLNK: return DT_LNK;
1747 case VSOCK: return DT_SOCK;
1748 case VFIFO: return DT_FIFO;
1749 default: return DT_UNKNOWN;
1750 }
1751 case DTYPE_PIPE: return DT_FIFO;
1752 case DTYPE_SOCKET: return DT_SOCK;
1753 case DTYPE_KQUEUE: /*FALLTHROUGH*/
1754 case DTYPE_MISC: /*FALLTHROUGH*/
1755 case DTYPE_SEM: return DT_LNK; /* symlinks */
1756 default: return DT_UNKNOWN;
1757 }
1758 }
1759