Print this page
curzone reality check and teardown changes to use the RIGHT zone
| Split |
Close |
| Expand all |
| Collapse all |
--- old/usr/src/uts/common/fs/nfs/nfs_auth.c
+++ new/usr/src/uts/common/fs/nfs/nfs_auth.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 /*
23 23 * Copyright (c) 1995, 2010, Oracle and/or its affiliates. All rights reserved.
24 24 * Copyright (c) 2015 by Delphix. All rights reserved.
25 25 * Copyright 2018 Nexenta Systems, Inc. All rights reserved.
26 26 */
27 27
28 28 #include <sys/param.h>
29 29 #include <sys/errno.h>
30 30 #include <sys/vfs.h>
31 31 #include <sys/vnode.h>
32 32 #include <sys/cred.h>
33 33 #include <sys/cmn_err.h>
34 34 #include <sys/systm.h>
35 35 #include <sys/kmem.h>
36 36 #include <sys/pathname.h>
37 37 #include <sys/utsname.h>
38 38 #include <sys/debug.h>
39 39 #include <sys/door.h>
40 40 #include <sys/sdt.h>
41 41 #include <sys/thread.h>
42 42 #include <sys/avl.h>
43 43
44 44 #include <rpc/types.h>
45 45 #include <rpc/auth.h>
46 46 #include <rpc/clnt.h>
47 47
48 48 #include <nfs/nfs.h>
49 49 #include <nfs/export.h>
50 50 #include <nfs/nfs_clnt.h>
51 51 #include <nfs/auth.h>
52 52
53 53 static struct kmem_cache *exi_cache_handle;
54 54 static void exi_cache_reclaim(void *);
55 55 static void exi_cache_trim(struct exportinfo *exi);
56 56 static void *nfsauth_zone_init(zoneid_t);
57 57 static void nfsauth_zone_shutdown(zoneid_t zoneid, void *data);
58 58 static void nfsauth_zone_fini(zoneid_t, void *);
59 59
60 60 extern pri_t minclsyspri;
61 61
62 62 /* NFS auth cache statistics */
63 63 volatile uint_t nfsauth_cache_hit;
64 64 volatile uint_t nfsauth_cache_miss;
65 65 volatile uint_t nfsauth_cache_refresh;
66 66 volatile uint_t nfsauth_cache_reclaim;
67 67 volatile uint_t exi_cache_auth_reclaim_failed;
68 68 volatile uint_t exi_cache_clnt_reclaim_failed;
69 69
70 70 /*
71 71 * The lifetime of an auth cache entry:
72 72 * ------------------------------------
73 73 *
74 74 * An auth cache entry is created with both the auth_time
75 75 * and auth_freshness times set to the current time.
76 76 *
77 77 * Upon every client access which results in a hit, the
78 78 * auth_time will be updated.
79 79 *
80 80 * If a client access determines that the auth_freshness
81 81 * indicates that the entry is STALE, then it will be
82 82 * refreshed. Note that this will explicitly reset
83 83 * auth_time.
84 84 *
85 85 * When the REFRESH successfully occurs, then the
86 86 * auth_freshness is updated.
87 87 *
88 88 * There are two ways for an entry to leave the cache:
89 89 *
90 90 * 1) Purged by an action on the export (remove or changed)
91 91 * 2) Memory backpressure from the kernel (check against NFSAUTH_CACHE_TRIM)
92 92 *
93 93 * For 2) we check the timeout value against auth_time.
94 94 */
95 95
96 96 /*
97 97 * Number of seconds until we mark for refresh an auth cache entry.
98 98 */
99 99 #define NFSAUTH_CACHE_REFRESH 600
100 100
101 101 /*
102 102 * Number of idle seconds until we yield to backpressure
103 103 * to trim a cache entry.
104 104 */
105 105 #define NFSAUTH_CACHE_TRIM 3600
106 106
107 107 /*
108 108 * While we could encapuslate the exi_list inside the
109 109 * exi structure, we can't do that for the auth_list.
110 110 * So, to keep things looking clean, we keep them both
111 111 * in these external lists.
112 112 */
113 113 typedef struct refreshq_exi_node {
114 114 struct exportinfo *ren_exi;
115 115 list_t ren_authlist;
116 116 list_node_t ren_node;
117 117 } refreshq_exi_node_t;
118 118
119 119 typedef struct refreshq_auth_node {
120 120 struct auth_cache *ran_auth;
121 121 char *ran_netid;
122 122 list_node_t ran_node;
123 123 } refreshq_auth_node_t;
124 124
125 125 /*
126 126 * Used to manipulate things on the refreshq_queue. Note that the refresh
127 127 * thread will effectively pop a node off of the queue, at which point it
128 128 * will no longer need to hold the mutex.
129 129 */
130 130 static kmutex_t refreshq_lock;
131 131 static list_t refreshq_queue;
132 132 static kcondvar_t refreshq_cv;
133 133
134 134 /*
135 135 * If there is ever a problem with loading the module, then nfsauth_fini()
136 136 * needs to be called to remove state. In that event, since the refreshq
137 137 * thread has been started, they need to work together to get rid of state.
138 138 */
139 139 typedef enum nfsauth_refreshq_thread_state {
140 140 REFRESHQ_THREAD_RUNNING,
141 141 REFRESHQ_THREAD_FINI_REQ,
142 142 REFRESHQ_THREAD_HALTED,
143 143 REFRESHQ_THREAD_NEED_CREATE
144 144 } nfsauth_refreshq_thread_state_t;
145 145
146 146 typedef struct nfsauth_globals {
147 147 kmutex_t mountd_lock;
148 148 door_handle_t mountd_dh;
149 149
150 150 /*
151 151 * Used to manipulate things on the refreshq_queue. Note that the
152 152 * refresh thread will effectively pop a node off of the queue,
153 153 * at which point it will no longer need to hold the mutex.
154 154 */
155 155 kmutex_t refreshq_lock;
156 156 list_t refreshq_queue;
157 157 kcondvar_t refreshq_cv;
158 158
159 159 /*
160 160 * A list_t would be overkill. These are auth_cache entries which are
161 161 * no longer linked to an exi. It should be the case that all of their
162 162 * states are NFS_AUTH_INVALID, i.e., the only way to be put on this
163 163 * list is iff their state indicated that they had been placed on the
164 164 * refreshq_queue.
165 165 *
166 166 * Note that while there is no link from the exi or back to the exi,
167 167 * the exi can not go away until these entries are harvested.
168 168 */
169 169 struct auth_cache *refreshq_dead_entries;
170 170 nfsauth_refreshq_thread_state_t refreshq_thread_state;
171 171
172 172 } nfsauth_globals_t;
173 173
174 174 static void nfsauth_free_node(struct auth_cache *);
175 175 static void nfsauth_refresh_thread(nfsauth_globals_t *);
176 176
177 177 static int nfsauth_cache_compar(const void *, const void *);
178 178
179 179 static zone_key_t nfsauth_zone_key;
180 180
181 181 void
182 182 mountd_args(uint_t did)
183 183 {
184 184 nfsauth_globals_t *nag;
185 185
186 186 nag = zone_getspecific(nfsauth_zone_key, curzone);
187 187 mutex_enter(&nag->mountd_lock);
188 188 if (nag->mountd_dh != NULL)
189 189 door_ki_rele(nag->mountd_dh);
190 190 nag->mountd_dh = door_ki_lookup(did);
191 191 mutex_exit(&nag->mountd_lock);
192 192 }
193 193
194 194 void
195 195 nfsauth_init(void)
196 196 {
197 197 zone_key_create(&nfsauth_zone_key, nfsauth_zone_init,
198 198 nfsauth_zone_shutdown, nfsauth_zone_fini);
199 199
200 200 exi_cache_handle = kmem_cache_create("exi_cache_handle",
201 201 sizeof (struct auth_cache), 0, NULL, NULL,
202 202 exi_cache_reclaim, NULL, NULL, 0);
203 203 }
204 204
205 205 void
206 206 nfsauth_fini(void)
207 207 {
208 208 kmem_cache_destroy(exi_cache_handle);
209 209 }
210 210
211 211 /*ARGSUSED*/
212 212 static void *
213 213 nfsauth_zone_init(zoneid_t zoneid)
214 214 {
215 215 nfsauth_globals_t *nag;
216 216
217 217 nag = kmem_zalloc(sizeof (*nag), KM_SLEEP);
218 218
219 219 /*
220 220 * mountd can be restarted by smf(5). We need to make sure
221 221 * the updated door handle will safely make it to mountd_dh.
222 222 */
223 223 mutex_init(&nag->mountd_lock, NULL, MUTEX_DEFAULT, NULL);
224 224 mutex_init(&nag->refreshq_lock, NULL, MUTEX_DEFAULT, NULL);
225 225 list_create(&nag->refreshq_queue, sizeof (refreshq_exi_node_t),
226 226 offsetof(refreshq_exi_node_t, ren_node));
227 227 cv_init(&nag->refreshq_cv, NULL, CV_DEFAULT, NULL);
228 228 nag->refreshq_thread_state = REFRESHQ_THREAD_NEED_CREATE;
229 229
230 230 return (nag);
231 231 }
232 232
233 233 /*ARGSUSED*/
234 234 static void
235 235 nfsauth_zone_shutdown(zoneid_t zoneid, void *data)
236 236 {
237 237 refreshq_exi_node_t *ren;
238 238 nfsauth_globals_t *nag = data;
239 239
240 240 /* Prevent the nfsauth_refresh_thread from getting new work */
241 241 mutex_enter(&nag->refreshq_lock);
242 242 if (nag->refreshq_thread_state == REFRESHQ_THREAD_RUNNING) {
243 243 nag->refreshq_thread_state = REFRESHQ_THREAD_FINI_REQ;
244 244 cv_broadcast(&nag->refreshq_cv);
245 245
246 246 /* Wait for nfsauth_refresh_thread() to exit */
247 247 while (nag->refreshq_thread_state != REFRESHQ_THREAD_HALTED)
248 248 cv_wait(&nag->refreshq_cv, &nag->refreshq_lock);
249 249 }
250 250 mutex_exit(&nag->refreshq_lock);
251 251
252 252 /*
253 253 * Walk the exi_list and in turn, walk the auth_lists and free all
254 254 * lists. In addition, free INVALID auth_cache entries.
255 255 */
256 256 while ((ren = list_remove_head(&nag->refreshq_queue))) {
257 257 refreshq_auth_node_t *ran;
258 258
259 259 while ((ran = list_remove_head(&ren->ren_authlist)) != NULL) {
260 260 struct auth_cache *p = ran->ran_auth;
261 261 if (p->auth_state == NFS_AUTH_INVALID)
262 262 nfsauth_free_node(p);
263 263 strfree(ran->ran_netid);
264 264 kmem_free(ran, sizeof (*ran));
265 265 }
266 266
267 267 list_destroy(&ren->ren_authlist);
268 268 exi_rele(ren->ren_exi);
269 269 kmem_free(ren, sizeof (*ren));
270 270 }
271 271 }
272 272
273 273 /*ARGSUSED*/
274 274 static void
275 275 nfsauth_zone_fini(zoneid_t zoneid, void *data)
276 276 {
277 277 nfsauth_globals_t *nag = data;
278 278
279 279 list_destroy(&nag->refreshq_queue);
280 280 cv_destroy(&nag->refreshq_cv);
281 281 mutex_destroy(&nag->refreshq_lock);
282 282 mutex_destroy(&nag->mountd_lock);
283 283 kmem_free(nag, sizeof (*nag));
284 284 }
285 285
286 286 /*
287 287 * Convert the address in a netbuf to
288 288 * a hash index for the auth_cache table.
289 289 */
290 290 static int
291 291 hash(struct netbuf *a)
292 292 {
293 293 int i, h = 0;
294 294
295 295 for (i = 0; i < a->len; i++)
296 296 h ^= a->buf[i];
297 297
298 298 return (h & (AUTH_TABLESIZE - 1));
299 299 }
300 300
301 301 /*
302 302 * Mask out the components of an
303 303 * address that do not identify
304 304 * a host. For socket addresses the
305 305 * masking gets rid of the port number.
306 306 */
307 307 static void
308 308 addrmask(struct netbuf *addr, struct netbuf *mask)
309 309 {
310 310 int i;
311 311
312 312 for (i = 0; i < addr->len; i++)
313 313 addr->buf[i] &= mask->buf[i];
314 314 }
315 315
316 316 /*
317 317 * nfsauth4_access is used for NFS V4 auth checking. Besides doing
318 318 * the common nfsauth_access(), it will check if the client can
319 319 * have a limited access to this vnode even if the security flavor
320 320 * used does not meet the policy.
321 321 */
322 322 int
323 323 nfsauth4_access(struct exportinfo *exi, vnode_t *vp, struct svc_req *req,
324 324 cred_t *cr, uid_t *uid, gid_t *gid, uint_t *ngids, gid_t **gids)
325 325 {
326 326 int access;
327 327
328 328 access = nfsauth_access(exi, req, cr, uid, gid, ngids, gids);
329 329
330 330 /*
331 331 * There are cases that the server needs to allow the client
332 332 * to have a limited view.
333 333 *
334 334 * e.g.
335 335 * /export is shared as "sec=sys,rw=dfs-test-4,sec=krb5,rw"
336 336 * /export/home is shared as "sec=sys,rw"
337 337 *
338 338 * When the client mounts /export with sec=sys, the client
339 339 * would get a limited view with RO access on /export to see
340 340 * "home" only because the client is allowed to access
341 341 * /export/home with auth_sys.
342 342 */
343 343 if (access & NFSAUTH_DENIED || access & NFSAUTH_WRONGSEC) {
344 344 /*
345 345 * Allow ro permission with LIMITED view if there is a
346 346 * sub-dir exported under vp.
347 347 */
348 348 if (has_visible(exi, vp))
349 349 return (NFSAUTH_LIMITED);
350 350 }
351 351
352 352 return (access);
353 353 }
354 354
355 355 static void
356 356 sys_log(const char *msg)
357 357 {
358 358 static time_t tstamp = 0;
359 359 time_t now;
360 360
361 361 /*
362 362 * msg is shown (at most) once per minute
363 363 */
364 364 now = gethrestime_sec();
365 365 if ((tstamp + 60) < now) {
366 366 tstamp = now;
367 367 cmn_err(CE_WARN, msg);
368 368 }
369 369 }
370 370
371 371 /*
372 372 * Callup to the mountd to get access information in the kernel.
373 373 */
374 374 static bool_t
375 375 nfsauth_retrieve(nfsauth_globals_t *nag, struct exportinfo *exi,
376 376 char *req_netid, int flavor, struct netbuf *addr, int *access,
377 377 cred_t *clnt_cred, uid_t *srv_uid, gid_t *srv_gid, uint_t *srv_gids_cnt,
378 378 gid_t **srv_gids)
379 379 {
380 380 varg_t varg = {0};
381 381 nfsauth_res_t res = {0};
382 382 XDR xdrs;
383 383 size_t absz;
384 384 caddr_t abuf;
385 385 int last = 0;
386 386 door_arg_t da;
387 387 door_info_t di;
388 388 door_handle_t dh;
389 389 uint_t ntries = 0;
390 390
391 391 /*
392 392 * No entry in the cache for this client/flavor
393 393 * so we need to call the nfsauth service in the
394 394 * mount daemon.
395 395 */
396 396
397 397 varg.vers = V_PROTO;
398 398 varg.arg_u.arg.cmd = NFSAUTH_ACCESS;
399 399 varg.arg_u.arg.areq.req_client.n_len = addr->len;
400 400 varg.arg_u.arg.areq.req_client.n_bytes = addr->buf;
401 401 varg.arg_u.arg.areq.req_netid = req_netid;
402 402 varg.arg_u.arg.areq.req_path = exi->exi_export.ex_path;
403 403 varg.arg_u.arg.areq.req_flavor = flavor;
404 404 varg.arg_u.arg.areq.req_clnt_uid = crgetuid(clnt_cred);
405 405 varg.arg_u.arg.areq.req_clnt_gid = crgetgid(clnt_cred);
406 406 varg.arg_u.arg.areq.req_clnt_gids.len = crgetngroups(clnt_cred);
407 407 varg.arg_u.arg.areq.req_clnt_gids.val = (gid_t *)crgetgroups(clnt_cred);
408 408
409 409 DTRACE_PROBE1(nfsserv__func__nfsauth__varg, varg_t *, &varg);
410 410
411 411 /*
412 412 * Setup the XDR stream for encoding the arguments. Notice that
413 413 * in addition to the args having variable fields (req_netid and
414 414 * req_path), the argument data structure is itself versioned,
415 415 * so we need to make sure we can size the arguments buffer
416 416 * appropriately to encode all the args. If we can't get sizing
417 417 * info _or_ properly encode the arguments, there's really no
418 418 * point in continuting, so we fail the request.
419 419 */
420 420 if ((absz = xdr_sizeof(xdr_varg, &varg)) == 0) {
421 421 *access = NFSAUTH_DENIED;
422 422 return (FALSE);
423 423 }
424 424
425 425 abuf = (caddr_t)kmem_alloc(absz, KM_SLEEP);
426 426 xdrmem_create(&xdrs, abuf, absz, XDR_ENCODE);
427 427 if (!xdr_varg(&xdrs, &varg)) {
428 428 XDR_DESTROY(&xdrs);
429 429 goto fail;
430 430 }
431 431 XDR_DESTROY(&xdrs);
432 432
433 433 /*
434 434 * Prepare the door arguments
435 435 *
436 436 * We don't know the size of the message the daemon
437 437 * will pass back to us. By setting rbuf to NULL,
438 438 * we force the door code to allocate a buf of the
439 439 * appropriate size. We must set rsize > 0, however,
440 440 * else the door code acts as if no response was
441 441 * expected and doesn't pass the data to us.
442 442 */
443 443 da.data_ptr = (char *)abuf;
444 444 da.data_size = absz;
445 445 da.desc_ptr = NULL;
446 446 da.desc_num = 0;
447 447 da.rbuf = NULL;
448 448 da.rsize = 1;
449 449
450 450 retry:
451 451 mutex_enter(&nag->mountd_lock);
452 452 dh = nag->mountd_dh;
453 453 if (dh != NULL)
454 454 door_ki_hold(dh);
455 455 mutex_exit(&nag->mountd_lock);
456 456
457 457 if (dh == NULL) {
458 458 /*
459 459 * The rendezvous point has not been established yet!
460 460 * This could mean that either mountd(1m) has not yet
461 461 * been started or that _this_ routine nuked the door
462 462 * handle after receiving an EINTR for a REVOKED door.
463 463 *
464 464 * Returning NFSAUTH_DROP will cause the NFS client
465 465 * to retransmit the request, so let's try to be more
466 466 * rescillient and attempt for ntries before we bail.
467 467 */
468 468 if (++ntries % NFSAUTH_DR_TRYCNT) {
469 469 delay(hz);
470 470 goto retry;
471 471 }
472 472
473 473 kmem_free(abuf, absz);
474 474
475 475 sys_log("nfsauth: mountd has not established door");
476 476 *access = NFSAUTH_DROP;
477 477 return (FALSE);
478 478 }
479 479
480 480 ntries = 0;
481 481
482 482 /*
483 483 * Now that we've got what we need, place the call.
484 484 */
485 485 switch (door_ki_upcall_limited(dh, &da, NULL, SIZE_MAX, 0)) {
486 486 case 0: /* Success */
487 487 door_ki_rele(dh);
488 488
489 489 if (da.data_ptr == NULL && da.data_size == 0) {
490 490 /*
491 491 * The door_return that contained the data
492 492 * failed! We're here because of the 2nd
493 493 * door_return (w/o data) such that we can
494 494 * get control of the thread (and exit
495 495 * gracefully).
496 496 */
497 497 DTRACE_PROBE1(nfsserv__func__nfsauth__door__nil,
498 498 door_arg_t *, &da);
499 499 goto fail;
500 500 }
501 501
502 502 break;
503 503
504 504 case EAGAIN:
505 505 /*
506 506 * Server out of resources; back off for a bit
507 507 */
508 508 door_ki_rele(dh);
509 509 delay(hz);
510 510 goto retry;
511 511 /* NOTREACHED */
512 512
513 513 case EINTR:
514 514 if (!door_ki_info(dh, &di)) {
515 515 door_ki_rele(dh);
516 516
517 517 if (di.di_attributes & DOOR_REVOKED) {
518 518 /*
519 519 * The server barfed and revoked
520 520 * the (existing) door on us; we
521 521 * want to wait to give smf(5) a
522 522 * chance to restart mountd(1m)
523 523 * and establish a new door handle.
524 524 */
525 525 mutex_enter(&nag->mountd_lock);
526 526 if (dh == nag->mountd_dh) {
527 527 door_ki_rele(nag->mountd_dh);
528 528 nag->mountd_dh = NULL;
529 529 }
530 530 mutex_exit(&nag->mountd_lock);
531 531 delay(hz);
532 532 goto retry;
533 533 }
534 534 /*
535 535 * If the door was _not_ revoked on us,
536 536 * then more than likely we took an INTR,
537 537 * so we need to fail the operation.
538 538 */
539 539 goto fail;
540 540 }
541 541 /*
542 542 * The only failure that can occur from getting
543 543 * the door info is EINVAL, so we let the code
544 544 * below handle it.
545 545 */
546 546 /* FALLTHROUGH */
547 547
548 548 case EBADF:
549 549 case EINVAL:
550 550 default:
551 551 /*
552 552 * If we have a stale door handle, give smf a last
553 553 * chance to start it by sleeping for a little bit.
554 554 * If we're still hosed, we'll fail the call.
555 555 *
556 556 * Since we're going to reacquire the door handle
557 557 * upon the retry, we opt to sleep for a bit and
558 558 * _not_ to clear mountd_dh. If mountd restarted
559 559 * and was able to set mountd_dh, we should see
560 560 * the new instance; if not, we won't get caught
561 561 * up in the retry/DELAY loop.
562 562 */
563 563 door_ki_rele(dh);
564 564 if (!last) {
565 565 delay(hz);
566 566 last++;
567 567 goto retry;
568 568 }
569 569 sys_log("nfsauth: stale mountd door handle");
570 570 goto fail;
571 571 }
572 572
573 573 ASSERT(da.rbuf != NULL);
574 574
575 575 /*
576 576 * No door errors encountered; setup the XDR stream for decoding
577 577 * the results. If we fail to decode the results, we've got no
578 578 * other recourse than to fail the request.
579 579 */
580 580 xdrmem_create(&xdrs, da.rbuf, da.rsize, XDR_DECODE);
581 581 if (!xdr_nfsauth_res(&xdrs, &res)) {
582 582 xdr_free(xdr_nfsauth_res, (char *)&res);
583 583 XDR_DESTROY(&xdrs);
584 584 kmem_free(da.rbuf, da.rsize);
585 585 goto fail;
586 586 }
587 587 XDR_DESTROY(&xdrs);
588 588 kmem_free(da.rbuf, da.rsize);
589 589
590 590 DTRACE_PROBE1(nfsserv__func__nfsauth__results, nfsauth_res_t *, &res);
591 591 switch (res.stat) {
592 592 case NFSAUTH_DR_OKAY:
593 593 *access = res.ares.auth_perm;
594 594 *srv_uid = res.ares.auth_srv_uid;
595 595 *srv_gid = res.ares.auth_srv_gid;
596 596 *srv_gids_cnt = res.ares.auth_srv_gids.len;
597 597 *srv_gids = kmem_alloc(*srv_gids_cnt * sizeof (gid_t),
598 598 KM_SLEEP);
599 599 bcopy(res.ares.auth_srv_gids.val, *srv_gids,
600 600 *srv_gids_cnt * sizeof (gid_t));
601 601 break;
602 602
603 603 case NFSAUTH_DR_EFAIL:
604 604 case NFSAUTH_DR_DECERR:
605 605 case NFSAUTH_DR_BADCMD:
606 606 default:
607 607 xdr_free(xdr_nfsauth_res, (char *)&res);
608 608 fail:
609 609 *access = NFSAUTH_DENIED;
610 610 kmem_free(abuf, absz);
611 611 return (FALSE);
612 612 /* NOTREACHED */
613 613 }
614 614
615 615 xdr_free(xdr_nfsauth_res, (char *)&res);
616 616 kmem_free(abuf, absz);
617 617
618 618 return (TRUE);
619 619 }
620 620
621 621 static void
622 622 nfsauth_refresh_thread(nfsauth_globals_t *nag)
623 623 {
624 624 refreshq_exi_node_t *ren;
625 625 refreshq_auth_node_t *ran;
626 626
627 627 struct exportinfo *exi;
628 628
629 629 int access;
630 630 bool_t retrieval;
631 631
632 632 callb_cpr_t cprinfo;
633 633
634 634 CALLB_CPR_INIT(&cprinfo, &nag->refreshq_lock, callb_generic_cpr,
635 635 "nfsauth_refresh");
636 636
637 637 for (;;) {
638 638 mutex_enter(&nag->refreshq_lock);
639 639 if (nag->refreshq_thread_state != REFRESHQ_THREAD_RUNNING) {
640 640 /* Keep the hold on the lock! */
641 641 break;
642 642 }
643 643
644 644 ren = list_remove_head(&nag->refreshq_queue);
645 645 if (ren == NULL) {
646 646 CALLB_CPR_SAFE_BEGIN(&cprinfo);
647 647 cv_wait(&nag->refreshq_cv, &nag->refreshq_lock);
648 648 CALLB_CPR_SAFE_END(&cprinfo, &nag->refreshq_lock);
649 649 mutex_exit(&nag->refreshq_lock);
650 650 continue;
651 651 }
652 652 mutex_exit(&nag->refreshq_lock);
653 653
654 654 exi = ren->ren_exi;
655 655 ASSERT(exi != NULL);
656 656
657 657 /*
658 658 * Since the ren was removed from the refreshq_queue above,
659 659 * this is the only thread aware about the ren existence, so we
660 660 * have the exclusive ownership of it and we do not need to
661 661 * protect it by any lock.
662 662 */
663 663 while ((ran = list_remove_head(&ren->ren_authlist))) {
664 664 uid_t uid;
665 665 gid_t gid;
666 666 uint_t ngids;
667 667 gid_t *gids;
668 668 struct auth_cache *p = ran->ran_auth;
669 669 char *netid = ran->ran_netid;
670 670
671 671 ASSERT(p != NULL);
672 672 ASSERT(netid != NULL);
673 673
674 674 kmem_free(ran, sizeof (refreshq_auth_node_t));
675 675
676 676 mutex_enter(&p->auth_lock);
677 677
678 678 /*
679 679 * Once the entry goes INVALID, it can not change
680 680 * state.
681 681 *
682 682 * No need to refresh entries also in a case we are
683 683 * just shutting down.
684 684 *
685 685 * In general, there is no need to hold the
686 686 * refreshq_lock to test the refreshq_thread_state. We
687 687 * do hold it at other places because there is some
688 688 * related thread synchronization (or some other tasks)
689 689 * close to the refreshq_thread_state check.
690 690 *
691 691 * The check for the refreshq_thread_state value here
692 692 * is purely advisory to allow the faster
693 693 * nfsauth_refresh_thread() shutdown. In a case we
694 694 * will miss such advisory, nothing catastrophic
695 695 * happens: we will just spin longer here before the
696 696 * shutdown.
697 697 */
698 698 if (p->auth_state == NFS_AUTH_INVALID ||
699 699 nag->refreshq_thread_state !=
700 700 REFRESHQ_THREAD_RUNNING) {
701 701 mutex_exit(&p->auth_lock);
702 702
703 703 if (p->auth_state == NFS_AUTH_INVALID)
704 704 nfsauth_free_node(p);
705 705
706 706 strfree(netid);
707 707
708 708 continue;
709 709 }
710 710
711 711 /*
712 712 * Make sure the state is valid. Note that once we
713 713 * change the state to NFS_AUTH_REFRESHING, no other
714 714 * thread will be able to work on this entry.
715 715 */
716 716 ASSERT(p->auth_state == NFS_AUTH_STALE);
717 717
718 718 p->auth_state = NFS_AUTH_REFRESHING;
719 719 mutex_exit(&p->auth_lock);
720 720
721 721 DTRACE_PROBE2(nfsauth__debug__cache__refresh,
722 722 struct exportinfo *, exi,
723 723 struct auth_cache *, p);
724 724
725 725 /*
726 726 * The first caching of the access rights
727 727 * is done with the netid pulled out of the
728 728 * request from the client. All subsequent
729 729 * users of the cache may or may not have
730 730 * the same netid. It doesn't matter. So
731 731 * when we refresh, we simply use the netid
732 732 * of the request which triggered the
733 733 * refresh attempt.
734 734 */
735 735 retrieval = nfsauth_retrieve(nag, exi, netid,
736 736 p->auth_flavor, &p->auth_clnt->authc_addr, &access,
737 737 p->auth_clnt_cred, &uid, &gid, &ngids, &gids);
738 738
739 739 /*
740 740 * This can only be set in one other place
741 741 * and the state has to be NFS_AUTH_FRESH.
742 742 */
743 743 strfree(netid);
744 744
745 745 mutex_enter(&p->auth_lock);
746 746 if (p->auth_state == NFS_AUTH_INVALID) {
747 747 mutex_exit(&p->auth_lock);
748 748 nfsauth_free_node(p);
749 749 if (retrieval == TRUE)
750 750 kmem_free(gids, ngids * sizeof (gid_t));
751 751 } else {
752 752 /*
753 753 * If we got an error, do not reset the
754 754 * time. This will cause the next access
755 755 * check for the client to reschedule this
756 756 * node.
757 757 */
758 758 if (retrieval == TRUE) {
759 759 p->auth_access = access;
760 760
761 761 p->auth_srv_uid = uid;
762 762 p->auth_srv_gid = gid;
763 763 kmem_free(p->auth_srv_gids,
764 764 p->auth_srv_ngids * sizeof (gid_t));
765 765 p->auth_srv_ngids = ngids;
766 766 p->auth_srv_gids = gids;
767 767
768 768 p->auth_freshness = gethrestime_sec();
769 769 }
770 770 p->auth_state = NFS_AUTH_FRESH;
771 771
772 772 cv_broadcast(&p->auth_cv);
773 773 mutex_exit(&p->auth_lock);
774 774 }
775 775 }
776 776
777 777 list_destroy(&ren->ren_authlist);
778 778 exi_rele(ren->ren_exi);
779 779 kmem_free(ren, sizeof (refreshq_exi_node_t));
780 780 }
781 781
782 782 nag->refreshq_thread_state = REFRESHQ_THREAD_HALTED;
783 783 cv_broadcast(&nag->refreshq_cv);
784 784 CALLB_CPR_EXIT(&cprinfo);
785 785 DTRACE_PROBE(nfsauth__nfsauth__refresh__thread__exit);
786 786 zthread_exit();
787 787 }
788 788
789 789 int
790 790 nfsauth_cache_clnt_compar(const void *v1, const void *v2)
791 791 {
792 792 int c;
793 793
794 794 const struct auth_cache_clnt *a1 = (const struct auth_cache_clnt *)v1;
795 795 const struct auth_cache_clnt *a2 = (const struct auth_cache_clnt *)v2;
796 796
797 797 if (a1->authc_addr.len < a2->authc_addr.len)
798 798 return (-1);
799 799 if (a1->authc_addr.len > a2->authc_addr.len)
800 800 return (1);
801 801
802 802 c = memcmp(a1->authc_addr.buf, a2->authc_addr.buf, a1->authc_addr.len);
803 803 if (c < 0)
804 804 return (-1);
805 805 if (c > 0)
806 806 return (1);
807 807
808 808 return (0);
809 809 }
810 810
811 811 static int
812 812 nfsauth_cache_compar(const void *v1, const void *v2)
813 813 {
814 814 int c;
815 815
816 816 const struct auth_cache *a1 = (const struct auth_cache *)v1;
817 817 const struct auth_cache *a2 = (const struct auth_cache *)v2;
818 818
819 819 if (a1->auth_flavor < a2->auth_flavor)
820 820 return (-1);
821 821 if (a1->auth_flavor > a2->auth_flavor)
822 822 return (1);
823 823
824 824 if (crgetuid(a1->auth_clnt_cred) < crgetuid(a2->auth_clnt_cred))
825 825 return (-1);
826 826 if (crgetuid(a1->auth_clnt_cred) > crgetuid(a2->auth_clnt_cred))
827 827 return (1);
828 828
829 829 if (crgetgid(a1->auth_clnt_cred) < crgetgid(a2->auth_clnt_cred))
830 830 return (-1);
831 831 if (crgetgid(a1->auth_clnt_cred) > crgetgid(a2->auth_clnt_cred))
832 832 return (1);
833 833
834 834 if (crgetngroups(a1->auth_clnt_cred) < crgetngroups(a2->auth_clnt_cred))
835 835 return (-1);
836 836 if (crgetngroups(a1->auth_clnt_cred) > crgetngroups(a2->auth_clnt_cred))
837 837 return (1);
838 838
839 839 c = memcmp(crgetgroups(a1->auth_clnt_cred),
840 840 crgetgroups(a2->auth_clnt_cred), crgetngroups(a1->auth_clnt_cred));
841 841 if (c < 0)
842 842 return (-1);
843 843 if (c > 0)
844 844 return (1);
845 845
846 846 return (0);
847 847 }
848 848
849 849 /*
850 850 * Get the access information from the cache or callup to the mountd
851 851 * to get and cache the access information in the kernel.
852 852 */
853 853 static int
854 854 nfsauth_cache_get(struct exportinfo *exi, struct svc_req *req, int flavor,
855 855 cred_t *cr, uid_t *uid, gid_t *gid, uint_t *ngids, gid_t **gids)
856 856 {
857 857 nfsauth_globals_t *nag;
858 858 struct netbuf *taddrmask;
859 859 struct netbuf addr; /* temporary copy of client's address */
860 860 const struct netbuf *claddr;
861 861 avl_tree_t *tree;
862 862 struct auth_cache ac; /* used as a template for avl_find() */
863 863 struct auth_cache_clnt *c;
864 864 struct auth_cache_clnt acc; /* used as a template for avl_find() */
865 865 struct auth_cache *p = NULL;
866 866 int access;
|
↓ open down ↓ |
866 lines elided |
↑ open up ↑ |
867 867
868 868 uid_t tmpuid;
869 869 gid_t tmpgid;
870 870 uint_t tmpngids;
871 871 gid_t *tmpgids;
872 872
873 873 avl_index_t where; /* used for avl_find()/avl_insert() */
874 874
875 875 ASSERT(cr != NULL);
876 876
877 + ASSERT3P(curzone, ==, exi->exi_zone);
877 878 nag = zone_getspecific(nfsauth_zone_key, curzone);
878 879
879 880 /*
880 881 * Now check whether this client already
881 882 * has an entry for this flavor in the cache
882 883 * for this export.
883 884 * Get the caller's address, mask off the
884 885 * parts of the address that do not identify
885 886 * the host (port number, etc), and then hash
886 887 * it to find the chain of cache entries.
887 888 */
888 889
889 890 claddr = svc_getrpccaller(req->rq_xprt);
890 891 addr = *claddr;
891 892 addr.buf = kmem_alloc(addr.maxlen, KM_SLEEP);
892 893 bcopy(claddr->buf, addr.buf, claddr->len);
893 894
894 895 SVC_GETADDRMASK(req->rq_xprt, SVC_TATTR_ADDRMASK, (void **)&taddrmask);
895 896 ASSERT(taddrmask != NULL);
896 897 addrmask(&addr, taddrmask);
897 898
898 899 ac.auth_flavor = flavor;
899 900 ac.auth_clnt_cred = crdup(cr);
900 901
901 902 acc.authc_addr = addr;
902 903
903 904 tree = exi->exi_cache[hash(&addr)];
904 905
905 906 rw_enter(&exi->exi_cache_lock, RW_READER);
906 907 c = (struct auth_cache_clnt *)avl_find(tree, &acc, NULL);
907 908
908 909 if (c == NULL) {
909 910 struct auth_cache_clnt *nc;
910 911
911 912 rw_exit(&exi->exi_cache_lock);
912 913
913 914 nc = kmem_alloc(sizeof (*nc), KM_NOSLEEP | KM_NORMALPRI);
914 915 if (nc == NULL)
915 916 goto retrieve;
916 917
917 918 /*
918 919 * Initialize the new auth_cache_clnt
919 920 */
920 921 nc->authc_addr = addr;
921 922 nc->authc_addr.buf = kmem_alloc(addr.maxlen,
922 923 KM_NOSLEEP | KM_NORMALPRI);
923 924 if (addr.maxlen != 0 && nc->authc_addr.buf == NULL) {
924 925 kmem_free(nc, sizeof (*nc));
925 926 goto retrieve;
926 927 }
927 928 bcopy(addr.buf, nc->authc_addr.buf, addr.len);
928 929 rw_init(&nc->authc_lock, NULL, RW_DEFAULT, NULL);
929 930 avl_create(&nc->authc_tree, nfsauth_cache_compar,
930 931 sizeof (struct auth_cache),
931 932 offsetof(struct auth_cache, auth_link));
932 933
933 934 rw_enter(&exi->exi_cache_lock, RW_WRITER);
934 935 c = (struct auth_cache_clnt *)avl_find(tree, &acc, &where);
935 936 if (c == NULL) {
936 937 avl_insert(tree, nc, where);
937 938 rw_downgrade(&exi->exi_cache_lock);
938 939 c = nc;
939 940 } else {
940 941 rw_downgrade(&exi->exi_cache_lock);
941 942
942 943 avl_destroy(&nc->authc_tree);
943 944 rw_destroy(&nc->authc_lock);
944 945 kmem_free(nc->authc_addr.buf, nc->authc_addr.maxlen);
945 946 kmem_free(nc, sizeof (*nc));
946 947 }
947 948 }
948 949
949 950 ASSERT(c != NULL);
950 951
951 952 rw_enter(&c->authc_lock, RW_READER);
952 953 p = (struct auth_cache *)avl_find(&c->authc_tree, &ac, NULL);
953 954
954 955 if (p == NULL) {
955 956 struct auth_cache *np;
956 957
957 958 rw_exit(&c->authc_lock);
958 959
959 960 np = kmem_cache_alloc(exi_cache_handle,
960 961 KM_NOSLEEP | KM_NORMALPRI);
961 962 if (np == NULL) {
962 963 rw_exit(&exi->exi_cache_lock);
963 964 goto retrieve;
964 965 }
965 966
966 967 /*
967 968 * Initialize the new auth_cache
968 969 */
969 970 np->auth_clnt = c;
970 971 np->auth_flavor = flavor;
971 972 np->auth_clnt_cred = ac.auth_clnt_cred;
972 973 np->auth_srv_ngids = 0;
973 974 np->auth_srv_gids = NULL;
974 975 np->auth_time = np->auth_freshness = gethrestime_sec();
975 976 np->auth_state = NFS_AUTH_NEW;
976 977 mutex_init(&np->auth_lock, NULL, MUTEX_DEFAULT, NULL);
977 978 cv_init(&np->auth_cv, NULL, CV_DEFAULT, NULL);
978 979
979 980 rw_enter(&c->authc_lock, RW_WRITER);
980 981 rw_exit(&exi->exi_cache_lock);
981 982
982 983 p = (struct auth_cache *)avl_find(&c->authc_tree, &ac, &where);
983 984 if (p == NULL) {
984 985 avl_insert(&c->authc_tree, np, where);
985 986 rw_downgrade(&c->authc_lock);
986 987 p = np;
987 988 } else {
988 989 rw_downgrade(&c->authc_lock);
989 990
990 991 cv_destroy(&np->auth_cv);
991 992 mutex_destroy(&np->auth_lock);
992 993 crfree(ac.auth_clnt_cred);
993 994 kmem_cache_free(exi_cache_handle, np);
994 995 }
995 996 } else {
996 997 rw_exit(&exi->exi_cache_lock);
997 998 crfree(ac.auth_clnt_cred);
998 999 }
999 1000
1000 1001 mutex_enter(&p->auth_lock);
1001 1002 rw_exit(&c->authc_lock);
1002 1003
1003 1004 /*
1004 1005 * If the entry is in the WAITING state then some other thread is just
1005 1006 * retrieving the required info. The entry was either NEW, or the list
1006 1007 * of client's supplemental groups is going to be changed (either by
1007 1008 * this thread, or by some other thread). We need to wait until the
1008 1009 * nfsauth_retrieve() is done.
1009 1010 */
1010 1011 while (p->auth_state == NFS_AUTH_WAITING)
1011 1012 cv_wait(&p->auth_cv, &p->auth_lock);
1012 1013
1013 1014 /*
1014 1015 * Here the entry cannot be in WAITING or INVALID state.
1015 1016 */
1016 1017 ASSERT(p->auth_state != NFS_AUTH_WAITING);
1017 1018 ASSERT(p->auth_state != NFS_AUTH_INVALID);
1018 1019
1019 1020 /*
1020 1021 * If the cache entry is not valid yet, we need to retrieve the
1021 1022 * info ourselves.
1022 1023 */
1023 1024 if (p->auth_state == NFS_AUTH_NEW) {
1024 1025 bool_t res;
1025 1026 /*
1026 1027 * NFS_AUTH_NEW is the default output auth_state value in a
1027 1028 * case we failed somewhere below.
1028 1029 */
1029 1030 auth_state_t state = NFS_AUTH_NEW;
1030 1031
1031 1032 p->auth_state = NFS_AUTH_WAITING;
1032 1033 mutex_exit(&p->auth_lock);
1033 1034 kmem_free(addr.buf, addr.maxlen);
1034 1035 addr = p->auth_clnt->authc_addr;
1035 1036
1036 1037 atomic_inc_uint(&nfsauth_cache_miss);
1037 1038
1038 1039 res = nfsauth_retrieve(nag, exi, svc_getnetid(req->rq_xprt),
1039 1040 flavor, &addr, &access, cr, &tmpuid, &tmpgid, &tmpngids,
1040 1041 &tmpgids);
1041 1042
1042 1043 p->auth_access = access;
1043 1044 p->auth_time = p->auth_freshness = gethrestime_sec();
1044 1045
1045 1046 if (res == TRUE) {
1046 1047 if (uid != NULL)
1047 1048 *uid = tmpuid;
1048 1049 if (gid != NULL)
1049 1050 *gid = tmpgid;
1050 1051 if (ngids != NULL && gids != NULL) {
1051 1052 *ngids = tmpngids;
1052 1053 *gids = tmpgids;
1053 1054
1054 1055 /*
1055 1056 * We need a copy of gids for the
1056 1057 * auth_cache entry
1057 1058 */
1058 1059 tmpgids = kmem_alloc(tmpngids * sizeof (gid_t),
1059 1060 KM_NOSLEEP | KM_NORMALPRI);
1060 1061 if (tmpgids != NULL)
1061 1062 bcopy(*gids, tmpgids,
1062 1063 tmpngids * sizeof (gid_t));
1063 1064 }
1064 1065
1065 1066 if (tmpgids != NULL || tmpngids == 0) {
1066 1067 p->auth_srv_uid = tmpuid;
1067 1068 p->auth_srv_gid = tmpgid;
1068 1069 p->auth_srv_ngids = tmpngids;
1069 1070 p->auth_srv_gids = tmpgids;
1070 1071
1071 1072 state = NFS_AUTH_FRESH;
1072 1073 }
1073 1074 }
1074 1075
1075 1076 /*
1076 1077 * Set the auth_state and notify waiters.
1077 1078 */
1078 1079 mutex_enter(&p->auth_lock);
1079 1080 p->auth_state = state;
1080 1081 cv_broadcast(&p->auth_cv);
1081 1082 mutex_exit(&p->auth_lock);
1082 1083 } else {
1083 1084 uint_t nach;
1084 1085 time_t refresh;
1085 1086
1086 1087 refresh = gethrestime_sec() - p->auth_freshness;
1087 1088
1088 1089 p->auth_time = gethrestime_sec();
1089 1090
1090 1091 if (uid != NULL)
1091 1092 *uid = p->auth_srv_uid;
1092 1093 if (gid != NULL)
1093 1094 *gid = p->auth_srv_gid;
1094 1095 if (ngids != NULL && gids != NULL) {
1095 1096 *ngids = p->auth_srv_ngids;
1096 1097 *gids = kmem_alloc(*ngids * sizeof (gid_t), KM_SLEEP);
1097 1098 bcopy(p->auth_srv_gids, *gids, *ngids * sizeof (gid_t));
1098 1099 }
1099 1100
1100 1101 access = p->auth_access;
1101 1102
1102 1103 if ((refresh > NFSAUTH_CACHE_REFRESH) &&
1103 1104 p->auth_state == NFS_AUTH_FRESH) {
1104 1105 refreshq_auth_node_t *ran;
1105 1106 uint_t nacr;
1106 1107
1107 1108 p->auth_state = NFS_AUTH_STALE;
1108 1109 mutex_exit(&p->auth_lock);
1109 1110
1110 1111 nacr = atomic_inc_uint_nv(&nfsauth_cache_refresh);
1111 1112 DTRACE_PROBE3(nfsauth__debug__cache__stale,
1112 1113 struct exportinfo *, exi,
1113 1114 struct auth_cache *, p,
1114 1115 uint_t, nacr);
1115 1116
1116 1117 ran = kmem_alloc(sizeof (refreshq_auth_node_t),
1117 1118 KM_SLEEP);
1118 1119 ran->ran_auth = p;
1119 1120 ran->ran_netid = strdup(svc_getnetid(req->rq_xprt));
1120 1121
1121 1122 mutex_enter(&nag->refreshq_lock);
1122 1123
1123 1124 if (nag->refreshq_thread_state ==
1124 1125 REFRESHQ_THREAD_NEED_CREATE) {
1125 1126 /* Launch nfsauth refresh thread */
1126 1127 nag->refreshq_thread_state =
1127 1128 REFRESHQ_THREAD_RUNNING;
1128 1129 (void) zthread_create(NULL, 0,
1129 1130 nfsauth_refresh_thread, nag, 0,
1130 1131 minclsyspri);
1131 1132 }
1132 1133
1133 1134 /*
1134 1135 * We should not add a work queue item if the thread
1135 1136 * is not accepting them.
1136 1137 */
1137 1138 if (nag->refreshq_thread_state ==
1138 1139 REFRESHQ_THREAD_RUNNING) {
1139 1140 refreshq_exi_node_t *ren;
1140 1141
1141 1142 /*
1142 1143 * Is there an existing exi_list?
1143 1144 */
1144 1145 for (ren = list_head(&nag->refreshq_queue);
1145 1146 ren != NULL;
1146 1147 ren = list_next(&nag->refreshq_queue,
1147 1148 ren)) {
1148 1149 if (ren->ren_exi == exi) {
1149 1150 list_insert_tail(
1150 1151 &ren->ren_authlist, ran);
1151 1152 break;
1152 1153 }
1153 1154 }
1154 1155
1155 1156 if (ren == NULL) {
1156 1157 ren = kmem_alloc(
1157 1158 sizeof (refreshq_exi_node_t),
1158 1159 KM_SLEEP);
1159 1160
1160 1161 exi_hold(exi);
1161 1162 ren->ren_exi = exi;
1162 1163
1163 1164 list_create(&ren->ren_authlist,
1164 1165 sizeof (refreshq_auth_node_t),
1165 1166 offsetof(refreshq_auth_node_t,
1166 1167 ran_node));
1167 1168
1168 1169 list_insert_tail(&ren->ren_authlist,
1169 1170 ran);
1170 1171 list_insert_tail(&nag->refreshq_queue,
1171 1172 ren);
1172 1173 }
1173 1174
1174 1175 cv_broadcast(&nag->refreshq_cv);
1175 1176 } else {
1176 1177 strfree(ran->ran_netid);
1177 1178 kmem_free(ran, sizeof (refreshq_auth_node_t));
1178 1179 }
1179 1180
1180 1181 mutex_exit(&nag->refreshq_lock);
1181 1182 } else {
1182 1183 mutex_exit(&p->auth_lock);
1183 1184 }
1184 1185
1185 1186 nach = atomic_inc_uint_nv(&nfsauth_cache_hit);
1186 1187 DTRACE_PROBE2(nfsauth__debug__cache__hit,
1187 1188 uint_t, nach,
1188 1189 time_t, refresh);
1189 1190
1190 1191 kmem_free(addr.buf, addr.maxlen);
1191 1192 }
1192 1193
1193 1194 return (access);
1194 1195
1195 1196 retrieve:
1196 1197 crfree(ac.auth_clnt_cred);
1197 1198
1198 1199 /*
1199 1200 * Retrieve the required data without caching.
1200 1201 */
1201 1202
1202 1203 ASSERT(p == NULL);
1203 1204
1204 1205 atomic_inc_uint(&nfsauth_cache_miss);
1205 1206
1206 1207 if (nfsauth_retrieve(nag, exi, svc_getnetid(req->rq_xprt), flavor,
1207 1208 &addr, &access, cr, &tmpuid, &tmpgid, &tmpngids, &tmpgids)) {
1208 1209 if (uid != NULL)
1209 1210 *uid = tmpuid;
1210 1211 if (gid != NULL)
1211 1212 *gid = tmpgid;
1212 1213 if (ngids != NULL && gids != NULL) {
1213 1214 *ngids = tmpngids;
1214 1215 *gids = tmpgids;
1215 1216 } else {
1216 1217 kmem_free(tmpgids, tmpngids * sizeof (gid_t));
1217 1218 }
1218 1219 }
1219 1220
1220 1221 kmem_free(addr.buf, addr.maxlen);
1221 1222
1222 1223 return (access);
1223 1224 }
1224 1225
1225 1226 /*
1226 1227 * Check if the requesting client has access to the filesystem with
1227 1228 * a given nfs flavor number which is an explicitly shared flavor.
1228 1229 */
1229 1230 int
1230 1231 nfsauth4_secinfo_access(struct exportinfo *exi, struct svc_req *req,
1231 1232 int flavor, int perm, cred_t *cr)
1232 1233 {
1233 1234 int access;
1234 1235
1235 1236 if (! (perm & M_4SEC_EXPORTED)) {
1236 1237 return (NFSAUTH_DENIED);
1237 1238 }
1238 1239
1239 1240 /*
1240 1241 * Optimize if there are no lists
1241 1242 */
1242 1243 if ((perm & (M_ROOT | M_NONE | M_MAP)) == 0) {
1243 1244 perm &= ~M_4SEC_EXPORTED;
1244 1245 if (perm == M_RO)
1245 1246 return (NFSAUTH_RO);
1246 1247 if (perm == M_RW)
1247 1248 return (NFSAUTH_RW);
1248 1249 }
1249 1250
1250 1251 access = nfsauth_cache_get(exi, req, flavor, cr, NULL, NULL, NULL,
1251 1252 NULL);
1252 1253
1253 1254 return (access);
1254 1255 }
1255 1256
1256 1257 int
1257 1258 nfsauth_access(struct exportinfo *exi, struct svc_req *req, cred_t *cr,
1258 1259 uid_t *uid, gid_t *gid, uint_t *ngids, gid_t **gids)
1259 1260 {
1260 1261 int access, mapaccess;
1261 1262 struct secinfo *sp;
1262 1263 int i, flavor, perm;
1263 1264 int authnone_entry = -1;
1264 1265
1265 1266 /*
1266 1267 * By default root is mapped to anonymous user.
1267 1268 * This might get overriden later in nfsauth_cache_get().
1268 1269 */
1269 1270 if (crgetuid(cr) == 0) {
1270 1271 if (uid != NULL)
1271 1272 *uid = exi->exi_export.ex_anon;
1272 1273 if (gid != NULL)
1273 1274 *gid = exi->exi_export.ex_anon;
1274 1275 } else {
1275 1276 if (uid != NULL)
1276 1277 *uid = crgetuid(cr);
1277 1278 if (gid != NULL)
1278 1279 *gid = crgetgid(cr);
1279 1280 }
1280 1281
1281 1282 if (ngids != NULL)
1282 1283 *ngids = 0;
1283 1284 if (gids != NULL)
1284 1285 *gids = NULL;
1285 1286
1286 1287 /*
1287 1288 * Get the nfs flavor number from xprt.
1288 1289 */
1289 1290 flavor = (int)(uintptr_t)req->rq_xprt->xp_cookie;
1290 1291
1291 1292 /*
1292 1293 * First check the access restrictions on the filesystem. If
1293 1294 * there are no lists associated with this flavor then there's no
1294 1295 * need to make an expensive call to the nfsauth service or to
1295 1296 * cache anything.
1296 1297 */
1297 1298
1298 1299 sp = exi->exi_export.ex_secinfo;
1299 1300 for (i = 0; i < exi->exi_export.ex_seccnt; i++) {
1300 1301 if (flavor != sp[i].s_secinfo.sc_nfsnum) {
1301 1302 if (sp[i].s_secinfo.sc_nfsnum == AUTH_NONE)
1302 1303 authnone_entry = i;
1303 1304 continue;
1304 1305 }
1305 1306 break;
1306 1307 }
1307 1308
1308 1309 mapaccess = 0;
1309 1310
1310 1311 if (i >= exi->exi_export.ex_seccnt) {
1311 1312 /*
1312 1313 * Flavor not found, but use AUTH_NONE if it exists
1313 1314 */
1314 1315 if (authnone_entry == -1)
1315 1316 return (NFSAUTH_DENIED);
1316 1317 flavor = AUTH_NONE;
1317 1318 mapaccess = NFSAUTH_MAPNONE;
1318 1319 i = authnone_entry;
1319 1320 }
1320 1321
1321 1322 /*
1322 1323 * If the flavor is in the ex_secinfo list, but not an explicitly
1323 1324 * shared flavor by the user, it is a result of the nfsv4 server
1324 1325 * namespace setup. We will grant an RO permission similar for
1325 1326 * a pseudo node except that this node is a shared one.
1326 1327 *
1327 1328 * e.g. flavor in (flavor) indicates that it is not explictly
1328 1329 * shared by the user:
1329 1330 *
1330 1331 * / (sys, krb5)
1331 1332 * |
1332 1333 * export #share -o sec=sys (krb5)
1333 1334 * |
1334 1335 * secure #share -o sec=krb5
1335 1336 *
1336 1337 * In this case, when a krb5 request coming in to access
1337 1338 * /export, RO permission is granted.
1338 1339 */
1339 1340 if (!(sp[i].s_flags & M_4SEC_EXPORTED))
1340 1341 return (mapaccess | NFSAUTH_RO);
1341 1342
1342 1343 /*
1343 1344 * Optimize if there are no lists.
1344 1345 * We cannot optimize for AUTH_SYS with NGRPS (16) supplemental groups.
1345 1346 */
1346 1347 perm = sp[i].s_flags;
1347 1348 if ((perm & (M_ROOT | M_NONE | M_MAP)) == 0 && (ngroups_max <= NGRPS ||
1348 1349 flavor != AUTH_SYS || crgetngroups(cr) < NGRPS)) {
1349 1350 perm &= ~M_4SEC_EXPORTED;
1350 1351 if (perm == M_RO)
1351 1352 return (mapaccess | NFSAUTH_RO);
1352 1353 if (perm == M_RW)
1353 1354 return (mapaccess | NFSAUTH_RW);
1354 1355 }
1355 1356
1356 1357 access = nfsauth_cache_get(exi, req, flavor, cr, uid, gid, ngids, gids);
1357 1358
1358 1359 /*
1359 1360 * For both NFSAUTH_DENIED and NFSAUTH_WRONGSEC we do not care about
1360 1361 * the supplemental groups.
1361 1362 */
1362 1363 if (access & NFSAUTH_DENIED || access & NFSAUTH_WRONGSEC) {
1363 1364 if (ngids != NULL && gids != NULL) {
1364 1365 kmem_free(*gids, *ngids * sizeof (gid_t));
1365 1366 *ngids = 0;
1366 1367 *gids = NULL;
1367 1368 }
1368 1369 }
1369 1370
1370 1371 /*
1371 1372 * Client's security flavor doesn't match with "ro" or
1372 1373 * "rw" list. Try again using AUTH_NONE if present.
1373 1374 */
1374 1375 if ((access & NFSAUTH_WRONGSEC) && (flavor != AUTH_NONE)) {
1375 1376 /*
1376 1377 * Have we already encountered AUTH_NONE ?
1377 1378 */
1378 1379 if (authnone_entry != -1) {
1379 1380 mapaccess = NFSAUTH_MAPNONE;
1380 1381 access = nfsauth_cache_get(exi, req, AUTH_NONE, cr,
1381 1382 NULL, NULL, NULL, NULL);
1382 1383 } else {
1383 1384 /*
1384 1385 * Check for AUTH_NONE presence.
1385 1386 */
1386 1387 for (; i < exi->exi_export.ex_seccnt; i++) {
1387 1388 if (sp[i].s_secinfo.sc_nfsnum == AUTH_NONE) {
1388 1389 mapaccess = NFSAUTH_MAPNONE;
1389 1390 access = nfsauth_cache_get(exi, req,
1390 1391 AUTH_NONE, cr, NULL, NULL, NULL,
1391 1392 NULL);
1392 1393 break;
1393 1394 }
1394 1395 }
1395 1396 }
1396 1397 }
1397 1398
1398 1399 if (access & NFSAUTH_DENIED)
1399 1400 access = NFSAUTH_DENIED;
1400 1401
1401 1402 return (access | mapaccess);
1402 1403 }
1403 1404
1404 1405 static void
1405 1406 nfsauth_free_clnt_node(struct auth_cache_clnt *p)
1406 1407 {
1407 1408 void *cookie = NULL;
1408 1409 struct auth_cache *node;
1409 1410
1410 1411 while ((node = avl_destroy_nodes(&p->authc_tree, &cookie)) != NULL)
1411 1412 nfsauth_free_node(node);
1412 1413 avl_destroy(&p->authc_tree);
1413 1414
1414 1415 kmem_free(p->authc_addr.buf, p->authc_addr.maxlen);
1415 1416 rw_destroy(&p->authc_lock);
1416 1417
1417 1418 kmem_free(p, sizeof (*p));
1418 1419 }
1419 1420
1420 1421 static void
1421 1422 nfsauth_free_node(struct auth_cache *p)
1422 1423 {
1423 1424 crfree(p->auth_clnt_cred);
1424 1425 kmem_free(p->auth_srv_gids, p->auth_srv_ngids * sizeof (gid_t));
1425 1426 mutex_destroy(&p->auth_lock);
1426 1427 cv_destroy(&p->auth_cv);
1427 1428 kmem_cache_free(exi_cache_handle, p);
1428 1429 }
1429 1430
1430 1431 /*
1431 1432 * Free the nfsauth cache for a given export
1432 1433 */
1433 1434 void
1434 1435 nfsauth_cache_free(struct exportinfo *exi)
1435 1436 {
1436 1437 int i;
1437 1438
1438 1439 /*
1439 1440 * The only way we got here was with an exi_rele, which means that no
1440 1441 * auth cache entry is being refreshed.
1441 1442 */
1442 1443
1443 1444 for (i = 0; i < AUTH_TABLESIZE; i++) {
1444 1445 avl_tree_t *tree = exi->exi_cache[i];
1445 1446 void *cookie = NULL;
1446 1447 struct auth_cache_clnt *node;
1447 1448
1448 1449 while ((node = avl_destroy_nodes(tree, &cookie)) != NULL)
1449 1450 nfsauth_free_clnt_node(node);
1450 1451 }
1451 1452 }
1452 1453
1453 1454 /*
1454 1455 * Called by the kernel memory allocator when
1455 1456 * memory is low. Free unused cache entries.
1456 1457 * If that's not enough, the VM system will
1457 1458 * call again for some more.
1458 1459 */
1459 1460 /*ARGSUSED*/
1460 1461 void
1461 1462 exi_cache_reclaim(void *cdrarg)
1462 1463 {
1463 1464 int i;
1464 1465 struct exportinfo *exi;
1465 1466 nfs_export_t *ne = nfs_get_export();
1466 1467
1467 1468 rw_enter(&ne->exported_lock, RW_READER);
1468 1469
1469 1470 for (i = 0; i < EXPTABLESIZE; i++) {
1470 1471 for (exi = ne->exptable[i]; exi; exi = exi->fid_hash.next)
1471 1472 exi_cache_trim(exi);
1472 1473 }
1473 1474
1474 1475 rw_exit(&ne->exported_lock);
1475 1476
1476 1477 atomic_inc_uint(&nfsauth_cache_reclaim);
1477 1478 }
1478 1479
1479 1480 void
1480 1481 exi_cache_trim(struct exportinfo *exi)
1481 1482 {
1482 1483 struct auth_cache_clnt *c;
1483 1484 struct auth_cache_clnt *nextc;
1484 1485 struct auth_cache *p;
1485 1486 struct auth_cache *next;
1486 1487 int i;
1487 1488 time_t stale_time;
1488 1489 avl_tree_t *tree;
1489 1490
1490 1491 for (i = 0; i < AUTH_TABLESIZE; i++) {
1491 1492 tree = exi->exi_cache[i];
1492 1493 stale_time = gethrestime_sec() - NFSAUTH_CACHE_TRIM;
1493 1494 rw_enter(&exi->exi_cache_lock, RW_READER);
1494 1495
1495 1496 /*
1496 1497 * Free entries that have not been
1497 1498 * used for NFSAUTH_CACHE_TRIM seconds.
1498 1499 */
1499 1500 for (c = avl_first(tree); c != NULL; c = AVL_NEXT(tree, c)) {
1500 1501 /*
1501 1502 * We are being called by the kmem subsystem to reclaim
1502 1503 * memory so don't block if we can't get the lock.
1503 1504 */
1504 1505 if (rw_tryenter(&c->authc_lock, RW_WRITER) == 0) {
1505 1506 exi_cache_auth_reclaim_failed++;
1506 1507 rw_exit(&exi->exi_cache_lock);
1507 1508 return;
1508 1509 }
1509 1510
1510 1511 for (p = avl_first(&c->authc_tree); p != NULL;
1511 1512 p = next) {
1512 1513 next = AVL_NEXT(&c->authc_tree, p);
1513 1514
1514 1515 ASSERT(p->auth_state != NFS_AUTH_INVALID);
1515 1516
1516 1517 mutex_enter(&p->auth_lock);
1517 1518
1518 1519 /*
1519 1520 * We won't trim recently used and/or WAITING
1520 1521 * entries.
1521 1522 */
1522 1523 if (p->auth_time > stale_time ||
1523 1524 p->auth_state == NFS_AUTH_WAITING) {
1524 1525 mutex_exit(&p->auth_lock);
1525 1526 continue;
1526 1527 }
1527 1528
1528 1529 DTRACE_PROBE1(nfsauth__debug__trim__state,
1529 1530 auth_state_t, p->auth_state);
1530 1531
1531 1532 /*
1532 1533 * STALE and REFRESHING entries needs to be
1533 1534 * marked INVALID only because they are
1534 1535 * referenced by some other structures or
1535 1536 * threads. They will be freed later.
1536 1537 */
1537 1538 if (p->auth_state == NFS_AUTH_STALE ||
1538 1539 p->auth_state == NFS_AUTH_REFRESHING) {
1539 1540 p->auth_state = NFS_AUTH_INVALID;
1540 1541 mutex_exit(&p->auth_lock);
1541 1542
1542 1543 avl_remove(&c->authc_tree, p);
1543 1544 } else {
1544 1545 mutex_exit(&p->auth_lock);
1545 1546
1546 1547 avl_remove(&c->authc_tree, p);
1547 1548 nfsauth_free_node(p);
1548 1549 }
1549 1550 }
1550 1551 rw_exit(&c->authc_lock);
1551 1552 }
1552 1553
1553 1554 if (rw_tryupgrade(&exi->exi_cache_lock) == 0) {
1554 1555 rw_exit(&exi->exi_cache_lock);
1555 1556 exi_cache_clnt_reclaim_failed++;
1556 1557 continue;
1557 1558 }
1558 1559
1559 1560 for (c = avl_first(tree); c != NULL; c = nextc) {
1560 1561 nextc = AVL_NEXT(tree, c);
1561 1562
1562 1563 if (avl_is_empty(&c->authc_tree) == B_FALSE)
1563 1564 continue;
1564 1565
1565 1566 avl_remove(tree, c);
1566 1567
1567 1568 nfsauth_free_clnt_node(c);
1568 1569 }
1569 1570
1570 1571 rw_exit(&exi->exi_cache_lock);
1571 1572 }
1572 1573 }
|
↓ open down ↓ |
686 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX