1 /* $NetBSD: tty_ptm.c,v 1.47 2025/08/05 23:56:21 gutteridge Exp $ */ 2 3 /*- 4 * Copyright (c) 2004, 2020 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 17 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 18 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 19 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 20 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 26 * POSSIBILITY OF SUCH DAMAGE. 27 */ 28 29 #include <sys/cdefs.h> 30 __KERNEL_RCSID(0, "$NetBSD: tty_ptm.c,v 1.47 2025/08/05 23:56:21 gutteridge Exp $"); 31 32 #ifdef _KERNEL_OPT 33 #include "opt_compat_netbsd.h" 34 #include "opt_ptm.h" 35 #endif 36 37 /* pty multiplexor driver /dev/ptm{,x} */ 38 39 #include <sys/param.h> 40 #include <sys/systm.h> 41 #include <sys/ioctl.h> 42 #include <sys/proc.h> 43 #include <sys/tty.h> 44 #include <sys/stat.h> 45 #include <sys/file.h> 46 #include <sys/uio.h> 47 #include <sys/kernel.h> 48 #include <sys/vnode.h> 49 #include <sys/namei.h> 50 #include <sys/signalvar.h> 51 #include <sys/filedesc.h> 52 #include <sys/conf.h> 53 #include <sys/poll.h> 54 #include <sys/pty.h> 55 #include <sys/kauth.h> 56 #include <sys/compat_stub.h> 57 58 #include <miscfs/specfs/specdev.h> 59 60 #include <compat/sys/ttycom.h> 61 62 #include "ioconf.h" 63 64 #ifdef DEBUG_PTM 65 #define DPRINTF(a) printf a 66 #else 67 #define DPRINTF(a) 68 #endif 69 70 #ifdef NO_DEV_PTM 71 const struct cdevsw ptm_cdevsw = { 72 .d_open = noopen, 73 .d_close = noclose, 74 .d_read = noread, 75 .d_write = nowrite, 76 .d_ioctl = noioctl, 77 .d_stop = nostop, 78 .d_tty = notty, 79 .d_poll = nopoll, 80 .d_mmap = nommap, 81 .d_kqfilter = nokqfilter, 82 .d_discard = nodiscard, 83 .d_flag = D_TTY 84 }; 85 #else 86 87 int pts_major, ptc_major; 88 89 static dev_t pty_getfree(void); 90 static int pty_alloc_master(struct lwp *, int *, dev_t *, struct mount *, int); 91 static int pty_alloc_slave(struct lwp *, int *, dev_t, struct mount *); 92 static int pty_vn_open(struct vnode *, struct lwp *); 93 94 int 95 pty_getmp(struct lwp *l, struct mount **mpp) 96 { 97 if (ptm == NULL) 98 return EOPNOTSUPP; 99 100 return (*ptm->getmp)(l, mpp); 101 } 102 103 dev_t 104 pty_makedev(char ms, int minor) 105 { 106 return makedev(ms == 't' ? pts_major : ptc_major, minor); 107 } 108 109 110 static dev_t 111 pty_getfree(void) 112 { 113 extern kmutex_t pt_softc_mutex; 114 int i; 115 116 mutex_enter(&pt_softc_mutex); 117 for (i = 0; i < npty; i++) { 118 if (pty_isfree(i, 0)) 119 break; 120 } 121 mutex_exit(&pt_softc_mutex); 122 return pty_makedev('t', i); 123 } 124 125 /* 126 * Hacked up version of vn_open. We _only_ handle ptys and only open 127 * them with FREAD|FWRITE and never deal with creat or stuff like that. 128 * 129 * We need it because we have to fake up root credentials to open the pty. 130 */ 131 int 132 pty_vn_open(struct vnode *vp, struct lwp *l) 133 { 134 int error; 135 136 if (vp->v_type != VCHR) { 137 vput(vp); 138 return EINVAL; 139 } 140 141 error = VOP_OPEN(vp, FREAD|FWRITE, lwp0.l_cred); 142 143 if (error) { 144 /* only ptys mean we can't get these */ 145 KASSERT(error != EDUPFD); 146 KASSERT(error != EMOVEFD); 147 vput(vp); 148 return error; 149 } 150 151 mutex_enter(vp->v_interlock); 152 vp->v_writecount++; 153 mutex_exit(vp->v_interlock); 154 155 return 0; 156 } 157 158 static int 159 pty_alloc_master(struct lwp *l, int *fd, dev_t *dev, struct mount *mp, 160 int flags) 161 { 162 int error; 163 struct file *fp; 164 struct vnode *vp; 165 int md; 166 167 if ((error = fd_allocfile(&fp, fd)) != 0) { 168 DPRINTF(("fd_allocfile %d\n", error)); 169 return error; 170 } 171 retry: 172 /* Find and open a free master pty. */ 173 *dev = pty_getfree(); 174 md = minor(*dev); 175 if ((error = pty_check(md)) != 0) { 176 DPRINTF(("pty_check %d\n", error)); 177 goto bad; 178 } 179 if (ptm == NULL) { 180 DPRINTF(("no ptm\n")); 181 error = EOPNOTSUPP; 182 goto bad; 183 } 184 if ((error = (*ptm->allocvp)(mp, l, &vp, *dev, 'p')) != 0) { 185 DPRINTF(("pty_allocvp %d\n", error)); 186 goto bad; 187 } 188 189 if ((error = pty_vn_open(vp, l)) != 0) { 190 DPRINTF(("pty_vn_open %d\n", error)); 191 /* 192 * Check if the master open failed because we lost 193 * the race to grab it. 194 */ 195 if (error != EIO) 196 goto bad; 197 error = !pty_isfree(md, 1); 198 DPRINTF(("pty_isfree %d\n", error)); 199 if (error) 200 goto retry; 201 else 202 goto bad; 203 } 204 fp->f_flag = FREAD|FWRITE|(flags&FMASK); 205 fp->f_type = DTYPE_VNODE; 206 fp->f_ops = &vnops; 207 fp->f_vnode = vp; 208 209 VOP_UNLOCK(vp); 210 fd_set_exclose(l, *fd, (flags & O_CLOEXEC) != 0); 211 fd_set_foclose(l, *fd, (flags & O_CLOFORK) != 0); 212 fd_affix(curproc, fp, *fd); 213 return 0; 214 bad: 215 fd_abort(curproc, fp, *fd); 216 return error; 217 } 218 219 int 220 pty_grant_slave(struct lwp *l, dev_t dev, struct mount *mp) 221 { 222 int error; 223 struct vnode *vp; 224 225 /* 226 * Open the slave. 227 * namei -> setattr -> unlock -> revoke -> vrele -> 228 * namei -> open -> unlock 229 * Three stage rocket: 230 * 1. Change the owner and permissions on the slave. 231 * 2. Revoke all the users of the slave. 232 * 3. open the slave. 233 */ 234 if (ptm == NULL) 235 return EOPNOTSUPP; 236 if ((error = (*ptm->allocvp)(mp, l, &vp, dev, 't')) != 0) 237 return error; 238 239 if ((vp->v_mount->mnt_flag & MNT_RDONLY) == 0) { 240 struct vattr vattr; 241 (*ptm->getvattr)(mp, l, &vattr); 242 /* Do the VOP_SETATTR() as root. */ 243 error = VOP_SETATTR(vp, &vattr, lwp0.l_cred); 244 if (error) { 245 DPRINTF(("setattr %d\n", error)); 246 vput(vp); 247 return error; 248 } 249 } 250 VOP_UNLOCK(vp); 251 VOP_REVOKE(vp, REVOKEALL); 252 253 /* 254 * The vnode is useless after the revoke, we need to get it again. 255 */ 256 vrele(vp); 257 return 0; 258 } 259 260 static int 261 pty_alloc_slave(struct lwp *l, int *fd, dev_t dev, struct mount *mp) 262 { 263 int error; 264 struct file *fp; 265 struct vnode *vp; 266 267 /* Grab a filedescriptor for the slave */ 268 if ((error = fd_allocfile(&fp, fd)) != 0) { 269 DPRINTF(("fd_allocfile %d\n", error)); 270 return error; 271 } 272 273 if (ptm == NULL) { 274 error = EOPNOTSUPP; 275 goto bad; 276 } 277 278 if ((error = (*ptm->allocvp)(mp, l, &vp, dev, 't')) != 0) 279 goto bad; 280 if ((error = pty_vn_open(vp, l)) != 0) 281 goto bad; 282 283 fp->f_flag = FREAD|FWRITE; 284 fp->f_type = DTYPE_VNODE; 285 fp->f_ops = &vnops; 286 fp->f_vnode = vp; 287 VOP_UNLOCK(vp); 288 fd_affix(curproc, fp, *fd); 289 return 0; 290 bad: 291 fd_abort(curproc, fp, *fd); 292 return error; 293 } 294 295 struct ptm_pty * 296 pty_sethandler(struct ptm_pty *nptm) 297 { 298 struct ptm_pty *optm = ptm; 299 ptm = nptm; 300 return optm; 301 } 302 303 int 304 pty_fill_ptmget(struct lwp *l, dev_t dev, int cfd, int sfd, void *data, struct mount *mp) 305 { 306 struct ptmget *ptmg = data; 307 int error; 308 309 if (ptm == NULL) 310 return EOPNOTSUPP; 311 312 ptmg->cfd = cfd == -1 ? minor(dev) : cfd; 313 ptmg->sfd = sfd == -1 ? minor(dev) : sfd; 314 315 error = (*ptm->makename)(mp, l, ptmg->cn, sizeof(ptmg->cn), dev, 'p'); 316 if (error) 317 return error; 318 319 return (*ptm->makename)(mp, l, ptmg->sn, sizeof(ptmg->sn), dev, 't'); 320 } 321 322 void 323 /*ARGSUSED*/ 324 ptmattach(int n) 325 { 326 extern const struct cdevsw pts_cdevsw, ptc_cdevsw; 327 /* find the major and minor of the pty devices */ 328 if ((pts_major = cdevsw_lookup_major(&pts_cdevsw)) == -1) 329 panic("%s: Can't find pty slave in cdevsw", __func__); 330 if ((ptc_major = cdevsw_lookup_major(&ptc_cdevsw)) == -1) 331 panic("%s: Can't find pty master in cdevsw", __func__); 332 #ifdef COMPAT_BSDPTY 333 ptm = &ptm_bsdpty; 334 #endif 335 } 336 337 static int 338 /*ARGSUSED*/ 339 ptmopen(dev_t dev, int flag, int mode, struct lwp *l) 340 { 341 int error; 342 int fd; 343 dev_t ttydev; 344 struct mount *mp; 345 346 switch(minor(dev)) { 347 case 0: /* /dev/ptmx */ 348 case 2: /* /emul/linux/dev/ptmx */ 349 if ((error = pty_getmp(l, &mp)) != 0) 350 return error; 351 if ((error = pty_alloc_master(l, &fd, &ttydev, mp, flag)) != 0) 352 return error; 353 if (minor(dev) == 2) { 354 /* 355 * Linux ptyfs grants the pty right here. 356 * Handle this case here, instead of writing 357 * a new linux module. 358 */ 359 if ((error = pty_grant_slave(l, ttydev, mp)) != 0) { 360 file_t *fp = fd_getfile(fd); 361 if (fp != NULL) { 362 fd_close(fd); 363 } 364 return error; 365 } 366 } 367 curlwp->l_dupfd = fd; 368 return EMOVEFD; 369 case 1: /* /dev/ptm */ 370 return 0; 371 default: 372 return ENODEV; 373 } 374 } 375 376 static int 377 /*ARGSUSED*/ 378 ptmclose(dev_t dev, int flag, int mode, struct lwp *l) 379 { 380 381 return (0); 382 } 383 384 static int 385 /*ARGSUSED*/ 386 ptmioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l) 387 { 388 int error; 389 dev_t newdev; 390 int cfd, sfd; 391 file_t *fp; 392 struct mount *mp; 393 394 error = 0; 395 switch (cmd) { 396 case TIOCPTMGET: 397 if ((error = pty_getmp(l, &mp)) != 0) 398 return error; 399 400 if ((error = pty_alloc_master(l, &cfd, &newdev, mp, 0)) != 0) 401 return error; 402 403 if ((error = pty_grant_slave(l, newdev, mp)) != 0) 404 goto bad; 405 406 if ((error = pty_alloc_slave(l, &sfd, newdev, mp)) != 0) 407 goto bad; 408 409 /* now, put the indices and names into struct ptmget */ 410 if ((error = pty_fill_ptmget(l, newdev, cfd, sfd, data, mp)) != 0) 411 goto bad2; 412 return 0; 413 default: 414 MODULE_HOOK_CALL(tty_ptmioctl_60_hook, 415 (dev, cmd, data, flag, l), EPASSTHROUGH, error); 416 if (error != EPASSTHROUGH) 417 return error; 418 DPRINTF(("ptmioctl EINVAL\n")); 419 return EINVAL; 420 } 421 bad2: 422 fp = fd_getfile(sfd); 423 if (fp != NULL) { 424 fd_close(sfd); 425 } 426 bad: 427 fp = fd_getfile(cfd); 428 if (fp != NULL) { 429 fd_close(cfd); 430 } 431 return error; 432 } 433 434 const struct cdevsw ptm_cdevsw = { 435 .d_open = ptmopen, 436 .d_close = ptmclose, 437 .d_read = noread, 438 .d_write = nowrite, 439 .d_ioctl = ptmioctl, 440 .d_stop = nullstop, 441 .d_tty = notty, 442 .d_poll = nopoll, 443 .d_mmap = nommap, 444 .d_kqfilter = nokqfilter, 445 .d_discard = nodiscard, 446 .d_flag = D_TTY 447 }; 448 #endif 449