// Copyright (c) 2011 The Chromium Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file.
// Holds the information about a known registry key. struct KnownReservedKey { constwchar_t* name;
HKEY key;
};
// Contains all the known registry key by name and by handle. const KnownReservedKey kKnownKey[] = {
{L"HKEY_CLASSES_ROOT", HKEY_CLASSES_ROOT},
{L"HKEY_CURRENT_USER", HKEY_CURRENT_USER},
{L"HKEY_LOCAL_MACHINE", HKEY_LOCAL_MACHINE},
{L"HKEY_USERS", HKEY_USERS},
{L"HKEY_PERFORMANCE_DATA", HKEY_PERFORMANCE_DATA},
{L"HKEY_PERFORMANCE_TEXT", HKEY_PERFORMANCE_TEXT},
{L"HKEY_PERFORMANCE_NLSTEXT", HKEY_PERFORMANCE_NLSTEXT},
{L"HKEY_CURRENT_CONFIG", HKEY_CURRENT_CONFIG},
{L"HKEY_DYN_DATA", HKEY_DYN_DATA}};
// Returns the offset to the path seperator following // "\Device\HarddiskVolumeX" in |path|.
size_t PassHarddiskVolume(const std::wstring& path) { static constexpr wchar_t pattern[] = L"\\Device\\HarddiskVolume"; const size_t patternLen = base::size(pattern) - 1;
// First, check for |pattern|. if ((path.size() < patternLen) || (!EqualPath(path, pattern, patternLen))) return std::wstring::npos;
// Find the next path separator, after the pattern match. return path.find_first_of(L'\\', patternLen - 1);
}
// Returns true if |path| starts with "\Device\HarddiskVolumeX\" and returns a // path without that component. |removed| will hold the prefix removed. bool IsDeviceHarddiskPath(const std::wstring& path,
std::wstring* trimmed_path,
std::wstring* removed) {
size_t offset = PassHarddiskVolume(path); if (offset == std::wstring::npos) returnfalse;
// Remove up to and including the path separator.
*removed = path.substr(0, offset + 1); // Remaining path starts after the path separator.
*trimmed_path = path.substr(offset + 1); returntrue;
}
bool StartsWithDriveLetter(const std::wstring& path) { if (path.size() < kDriveLetterLen) returnfalse;
if (path[1] != L':' || path[2] != L'\\') returnfalse;
return base::IsAsciiAlpha(path[0]);
}
// Removes "\\\\.\\" from the path. void RemoveImpliedDevice(std::wstring* path) { if (EqualPath(*path, kNTDotPrefix, kNTDotPrefixLen))
*path = path->substr(kNTDotPrefixLen);
}
} // namespace
namespace sandbox {
// Returns true if the provided path points to a pipe. bool IsPipe(const std::wstring& path) {
size_t start = 0; if (EqualPath(path, sandbox::kNTPrefix, sandbox::kNTPrefixLen))
start = sandbox::kNTPrefixLen;
// |full_path| can have any of the following forms: // \??\c:\some\foo\bar // \Device\HarddiskVolume0\some\foo\bar // \??\HarddiskVolume0\some\foo\bar // \??\UNC\SERVER\Share\some\foo\bar
DWORD IsReparsePoint(const std::wstring& full_path) { // Check if it's a pipe. We can't query the attributes of a pipe. if (IsPipe(full_path)) return ERROR_NOT_A_REPARSE_POINT;
if (!has_drive && !is_device_path && !nt_path) return ERROR_INVALID_NAME;
if (!has_drive) { // Add Win32 device namespace prefix, required for some Windows APIs.
path.insert(0, kNTDotPrefix);
}
// Ensure that volume path matches start of path. wchar_t vol_path[MAX_PATH]; if (!::GetVolumePathNameW(path.c_str(), vol_path, MAX_PATH)) { // This will fail if this is a device that isn't volume related, which can't // then be a reparse point. return is_device_path ? ERROR_NOT_A_REPARSE_POINT : ERROR_INVALID_NAME;
}
// vol_path includes a trailing slash, so reduce size for path and loop check.
size_t vol_path_len = wcslen(vol_path) - 1; if (!EqualPath(path, vol_path, vol_path_len)) { return ERROR_INVALID_NAME;
}
do {
DWORD attributes = ::GetFileAttributes(path.c_str()); if (INVALID_FILE_ATTRIBUTES == attributes) {
DWORD error = ::GetLastError(); if (error != ERROR_FILE_NOT_FOUND && error != ERROR_PATH_NOT_FOUND &&
error != ERROR_INVALID_FUNCTION &&
error != ERROR_INVALID_NAME) { // Unexpected error. return error;
}
} elseif (FILE_ATTRIBUTE_REPARSE_POINT & attributes) { // This is a reparse point. return ERROR_SUCCESS;
}
path.resize(path.rfind(L'\\'));
} while (path.size() > vol_path_len); // Skip root dir.
return ERROR_NOT_A_REPARSE_POINT;
}
// We get a |full_path| of the forms accepted by IsReparsePoint(), and the name // we'll get from |handle| will be \device\harddiskvolume1\some\foo\bar. bool SameObject(HANDLE handle, constwchar_t* full_path) { // Check if it's a pipe. if (IsPipe(full_path)) returntrue;
std::wstring actual_path; if (!GetPathFromHandle(handle, &actual_path)) returnfalse;
if (!has_drive && nt_path) {
std::wstring simple_actual_path; if (IsDevicePath(path, &path)) { if (IsDevicePath(actual_path, &simple_actual_path)) { // Perfect match (case-insensitive check). return (EqualPath(simple_actual_path, path));
} else { returnfalse;
}
} else { // Add Win32 device namespace for GetVolumePathName.
path.insert(0, kNTDotPrefix);
}
}
// Get the volume path in the same format as actual_path. wchar_t vol_path[MAX_PATH]; if (!::GetVolumePathName(path.c_str(), vol_path, MAX_PATH)) { returnfalse;
}
size_t vol_path_len = wcslen(vol_path);
base::string16 nt_vol; if (!GetNtPathFromWin32Path(vol_path, &nt_vol)) { returnfalse;
}
// The two paths should be the same length. if (nt_vol.size() + path.size() - vol_path_len != actual_path.size()) { returnfalse;
}
// Check the volume matches. if (!EqualPath(actual_path, nt_vol.c_str(), nt_vol.size())) { returnfalse;
}
// Check the path after the volume matches. if (!EqualPath(actual_path, nt_vol.size(), path, vol_path_len)) { returnfalse;
}
returntrue;
}
// Just make a best effort here. There are lots of corner cases that we're // not expecting - and will fail to make long. bool ConvertToLongPath(std::wstring* native_path, const std::wstring* drive_letter) { if (IsPipe(*native_path)) returntrue;
// Process a few prefix types. if (IsNTPath(*native_path, &temp_path)) { // "\??\" if (!StartsWithDriveLetter(temp_path)) { // Prepend with "\\.\".
temp_path = std::wstring(kNTDotPrefix) + temp_path;
added_implied_device = true;
}
is_nt_path = true;
} elseif (IsDeviceHarddiskPath(*native_path, &temp_path, &to_restore)) { // "\Device\HarddiskVolumeX\" - hacky attempt making ::GetLongPathName // work for native device paths. Remove "\Device\HarddiskVolumeX\" and // replace with drive letter.
// Nothing we can do if we don't have a drive letter. Leave |native_path| // as is. if (!drive_letter || drive_letter->empty()) returnfalse;
temp_path = *drive_letter + temp_path;
is_device_harddisk_path = true;
} elseif (IsDevicePath(*native_path, &temp_path)) { // "\Device\" - there's nothing we can do to convert to long here. returnfalse;
}
DWORD last_error = ::GetLastError(); if (0 == return_value && (ERROR_FILE_NOT_FOUND == last_error ||
ERROR_PATH_NOT_FOUND == last_error ||
ERROR_INVALID_NAME == last_error)) { // The file does not exist, but maybe a sub path needs to be expanded.
std::wstring::size_type last_slash = temp_path.rfind(L'\\'); if (std::wstring::npos == last_slash) returnfalse;
std::wstring begin = temp_path.substr(0, last_slash);
std::wstring end = temp_path.substr(last_slash); if (!ConvertToLongPath(&begin)) returnfalse;
// Ok, it worked. Let's reset the return value.
temp_path = begin + end;
return_value = 1;
} elseif (0 != return_value) {
temp_path = long_path_buf.get();
}
// If successful, re-apply original namespace prefix before returning. if (return_value != 0) { if (added_implied_device)
RemoveImpliedDevice(&temp_path);
OBJECT_NAME_INFORMATION initial_buffer;
OBJECT_NAME_INFORMATION* name = &initial_buffer;
ULONG size = sizeof(initial_buffer); // Query the name information a first time to get the size of the name. // Windows XP requires that the size of the buffer passed in here be != 0.
NTSTATUS status =
NtQueryObject(handle, ObjectNameInformation, name, size, &size);
std::unique_ptr<BYTE[]> name_ptr; if (size) {
name_ptr.reset(new BYTE[size]);
name = reinterpret_cast<OBJECT_NAME_INFORMATION*>(name_ptr.get());
// Query the name information a second time to get the name of the // object referenced by the handle.
status = NtQueryObject(handle, ObjectNameInformation, name, size, &size);
}
// Always attempt to restore the original protection. if (!::VirtualProtectEx(child_process, address, length, old_protection,
&old_protection)) returnfalse;
// Allocate memory in the target process without specifying the address void* remote_data = ::VirtualAllocEx(child, nullptr, buffer_bytes, MEM_COMMIT,
PAGE_READWRITE); if (!remote_data) returnfalse;
// This function uses the undocumented PEB ImageBaseAddress field to extract // the base address of the new process. void* GetProcessBaseAddress(HANDLE process) {
NtQueryInformationProcessFunction query_information_process = nullptr;
ResolveNTFunctionPtr("NtQueryInformationProcess", &query_information_process); if (!query_information_process) return nullptr;
PROCESS_BASIC_INFORMATION process_basic_info = {};
NTSTATUS status = query_information_process(
process, ProcessBasicInformation, &process_basic_info, sizeof(process_basic_info), nullptr); if (STATUS_SUCCESS != status) return nullptr;
if (magic[0] != 'M' || magic[1] != 'Z') return nullptr;
#ifdefined(_M_ARM64) // Windows 10 on ARM64 has multi-threaded DLL loading that does not work with // the sandbox. (On x86 this gets disabled by hook detection code that was not // ported to ARM64). This overwrites the LoaderThreads value in the process // parameters part of the PEB, if it is set to the default of 0 (which // actually means it defaults to 4 loading threads). This is an undocumented // field so there is a, probably small, risk that it might change or move in // the future. In order to slightly guard against that we only update if the // value is currently 0. auto processParameters = reinterpret_cast<uint8_t*>(peb.ProcessParameters); const uint32_t loaderThreadsOffset = 0x40c;
uint32_t maxLoaderThreads = 0; BOOL memoryRead = ::ReadProcessMemory(
process, processParameters + loaderThreadsOffset, &maxLoaderThreads, sizeof(maxLoaderThreads), &bytes_read); if (memoryRead && (sizeof(maxLoaderThreads) == bytes_read) &&
(maxLoaderThreads == 0)) {
maxLoaderThreads = 1;
WriteProtectedChildMemory(process, processParameters + loaderThreadsOffset,
&maxLoaderThreads, sizeof(maxLoaderThreads),
PAGE_READWRITE);
} #endif
return base_address;
}
DWORD GetTokenInformation(HANDLE token,
TOKEN_INFORMATION_CLASS info_class,
std::unique_ptr<BYTE[]>* buffer) { // Get the required buffer size.
DWORD size = 0;
::GetTokenInformation(token, info_class, nullptr, 0, &size); if (!size) { return ::GetLastError();
}
auto temp_buffer = std::make_unique<BYTE[]>(size); if (!::GetTokenInformation(token, info_class, temp_buffer.get(), size,
&size)) { return ::GetLastError();
}
if (!ntdll) {
HMODULE ntdll_local = ::GetModuleHandle(sandbox::kNtdllName); // Use PEImage to sanity-check that we have a valid ntdll handle.
base::win::PEImage ntdll_peimage(ntdll_local);
CHECK_NT(ntdll_peimage.VerifyMagic()); // Race-safe way to set static ntdll.
::InterlockedCompareExchangePointer( reinterpret_cast<PVOID volatile*>(&ntdll), ntdll_local, nullptr);
}
¤ Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.0.37Bemerkung:
(vorverarbeitet)
¤
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.