xref: /netbsd-src/sys/kern/tty_ptm.c (revision 7d62b00eb9ad855ffcd7da46b41e23feb5476fac)
1 /*	$NetBSD: tty_ptm.c,v 1.45 2022/09/29 12:18:27 christos 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.45 2022/09/29 12:18:27 christos 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 && error != EMOVEFD);
146 		vput(vp);
147 		return error;
148 	}
149 
150 	mutex_enter(vp->v_interlock);
151 	vp->v_writecount++;
152 	mutex_exit(vp->v_interlock);
153 
154 	return 0;
155 }
156 
157 static int
158 pty_alloc_master(struct lwp *l, int *fd, dev_t *dev, struct mount *mp,
159     int flags)
160 {
161 	int error;
162 	struct file *fp;
163 	struct vnode *vp;
164 	int md;
165 
166 	if ((error = fd_allocfile(&fp, fd)) != 0) {
167 		DPRINTF(("fd_allocfile %d\n", error));
168 		return error;
169 	}
170 retry:
171 	/* Find and open a free master pty. */
172 	*dev = pty_getfree();
173 	md = minor(*dev);
174 	if ((error = pty_check(md)) != 0) {
175 		DPRINTF(("pty_check %d\n", error));
176 		goto bad;
177 	}
178 	if (ptm == NULL) {
179 		DPRINTF(("no ptm\n"));
180 		error = EOPNOTSUPP;
181 		goto bad;
182 	}
183 	if ((error = (*ptm->allocvp)(mp, l, &vp, *dev, 'p')) != 0) {
184 		DPRINTF(("pty_allocvp %d\n", error));
185 		goto bad;
186 	}
187 
188 	if ((error = pty_vn_open(vp, l)) != 0) {
189 		DPRINTF(("pty_vn_open %d\n", error));
190 		/*
191 		 * Check if the master open failed because we lost
192 		 * the race to grab it.
193 		 */
194 		if (error != EIO)
195 			goto bad;
196 		error = !pty_isfree(md, 1);
197 		DPRINTF(("pty_isfree %d\n", error));
198 		if (error)
199 			goto retry;
200 		else
201 			goto bad;
202 	}
203 	fp->f_flag = FREAD|FWRITE|(flags&FMASK);
204 	fp->f_type = DTYPE_VNODE;
205 	fp->f_ops = &vnops;
206 	fp->f_vnode = vp;
207 
208 	VOP_UNLOCK(vp);
209 	fd_set_exclose(l, *fd, (flags & O_CLOEXEC) != 0);
210 	fd_affix(curproc, fp, *fd);
211 	return 0;
212 bad:
213 	fd_abort(curproc, fp, *fd);
214 	return error;
215 }
216 
217 int
218 pty_grant_slave(struct lwp *l, dev_t dev, struct mount *mp)
219 {
220 	int error;
221 	struct vnode *vp;
222 
223 	/*
224 	 * Open the slave.
225 	 * namei -> setattr -> unlock -> revoke -> vrele ->
226 	 * namei -> open -> unlock
227 	 * Three stage rocket:
228 	 * 1. Change the owner and permissions on the slave.
229 	 * 2. Revoke all the users of the slave.
230 	 * 3. open the slave.
231 	 */
232 	if (ptm == NULL)
233 		return EOPNOTSUPP;
234 	if ((error = (*ptm->allocvp)(mp, l, &vp, dev, 't')) != 0)
235 		return error;
236 
237 	if ((vp->v_mount->mnt_flag & MNT_RDONLY) == 0) {
238 		struct vattr vattr;
239 		(*ptm->getvattr)(mp, l, &vattr);
240 		/* Do the VOP_SETATTR() as root. */
241 		error = VOP_SETATTR(vp, &vattr, lwp0.l_cred);
242 		if (error) {
243 			DPRINTF(("setattr %d\n", error));
244 			vput(vp);
245 			return error;
246 		}
247 	}
248 	VOP_UNLOCK(vp);
249 	VOP_REVOKE(vp, REVOKEALL);
250 
251 	/*
252 	 * The vnode is useless after the revoke, we need to get it again.
253 	 */
254 	vrele(vp);
255 	return 0;
256 }
257 
258 static int
259 pty_alloc_slave(struct lwp *l, int *fd, dev_t dev, struct mount *mp)
260 {
261 	int error;
262 	struct file *fp;
263 	struct vnode *vp;
264 
265 	/* Grab a filedescriptor for the slave */
266 	if ((error = fd_allocfile(&fp, fd)) != 0) {
267 		DPRINTF(("fd_allocfile %d\n", error));
268 		return error;
269 	}
270 
271 	if (ptm == NULL) {
272 		error = EOPNOTSUPP;
273 		goto bad;
274 	}
275 
276 	if ((error = (*ptm->allocvp)(mp, l, &vp, dev, 't')) != 0)
277 		goto bad;
278 	if ((error = pty_vn_open(vp, l)) != 0)
279 		goto bad;
280 
281 	fp->f_flag = FREAD|FWRITE;
282 	fp->f_type = DTYPE_VNODE;
283 	fp->f_ops = &vnops;
284 	fp->f_vnode = vp;
285 	VOP_UNLOCK(vp);
286 	fd_affix(curproc, fp, *fd);
287 	return 0;
288 bad:
289 	fd_abort(curproc, fp, *fd);
290 	return error;
291 }
292 
293 struct ptm_pty *
294 pty_sethandler(struct ptm_pty *nptm)
295 {
296 	struct ptm_pty *optm = ptm;
297 	ptm = nptm;
298 	return optm;
299 }
300 
301 int
302 pty_fill_ptmget(struct lwp *l, dev_t dev, int cfd, int sfd, void *data, struct mount *mp)
303 {
304 	struct ptmget *ptmg = data;
305 	int error;
306 
307 	if (ptm == NULL)
308 		return EOPNOTSUPP;
309 
310 	ptmg->cfd = cfd == -1 ? minor(dev) : cfd;
311 	ptmg->sfd = sfd == -1 ? minor(dev) : sfd;
312 
313 	error = (*ptm->makename)(mp, l, ptmg->cn, sizeof(ptmg->cn), dev, 'p');
314 	if (error)
315 		return error;
316 
317 	return (*ptm->makename)(mp, l, ptmg->sn, sizeof(ptmg->sn), dev, 't');
318 }
319 
320 void
321 /*ARGSUSED*/
322 ptmattach(int n)
323 {
324 	extern const struct cdevsw pts_cdevsw, ptc_cdevsw;
325 	/* find the major and minor of the pty devices */
326 	if ((pts_major = cdevsw_lookup_major(&pts_cdevsw)) == -1)
327 		panic("%s: Can't find pty slave in cdevsw", __func__);
328 	if ((ptc_major = cdevsw_lookup_major(&ptc_cdevsw)) == -1)
329 		panic("%s: Can't find pty master in cdevsw", __func__);
330 #ifdef COMPAT_BSDPTY
331 	ptm = &ptm_bsdpty;
332 #endif
333 }
334 
335 static int
336 /*ARGSUSED*/
337 ptmopen(dev_t dev, int flag, int mode, struct lwp *l)
338 {
339 	int error;
340 	int fd;
341 	dev_t ttydev;
342 	struct mount *mp;
343 
344 	switch(minor(dev)) {
345 	case 0:		/* /dev/ptmx */
346 	case 2:		/* /emul/linux/dev/ptmx */
347 		if ((error = pty_getmp(l, &mp)) != 0)
348 			return error;
349 		if ((error = pty_alloc_master(l, &fd, &ttydev, mp, flag)) != 0)
350 			return error;
351 		if (minor(dev) == 2) {
352 			/*
353 			 * Linux ptyfs grants the pty right here.
354 			 * Handle this case here, instead of writing
355 			 * a new linux module.
356 			 */
357 			if ((error = pty_grant_slave(l, ttydev, mp)) != 0) {
358 				file_t *fp = fd_getfile(fd);
359 				if (fp != NULL) {
360 					fd_close(fd);
361 				}
362 				return error;
363 			}
364 		}
365 		curlwp->l_dupfd = fd;
366 		return EMOVEFD;
367 	case 1:		/* /dev/ptm */
368 		return 0;
369 	default:
370 		return ENODEV;
371 	}
372 }
373 
374 static int
375 /*ARGSUSED*/
376 ptmclose(dev_t dev, int flag, int mode, struct lwp *l)
377 {
378 
379 	return (0);
380 }
381 
382 static int
383 /*ARGSUSED*/
384 ptmioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
385 {
386 	int error;
387 	dev_t newdev;
388 	int cfd, sfd;
389 	file_t *fp;
390 	struct mount *mp;
391 
392 	error = 0;
393 	switch (cmd) {
394 	case TIOCPTMGET:
395 		if ((error = pty_getmp(l, &mp)) != 0)
396 			return error;
397 
398 		if ((error = pty_alloc_master(l, &cfd, &newdev, mp, 0)) != 0)
399 			return error;
400 
401 		if ((error = pty_grant_slave(l, newdev, mp)) != 0)
402 			goto bad;
403 
404 		if ((error = pty_alloc_slave(l, &sfd, newdev, mp)) != 0)
405 			goto bad;
406 
407 		/* now, put the indices and names into struct ptmget */
408 		if ((error = pty_fill_ptmget(l, newdev, cfd, sfd, data, mp)) != 0)
409 			goto bad2;
410 		return 0;
411 	default:
412 		MODULE_HOOK_CALL(tty_ptmioctl_60_hook,
413 		    (dev, cmd, data, flag, l), EPASSTHROUGH, error);
414 		if (error != EPASSTHROUGH)
415 			return error;
416 		DPRINTF(("ptmioctl EINVAL\n"));
417 		return EINVAL;
418 	}
419 bad2:
420 	fp = fd_getfile(sfd);
421 	if (fp != NULL) {
422 		fd_close(sfd);
423 	}
424  bad:
425 	fp = fd_getfile(cfd);
426 	if (fp != NULL) {
427 		fd_close(cfd);
428 	}
429 	return error;
430 }
431 
432 const struct cdevsw ptm_cdevsw = {
433 	.d_open = ptmopen,
434 	.d_close = ptmclose,
435 	.d_read = noread,
436 	.d_write = nowrite,
437 	.d_ioctl = ptmioctl,
438 	.d_stop = nullstop,
439 	.d_tty = notty,
440 	.d_poll = nopoll,
441 	.d_mmap = nommap,
442 	.d_kqfilter = nokqfilter,
443 	.d_discard = nodiscard,
444 	.d_flag = D_TTY
445 };
446 #endif
447