LCOV - code coverage report
Current view: top level - libcli/security - access_check.c (source / functions) Hit Total Coverage
Test: coverage report for master 2f515e9b Lines: 323 413 78.2 %
Date: 2024-04-21 15:09:00 Functions: 13 13 100.0 %

          Line data    Source code
       1             : /*
       2             :    Unix SMB/CIFS implementation.
       3             : 
       4             :    Copyright (C) Andrew Tridgell 2004
       5             :    Copyright (C) Gerald Carter 2005
       6             :    Copyright (C) Volker Lendecke 2007
       7             :    Copyright (C) Jeremy Allison 2008
       8             :    Copyright (C) Andrew Bartlett 2010
       9             : 
      10             :    This program is free software; you can redistribute it and/or modify
      11             :    it under the terms of the GNU General Public License as published by
      12             :    the Free Software Foundation; either version 3 of the License, or
      13             :    (at your option) any later version.
      14             : 
      15             :    This program is distributed in the hope that it will be useful,
      16             :    but WITHOUT ANY WARRANTY; without even the implied warranty of
      17             :    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      18             :    GNU General Public License for more details.
      19             : 
      20             :    You should have received a copy of the GNU General Public License
      21             :    along with this program.  If not, see <http://www.gnu.org/licenses/>.
      22             : */
      23             : 
      24             : #include "replace.h"
      25             : #include "lib/util/debug.h"
      26             : #include "libcli/security/security.h"
      27             : #include "librpc/gen_ndr/conditional_ace.h"
      28             : #include "libcli/security/conditional_ace.h"
      29             : 
      30             : /* Map generic access rights to object specific rights.  This technique is
      31             :    used to give meaning to assigning read, write, execute and all access to
      32             :    objects.  Each type of object has its own mapping of generic to object
      33             :    specific access rights. */
      34             : 
      35      694525 : void se_map_generic(uint32_t *access_mask, const struct generic_mapping *mapping)
      36             : {
      37      694525 :         uint32_t old_mask = *access_mask;
      38             : 
      39      694525 :         if (*access_mask & GENERIC_READ_ACCESS) {
      40        4511 :                 *access_mask &= ~GENERIC_READ_ACCESS;
      41        4511 :                 *access_mask |= mapping->generic_read;
      42             :         }
      43             : 
      44      694525 :         if (*access_mask & GENERIC_WRITE_ACCESS) {
      45         164 :                 *access_mask &= ~GENERIC_WRITE_ACCESS;
      46         164 :                 *access_mask |= mapping->generic_write;
      47             :         }
      48             : 
      49      694525 :         if (*access_mask & GENERIC_EXECUTE_ACCESS) {
      50       11539 :                 *access_mask &= ~GENERIC_EXECUTE_ACCESS;
      51       11539 :                 *access_mask |= mapping->generic_execute;
      52             :         }
      53             : 
      54      694525 :         if (*access_mask & GENERIC_ALL_ACCESS) {
      55        4153 :                 *access_mask &= ~GENERIC_ALL_ACCESS;
      56        4153 :                 *access_mask |= mapping->generic_all;
      57             :         }
      58             : 
      59      694525 :         if (old_mask != *access_mask) {
      60       12511 :                 DEBUG(10, ("se_map_generic(): mapped mask 0x%08x to 0x%08x\n",
      61             :                            old_mask, *access_mask));
      62             :         }
      63      694525 : }
      64             : 
      65             : /* Map generic access rights to object specific rights for all the ACE's
      66             :  * in a security_acl.
      67             :  */
      68             : 
      69       15341 : void security_acl_map_generic(struct security_acl *sa,
      70             :                                 const struct generic_mapping *mapping)
      71             : {
      72         149 :         unsigned int i;
      73             : 
      74       15341 :         if (!sa) {
      75           0 :                 return;
      76             :         }
      77             : 
      78       77219 :         for (i = 0; i < sa->num_aces; i++) {
      79       61878 :                 se_map_generic(&sa->aces[i].access_mask, mapping);
      80             :         }
      81             : }
      82             : 
      83             : /* Map standard access rights to object specific rights.  This technique is
      84             :    used to give meaning to assigning read, write, execute and all access to
      85             :    objects.  Each type of object has its own mapping of standard to object
      86             :    specific access rights. */
      87             : 
      88         624 : void se_map_standard(uint32_t *access_mask, const struct standard_mapping *mapping)
      89             : {
      90         624 :         uint32_t old_mask = *access_mask;
      91             : 
      92         624 :         if (*access_mask & SEC_STD_READ_CONTROL) {
      93          16 :                 *access_mask &= ~SEC_STD_READ_CONTROL;
      94          16 :                 *access_mask |= mapping->std_read;
      95             :         }
      96             : 
      97         624 :         if (*access_mask & (SEC_STD_DELETE|SEC_STD_WRITE_DAC|SEC_STD_WRITE_OWNER|SEC_STD_SYNCHRONIZE)) {
      98           8 :                 *access_mask &= ~(SEC_STD_DELETE|SEC_STD_WRITE_DAC|SEC_STD_WRITE_OWNER|SEC_STD_SYNCHRONIZE);
      99           8 :                 *access_mask |= mapping->std_all;
     100             :         }
     101             : 
     102         624 :         if (old_mask != *access_mask) {
     103           4 :                 DEBUG(10, ("se_map_standard(): mapped mask 0x%08x to 0x%08x\n",
     104             :                            old_mask, *access_mask));
     105             :         }
     106         624 : }
     107             : 
     108             : enum ace_callback_result {
     109             :         ACE_CALLBACK_DENY,
     110             :         ACE_CALLBACK_ALLOW,
     111             :         ACE_CALLBACK_SKIP,      /* do not apply this ACE */
     112             :         ACE_CALLBACK_INVALID    /* we don't want to process the conditional ACE */
     113             : };
     114             : 
     115             : 
     116         529 : static enum ace_callback_result check_callback_ace_allow(
     117             :         const struct security_ace *ace,
     118             :         const struct security_token *token,
     119             :         const struct security_descriptor *sd)
     120             : {
     121         135 :         bool ok;
     122         135 :         int result;
     123             : 
     124         529 :         switch (token->evaluate_claims) {
     125         394 :         case CLAIMS_EVALUATION_ALWAYS:
     126         529 :                 break;
     127             : 
     128           0 :         case CLAIMS_EVALUATION_INVALID_STATE:
     129           0 :                 DBG_WARNING("Refusing to evaluate ACL with "
     130             :                             "conditional ACE against security "
     131             :                             "token with CLAIMS_EVALUATION_INVALID_STATE\n");
     132           0 :                 return ACE_CALLBACK_INVALID;
     133           0 :         case CLAIMS_EVALUATION_NEVER:
     134             :         default:
     135             :                 /*
     136             :                  * We are asked to pretend we never understood this
     137             :                  * ACE type.
     138             :                  *
     139             :                  * By returning SKIP, this ACE will not adjust any
     140             :                  * permission bits making it an effective no-op, which
     141             :                  * was the default behaviour up to Samba 4.19.
     142             :                  */
     143           0 :                 return ACE_CALLBACK_SKIP;
     144             :         }
     145             : 
     146         529 :         if (ace->type != SEC_ACE_TYPE_ACCESS_ALLOWED_CALLBACK &&
     147           0 :             ace->type != SEC_ACE_TYPE_ACCESS_ALLOWED_CALLBACK_OBJECT) {
     148             :                 /* This indicates a programming error */
     149           0 :                 DBG_ERR("bad conditional allow ACE type: %u\n", ace->type);
     150           0 :                 return ACE_CALLBACK_INVALID;
     151             :         }
     152             : 
     153             :         /*
     154             :          * Until we discover otherwise, we assume all callback ACEs
     155             :          * are conditional ACEs.
     156             :          */
     157         529 :         ok = access_check_conditional_ace(ace, token, sd, &result);
     158         529 :         if (!ok) {
     159             :                 /*
     160             :                  * An error in processing the conditional ACE is
     161             :                  * treated as UNKNOWN, which amounts to a DENY/SKIP
     162             :                  * result.
     163             :                  *
     164             :                  * This is different from the INVALID result which
     165             :                  * means we should not be thinking about conditional
     166             :                  * ACES at all, and will abort the whole access check.
     167             :                  */
     168           0 :                 DBG_WARNING("callback ACE was not a valid conditional ACE\n");
     169           0 :                 return ACE_CALLBACK_SKIP;
     170             :         }
     171         529 :         if (result == ACE_CONDITION_TRUE) {
     172         284 :                 return ACE_CALLBACK_ALLOW;
     173             :         }
     174             :         /* UNKNOWN means do not allow */
     175         196 :         return ACE_CALLBACK_SKIP;
     176             : }
     177             : 
     178             : 
     179         142 : static enum ace_callback_result check_callback_ace_deny(
     180             :         const struct security_ace *ace,
     181             :         const struct security_token *token,
     182             :         const struct security_descriptor *sd)
     183             : {
     184           4 :         bool ok;
     185           4 :         int result;
     186             : 
     187         142 :         switch (token->evaluate_claims) {
     188         138 :         case CLAIMS_EVALUATION_ALWAYS:
     189         142 :                 break;
     190             : 
     191           0 :         case CLAIMS_EVALUATION_INVALID_STATE:
     192           0 :                 DBG_WARNING("Refusing to evaluate ACL with "
     193             :                             "conditional ACE against security "
     194             :                             "token with CLAIMS_EVALUATION_INVALID_STATE\n");
     195           0 :                 return ACE_CALLBACK_INVALID;
     196           0 :         case CLAIMS_EVALUATION_NEVER:
     197             :         default:
     198             :                 /*
     199             :                  * We are asked to pretend we never understood this
     200             :                  * ACE type.
     201             :                  */
     202           0 :                 return ACE_CALLBACK_SKIP;
     203             :         }
     204             : 
     205         142 :         if (ace->type != SEC_ACE_TYPE_ACCESS_DENIED_CALLBACK &&
     206           0 :             ace->type != SEC_ACE_TYPE_ACCESS_DENIED_CALLBACK_OBJECT) {
     207           0 :                 DBG_ERR("bad conditional deny ACE type: %u\n", ace->type);
     208           0 :                 return ACE_CALLBACK_INVALID;
     209             :         }
     210             : 
     211             :         /*
     212             :          * Until we discover otherwise, we assume all callback ACEs
     213             :          * are conditional ACEs.
     214             :          */
     215         142 :         ok = access_check_conditional_ace(ace, token, sd, &result);
     216         142 :         if (!ok) {
     217             :                 /*
     218             :                  * An error in processing the conditional ACE is
     219             :                  * treated as UNKNOWN, which means DENY.
     220             :                  */
     221           0 :                 DBG_WARNING("callback ACE was not a valid conditional ACE\n");
     222           0 :                 return ACE_CALLBACK_DENY;
     223             :         }
     224         142 :         if (result != ACE_CONDITION_FALSE) {
     225             :                 /* UNKNOWN means deny */
     226         120 :                 return ACE_CALLBACK_DENY;
     227             :         }
     228          22 :         return ACE_CALLBACK_SKIP;
     229             : }
     230             : 
     231             : 
     232             : /*
     233             :   perform a SEC_FLAG_MAXIMUM_ALLOWED access check
     234             : */
     235      110953 : static uint32_t access_check_max_allowed(const struct security_descriptor *sd,
     236             :                                          const struct security_token *token,
     237             :                                          enum implicit_owner_rights implicit_owner_rights)
     238             : {
     239      110953 :         uint32_t denied = 0, granted = 0;
     240      110953 :         bool am_owner = false;
     241      110953 :         bool have_owner_rights_ace = false;
     242        1774 :         unsigned i;
     243             : 
     244      110953 :         if (sd->dacl == NULL) {
     245           0 :                 if (security_token_has_sid(token, sd->owner_sid)) {
     246           0 :                         switch (implicit_owner_rights) {
     247           0 :                         case IMPLICIT_OWNER_READ_CONTROL_AND_WRITE_DAC_RIGHTS:
     248           0 :                                 granted |= SEC_STD_WRITE_DAC;
     249           0 :                                 FALL_THROUGH;
     250           0 :                         case IMPLICIT_OWNER_READ_CONTROL_RIGHTS:
     251           0 :                                 granted |= SEC_STD_READ_CONTROL;
     252           0 :                                 break;
     253             :                         }
     254             :                 }
     255           0 :                 return granted;
     256             :         }
     257             : 
     258      110953 :         if (security_token_has_sid(token, sd->owner_sid)) {
     259             :                 /*
     260             :                  * Check for explicit owner rights: if there are none, we remove
     261             :                  * the default owner right SEC_STD_WRITE_DAC|SEC_STD_READ_CONTROL
     262             :                  * from remaining_access. Otherwise we just process the
     263             :                  * explicitly granted rights when processing the ACEs.
     264             :                  */
     265        5329 :                 am_owner = true;
     266             : 
     267       36301 :                 for (i=0; i < sd->dacl->num_aces; i++) {
     268       31862 :                         struct security_ace *ace = &sd->dacl->aces[i];
     269             : 
     270       31862 :                         if (ace->flags & SEC_ACE_FLAG_INHERIT_ONLY) {
     271        1507 :                                 continue;
     272             :                         }
     273             : 
     274       31219 :                         have_owner_rights_ace = dom_sid_equal(
     275       30355 :                                 &ace->trustee, &global_sid_Owner_Rights);
     276       30355 :                         if (have_owner_rights_ace) {
     277          12 :                                 break;
     278             :                         }
     279             :                 }
     280             :         }
     281             : 
     282      110953 :         if (am_owner && !have_owner_rights_ace) {
     283        4439 :                 switch (implicit_owner_rights) {
     284        4439 :                 case IMPLICIT_OWNER_READ_CONTROL_AND_WRITE_DAC_RIGHTS:
     285        4439 :                         granted |= SEC_STD_WRITE_DAC;
     286         114 :                         FALL_THROUGH;
     287        4439 :                 case IMPLICIT_OWNER_READ_CONTROL_RIGHTS:
     288        4439 :                         granted |= SEC_STD_READ_CONTROL;
     289        4439 :                         break;
     290             :                 }
     291             :         }
     292             : 
     293      250009 :         for (i = 0;i<sd->dacl->num_aces; i++) {
     294      139056 :                 struct security_ace *ace = &sd->dacl->aces[i];
     295      139056 :                 bool is_owner_rights_ace = false;
     296             : 
     297      139056 :                 if (ace->flags & SEC_ACE_FLAG_INHERIT_ONLY) {
     298        1507 :                         continue;
     299             :                 }
     300             : 
     301      137549 :                 if (am_owner) {
     302       30363 :                         is_owner_rights_ace = dom_sid_equal(
     303       30363 :                                 &ace->trustee, &global_sid_Owner_Rights);
     304             :                 }
     305             : 
     306      138413 :                 if (!is_owner_rights_ace &&
     307      137533 :                     !security_token_has_sid(token, &ace->trustee))
     308             :                 {
     309       23321 :                         continue;
     310             :                 }
     311             : 
     312      114228 :                 switch (ace->type) {
     313      114189 :                 case SEC_ACE_TYPE_ACCESS_ALLOWED:
     314      114189 :                         granted |= ace->access_mask;
     315      114189 :                         break;
     316          18 :                 case SEC_ACE_TYPE_ACCESS_DENIED:
     317             :                 case SEC_ACE_TYPE_ACCESS_DENIED_OBJECT:
     318          18 :                         denied |= ~granted & ace->access_mask;
     319          18 :                         break;
     320             : 
     321           0 :                 case SEC_ACE_TYPE_ACCESS_ALLOWED_CALLBACK:
     322             :                 {
     323           0 :                         enum ace_callback_result allow =
     324           0 :                                 check_callback_ace_allow(ace, token, sd);
     325           0 :                         if (allow == ACE_CALLBACK_INVALID) {
     326           0 :                                 return 0;
     327             :                         }
     328           0 :                         if (allow == ACE_CALLBACK_ALLOW) {
     329           0 :                                 granted |= ace->access_mask;
     330             :                         }
     331           0 :                         break;
     332             :                 }
     333             : 
     334           0 :                 case SEC_ACE_TYPE_ACCESS_DENIED_CALLBACK:
     335             :                 {
     336           0 :                         enum ace_callback_result deny =
     337           0 :                                 check_callback_ace_deny(ace, token, sd);
     338           0 :                         if (deny == ACE_CALLBACK_INVALID) {
     339           0 :                                 return 0;
     340             :                         }
     341           0 :                         if (deny == ACE_CALLBACK_DENY) {
     342           0 :                                 denied |= ~granted & ace->access_mask;
     343             :                         }
     344           0 :                         break;
     345             :                 }
     346             : 
     347          21 :                 default:        /* Other ACE types not handled/supported */
     348          21 :                         break;
     349             :                 }
     350             :         }
     351             : 
     352      110953 :         return granted & ~denied;
     353             : }
     354             : 
     355             : 
     356             : 
     357      869661 : static NTSTATUS se_access_check_implicit_owner(const struct security_descriptor *sd,
     358             :                                                const struct security_token *token,
     359             :                                                uint32_t access_desired,
     360             :                                                uint32_t *access_granted,
     361             :                                                enum implicit_owner_rights implicit_owner_rights)
     362             : {
     363        3127 :         uint32_t i;
     364        3127 :         uint32_t bits_remaining;
     365      869661 :         uint32_t explicitly_denied_bits = 0;
     366      869661 :         bool am_owner = false;
     367      869661 :         bool have_owner_rights_ace = false;
     368             : 
     369      869661 :         switch (token->evaluate_claims) {
     370      199093 :         case CLAIMS_EVALUATION_INVALID_STATE:
     371      199093 :                 if (token->num_local_claims > 0 ||
     372      199093 :                     token->num_user_claims > 0 ||
     373      199093 :                     token->num_device_claims > 0 ||
     374      199093 :                     token->num_device_sids > 0) {
     375           0 :                         DBG_WARNING("Refusing to evaluate token with claims or device SIDs but also "
     376             :                                     "with CLAIMS_EVALUATION_INVALID_STATE\n");
     377           0 :                         return NT_STATUS_INVALID_TOKEN;
     378             :                 }
     379      199021 :                 break;
     380      667513 :         case CLAIMS_EVALUATION_ALWAYS:
     381             :         case CLAIMS_EVALUATION_NEVER:
     382      667513 :                 break;
     383             :         }
     384             : 
     385      869661 :         *access_granted = access_desired;
     386      869661 :         bits_remaining = access_desired;
     387             : 
     388             :         /* handle the maximum allowed flag */
     389      869661 :         if (access_desired & SEC_FLAG_MAXIMUM_ALLOWED) {
     390        4659 :                 uint32_t orig_access_desired = access_desired;
     391             : 
     392        4659 :                 access_desired |= access_check_max_allowed(sd, token, implicit_owner_rights);
     393        4659 :                 access_desired &= ~SEC_FLAG_MAXIMUM_ALLOWED;
     394        4659 :                 *access_granted = access_desired;
     395        4659 :                 bits_remaining = access_desired;
     396             : 
     397        4659 :                 DEBUG(10,("se_access_check: MAX desired = 0x%x, granted = 0x%x, remaining = 0x%x\n",
     398             :                         orig_access_desired,
     399             :                         *access_granted,
     400             :                         bits_remaining));
     401             :         }
     402             : 
     403             :         /* a NULL dacl allows access */
     404      869661 :         if ((sd->type & SEC_DESC_DACL_PRESENT) && sd->dacl == NULL) {
     405          77 :                 *access_granted = access_desired;
     406          77 :                 return NT_STATUS_OK;
     407             :         }
     408             : 
     409      869584 :         if (sd->dacl == NULL) {
     410           1 :                 goto done;
     411             :         }
     412             : 
     413      869583 :         if (security_token_has_sid(token, sd->owner_sid)) {
     414             :                 /*
     415             :                  * Check for explicit owner rights: if there are none, we remove
     416             :                  * the default owner right SEC_STD_WRITE_DAC|SEC_STD_READ_CONTROL
     417             :                  * from remaining_access. Otherwise we just process the
     418             :                  * explicitly granted rights when processing the ACEs.
     419             :                  */
     420      387025 :                 am_owner = true;
     421             : 
     422     1658269 :                 for (i=0; i < sd->dacl->num_aces; i++) {
     423     1275242 :                         struct security_ace *ace = &sd->dacl->aces[i];
     424             : 
     425     1275242 :                         if (ace->flags & SEC_ACE_FLAG_INHERIT_ONLY) {
     426       81655 :                                 continue;
     427             :                         }
     428             : 
     429     1196827 :                         have_owner_rights_ace = dom_sid_equal(
     430     1193587 :                                 &ace->trustee, &global_sid_Owner_Rights);
     431     1193587 :                         if (have_owner_rights_ace) {
     432          80 :                                 break;
     433             :                         }
     434             :                 }
     435             :         }
     436      869583 :         if (am_owner && !have_owner_rights_ace) {
     437      383027 :                 switch (implicit_owner_rights) {
     438      383027 :                 case IMPLICIT_OWNER_READ_CONTROL_AND_WRITE_DAC_RIGHTS:
     439      383027 :                         bits_remaining &= ~SEC_STD_WRITE_DAC;
     440         906 :                         FALL_THROUGH;
     441      383027 :                 case IMPLICIT_OWNER_READ_CONTROL_RIGHTS:
     442      383027 :                         bits_remaining &= ~SEC_STD_READ_CONTROL;
     443      383027 :                         break;
     444             :                 }
     445             :         }
     446             : 
     447             :         /* check each ace in turn. */
     448     1775314 :         for (i=0; bits_remaining && i < sd->dacl->num_aces; i++) {
     449      905731 :                 struct security_ace *ace = &sd->dacl->aces[i];
     450      905731 :                 bool is_owner_rights_ace = false;
     451             : 
     452      905731 :                 if (ace->flags & SEC_ACE_FLAG_INHERIT_ONLY) {
     453        4233 :                         continue;
     454             :                 }
     455             : 
     456      901498 :                 if (am_owner) {
     457      390891 :                         is_owner_rights_ace = dom_sid_equal(
     458      390891 :                                 &ace->trustee, &global_sid_Owner_Rights);
     459             :                 }
     460             : 
     461      902557 :                 if (!is_owner_rights_ace &&
     462      901394 :                     !security_token_has_sid(token, &ace->trustee))
     463             :                 {
     464       39906 :                         continue;
     465             :                 }
     466             : 
     467      861592 :                 switch (ace->type) {
     468      861098 :                 case SEC_ACE_TYPE_ACCESS_ALLOWED:
     469      861098 :                         bits_remaining &= ~ace->access_mask;
     470      861098 :                         break;
     471         107 :                 case SEC_ACE_TYPE_ACCESS_DENIED:
     472             :                 case SEC_ACE_TYPE_ACCESS_DENIED_OBJECT:
     473         107 :                         explicitly_denied_bits |= (bits_remaining & ace->access_mask);
     474         107 :                         break;
     475             : 
     476         135 :                 case SEC_ACE_TYPE_ACCESS_ALLOWED_CALLBACK:
     477             :                 {
     478         135 :                         enum ace_callback_result allow =
     479         135 :                                 check_callback_ace_allow(ace, token, sd);
     480         135 :                         if (allow == ACE_CALLBACK_INVALID) {
     481           0 :                                 return NT_STATUS_INVALID_ACE_CONDITION;
     482             :                         }
     483         135 :                         if (allow == ACE_CALLBACK_ALLOW) {
     484          86 :                                 bits_remaining &= ~ace->access_mask;
     485             :                         }
     486           0 :                         break;
     487             :                 }
     488             : 
     489           4 :                 case SEC_ACE_TYPE_ACCESS_DENIED_CALLBACK:
     490             :                 case SEC_ACE_TYPE_ACCESS_DENIED_CALLBACK_OBJECT:
     491             :                 {
     492           4 :                         enum ace_callback_result deny =
     493           4 :                                 check_callback_ace_deny(ace, token, sd);
     494           4 :                         if (deny == ACE_CALLBACK_INVALID) {
     495           0 :                                 return NT_STATUS_INVALID_ACE_CONDITION;
     496             :                         }
     497           4 :                         if (deny == ACE_CALLBACK_DENY) {
     498           4 :                                 explicitly_denied_bits |= (bits_remaining & ace->access_mask);
     499             :                         }
     500           0 :                         break;
     501             :                 }
     502             : 
     503         248 :                 default:        /* Other ACE types not handled/supported */
     504         248 :                         break;
     505             :                 }
     506             :         }
     507             : 
     508             :         /* Explicitly denied bits always override */
     509      869583 :         bits_remaining |= explicitly_denied_bits;
     510             : 
     511             :         /*
     512             :          * We check privileges here because they override even DENY entries.
     513             :          */
     514             : 
     515             :         /* Does the user have the privilege to gain SEC_PRIV_SECURITY? */
     516      869583 :         if (bits_remaining & SEC_FLAG_SYSTEM_SECURITY) {
     517       21385 :                 if (security_token_has_privilege(token, SEC_PRIV_SECURITY)) {
     518       19904 :                         bits_remaining &= ~SEC_FLAG_SYSTEM_SECURITY;
     519             :                 } else {
     520        1481 :                         return NT_STATUS_PRIVILEGE_NOT_HELD;
     521             :                 }
     522             :         }
     523             : 
     524      872385 :         if ((bits_remaining & SEC_STD_WRITE_OWNER) &&
     525        4283 :              security_token_has_privilege(token, SEC_PRIV_TAKE_OWNERSHIP)) {
     526        2917 :                 bits_remaining &= ~(SEC_STD_WRITE_OWNER);
     527             :         }
     528             : 
     529      865185 : done:
     530      868103 :         if (bits_remaining != 0) {
     531       13090 :                 *access_granted = bits_remaining;
     532       13090 :                 return NT_STATUS_ACCESS_DENIED;
     533             :         }
     534             : 
     535      855013 :         return NT_STATUS_OK;
     536             : }
     537             : 
     538             : /*
     539             :   The main entry point for access checking. If returning ACCESS_DENIED
     540             :   this function returns the denied bits in the uint32_t pointed
     541             :   to by the access_granted pointer.
     542             : */
     543      210562 : NTSTATUS se_access_check(const struct security_descriptor *sd,
     544             :                          const struct security_token *token,
     545             :                          uint32_t access_desired,
     546             :                          uint32_t *access_granted)
     547             : {
     548      210562 :         return se_access_check_implicit_owner(sd,
     549             :                                               token,
     550             :                                               access_desired,
     551             :                                               access_granted,
     552             :                                               IMPLICIT_OWNER_READ_CONTROL_AND_WRITE_DAC_RIGHTS);
     553             : }
     554             : 
     555             : /*
     556             :   The main entry point for access checking FOR THE FILE SERVER ONLY !
     557             :   If returning ACCESS_DENIED this function returns the denied bits in
     558             :   the uint32_t pointed to by the access_granted pointer.
     559             : */
     560      659099 : NTSTATUS se_file_access_check(const struct security_descriptor *sd,
     561             :                           const struct security_token *token,
     562             :                           bool priv_open_requested,
     563             :                           uint32_t access_desired,
     564             :                           uint32_t *access_granted)
     565             : {
     566        2770 :         uint32_t bits_remaining;
     567        2770 :         NTSTATUS status;
     568             : 
     569      659099 :         if (!priv_open_requested) {
     570             :                 /* Fall back to generic se_access_check(). */
     571      552775 :                 return se_access_check_implicit_owner(sd,
     572             :                                                       token,
     573             :                                                       access_desired,
     574             :                                                       access_granted,
     575             :                                                       IMPLICIT_OWNER_READ_CONTROL_AND_WRITE_DAC_RIGHTS);
     576             :         }
     577             : 
     578             :         /*
     579             :          * We need to handle the maximum allowed flag
     580             :          * outside of se_access_check(), as we need to
     581             :          * add in the access allowed by the privileges
     582             :          * as well.
     583             :          */
     584             : 
     585      106324 :         if (access_desired & SEC_FLAG_MAXIMUM_ALLOWED) {
     586      106294 :                 uint32_t orig_access_desired = access_desired;
     587             : 
     588      106294 :                 access_desired |= access_check_max_allowed(sd, token, true);
     589      106294 :                 access_desired &= ~SEC_FLAG_MAXIMUM_ALLOWED;
     590             : 
     591      106294 :                 if (security_token_has_privilege(token, SEC_PRIV_BACKUP)) {
     592       19394 :                         access_desired |= SEC_RIGHTS_PRIV_BACKUP;
     593             :                 }
     594             : 
     595      106294 :                 if (security_token_has_privilege(token, SEC_PRIV_RESTORE)) {
     596       20874 :                         access_desired |= SEC_RIGHTS_PRIV_RESTORE;
     597             :                 }
     598             : 
     599      106294 :                 DEBUG(10,("se_file_access_check: MAX desired = 0x%x "
     600             :                         "mapped to 0x%x\n",
     601             :                         orig_access_desired,
     602             :                         access_desired));
     603             :         }
     604             : 
     605      106324 :         status = se_access_check_implicit_owner(sd,
     606             :                                                 token,
     607             :                                                 access_desired,
     608             :                                                 access_granted,
     609             :                                                 IMPLICIT_OWNER_READ_CONTROL_AND_WRITE_DAC_RIGHTS);
     610             : 
     611      106324 :         if (!NT_STATUS_EQUAL(status, NT_STATUS_ACCESS_DENIED)) {
     612      103826 :                 return status;
     613             :         }
     614             : 
     615        2498 :         bits_remaining = *access_granted;
     616             : 
     617             :         /* Check if we should override with privileges. */
     618        4996 :         if ((bits_remaining & SEC_RIGHTS_PRIV_BACKUP) &&
     619        2498 :             security_token_has_privilege(token, SEC_PRIV_BACKUP)) {
     620        2498 :                 bits_remaining &= ~(SEC_RIGHTS_PRIV_BACKUP);
     621             :         }
     622        4996 :         if ((bits_remaining & SEC_RIGHTS_PRIV_RESTORE) &&
     623        2498 :             security_token_has_privilege(token, SEC_PRIV_RESTORE)) {
     624        2498 :                 bits_remaining &= ~(SEC_RIGHTS_PRIV_RESTORE);
     625             :         }
     626        2498 :         if (bits_remaining != 0) {
     627           0 :                 *access_granted = bits_remaining;
     628           0 :                 return NT_STATUS_ACCESS_DENIED;
     629             :         }
     630             : 
     631        2498 :         return NT_STATUS_OK;
     632             : }
     633             : 
     634     5677292 : static const struct GUID *get_ace_object_type(const struct security_ace *ace)
     635             : {
     636     5677292 :         if (ace->object.object.flags & SEC_ACE_OBJECT_TYPE_PRESENT) {
     637     5402078 :                 return &ace->object.object.type.type;
     638             :         }
     639             : 
     640      275214 :         return NULL;
     641             : }
     642             : 
     643             : /**
     644             :  * Evaluates access rights specified in a object-specific ACE for an AD object.
     645             :  * This logic corresponds to MS-ADTS 5.1.3.3.3 Checking Object-Specific Access.
     646             :  * @param[in] ace - the ACE being processed
     647             :  * @param[in/out] tree - remaining_access gets updated for the tree
     648             :  * @param[out] grant_access - set to true if the ACE grants sufficient access
     649             :  *                            rights to the object/attribute
     650             :  * @returns NT_STATUS_OK, unless access was denied
     651             :  */
     652     6600062 : static NTSTATUS check_object_specific_access(const struct security_ace *ace,
     653             :                                              struct object_tree *tree,
     654             :                                              bool *grant_access)
     655             : {
     656     6600062 :         struct object_tree *node = NULL;
     657     6600062 :         const struct GUID *type = NULL;
     658             : 
     659     6600062 :         *grant_access = false;
     660             : 
     661             :         /* if no tree was supplied, we can't do object-specific access checks */
     662     6600062 :         if (!tree) {
     663      922770 :                 return NT_STATUS_OK;
     664             :         }
     665             : 
     666             :         /* Get the ObjectType GUID this ACE applies to */
     667     5677292 :         type = get_ace_object_type(ace);
     668             : 
     669             :         /*
     670             :          * If the ACE doesn't have a type, then apply it to the whole tree, i.e.
     671             :          * treat 'OA' ACEs as 'A' and 'OD' as 'D'
     672             :          */
     673     5677292 :         if (!type) {
     674      275214 :                 node = tree;
     675             :         } else {
     676             : 
     677             :                 /* skip it if the ACE's ObjectType GUID is not in the tree */
     678     5402078 :                 node = get_object_tree_by_GUID(tree, type);
     679     5402078 :                 if (!node) {
     680     4726285 :                         return NT_STATUS_OK;
     681             :                 }
     682             :         }
     683             : 
     684      951007 :         if (ace->type == SEC_ACE_TYPE_ACCESS_ALLOWED_OBJECT ||
     685         590 :             ace->type == SEC_ACE_TYPE_ACCESS_ALLOWED_CALLBACK_OBJECT) {
     686             :                 /* apply the access rights to this node, and any children */
     687      950417 :                 object_tree_modify_access(node, ace->access_mask);
     688             : 
     689             :                 /*
     690             :                  * Currently all nodes in the tree request the same access mask,
     691             :                  * so we can use any node to check if processing this ACE now
     692             :                  * means the requested access has been granted
     693             :                  */
     694      950417 :                 if (node->remaining_access == 0) {
     695      935611 :                         *grant_access = true;
     696      935611 :                         return NT_STATUS_OK;
     697             :                 }
     698             : 
     699             :                 /*
     700             :                  * As per 5.1.3.3.4 Checking Control Access Right-Based Access,
     701             :                  * if the CONTROL_ACCESS right is present, then we can grant
     702             :                  * access and stop any further access checks
     703             :                  */
     704       14806 :                 if (ace->access_mask & SEC_ADS_CONTROL_ACCESS) {
     705         894 :                         *grant_access = true;
     706         894 :                         return NT_STATUS_OK;
     707             :                 }
     708             :         } else {
     709             : 
     710             :                 /* this ACE denies access to the requested object/attribute */
     711         590 :                 if (node->remaining_access & ace->access_mask){
     712         554 :                         return NT_STATUS_ACCESS_DENIED;
     713             :                 }
     714             :         }
     715       13948 :         return NT_STATUS_OK;
     716             : }
     717             : 
     718             : 
     719     8805190 : NTSTATUS sec_access_check_ds_implicit_owner(const struct security_descriptor *sd,
     720             :                                             const struct security_token *token,
     721             :                                             uint32_t access_desired,
     722             :                                             uint32_t *access_granted,
     723             :                                             struct object_tree *tree,
     724             :                                             const struct dom_sid *replace_sid,
     725             :                                             enum implicit_owner_rights implicit_owner_rights)
     726             : {
     727       89132 :         uint32_t i;
     728       89132 :         uint32_t bits_remaining;
     729             : 
     730     8805190 :         *access_granted = access_desired;
     731     8805190 :         bits_remaining = access_desired;
     732             : 
     733             :         /* handle the maximum allowed flag */
     734     8805190 :         if (access_desired & SEC_FLAG_MAXIMUM_ALLOWED) {
     735           0 :                 access_desired |= access_check_max_allowed(sd, token, implicit_owner_rights);
     736           0 :                 access_desired &= ~SEC_FLAG_MAXIMUM_ALLOWED;
     737           0 :                 *access_granted = access_desired;
     738           0 :                 bits_remaining = access_desired;
     739             :         }
     740             : 
     741     8805190 :         if (access_desired & SEC_FLAG_SYSTEM_SECURITY) {
     742       35697 :                 if (security_token_has_privilege(token, SEC_PRIV_SECURITY)) {
     743       34753 :                         bits_remaining &= ~SEC_FLAG_SYSTEM_SECURITY;
     744             :                 } else {
     745         944 :                         return NT_STATUS_PRIVILEGE_NOT_HELD;
     746             :                 }
     747             :         }
     748             : 
     749             :         /* the owner always gets SEC_STD_WRITE_DAC and SEC_STD_READ_CONTROL */
     750     8856141 :         if ((bits_remaining & (SEC_STD_WRITE_DAC|SEC_STD_READ_CONTROL)) &&
     751       51895 :             security_token_has_sid(token, sd->owner_sid)) {
     752       49481 :                 switch (implicit_owner_rights) {
     753       27049 :                 case IMPLICIT_OWNER_READ_CONTROL_AND_WRITE_DAC_RIGHTS:
     754       27049 :                         bits_remaining &= ~SEC_STD_WRITE_DAC;
     755          88 :                         FALL_THROUGH;
     756       49481 :                 case IMPLICIT_OWNER_READ_CONTROL_RIGHTS:
     757       49481 :                         bits_remaining &= ~SEC_STD_READ_CONTROL;
     758       49481 :                         break;
     759             :                 }
     760             :         }
     761             : 
     762             :         /* SEC_PRIV_TAKE_OWNERSHIP grants SEC_STD_WRITE_OWNER */
     763     8817498 :         if ((bits_remaining & (SEC_STD_WRITE_OWNER)) &&
     764       13252 :             security_token_has_privilege(token, SEC_PRIV_TAKE_OWNERSHIP)) {
     765       12880 :                 bits_remaining &= ~(SEC_STD_WRITE_OWNER);
     766             :         }
     767             : 
     768             :         /* a NULL dacl allows access */
     769     8804246 :         if ((sd->type & SEC_DESC_DACL_PRESENT) && sd->dacl == NULL) {
     770           0 :                 *access_granted = access_desired;
     771           0 :                 return NT_STATUS_OK;
     772             :         }
     773             : 
     774     8804246 :         if (sd->dacl == NULL) {
     775           2 :                 goto done;
     776             :         }
     777             : 
     778             :         /* check each ace in turn. */
     779    47396660 :         for (i=0; bits_remaining && i < sd->dacl->num_aces; i++) {
     780      511452 :                 const struct dom_sid *trustee;
     781    39545118 :                 const struct security_ace *ace = &sd->dacl->aces[i];
     782      511452 :                 NTSTATUS status;
     783    39545118 :                 bool grant_access = false;
     784             : 
     785    39545118 :                 if (ace->flags & SEC_ACE_FLAG_INHERIT_ONLY) {
     786    23945971 :                         continue;
     787             :                 }
     788             : 
     789    33611908 :                 if (dom_sid_equal(&ace->trustee, &global_sid_Self) && replace_sid) {
     790     6509504 :                         trustee = replace_sid;
     791             :                 } else {
     792    27100913 :                         trustee = &ace->trustee;
     793             :                 }
     794             : 
     795    33611908 :                 if (!security_token_has_sid(token, trustee)) {
     796    18012761 :                         continue;
     797             :                 }
     798             : 
     799    15599147 :                 switch (ace->type) {
     800     8963565 :                 case SEC_ACE_TYPE_ACCESS_ALLOWED:
     801     8963565 :                         if (tree) {
     802     8043037 :                                 object_tree_modify_access(tree, ace->access_mask);
     803             :                         }
     804             : 
     805     8963565 :                         bits_remaining &= ~ace->access_mask;
     806     8963565 :                         break;
     807       34988 :                 case SEC_ACE_TYPE_ACCESS_DENIED:
     808       34988 :                         if (bits_remaining & ace->access_mask) {
     809       15527 :                                 return NT_STATUS_ACCESS_DENIED;
     810             :                         }
     811       19461 :                         break;
     812         394 :                 case SEC_ACE_TYPE_ACCESS_ALLOWED_CALLBACK:
     813             :                 {
     814           0 :                         enum ace_callback_result allow =
     815         394 :                                 check_callback_ace_allow(ace, token, sd);
     816         394 :                         if (allow == ACE_CALLBACK_INVALID) {
     817           0 :                                 return NT_STATUS_INVALID_ACE_CONDITION;
     818             :                         }
     819         394 :                         if (allow == ACE_CALLBACK_ALLOW) {
     820         198 :                                 bits_remaining &= ~ace->access_mask;
     821             :                         }
     822         394 :                         break;
     823             :                 }
     824             : 
     825         138 :                 case SEC_ACE_TYPE_ACCESS_DENIED_CALLBACK:
     826             :                 {
     827           0 :                         enum ace_callback_result deny =
     828         138 :                                 check_callback_ace_deny(ace, token, sd);
     829         138 :                         if (deny == ACE_CALLBACK_INVALID) {
     830           0 :                                 return NT_STATUS_INVALID_ACE_CONDITION;
     831             :                         }
     832         138 :                         if (deny == ACE_CALLBACK_DENY) {
     833         116 :                                 if (bits_remaining & ace->access_mask) {
     834         116 :                                         return NT_STATUS_ACCESS_DENIED;
     835             :                                 }
     836             :                         }
     837          22 :                         break;
     838             :                 }
     839             : 
     840     6600062 :                 case SEC_ACE_TYPE_ACCESS_DENIED_OBJECT:
     841             :                 case SEC_ACE_TYPE_ACCESS_ALLOWED_OBJECT:
     842     6600062 :                         status = check_object_specific_access(ace, tree,
     843             :                                                               &grant_access);
     844             : 
     845     6600062 :                         if (!NT_STATUS_IS_OK(status)) {
     846         554 :                                 return status;
     847             :                         }
     848             : 
     849     6599508 :                         if (grant_access) {
     850      936505 :                                 return NT_STATUS_OK;
     851             :                         }
     852     5486129 :                         break;
     853           0 :                 case SEC_ACE_TYPE_ACCESS_ALLOWED_CALLBACK_OBJECT:
     854             :                 {
     855             :                         /*
     856             :                          * if the callback says ALLOW, we treat this as a
     857             :                          * SEC_ACE_TYPE_ACCESS_ALLOWED_OBJECT.
     858             :                          *
     859             :                          * Otherwise we act as if this ACE does not exist.
     860             :                          */
     861           0 :                         enum ace_callback_result allow =
     862           0 :                                 check_callback_ace_allow(ace, token, sd);
     863           0 :                         if (allow == ACE_CALLBACK_INVALID) {
     864           0 :                                 return NT_STATUS_INVALID_ACE_CONDITION;
     865             :                         }
     866           0 :                         if (allow != ACE_CALLBACK_ALLOW) {
     867           0 :                                 break;
     868             :                         }
     869             : 
     870           0 :                         status = check_object_specific_access(ace, tree,
     871             :                                                               &grant_access);
     872             : 
     873           0 :                         if (!NT_STATUS_IS_OK(status)) {
     874           0 :                                 return status;
     875             :                         }
     876             : 
     877           0 :                         if (grant_access) {
     878           0 :                                 return NT_STATUS_OK;
     879             :                         }
     880           0 :                         break;
     881             :                 }
     882           0 :                 case SEC_ACE_TYPE_ACCESS_DENIED_CALLBACK_OBJECT:
     883             :                 {
     884             :                         /*
     885             :                          * ACCESS_DENIED_OBJECT ACEs can't grant access --
     886             :                          * they either don't match the object and slide
     887             :                          * harmlessly past or they return
     888             :                          * NT_STATUS_ACCESS_DENIED.
     889             :                          *
     890             :                          * ACCESS_DENIED_CALLBACK_OBJECT ACEs add another way
     891             :                          * of not applying, and another way of failing.
     892             :                          */
     893           0 :                         enum ace_callback_result deny =
     894           0 :                                 check_callback_ace_deny(ace, token, sd);
     895           0 :                         if (deny == ACE_CALLBACK_INVALID) {
     896           0 :                                 return NT_STATUS_INVALID_ACE_CONDITION;
     897             :                         }
     898           0 :                         if (deny != ACE_CALLBACK_DENY) {
     899           0 :                                 break;
     900             :                         }
     901           0 :                         status = check_object_specific_access(ace, tree,
     902             :                                                               &grant_access);
     903             : 
     904           0 :                         if (!NT_STATUS_IS_OK(status)) {
     905           0 :                                 return status;
     906             :                         }
     907           0 :                         break;
     908             :                 }
     909           0 :                 default:        /* Other ACE types not handled/supported */
     910           0 :                         break;
     911             :                 }
     912             :         }
     913             : 
     914     7851542 : done:
     915     7851544 :         if (bits_remaining != 0) {
     916       65594 :                 return NT_STATUS_ACCESS_DENIED;
     917             :         }
     918             : 
     919     7785950 :         return NT_STATUS_OK;
     920             : }
     921             : 
     922             : /**
     923             :  * @brief Perform directoryservice (DS) related access checks for a given user
     924             :  *
     925             :  * Perform DS access checks for the user represented by its security_token, on
     926             :  * the provided security descriptor. If an tree associating GUID and access
     927             :  * required is provided then object access (OA) are checked as well. *
     928             :  * @param[in]   sd             The security descriptor against which the required
     929             :  *                             access are requested
     930             :  *
     931             :  * @param[in]   token          The security_token associated with the user to
     932             :  *                             test
     933             :  *
     934             :  * @param[in]   access_desired A bitfield of rights that must be granted for the
     935             :  *                             given user in the specified SD.
     936             :  *
     937             :  * If one
     938             :  * of the entry in the tree grants all the requested rights for the given GUID
     939             :  * FIXME
     940             :  * tree can be null if not null it's the
     941             :  * Lots of code duplication, it will be united in just one
     942             :  * function eventually */
     943             : 
     944     1565965 : NTSTATUS sec_access_check_ds(const struct security_descriptor *sd,
     945             :                              const struct security_token *token,
     946             :                              uint32_t access_desired,
     947             :                              uint32_t *access_granted,
     948             :                              struct object_tree *tree,
     949             :                              const struct dom_sid *replace_sid)
     950             : {
     951     1565965 :         return sec_access_check_ds_implicit_owner(sd,
     952             :                                                   token,
     953             :                                                   access_desired,
     954             :                                                   access_granted,
     955             :                                                   tree,
     956             :                                                   replace_sid,
     957             :                                                   IMPLICIT_OWNER_READ_CONTROL_RIGHTS);
     958             : }

Generated by: LCOV version 1.14