Home | History | Annotate | Line # | Download | only in procfs
procfs_vfsops.c revision 1.103
      1 /*	$NetBSD: procfs_vfsops.c,v 1.103 2020/03/16 21:20:11 pgoyette 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.103 2020/03/16 21:20:11 pgoyette 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/file.h>
     98 #include <sys/filedesc.h>
     99 #include <sys/kauth.h>
    100 #include <sys/module.h>
    101 
    102 #include <miscfs/genfs/genfs.h>
    103 
    104 #include <miscfs/procfs/procfs.h>
    105 
    106 #include <uvm/uvm_extern.h>			/* for PAGE_SIZE */
    107 
    108 MODULE(MODULE_CLASS_VFS, procfs, "ptrace_common");
    109 
    110 VFS_PROTOS(procfs);
    111 
    112 static kauth_listener_t procfs_listener;
    113 
    114 /*
    115  * VFS Operations.
    116  *
    117  * mount system call
    118  */
    119 /* ARGSUSED */
    120 int
    121 procfs_mount(
    122     struct mount *mp,
    123     const char *path,
    124     void *data,
    125     size_t *data_len)
    126 {
    127 	struct lwp *l = curlwp;
    128 	struct procfsmount *pmnt;
    129 	struct procfs_args *args = data;
    130 	int error;
    131 
    132 	if (args == NULL)
    133 		return EINVAL;
    134 
    135 	if (UIO_MX & (UIO_MX-1)) {
    136 		log(LOG_ERR, "procfs: invalid directory entry size");
    137 		return (EINVAL);
    138 	}
    139 
    140 	if (mp->mnt_flag & MNT_GETARGS) {
    141 		if (*data_len < sizeof *args)
    142 			return EINVAL;
    143 
    144 		pmnt = VFSTOPROC(mp);
    145 		if (pmnt == NULL)
    146 			return EIO;
    147 		args->version = PROCFS_ARGSVERSION;
    148 		args->flags = pmnt->pmnt_flags;
    149 		*data_len = sizeof *args;
    150 		return 0;
    151 	}
    152 
    153 	if (mp->mnt_flag & MNT_UPDATE)
    154 		return (EOPNOTSUPP);
    155 
    156 	if (*data_len >= sizeof *args && args->version != PROCFS_ARGSVERSION)
    157 		return EINVAL;
    158 
    159 	pmnt = kmem_zalloc(sizeof(struct procfsmount), KM_SLEEP);
    160 
    161 	mp->mnt_stat.f_namemax = PROCFS_MAXNAMLEN;
    162 	mp->mnt_flag |= MNT_LOCAL;
    163 	mp->mnt_data = pmnt;
    164 	vfs_getnewfsid(mp);
    165 
    166 	error = set_statvfs_info(path, UIO_USERSPACE, "procfs", UIO_SYSSPACE,
    167 	    mp->mnt_op->vfs_name, mp, l);
    168 	pmnt->pmnt_exechook = exechook_establish(procfs_revoke_vnodes, mp);
    169 	if (*data_len >= sizeof *args)
    170 		pmnt->pmnt_flags = args->flags;
    171 	else
    172 		pmnt->pmnt_flags = 0;
    173 
    174 	mp->mnt_iflag |= IMNT_MPSAFE;
    175 	return error;
    176 }
    177 
    178 /*
    179  * unmount system call
    180  */
    181 int
    182 procfs_unmount(struct mount *mp, int mntflags)
    183 {
    184 	int error;
    185 	int flags = 0;
    186 
    187 	if (mntflags & MNT_FORCE)
    188 		flags |= FORCECLOSE;
    189 
    190 	if ((error = vflush(mp, 0, flags)) != 0)
    191 		return (error);
    192 
    193 	exechook_disestablish(VFSTOPROC(mp)->pmnt_exechook);
    194 
    195 	kmem_free(mp->mnt_data, sizeof(struct procfsmount));
    196 	mp->mnt_data = NULL;
    197 
    198 	return 0;
    199 }
    200 
    201 int
    202 procfs_root(struct mount *mp, int lktype, struct vnode **vpp)
    203 {
    204 	int error;
    205 
    206 	error = procfs_allocvp(mp, vpp, 0, PFSroot, -1);
    207 	if (error == 0) {
    208 		error = vn_lock(*vpp, lktype);
    209 		if (error != 0) {
    210 			vrele(*vpp);
    211 			*vpp = NULL;
    212 		}
    213 	}
    214 
    215 	return error;
    216 }
    217 
    218 /* ARGSUSED */
    219 int
    220 procfs_start(struct mount *mp, int flags)
    221 {
    222 
    223 	return (0);
    224 }
    225 
    226 /*
    227  * Get file system statistics.
    228  */
    229 int
    230 procfs_statvfs(struct mount *mp, struct statvfs *sbp)
    231 {
    232 
    233 	genfs_statvfs(mp, sbp);
    234 
    235 	sbp->f_bsize = PAGE_SIZE;
    236 	sbp->f_frsize = PAGE_SIZE;
    237 	sbp->f_iosize = PAGE_SIZE;
    238 	sbp->f_blocks = 1;
    239 	sbp->f_files = maxproc;			/* approx */
    240 	sbp->f_ffree = maxproc - nprocs;	/* approx */
    241 	sbp->f_favail = maxproc - nprocs;	/* approx */
    242 
    243 	return (0);
    244 }
    245 
    246 /*ARGSUSED*/
    247 int
    248 procfs_sync(
    249     struct mount *mp,
    250     int waitfor,
    251     kauth_cred_t uc)
    252 {
    253 
    254 	return (0);
    255 }
    256 
    257 /*ARGSUSED*/
    258 int
    259 procfs_vget(struct mount *mp, ino_t ino, int lktype,
    260     struct vnode **vpp)
    261 {
    262 	return (EOPNOTSUPP);
    263 }
    264 
    265 int
    266 procfs_loadvnode(struct mount *mp, struct vnode *vp,
    267     const void *key, size_t key_len, const void **new_key)
    268 {
    269 	int error;
    270 	struct pfskey pfskey;
    271 	struct pfsnode *pfs;
    272 
    273 	KASSERT(key_len == sizeof(pfskey));
    274 	memcpy(&pfskey, key, key_len);
    275 
    276 	pfs = kmem_alloc(sizeof(*pfs), KM_SLEEP);
    277 	pfs->pfs_pid = pfskey.pk_pid;
    278 	pfs->pfs_type = pfskey.pk_type;
    279 	pfs->pfs_fd = pfskey.pk_fd;
    280 	pfs->pfs_vnode = vp;
    281 	pfs->pfs_flags = 0;
    282 	pfs->pfs_fileno =
    283 	    PROCFS_FILENO(pfs->pfs_pid, pfs->pfs_type, pfs->pfs_fd);
    284 	vp->v_tag = VT_PROCFS;
    285 	vp->v_op = procfs_vnodeop_p;
    286 	vp->v_data = pfs;
    287 
    288 	switch (pfs->pfs_type) {
    289 	case PFSroot:	/* /proc = dr-xr-xr-x */
    290 		vp->v_vflag |= VV_ROOT;
    291 		/*FALLTHROUGH*/
    292 	case PFSproc:	/* /proc/N = dr-xr-xr-x */
    293 		pfs->pfs_mode = S_IRUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH;
    294 		vp->v_type = VDIR;
    295 		break;
    296 
    297 	case PFStask:	/* /proc/N/task = dr-xr-xr-x */
    298 		if (pfs->pfs_fd == -1) {
    299 			pfs->pfs_mode = S_IRUSR|S_IXUSR|S_IRGRP|S_IXGRP|
    300 			    S_IROTH|S_IXOTH;
    301 			vp->v_type = VDIR;
    302 			break;
    303 		}
    304 		/*FALLTHROUGH*/
    305 	case PFScurproc:	/* /proc/curproc = lr-xr-xr-x */
    306 	case PFSself:	/* /proc/self    = lr-xr-xr-x */
    307 	case PFScwd:	/* /proc/N/cwd = lr-xr-xr-x */
    308 	case PFSchroot:	/* /proc/N/chroot = lr-xr-xr-x */
    309 	case PFSexe:	/* /proc/N/exe = lr-xr-xr-x */
    310 		pfs->pfs_mode = S_IRUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH;
    311 		vp->v_type = VLNK;
    312 		break;
    313 
    314 	case PFSfd:
    315 		if (pfs->pfs_fd == -1) {	/* /proc/N/fd = dr-x------ */
    316 			pfs->pfs_mode = S_IRUSR|S_IXUSR;
    317 			vp->v_type = VDIR;
    318 		} else {	/* /proc/N/fd/M = [ps-]rw------- */
    319 			file_t *fp;
    320 			vnode_t *vxp;
    321 			struct proc *p;
    322 
    323 			mutex_enter(proc_lock);
    324 			p = proc_find(pfs->pfs_pid);
    325 			mutex_exit(proc_lock);
    326 			if (p == NULL) {
    327 				error = ENOENT;
    328 				goto bad;
    329 			}
    330 			KASSERT(rw_read_held(&p->p_reflock));
    331 			if ((fp = fd_getfile2(p, pfs->pfs_fd)) == NULL) {
    332 				error = EBADF;
    333 				goto bad;
    334 			}
    335 
    336 			pfs->pfs_mode = S_IRUSR|S_IWUSR;
    337 			switch (fp->f_type) {
    338 			case DTYPE_VNODE:
    339 				vxp = fp->f_vnode;
    340 
    341 				/*
    342 				 * We make symlinks for directories
    343 				 * to avoid cycles.
    344 				 */
    345 				if (vxp->v_type == VDIR)
    346 					goto symlink;
    347 				vp->v_type = vxp->v_type;
    348 				break;
    349 			case DTYPE_PIPE:
    350 				vp->v_type = VFIFO;
    351 				break;
    352 			case DTYPE_SOCKET:
    353 				vp->v_type = VSOCK;
    354 				break;
    355 			case DTYPE_KQUEUE:
    356 			case DTYPE_MISC:
    357 			case DTYPE_SEM:
    358 			symlink:
    359 				pfs->pfs_mode = S_IRUSR|S_IXUSR|S_IRGRP|
    360 				    S_IXGRP|S_IROTH|S_IXOTH;
    361 				vp->v_type = VLNK;
    362 				break;
    363 			default:
    364 				error = EOPNOTSUPP;
    365 				closef(fp);
    366 				goto bad;
    367 			}
    368 			closef(fp);
    369 		}
    370 		break;
    371 
    372 	case PFSfile:	/* /proc/N/file = -rw------- */
    373 	case PFSmem:	/* /proc/N/mem = -rw------- */
    374 	case PFSregs:	/* /proc/N/regs = -rw------- */
    375 	case PFSfpregs:	/* /proc/N/fpregs = -rw------- */
    376 		pfs->pfs_mode = S_IRUSR|S_IWUSR;
    377 		vp->v_type = VREG;
    378 		break;
    379 
    380 	case PFSnote:	/* /proc/N/note = --w------ */
    381 	case PFSnotepg:	/* /proc/N/notepg = --w------ */
    382 		pfs->pfs_mode = S_IWUSR;
    383 		vp->v_type = VREG;
    384 		break;
    385 
    386 	case PFSmap:		/* /proc/N/map = -r-------- */
    387 	case PFSmaps:		/* /proc/N/maps = -r-------- */
    388 	case PFSauxv:		/* /proc/N/auxv = -r-------- */
    389 		pfs->pfs_mode = S_IRUSR;
    390 		vp->v_type = VREG;
    391 		break;
    392 
    393 	case PFSstatus:		/* /proc/N/status = -r--r--r-- */
    394 	case PFSstat:		/* /proc/N/stat = -r--r--r-- */
    395 	case PFScmdline:	/* /proc/N/cmdline = -r--r--r-- */
    396 	case PFSenviron:	/* /proc/N/environ = -r--r--r-- */
    397 	case PFSemul:		/* /proc/N/emul = -r--r--r-- */
    398 	case PFSmeminfo:	/* /proc/meminfo = -r--r--r-- */
    399 	case PFScpustat:	/* /proc/stat = -r--r--r-- */
    400 	case PFSdevices:	/* /proc/devices = -r--r--r-- */
    401 	case PFScpuinfo:	/* /proc/cpuinfo = -r--r--r-- */
    402 	case PFSuptime:		/* /proc/uptime = -r--r--r-- */
    403 	case PFSmounts:		/* /proc/mounts = -r--r--r-- */
    404 	case PFSloadavg:	/* /proc/loadavg = -r--r--r-- */
    405 	case PFSstatm:		/* /proc/N/statm = -r--r--r-- */
    406 	case PFSversion:	/* /proc/version = -r--r--r-- */
    407 	case PFSlimit:		/* /proc/limit = -r--r--r-- */
    408 		pfs->pfs_mode = S_IRUSR|S_IRGRP|S_IROTH;
    409 		vp->v_type = VREG;
    410 		break;
    411 
    412 #ifdef __HAVE_PROCFS_MACHDEP
    413 	PROCFS_MACHDEP_NODETYPE_CASES
    414 		procfs_machdep_allocvp(vp);
    415 		break;
    416 #endif
    417 
    418 	default:
    419 		panic("procfs_allocvp");
    420 	}
    421 
    422 	uvm_vnp_setsize(vp, 0);
    423 	*new_key = &pfs->pfs_key;
    424 
    425 	return 0;
    426 
    427 bad:
    428 	vp->v_tag =VT_NON;
    429 	vp->v_type = VNON;
    430 	vp->v_op = NULL;
    431 	vp->v_data = NULL;
    432 	kmem_free(pfs, sizeof(*pfs));
    433 	return error;
    434 }
    435 
    436 void
    437 procfs_init(void)
    438 {
    439 
    440 }
    441 
    442 void
    443 procfs_reinit(void)
    444 {
    445 
    446 }
    447 
    448 void
    449 procfs_done(void)
    450 {
    451 
    452 }
    453 
    454 extern const struct vnodeopv_desc procfs_vnodeop_opv_desc;
    455 
    456 const struct vnodeopv_desc * const procfs_vnodeopv_descs[] = {
    457 	&procfs_vnodeop_opv_desc,
    458 	NULL,
    459 };
    460 
    461 struct vfsops procfs_vfsops = {
    462 	.vfs_name = MOUNT_PROCFS,
    463 	.vfs_min_mount_data = sizeof (struct procfs_args),
    464 	.vfs_mount = procfs_mount,
    465 	.vfs_start = procfs_start,
    466 	.vfs_unmount = procfs_unmount,
    467 	.vfs_root = procfs_root,
    468 	.vfs_quotactl = (void *)eopnotsupp,
    469 	.vfs_statvfs = procfs_statvfs,
    470 	.vfs_sync = procfs_sync,
    471 	.vfs_vget = procfs_vget,
    472 	.vfs_loadvnode = procfs_loadvnode,
    473 	.vfs_fhtovp = (void *)eopnotsupp,
    474 	.vfs_vptofh = (void *)eopnotsupp,
    475 	.vfs_init = procfs_init,
    476 	.vfs_reinit = procfs_reinit,
    477 	.vfs_done = procfs_done,
    478 	.vfs_snapshot = (void *)eopnotsupp,
    479 	.vfs_extattrctl = vfs_stdextattrctl,
    480 	.vfs_suspendctl = genfs_suspendctl,
    481 	.vfs_renamelock_enter = genfs_renamelock_enter,
    482 	.vfs_renamelock_exit = genfs_renamelock_exit,
    483 	.vfs_fsync = (void *)eopnotsupp,
    484 	.vfs_opv_descs = procfs_vnodeopv_descs
    485 };
    486 
    487 static int
    488 procfs_listener_cb(kauth_cred_t cred, kauth_action_t action, void *cookie,
    489     void *arg0, void *arg1, void *arg2, void *arg3)
    490 {
    491 	struct proc *p;
    492 	struct pfsnode *pfs;
    493 	int result;
    494 
    495 	result = KAUTH_RESULT_DEFER;
    496 	p = arg0;
    497 	pfs = arg1;
    498 
    499 	if (action != KAUTH_PROCESS_PROCFS)
    500 		return result;
    501 
    502 	switch (pfs->pfs_type) {
    503 	case PFSregs:
    504 	case PFSfpregs:
    505 	case PFSmem:
    506 		if (kauth_cred_getuid(cred) != kauth_cred_getuid(p->p_cred) ||
    507 		    ISSET(p->p_flag, PK_SUGID))
    508 			break;
    509 
    510 		/*FALLTHROUGH*/
    511 	default:
    512 		result = KAUTH_RESULT_ALLOW;
    513 		break;
    514 	}
    515 
    516 	return result;
    517 }
    518 
    519 SYSCTL_SETUP(procfs_sysctl_setup, "procfs sysctl")
    520 {
    521 
    522 	sysctl_createv(clog, 0, NULL, NULL,
    523 		       CTLFLAG_PERMANENT,
    524 		       CTLTYPE_NODE, "procfs",
    525 		       SYSCTL_DESCR("Process file system"),
    526 		       NULL, 0, NULL, 0,
    527 		       CTL_VFS, 12, CTL_EOL);
    528 	/*
    529 	 * XXX the "12" above could be dynamic, thereby eliminating
    530 	 * one more instance of the "number to vfs" mapping problem,
    531 	 * but "12" is the order as taken from sys/mount.h
    532 	 */
    533 }
    534 
    535 static int
    536 procfs_modcmd(modcmd_t cmd, void *arg)
    537 {
    538 	int error;
    539 
    540 	switch (cmd) {
    541 	case MODULE_CMD_INIT:
    542 		error = vfs_attach(&procfs_vfsops);
    543 		if (error != 0)
    544 			break;
    545 
    546 		procfs_listener = kauth_listen_scope(KAUTH_SCOPE_PROCESS,
    547 		    procfs_listener_cb, NULL);
    548 
    549 		break;
    550 	case MODULE_CMD_FINI:
    551 		error = vfs_detach(&procfs_vfsops);
    552 		if (error != 0)
    553 			break;
    554 		kauth_unlisten_scope(procfs_listener);
    555 		break;
    556 	default:
    557 		error = ENOTTY;
    558 		break;
    559 	}
    560 
    561 	return (error);
    562 }
    563