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