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