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