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