ptyfs_vfsops.c revision 1.49 1 /* $NetBSD: ptyfs_vfsops.c,v 1.49 2014/04/04 18:10:29 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.49 2014/04/04 18:10:29 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
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
159 switch (ms) {
160 case 'p':
161 /* We don't provide access to the master, should we? */
162 len = snprintf(tbuf, bufsiz, "/dev/null");
163 break;
164 case 't':
165 /*
166 * We support traditional ptys, so we can get here,
167 * if pty had been opened before PTYFS was mounted,
168 * or was opened through /dev/ptyXX devices.
169 * Return it only outside chroot for more security .
170 */
171 if (l->l_proc->p_cwdi->cwdi_rdir == NULL
172 && ptyfs_save_ptm != NULL
173 && ptyfs_used_get(PTYFSptc, minor(dev), mp, 0) == NULL)
174 return (*ptyfs_save_ptm->makename)(mp, l,
175 tbuf, bufsiz, dev, ms);
176
177 np = ptyfs__getpath(l, mp);
178 if (np == NULL)
179 return EOPNOTSUPP;
180 len = snprintf(tbuf, bufsiz, "%s/%llu", np,
181 (unsigned long long)minor(dev));
182 break;
183 default:
184 return EINVAL;
185 }
186
187 return len >= bufsiz ? ENOSPC : 0;
188 }
189
190
191 static int
192 /*ARGSUSED*/
193 ptyfs__allocvp(struct mount *mp, struct lwp *l, struct vnode **vpp,
194 dev_t dev, char ms)
195 {
196 ptyfstype type;
197
198 switch (ms) {
199 case 'p':
200 type = PTYFSptc;
201 break;
202 case 't':
203 type = PTYFSpts;
204 break;
205 default:
206 return EINVAL;
207 }
208
209 return ptyfs_allocvp(mp, vpp, type, minor(dev), l);
210 }
211
212
213 static void
214 ptyfs__getvattr(struct mount *mp, struct lwp *l, struct vattr *vattr)
215 {
216 struct ptyfsmount *pmnt = VFSTOPTY(mp);
217 vattr_null(vattr);
218 /* get real uid */
219 vattr->va_uid = kauth_cred_getuid(l->l_cred);
220 vattr->va_gid = pmnt->pmnt_gid;
221 vattr->va_mode = pmnt->pmnt_mode;
222 }
223
224
225 void
226 ptyfs_init(void)
227 {
228
229 TAILQ_INIT(&ptyfs_head);
230 malloc_type_attach(M_PTYFSMNT);
231 malloc_type_attach(M_PTYFSTMP);
232 ptyfs_hashinit();
233 }
234
235 void
236 ptyfs_reinit(void)
237 {
238 ptyfs_hashreinit();
239 }
240
241 void
242 ptyfs_done(void)
243 {
244
245 ptyfs_hashdone();
246 malloc_type_detach(M_PTYFSTMP);
247 malloc_type_detach(M_PTYFSMNT);
248 }
249
250 #define OSIZE sizeof(struct { int f; gid_t g; mode_t m; })
251 /*
252 * Mount the Pseudo tty params filesystem
253 */
254 int
255 ptyfs_mount(struct mount *mp, const char *path, void *data, size_t *data_len)
256 {
257 struct lwp *l = curlwp;
258 int error = 0;
259 struct ptyfsmount *pmnt;
260 struct ptyfs_args *args = data;
261
262 if (*data_len != sizeof *args && *data_len != OSIZE)
263 return EINVAL;
264
265 if (UIO_MX & (UIO_MX - 1)) {
266 log(LOG_ERR, "ptyfs: invalid directory entry size");
267 return EINVAL;
268 }
269
270 if (mp->mnt_flag & MNT_GETARGS) {
271 pmnt = VFSTOPTY(mp);
272 if (pmnt == NULL)
273 return EIO;
274 args->mode = pmnt->pmnt_mode;
275 args->gid = pmnt->pmnt_gid;
276 if (args->version >= PTYFS_ARGSVERSION) {
277 args->flags = pmnt->pmnt_flags;
278 *data_len = sizeof *args;
279 } else {
280 *data_len = OSIZE;
281 }
282 return 0;
283 }
284
285 #if 0
286 /* Don't allow more than one mount */
287 if (ptyfs_count)
288 return EBUSY;
289 #endif
290
291 if (mp->mnt_flag & MNT_UPDATE)
292 return EOPNOTSUPP;
293
294 if (args->version > PTYFS_ARGSVERSION)
295 return EINVAL;
296
297 pmnt = malloc(sizeof(struct ptyfsmount), M_PTYFSMNT, M_WAITOK);
298
299 mp->mnt_data = pmnt;
300 pmnt->pmnt_gid = args->gid;
301 pmnt->pmnt_mode = args->mode;
302 if (args->version >= PTYFS_ARGSVERSION)
303 pmnt->pmnt_flags = args->flags;
304 else
305 pmnt->pmnt_flags = 0;
306 mp->mnt_flag |= MNT_LOCAL;
307 vfs_getnewfsid(mp);
308
309 if ((error = set_statvfs_info(path, UIO_USERSPACE, "ptyfs",
310 UIO_SYSSPACE, mp->mnt_op->vfs_name, mp, l)) != 0) {
311 free(pmnt, M_PTYFSMNT);
312 return error;
313 }
314
315 pmnt->pmnt_mp = mp;
316 TAILQ_INSERT_TAIL(&ptyfs_head, pmnt, pmnt_le);
317 if (ptyfs_count++ == 0) {
318 /* Point pty access to us */
319 ptyfs_save_ptm = pty_sethandler(&ptm_ptyfspty);
320 }
321 return 0;
322 }
323
324 /*ARGSUSED*/
325 int
326 ptyfs_start(struct mount *mp, int flags)
327 {
328 return 0;
329 }
330
331 /*ARGSUSED*/
332 int
333 ptyfs_unmount(struct mount *mp, int mntflags)
334 {
335 int error;
336 int flags = 0;
337 struct ptyfsmount *pmnt;
338
339 if (mntflags & MNT_FORCE)
340 flags |= FORCECLOSE;
341
342 if ((error = vflush(mp, 0, flags)) != 0)
343 return error;
344
345 ptyfs_count--;
346 if (ptyfs_count == 0) {
347 /* Restore where pty access was pointing */
348 (void)pty_sethandler(ptyfs_save_ptm);
349 ptyfs_save_ptm = NULL;
350 }
351 TAILQ_FOREACH(pmnt, &ptyfs_head, pmnt_le) {
352 if (pmnt->pmnt_mp == mp) {
353 TAILQ_REMOVE(&ptyfs_head, pmnt, pmnt_le);
354 break;
355 }
356 }
357
358 /*
359 * Finally, throw away the ptyfsmount structure
360 */
361 free(mp->mnt_data, M_PTYFSMNT);
362 mp->mnt_data = NULL;
363
364 return 0;
365 }
366
367 int
368 ptyfs_root(struct mount *mp, struct vnode **vpp)
369 {
370 /* setup "." */
371 return ptyfs_allocvp(mp, vpp, PTYFSroot, 0, NULL);
372 }
373
374 /*ARGSUSED*/
375 int
376 ptyfs_sync(struct mount *mp, int waitfor,
377 kauth_cred_t uc)
378 {
379 return 0;
380 }
381
382 /*
383 * Kernfs flat namespace lookup.
384 * Currently unsupported.
385 */
386 /*ARGSUSED*/
387 int
388 ptyfs_vget(struct mount *mp, ino_t ino,
389 struct vnode **vpp)
390 {
391 return EOPNOTSUPP;
392 }
393
394 extern const struct vnodeopv_desc ptyfs_vnodeop_opv_desc;
395
396 const struct vnodeopv_desc * const ptyfs_vnodeopv_descs[] = {
397 &ptyfs_vnodeop_opv_desc,
398 NULL,
399 };
400
401 struct vfsops ptyfs_vfsops = {
402 .vfs_name = MOUNT_PTYFS,
403 .vfs_min_mount_data = sizeof (struct ptyfs_args),
404 .vfs_mount = ptyfs_mount,
405 .vfs_start = ptyfs_start,
406 .vfs_unmount = ptyfs_unmount,
407 .vfs_root = ptyfs_root,
408 .vfs_quotactl = (void *)eopnotsupp,
409 .vfs_statvfs = genfs_statvfs,
410 .vfs_sync = ptyfs_sync,
411 .vfs_vget = ptyfs_vget,
412 .vfs_fhtovp = (void *)eopnotsupp,
413 .vfs_vptofh = (void *)eopnotsupp,
414 .vfs_init = ptyfs_init,
415 .vfs_reinit = ptyfs_reinit,
416 .vfs_done = ptyfs_done,
417 .vfs_snapshot = (void *)eopnotsupp,
418 .vfs_extattrctl = (void *)eopnotsupp,
419 .vfs_suspendctl = (void *)eopnotsupp,
420 .vfs_renamelock_enter = genfs_renamelock_enter,
421 .vfs_renamelock_exit = genfs_renamelock_exit,
422 .vfs_fsync = (void *)eopnotsupp,
423 .vfs_opv_descs = ptyfs_vnodeopv_descs
424 };
425
426 static int
427 ptyfs_modcmd(modcmd_t cmd, void *arg)
428 {
429 int error;
430
431 switch (cmd) {
432 case MODULE_CMD_INIT:
433 error = vfs_attach(&ptyfs_vfsops);
434 if (error != 0)
435 break;
436 sysctl_createv(&ptyfs_sysctl_log, 0, NULL, NULL,
437 CTLFLAG_PERMANENT,
438 CTLTYPE_NODE, "ptyfs",
439 SYSCTL_DESCR("Pty file system"),
440 NULL, 0, NULL, 0,
441 CTL_VFS, 23, CTL_EOL);
442 /*
443 * XXX the "23" above could be dynamic, thereby eliminating
444 * one more instance of the "number to vfs" mapping problem,
445 * but "23" is the order as taken from sys/mount.h
446 */
447 break;
448 case MODULE_CMD_FINI:
449 error = vfs_detach(&ptyfs_vfsops);
450 if (error != 0)
451 break;
452 sysctl_teardown(&ptyfs_sysctl_log);
453 break;
454 default:
455 error = ENOTTY;
456 break;
457 }
458
459 return (error);
460 }
461