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 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480
|
#include "security_descriptor_builder.hpp"
#ifdef FZ_WINDOWS
#include "../libfilezilla/logger.hpp"
#include "../libfilezilla/thread.hpp"
#include "../libfilezilla/glue/dll.hpp"
#include "../libfilezilla/translate.hpp"
#include <array>
#include <map>
#include <sddl.h>
#include <lm.h>
#include <dsgetdc.h>
namespace fz {
namespace {
template<typename T>
struct holder final
{
holder() = default;
~holder()
{
clear();
}
static holder create(size_t s) {
holder h;
h.v_ = reinterpret_cast<T*>(::operator new[](s, std::align_val_t(alignof(T))));
h.size_ = s;
h.delete_ = true;
return h;
}
void clear()
{
if (delete_) {
::operator delete[](reinterpret_cast<void*>(v_), std::align_val_t(alignof(T)));
}
size_ = 0;
v_ = nullptr;
}
static holder create(void* v, bool del)
{
holder h;
h.v_ = reinterpret_cast<T*>(v);
h.size_ = v ? size_t(-1) : 0;
h.delete_ = del;
return h;
}
holder(holder&& h) noexcept
: v_(h.v_)
, size_(h.size_)
{
h.v_ = nullptr;
h.size_ = 0;
delete_ = h.delete_;
}
holder& operator=(holder&& h) noexcept {
if (this != &h) {
clear();
v_ = h.v_;
size_ = h.size_;
h.v_ = nullptr;
h.size_ = 0;
delete_ = h.delete_;
}
return *this;
}
size_t size() const { return size_; }
explicit operator bool() const { return v_ != nullptr; }
holder(holder const&) = delete;
holder& operator=(holder const&) = delete;
T* get() { return v_; }
T& operator*() { return *v_; }
T* operator->() { return v_; }
private:
bool delete_{};
T* v_{};
size_t size_{};
};
}
struct security_descriptor_builder::impl
{
holder<SID> get_sid(entity e);
bool init_user();
std::map<entity, DWORD> rights_;
holder<TOKEN_USER> user_;
holder<ACL> acl_;
SECURITY_DESCRIPTOR sd_{};
};
security_descriptor_builder::security_descriptor_builder()
: impl_(std::make_unique<impl>())
{
}
security_descriptor_builder::~security_descriptor_builder()
{
}
void security_descriptor_builder::add(entity e, DWORD rights)
{
impl_->acl_.clear();
impl_->rights_[e] = rights;
}
ACL* security_descriptor_builder::get_acl(sdb_flags f)
{
if (impl_->acl_) {
return impl_->acl_.get();
}
if (!impl_->init_user()) {
return nullptr;
}
DWORD const needed = static_cast<DWORD>(sizeof(ACL) + (sizeof(ACCESS_ALLOWED_ACE) - sizeof(DWORD) + SECURITY_MAX_SID_SIZE) * impl_->rights_.size());
auto acl = holder<ACL>::create(needed);
if (InitializeAcl(acl.get(), needed, ACL_REVISION)) {
for (auto it = impl_->rights_.cbegin(); acl && it != impl_->rights_.cend(); ++it) {
auto sid = impl_->get_sid(it->first);
DWORD flags = (f & sdb_flags::inheritable) ? (CONTAINER_INHERIT_ACE | OBJECT_INHERIT_ACE) : 0;
if (!sid || !AddAccessAllowedAceEx(acl.get(), ACL_REVISION, flags, it->second, sid.get())) {
return {};
}
}
impl_->acl_ = std::move(acl);
}
if (impl_->acl_) {
InitializeSecurityDescriptor(&impl_->sd_, SECURITY_DESCRIPTOR_REVISION);
SetSecurityDescriptorControl(&impl_->sd_, SE_DACL_PROTECTED, (f & sdb_flags::inherit_from_parent) ? 0 : SE_DACL_PROTECTED);
SetSecurityDescriptorDacl(&impl_->sd_, TRUE, impl_->acl_.get(), FALSE);
SetSecurityDescriptorOwner(&impl_->sd_, impl_->user_->User.Sid, FALSE);
SetSecurityDescriptorGroup(&impl_->sd_, NULL, FALSE);
SetSecurityDescriptorSacl(&impl_->sd_, FALSE, NULL, FALSE);
}
return impl_->acl_.get();
}
SECURITY_DESCRIPTOR* security_descriptor_builder::get_sd(sdb_flags f)
{
if (!get_acl(f)) {
return nullptr;
}
return &impl_->sd_;
}
namespace {
WELL_KNOWN_SID_TYPE GetWellKnownSidType(security_descriptor_builder::entity e)
{
switch (e) {
case security_descriptor_builder::administrators:
return WinBuiltinAdministratorsSid;
case security_descriptor_builder::authenticated_users:
return WinAuthenticatedUserSid;
case security_descriptor_builder::users:
return WinBuiltinUsersSid;
case security_descriptor_builder::system:
return WinLocalSystemSid;
default:
return WinNullSid;
}
}
}
holder<SID> security_descriptor_builder::impl::get_sid(entity e)
{
if (e == self) {
init_user();
return holder<SID>::create(user_ ? user_->User.Sid : nullptr, false);
}
else {
WELL_KNOWN_SID_TYPE wk = GetWellKnownSidType(e);
if (wk == WinNullSid) {
return {};
}
auto sid = holder<SID>::create(SECURITY_MAX_SID_SIZE);
DWORD l = SECURITY_MAX_SID_SIZE;
if (!CreateWellKnownSid(wk, nullptr, sid.get(), &l)) {
return {};
}
return sid;
}
}
namespace {
holder<TOKEN_USER> GetUserFromToken(HANDLE token)
{
DWORD needed{};
GetTokenInformation(token, TokenUser, NULL, 0, &needed);
if (GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
auto user = holder<TOKEN_USER>::create(needed);
if (GetTokenInformation(token, TokenUser, user.get(), needed, &needed)) {
return user;
}
}
return {};
}
}
bool security_descriptor_builder::impl::init_user()
{
if (user_) {
return true;
}
HANDLE token{INVALID_HANDLE_VALUE};
if (!OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY, &token)) {
return false;
}
user_ = GetUserFromToken(token);
CloseHandle(token);
return user_.operator bool();
}
bool GetUserAndDomainFromSid(PSID sid, std::wstring& user, std::wstring& domain)
{
DWORD user_size = 0;
DWORD domain_size = 0;
SID_NAME_USE sid_type;
if (LookupAccountSidW(nullptr, sid, nullptr, &user_size, nullptr, &domain_size, &sid_type)) {
return false;
}
if (GetLastError() != ERROR_INSUFFICIENT_BUFFER || user_size == 0 || domain_size == 0) {
return false;
}
user.resize(user_size - 1);
domain.resize(domain_size - 1);
if (!LookupAccountSidW(nullptr, sid, user.data(), &user_size, domain.data(), &domain_size, &sid_type)) {
return false;
}
user.resize(std::char_traits<wchar_t>::length(user.data()));
domain.resize(std::char_traits<wchar_t>::length(domain.data()));
return true;
}
namespace {
bool GetRoamingProfilePath(HANDLE h, std::wstring const& user, std::wstring &domain, std::wstring& roaming_profile_path, logger_interface& logger)
{
static dll netapi32_dll(L"netapi32.dll", LOAD_LIBRARY_SEARCH_SYSTEM32);
FZ_DLL_IMPORT(netapi32_dll, DsGetDcNameW);
FZ_DLL_IMPORT(netapi32_dll, NetApiBufferFree);
FZ_DLL_IMPORT(netapi32_dll, NetUserGetInfo);
logger.log(logmsg::debug_debug, L"Entering GetRoamingProfilePath(%s, %s)", user, domain);
if (!DsGetDcNameW || !NetApiBufferFree || !NetUserGetInfo) {
logger.log_raw(logmsg::error, fztranslate("GetRoamingProfilePath: couldn't import netapi32.dll functions."));
return false;
}
auto make_fqdn_if_necessary = [&](std::wstring &fqdn) {
static wchar_t computer_name[MAX_COMPUTERNAME_LENGTH + 1];
DWORD computer_name_size = MAX_COMPUTERNAME_LENGTH + 1;
if (!GetComputerNameW(computer_name, &computer_name_size)) {
auto err = GetLastError();
logger.log(logmsg::error, fztranslate("GetRoamingProfilePath: GetComputerNameW failed. User: '%s', Domain: '%s'. Last Error: %d."), user, domain, err);
return false;
}
if (domain == computer_name) {
domain.clear();
fqdn.clear();
return true;
}
// Resolve the domain controller name
DOMAIN_CONTROLLER_INFOW* dc_info{};
DWORD error = DsGetDcNameW(
nullptr,
domain.c_str(),
nullptr,
nullptr,
DS_DIRECTORY_SERVICE_REQUIRED | DS_RETURN_DNS_NAME,
&dc_info
);
bool success = error == ERROR_SUCCESS && dc_info && dc_info->DomainControllerName;
if (success) {
logger.log(logmsg::debug_debug, L"GetRoamingProfilePath(%s, %s): DC name = [%s]", user, domain, dc_info->DomainControllerName);
fqdn = dc_info->DomainControllerName;
}
else {
logger.log(logmsg::debug_debug, L"GetRoamingProfilePath(%s, %s): DsGetDcNameW failed. Error: %d.", user, domain, error);
}
NetApiBufferFree(dc_info);
return success;
};
std::wstring fqdn;
if (!make_fqdn_if_necessary(fqdn)) {
return false;
}
thread worker;
NET_API_STATUS netusergetinfo_status{};
DWORD impersonateloggedonuser_err{};
DWORD reverttoself_err{};
worker.run([&] {
if (!ImpersonateLoggedOnUser(h)) {
impersonateloggedonuser_err = GetLastError();
return;
}
LPBYTE buf_ptr = nullptr;
netusergetinfo_status = NetUserGetInfo(fqdn.empty() ? nullptr : fqdn.c_str(), user.c_str(), 3, &buf_ptr);
if (netusergetinfo_status == NERR_Success) {
USER_INFO_3* user_info = reinterpret_cast<USER_INFO_3*>(buf_ptr);
if (user_info->usri3_profile) {
roaming_profile_path = user_info->usri3_profile;
}
}
NetApiBufferFree(buf_ptr);
if (!RevertToSelf()) {
reverttoself_err = GetLastError();
}
});
worker.join();
if (impersonateloggedonuser_err) {
logger.log(logmsg::error, fztranslate("GetRoamingProfilePath: ImpersonateLoggedOnUser failed. User: '%s', Domain: '%s'. Last Error: %d."), user, domain, impersonateloggedonuser_err);
return false;
}
if (netusergetinfo_status != NERR_Success) {
logger.log(logmsg::error, fztranslate("GetRoamingProfilePath: NetUserGetInfo failed. User: '%s', Domain: '%s'. Status: %d."), user, domain, netusergetinfo_status);
}
if (reverttoself_err) {
logger.log(logmsg::error, fztranslate("GetRoamingProfilePath: RevertToSelf failed, aborting. User: '%s', Domain: '%s'. Last Error: %d."), user, domain, reverttoself_err);
std::fflush(nullptr);
TerminateProcess(GetCurrentProcess(), EXIT_FAILURE);
}
return netusergetinfo_status == NERR_Success;
}
}
bool GetUserInfoFromToken(
HANDLE h,
std::string& sid_string,
std::wstring& roaming_profile_path,
std::wstring& username,
std::wstring& domain,
logger_interface& logger
)
{
auto user = GetUserFromToken(h);
if (!user) {
auto err = GetLastError();
logger.log(logmsg::error, fztranslate("GetUserInfoFromToken: GetUserFromToken failed. Last Error: %d."), err);
return false;
}
LPSTR sid{};
if (!ConvertSidToStringSidA(user->User.Sid, &sid)) {
auto err = GetLastError();
logger.log(logmsg::error, fztranslate("GetUserInfoFromToken: ConvertSidToStringSidA failed. Last Error: %d."), err);
return false;
}
sid_string = sid;
LocalFree(sid);
if (!GetUserAndDomainFromSid(user->User.Sid, username, domain)) {
auto err = GetLastError();
logger.log(logmsg::error, fztranslate("GetUserInfoFromToken: GetUserAndDomainFromSid failed. Last Error: %d."), err);
return false;
}
if (domain == L"NT AUTHORITY" || domain == L"BUILTIN") {
roaming_profile_path.clear();
domain.clear();
}
else
if (!GetRoamingProfilePath(h, username, domain, roaming_profile_path, logger)) {
return false;
}
return true;
}
namespace {
holder<TOKEN_PRIVILEGES> GetPrivileges(HANDLE token)
{
DWORD needed{};
GetTokenInformation(token, TokenPrivileges, NULL, 0, &needed);
if (GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
auto privs = holder<TOKEN_PRIVILEGES>::create(needed);
if (GetTokenInformation(token, TokenPrivileges, privs.get(), needed, &needed)) {
return privs;
}
}
return {};
}
}
bool DropAdminPrivilegesFromToken(HANDLE h)
{
auto privs = GetPrivileges(h);
if (!privs) {
return false;
}
std::array<LUID, 2> allowed;
if (!LookupPrivilegeValue(nullptr, SE_INC_WORKING_SET_NAME, &allowed[0])) {
return false;
}
if (!LookupPrivilegeValue(nullptr, SE_CHANGE_NOTIFY_NAME, &allowed[1])) {
return false;
}
DWORD out{};
for (DWORD i = 0; i < privs->PrivilegeCount; ++i) {
bool found{};
for (auto const& luid : allowed) {
if (std::tie(luid.LowPart, luid.HighPart) == std::tie(privs->Privileges[i].Luid.LowPart, privs->Privileges[i].Luid.HighPart)) {
found = true;
break;
}
}
if (!found) {
privs->Privileges[out].Luid = privs->Privileges[i].Luid;
privs->Privileges[out].Attributes = SE_PRIVILEGE_REMOVED;
++out;
}
}
if (out) {
privs->PrivilegeCount = out;
if (!AdjustTokenPrivileges(h, false, privs.get(), privs.size(), nullptr, nullptr)) {
return false;
}
}
return true;
}
}
#endif
|