This source file includes following definitions.
- RecordSuccessfulThunkSetup
- LeaveSetupBeacon
- ResetBeacon
- BlacklistSize
- IsBlacklistInitialized
- AddDllToBlacklist
- RemoveDllFromBlacklist
- SuccessfullyBlocked
- BlockedDll
- Initialize
#include "chrome_elf/blacklist/blacklist.h"
#include <assert.h>
#include <string.h>
#include "base/basictypes.h"
#include "chrome_elf/blacklist/blacklist_interceptions.h"
#include "chrome_elf/chrome_elf_constants.h"
#include "chrome_elf/chrome_elf_util.h"
#include "chrome_elf/thunk_getter.h"
#include "sandbox/win/src/interception_internal.h"
#include "sandbox/win/src/internal_types.h"
#include "sandbox/win/src/service_resolver.h"
extern "C" IMAGE_DOS_HEADER __ImageBase;
namespace blacklist{
const wchar_t* g_troublesome_dlls[kTroublesomeDllsMaxCount] = {
L"datamngr.dll",
L"hk.dll",
L"libsvn_tsvn32.dll",
L"lmrn.dll",
NULL,
};
bool g_blocked_dlls[kTroublesomeDllsMaxCount] = {};
int g_num_blocked_dlls = 0;
}
#pragma section(".crthunk",read,execute)
__declspec(allocate(".crthunk")) sandbox::ThunkData g_thunk_storage;
namespace {
bool g_blacklist_initialized = false;
void RecordSuccessfulThunkSetup(HKEY* key) {
if (key != NULL) {
DWORD blacklist_state = blacklist::BLACKLIST_SETUP_RUNNING;
::RegSetValueEx(*key,
blacklist::kBeaconState,
0,
REG_DWORD,
reinterpret_cast<LPBYTE>(&blacklist_state),
sizeof(blacklist_state));
::RegCloseKey(*key);
key = NULL;
}
}
}
namespace blacklist {
#if defined(_WIN64)
#pragma section(".oldntmap",write,read)
__declspec(allocate(".oldntmap"))
NtMapViewOfSectionFunction g_nt_map_view_of_section_func = NULL;
#endif
bool LeaveSetupBeacon() {
HKEY key = NULL;
DWORD disposition = 0;
LONG result = ::RegCreateKeyEx(HKEY_CURRENT_USER,
kRegistryBeaconPath,
0,
NULL,
REG_OPTION_NON_VOLATILE,
KEY_QUERY_VALUE | KEY_SET_VALUE,
NULL,
&key,
&disposition);
if (result != ERROR_SUCCESS)
return false;
DWORD blacklist_state = BLACKLIST_DISABLED;
DWORD blacklist_state_size = sizeof(blacklist_state);
DWORD type = 0;
result = ::RegQueryValueEx(key,
kBeaconState,
0,
&type,
reinterpret_cast<LPBYTE>(&blacklist_state),
&blacklist_state_size);
if (blacklist_state != BLACKLIST_ENABLED ||
result != ERROR_SUCCESS || type != REG_DWORD) {
::RegCloseKey(key);
return false;
}
blacklist_state = BLACKLIST_SETUP_RUNNING;
result = ::RegSetValueEx(key,
kBeaconState,
0,
REG_DWORD,
reinterpret_cast<LPBYTE>(&blacklist_state),
sizeof(blacklist_state));
::RegCloseKey(key);
return (result == ERROR_SUCCESS);
}
bool ResetBeacon() {
HKEY key = NULL;
DWORD disposition = 0;
LONG result = ::RegCreateKeyEx(HKEY_CURRENT_USER,
kRegistryBeaconPath,
0,
NULL,
REG_OPTION_NON_VOLATILE,
KEY_QUERY_VALUE | KEY_SET_VALUE,
NULL,
&key,
&disposition);
if (result != ERROR_SUCCESS)
return false;
DWORD blacklist_state = BLACKLIST_ENABLED;
result = ::RegSetValueEx(key,
kBeaconState,
0,
REG_DWORD,
reinterpret_cast<LPBYTE>(&blacklist_state),
sizeof(blacklist_state));
::RegCloseKey(key);
return (result == ERROR_SUCCESS);
}
int BlacklistSize() {
int size = -1;
while (blacklist::g_troublesome_dlls[++size] != NULL) {}
return size;
}
bool IsBlacklistInitialized() {
return g_blacklist_initialized;
}
bool AddDllToBlacklist(const wchar_t* dll_name) {
int blacklist_size = BlacklistSize();
if (blacklist_size + 1 >= kTroublesomeDllsMaxCount)
return false;
for (int i = 0; i < blacklist_size; ++i) {
if (!_wcsicmp(g_troublesome_dlls[i], dll_name))
return true;
}
wchar_t* str_buffer = new wchar_t[wcslen(dll_name) + 1];
wcscpy(str_buffer, dll_name);
g_troublesome_dlls[blacklist_size] = str_buffer;
g_blocked_dlls[blacklist_size] = false;
return true;
}
bool RemoveDllFromBlacklist(const wchar_t* dll_name) {
int blacklist_size = BlacklistSize();
for (int i = 0; i < blacklist_size; ++i) {
if (!_wcsicmp(g_troublesome_dlls[i], dll_name)) {
delete[] g_troublesome_dlls[i];
g_troublesome_dlls[i] = g_troublesome_dlls[blacklist_size - 1];
g_troublesome_dlls[blacklist_size - 1] = NULL;
if (g_blocked_dlls[i])
--g_num_blocked_dlls;
g_blocked_dlls[i] = g_blocked_dlls[blacklist_size - 1];
return true;
}
}
return false;
}
void SuccessfullyBlocked(const wchar_t** blocked_dlls, int* size) {
if (size == NULL)
return;
if (blocked_dlls == NULL && *size < g_num_blocked_dlls) {
*size = g_num_blocked_dlls;
return;
}
*size = g_num_blocked_dlls;
int strings_to_fill = 0;
for (int i = 0; strings_to_fill < g_num_blocked_dlls && g_troublesome_dlls[i];
++i) {
if (g_blocked_dlls[i]) {
blocked_dlls[strings_to_fill] = g_troublesome_dlls[i];
++strings_to_fill;
}
}
}
void BlockedDll(size_t blocked_index) {
assert(blocked_index < kTroublesomeDllsMaxCount);
if (!g_blocked_dlls[blocked_index] &&
blocked_index < kTroublesomeDllsMaxCount) {
++g_num_blocked_dlls;
g_blocked_dlls[blocked_index] = true;
}
}
bool Initialize(bool force) {
if (!InitializeInterceptImports())
return false;
if (IsNonBrowserProcess())
return false;
if (!force && !LeaveSetupBeacon())
return false;
const bool kRelaxed = false;
sandbox::ServiceResolverThunk* thunk = GetThunk(kRelaxed);
if (!thunk)
return false;
HKEY key = NULL;
DWORD disposition = 0;
LONG result = ::RegCreateKeyEx(HKEY_CURRENT_USER,
kRegistryBeaconPath,
0,
NULL,
REG_OPTION_NON_VOLATILE,
KEY_QUERY_VALUE | KEY_SET_VALUE,
NULL,
&key,
&disposition);
if (result == ERROR_SUCCESS) {
DWORD blacklist_state = BLACKLIST_THUNK_SETUP;
::RegSetValueEx(key,
kBeaconState,
0,
REG_DWORD,
reinterpret_cast<LPBYTE>(&blacklist_state),
sizeof(blacklist_state));
} else {
key = NULL;
}
g_blacklist_initialized = true;
BYTE* thunk_storage = reinterpret_cast<BYTE*>(&g_thunk_storage);
DWORD old_protect = 0;
if (!VirtualProtect(&g_thunk_storage,
sizeof(g_thunk_storage),
PAGE_EXECUTE_READWRITE,
&old_protect)) {
RecordSuccessfulThunkSetup(&key);
return false;
}
thunk->AllowLocalPatches();
BOOL page_executable = false;
#if defined(_WIN64)
NTSTATUS ret = thunk->Setup(::GetModuleHandle(sandbox::kNtdllName),
reinterpret_cast<void*>(&__ImageBase),
"NtMapViewOfSection",
NULL,
&blacklist::BlNtMapViewOfSection64,
thunk_storage,
sizeof(sandbox::ThunkData),
NULL);
g_nt_map_view_of_section_func = reinterpret_cast<NtMapViewOfSectionFunction>(
thunk_storage);
page_executable = VirtualProtect(&g_nt_map_view_of_section_func,
sizeof(g_nt_map_view_of_section_func),
PAGE_EXECUTE_READ,
&old_protect);
#else
NTSTATUS ret = thunk->Setup(::GetModuleHandle(sandbox::kNtdllName),
reinterpret_cast<void*>(&__ImageBase),
"NtMapViewOfSection",
NULL,
&blacklist::BlNtMapViewOfSection,
thunk_storage,
sizeof(sandbox::ThunkData),
NULL);
#endif
delete thunk;
page_executable = page_executable && VirtualProtect(&g_thunk_storage,
sizeof(g_thunk_storage),
PAGE_EXECUTE_READ,
&old_protect);
RecordSuccessfulThunkSetup(&key);
return NT_SUCCESS(ret) && page_executable;
}
}