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