Home | History | Annotate | Line # | Download | only in ptyfs
ptyfs_vfsops.c revision 1.52
      1 /*	$NetBSD: ptyfs_vfsops.c,v 1.52 2014/08/14 14:06:53 maxv Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1992, 1993, 1995
      5  *	The Regents of the University of California.  All rights reserved.
      6  *
      7  * This code is derived from software donated 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  */
     35 
     36 /*
     37  * Pseudo-tty Filesystem
     38  */
     39 
     40 #include <sys/cdefs.h>
     41 __KERNEL_RCSID(0, "$NetBSD: ptyfs_vfsops.c,v 1.52 2014/08/14 14:06:53 maxv Exp $");
     42 
     43 #include <sys/param.h>
     44 #include <sys/systm.h>
     45 #include <sys/sysctl.h>
     46 #include <sys/conf.h>
     47 #include <sys/proc.h>
     48 #include <sys/vnode.h>
     49 #include <sys/mount.h>
     50 #include <sys/namei.h>
     51 #include <sys/stat.h>
     52 #include <sys/dirent.h>
     53 #include <sys/malloc.h>
     54 #include <sys/syslog.h>
     55 #include <sys/select.h>
     56 #include <sys/filedesc.h>
     57 #include <sys/tty.h>
     58 #include <sys/pty.h>
     59 #include <sys/kauth.h>
     60 #include <sys/module.h>
     61 
     62 #include <fs/ptyfs/ptyfs.h>
     63 #include <miscfs/genfs/genfs.h>
     64 #include <miscfs/specfs/specdev.h>
     65 
     66 MODULE(MODULE_CLASS_VFS, ptyfs, NULL);
     67 
     68 MALLOC_JUSTDEFINE(M_PTYFSMNT, "ptyfs mount", "ptyfs mount structures");
     69 MALLOC_JUSTDEFINE(M_PTYFSTMP, "ptyfs temp", "ptyfs temporary structures");
     70 
     71 VFS_PROTOS(ptyfs);
     72 
     73 static struct sysctllog *ptyfs_sysctl_log;
     74 
     75 static int ptyfs__allocvp(struct mount *, struct lwp *, struct vnode **,
     76     dev_t, char);
     77 static int ptyfs__makename(struct mount *, struct lwp *, char *, size_t,
     78     dev_t, char);
     79 static void ptyfs__getvattr(struct mount *, struct lwp *, struct vattr *);
     80 static int ptyfs__getmp(struct lwp *, struct mount **);
     81 
     82 /*
     83  * ptm glue: When we mount, we make ptm point to us.
     84  */
     85 struct ptm_pty *ptyfs_save_ptm;
     86 static int ptyfs_count;
     87 
     88 static TAILQ_HEAD(, ptyfsmount) ptyfs_head;
     89 
     90 struct ptm_pty ptm_ptyfspty = {
     91 	ptyfs__allocvp,
     92 	ptyfs__makename,
     93 	ptyfs__getvattr,
     94 	ptyfs__getmp,
     95 };
     96 
     97 static int
     98 ptyfs__getmp(struct lwp *l, struct mount **mpp)
     99 {
    100  	struct cwdinfo *cwdi = l->l_proc->p_cwdi;
    101  	struct mount *mp;
    102 	struct ptyfsmount *pmnt;
    103 
    104 	TAILQ_FOREACH(pmnt, &ptyfs_head, pmnt_le) {
    105 		mp = pmnt->pmnt_mp;
    106 		if (cwdi->cwdi_rdir == NULL)
    107 			goto ok;
    108 
    109 		if (vn_isunder(mp->mnt_vnodecovered, cwdi->cwdi_rdir, l))
    110 			goto ok;
    111 	}
    112  	*mpp = NULL;
    113  	return EOPNOTSUPP;
    114 ok:
    115 	*mpp = mp;
    116 	return 0;
    117 }
    118 
    119 static const char *
    120 ptyfs__getpath(struct lwp *l, const struct mount *mp)
    121 {
    122 #define MAXBUF (sizeof(mp->mnt_stat.f_mntonname) + 32)
    123 	struct cwdinfo *cwdi = l->l_proc->p_cwdi;
    124 	char *buf;
    125 	const char *rv;
    126 	size_t len;
    127 	char *bp;
    128 	int error;
    129 
    130 	rv = mp->mnt_stat.f_mntonname;
    131 	if (cwdi->cwdi_rdir == NULL)
    132 		return rv;
    133 
    134 	buf = malloc(MAXBUF, M_TEMP, M_WAITOK);
    135 	bp = buf + MAXBUF;
    136 	*--bp = '\0';
    137 	error = getcwd_common(mp->mnt_vnodecovered, cwdi->cwdi_rdir, &bp,
    138 	    buf, MAXBUF / 2, 0, l);
    139 	if (error) {	/* Mount point is out of rdir */
    140 		rv = NULL;
    141 		goto out;
    142 	}
    143 
    144 	len = strlen(bp);
    145 	if (len < sizeof(mp->mnt_stat.f_mntonname))	/* XXX */
    146 		rv += strlen(rv) - len;
    147 out:
    148 	free(buf, M_TEMP);
    149 	return rv;
    150 }
    151 
    152 static int
    153 ptyfs__makename(struct mount *mp, struct lwp *l, char *tbuf, size_t bufsiz,
    154     dev_t dev, char ms)
    155 {
    156 	size_t len;
    157 	const char *np;
    158 	int pty = minor(dev);
    159 
    160 	switch (ms) {
    161 	case 'p':
    162 		/* We don't provide access to the master, should we? */
    163 		len = snprintf(tbuf, bufsiz, "/dev/null");
    164 		break;
    165 	case 't':
    166 		/*
    167 		 * We support traditional ptys, so we can get here,
    168 		 * if pty had been opened before PTYFS was mounted,
    169 		 * or was opened through /dev/ptyXX devices.
    170 		 * Return it only outside chroot for more security .
    171 		 */
    172 		if (l->l_proc->p_cwdi->cwdi_rdir == NULL
    173 		    && ptyfs_save_ptm != NULL
    174 		    && ptyfs_next_active(mp, pty) != pty)
    175 			return (*ptyfs_save_ptm->makename)(mp, l,
    176 			    tbuf, bufsiz, dev, ms);
    177 
    178 		np = ptyfs__getpath(l, mp);
    179 		if (np == NULL)
    180 			return EOPNOTSUPP;
    181 		len = snprintf(tbuf, bufsiz, "%s/%llu", np,
    182 			(unsigned long long)minor(dev));
    183 		break;
    184 	default:
    185 		return EINVAL;
    186 	}
    187 
    188 	return len >= bufsiz ? ENOSPC : 0;
    189 }
    190 
    191 
    192 static int
    193 /*ARGSUSED*/
    194 ptyfs__allocvp(struct mount *mp, struct lwp *l, struct vnode **vpp,
    195     dev_t dev, char ms)
    196 {
    197 	int error;
    198 	ptyfstype type;
    199 
    200 	switch (ms) {
    201 	case 'p':
    202 		type = PTYFSptc;
    203 		break;
    204 	case 't':
    205 		type = PTYFSpts;
    206 		break;
    207 	default:
    208 		return EINVAL;
    209 	}
    210 
    211 	error = ptyfs_allocvp(mp, vpp, type, minor(dev));
    212 	if (error == 0 && type == PTYFSptc)
    213 		ptyfs_set_active(mp, minor(dev));
    214 	return error;
    215 }
    216 
    217 
    218 static void
    219 ptyfs__getvattr(struct mount *mp, struct lwp *l, struct vattr *vattr)
    220 {
    221 	struct ptyfsmount *pmnt = VFSTOPTY(mp);
    222 	vattr_null(vattr);
    223 	/* get real uid */
    224 	vattr->va_uid = kauth_cred_getuid(l->l_cred);
    225 	vattr->va_gid = pmnt->pmnt_gid;
    226 	vattr->va_mode = pmnt->pmnt_mode;
    227 }
    228 
    229 
    230 void
    231 ptyfs_init(void)
    232 {
    233 
    234 	TAILQ_INIT(&ptyfs_head);
    235 	malloc_type_attach(M_PTYFSMNT);
    236 	malloc_type_attach(M_PTYFSTMP);
    237 	ptyfs_hashinit();
    238 }
    239 
    240 void
    241 ptyfs_reinit(void)
    242 {
    243 	ptyfs_hashreinit();
    244 }
    245 
    246 void
    247 ptyfs_done(void)
    248 {
    249 
    250 	ptyfs_hashdone();
    251 	malloc_type_detach(M_PTYFSTMP);
    252 	malloc_type_detach(M_PTYFSMNT);
    253 }
    254 
    255 #define OSIZE sizeof(struct { int f; gid_t g; mode_t m; })
    256 /*
    257  * Mount the Pseudo tty params filesystem
    258  */
    259 int
    260 ptyfs_mount(struct mount *mp, const char *path, void *data, size_t *data_len)
    261 {
    262 	struct lwp *l = curlwp;
    263 	int error = 0;
    264 	struct ptyfsmount *pmnt;
    265 	struct ptyfs_args *args = data;
    266 
    267 	if (args == NULL)
    268 		return EINVAL;
    269 	if (*data_len != sizeof *args) {
    270 		if (*data_len != OSIZE || args->version >= PTYFS_ARGSVERSION)
    271 			return EINVAL;
    272 	}
    273 
    274 	if (UIO_MX & (UIO_MX - 1)) {
    275 		log(LOG_ERR, "ptyfs: invalid directory entry size");
    276 		return EINVAL;
    277 	}
    278 
    279 	if (mp->mnt_flag & MNT_GETARGS) {
    280 		pmnt = VFSTOPTY(mp);
    281 		if (pmnt == NULL)
    282 			return EIO;
    283 		args->mode = pmnt->pmnt_mode;
    284 		args->gid = pmnt->pmnt_gid;
    285 		if (args->version >= PTYFS_ARGSVERSION) {
    286 			args->flags = pmnt->pmnt_flags;
    287 			*data_len = sizeof *args;
    288 		} else {
    289 			*data_len = OSIZE;
    290 		}
    291 		return 0;
    292 	}
    293 
    294 #if 0
    295 	/* Don't allow more than one mount */
    296 	if (ptyfs_count)
    297 		return EBUSY;
    298 #endif
    299 
    300 	if (mp->mnt_flag & MNT_UPDATE)
    301 		return EOPNOTSUPP;
    302 
    303 	if (args->version > PTYFS_ARGSVERSION)
    304 		return EINVAL;
    305 
    306 	pmnt = malloc(sizeof(struct ptyfsmount), M_PTYFSMNT, M_WAITOK);
    307 
    308 	mp->mnt_data = pmnt;
    309 	mutex_init(&pmnt->pmnt_lock, MUTEX_DEFAULT, IPL_NONE);
    310 	pmnt->pmnt_gid = args->gid;
    311 	pmnt->pmnt_mode = args->mode;
    312 	if (args->version >= PTYFS_ARGSVERSION)
    313 		pmnt->pmnt_flags = args->flags;
    314 	else
    315 		pmnt->pmnt_flags = 0;
    316 	pmnt->pmnt_bitmap_size = 0;
    317 	pmnt->pmnt_bitmap = NULL;
    318 	mp->mnt_flag |= MNT_LOCAL;
    319 	vfs_getnewfsid(mp);
    320 
    321 	if ((error = set_statvfs_info(path, UIO_USERSPACE, "ptyfs",
    322 	    UIO_SYSSPACE, mp->mnt_op->vfs_name, mp, l)) != 0) {
    323 		free(pmnt, M_PTYFSMNT);
    324 		return error;
    325 	}
    326 
    327 	pmnt->pmnt_mp = mp;
    328 	TAILQ_INSERT_TAIL(&ptyfs_head, pmnt, pmnt_le);
    329 	if (ptyfs_count++ == 0) {
    330 		/* Point pty access to us */
    331 		ptyfs_save_ptm = pty_sethandler(&ptm_ptyfspty);
    332 	}
    333 	return 0;
    334 }
    335 
    336 /*ARGSUSED*/
    337 int
    338 ptyfs_start(struct mount *mp, int flags)
    339 {
    340 	return 0;
    341 }
    342 
    343 /*ARGSUSED*/
    344 int
    345 ptyfs_unmount(struct mount *mp, int mntflags)
    346 {
    347 	int error;
    348 	int flags = 0;
    349 	struct ptyfsmount *pmnt;
    350 
    351 	if (mntflags & MNT_FORCE)
    352 		flags |= FORCECLOSE;
    353 
    354 	if ((error = vflush(mp, 0, flags)) != 0)
    355 		return error;
    356 
    357 	ptyfs_count--;
    358 	if (ptyfs_count == 0) {
    359 		/* Restore where pty access was pointing */
    360 		(void)pty_sethandler(ptyfs_save_ptm);
    361 		ptyfs_save_ptm = NULL;
    362 	}
    363 	TAILQ_FOREACH(pmnt, &ptyfs_head, pmnt_le) {
    364 		if (pmnt->pmnt_mp == mp) {
    365 			TAILQ_REMOVE(&ptyfs_head, pmnt, pmnt_le);
    366 			break;
    367 		}
    368  	}
    369 
    370 	/*
    371 	 * Finally, throw away the ptyfsmount structure
    372 	 */
    373 	if (pmnt->pmnt_bitmap_size > 0)
    374 		kmem_free(pmnt->pmnt_bitmap, pmnt->pmnt_bitmap_size);
    375 	mutex_destroy(&pmnt->pmnt_lock);
    376 	free(mp->mnt_data, M_PTYFSMNT);
    377 	mp->mnt_data = NULL;
    378 
    379 	return 0;
    380 }
    381 
    382 int
    383 ptyfs_root(struct mount *mp, struct vnode **vpp)
    384 {
    385 	/* setup "." */
    386 	return ptyfs_allocvp(mp, vpp, PTYFSroot, 0);
    387 }
    388 
    389 /*ARGSUSED*/
    390 int
    391 ptyfs_sync(struct mount *mp, int waitfor,
    392     kauth_cred_t uc)
    393 {
    394 	return 0;
    395 }
    396 
    397 /*
    398  * Kernfs flat namespace lookup.
    399  * Currently unsupported.
    400  */
    401 /*ARGSUSED*/
    402 int
    403 ptyfs_vget(struct mount *mp, ino_t ino,
    404     struct vnode **vpp)
    405 {
    406 	return EOPNOTSUPP;
    407 }
    408 
    409 extern const struct vnodeopv_desc ptyfs_vnodeop_opv_desc;
    410 
    411 const struct vnodeopv_desc * const ptyfs_vnodeopv_descs[] = {
    412 	&ptyfs_vnodeop_opv_desc,
    413 	NULL,
    414 };
    415 
    416 struct vfsops ptyfs_vfsops = {
    417 	.vfs_name = MOUNT_PTYFS,
    418 	.vfs_min_mount_data = sizeof (struct ptyfs_args),
    419 	.vfs_mount = ptyfs_mount,
    420 	.vfs_start = ptyfs_start,
    421 	.vfs_unmount = ptyfs_unmount,
    422 	.vfs_root = ptyfs_root,
    423 	.vfs_quotactl = (void *)eopnotsupp,
    424 	.vfs_statvfs = genfs_statvfs,
    425 	.vfs_sync = ptyfs_sync,
    426 	.vfs_vget = ptyfs_vget,
    427 	.vfs_fhtovp = (void *)eopnotsupp,
    428 	.vfs_vptofh = (void *)eopnotsupp,
    429 	.vfs_init = ptyfs_init,
    430 	.vfs_reinit = ptyfs_reinit,
    431 	.vfs_done = ptyfs_done,
    432 	.vfs_snapshot = (void *)eopnotsupp,
    433 	.vfs_extattrctl = (void *)eopnotsupp,
    434 	.vfs_suspendctl = (void *)eopnotsupp,
    435 	.vfs_renamelock_enter = genfs_renamelock_enter,
    436 	.vfs_renamelock_exit = genfs_renamelock_exit,
    437 	.vfs_fsync = (void *)eopnotsupp,
    438 	.vfs_opv_descs = ptyfs_vnodeopv_descs
    439 };
    440 
    441 static int
    442 ptyfs_modcmd(modcmd_t cmd, void *arg)
    443 {
    444 	int error;
    445 
    446 	switch (cmd) {
    447 	case MODULE_CMD_INIT:
    448 		error = vfs_attach(&ptyfs_vfsops);
    449 		if (error != 0)
    450 			break;
    451 		sysctl_createv(&ptyfs_sysctl_log, 0, NULL, NULL,
    452 			       CTLFLAG_PERMANENT,
    453 			       CTLTYPE_NODE, "ptyfs",
    454 			       SYSCTL_DESCR("Pty file system"),
    455 			       NULL, 0, NULL, 0,
    456 			       CTL_VFS, 23, CTL_EOL);
    457 		/*
    458 		 * XXX the "23" above could be dynamic, thereby eliminating
    459 		 * one more instance of the "number to vfs" mapping problem,
    460 		 * but "23" is the order as taken from sys/mount.h
    461 		 */
    462 		break;
    463 	case MODULE_CMD_FINI:
    464 		error = vfs_detach(&ptyfs_vfsops);
    465 		if (error != 0)
    466 			break;
    467 		sysctl_teardown(&ptyfs_sysctl_log);
    468 		break;
    469 	default:
    470 		error = ENOTTY;
    471 		break;
    472 	}
    473 
    474 	return (error);
    475 }
    476