diff options
Diffstat (limited to 'src/dutil/aclutil.cpp')
| -rw-r--r-- | src/dutil/aclutil.cpp | 126 |
1 files changed, 70 insertions, 56 deletions
diff --git a/src/dutil/aclutil.cpp b/src/dutil/aclutil.cpp index fc01ecc8..c9733033 100644 --- a/src/dutil/aclutil.cpp +++ b/src/dutil/aclutil.cpp | |||
| @@ -2,6 +2,20 @@ | |||
| 2 | 2 | ||
| 3 | #include "precomp.h" | 3 | #include "precomp.h" |
| 4 | 4 | ||
| 5 | // Exit macros | ||
| 6 | #define AclExitOnLastError(x, s, ...) ExitOnLastErrorSource(DUTIL_SOURCE_ACLUTIL, x, s, __VA_ARGS__) | ||
| 7 | #define AclExitOnLastErrorDebugTrace(x, s, ...) ExitOnLastErrorDebugTraceSource(DUTIL_SOURCE_ACLUTIL, x, s, __VA_ARGS__) | ||
| 8 | #define AclExitWithLastError(x, s, ...) ExitWithLastErrorSource(DUTIL_SOURCE_ACLUTIL, x, s, __VA_ARGS__) | ||
| 9 | #define AclExitOnFailure(x, s, ...) ExitOnFailureSource(DUTIL_SOURCE_ACLUTIL, x, s, __VA_ARGS__) | ||
| 10 | #define AclExitOnRootFailure(x, s, ...) ExitOnRootFailureSource(DUTIL_SOURCE_ACLUTIL, x, s, __VA_ARGS__) | ||
| 11 | #define AclExitOnFailureDebugTrace(x, s, ...) ExitOnFailureDebugTraceSource(DUTIL_SOURCE_ACLUTIL, x, s, __VA_ARGS__) | ||
| 12 | #define AclExitOnNull(p, x, e, s, ...) ExitOnNullSource(DUTIL_SOURCE_ACLUTIL, p, x, e, s, __VA_ARGS__) | ||
| 13 | #define AclExitOnNullWithLastError(p, x, s, ...) ExitOnNullWithLastErrorSource(DUTIL_SOURCE_ACLUTIL, p, x, s, __VA_ARGS__) | ||
| 14 | #define AclExitOnNullDebugTrace(p, x, e, s, ...) ExitOnNullDebugTraceSource(DUTIL_SOURCE_ACLUTIL, p, x, e, s, __VA_ARGS__) | ||
| 15 | #define AclExitOnInvalidHandleWithLastError(p, x, s, ...) ExitOnInvalidHandleWithLastErrorSource(DUTIL_SOURCE_ACLUTIL, p, x, s, __VA_ARGS__) | ||
| 16 | #define AclExitOnWin32Error(e, x, s, ...) ExitOnWin32ErrorSource(DUTIL_SOURCE_ACLUTIL, e, x, s, __VA_ARGS__) | ||
| 17 | #define AclExitOnGdipFailure(g, x, s, ...) ExitOnGdipFailureSource(DUTIL_SOURCE_ACLUTIL, g, x, s, __VA_ARGS__) | ||
| 18 | |||
| 5 | /******************************************************************** | 19 | /******************************************************************** |
| 6 | AclCheckAccess - determines if token has appropriate privileges | 20 | AclCheckAccess - determines if token has appropriate privileges |
| 7 | 21 | ||
| @@ -18,25 +32,25 @@ extern "C" HRESULT DAPI AclCheckAccess( | |||
| 18 | PSID psid = NULL; | 32 | PSID psid = NULL; |
| 19 | BOOL fIsMember = FALSE; | 33 | BOOL fIsMember = FALSE; |
| 20 | 34 | ||
| 21 | ExitOnNull(paa, hr, E_INVALIDARG, "Failed to check ACL access, because no acl access provided to check"); | 35 | AclExitOnNull(paa, hr, E_INVALIDARG, "Failed to check ACL access, because no acl access provided to check"); |
| 22 | Assert(0 == paa->fDenyAccess && 0 == paa->dwAccessMask); | 36 | Assert(0 == paa->fDenyAccess && 0 == paa->dwAccessMask); |
| 23 | 37 | ||
| 24 | if (paa->pwzAccountName) | 38 | if (paa->pwzAccountName) |
| 25 | { | 39 | { |
| 26 | hr = AclGetAccountSid(NULL, paa->pwzAccountName, &psid); | 40 | hr = AclGetAccountSid(NULL, paa->pwzAccountName, &psid); |
| 27 | ExitOnFailure(hr, "failed to get SID for account: %ls", paa->pwzAccountName); | 41 | AclExitOnFailure(hr, "failed to get SID for account: %ls", paa->pwzAccountName); |
| 28 | } | 42 | } |
| 29 | else | 43 | else |
| 30 | { | 44 | { |
| 31 | if (!::AllocateAndInitializeSid(&paa->sia, paa->nSubAuthorityCount, paa->nSubAuthority[0], paa->nSubAuthority[1], paa->nSubAuthority[2], paa->nSubAuthority[3], paa->nSubAuthority[4], paa->nSubAuthority[5], paa->nSubAuthority[6], paa->nSubAuthority[7], &psid)) | 45 | if (!::AllocateAndInitializeSid(&paa->sia, paa->nSubAuthorityCount, paa->nSubAuthority[0], paa->nSubAuthority[1], paa->nSubAuthority[2], paa->nSubAuthority[3], paa->nSubAuthority[4], paa->nSubAuthority[5], paa->nSubAuthority[6], paa->nSubAuthority[7], &psid)) |
| 32 | { | 46 | { |
| 33 | ExitWithLastError(hr, "failed to initialize SID"); | 47 | AclExitWithLastError(hr, "failed to initialize SID"); |
| 34 | } | 48 | } |
| 35 | } | 49 | } |
| 36 | 50 | ||
| 37 | if (!::CheckTokenMembership(hToken, psid, &fIsMember)) | 51 | if (!::CheckTokenMembership(hToken, psid, &fIsMember)) |
| 38 | { | 52 | { |
| 39 | ExitWithLastError(hr, "failed to check membership"); | 53 | AclExitWithLastError(hr, "failed to check membership"); |
| 40 | } | 54 | } |
| 41 | 55 | ||
| 42 | fIsMember ? hr = S_OK : hr = S_FALSE; | 56 | fIsMember ? hr = S_OK : hr = S_FALSE; |
| @@ -123,7 +137,7 @@ extern "C" HRESULT DAPI AclGetWellKnownSid( | |||
| 123 | // allocate memory for the SID and get it | 137 | // allocate memory for the SID and get it |
| 124 | // | 138 | // |
| 125 | psid = static_cast<PSID>(MemAlloc(cbSid, TRUE)); | 139 | psid = static_cast<PSID>(MemAlloc(cbSid, TRUE)); |
| 126 | ExitOnNull(psid, hr, E_OUTOFMEMORY, "failed allocate memory for well known SID"); | 140 | AclExitOnNull(psid, hr, E_OUTOFMEMORY, "failed allocate memory for well known SID"); |
| 127 | 141 | ||
| 128 | #if(_WIN32_WINNT < 0x0501) | 142 | #if(_WIN32_WINNT < 0x0501) |
| 129 | switch (wkst) | 143 | switch (wkst) |
| @@ -160,19 +174,19 @@ extern "C" HRESULT DAPI AclGetWellKnownSid( | |||
| 160 | break; | 174 | break; |
| 161 | default: | 175 | default: |
| 162 | hr = E_INVALIDARG; | 176 | hr = E_INVALIDARG; |
| 163 | ExitOnFailure(hr, "unknown well known SID: %d", wkst); | 177 | AclExitOnFailure(hr, "unknown well known SID: %d", wkst); |
| 164 | } | 178 | } |
| 165 | 179 | ||
| 166 | if (!fSuccess) | 180 | if (!fSuccess) |
| 167 | ExitOnLastError(hr, "failed to allocate well known SID: %d", wkst); | 181 | AclExitOnLastError(hr, "failed to allocate well known SID: %d", wkst); |
| 168 | 182 | ||
| 169 | if (!::CopySid(cbSid, psid, psidTemp)) | 183 | if (!::CopySid(cbSid, psid, psidTemp)) |
| 170 | ExitOnLastError(hr, "failed to create well known SID: %d", wkst); | 184 | AclExitOnLastError(hr, "failed to create well known SID: %d", wkst); |
| 171 | #else | 185 | #else |
| 172 | Assert(NULL == psidTemp); | 186 | Assert(NULL == psidTemp); |
| 173 | if (!::CreateWellKnownSid(wkst, NULL, psid, &cbSid)) | 187 | if (!::CreateWellKnownSid(wkst, NULL, psid, &cbSid)) |
| 174 | { | 188 | { |
| 175 | ExitWithLastError(hr, "failed to create well known SID: %d", wkst); | 189 | AclExitWithLastError(hr, "failed to create well known SID: %d", wkst); |
| 176 | } | 190 | } |
| 177 | #endif | 191 | #endif |
| 178 | 192 | ||
| @@ -216,9 +230,9 @@ extern "C" HRESULT DAPI AclGetAccountSid( | |||
| 216 | // allocate memory for the SID and domain name | 230 | // allocate memory for the SID and domain name |
| 217 | // | 231 | // |
| 218 | psid = static_cast<PSID>(MemAlloc(cbSid, TRUE)); | 232 | psid = static_cast<PSID>(MemAlloc(cbSid, TRUE)); |
| 219 | ExitOnNull(psid, hr, E_OUTOFMEMORY, "failed to allocate memory for SID"); | 233 | AclExitOnNull(psid, hr, E_OUTOFMEMORY, "failed to allocate memory for SID"); |
| 220 | hr = StrAlloc(&pwzDomainName, cbDomainName); | 234 | hr = StrAlloc(&pwzDomainName, cbDomainName); |
| 221 | ExitOnFailure(hr, "failed to allocate string for domain name"); | 235 | AclExitOnFailure(hr, "failed to allocate string for domain name"); |
| 222 | 236 | ||
| 223 | // | 237 | // |
| 224 | // try to lookup the account now | 238 | // try to lookup the account now |
| @@ -232,24 +246,24 @@ extern "C" HRESULT DAPI AclGetAccountSid( | |||
| 232 | if (SECURITY_MAX_SID_SIZE < cbSid) | 246 | if (SECURITY_MAX_SID_SIZE < cbSid) |
| 233 | { | 247 | { |
| 234 | PSID psidNew = static_cast<PSID>(MemReAlloc(psid, cbSid, TRUE)); | 248 | PSID psidNew = static_cast<PSID>(MemReAlloc(psid, cbSid, TRUE)); |
| 235 | ExitOnNullWithLastError(psidNew, hr, "failed to allocate memory for account: %ls", wzAccount); | 249 | AclExitOnNullWithLastError(psidNew, hr, "failed to allocate memory for account: %ls", wzAccount); |
| 236 | 250 | ||
| 237 | psid = psidNew; | 251 | psid = psidNew; |
| 238 | } | 252 | } |
| 239 | if (255 < cbDomainName) | 253 | if (255 < cbDomainName) |
| 240 | { | 254 | { |
| 241 | hr = StrAlloc(&pwzDomainName, cbDomainName); | 255 | hr = StrAlloc(&pwzDomainName, cbDomainName); |
| 242 | ExitOnFailure(hr, "failed to allocate string for domain name"); | 256 | AclExitOnFailure(hr, "failed to allocate string for domain name"); |
| 243 | } | 257 | } |
| 244 | 258 | ||
| 245 | if (!::LookupAccountNameW(wzSystem, wzAccount, psid, &cbSid, pwzDomainName, &cbDomainName, &peUse)) | 259 | if (!::LookupAccountNameW(wzSystem, wzAccount, psid, &cbSid, pwzDomainName, &cbDomainName, &peUse)) |
| 246 | { | 260 | { |
| 247 | ExitWithLastError(hr, "failed to lookup account: %ls", wzAccount); | 261 | AclExitWithLastError(hr, "failed to lookup account: %ls", wzAccount); |
| 248 | } | 262 | } |
| 249 | } | 263 | } |
| 250 | else | 264 | else |
| 251 | { | 265 | { |
| 252 | ExitOnWin32Error(er, hr, "failed to lookup account: %ls", wzAccount); | 266 | AclExitOnWin32Error(er, hr, "failed to lookup account: %ls", wzAccount); |
| 253 | } | 267 | } |
| 254 | } | 268 | } |
| 255 | 269 | ||
| @@ -284,12 +298,12 @@ extern "C" HRESULT DAPI AclGetAccountSidString( | |||
| 284 | *ppwzSid = NULL; | 298 | *ppwzSid = NULL; |
| 285 | 299 | ||
| 286 | hr = AclGetAccountSid(wzSystem, wzAccount, &psid); | 300 | hr = AclGetAccountSid(wzSystem, wzAccount, &psid); |
| 287 | ExitOnFailure(hr, "failed to get SID for account: %ls", wzAccount); | 301 | AclExitOnFailure(hr, "failed to get SID for account: %ls", wzAccount); |
| 288 | Assert(::IsValidSid(psid)); | 302 | Assert(::IsValidSid(psid)); |
| 289 | 303 | ||
| 290 | if (!::ConvertSidToStringSidW(psid, &pwz)) | 304 | if (!::ConvertSidToStringSidW(psid, &pwz)) |
| 291 | { | 305 | { |
| 292 | ExitWithLastError(hr, "failed to convert SID to string for Account: %ls", wzAccount); | 306 | AclExitWithLastError(hr, "failed to convert SID to string for Account: %ls", wzAccount); |
| 293 | } | 307 | } |
| 294 | 308 | ||
| 295 | hr = StrAllocString(ppwzSid, pwz, 0); | 309 | hr = StrAllocString(ppwzSid, pwz, 0); |
| @@ -347,14 +361,14 @@ extern "C" HRESULT DAPI AclCreateDacl( | |||
| 347 | } | 361 | } |
| 348 | 362 | ||
| 349 | pAcl = static_cast<ACL*>(MemAlloc(cbAcl, TRUE)); | 363 | pAcl = static_cast<ACL*>(MemAlloc(cbAcl, TRUE)); |
| 350 | ExitOnNull(pAcl, hr, E_OUTOFMEMORY, "failed to allocate ACL"); | 364 | AclExitOnNull(pAcl, hr, E_OUTOFMEMORY, "failed to allocate ACL"); |
| 351 | 365 | ||
| 352 | #pragma prefast(push) | 366 | #pragma prefast(push) |
| 353 | #pragma prefast(disable:25029) | 367 | #pragma prefast(disable:25029) |
| 354 | if (!::InitializeAcl(pAcl, cbAcl, ACL_REVISION)) | 368 | if (!::InitializeAcl(pAcl, cbAcl, ACL_REVISION)) |
| 355 | #pragma prefast(pop) | 369 | #pragma prefast(pop) |
| 356 | { | 370 | { |
| 357 | ExitWithLastError(hr, "failed to initialize ACL"); | 371 | AclExitWithLastError(hr, "failed to initialize ACL"); |
| 358 | } | 372 | } |
| 359 | 373 | ||
| 360 | // add in the ACEs (denied first) | 374 | // add in the ACEs (denied first) |
| @@ -365,7 +379,7 @@ extern "C" HRESULT DAPI AclCreateDacl( | |||
| 365 | if (!::AddAccessDeniedAceEx(pAcl, ACL_REVISION, rgaaDeny[i].dwFlags, rgaaDeny[i].dwMask, rgaaDeny[i].psid)) | 379 | if (!::AddAccessDeniedAceEx(pAcl, ACL_REVISION, rgaaDeny[i].dwFlags, rgaaDeny[i].dwMask, rgaaDeny[i].psid)) |
| 366 | #pragma prefast(pop) | 380 | #pragma prefast(pop) |
| 367 | { | 381 | { |
| 368 | ExitWithLastError(hr, "failed to add access denied ACE #%d to ACL", i); | 382 | AclExitWithLastError(hr, "failed to add access denied ACE #%d to ACL", i); |
| 369 | } | 383 | } |
| 370 | } | 384 | } |
| 371 | for (i = 0; i < cAllow; ++i) | 385 | for (i = 0; i < cAllow; ++i) |
| @@ -375,7 +389,7 @@ extern "C" HRESULT DAPI AclCreateDacl( | |||
| 375 | if (!::AddAccessAllowedAceEx(pAcl, ACL_REVISION, rgaaAllow[i].dwFlags, rgaaAllow[i].dwMask, rgaaAllow[i].psid)) | 389 | if (!::AddAccessAllowedAceEx(pAcl, ACL_REVISION, rgaaAllow[i].dwFlags, rgaaAllow[i].dwMask, rgaaAllow[i].psid)) |
| 376 | #pragma prefast(pop) | 390 | #pragma prefast(pop) |
| 377 | { | 391 | { |
| 378 | ExitWithLastError(hr, "failed to add access allowed ACE #$d to ACL", i); | 392 | AclExitWithLastError(hr, "failed to add access allowed ACE #%d to ACL", i); |
| 379 | } | 393 | } |
| 380 | } | 394 | } |
| 381 | 395 | ||
| @@ -422,7 +436,7 @@ extern "C" HRESULT DAPI AclAddToDacl( | |||
| 422 | // allocate memory for all the new ACEs (NOTE: this over calculates the memory necessary, but that's okay) | 436 | // allocate memory for all the new ACEs (NOTE: this over calculates the memory necessary, but that's okay) |
| 423 | if (!::GetAclInformation(pAcl, &asi, sizeof(asi), AclSizeInformation)) | 437 | if (!::GetAclInformation(pAcl, &asi, sizeof(asi), AclSizeInformation)) |
| 424 | { | 438 | { |
| 425 | ExitWithLastError(hr, "failed to get information about original ACL"); | 439 | AclExitWithLastError(hr, "failed to get information about original ACL"); |
| 426 | } | 440 | } |
| 427 | 441 | ||
| 428 | if ((asi.AceCount + cDeny) < asi.AceCount || // check for overflow | 442 | if ((asi.AceCount + cDeny) < asi.AceCount || // check for overflow |
| @@ -430,29 +444,29 @@ extern "C" HRESULT DAPI AclAddToDacl( | |||
| 430 | (asi.AceCount + cDeny) >= MAXSIZE_T / sizeof(ACL_ACE)) | 444 | (asi.AceCount + cDeny) >= MAXSIZE_T / sizeof(ACL_ACE)) |
| 431 | { | 445 | { |
| 432 | hr = E_OUTOFMEMORY; | 446 | hr = E_OUTOFMEMORY; |
| 433 | ExitOnFailure(hr, "Not enough memory to allocate %d ACEs", (asi.AceCount + cDeny)); | 447 | AclExitOnFailure(hr, "Not enough memory to allocate %d ACEs", (asi.AceCount + cDeny)); |
| 434 | } | 448 | } |
| 435 | 449 | ||
| 436 | paaNewDeny = static_cast<ACL_ACE*>(MemAlloc(sizeof(ACL_ACE) * (asi.AceCount + cDeny), TRUE)); | 450 | paaNewDeny = static_cast<ACL_ACE*>(MemAlloc(sizeof(ACL_ACE) * (asi.AceCount + cDeny), TRUE)); |
| 437 | ExitOnNull(paaNewDeny, hr, E_OUTOFMEMORY, "failed to allocate memory for new deny ACEs"); | 451 | AclExitOnNull(paaNewDeny, hr, E_OUTOFMEMORY, "failed to allocate memory for new deny ACEs"); |
| 438 | 452 | ||
| 439 | if ((asi.AceCount + cAllow) < asi.AceCount || // check for overflow | 453 | if ((asi.AceCount + cAllow) < asi.AceCount || // check for overflow |
| 440 | (asi.AceCount + cAllow) < cAllow || // check for overflow | 454 | (asi.AceCount + cAllow) < cAllow || // check for overflow |
| 441 | (asi.AceCount + cAllow) >= MAXSIZE_T / sizeof(ACL_ACE)) | 455 | (asi.AceCount + cAllow) >= MAXSIZE_T / sizeof(ACL_ACE)) |
| 442 | { | 456 | { |
| 443 | hr = E_OUTOFMEMORY; | 457 | hr = E_OUTOFMEMORY; |
| 444 | ExitOnFailure(hr, "Not enough memory to allocate %d ACEs", (asi.AceCount + cAllow)); | 458 | AclExitOnFailure(hr, "Not enough memory to allocate %d ACEs", (asi.AceCount + cAllow)); |
| 445 | } | 459 | } |
| 446 | 460 | ||
| 447 | paaNewAllow = static_cast<ACL_ACE*>(MemAlloc(sizeof(ACL_ACE) * (asi.AceCount + cAllow), TRUE)); | 461 | paaNewAllow = static_cast<ACL_ACE*>(MemAlloc(sizeof(ACL_ACE) * (asi.AceCount + cAllow), TRUE)); |
| 448 | ExitOnNull(paaNewAllow, hr, E_OUTOFMEMORY, "failed to allocate memory for new allow ACEs"); | 462 | AclExitOnNull(paaNewAllow, hr, E_OUTOFMEMORY, "failed to allocate memory for new allow ACEs"); |
| 449 | 463 | ||
| 450 | // fill in the new structures with old data then new data (denied first) | 464 | // fill in the new structures with old data then new data (denied first) |
| 451 | for (i = 0; i < asi.AceCount; ++i) | 465 | for (i = 0; i < asi.AceCount; ++i) |
| 452 | { | 466 | { |
| 453 | if (!::GetAce(pAcl, i, reinterpret_cast<LPVOID*>(&pada))) | 467 | if (!::GetAce(pAcl, i, reinterpret_cast<LPVOID*>(&pada))) |
| 454 | { | 468 | { |
| 455 | ExitWithLastError(hr, "failed to get ACE #%d from ACL", i); | 469 | AclExitWithLastError(hr, "failed to get ACE #%d from ACL", i); |
| 456 | } | 470 | } |
| 457 | 471 | ||
| 458 | if (ACCESS_DENIED_ACE_TYPE != pada->Header.AceType) | 472 | if (ACCESS_DENIED_ACE_TYPE != pada->Header.AceType) |
| @@ -474,7 +488,7 @@ extern "C" HRESULT DAPI AclAddToDacl( | |||
| 474 | { | 488 | { |
| 475 | if (!::GetAce(pAcl, i, reinterpret_cast<LPVOID*>(&paaa))) | 489 | if (!::GetAce(pAcl, i, reinterpret_cast<LPVOID*>(&paaa))) |
| 476 | { | 490 | { |
| 477 | ExitWithLastError(hr, "failed to get ACE #%d from ACL", i); | 491 | AclExitWithLastError(hr, "failed to get ACE #%d from ACL", i); |
| 478 | } | 492 | } |
| 479 | 493 | ||
| 480 | if (ACCESS_ALLOWED_ACE_TYPE != paaa->Header.AceType) | 494 | if (ACCESS_ALLOWED_ACE_TYPE != paaa->Header.AceType) |
| @@ -493,7 +507,7 @@ extern "C" HRESULT DAPI AclAddToDacl( | |||
| 493 | 507 | ||
| 494 | // create the dacl with the new | 508 | // create the dacl with the new |
| 495 | hr = AclCreateDacl(paaNewDeny, cNewDeny, paaNewAllow, cNewAllow, ppAclNew); | 509 | hr = AclCreateDacl(paaNewDeny, cNewDeny, paaNewAllow, cNewAllow, ppAclNew); |
| 496 | ExitOnFailure(hr, "failed to create new ACL from existing ACL"); | 510 | AclExitOnFailure(hr, "failed to create new ACL from existing ACL"); |
| 497 | 511 | ||
| 498 | AssertSz(::IsValidAcl(*ppAclNew), "AclAddToDacl() - created invalid ACL"); | 512 | AssertSz(::IsValidAcl(*ppAclNew), "AclAddToDacl() - created invalid ACL"); |
| 499 | Assert(S_OK == hr); | 513 | Assert(S_OK == hr); |
| @@ -551,9 +565,9 @@ extern "C" HRESULT DAPI AclCreateDaclOld( | |||
| 551 | // create the SIDs and calculate the space for the ACL | 565 | // create the SIDs and calculate the space for the ACL |
| 552 | // | 566 | // |
| 553 | pdwAccessMask = static_cast<DWORD*>(MemAlloc(sizeof(DWORD) * cAclAccesses, TRUE)); | 567 | pdwAccessMask = static_cast<DWORD*>(MemAlloc(sizeof(DWORD) * cAclAccesses, TRUE)); |
| 554 | ExitOnNull(pdwAccessMask, hr, E_OUTOFMEMORY, "failed allocate memory for access mask"); | 568 | AclExitOnNull(pdwAccessMask, hr, E_OUTOFMEMORY, "failed allocate memory for access mask"); |
| 555 | ppsid = static_cast<PSID*>(MemAlloc(sizeof(PSID) * cAclAccesses, TRUE)); | 569 | ppsid = static_cast<PSID*>(MemAlloc(sizeof(PSID) * cAclAccesses, TRUE)); |
| 556 | ExitOnNull(ppsid, hr, E_OUTOFMEMORY, "failed allocate memory for sid"); | 570 | AclExitOnNull(ppsid, hr, E_OUTOFMEMORY, "failed allocate memory for sid"); |
| 557 | 571 | ||
| 558 | cbAcl = sizeof (ACL); // start with the size of the header | 572 | cbAcl = sizeof (ACL); // start with the size of the header |
| 559 | for (i = 0; i < cAclAccesses; ++i) | 573 | for (i = 0; i < cAclAccesses; ++i) |
| @@ -561,7 +575,7 @@ extern "C" HRESULT DAPI AclCreateDaclOld( | |||
| 561 | if (paa[i].pwzAccountName) | 575 | if (paa[i].pwzAccountName) |
| 562 | { | 576 | { |
| 563 | hr = AclGetAccountSid(NULL, paa[i].pwzAccountName, ppsid + i); | 577 | hr = AclGetAccountSid(NULL, paa[i].pwzAccountName, ppsid + i); |
| 564 | ExitOnFailure(hr, "failed to get SID for account: %ls", paa[i].pwzAccountName); | 578 | AclExitOnFailure(hr, "failed to get SID for account: %ls", paa[i].pwzAccountName); |
| 565 | } | 579 | } |
| 566 | else | 580 | else |
| 567 | { | 581 | { |
| @@ -572,7 +586,7 @@ extern "C" HRESULT DAPI AclCreateDaclOld( | |||
| 572 | paa[i].nSubAuthority[6], paa[i].nSubAuthority[7], | 586 | paa[i].nSubAuthority[6], paa[i].nSubAuthority[7], |
| 573 | (void**)(ppsid + i)))) | 587 | (void**)(ppsid + i)))) |
| 574 | { | 588 | { |
| 575 | ExitWithLastError(hr, "failed to initialize SIDs #%u", i); | 589 | AclExitWithLastError(hr, "failed to initialize SIDs #%u", i); |
| 576 | } | 590 | } |
| 577 | } | 591 | } |
| 578 | 592 | ||
| @@ -594,14 +608,14 @@ extern "C" HRESULT DAPI AclCreateDaclOld( | |||
| 594 | // allocate the ACL and set the appropriate ACEs | 608 | // allocate the ACL and set the appropriate ACEs |
| 595 | // | 609 | // |
| 596 | *ppACL = static_cast<ACL*>(MemAlloc(cbAcl, FALSE)); | 610 | *ppACL = static_cast<ACL*>(MemAlloc(cbAcl, FALSE)); |
| 597 | ExitOnNull(*ppACL, hr, E_OUTOFMEMORY, "failed allocate memory for ACL"); | 611 | AclExitOnNull(*ppACL, hr, E_OUTOFMEMORY, "failed allocate memory for ACL"); |
| 598 | 612 | ||
| 599 | #pragma prefast(push) | 613 | #pragma prefast(push) |
| 600 | #pragma prefast(disable:25029) | 614 | #pragma prefast(disable:25029) |
| 601 | if (!::InitializeAcl(*ppACL, cbAcl, ACL_REVISION)) | 615 | if (!::InitializeAcl(*ppACL, cbAcl, ACL_REVISION)) |
| 602 | #pragma prefast(pop) | 616 | #pragma prefast(pop) |
| 603 | { | 617 | { |
| 604 | ExitWithLastError(hr, "failed to initialize ACLs"); | 618 | AclExitWithLastError(hr, "failed to initialize ACLs"); |
| 605 | } | 619 | } |
| 606 | 620 | ||
| 607 | // add an access-allowed ACE for each of the SIDs | 621 | // add an access-allowed ACE for each of the SIDs |
| @@ -614,7 +628,7 @@ extern "C" HRESULT DAPI AclCreateDaclOld( | |||
| 614 | if (!::AddAccessDeniedAceEx(*ppACL, ACL_REVISION, CONTAINER_INHERIT_ACE | OBJECT_INHERIT_ACE, pdwAccessMask[i], *(ppsid + i))) | 628 | if (!::AddAccessDeniedAceEx(*ppACL, ACL_REVISION, CONTAINER_INHERIT_ACE | OBJECT_INHERIT_ACE, pdwAccessMask[i], *(ppsid + i))) |
| 615 | #pragma prefast(pop) | 629 | #pragma prefast(pop) |
| 616 | { | 630 | { |
| 617 | ExitWithLastError(hr, "failed to add access denied for ACE"); | 631 | AclExitWithLastError(hr, "failed to add access denied for ACE"); |
| 618 | } | 632 | } |
| 619 | } | 633 | } |
| 620 | else | 634 | else |
| @@ -624,7 +638,7 @@ extern "C" HRESULT DAPI AclCreateDaclOld( | |||
| 624 | if (!::AddAccessAllowedAceEx(*ppACL, ACL_REVISION, CONTAINER_INHERIT_ACE | OBJECT_INHERIT_ACE, pdwAccessMask[i], *(ppsid + i))) | 638 | if (!::AddAccessAllowedAceEx(*ppACL, ACL_REVISION, CONTAINER_INHERIT_ACE | OBJECT_INHERIT_ACE, pdwAccessMask[i], *(ppsid + i))) |
| 625 | #pragma prefast(pop) | 639 | #pragma prefast(pop) |
| 626 | { | 640 | { |
| 627 | ExitWithLastError(hr, "failed to add access allowed for ACE"); | 641 | AclExitWithLastError(hr, "failed to add access allowed for ACE"); |
| 628 | } | 642 | } |
| 629 | } | 643 | } |
| 630 | } | 644 | } |
| @@ -669,8 +683,8 @@ extern "C" HRESULT DAPI AclCreateSecurityDescriptorFromDacl( | |||
| 669 | SECURITY_DESCRIPTOR sd; | 683 | SECURITY_DESCRIPTOR sd; |
| 670 | DWORD cbSD; | 684 | DWORD cbSD; |
| 671 | 685 | ||
| 672 | ExitOnNull(pACL, hr, E_INVALIDARG, "Failed to create security descriptor from DACL, because no DACL was provided"); | 686 | AclExitOnNull(pACL, hr, E_INVALIDARG, "Failed to create security descriptor from DACL, because no DACL was provided"); |
| 673 | ExitOnNull(ppsd, hr, E_INVALIDARG, "Failed to create security descriptor from DACL, because no output object was provided"); | 687 | AclExitOnNull(ppsd, hr, E_INVALIDARG, "Failed to create security descriptor from DACL, because no output object was provided"); |
| 674 | 688 | ||
| 675 | *ppsd = NULL; | 689 | *ppsd = NULL; |
| 676 | 690 | ||
| @@ -687,7 +701,7 @@ extern "C" HRESULT DAPI AclCreateSecurityDescriptorFromDacl( | |||
| 687 | (!::SetSecurityDescriptorOwner(&sd, NULL, FALSE))) | 701 | (!::SetSecurityDescriptorOwner(&sd, NULL, FALSE))) |
| 688 | #pragma prefast(pop) | 702 | #pragma prefast(pop) |
| 689 | { | 703 | { |
| 690 | ExitWithLastError(hr, "failed to initialize security descriptor"); | 704 | AclExitWithLastError(hr, "failed to initialize security descriptor"); |
| 691 | } | 705 | } |
| 692 | 706 | ||
| 693 | // | 707 | // |
| @@ -695,7 +709,7 @@ extern "C" HRESULT DAPI AclCreateSecurityDescriptorFromDacl( | |||
| 695 | // | 709 | // |
| 696 | cbSD = ::GetSecurityDescriptorLength(&sd); | 710 | cbSD = ::GetSecurityDescriptorLength(&sd); |
| 697 | *ppsd = static_cast<SECURITY_DESCRIPTOR*>(MemAlloc(cbSD, FALSE)); | 711 | *ppsd = static_cast<SECURITY_DESCRIPTOR*>(MemAlloc(cbSD, FALSE)); |
| 698 | ExitOnNull(*ppsd, hr, E_OUTOFMEMORY, "failed allocate memory for security descriptor"); | 712 | AclExitOnNull(*ppsd, hr, E_OUTOFMEMORY, "failed allocate memory for security descriptor"); |
| 699 | 713 | ||
| 700 | ::MakeSelfRelativeSD(&sd, (BYTE*)*ppsd, &cbSD); | 714 | ::MakeSelfRelativeSD(&sd, (BYTE*)*ppsd, &cbSD); |
| 701 | Assert(::IsValidSecurityDescriptor(*ppsd)); | 715 | Assert(::IsValidSecurityDescriptor(*ppsd)); |
| @@ -734,7 +748,7 @@ extern "C" HRESULT DAPI AclCreateSecurityDescriptor( | |||
| 734 | // create the DACL | 748 | // create the DACL |
| 735 | // | 749 | // |
| 736 | hr = AclCreateDaclOld(paa, cAclAccesses, &pACL); | 750 | hr = AclCreateDaclOld(paa, cAclAccesses, &pACL); |
| 737 | ExitOnFailure(hr, "failed to create DACL for security descriptor"); | 751 | AclExitOnFailure(hr, "failed to create DACL for security descriptor"); |
| 738 | 752 | ||
| 739 | // | 753 | // |
| 740 | // create self-relative security descriptor | 754 | // create self-relative security descriptor |
| @@ -770,15 +784,15 @@ extern "C" HRESULT DAPI AclCreateSecurityDescriptorFromString( | |||
| 770 | va_start(args, wzSddlFormat); | 784 | va_start(args, wzSddlFormat); |
| 771 | hr = StrAllocFormattedArgs(&pwzSddl, wzSddlFormat, args); | 785 | hr = StrAllocFormattedArgs(&pwzSddl, wzSddlFormat, args); |
| 772 | va_end(args); | 786 | va_end(args); |
| 773 | ExitOnFailure(hr, "failed to create SDDL string for format: %ls", wzSddlFormat); | 787 | AclExitOnFailure(hr, "failed to create SDDL string for format: %ls", wzSddlFormat); |
| 774 | 788 | ||
| 775 | if (!::ConvertStringSecurityDescriptorToSecurityDescriptorW(pwzSddl, SDDL_REVISION_1, &psd, &cbSD)) | 789 | if (!::ConvertStringSecurityDescriptorToSecurityDescriptorW(pwzSddl, SDDL_REVISION_1, &psd, &cbSD)) |
| 776 | { | 790 | { |
| 777 | ExitWithLastError(hr, "failed to create security descriptor from SDDL: %ls", pwzSddl); | 791 | AclExitWithLastError(hr, "failed to create security descriptor from SDDL: %ls", pwzSddl); |
| 778 | } | 792 | } |
| 779 | 793 | ||
| 780 | *ppsd = static_cast<SECURITY_DESCRIPTOR*>(MemAlloc(cbSD, FALSE)); | 794 | *ppsd = static_cast<SECURITY_DESCRIPTOR*>(MemAlloc(cbSD, FALSE)); |
| 781 | ExitOnNull(*ppsd, hr, E_OUTOFMEMORY, "failed to allocate memory for security descriptor"); | 795 | AclExitOnNull(*ppsd, hr, E_OUTOFMEMORY, "failed to allocate memory for security descriptor"); |
| 782 | 796 | ||
| 783 | memcpy(*ppsd, psd, cbSD); | 797 | memcpy(*ppsd, psd, cbSD); |
| 784 | Assert(::IsValidSecurityDescriptor(*ppsd)); | 798 | Assert(::IsValidSecurityDescriptor(*ppsd)); |
| @@ -815,7 +829,7 @@ extern "C" HRESULT DAPI AclDuplicateSecurityDescriptor( | |||
| 815 | HRESULT hr = S_OK; | 829 | HRESULT hr = S_OK; |
| 816 | DWORD cbSD; | 830 | DWORD cbSD; |
| 817 | 831 | ||
| 818 | ExitOnNull(ppsd, hr, E_INVALIDARG, "Failed to get duplicate ACL security descriptor because no place to output was provided"); | 832 | AclExitOnNull(ppsd, hr, E_INVALIDARG, "Failed to get duplicate ACL security descriptor because no place to output was provided"); |
| 819 | *ppsd = NULL; | 833 | *ppsd = NULL; |
| 820 | 834 | ||
| 821 | // | 835 | // |
| @@ -823,7 +837,7 @@ extern "C" HRESULT DAPI AclDuplicateSecurityDescriptor( | |||
| 823 | // | 837 | // |
| 824 | cbSD = ::GetSecurityDescriptorLength(psd); | 838 | cbSD = ::GetSecurityDescriptorLength(psd); |
| 825 | *ppsd = static_cast<SECURITY_DESCRIPTOR*>(MemAlloc(cbSD, 0)); | 839 | *ppsd = static_cast<SECURITY_DESCRIPTOR*>(MemAlloc(cbSD, 0)); |
| 826 | ExitOnNull(*ppsd, hr, E_OUTOFMEMORY, "failed allocate memory for security descriptor"); | 840 | AclExitOnNull(*ppsd, hr, E_OUTOFMEMORY, "failed allocate memory for security descriptor"); |
| 827 | 841 | ||
| 828 | memcpy(*ppsd, psd, cbSD); | 842 | memcpy(*ppsd, psd, cbSD); |
| 829 | Assert(::IsValidSecurityDescriptor(*ppsd)); | 843 | Assert(::IsValidSecurityDescriptor(*ppsd)); |
| @@ -856,18 +870,18 @@ extern "C" HRESULT DAPI AclGetSecurityDescriptor( | |||
| 856 | PSECURITY_DESCRIPTOR psd = NULL; | 870 | PSECURITY_DESCRIPTOR psd = NULL; |
| 857 | DWORD cbSD; | 871 | DWORD cbSD; |
| 858 | 872 | ||
| 859 | ExitOnNull(ppsd, hr, E_INVALIDARG, "Failed to get ACL Security Descriptor because no place to output was provided"); | 873 | AclExitOnNull(ppsd, hr, E_INVALIDARG, "Failed to get ACL Security Descriptor because no place to output was provided"); |
| 860 | *ppsd = NULL; | 874 | *ppsd = NULL; |
| 861 | 875 | ||
| 862 | // get the security descriptor for the object | 876 | // get the security descriptor for the object |
| 863 | er = ::GetNamedSecurityInfoW(const_cast<LPWSTR>(wzObject), sot, securityInformation, NULL, NULL, NULL, NULL, &psd); | 877 | er = ::GetNamedSecurityInfoW(const_cast<LPWSTR>(wzObject), sot, securityInformation, NULL, NULL, NULL, NULL, &psd); |
| 864 | ExitOnWin32Error(er, hr, "failed to get security info from object: %ls", wzObject); | 878 | AclExitOnWin32Error(er, hr, "failed to get security info from object: %ls", wzObject); |
| 865 | Assert(::IsValidSecurityDescriptor(psd)); | 879 | Assert(::IsValidSecurityDescriptor(psd)); |
| 866 | 880 | ||
| 867 | // copy the self-relative security descriptor | 881 | // copy the self-relative security descriptor |
| 868 | cbSD = ::GetSecurityDescriptorLength(psd); | 882 | cbSD = ::GetSecurityDescriptorLength(psd); |
| 869 | *ppsd = static_cast<SECURITY_DESCRIPTOR*>(MemAlloc(cbSD, 0)); | 883 | *ppsd = static_cast<SECURITY_DESCRIPTOR*>(MemAlloc(cbSD, 0)); |
| 870 | ExitOnNull(*ppsd, hr, E_OUTOFMEMORY, "failed allocate memory for security descriptor"); | 884 | AclExitOnNull(*ppsd, hr, E_OUTOFMEMORY, "failed allocate memory for security descriptor"); |
| 871 | 885 | ||
| 872 | memcpy(*ppsd, psd, cbSD); | 886 | memcpy(*ppsd, psd, cbSD); |
| 873 | Assert(::IsValidSecurityDescriptor(*ppsd)); | 887 | Assert(::IsValidSecurityDescriptor(*ppsd)); |
| @@ -905,7 +919,7 @@ extern "C" HRESULT DAPI AclSetSecurityWithRetry( | |||
| 905 | DWORD i = 0; | 919 | DWORD i = 0; |
| 906 | 920 | ||
| 907 | hr = StrAllocString(&sczObject, wzObject, 0); | 921 | hr = StrAllocString(&sczObject, wzObject, 0); |
| 908 | ExitOnFailure(hr, "Failed to copy object to secure."); | 922 | AclExitOnFailure(hr, "Failed to copy object to secure."); |
| 909 | 923 | ||
| 910 | hr = E_FAIL; | 924 | hr = E_FAIL; |
| 911 | for (i = 0; FAILED(hr) && i <= cRetry; ++i) | 925 | for (i = 0; FAILED(hr) && i <= cRetry; ++i) |
| @@ -918,7 +932,7 @@ extern "C" HRESULT DAPI AclSetSecurityWithRetry( | |||
| 918 | DWORD er = ::SetNamedSecurityInfoW(sczObject, sot, securityInformation, psidOwner, psidGroup, pDacl, pSacl); | 932 | DWORD er = ::SetNamedSecurityInfoW(sczObject, sot, securityInformation, psidOwner, psidGroup, pDacl, pSacl); |
| 919 | hr = HRESULT_FROM_WIN32(er); | 933 | hr = HRESULT_FROM_WIN32(er); |
| 920 | } | 934 | } |
| 921 | ExitOnRootFailure(hr, "Failed to set security on object '%ls' after %u retries.", wzObject, i); | 935 | AclExitOnRootFailure(hr, "Failed to set security on object '%ls' after %u retries.", wzObject, i); |
| 922 | 936 | ||
| 923 | LExit: | 937 | LExit: |
| 924 | ReleaseStr(sczObject); | 938 | ReleaseStr(sczObject); |
| @@ -996,20 +1010,20 @@ extern "C" HRESULT DAPI AclAddAdminToSecurityDescriptor( | |||
| 996 | 1010 | ||
| 997 | if (!::GetSecurityDescriptorDacl(pSecurity, &fValid, &pAcl, &fDaclDefaulted) || !fValid) | 1011 | if (!::GetSecurityDescriptorDacl(pSecurity, &fValid, &pAcl, &fDaclDefaulted) || !fValid) |
| 998 | { | 1012 | { |
| 999 | ExitOnLastError(hr, "Failed to get acl from security descriptor"); | 1013 | AclExitOnLastError(hr, "Failed to get acl from security descriptor"); |
| 1000 | } | 1014 | } |
| 1001 | 1015 | ||
| 1002 | hr = AclGetWellKnownSid(WinBuiltinAdministratorsSid, &ace[0].psid); | 1016 | hr = AclGetWellKnownSid(WinBuiltinAdministratorsSid, &ace[0].psid); |
| 1003 | ExitOnFailure(hr, "failed to get sid for Administrators group"); | 1017 | AclExitOnFailure(hr, "failed to get sid for Administrators group"); |
| 1004 | 1018 | ||
| 1005 | ace[0].dwFlags = NO_PROPAGATE_INHERIT_ACE; | 1019 | ace[0].dwFlags = NO_PROPAGATE_INHERIT_ACE; |
| 1006 | ace[0].dwMask = GENERIC_ALL; | 1020 | ace[0].dwMask = GENERIC_ALL; |
| 1007 | 1021 | ||
| 1008 | hr = AclAddToDacl(pAcl, NULL, 0, ace, 1, &pAclNew); | 1022 | hr = AclAddToDacl(pAcl, NULL, 0, ace, 1, &pAclNew); |
| 1009 | ExitOnFailure(hr, "failed to add Administrators ACE to ACL"); | 1023 | AclExitOnFailure(hr, "failed to add Administrators ACE to ACL"); |
| 1010 | 1024 | ||
| 1011 | hr = AclCreateSecurityDescriptorFromDacl(pAclNew, &pSecurityNew); | 1025 | hr = AclCreateSecurityDescriptorFromDacl(pAclNew, &pSecurityNew); |
| 1012 | ExitOnLastError(hr, "Failed to create new security descriptor"); | 1026 | AclExitOnLastError(hr, "Failed to create new security descriptor"); |
| 1013 | 1027 | ||
| 1014 | // The DACL is referenced by, not copied into, the security descriptor. Make sure not to free it. | 1028 | // The DACL is referenced by, not copied into, the security descriptor. Make sure not to free it. |
| 1015 | pAclNew = NULL; | 1029 | pAclNew = NULL; |
