Print this page
    
7029 want per-process exploit mitigation features (secflags)
7030 want basic address space layout randomization (aslr)
7031 noexec_user_stack should be a secflag
7032 want a means to forbid mappings around NULL.
    
      
        | Split | 
	Close | 
      
      | Expand all | 
      | Collapse all | 
    
    
          --- old/usr/src/uts/common/os/priv.c
          +++ new/usr/src/uts/common/os/priv.c
   1    1  /*
   2    2   * CDDL HEADER START
   3    3   *
   4    4   * The contents of this file are subject to the terms of the
   5    5   * Common Development and Distribution License (the "License").
   6    6   * You may not use this file except in compliance with the License.
   7    7   *
   8    8   * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
   9    9   * or http://www.opensolaris.org/os/licensing.
  10   10   * See the License for the specific language governing permissions
  11   11   * and limitations under the License.
  12   12   *
  13   13   * When distributing Covered Code, include this CDDL HEADER in each
  14   14   * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
  15   15   * If applicable, add the following below this CDDL HEADER, with the
  16   16   * fields enclosed by brackets "[]" replaced with your own identifying
  17   17   * information: Portions Copyright [yyyy] [name of copyright owner]
  18   18   *
  19   19   * CDDL HEADER END
  20   20   */
  21   21  /*
  22   22   * Copyright 2010 Sun Microsystems, Inc.  All rights reserved.
  23   23   * Use is subject to license terms.
  24   24   */
  25   25  
  26   26  /*
  27   27   * Privilege implementation.
  28   28   *
  29   29   * This file provides the infrastructure for privilege sets and limits
  30   30   * the number of files that requires to include <sys/cred_impl.h> and/or
  31   31   * <sys/priv_impl.h>.
  32   32   *
  33   33   * The Solaris privilege mechanism has been designed in a
  34   34   * future proof manner.  While the kernel may use fixed size arrays
  35   35   * and fixed bitmasks and bit values, the representation of those
  36   36   * is kernel private.  All external interfaces as well as K-to-K interfaces
  37   37   * have been constructed in a manner to provide the maximum flexibility.
  38   38   *
  39   39   * There can be X privilege sets each containing Y 32 bit words.
  40   40   * <X, Y> are constant for a kernel invocation.
  41   41   *
  42   42   * As a consequence, all privilege set manipulation happens in functions
  43   43   * below.
  44   44   *
  45   45   */
  46   46  
  47   47  #include <sys/systm.h>
  48   48  #include <sys/ddi.h>
  49   49  #include <sys/kmem.h>
  50   50  #include <sys/sunddi.h>
  51   51  #include <sys/errno.h>
  52   52  #include <sys/debug.h>
  53   53  #include <sys/priv_impl.h>
  54   54  #include <sys/procfs.h>
  55   55  #include <sys/policy.h>
  56   56  #include <sys/cred_impl.h>
  57   57  #include <sys/devpolicy.h>
  58   58  #include <sys/atomic.h>
  59   59  
  60   60  /*
  61   61   * Privilege name to number mapping table consists in the generated
  62   62   * priv_const.c file.  This lock protects against updates of the privilege
  63   63   * names and counts; all other priv_info fields are read-only.
  64   64   * The actual protected values are:
  65   65   *      global variable nprivs
  66   66   *      the priv_max field
  67   67   *      the priv_names field
  68   68   *      the priv names info item (cnt/strings)
  69   69   */
  70   70  krwlock_t privinfo_lock;
  71   71  
  72   72  static boolean_t priv_valid(const cred_t *);
  73   73  
  74   74  priv_set_t priv_fullset;        /* set of all privileges */
  75   75  priv_set_t priv_unsafe; /* unsafe to exec set-uid root if these are not in L */
  76   76  
  77   77  /*
  78   78   * Privilege initialization functions.
  79   79   * Called from common/os/cred.c when cred_init is called.
  80   80   */
  81   81  
  
    | 
      ↓ open down ↓ | 
    81 lines elided | 
    
      ↑ open up ↑ | 
  
  82   82  void
  83   83  priv_init(void)
  84   84  {
  85   85  #ifdef DEBUG
  86   86          int alloc_test_priv = 1;
  87   87  #else
  88   88          int alloc_test_priv = priv_debug;
  89   89  #endif
  90   90          rw_init(&privinfo_lock, NULL, RW_DRIVER, NULL);
  91   91  
  92      -        PRIV_BASIC_ASSERT(priv_basic);
  93      -        PRIV_UNSAFE_ASSERT(&priv_unsafe);
       92 +        PRIV_BASIC_ADDSET(priv_basic);
       93 +        PRIV_UNSAFE_ADDSET(&priv_unsafe);
  94   94          priv_fillset(&priv_fullset);
  95   95  
  96   96          /*
  97   97           * When booting with priv_debug set or in a DEBUG kernel, then we'll
  98   98           * add an additional basic privilege and we verify that it is always
  99   99           * present in E.
 100  100           */
 101  101          if (alloc_test_priv != 0 &&
 102  102              (priv_basic_test = priv_getbyname("basic_test", PRIV_ALLOC)) >= 0) {
 103  103                  priv_addset(priv_basic, priv_basic_test);
 104  104          }
 105  105  
 106  106          devpolicy_init();
 107  107  }
 108  108  
 109  109  /* Utility functions: privilege sets as opaque data types */
 110  110  
 111  111  /*
 112  112   * Guts of prgetprivsize.
 113  113   */
 114  114  int
 115  115  priv_prgetprivsize(prpriv_t *tmpl)
 116  116  {
 117  117          return (sizeof (prpriv_t) +
 118  118              PRIV_SETBYTES - sizeof (priv_chunk_t) +
 119  119              (tmpl ? tmpl->pr_infosize : priv_info->priv_infosize));
 120  120  }
 121  121  
 122  122  /*
 123  123   * Guts of prgetpriv.
 124  124   */
 125  125  void
 126  126  cred2prpriv(const cred_t *cp, prpriv_t *pr)
 127  127  {
 128  128          priv_set_t *psa;
 129  129          int i;
 130  130  
 131  131          pr->pr_nsets = PRIV_NSET;
 132  132          pr->pr_setsize = PRIV_SETSIZE;
 133  133          pr->pr_infosize = priv_info->priv_infosize;
 134  134  
 135  135          psa = (priv_set_t *)pr->pr_sets;
 136  136  
 137  137          for (i = 0; i < PRIV_NSET; i++)
 138  138                  psa[i] = *priv_getset(cp, i);
 139  139  
 140  140          priv_getinfo(cp, (char *)pr + PRIV_PRPRIV_INFO_OFFSET(pr));
 141  141  }
 142  142  
 143  143  /*
 144  144   * Guts of pr_spriv:
 145  145   *
 146  146   * Set the privileges of a process.
 147  147   *
 148  148   * In order to set the privileges, the setting process will need to
 149  149   * have those privileges in its effective set in order to prevent
 150  150   * specially privileged processes to easily gain additional privileges.
 151  151   * Pre-existing privileges can be retained.  To change any privileges,
 152  152   * PRIV_PROC_OWNER needs to be asserted.
 153  153   *
 154  154   * In formula:
 155  155   *
 156  156   *      S' <= S || S' <= S + Ea
 157  157   *
 158  158   * the new set must either be subset of the old set or a subset of
 159  159   * the oldset merged with the effective set of the acting process; or just:
 160  160   *
 161  161   *      S' <= S + Ea
 162  162   *
 163  163   * It's not legal to grow the limit set this way.
 164  164   *
 165  165   */
 166  166  int
 167  167  priv_pr_spriv(proc_t *p, prpriv_t *prpriv, const cred_t *cr)
 168  168  {
 169  169          cred_t *oldcred;
 170  170          cred_t *newcred;
 171  171          int i;
 172  172          int err = EPERM;
 173  173          cred_priv_t *cp, *ocp;
 174  174          priv_set_t eset;
 175  175  
 176  176          ASSERT(MUTEX_HELD(&p->p_lock));
 177  177  
 178  178          /*
 179  179           * Set must have proper dimension; infosize must be absent
 180  180           * or properly sized.
 181  181           */
 182  182          if (prpriv->pr_nsets != PRIV_NSET ||
 183  183              prpriv->pr_setsize != PRIV_SETSIZE ||
 184  184              (prpriv->pr_infosize & (sizeof (uint32_t) - 1)) != 0 ||
 185  185              prpriv->pr_infosize > priv_info->priv_infosize ||
 186  186              prpriv->pr_infosize < 0)
 187  187                  return (EINVAL);
 188  188  
 189  189          mutex_exit(&p->p_lock);
 190  190  
 191  191          if (priv_proc_cred_perm(cr, p, &oldcred, VWRITE) != 0) {
 192  192                  mutex_enter(&p->p_lock);
 193  193                  return (EPERM);
 194  194          }
 195  195  
 196  196          newcred = crdup(oldcred);
 197  197  
 198  198          /* Copy the privilege sets from prpriv to newcred */
 199  199          bcopy(prpriv->pr_sets, CR_PRIVSETS(newcred), PRIV_SETBYTES);
 200  200  
 201  201          cp = &newcred->cr_priv;
 202  202          ocp = &oldcred->cr_priv;
 203  203          eset = CR_OEPRIV(cr);
 204  204  
 205  205          priv_intersect(&CR_LPRIV(oldcred), &eset);
 206  206  
 207  207          /*
 208  208           * Verify the constraints laid out:
 209  209           * for the limit set, we require that the new set is a subset
 210  210           * of the old limit set.
 211  211           * for all other sets, we require that the new set is either a
 212  212           * subset of the old set or a subset of the intersection of
 213  213           * the old limit set and the effective set of the acting process.
 214  214           */
 215  215          for (i = 0; i < PRIV_NSET; i++)
 216  216                  if (!priv_issubset(&cp->crprivs[i], &ocp->crprivs[i]) &&
 217  217                      (i == PRIV_LIMIT || !priv_issubset(&cp->crprivs[i], &eset)))
 218  218                          break;
 219  219  
 220  220          crfree(oldcred);
 221  221  
 222  222          if (i < PRIV_NSET || !priv_valid(newcred))
 223  223                  goto err;
 224  224  
 225  225          /* Load the settable privilege information */
 226  226          if (prpriv->pr_infosize > 0) {
 227  227                  char *x = (char *)prpriv + PRIV_PRPRIV_INFO_OFFSET(prpriv);
 228  228                  char *lastx = x + prpriv->pr_infosize;
 229  229  
 230  230                  while (x < lastx) {
 231  231                          priv_info_t *pi = (priv_info_t *)x;
 232  232                          priv_info_uint_t *pii;
 233  233  
 234  234                          switch (pi->priv_info_type) {
 235  235                          case PRIV_INFO_FLAGS:
 236  236                                  pii = (priv_info_uint_t *)x;
 237  237                                  if (pii->info.priv_info_size != sizeof (*pii)) {
 238  238                                          err = EINVAL;
 239  239                                          goto err;
 240  240                                  }
 241  241                                  CR_FLAGS(newcred) &= ~PRIV_USER;
 242  242                                  CR_FLAGS(newcred) |= (pii->val & PRIV_USER);
 243  243                                  break;
 244  244                          default:
 245  245                                  err = EINVAL;
 246  246                                  goto err;
 247  247                          }
 248  248                          /* Guarantee alignment and forward progress */
 249  249                          if ((pi->priv_info_size & (sizeof (uint32_t) - 1)) ||
 250  250                              pi->priv_info_size < sizeof (*pi) ||
 251  251                              lastx - x > pi->priv_info_size) {
 252  252                                  err = EINVAL;
 253  253                                  goto err;
 254  254                          }
 255  255  
 256  256                          x += pi->priv_info_size;
 257  257                  }
 258  258          }
 259  259  
 260  260          /*
 261  261           * We'll try to copy the privilege aware flag; but since the
 262  262           * privileges sets are all individually set, they are set
 263  263           * as if we're privilege aware.  If PRIV_AWARE wasn't set
 264  264           * or was explicitely unset, we need to set the flag and then
 265  265           * try to get rid of it.
 266  266           */
 267  267          if ((CR_FLAGS(newcred) & PRIV_AWARE) == 0) {
 268  268                  CR_FLAGS(newcred) |= PRIV_AWARE;
 269  269                  priv_adjust_PA(newcred);
 270  270          }
 271  271  
 272  272          mutex_enter(&p->p_crlock);
 273  273          oldcred = p->p_cred;
 274  274          p->p_cred = newcred;
 275  275          mutex_exit(&p->p_crlock);
 276  276          crfree(oldcred);
 277  277  
 278  278          mutex_enter(&p->p_lock);
 279  279          return (0);
 280  280  
 281  281  err:
 282  282          crfree(newcred);
 283  283          mutex_enter(&p->p_lock);
 284  284          return (err);
 285  285  }
 286  286  
 287  287  priv_impl_info_t
 288  288  *priv_hold_implinfo(void)
 289  289  {
 290  290          rw_enter(&privinfo_lock, RW_READER);
 291  291          return (priv_info);
 292  292  }
 293  293  
 294  294  void
 295  295  priv_release_implinfo(void)
 296  296  {
 297  297          rw_exit(&privinfo_lock);
 298  298  }
 299  299  
 300  300  size_t
 301  301  priv_get_implinfo_size(void)
 302  302  {
 303  303          return (privinfosize);
 304  304  }
 305  305  
 306  306  
 307  307  /*
 308  308   * Return the nth privilege set
 309  309   */
 310  310  const priv_set_t *
 311  311  priv_getset(const cred_t *cr, int set)
 312  312  {
 313  313          ASSERT(PRIV_VALIDSET(set));
 314  314  
 315  315          if ((CR_FLAGS(cr) & PRIV_AWARE) == 0)
 316  316                  switch (set) {
 317  317                  case PRIV_EFFECTIVE:
 318  318                          return (&CR_OEPRIV(cr));
 319  319                  case PRIV_PERMITTED:
 320  320                          return (&CR_OPPRIV(cr));
 321  321                  }
 322  322          return (&CR_PRIVS(cr)->crprivs[set]);
 323  323  }
 324  324  
 325  325  /*
 326  326   * Buf must be allocated by caller and contain sufficient space to
 327  327   * contain all additional info structures using priv_info.priv_infosize.
 328  328   * The buffer must be properly aligned.
 329  329   */
 330  330  /*ARGSUSED*/
 331  331  void
 332  332  priv_getinfo(const cred_t *cr, void *buf)
 333  333  {
 334  334          struct priv_info_uint *ii;
 335  335  
 336  336          ii = buf;
 337  337          ii->val = CR_FLAGS(cr);
 338  338          ii->info.priv_info_size = (uint32_t)sizeof (*ii);
 339  339          ii->info.priv_info_type = PRIV_INFO_FLAGS;
 340  340  }
 341  341  
 342  342  int
 343  343  priv_getbyname(const char *name, uint_t flag)
 344  344  {
 345  345          int i;
 346  346          int wheld = 0;
 347  347          int len;
 348  348          char *p;
 349  349  
 350  350          if (flag != 0 && flag != PRIV_ALLOC)
 351  351                  return (-EINVAL);
 352  352  
 353  353          if (strncasecmp(name, "priv_", 5) == 0)
 354  354                  name += 5;
 355  355  
 356  356          rw_enter(&privinfo_lock, RW_READER);
 357  357  rescan:
 358  358          for (i = 0; i < nprivs; i++)
 359  359                  if (strcasecmp(priv_names[i], name) == 0) {
 360  360                          rw_exit(&privinfo_lock);
 361  361                          return (i);
 362  362                  }
 363  363  
 364  364  
 365  365          if (!wheld) {
 366  366                  if (!(flag & PRIV_ALLOC)) {
 367  367                          rw_exit(&privinfo_lock);
 368  368                          return (-EINVAL);
 369  369                  }
 370  370  
 371  371                  /* check length, validity and available space */
 372  372                  len = strlen(name) + 1;
 373  373  
 374  374                  if (len > PRIVNAME_MAX) {
 375  375                          rw_exit(&privinfo_lock);
 376  376                          return (-ENAMETOOLONG);
 377  377                  }
 378  378  
 379  379                  for (p = (char *)name; *p != '\0'; p++) {
 380  380                          char c = *p;
 381  381  
 382  382                          if (!((c >= 'A' && c <= 'Z') ||
 383  383                              (c >= 'a' && c <= 'z') ||
 384  384                              (c >= '0' && c <= '9') ||
 385  385                              c == '_')) {
 386  386                                  rw_exit(&privinfo_lock);
 387  387                                  return (-EINVAL);
 388  388                          }
 389  389                  }
 390  390  
 391  391                  if (!rw_tryupgrade(&privinfo_lock)) {
 392  392                          rw_exit(&privinfo_lock);
 393  393                          rw_enter(&privinfo_lock, RW_WRITER);
 394  394                          wheld = 1;
 395  395                          /* Someone may have added our privilege */
 396  396                          goto rescan;
 397  397                  }
 398  398          }
 399  399  
 400  400          if (nprivs == MAX_PRIVILEGE || len + privbytes > maxprivbytes) {
 401  401                  rw_exit(&privinfo_lock);
 402  402                  return (-ENOMEM);
 403  403          }
 404  404  
 405  405          priv_names[i] = p = priv_str + privbytes;
 406  406  
 407  407          bcopy(name, p, len);
 408  408  
 409  409          /* make the priv_names[i] and privilege name globally visible */
 410  410          membar_producer();
 411  411  
 412  412          /* adjust priv count and bytes count */
 413  413          priv_ninfo->cnt = priv_info->priv_max = ++nprivs;
 414  414          privbytes += len;
 415  415  
 416  416          rw_exit(&privinfo_lock);
 417  417          return (i);
 418  418  }
 419  419  
 420  420  /*
 421  421   * We can't afford locking the privileges here because of the locations
 422  422   * we call this from; so we make sure that the privileges table
 423  423   * is visible to us; it is made visible before the value of nprivs is
 424  424   * updated.
 425  425   */
 426  426  const char *
 427  427  priv_getbynum(int priv)
 428  428  {
 429  429          int maxpriv = nprivs;
 430  430  
 431  431          membar_consumer();
 432  432  
 433  433          if (priv >= 0 && priv < maxpriv)
 434  434                  return (priv_names[priv]);
 435  435  
 436  436          return (NULL);
 437  437  }
 438  438  
 439  439  const char *
 440  440  priv_getsetbynum(int setno)
 441  441  {
 442  442          if (!PRIV_VALIDSET(setno))
 443  443                  return (NULL);
 444  444  
 445  445          return (priv_setnames[setno]);
 446  446  }
 447  447  
 448  448  /*
 449  449   * Privilege sanity checking when setting: E <= P.
 450  450   */
 451  451  static boolean_t
 452  452  priv_valid(const cred_t *cr)
 453  453  {
 454  454          return (priv_issubset(&CR_EPRIV(cr), &CR_PPRIV(cr)));
 455  455  }
 456  456  
 457  457  /*
 458  458   * Privilege manipulation functions
 459  459   *
 460  460   * Without knowing the details of the privilege set implementation,
 461  461   * opaque pointers can be used to manipulate sets at will.
 462  462   */
 463  463  void
 464  464  priv_emptyset(priv_set_t *set)
 465  465  {
 466  466          bzero(set, sizeof (*set));
 467  467  }
 468  468  
 469  469  void
 470  470  priv_fillset(priv_set_t *set)
 471  471  {
 472  472          int i;
  
    | 
      ↓ open down ↓ | 
    369 lines elided | 
    
      ↑ open up ↑ | 
  
 473  473  
 474  474          /* memset? */
 475  475          for (i = 0; i < PRIV_SETSIZE; i++)
 476  476                  set->pbits[i] = ~(priv_chunk_t)0;
 477  477  }
 478  478  
 479  479  void
 480  480  priv_addset(priv_set_t *set, int priv)
 481  481  {
 482  482          ASSERT(priv >= 0 && priv < MAX_PRIVILEGE);
 483      -        __PRIV_ASSERT(set, priv);
      483 +        __PRIV_ADDSET(set, priv);
 484  484  }
 485  485  
 486  486  void
 487  487  priv_delset(priv_set_t *set, int priv)
 488  488  {
 489  489          ASSERT(priv >= 0 && priv < MAX_PRIVILEGE);
 490      -        __PRIV_CLEAR(set, priv);
      490 +        __PRIV_DELSET(set, priv);
 491  491  }
 492  492  
 493  493  boolean_t
 494  494  priv_ismember(const priv_set_t *set, int priv)
 495  495  {
 496  496          ASSERT(priv >= 0 && priv < MAX_PRIVILEGE);
 497      -        return (__PRIV_ISASSERT(set, priv) ? B_TRUE : B_FALSE);
      497 +        return (__PRIV_ISMEMBER(set, priv) ? B_TRUE : B_FALSE);
 498  498  }
 499  499  
 500  500  #define PRIV_TEST_BODY(test) \
 501  501          int i; \
 502  502  \
 503  503          for (i = 0; i < PRIV_SETSIZE; i++) \
 504  504                  if (!(test)) \
 505  505                          return (B_FALSE); \
 506  506  \
 507  507          return (B_TRUE)
 508  508  
 509  509  boolean_t
 510  510  priv_isequalset(const priv_set_t *a, const priv_set_t *b)
 511  511  {
 512  512          return ((boolean_t)(bcmp(a, b, sizeof (*a)) == 0));
 513  513  }
 514  514  
 515  515  boolean_t
 516  516  priv_isemptyset(const priv_set_t *set)
 517  517  {
 518  518          PRIV_TEST_BODY(set->pbits[i] == 0);
 519  519  }
 520  520  
 521  521  boolean_t
 522  522  priv_isfullset(const priv_set_t *set)
 523  523  {
 524  524          PRIV_TEST_BODY(set->pbits[i] == ~(priv_chunk_t)0);
 525  525  }
 526  526  
 527  527  /*
 528  528   * Return true if a is a subset of b
 529  529   */
 530  530  boolean_t
 531  531  priv_issubset(const priv_set_t *a, const priv_set_t *b)
 532  532  {
 533  533          PRIV_TEST_BODY((a->pbits[i] | b->pbits[i]) == b->pbits[i]);
 534  534  }
 535  535  
 536  536  #define PRIV_CHANGE_BODY(a, op, b) \
 537  537          int i; \
 538  538  \
 539  539          for (i = 0; i < PRIV_SETSIZE; i++) \
 540  540                  a->pbits[i] op b->pbits[i]
 541  541  
 542  542  /* B = A ^ B */
 543  543  void
 544  544  priv_intersect(const priv_set_t *a, priv_set_t *b)
 545  545  {
 546  546          /* CSTYLED */
 547  547          PRIV_CHANGE_BODY(b, &=, a);
 548  548  }
 549  549  
 550  550  /* B = A v B */
 551  551  void
 552  552  priv_union(const priv_set_t *a, priv_set_t *b)
 553  553  {
 554  554          /* CSTYLED */
 555  555          PRIV_CHANGE_BODY(b, |=, a);
 556  556  }
 557  557  
 558  558  /* A = ! A */
 559  559  void
 560  560  priv_inverse(priv_set_t *a)
 561  561  {
 562  562          PRIV_CHANGE_BODY(a, = ~, a);
 563  563  }
 564  564  
 565  565  /*
 566  566   * Can the source cred act on the target credential?
 567  567   *
 568  568   * We will you allow to gain uids this way but not privileges.
 569  569   */
 570  570  int
 571  571  priv_proc_cred_perm(const cred_t *scr, proc_t *tp, cred_t **pcr, int mode)
 572  572  {
 573  573          const priv_set_t *eset;
 574  574          int idsmatch;
 575  575          cred_t *tcr;
 576  576          int res = 0;
 577  577  
 578  578          /* prevent the cred from going away */
 579  579          mutex_enter(&tp->p_crlock);
 580  580          crhold(tcr = tp->p_cred);
 581  581          mutex_exit(&tp->p_crlock);
 582  582  
 583  583          if (scr == tcr && !(tp->p_flag & SNOCD))
 584  584                  goto out;
 585  585  
 586  586          idsmatch = (scr->cr_uid == tcr->cr_uid &&
 587  587              scr->cr_uid == tcr->cr_ruid &&
 588  588              scr->cr_uid == tcr->cr_suid &&
 589  589              scr->cr_gid == tcr->cr_gid &&
 590  590              scr->cr_gid == tcr->cr_rgid &&
 591  591              scr->cr_gid == tcr->cr_sgid &&
 592  592              !(tp->p_flag & SNOCD));
 593  593  
 594  594          /*
 595  595           * Source credential must have the proc_zone privilege if referencing
 596  596           * a process in another zone.
 597  597           */
 598  598          if (scr->cr_zone != tcr->cr_zone && secpolicy_proc_zone(scr) != 0) {
 599  599                  res = EACCES;
 600  600                  goto out;
 601  601          }
 602  602  
 603  603          if (!(mode & VWRITE)) {
 604  604                  if (!idsmatch && secpolicy_proc_owner(scr, tcr, 0) != 0)
 605  605                          res = EACCES;
 606  606                  goto out;
 607  607          }
 608  608  
 609  609          /*
 610  610           * For writing, the effective set of scr must dominate all sets of tcr,
 611  611           * We test Pt <= Es (Et <= Pt so no need to test) and It <= Es
 612  612           * The Limit set of scr must be a superset of the limitset of
 613  613           * tcr.
 614  614           */
 615  615          eset = &CR_OEPRIV(scr);
 616  616  
 617  617          if (!priv_issubset(&CR_IPRIV(tcr), eset) ||
 618  618              !priv_issubset(&CR_OPPRIV(tcr), eset) ||
 619  619              !priv_issubset(&CR_LPRIV(tcr), &CR_LPRIV(scr)) ||
 620  620              !idsmatch && secpolicy_proc_owner(scr, tcr, mode) != 0)
 621  621                  res = EACCES;
 622  622  
 623  623  out:
 624  624          if (res == 0 && pcr != NULL)
 625  625                  *pcr = tcr;
 626  626          else
 627  627                  crfree(tcr);
 628  628          return (res);
 629  629  }
 630  630  
 631  631  /*
 632  632   * Set the privilege aware bit, adding L to E/P if necessary.
 633  633   * Each time we set it, we also clear PRIV_AWARE_RESET.
 634  634   */
 635  635  void
 636  636  priv_set_PA(cred_t *cr)
 637  637  {
 638  638          ASSERT(cr->cr_ref <= 2);
 639  639  
 640  640          if ((CR_FLAGS(cr) & (PRIV_AWARE|PRIV_AWARE_RESET)) == PRIV_AWARE)
 641  641                  return;
 642  642  
 643  643          CR_FLAGS(cr) |= PRIV_AWARE;
 644  644          CR_FLAGS(cr) &= ~PRIV_AWARE_RESET;
 645  645  
 646  646          if (cr->cr_uid == 0)
 647  647                  priv_union(&CR_LPRIV(cr), &CR_EPRIV(cr));
 648  648  
 649  649          if (cr->cr_uid == 0 || cr->cr_suid == 0 || cr->cr_ruid == 0)
 650  650                  priv_union(&CR_LPRIV(cr), &CR_PPRIV(cr));
 651  651  }
 652  652  
 653  653  boolean_t
 654  654  priv_can_clear_PA(const cred_t *cr)
 655  655  {
 656  656          /*
 657  657           * We can clear PA in the following cases:
 658  658           *
 659  659           * None of the uids are 0.
 660  660           * Any uid == 0 and P == L and (Euid != 0 or E == L)
 661  661           */
 662  662          return ((cr->cr_suid != 0 && cr->cr_ruid != 0 && cr->cr_uid != 0) ||
 663  663              priv_isequalset(&CR_PPRIV(cr), &CR_LPRIV(cr)) &&
 664  664              (cr->cr_uid != 0 || priv_isequalset(&CR_EPRIV(cr), &CR_LPRIV(cr))));
 665  665  }
 666  666  
 667  667  /*
 668  668   * Clear privilege aware bit if it is an idempotent operation and by
 669  669   * clearing it the process cannot get to uid 0 and all privileges.
 670  670   *
 671  671   * This function should be called with caution as it may cause "E" to be
 672  672   * lost once a processes assumes euid 0 again.
 673  673   */
 674  674  void
 675  675  priv_adjust_PA(cred_t *cr)
 676  676  {
 677  677          ASSERT(cr->cr_ref <= 2);
 678  678  
 679  679          if (!(CR_FLAGS(cr) & PRIV_AWARE) ||
 680  680              !priv_can_clear_PA(cr)) {
 681  681                  CR_FLAGS(cr) &= ~PRIV_AWARE_RESET;
 682  682                  return;
 683  683          }
 684  684  
 685  685          if (CR_FLAGS(cr) & PRIV_AWARE_INHERIT)
 686  686                  return;
 687  687  
 688  688          /*
 689  689           * We now need to adjust P/E in those cases when uids
 690  690           * are zero; the rules are P' = I & L, E' = I & L;
 691  691           * but since P = L and E = L, we can use P &= I, E &= I,
 692  692           * depending on which uids are 0.
 693  693           */
 694  694          if (cr->cr_suid == 0 || cr->cr_ruid == 0 || cr->cr_uid == 0) {
 695  695                  if (cr->cr_uid == 0)
 696  696                          priv_intersect(&CR_IPRIV(cr), &CR_EPRIV(cr));
 697  697                  priv_intersect(&CR_IPRIV(cr), &CR_PPRIV(cr));
 698  698          }
 699  699  
 700  700          CR_FLAGS(cr) &= ~(PRIV_AWARE|PRIV_AWARE_RESET);
 701  701  }
 702  702  
 703  703  /*
 704  704   * Reset privilege aware bit if so requested by setting the PRIV_AWARE_RESET
 705  705   * flag.
 706  706   */
 707  707  void
 708  708  priv_reset_PA(cred_t *cr, boolean_t finalize)
 709  709  {
 710  710          ASSERT(cr->cr_ref <= 2);
 711  711  
 712  712          if ((CR_FLAGS(cr) & (PRIV_AWARE|PRIV_AWARE_RESET)) !=
 713  713              (PRIV_AWARE|PRIV_AWARE_RESET)) {
 714  714                  CR_FLAGS(cr) &= ~PRIV_AWARE_RESET;
 715  715                  return;
 716  716          }
 717  717  
 718  718          /*
 719  719           * When PRIV_AWARE_RESET is enabled, any change of uids causes
 720  720           * a change to the P and E sets.  Bracketing with
 721  721           * seteuid(0) ... seteuid(uid)/setreuid(-1, 0) .. setreuid(-1, uid)
 722  722           * will cause the privilege sets "do the right thing.".
 723  723           * When the change of the uid is "final", e.g., by using setuid(uid),
 724  724           * or setreuid(uid, uid) or when the last set*uid() call causes all
 725  725           * uids to be the same, we set P and E to I & L, like when you exec.
 726  726           * We make an exception when all the uids are 0; this is required
 727  727           * when we login as root as in that particular case we cannot
 728  728           * make a distinction between seteuid(0) and seteuid(uid).
 729  729           * We rely on seteuid/setreuid/setuid to tell us with the
 730  730           * "finalize" argument that we no longer expect new uid changes,
 731  731           * cf. setreuid(uid, uid) and setuid(uid).
 732  732           */
 733  733          if (cr->cr_suid == cr->cr_ruid && cr->cr_suid == cr->cr_uid) {
 734  734                  if (finalize || cr->cr_uid != 0) {
 735  735                          CR_EPRIV(cr) = CR_IPRIV(cr);
 736  736                          priv_intersect(&CR_LPRIV(cr), &CR_EPRIV(cr));
 737  737                          CR_PPRIV(cr) = CR_EPRIV(cr);
 738  738                          CR_FLAGS(cr) &= ~(PRIV_AWARE|PRIV_AWARE_RESET);
 739  739                  } else {
 740  740                          CR_EPRIV(cr) = CR_PPRIV(cr);
 741  741                  }
 742  742          } else if (cr->cr_uid != 0 && (cr->cr_ruid == 0 || cr->cr_suid == 0)) {
 743  743                  CR_EPRIV(cr) = CR_IPRIV(cr);
 744  744                  priv_intersect(&CR_LPRIV(cr), &CR_EPRIV(cr));
 745  745          }
 746  746  }
  
    | 
      ↓ open down ↓ | 
    239 lines elided | 
    
      ↑ open up ↑ | 
  
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX