ptyfs_vfsops.c revision 1.54
1/* $NetBSD: ptyfs_vfsops.c,v 1.54 2014/10/15 15:00:03 christos 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.54 2014/10/15 15:00:03 christos 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 66MODULE(MODULE_CLASS_VFS, ptyfs, NULL); 67 68MALLOC_JUSTDEFINE(M_PTYFSMNT, "ptyfs mount", "ptyfs mount structures"); 69MALLOC_JUSTDEFINE(M_PTYFSTMP, "ptyfs temp", "ptyfs temporary structures"); 70 71VFS_PROTOS(ptyfs); 72 73static struct sysctllog *ptyfs_sysctl_log; 74 75static int ptyfs__allocvp(struct mount *, struct lwp *, struct vnode **, 76 dev_t, char); 77static int ptyfs__makename(struct mount *, struct lwp *, char *, size_t, 78 dev_t, char); 79static void ptyfs__getvattr(struct mount *, struct lwp *, struct vattr *); 80static int ptyfs__getmp(struct lwp *, struct mount **); 81 82/* 83 * ptm glue: When we mount, we make ptm point to us. 84 */ 85struct ptm_pty *ptyfs_save_ptm; 86static int ptyfs_count; 87 88static TAILQ_HEAD(, ptyfsmount) ptyfs_head; 89 90struct ptm_pty ptm_ptyfspty = { 91 ptyfs__allocvp, 92 ptyfs__makename, 93 ptyfs__getvattr, 94 ptyfs__getmp, 95}; 96 97static int 98ptyfs__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; 114ok: 115 *mpp = mp; 116 return 0; 117} 118 119static const char * 120ptyfs__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; 147out: 148 free(buf, M_TEMP); 149 return rv; 150} 151 152static int 153ptyfs__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 192static int 193/*ARGSUSED*/ 194ptyfs__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) 213 return error; 214 error = vn_lock(*vpp, LK_EXCLUSIVE); 215 if (error) { 216 vrele(*vpp); 217 *vpp = NULL; 218 return error; 219 } 220 /* Activate node only after we have grabbed device. */ 221 if (type == PTYFSpts) 222 ptyfs_set_active(mp, minor(dev)); 223 return 0; 224} 225 226 227static void 228ptyfs__getvattr(struct mount *mp, struct lwp *l, struct vattr *vattr) 229{ 230 struct ptyfsmount *pmnt = VFSTOPTY(mp); 231 vattr_null(vattr); 232 /* get real uid */ 233 vattr->va_uid = kauth_cred_getuid(l->l_cred); 234 vattr->va_gid = pmnt->pmnt_gid; 235 vattr->va_mode = pmnt->pmnt_mode; 236} 237 238 239void 240ptyfs_init(void) 241{ 242 243 TAILQ_INIT(&ptyfs_head); 244 malloc_type_attach(M_PTYFSMNT); 245 malloc_type_attach(M_PTYFSTMP); 246 ptyfs_hashinit(); 247} 248 249void 250ptyfs_reinit(void) 251{ 252 253} 254 255void 256ptyfs_done(void) 257{ 258 259 ptyfs_hashdone(); 260 malloc_type_detach(M_PTYFSTMP); 261 malloc_type_detach(M_PTYFSMNT); 262} 263 264#define OSIZE sizeof(struct { int f; gid_t g; mode_t m; }) 265/* 266 * Mount the Pseudo tty params filesystem 267 */ 268int 269ptyfs_mount(struct mount *mp, const char *path, void *data, size_t *data_len) 270{ 271 struct lwp *l = curlwp; 272 int error = 0; 273 struct ptyfsmount *pmnt; 274 struct ptyfs_args *args = data; 275 276 if (args == NULL) 277 return EINVAL; 278 if (*data_len != sizeof *args) { 279 if (*data_len != OSIZE || args->version >= PTYFS_ARGSVERSION) 280 return EINVAL; 281 } 282 283 if (UIO_MX & (UIO_MX - 1)) { 284 log(LOG_ERR, "ptyfs: invalid directory entry size"); 285 return EINVAL; 286 } 287 288 if (mp->mnt_flag & MNT_GETARGS) { 289 pmnt = VFSTOPTY(mp); 290 if (pmnt == NULL) 291 return EIO; 292 args->mode = pmnt->pmnt_mode; 293 args->gid = pmnt->pmnt_gid; 294 if (args->version >= PTYFS_ARGSVERSION) { 295 args->flags = pmnt->pmnt_flags; 296 *data_len = sizeof *args; 297 } else { 298 *data_len = OSIZE; 299 } 300 return 0; 301 } 302 303#if 0 304 /* Don't allow more than one mount */ 305 if (ptyfs_count) 306 return EBUSY; 307#endif 308 309 if (mp->mnt_flag & MNT_UPDATE) 310 return EOPNOTSUPP; 311 312 if (args->version > PTYFS_ARGSVERSION) 313 return EINVAL; 314 315 pmnt = malloc(sizeof(struct ptyfsmount), M_PTYFSMNT, M_WAITOK); 316 317 mp->mnt_data = pmnt; 318 mutex_init(&pmnt->pmnt_lock, MUTEX_DEFAULT, IPL_NONE); 319 pmnt->pmnt_gid = args->gid; 320 pmnt->pmnt_mode = args->mode; 321 if (args->version >= PTYFS_ARGSVERSION) 322 pmnt->pmnt_flags = args->flags; 323 else 324 pmnt->pmnt_flags = 0; 325 pmnt->pmnt_bitmap_size = 0; 326 pmnt->pmnt_bitmap = NULL; 327 mp->mnt_flag |= MNT_LOCAL; 328 vfs_getnewfsid(mp); 329 330 if ((error = set_statvfs_info(path, UIO_USERSPACE, "ptyfs", 331 UIO_SYSSPACE, mp->mnt_op->vfs_name, mp, l)) != 0) { 332 free(pmnt, M_PTYFSMNT); 333 return error; 334 } 335 336 pmnt->pmnt_mp = mp; 337 TAILQ_INSERT_TAIL(&ptyfs_head, pmnt, pmnt_le); 338 if (ptyfs_count++ == 0) { 339 /* Point pty access to us */ 340 ptyfs_save_ptm = pty_sethandler(&ptm_ptyfspty); 341 } 342 return 0; 343} 344 345/*ARGSUSED*/ 346int 347ptyfs_start(struct mount *mp, int flags) 348{ 349 return 0; 350} 351 352/*ARGSUSED*/ 353int 354ptyfs_unmount(struct mount *mp, int mntflags) 355{ 356 int error; 357 int flags = 0; 358 struct ptyfsmount *pmnt; 359 360 if (mntflags & MNT_FORCE) 361 flags |= FORCECLOSE; 362 363 if ((error = vflush(mp, 0, flags)) != 0) 364 return error; 365 366 ptyfs_count--; 367 if (ptyfs_count == 0) { 368 /* Restore where pty access was pointing */ 369 (void)pty_sethandler(ptyfs_save_ptm); 370 ptyfs_save_ptm = NULL; 371 } 372 TAILQ_FOREACH(pmnt, &ptyfs_head, pmnt_le) { 373 if (pmnt->pmnt_mp == mp) { 374 TAILQ_REMOVE(&ptyfs_head, pmnt, pmnt_le); 375 break; 376 } 377 } 378 379 /* 380 * Finally, throw away the ptyfsmount structure 381 */ 382 if (pmnt->pmnt_bitmap_size > 0) 383 kmem_free(pmnt->pmnt_bitmap, pmnt->pmnt_bitmap_size); 384 mutex_destroy(&pmnt->pmnt_lock); 385 free(mp->mnt_data, M_PTYFSMNT); 386 mp->mnt_data = NULL; 387 388 return 0; 389} 390 391int 392ptyfs_root(struct mount *mp, struct vnode **vpp) 393{ 394 int error; 395 396 /* setup "." */ 397 error = ptyfs_allocvp(mp, vpp, PTYFSroot, 0); 398 if (error) 399 return error; 400 error = vn_lock(*vpp, LK_EXCLUSIVE); 401 if (error) { 402 vrele(*vpp); 403 *vpp = NULL; 404 return error; 405 } 406 return 0; 407} 408 409/*ARGSUSED*/ 410int 411ptyfs_sync(struct mount *mp, int waitfor, 412 kauth_cred_t uc) 413{ 414 return 0; 415} 416 417/* 418 * Initialize this vnode / ptynode pair. 419 * Only for the slave side of a pty, caller assures 420 * no other thread will try to load this node. 421 */ 422int 423ptyfs_loadvnode(struct mount *mp, struct vnode *vp, 424 const void *key, size_t key_len, const void **new_key) 425{ 426 struct ptyfskey pkey; 427 struct ptyfsnode *ptyfs; 428 429 KASSERT(key_len == sizeof(pkey)); 430 memcpy(&pkey, key, key_len); 431 432 ptyfs = ptyfs_get_node(pkey.ptk_type, pkey.ptk_pty); 433 KASSERT(memcmp(&ptyfs->ptyfs_key, &pkey, sizeof(pkey)) == 0); 434 435 switch (pkey.ptk_type) { 436 case PTYFSroot: /* /pts = dr-xr-xr-x */ 437 vp->v_type = VDIR; 438 vp->v_vflag = VV_ROOT; 439 break; 440 441 case PTYFSpts: /* /pts/N = cxxxxxxxxx */ 442 case PTYFSptc: /* controlling side = cxxxxxxxxx */ 443 vp->v_type = VCHR; 444 spec_node_init(vp, PTYFS_MAKEDEV(ptyfs)); 445 break; 446 default: 447 panic("ptyfs_loadvnode"); 448 } 449 450 vp->v_tag = VT_PTYFS; 451 vp->v_op = ptyfs_vnodeop_p; 452 vp->v_data = ptyfs; 453 uvm_vnp_setsize(vp, 0); 454 *new_key = &ptyfs->ptyfs_key; 455 return 0; 456} 457 458/* 459 * Kernfs flat namespace lookup. 460 * Currently unsupported. 461 */ 462/*ARGSUSED*/ 463int 464ptyfs_vget(struct mount *mp, ino_t ino, 465 struct vnode **vpp) 466{ 467 return EOPNOTSUPP; 468} 469 470extern const struct vnodeopv_desc ptyfs_vnodeop_opv_desc; 471 472const struct vnodeopv_desc * const ptyfs_vnodeopv_descs[] = { 473 &ptyfs_vnodeop_opv_desc, 474 NULL, 475}; 476 477struct vfsops ptyfs_vfsops = { 478 .vfs_name = MOUNT_PTYFS, 479 .vfs_min_mount_data = sizeof (struct ptyfs_args), 480 .vfs_mount = ptyfs_mount, 481 .vfs_start = ptyfs_start, 482 .vfs_unmount = ptyfs_unmount, 483 .vfs_root = ptyfs_root, 484 .vfs_quotactl = (void *)eopnotsupp, 485 .vfs_statvfs = genfs_statvfs, 486 .vfs_sync = ptyfs_sync, 487 .vfs_vget = ptyfs_vget, 488 .vfs_loadvnode = ptyfs_loadvnode, 489 .vfs_fhtovp = (void *)eopnotsupp, 490 .vfs_vptofh = (void *)eopnotsupp, 491 .vfs_init = ptyfs_init, 492 .vfs_reinit = ptyfs_reinit, 493 .vfs_done = ptyfs_done, 494 .vfs_snapshot = (void *)eopnotsupp, 495 .vfs_extattrctl = (void *)eopnotsupp, 496 .vfs_suspendctl = (void *)eopnotsupp, 497 .vfs_renamelock_enter = genfs_renamelock_enter, 498 .vfs_renamelock_exit = genfs_renamelock_exit, 499 .vfs_fsync = (void *)eopnotsupp, 500 .vfs_opv_descs = ptyfs_vnodeopv_descs 501}; 502 503static int 504ptyfs_modcmd(modcmd_t cmd, void *arg) 505{ 506 int error; 507 508 switch (cmd) { 509 case MODULE_CMD_INIT: 510 error = vfs_attach(&ptyfs_vfsops); 511 if (error != 0) 512 break; 513 sysctl_createv(&ptyfs_sysctl_log, 0, NULL, NULL, 514 CTLFLAG_PERMANENT, 515 CTLTYPE_NODE, "ptyfs", 516 SYSCTL_DESCR("Pty file system"), 517 NULL, 0, NULL, 0, 518 CTL_VFS, 23, CTL_EOL); 519 /* 520 * XXX the "23" above could be dynamic, thereby eliminating 521 * one more instance of the "number to vfs" mapping problem, 522 * but "23" is the order as taken from sys/mount.h 523 */ 524 break; 525 case MODULE_CMD_FINI: 526 error = vfs_detach(&ptyfs_vfsops); 527 if (error != 0) 528 break; 529 sysctl_teardown(&ptyfs_sysctl_log); 530 break; 531 default: 532 error = ENOTTY; 533 break; 534 } 535 536 return (error); 537} 538