2020-10-13 19:42:30 +02:00
|
|
|
/* SPDX-License-Identifier: GPL-2.0
|
|
|
|
*
|
2021-01-30 16:45:26 +01:00
|
|
|
* Copyright (C) 2018-2021 WireGuard LLC. All Rights Reserved.
|
2020-10-13 19:42:30 +02:00
|
|
|
*/
|
|
|
|
|
2020-10-31 11:55:26 +01:00
|
|
|
#include "logger.h"
|
2021-07-28 13:50:40 +02:00
|
|
|
#include "main.h"
|
2020-10-31 11:55:26 +01:00
|
|
|
#include "resource.h"
|
|
|
|
#include <Windows.h>
|
2021-07-28 20:20:09 +02:00
|
|
|
#include <Shlwapi.h>
|
|
|
|
#include <NTSecAPI.h>
|
2020-10-13 19:42:30 +02:00
|
|
|
|
2021-07-28 20:20:09 +02:00
|
|
|
_Use_decl_annotations_
|
|
|
|
const VOID *
|
|
|
|
ResourceGetAddress(LPCWSTR ResourceName, DWORD *Size)
|
2020-10-13 19:42:30 +02:00
|
|
|
{
|
|
|
|
HRSRC FoundResource = FindResourceW(ResourceModule, ResourceName, RT_RCDATA);
|
|
|
|
if (!FoundResource)
|
2020-11-03 12:29:34 +01:00
|
|
|
{
|
2021-02-02 13:12:45 +01:00
|
|
|
LOG_LAST_ERROR(L"Failed to find resource %s", ResourceName);
|
2020-11-03 12:29:34 +01:00
|
|
|
return NULL;
|
|
|
|
}
|
2020-10-14 08:29:01 +02:00
|
|
|
*Size = SizeofResource(ResourceModule, FoundResource);
|
|
|
|
if (!*Size)
|
2020-11-03 12:29:34 +01:00
|
|
|
{
|
2021-02-02 13:12:45 +01:00
|
|
|
LOG_LAST_ERROR(L"Failed to query resource %s size", ResourceName);
|
2020-11-03 12:29:34 +01:00
|
|
|
return NULL;
|
|
|
|
}
|
2020-10-13 19:42:30 +02:00
|
|
|
HGLOBAL LoadedResource = LoadResource(ResourceModule, FoundResource);
|
|
|
|
if (!LoadedResource)
|
2020-11-03 12:29:34 +01:00
|
|
|
{
|
2021-02-02 13:12:45 +01:00
|
|
|
LOG_LAST_ERROR(L"Failed to load resource %s", ResourceName);
|
2020-11-03 12:29:34 +01:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
BYTE *Address = LockResource(LoadedResource);
|
|
|
|
if (!Address)
|
2020-10-13 19:42:30 +02:00
|
|
|
{
|
2021-02-02 13:12:45 +01:00
|
|
|
LOG(WINTUN_LOG_ERR, L"Failed to lock resource %s", ResourceName);
|
2020-11-03 12:29:34 +01:00
|
|
|
SetLastError(ERROR_LOCK_FAILED);
|
|
|
|
return NULL;
|
2020-10-13 19:42:30 +02:00
|
|
|
}
|
2020-11-03 12:29:34 +01:00
|
|
|
return Address;
|
2020-10-14 08:29:01 +02:00
|
|
|
}
|
|
|
|
|
2021-07-28 20:20:09 +02:00
|
|
|
_Use_decl_annotations_
|
|
|
|
BOOL
|
|
|
|
ResourceCopyToFile(LPCWSTR DestinationPath, LPCWSTR ResourceName)
|
2020-10-14 08:29:01 +02:00
|
|
|
{
|
|
|
|
DWORD SizeResource;
|
2021-07-28 20:20:09 +02:00
|
|
|
const VOID *LockedResource = ResourceGetAddress(ResourceName, &SizeResource);
|
2020-11-03 12:29:34 +01:00
|
|
|
if (!LockedResource)
|
|
|
|
{
|
2021-02-02 13:12:45 +01:00
|
|
|
LOG(WINTUN_LOG_ERR, L"Failed to locate resource %s", ResourceName);
|
2020-11-03 12:29:34 +01:00
|
|
|
return FALSE;
|
|
|
|
}
|
2020-10-13 19:42:30 +02:00
|
|
|
HANDLE DestinationHandle = CreateFileW(
|
|
|
|
DestinationPath,
|
|
|
|
GENERIC_WRITE,
|
|
|
|
0,
|
2020-10-30 13:26:36 +01:00
|
|
|
&SecurityAttributes,
|
2020-10-13 19:42:30 +02:00
|
|
|
CREATE_NEW,
|
|
|
|
FILE_ATTRIBUTE_NORMAL | FILE_ATTRIBUTE_TEMPORARY,
|
|
|
|
NULL);
|
|
|
|
if (DestinationHandle == INVALID_HANDLE_VALUE)
|
2020-11-03 12:29:34 +01:00
|
|
|
{
|
2021-02-02 13:12:45 +01:00
|
|
|
LOG_LAST_ERROR(L"Failed to create file %s", DestinationPath);
|
2020-11-03 12:29:34 +01:00
|
|
|
return FALSE;
|
|
|
|
}
|
2020-10-13 19:42:30 +02:00
|
|
|
DWORD BytesWritten;
|
2020-11-03 12:29:34 +01:00
|
|
|
DWORD LastError;
|
2020-10-13 19:42:30 +02:00
|
|
|
if (!WriteFile(DestinationHandle, LockedResource, SizeResource, &BytesWritten, NULL))
|
2020-11-03 12:29:34 +01:00
|
|
|
{
|
2021-02-02 13:12:45 +01:00
|
|
|
LastError = LOG_LAST_ERROR(L"Failed to write file %s", DestinationPath);
|
2020-11-03 12:29:34 +01:00
|
|
|
goto cleanupDestinationHandle;
|
|
|
|
}
|
2020-10-13 19:42:30 +02:00
|
|
|
if (BytesWritten != SizeResource)
|
|
|
|
{
|
2021-02-02 13:12:45 +01:00
|
|
|
LOG(WINTUN_LOG_ERR,
|
|
|
|
L"Incomplete write to %s (written: %u, expected: %u)",
|
|
|
|
DestinationPath,
|
|
|
|
BytesWritten,
|
|
|
|
SizeResource);
|
2020-11-03 12:29:34 +01:00
|
|
|
LastError = ERROR_WRITE_FAULT;
|
|
|
|
goto cleanupDestinationHandle;
|
2020-10-13 19:42:30 +02:00
|
|
|
}
|
2020-11-03 12:29:34 +01:00
|
|
|
LastError = ERROR_SUCCESS;
|
|
|
|
cleanupDestinationHandle:
|
2020-10-13 19:42:30 +02:00
|
|
|
CloseHandle(DestinationHandle);
|
2020-11-03 12:29:34 +01:00
|
|
|
return RET_ERROR(TRUE, LastError);
|
2020-10-13 19:42:30 +02:00
|
|
|
}
|
2021-07-28 20:20:09 +02:00
|
|
|
|
|
|
|
_Return_type_success_(return != FALSE)
|
|
|
|
BOOL
|
|
|
|
ResourceCreateTemporaryDirectory(_Out_writes_z_(MAX_PATH) LPWSTR RandomTempSubDirectory)
|
|
|
|
{
|
|
|
|
WCHAR WindowsDirectory[MAX_PATH];
|
|
|
|
if (!GetWindowsDirectoryW(WindowsDirectory, _countof(WindowsDirectory)))
|
|
|
|
{
|
|
|
|
LOG_LAST_ERROR(L"Failed to get Windows folder");
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
WCHAR WindowsTempDirectory[MAX_PATH];
|
|
|
|
if (!PathCombineW(WindowsTempDirectory, WindowsDirectory, L"Temp"))
|
|
|
|
{
|
|
|
|
SetLastError(ERROR_BUFFER_OVERFLOW);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
UCHAR RandomBytes[32] = { 0 };
|
|
|
|
if (!RtlGenRandom(RandomBytes, sizeof(RandomBytes)))
|
|
|
|
{
|
|
|
|
LOG(WINTUN_LOG_ERR, L"Failed to generate random");
|
|
|
|
SetLastError(ERROR_GEN_FAILURE);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
WCHAR RandomSubDirectory[sizeof(RandomBytes) * 2 + 1];
|
|
|
|
for (int i = 0; i < sizeof(RandomBytes); ++i)
|
|
|
|
swprintf_s(&RandomSubDirectory[i * 2], 3, L"%02x", RandomBytes[i]);
|
|
|
|
if (!PathCombineW(RandomTempSubDirectory, WindowsTempDirectory, RandomSubDirectory))
|
|
|
|
{
|
|
|
|
SetLastError(ERROR_BUFFER_OVERFLOW);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
if (!CreateDirectoryW(RandomTempSubDirectory, &SecurityAttributes))
|
|
|
|
{
|
|
|
|
LOG_LAST_ERROR(L"Failed to create temporary folder %s", RandomTempSubDirectory);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
return TRUE;
|
|
|
|
}
|