api: selectively use temporary variable to prepare output

Suggested-by: Jason A. Donenfeld <Jason@zx2c4.com>
Signed-off-by: Simon Rozman <simon@rozman.si>
This commit is contained in:
Simon Rozman 2020-10-30 13:08:57 +01:00
parent 8c935ce151
commit 1b3af95be3
3 changed files with 66 additions and 55 deletions

View File

@ -33,14 +33,17 @@ AdapterGetDrvInfoDetail(
DWORD Size = sizeof(SP_DRVINFO_DETAIL_DATA_W) + 0x100; DWORD Size = sizeof(SP_DRVINFO_DETAIL_DATA_W) + 0x100;
for (;;) for (;;)
{ {
*DrvInfoDetailData = HeapAlloc(ModuleHeap, 0, Size); SP_DRVINFO_DETAIL_DATA_W *p = HeapAlloc(ModuleHeap, 0, Size);
if (!*DrvInfoDetailData) if (!p)
return LOG(WINTUN_LOG_ERR, L"Out of memory"), ERROR_OUTOFMEMORY; return LOG(WINTUN_LOG_ERR, L"Out of memory"), ERROR_OUTOFMEMORY;
(*DrvInfoDetailData)->cbSize = sizeof(SP_DRVINFO_DETAIL_DATA_W); p->cbSize = sizeof(SP_DRVINFO_DETAIL_DATA_W);
if (SetupDiGetDriverInfoDetailW(DevInfo, DevInfoData, DrvInfoData, *DrvInfoDetailData, Size, &Size)) if (SetupDiGetDriverInfoDetailW(DevInfo, DevInfoData, DrvInfoData, p, Size, &Size))
{
*DrvInfoDetailData = p;
return ERROR_SUCCESS; return ERROR_SUCCESS;
}
DWORD Result = GetLastError(); DWORD Result = GetLastError();
HeapFree(ModuleHeap, 0, *DrvInfoDetailData); HeapFree(ModuleHeap, 0, p);
if (Result != ERROR_INSUFFICIENT_BUFFER) if (Result != ERROR_INSUFFICIENT_BUFFER)
return LOG_ERROR(L"Failed", Result); return LOG_ERROR(L"Failed", Result);
} }
@ -57,13 +60,16 @@ GetDeviceRegistryProperty(
{ {
for (;;) for (;;)
{ {
*Buf = HeapAlloc(ModuleHeap, 0, *BufLen); BYTE *p = HeapAlloc(ModuleHeap, 0, *BufLen);
if (!*Buf) if (!p)
return LOG(WINTUN_LOG_ERR, L"Out of memory"), ERROR_OUTOFMEMORY; return LOG(WINTUN_LOG_ERR, L"Out of memory"), ERROR_OUTOFMEMORY;
if (SetupDiGetDeviceRegistryPropertyW(DevInfo, DevInfoData, Property, ValueType, *Buf, *BufLen, BufLen)) if (SetupDiGetDeviceRegistryPropertyW(DevInfo, DevInfoData, Property, ValueType, p, *BufLen, BufLen))
{
*Buf = p;
return ERROR_SUCCESS; return ERROR_SUCCESS;
}
DWORD Result = GetLastError(); DWORD Result = GetLastError();
HeapFree(ModuleHeap, 0, *Buf); HeapFree(ModuleHeap, 0, p);
if (Result != ERROR_INSUFFICIENT_BUFFER) if (Result != ERROR_INSUFFICIENT_BUFFER)
return LOG_ERROR(L"Querying property failed", Result); return LOG_ERROR(L"Querying property failed", Result);
} }
@ -506,8 +512,8 @@ CreateAdapterData(
if (Key == INVALID_HANDLE_VALUE) if (Key == INVALID_HANDLE_VALUE)
return LOG_LAST_ERROR(L"Opening device registry key failed"); return LOG_LAST_ERROR(L"Opening device registry key failed");
*Adapter = HeapAlloc(ModuleHeap, 0, sizeof(WINTUN_ADAPTER)); WINTUN_ADAPTER *a = HeapAlloc(ModuleHeap, 0, sizeof(WINTUN_ADAPTER));
if (!*Adapter) if (!a)
{ {
LOG(WINTUN_LOG_ERR, L"Out of memory"); LOG(WINTUN_LOG_ERR, L"Out of memory");
Result = ERROR_OUTOFMEMORY; Result = ERROR_OUTOFMEMORY;
@ -522,7 +528,7 @@ CreateAdapterData(
LOG(WINTUN_LOG_ERR, L"Failed to query NetCfgInstanceId value"); LOG(WINTUN_LOG_ERR, L"Failed to query NetCfgInstanceId value");
goto cleanupAdapter; goto cleanupAdapter;
} }
if (FAILED(CLSIDFromString(ValueStr, &(*Adapter)->CfgInstanceID))) if (FAILED(CLSIDFromString(ValueStr, &a->CfgInstanceID)))
{ {
LOG(WINTUN_LOG_ERR, L"NetCfgInstanceId is not a GUID"); LOG(WINTUN_LOG_ERR, L"NetCfgInstanceId is not a GUID");
HeapFree(ModuleHeap, 0, ValueStr); HeapFree(ModuleHeap, 0, ValueStr);
@ -532,7 +538,7 @@ CreateAdapterData(
HeapFree(ModuleHeap, 0, ValueStr); HeapFree(ModuleHeap, 0, ValueStr);
/* Read the NetLuidIndex value. */ /* Read the NetLuidIndex value. */
Result = RegistryQueryDWORD(Key, L"NetLuidIndex", &(*Adapter)->LuidIndex, TRUE); Result = RegistryQueryDWORD(Key, L"NetLuidIndex", &a->LuidIndex, TRUE);
if (Result != ERROR_SUCCESS) if (Result != ERROR_SUCCESS)
{ {
LOG(WINTUN_LOG_ERR, L"Failed to query NetLuidIndex value"); LOG(WINTUN_LOG_ERR, L"Failed to query NetLuidIndex value");
@ -540,7 +546,7 @@ CreateAdapterData(
} }
/* Read the NetLuidIndex value. */ /* Read the NetLuidIndex value. */
Result = RegistryQueryDWORD(Key, L"*IfType", &(*Adapter)->IfType, TRUE); Result = RegistryQueryDWORD(Key, L"*IfType", &a->IfType, TRUE);
if (Result != ERROR_SUCCESS) if (Result != ERROR_SUCCESS)
{ {
LOG(WINTUN_LOG_ERR, L"Failed to query *IfType value"); LOG(WINTUN_LOG_ERR, L"Failed to query *IfType value");
@ -548,24 +554,24 @@ CreateAdapterData(
} }
DWORD Size; DWORD Size;
if (!SetupDiGetDeviceInstanceIdW( if (!SetupDiGetDeviceInstanceIdW(DevInfo, DevInfoData, a->DevInstanceID, _countof(a->DevInstanceID), &Size))
DevInfo, DevInfoData, (*Adapter)->DevInstanceID, _countof((*Adapter)->DevInstanceID), &Size))
{ {
Result = LOG_LAST_ERROR(L"Failed to get device instance ID"); Result = LOG_LAST_ERROR(L"Failed to get device instance ID");
goto cleanupAdapter; goto cleanupAdapter;
} }
if (wcsncpy_s((*Adapter)->Pool, _countof((*Adapter)->Pool), Pool, _TRUNCATE) == STRUNCATE) if (wcsncpy_s(a->Pool, _countof(a->Pool), Pool, _TRUNCATE) == STRUNCATE)
{ {
LOG(WINTUN_LOG_ERR, L"Pool name too long"); LOG(WINTUN_LOG_ERR, L"Pool name too long");
Result = ERROR_INVALID_PARAMETER; Result = ERROR_INVALID_PARAMETER;
goto cleanupAdapter; goto cleanupAdapter;
} }
*Adapter = a;
Result = ERROR_SUCCESS; Result = ERROR_SUCCESS;
cleanupAdapter: cleanupAdapter:
if (Result != ERROR_SUCCESS) if (Result != ERROR_SUCCESS)
HeapFree(ModuleHeap, 0, *Adapter); HeapFree(ModuleHeap, 0, a);
cleanupKey: cleanupKey:
RegCloseKey(Key); RegCloseKey(Key);
return Result; return Result;
@ -884,7 +890,6 @@ cleanupQueriedStore:
return Result; return Result;
} }
static BOOL static BOOL
IsOurDrvInfoDetail(_In_ const SP_DRVINFO_DETAIL_DATA_W *DrvInfoDetailData) IsOurDrvInfoDetail(_In_ const SP_DRVINFO_DETAIL_DATA_W *DrvInfoDetailData)
{ {
@ -1167,7 +1172,8 @@ CreateAdapter(
goto cleanupNetDevRegKey; goto cleanupNetDevRegKey;
} }
Result = CreateAdapterData(Pool, DevInfo, &DevInfoData, Adapter); WINTUN_ADAPTER *a;
Result = CreateAdapterData(Pool, DevInfo, &DevInfoData, &a);
if (Result != ERROR_SUCCESS) if (Result != ERROR_SUCCESS)
{ {
LOG(WINTUN_LOG_ERR, L"Failed to create adapter data"); LOG(WINTUN_LOG_ERR, L"Failed to create adapter data");
@ -1176,7 +1182,7 @@ CreateAdapter(
HKEY TcpipAdapterRegKey; HKEY TcpipAdapterRegKey;
WCHAR TcpipAdapterRegPath[MAX_REG_PATH]; WCHAR TcpipAdapterRegPath[MAX_REG_PATH];
Result = GetTcpipAdapterRegPath(*Adapter, TcpipAdapterRegPath); Result = GetTcpipAdapterRegPath(a, TcpipAdapterRegPath);
if (Result != ERROR_SUCCESS) if (Result != ERROR_SUCCESS)
goto cleanupAdapter; goto cleanupAdapter;
Result = RegistryOpenKeyWait( Result = RegistryOpenKeyWait(
@ -1200,7 +1206,7 @@ CreateAdapter(
HKEY TcpipInterfaceRegKey; HKEY TcpipInterfaceRegKey;
WCHAR TcpipInterfaceRegPath[MAX_REG_PATH]; WCHAR TcpipInterfaceRegPath[MAX_REG_PATH];
Result = GetTcpipInterfaceRegPath(*Adapter, TcpipInterfaceRegPath); Result = GetTcpipInterfaceRegPath(a, TcpipInterfaceRegPath);
if (Result != ERROR_SUCCESS) if (Result != ERROR_SUCCESS)
{ {
LOG(WINTUN_LOG_ERR, L"Failed to determine interface-specific TCP/IP network registry key path"); LOG(WINTUN_LOG_ERR, L"Failed to determine interface-specific TCP/IP network registry key path");
@ -1224,15 +1230,17 @@ CreateAdapter(
if (Result != ERROR_SUCCESS) if (Result != ERROR_SUCCESS)
LOG_ERROR(L"Failed to set EnableDeadGWDetect", Result); LOG_ERROR(L"Failed to set EnableDeadGWDetect", Result);
Result = WintunSetAdapterName(*Adapter, Name); Result = WintunSetAdapterName(a, Name);
if (Result != ERROR_SUCCESS) if (Result == ERROR_SUCCESS)
*Adapter = a;
else
LOG_ERROR(L"Failed to set adapter name", Result); LOG_ERROR(L"Failed to set adapter name", Result);
RegCloseKey(TcpipInterfaceRegKey); RegCloseKey(TcpipInterfaceRegKey);
cleanupTcpipAdapterRegKey: cleanupTcpipAdapterRegKey:
RegCloseKey(TcpipAdapterRegKey); RegCloseKey(TcpipAdapterRegKey);
cleanupAdapter: cleanupAdapter:
if (Result != ERROR_SUCCESS) if (Result != ERROR_SUCCESS)
HeapFree(ModuleHeap, 0, *Adapter); HeapFree(ModuleHeap, 0, a);
cleanupNetDevRegKey: cleanupNetDevRegKey:
RegCloseKey(NetDevRegKey); RegCloseKey(NetDevRegKey);
cleanupDevice: cleanupDevice:

View File

@ -181,13 +181,16 @@ RegistryQuery(
{ {
for (;;) for (;;)
{ {
*Buf = HeapAlloc(ModuleHeap, 0, *BufLen); BYTE *p = HeapAlloc(ModuleHeap, 0, *BufLen);
if (!*Buf) if (!p)
return LOG(WINTUN_LOG_ERR, L"Out of memory"), ERROR_OUTOFMEMORY; return LOG(WINTUN_LOG_ERR, L"Out of memory"), ERROR_OUTOFMEMORY;
LSTATUS Result = RegQueryValueExW(Key, Name, NULL, ValueType, (BYTE *)*Buf, BufLen); LSTATUS Result = RegQueryValueExW(Key, Name, NULL, ValueType, p, BufLen);
if (Result == ERROR_SUCCESS) if (Result == ERROR_SUCCESS)
{
*Buf = p;
return ERROR_SUCCESS; return ERROR_SUCCESS;
HeapFree(ModuleHeap, 0, *Buf); }
HeapFree(ModuleHeap, 0, p);
if (Result != ERROR_MORE_DATA) if (Result != ERROR_MORE_DATA)
return Log ? LOG_ERROR(L"Querying value failed", Result) : Result; return Log ? LOG_ERROR(L"Querying value failed", Result) : Result;
} }

View File

@ -67,8 +67,8 @@ typedef struct _TUN_SESSION
WINTUN_STATUS WINAPI WINTUN_STATUS WINAPI
WintunStartSession(_In_ const WINTUN_ADAPTER *Adapter, _In_ DWORD Capacity, _Out_ TUN_SESSION **Session) WintunStartSession(_In_ const WINTUN_ADAPTER *Adapter, _In_ DWORD Capacity, _Out_ TUN_SESSION **Session)
{ {
*Session = HeapAlloc(ModuleHeap, HEAP_ZERO_MEMORY, sizeof(TUN_SESSION)); TUN_SESSION *s = HeapAlloc(ModuleHeap, HEAP_ZERO_MEMORY, sizeof(TUN_SESSION));
if (!*Session) if (!s)
return LOG(WINTUN_LOG_ERR, L"Out of memory"), ERROR_OUTOFMEMORY; return LOG(WINTUN_LOG_ERR, L"Out of memory"), ERROR_OUTOFMEMORY;
const ULONG RingSize = TUN_RING_SIZE(Capacity); const ULONG RingSize = TUN_RING_SIZE(Capacity);
DWORD Result; DWORD Result;
@ -84,25 +84,25 @@ WintunStartSession(_In_ const WINTUN_ADAPTER *Adapter, _In_ DWORD Capacity, _Out
Result = ERROR_ACCESS_DENIED; Result = ERROR_ACCESS_DENIED;
goto cleanupAllocatedRegion; goto cleanupAllocatedRegion;
} }
(*Session)->Descriptor.Send.RingSize = RingSize; s->Descriptor.Send.RingSize = RingSize;
(*Session)->Descriptor.Send.Ring = (TUN_RING *)AllocatedRegion; s->Descriptor.Send.Ring = (TUN_RING *)AllocatedRegion;
(*Session)->Descriptor.Send.TailMoved = CreateEventW(&SecurityAttributes, FALSE, FALSE, NULL); s->Descriptor.Send.TailMoved = CreateEventW(&SecurityAttributes, FALSE, FALSE, NULL);
if (!(*Session)->Descriptor.Send.TailMoved) if (!s->Descriptor.Send.TailMoved)
{ {
Result = LOG_LAST_ERROR(L"Failed to create send event"); Result = LOG_LAST_ERROR(L"Failed to create send event");
goto cleanupToken; goto cleanupToken;
} }
(*Session)->Descriptor.Receive.RingSize = RingSize; s->Descriptor.Receive.RingSize = RingSize;
(*Session)->Descriptor.Receive.Ring = (TUN_RING *)(AllocatedRegion + RingSize); s->Descriptor.Receive.Ring = (TUN_RING *)(AllocatedRegion + RingSize);
(*Session)->Descriptor.Receive.TailMoved = CreateEvent(&SecurityAttributes, FALSE, FALSE, NULL); s->Descriptor.Receive.TailMoved = CreateEventW(&SecurityAttributes, FALSE, FALSE, NULL);
if (!(*Session)->Descriptor.Receive.TailMoved) if (!s->Descriptor.Receive.TailMoved)
{ {
Result = LOG_LAST_ERROR(L"Failed to create receive event"); Result = LOG_LAST_ERROR(L"Failed to create receive event");
goto cleanupSendTailMoved; goto cleanupSendTailMoved;
} }
Result = WintunGetAdapterDeviceObject(Adapter, &(*Session)->Handle); Result = WintunGetAdapterDeviceObject(Adapter, &s->Handle);
if (Result != ERROR_SUCCESS) if (Result != ERROR_SUCCESS)
{ {
LOG(WINTUN_LOG_ERR, L"Failed to open adapter device object"); LOG(WINTUN_LOG_ERR, L"Failed to open adapter device object");
@ -110,9 +110,9 @@ WintunStartSession(_In_ const WINTUN_ADAPTER *Adapter, _In_ DWORD Capacity, _Out
} }
DWORD BytesReturned; DWORD BytesReturned;
if (!DeviceIoControl( if (!DeviceIoControl(
(*Session)->Handle, s->Handle,
TUN_IOCTL_REGISTER_RINGS, TUN_IOCTL_REGISTER_RINGS,
&(*Session)->Descriptor, &s->Descriptor,
sizeof(TUN_REGISTER_RINGS), sizeof(TUN_REGISTER_RINGS),
NULL, NULL,
0, 0,
@ -123,23 +123,23 @@ WintunStartSession(_In_ const WINTUN_ADAPTER *Adapter, _In_ DWORD Capacity, _Out
goto cleanupHandle; goto cleanupHandle;
} }
RevertToSelf(); RevertToSelf();
(*Session)->Capacity = Capacity; s->Capacity = Capacity;
(void)InitializeCriticalSectionAndSpinCount(&(*Session)->Receive.Lock, LOCK_SPIN_COUNT); (void)InitializeCriticalSectionAndSpinCount(&s->Receive.Lock, LOCK_SPIN_COUNT);
(void)InitializeCriticalSectionAndSpinCount(&(*Session)->Send.Lock, LOCK_SPIN_COUNT); (void)InitializeCriticalSectionAndSpinCount(&s->Send.Lock, LOCK_SPIN_COUNT);
*Session = s;
return ERROR_SUCCESS; return ERROR_SUCCESS;
cleanupHandle: cleanupHandle:
CloseHandle((*Session)->Handle); CloseHandle(s->Handle);
cleanupReceiveTailMoved: cleanupReceiveTailMoved:
CloseHandle((*Session)->Descriptor.Receive.TailMoved); CloseHandle(s->Descriptor.Receive.TailMoved);
cleanupSendTailMoved: cleanupSendTailMoved:
CloseHandle((*Session)->Descriptor.Send.TailMoved); CloseHandle(s->Descriptor.Send.TailMoved);
cleanupToken: cleanupToken:
RevertToSelf(); RevertToSelf();
cleanupAllocatedRegion: cleanupAllocatedRegion:
VirtualFree(AllocatedRegion, 0, MEM_RELEASE); VirtualFree(AllocatedRegion, 0, MEM_RELEASE);
cleanupRings: cleanupRings:
HeapFree(ModuleHeap, 0, *Session); HeapFree(ModuleHeap, 0, s);
*Session = NULL;
return Result; return Result;
} }