1 /* $NetBSD: procfs_vfsops.c,v 1.93 2014/09/05 09:26:16 matt Exp $ */ 2 3 /* 4 * Copyright (c) 1993 5 * The Regents of the University of California. All rights reserved. 6 * 7 * This code is derived from software contributed to Berkeley by 8 * Jan-Simon Pendry. 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 * 3. Neither the name of the University nor the names of its contributors 19 * may be used to endorse or promote products derived from this software 20 * without specific prior written permission. 21 * 22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32 * SUCH DAMAGE. 33 * 34 * @(#)procfs_vfsops.c 8.7 (Berkeley) 5/10/95 35 */ 36 37 /* 38 * Copyright (c) 1993 Jan-Simon Pendry 39 * 40 * This code is derived from software contributed to Berkeley by 41 * Jan-Simon Pendry. 42 * 43 * Redistribution and use in source and binary forms, with or without 44 * modification, are permitted provided that the following conditions 45 * are met: 46 * 1. Redistributions of source code must retain the above copyright 47 * notice, this list of conditions and the following disclaimer. 48 * 2. Redistributions in binary form must reproduce the above copyright 49 * notice, this list of conditions and the following disclaimer in the 50 * documentation and/or other materials provided with the distribution. 51 * 3. All advertising materials mentioning features or use of this software 52 * must display the following acknowledgement: 53 * This product includes software developed by the University of 54 * California, Berkeley and its contributors. 55 * 4. Neither the name of the University nor the names of its contributors 56 * may be used to endorse or promote products derived from this software 57 * without specific prior written permission. 58 * 59 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 60 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 61 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 62 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 63 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 64 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 65 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 66 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 67 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 68 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 69 * SUCH DAMAGE. 70 * 71 * @(#)procfs_vfsops.c 8.7 (Berkeley) 5/10/95 72 */ 73 74 /* 75 * procfs VFS interface 76 */ 77 78 #include <sys/cdefs.h> 79 __KERNEL_RCSID(0, "$NetBSD: procfs_vfsops.c,v 1.93 2014/09/05 09:26:16 matt Exp $"); 80 81 #if defined(_KERNEL_OPT) 82 #include "opt_compat_netbsd.h" 83 #endif 84 85 #include <sys/param.h> 86 #include <sys/time.h> 87 #include <sys/kernel.h> 88 #include <sys/systm.h> 89 #include <sys/sysctl.h> 90 #include <sys/proc.h> 91 #include <sys/buf.h> 92 #include <sys/syslog.h> 93 #include <sys/mount.h> 94 #include <sys/dirent.h> 95 #include <sys/signalvar.h> 96 #include <sys/vnode.h> 97 #include <sys/malloc.h> 98 #include <sys/file.h> 99 #include <sys/filedesc.h> 100 #include <sys/kauth.h> 101 #include <sys/module.h> 102 103 #include <miscfs/genfs/genfs.h> 104 105 #include <miscfs/procfs/procfs.h> 106 107 #include <uvm/uvm_extern.h> /* for PAGE_SIZE */ 108 109 MODULE(MODULE_CLASS_VFS, procfs, NULL); 110 111 VFS_PROTOS(procfs); 112 113 static struct sysctllog *procfs_sysctl_log; 114 115 static kauth_listener_t procfs_listener; 116 117 /* 118 * VFS Operations. 119 * 120 * mount system call 121 */ 122 /* ARGSUSED */ 123 int 124 procfs_mount( 125 struct mount *mp, 126 const char *path, 127 void *data, 128 size_t *data_len) 129 { 130 struct lwp *l = curlwp; 131 struct procfsmount *pmnt; 132 struct procfs_args *args = data; 133 int error; 134 135 if (args == NULL) 136 return EINVAL; 137 138 if (UIO_MX & (UIO_MX-1)) { 139 log(LOG_ERR, "procfs: invalid directory entry size"); 140 return (EINVAL); 141 } 142 143 if (mp->mnt_flag & MNT_GETARGS) { 144 if (*data_len < sizeof *args) 145 return EINVAL; 146 147 pmnt = VFSTOPROC(mp); 148 if (pmnt == NULL) 149 return EIO; 150 args->version = PROCFS_ARGSVERSION; 151 args->flags = pmnt->pmnt_flags; 152 *data_len = sizeof *args; 153 return 0; 154 } 155 156 if (mp->mnt_flag & MNT_UPDATE) 157 return (EOPNOTSUPP); 158 159 if (*data_len >= sizeof *args && args->version != PROCFS_ARGSVERSION) 160 return EINVAL; 161 162 pmnt = kmem_zalloc(sizeof(struct procfsmount), KM_SLEEP); 163 164 mp->mnt_stat.f_namemax = PROCFS_MAXNAMLEN; 165 mp->mnt_flag |= MNT_LOCAL; 166 mp->mnt_data = pmnt; 167 vfs_getnewfsid(mp); 168 169 error = set_statvfs_info(path, UIO_USERSPACE, "procfs", UIO_SYSSPACE, 170 mp->mnt_op->vfs_name, mp, l); 171 pmnt->pmnt_exechook = exechook_establish(procfs_revoke_vnodes, mp); 172 if (*data_len >= sizeof *args) 173 pmnt->pmnt_flags = args->flags; 174 else 175 pmnt->pmnt_flags = 0; 176 177 mp->mnt_iflag |= IMNT_MPSAFE; 178 return error; 179 } 180 181 /* 182 * unmount system call 183 */ 184 int 185 procfs_unmount(struct mount *mp, int mntflags) 186 { 187 int error; 188 int flags = 0; 189 190 if (mntflags & MNT_FORCE) 191 flags |= FORCECLOSE; 192 193 if ((error = vflush(mp, 0, flags)) != 0) 194 return (error); 195 196 exechook_disestablish(VFSTOPROC(mp)->pmnt_exechook); 197 198 kmem_free(mp->mnt_data, sizeof(struct procfsmount)); 199 mp->mnt_data = NULL; 200 201 return 0; 202 } 203 204 int 205 procfs_root(struct mount *mp, struct vnode **vpp) 206 { 207 int error; 208 209 error = procfs_allocvp(mp, vpp, 0, PFSroot, -1); 210 if (error == 0) { 211 error = vn_lock(*vpp, LK_EXCLUSIVE); 212 if (error != 0) { 213 vrele(*vpp); 214 *vpp = NULL; 215 } 216 } 217 218 return error; 219 } 220 221 /* ARGSUSED */ 222 int 223 procfs_start(struct mount *mp, int flags) 224 { 225 226 return (0); 227 } 228 229 /* 230 * Get file system statistics. 231 */ 232 int 233 procfs_statvfs(struct mount *mp, struct statvfs *sbp) 234 { 235 236 genfs_statvfs(mp, sbp); 237 238 sbp->f_bsize = PAGE_SIZE; 239 sbp->f_frsize = PAGE_SIZE; 240 sbp->f_iosize = PAGE_SIZE; 241 sbp->f_blocks = 1; 242 sbp->f_files = maxproc; /* approx */ 243 sbp->f_ffree = maxproc - nprocs; /* approx */ 244 sbp->f_favail = maxproc - nprocs; /* approx */ 245 246 return (0); 247 } 248 249 /*ARGSUSED*/ 250 int 251 procfs_sync( 252 struct mount *mp, 253 int waitfor, 254 kauth_cred_t uc) 255 { 256 257 return (0); 258 } 259 260 /*ARGSUSED*/ 261 int 262 procfs_vget(struct mount *mp, ino_t ino, 263 struct vnode **vpp) 264 { 265 return (EOPNOTSUPP); 266 } 267 268 int 269 procfs_loadvnode(struct mount *mp, struct vnode *vp, 270 const void *key, size_t key_len, const void **new_key) 271 { 272 int error; 273 struct pfskey pfskey; 274 struct pfsnode *pfs; 275 276 KASSERT(key_len == sizeof(pfskey)); 277 memcpy(&pfskey, key, key_len); 278 279 pfs = kmem_alloc(sizeof(*pfs), KM_SLEEP); 280 pfs->pfs_pid = pfskey.pk_pid; 281 pfs->pfs_type = pfskey.pk_type; 282 pfs->pfs_fd = pfskey.pk_fd; 283 pfs->pfs_vnode = vp; 284 pfs->pfs_flags = 0; 285 pfs->pfs_fileno = 286 PROCFS_FILENO(pfs->pfs_pid, pfs->pfs_type, pfs->pfs_fd); 287 vp->v_tag = VT_PROCFS; 288 vp->v_op = procfs_vnodeop_p; 289 vp->v_data = pfs; 290 291 switch (pfs->pfs_type) { 292 case PFSroot: /* /proc = dr-xr-xr-x */ 293 vp->v_vflag |= VV_ROOT; 294 /*FALLTHROUGH*/ 295 case PFSproc: /* /proc/N = dr-xr-xr-x */ 296 pfs->pfs_mode = S_IRUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH; 297 vp->v_type = VDIR; 298 break; 299 300 case PFStask: /* /proc/N/task = dr-xr-xr-x */ 301 if (pfs->pfs_fd == -1) { 302 pfs->pfs_mode = S_IRUSR|S_IXUSR|S_IRGRP|S_IXGRP| 303 S_IROTH|S_IXOTH; 304 vp->v_type = VDIR; 305 break; 306 } 307 /*FALLTHROUGH*/ 308 case PFScurproc: /* /proc/curproc = lr-xr-xr-x */ 309 case PFSself: /* /proc/self = lr-xr-xr-x */ 310 case PFScwd: /* /proc/N/cwd = lr-xr-xr-x */ 311 case PFSchroot: /* /proc/N/chroot = lr-xr-xr-x */ 312 case PFSexe: /* /proc/N/exe = lr-xr-xr-x */ 313 pfs->pfs_mode = S_IRUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH; 314 vp->v_type = VLNK; 315 break; 316 317 case PFSfd: 318 if (pfs->pfs_fd == -1) { /* /proc/N/fd = dr-x------ */ 319 pfs->pfs_mode = S_IRUSR|S_IXUSR; 320 vp->v_type = VDIR; 321 } else { /* /proc/N/fd/M = [ps-]rw------- */ 322 file_t *fp; 323 vnode_t *vxp; 324 struct proc *p; 325 326 mutex_enter(proc_lock); 327 p = proc_find(pfs->pfs_pid); 328 mutex_exit(proc_lock); 329 if (p == NULL) { 330 error = ENOENT; 331 goto bad; 332 } 333 KASSERT(rw_read_held(&p->p_reflock)); 334 if ((fp = fd_getfile2(p, pfs->pfs_fd)) == NULL) { 335 error = EBADF; 336 goto bad; 337 } 338 339 pfs->pfs_mode = S_IRUSR|S_IWUSR; 340 switch (fp->f_type) { 341 case DTYPE_VNODE: 342 vxp = fp->f_vnode; 343 344 /* 345 * We make symlinks for directories 346 * to avoid cycles. 347 */ 348 if (vxp->v_type == VDIR) 349 goto symlink; 350 vp->v_type = vxp->v_type; 351 break; 352 case DTYPE_PIPE: 353 vp->v_type = VFIFO; 354 break; 355 case DTYPE_SOCKET: 356 vp->v_type = VSOCK; 357 break; 358 case DTYPE_KQUEUE: 359 case DTYPE_MISC: 360 case DTYPE_SEM: 361 symlink: 362 pfs->pfs_mode = S_IRUSR|S_IXUSR|S_IRGRP| 363 S_IXGRP|S_IROTH|S_IXOTH; 364 vp->v_type = VLNK; 365 break; 366 default: 367 error = EOPNOTSUPP; 368 closef(fp); 369 goto bad; 370 } 371 closef(fp); 372 } 373 break; 374 375 case PFSfile: /* /proc/N/file = -rw------- */ 376 case PFSmem: /* /proc/N/mem = -rw------- */ 377 case PFSregs: /* /proc/N/regs = -rw------- */ 378 case PFSfpregs: /* /proc/N/fpregs = -rw------- */ 379 pfs->pfs_mode = S_IRUSR|S_IWUSR; 380 vp->v_type = VREG; 381 break; 382 383 case PFSctl: /* /proc/N/ctl = --w------ */ 384 case PFSnote: /* /proc/N/note = --w------ */ 385 case PFSnotepg: /* /proc/N/notepg = --w------ */ 386 pfs->pfs_mode = S_IWUSR; 387 vp->v_type = VREG; 388 break; 389 390 case PFSmap: /* /proc/N/map = -r--r--r-- */ 391 case PFSmaps: /* /proc/N/maps = -r--r--r-- */ 392 case PFSstatus: /* /proc/N/status = -r--r--r-- */ 393 case PFSstat: /* /proc/N/stat = -r--r--r-- */ 394 case PFScmdline: /* /proc/N/cmdline = -r--r--r-- */ 395 case PFSemul: /* /proc/N/emul = -r--r--r-- */ 396 case PFSmeminfo: /* /proc/meminfo = -r--r--r-- */ 397 case PFScpustat: /* /proc/stat = -r--r--r-- */ 398 case PFSdevices: /* /proc/devices = -r--r--r-- */ 399 case PFScpuinfo: /* /proc/cpuinfo = -r--r--r-- */ 400 case PFSuptime: /* /proc/uptime = -r--r--r-- */ 401 case PFSmounts: /* /proc/mounts = -r--r--r-- */ 402 case PFSloadavg: /* /proc/loadavg = -r--r--r-- */ 403 case PFSstatm: /* /proc/N/statm = -r--r--r-- */ 404 case PFSversion: /* /proc/version = -r--r--r-- */ 405 pfs->pfs_mode = S_IRUSR|S_IRGRP|S_IROTH; 406 vp->v_type = VREG; 407 break; 408 409 #ifdef __HAVE_PROCFS_MACHDEP 410 PROCFS_MACHDEP_NODETYPE_CASES 411 procfs_machdep_allocvp(vp); 412 break; 413 #endif 414 415 default: 416 panic("procfs_allocvp"); 417 } 418 419 uvm_vnp_setsize(vp, 0); 420 *new_key = &pfs->pfs_key; 421 422 return 0; 423 424 bad: 425 vp->v_tag =VT_NON; 426 vp->v_type = VNON; 427 vp->v_op = NULL; 428 vp->v_data = NULL; 429 kmem_free(pfs, sizeof(*pfs)); 430 return error; 431 } 432 433 void 434 procfs_init(void) 435 { 436 437 } 438 439 void 440 procfs_reinit(void) 441 { 442 443 } 444 445 void 446 procfs_done(void) 447 { 448 449 } 450 451 extern const struct vnodeopv_desc procfs_vnodeop_opv_desc; 452 453 const struct vnodeopv_desc * const procfs_vnodeopv_descs[] = { 454 &procfs_vnodeop_opv_desc, 455 NULL, 456 }; 457 458 struct vfsops procfs_vfsops = { 459 .vfs_name = MOUNT_PROCFS, 460 .vfs_min_mount_data = sizeof (struct procfs_args), 461 .vfs_mount = procfs_mount, 462 .vfs_start = procfs_start, 463 .vfs_unmount = procfs_unmount, 464 .vfs_root = procfs_root, 465 .vfs_quotactl = (void *)eopnotsupp, 466 .vfs_statvfs = procfs_statvfs, 467 .vfs_sync = procfs_sync, 468 .vfs_vget = procfs_vget, 469 .vfs_loadvnode = procfs_loadvnode, 470 .vfs_fhtovp = (void *)eopnotsupp, 471 .vfs_vptofh = (void *)eopnotsupp, 472 .vfs_init = procfs_init, 473 .vfs_reinit = procfs_reinit, 474 .vfs_done = procfs_done, 475 .vfs_snapshot = (void *)eopnotsupp, 476 .vfs_extattrctl = vfs_stdextattrctl, 477 .vfs_suspendctl = (void *)eopnotsupp, 478 .vfs_renamelock_enter = genfs_renamelock_enter, 479 .vfs_renamelock_exit = genfs_renamelock_exit, 480 .vfs_fsync = (void *)eopnotsupp, 481 .vfs_opv_descs = procfs_vnodeopv_descs 482 }; 483 484 static int 485 procfs_listener_cb(kauth_cred_t cred, kauth_action_t action, void *cookie, 486 void *arg0, void *arg1, void *arg2, void *arg3) 487 { 488 struct proc *p; 489 struct pfsnode *pfs; 490 enum kauth_process_req req; 491 int result; 492 493 result = KAUTH_RESULT_DEFER; 494 p = arg0; 495 pfs = arg1; 496 req = (enum kauth_process_req)(unsigned long)arg2; 497 498 if (action != KAUTH_PROCESS_PROCFS) 499 return result; 500 501 /* Privileged; let secmodel handle that. */ 502 if (req == KAUTH_REQ_PROCESS_PROCFS_CTL) 503 return result; 504 505 switch (pfs->pfs_type) { 506 case PFSregs: 507 case PFSfpregs: 508 case PFSmem: 509 if (kauth_cred_getuid(cred) != kauth_cred_getuid(p->p_cred) || 510 ISSET(p->p_flag, PK_SUGID)) 511 break; 512 513 /*FALLTHROUGH*/ 514 default: 515 result = KAUTH_RESULT_ALLOW; 516 break; 517 } 518 519 return result; 520 } 521 522 523 static int 524 procfs_modcmd(modcmd_t cmd, void *arg) 525 { 526 int error; 527 528 switch (cmd) { 529 case MODULE_CMD_INIT: 530 error = vfs_attach(&procfs_vfsops); 531 if (error != 0) 532 break; 533 sysctl_createv(&procfs_sysctl_log, 0, NULL, NULL, 534 CTLFLAG_PERMANENT, 535 CTLTYPE_NODE, "procfs", 536 SYSCTL_DESCR("Process file system"), 537 NULL, 0, NULL, 0, 538 CTL_VFS, 12, CTL_EOL); 539 /* 540 * XXX the "12" above could be dynamic, thereby eliminating 541 * one more instance of the "number to vfs" mapping problem, 542 * but "12" is the order as taken from sys/mount.h 543 */ 544 545 procfs_listener = kauth_listen_scope(KAUTH_SCOPE_PROCESS, 546 procfs_listener_cb, NULL); 547 548 break; 549 case MODULE_CMD_FINI: 550 error = vfs_detach(&procfs_vfsops); 551 if (error != 0) 552 break; 553 sysctl_teardown(&procfs_sysctl_log); 554 kauth_unlisten_scope(procfs_listener); 555 break; 556 default: 557 error = ENOTTY; 558 break; 559 } 560 561 return (error); 562 } 563