xref: /titanic_52/usr/src/uts/common/fs/dev/sdev_ptsops.c (revision c1ecd8b9404ee0d96d93f02e82c441b9bb149a3d)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #pragma ident	"%Z%%M%	%I%	%E% SMI"
27 
28 /*
29  * vnode ops for the /dev/pts directory
30  *	The lookup is based on the internal pty table. We also
31  *	override readdir in order to delete pts nodes no longer
32  *	in use.
33  */
34 
35 #include <sys/types.h>
36 #include <sys/param.h>
37 #include <sys/sysmacros.h>
38 #include <sys/sunndi.h>
39 #include <fs/fs_subr.h>
40 #include <sys/fs/dv_node.h>
41 #include <sys/fs/sdev_impl.h>
42 #include <sys/policy.h>
43 #include <sys/ptms.h>
44 #include <sys/stat.h>
45 #include <sys/vfs_opreg.h>
46 
47 #define	DEVPTS_UID_DEFAULT	0
48 #define	DEVPTS_GID_DEFAULT	3
49 #define	DEVPTS_DEVMODE_DEFAULT	(0620)
50 
51 #define	isdigit(ch)	((ch) >= '0' && (ch) <= '9')
52 
53 static vattr_t devpts_vattr = {
54 	AT_TYPE|AT_MODE|AT_UID|AT_GID,		/* va_mask */
55 	VCHR,					/* va_type */
56 	S_IFCHR | DEVPTS_DEVMODE_DEFAULT,	/* va_mode */
57 	DEVPTS_UID_DEFAULT,			/* va_uid */
58 	DEVPTS_GID_DEFAULT,			/* va_gid */
59 	0					/* 0 hereafter */
60 };
61 
62 struct vnodeops		*devpts_vnodeops;
63 
64 struct vnodeops *
65 devpts_getvnodeops(void)
66 {
67 	return (devpts_vnodeops);
68 }
69 
70 /*
71  * Convert string to minor number. Some care must be taken
72  * as we are processing user input. Catch cases like
73  * /dev/pts/4foo and /dev/pts/-1
74  */
75 static int
76 devpts_strtol(const char *nm, minor_t *mp)
77 {
78 	long uminor = 0;
79 	char *endptr = NULL;
80 
81 	if (nm == NULL || !isdigit(*nm))
82 		return (EINVAL);
83 
84 	*mp = 0;
85 	if (ddi_strtol(nm, &endptr, 10, &uminor) != 0 ||
86 	    *endptr != '\0' || uminor < 0) {
87 		return (EINVAL);
88 	}
89 
90 	*mp = (minor_t)uminor;
91 	return (0);
92 }
93 
94 /*
95  * Check if a pts sdev_node is still valid - i.e. it represents a current pty.
96  * This serves two purposes
97  *	- only valid pts nodes are returned during lookup() and readdir().
98  *	- since pts sdev_nodes are not actively destroyed when a pty goes
99  *	  away, we use the validator to do deferred cleanup i.e. when such
100  *	  nodes are encountered during subsequent lookup() and readdir().
101  */
102 /*ARGSUSED*/
103 int
104 devpts_validate(struct sdev_node *dv)
105 {
106 	minor_t min;
107 	uid_t uid;
108 	gid_t gid;
109 	timestruc_t now;
110 	char *nm = dv->sdev_name;
111 
112 	ASSERT(!(dv->sdev_flags & SDEV_STALE));
113 	ASSERT(dv->sdev_state == SDEV_READY);
114 
115 	/* validate only READY nodes */
116 	if (dv->sdev_state != SDEV_READY) {
117 		sdcmn_err(("dev fs: skipping: node not ready %s(%p)",
118 		    nm, (void *)dv));
119 		return (SDEV_VTOR_SKIP);
120 	}
121 
122 	if (devpts_strtol(nm, &min) != 0) {
123 		sdcmn_err7(("devpts_validate: not a valid minor: %s\n", nm));
124 		return (SDEV_VTOR_INVALID);
125 	}
126 
127 	/*
128 	 * Check if pts driver is attached
129 	 */
130 	if (ptms_slave_attached() == (major_t)-1) {
131 		sdcmn_err7(("devpts_validate: slave not attached\n"));
132 		return (SDEV_VTOR_INVALID);
133 	}
134 
135 	if (ptms_minor_valid(min, &uid, &gid) == 0) {
136 		if (ptms_minor_exists(min)) {
137 			sdcmn_err7(("devpts_validate: valid in different zone "
138 			    "%s\n", nm));
139 			return (SDEV_VTOR_SKIP);
140 		} else {
141 			sdcmn_err7(("devpts_validate: %s not valid pty\n",
142 			    nm));
143 			return (SDEV_VTOR_INVALID);
144 		}
145 	}
146 
147 	ASSERT(dv->sdev_attr);
148 	if (dv->sdev_attr->va_uid != uid || dv->sdev_attr->va_gid != gid) {
149 		dv->sdev_attr->va_uid = uid;
150 		dv->sdev_attr->va_gid = gid;
151 		gethrestime(&now);
152 		dv->sdev_attr->va_atime = now;
153 		dv->sdev_attr->va_mtime = now;
154 		dv->sdev_attr->va_ctime = now;
155 		sdcmn_err7(("devpts_validate: update uid/gid/times%s\n", nm));
156 	}
157 
158 	return (SDEV_VTOR_VALID);
159 }
160 
161 /*
162  * This callback is invoked from devname_lookup_func() to create
163  * a pts entry when the node is not found in the cache.
164  */
165 /*ARGSUSED*/
166 static int
167 devpts_create_rvp(struct sdev_node *ddv, char *nm,
168     void **arg, cred_t *cred, void *whatever, char *whichever)
169 {
170 	minor_t min;
171 	major_t maj;
172 	uid_t uid;
173 	gid_t gid;
174 	timestruc_t now;
175 	struct vattr *vap = (struct vattr *)arg;
176 
177 	if (devpts_strtol(nm, &min) != 0) {
178 		sdcmn_err7(("devpts_create_rvp: not a valid minor: %s\n", nm));
179 		return (-1);
180 	}
181 
182 	/*
183 	 * Check if pts driver is attached and if it is
184 	 * get the major number.
185 	 */
186 	maj = ptms_slave_attached();
187 	if (maj == (major_t)-1) {
188 		sdcmn_err7(("devpts_create_rvp: slave not attached\n"));
189 		return (-1);
190 	}
191 
192 	/*
193 	 * Only allow creation of ptys allocated to our zone
194 	 */
195 	if (!ptms_minor_valid(min, &uid, &gid)) {
196 		sdcmn_err7(("devpts_create_rvp: %s not valid pty"
197 		    "or not valid in this zone\n", nm));
198 		return (-1);
199 	}
200 
201 
202 	/*
203 	 * This is a valid pty (at least at this point in time).
204 	 * Create the node by setting the attribute. The rest
205 	 * is taken care of by devname_lookup_func().
206 	 */
207 	*vap = devpts_vattr;
208 	vap->va_rdev = makedevice(maj, min);
209 	vap->va_uid = uid;
210 	vap->va_gid = gid;
211 	gethrestime(&now);
212 	vap->va_atime = now;
213 	vap->va_mtime = now;
214 	vap->va_ctime = now;
215 
216 	return (0);
217 }
218 
219 /*
220  * Clean pts sdev_nodes that are no longer valid.
221  */
222 static void
223 devpts_prunedir(struct sdev_node *ddv)
224 {
225 	struct vnode *vp;
226 	struct sdev_node *dv, *next = NULL;
227 	int (*vtor)(struct sdev_node *) = NULL;
228 
229 	ASSERT(ddv->sdev_flags & SDEV_VTOR);
230 
231 	vtor = (int (*)(struct sdev_node *))sdev_get_vtor(ddv);
232 	ASSERT(vtor);
233 
234 	if (rw_tryupgrade(&ddv->sdev_contents) == NULL) {
235 		rw_exit(&ddv->sdev_contents);
236 		rw_enter(&ddv->sdev_contents, RW_WRITER);
237 	}
238 
239 	for (dv = SDEV_FIRST_ENTRY(ddv); dv; dv = next) {
240 		next = SDEV_NEXT_ENTRY(ddv, dv);
241 
242 		/* validate and prune only ready nodes */
243 		if (dv->sdev_state != SDEV_READY)
244 			continue;
245 
246 		switch (vtor(dv)) {
247 		case SDEV_VTOR_VALID:
248 		case SDEV_VTOR_SKIP:
249 			continue;
250 		case SDEV_VTOR_INVALID:
251 			sdcmn_err7(("prunedir: destroy invalid "
252 			    "node: %s(%p)\n", dv->sdev_name, (void *)dv));
253 			break;
254 		}
255 		vp = SDEVTOV(dv);
256 		if (vp->v_count > 0)
257 			continue;
258 		SDEV_HOLD(dv);
259 		/* remove the cache node */
260 		(void) sdev_cache_update(ddv, &dv, dv->sdev_name,
261 		    SDEV_CACHE_DELETE);
262 	}
263 	rw_downgrade(&ddv->sdev_contents);
264 }
265 
266 /*
267  * Lookup for /dev/pts directory
268  *	If the entry does not exist, the devpts_create_rvp() callback
269  *	is invoked to create it. Nodes do not persist across reboot.
270  *
271  * There is a potential denial of service here via
272  * fattach on top of a /dev/pts node - any permission changes
273  * applied to the node, apply to the fattached file and not
274  * to the underlying pts node. As a result when the previous
275  * user fdetaches, the pts node is still owned by the previous
276  * owner. To prevent this we don't allow fattach() on top of a pts
277  * node. This is done by a modification in the namefs filesystem
278  * where we check if the underlying node has the /dev/pts vnodeops.
279  * We do this via VOP_REALVP() on the underlying specfs node.
280  * sdev_nodes currently don't have a realvp. If a realvp is ever
281  * created for sdev_nodes, then VOP_REALVP() will return the
282  * actual realvp (possibly a ufs vnode). This will defeat the check
283  * in namefs code which checks if VOP_REALVP() returns a devpts
284  * node. We add an ASSERT here in /dev/pts lookup() to check for
285  * this condition. If sdev_nodes ever get a VOP_REALVP() entry point,
286  * change the code in the namefs filesystem code (in nm_mount()) to
287  * access the realvp of the specfs node directly instead of using
288  * VOP_REALVP().
289  */
290 /*ARGSUSED3*/
291 static int
292 devpts_lookup(struct vnode *dvp, char *nm, struct vnode **vpp,
293     struct pathname *pnp, int flags, struct vnode *rdir, struct cred *cred,
294     caller_context_t *ct, int *direntflags, pathname_t *realpnp)
295 {
296 	struct sdev_node *sdvp = VTOSDEV(dvp);
297 	struct sdev_node *dv;
298 	struct vnode *rvp = NULL;
299 	int error;
300 
301 	error = devname_lookup_func(sdvp, nm, vpp, cred, devpts_create_rvp,
302 	    SDEV_VATTR);
303 
304 	if (error == 0) {
305 		switch ((*vpp)->v_type) {
306 		case VCHR:
307 			dv = VTOSDEV(VTOS(*vpp)->s_realvp);
308 			ASSERT(VOP_REALVP(SDEVTOV(dv), &rvp, NULL) == ENOSYS);
309 			break;
310 		case VDIR:
311 			dv = VTOSDEV(*vpp);
312 			break;
313 		default:
314 			cmn_err(CE_PANIC, "devpts_lookup: Unsupported node "
315 			    "type: %p: %d", (void *)(*vpp), (*vpp)->v_type);
316 			break;
317 		}
318 		ASSERT(SDEV_HELD(dv));
319 	}
320 
321 	return (error);
322 }
323 
324 /*
325  * We allow create to find existing nodes
326  *	- if the node doesn't exist - EROFS
327  *	- creating an existing dir read-only succeeds, otherwise EISDIR
328  *	- exclusive creates fail - EEXIST
329  */
330 /*ARGSUSED2*/
331 static int
332 devpts_create(struct vnode *dvp, char *nm, struct vattr *vap, vcexcl_t excl,
333     int mode, struct vnode **vpp, struct cred *cred, int flag,
334     caller_context_t *ct, vsecattr_t *vsecp)
335 {
336 	int error;
337 	struct vnode *vp;
338 
339 	*vpp = NULL;
340 
341 	error = devpts_lookup(dvp, nm, &vp, NULL, 0, NULL, cred, ct, NULL,
342 	    NULL);
343 	if (error == 0) {
344 		if (excl == EXCL)
345 			error = EEXIST;
346 		else if (vp->v_type == VDIR && (mode & VWRITE))
347 			error = EISDIR;
348 		else
349 			error = VOP_ACCESS(vp, mode, 0, cred, ct);
350 
351 		if (error) {
352 			VN_RELE(vp);
353 		} else
354 			*vpp = vp;
355 	} else if (error == ENOENT) {
356 		error = EROFS;
357 	}
358 
359 	return (error);
360 }
361 
362 /*
363  * Display all instantiated pts (slave) device nodes.
364  * A /dev/pts entry will be created only after the first lookup of the slave
365  * device succeeds.
366  */
367 /*ARGSUSED4*/
368 static int
369 devpts_readdir(struct vnode *dvp, struct uio *uiop, struct cred *cred,
370     int *eofp, caller_context_t *ct, int flags)
371 {
372 	struct sdev_node *sdvp = VTOSDEV(dvp);
373 	if (uiop->uio_offset == 0) {
374 		devpts_prunedir(sdvp);
375 	}
376 
377 	return (devname_readdir_func(dvp, uiop, cred, eofp, 0));
378 }
379 
380 
381 static int
382 devpts_set_id(struct sdev_node *dv, struct vattr *vap, int protocol)
383 {
384 	ASSERT((protocol & AT_UID) || (protocol & AT_GID));
385 	ptms_set_owner(getminor(SDEVTOV(dv)->v_rdev),
386 	    vap->va_uid, vap->va_gid);
387 	return (0);
388 
389 }
390 
391 /*ARGSUSED4*/
392 static int
393 devpts_setattr(struct vnode *vp, struct vattr *vap, int flags,
394     struct cred *cred, caller_context_t *ctp)
395 {
396 	ASSERT((vp->v_type == VCHR) || (vp->v_type == VDIR));
397 	return (devname_setattr_func(vp, vap, flags, cred,
398 	    devpts_set_id, AT_UID|AT_GID));
399 }
400 
401 
402 /*
403  * We override lookup and readdir to build entries based on the
404  * in kernel pty table. Also override setattr/setsecattr to
405  * avoid persisting permissions.
406  */
407 const fs_operation_def_t devpts_vnodeops_tbl[] = {
408 	VOPNAME_READDIR,	{ .vop_readdir = devpts_readdir },
409 	VOPNAME_LOOKUP,		{ .vop_lookup = devpts_lookup },
410 	VOPNAME_CREATE,		{ .vop_create = devpts_create },
411 	VOPNAME_SETATTR,	{ .vop_setattr = devpts_setattr },
412 	VOPNAME_REMOVE,		{ .error = fs_nosys },
413 	VOPNAME_MKDIR,		{ .error = fs_nosys },
414 	VOPNAME_RMDIR,		{ .error = fs_nosys },
415 	VOPNAME_SYMLINK,	{ .error = fs_nosys },
416 	VOPNAME_SETSECATTR,	{ .error = fs_nosys },
417 	NULL,			NULL
418 };
419