1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
|
// Copyright 2021 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "base/win/security_util.h"
#include <windows.h>
#include <winternl.h>
#include <optional>
#include "base/check.h"
#include "base/containers/to_vector.h"
#include "base/files/file_path.h"
#include "base/logging.h"
#include "base/threading/scoped_blocking_call.h"
#include "base/win/access_control_list.h"
#include "base/win/scoped_handle.h"
#include "base/win/security_descriptor.h"
namespace base {
namespace win {
namespace {
bool AddACEToPath(const FilePath& path,
const std::vector<Sid>& sids,
DWORD access_mask,
DWORD inheritance,
bool recursive,
SecurityAccessMode access_mode) {
DCHECK(!path.empty());
if (sids.empty()) {
return true;
}
base::ScopedBlockingCall scoped_blocking_call(FROM_HERE,
base::BlockingType::MAY_BLOCK);
std::optional<SecurityDescriptor> sd =
SecurityDescriptor::FromFile(path, DACL_SECURITY_INFORMATION);
if (!sd) {
return false;
}
std::vector<ExplicitAccessEntry> entries;
for (const Sid& sid : sids) {
entries.emplace_back(sid, access_mode, access_mask, inheritance);
}
if (!sd->SetDaclEntries(entries)) {
return false;
}
if (recursive) {
return sd->WriteToFile(path, DACL_SECURITY_INFORMATION);
}
ScopedHandle handle(::CreateFile(path.value().c_str(), WRITE_DAC, 0, nullptr,
OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS,
nullptr));
if (!handle.is_valid()) {
DPLOG(ERROR) << "Failed opening path \"" << path.value()
<< "\" to write DACL";
return false;
}
return sd->WriteToHandle(handle.get(), SecurityObjectType::kKernel,
DACL_SECURITY_INFORMATION);
}
} // namespace
bool GrantAccessToPath(const FilePath& path,
const std::vector<Sid>& sids,
DWORD access_mask,
DWORD inheritance,
bool recursive) {
return AddACEToPath(path, sids, access_mask, inheritance, recursive,
SecurityAccessMode::kGrant);
}
bool DenyAccessToPath(const FilePath& path,
const std::vector<Sid>& sids,
DWORD access_mask,
DWORD inheritance,
bool recursive) {
return AddACEToPath(path, sids, access_mask, inheritance, recursive,
SecurityAccessMode::kDeny);
}
bool HasAccessToPath(const FilePath& path,
const std::vector<Sid>& sids,
DWORD access_mask,
DWORD inheritance) {
DCHECK(!path.empty());
if (sids.empty()) {
return true;
}
base::ScopedBlockingCall scoped_blocking_call(FROM_HERE,
base::BlockingType::MAY_BLOCK);
std::optional<SecurityDescriptor> sd =
SecurityDescriptor::FromFile(path, DACL_SECURITY_INFORMATION);
if (!sd || !sd->dacl()) {
return false;
}
if (sd->dacl()->is_null()) {
// If the DACL is null, access is allowed.
return true;
}
PACL pacl = sd->dacl()->get();
// Verify that each SID has the specified access.
// Note that this does not check for the presence of a deny ACE.
for (const Sid& sid : sids) {
bool sid_found = false;
for (DWORD ace_index = 0; ace_index < pacl->AceCount; ++ace_index) {
PACCESS_ALLOWED_ACE ace = nullptr;
if (::GetAce(pacl, ace_index, reinterpret_cast<LPVOID*>(&ace)) &&
ace->Header.AceType == ACCESS_ALLOWED_ACE_TYPE &&
(ace->Header.AceFlags & inheritance) == inheritance &&
(ace->Mask & access_mask) == access_mask &&
sid.Equal(&ace->SidStart)) {
sid_found = true;
break;
}
}
if (!sid_found) {
return false;
}
}
return true;
}
std::vector<Sid> CloneSidVector(const std::vector<Sid>& sids) {
return base::ToVector(sids, &Sid::Clone);
}
void AppendSidVector(std::vector<Sid>& base_sids,
const std::vector<Sid>& append_sids) {
for (const Sid& sid : append_sids) {
base_sids.push_back(sid.Clone());
}
}
std::optional<ACCESS_MASK> GetGrantedAccess(HANDLE handle) {
PUBLIC_OBJECT_BASIC_INFORMATION basic_info = {};
if (!NT_SUCCESS(::NtQueryObject(handle, ObjectBasicInformation, &basic_info,
sizeof(basic_info), nullptr))) {
return std::nullopt;
}
return basic_info.GrantedAccess;
}
} // namespace win
} // namespace base
|