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