Milestone 5: deliver embedded RDP sessions and lifecycle hardening

This commit is contained in:
Keith Smith
2026-03-03 18:59:26 -07:00
parent 230a401386
commit 36006bd4aa
2941 changed files with 724359 additions and 77 deletions

View File

@@ -0,0 +1,22 @@
# WinPR: Windows Portable Runtime
# libwinpr-io cmake build script
#
# Copyright 2012 Marc-Andre Moreau <marcandre.moreau@gmail.com>
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
winpr_module_add(device.c io.c io.h)
if(BUILD_TESTING_INTERNAL OR BUILD_TESTING)
add_subdirectory(test)
endif()

View File

@@ -0,0 +1,9 @@
set(MINWIN_LAYER "1")
set(MINWIN_GROUP "core")
set(MINWIN_MAJOR_VERSION "1")
set(MINWIN_MINOR_VERSION "1")
set(MINWIN_SHORT_NAME "io")
set(MINWIN_LONG_NAME "Asynchronous I/O Functions")
set(MODULE_LIBRARY_NAME
"api-ms-win-${MINWIN_GROUP}-${MINWIN_SHORT_NAME}-l${MINWIN_LAYER}-${MINWIN_MAJOR_VERSION}-${MINWIN_MINOR_VERSION}"
)

View File

@@ -0,0 +1,235 @@
/**
* WinPR: Windows Portable Runtime
* Asynchronous I/O Functions
*
* Copyright 2012 Marc-Andre Moreau <marcandre.moreau@gmail.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <winpr/config.h>
#include <winpr/io.h>
#ifndef _WIN32
#include "io.h"
#include <stdio.h>
#include <fcntl.h>
#include <stdlib.h>
#include <errno.h>
#ifdef WINPR_HAVE_UNISTD_H
#include <unistd.h>
#endif
#include <sys/types.h>
#include <sys/stat.h>
#include <winpr/crt.h>
#include <winpr/path.h>
#include <winpr/file.h>
/**
* I/O Manager Routines
* http://msdn.microsoft.com/en-us/library/windows/hardware/ff551797/
*
* These routines are only accessible to kernel drivers, but we need
* similar functionality in WinPR in user space.
*
* This is a best effort non-conflicting port of this API meant for
* non-Windows, WinPR usage only.
*
* References:
*
* Device Objects and Device Stacks:
* http://msdn.microsoft.com/en-us/library/windows/hardware/ff543153/
*
* Driver Development Part 1: Introduction to Drivers:
* http://www.codeproject.com/Articles/9504/Driver-Development-Part-1-Introduction-to-Drivers/
*/
#define DEVICE_FILE_PREFIX_PATH "\\Device\\"
static char* GetDeviceFileNameWithoutPrefixA(LPCSTR lpName)
{
char* lpFileName = nullptr;
if (!lpName)
return nullptr;
if (strncmp(lpName, DEVICE_FILE_PREFIX_PATH, sizeof(DEVICE_FILE_PREFIX_PATH) - 1) != 0)
return nullptr;
lpFileName =
_strdup(&lpName[strnlen(DEVICE_FILE_PREFIX_PATH, sizeof(DEVICE_FILE_PREFIX_PATH))]);
return lpFileName;
}
static char* GetDeviceFileUnixDomainSocketBaseFilePathA(void)
{
char* lpTempPath = nullptr;
char* lpPipePath = nullptr;
lpTempPath = GetKnownPath(KNOWN_PATH_TEMP);
if (!lpTempPath)
return nullptr;
lpPipePath = GetCombinedPath(lpTempPath, ".device");
free(lpTempPath);
return lpPipePath;
}
static char* GetDeviceFileUnixDomainSocketFilePathA(LPCSTR lpName)
{
char* lpPipePath = nullptr;
char* lpFileName = nullptr;
char* lpFilePath = nullptr;
lpPipePath = GetDeviceFileUnixDomainSocketBaseFilePathA();
if (!lpPipePath)
return nullptr;
lpFileName = GetDeviceFileNameWithoutPrefixA(lpName);
if (!lpFileName)
{
free(lpPipePath);
return nullptr;
}
lpFilePath = GetCombinedPath(lpPipePath, lpFileName);
free(lpPipePath);
free(lpFileName);
return lpFilePath;
}
/**
* IoCreateDevice:
* http://msdn.microsoft.com/en-us/library/windows/hardware/ff548397/
*/
NTSTATUS _IoCreateDeviceEx(WINPR_ATTR_UNUSED PDRIVER_OBJECT_EX DriverObject,
WINPR_ATTR_UNUSED ULONG DeviceExtensionSize, PUNICODE_STRING DeviceName,
WINPR_ATTR_UNUSED DEVICE_TYPE DeviceType,
WINPR_ATTR_UNUSED ULONG DeviceCharacteristics,
WINPR_ATTR_UNUSED BOOLEAN Exclusive, PDEVICE_OBJECT_EX* DeviceObject)
{
int status = 0;
char* DeviceBasePath = nullptr;
DEVICE_OBJECT_EX* pDeviceObjectEx = nullptr;
DeviceBasePath = GetDeviceFileUnixDomainSocketBaseFilePathA();
if (!DeviceBasePath)
return STATUS_NO_MEMORY;
if (!winpr_PathFileExists(DeviceBasePath))
{
if (mkdir(DeviceBasePath, S_IRUSR | S_IWUSR | S_IXUSR) != 0)
{
free(DeviceBasePath);
return STATUS_ACCESS_DENIED;
}
}
free(DeviceBasePath);
pDeviceObjectEx = (DEVICE_OBJECT_EX*)calloc(1, sizeof(DEVICE_OBJECT_EX));
if (!pDeviceObjectEx)
return STATUS_NO_MEMORY;
pDeviceObjectEx->DeviceName =
ConvertWCharNToUtf8Alloc(DeviceName->Buffer, DeviceName->Length / sizeof(WCHAR), nullptr);
if (!pDeviceObjectEx->DeviceName)
{
free(pDeviceObjectEx);
return STATUS_NO_MEMORY;
}
pDeviceObjectEx->DeviceFileName =
GetDeviceFileUnixDomainSocketFilePathA(pDeviceObjectEx->DeviceName);
if (!pDeviceObjectEx->DeviceFileName)
{
free(pDeviceObjectEx->DeviceName);
free(pDeviceObjectEx);
return STATUS_NO_MEMORY;
}
if (winpr_PathFileExists(pDeviceObjectEx->DeviceFileName))
{
if (unlink(pDeviceObjectEx->DeviceFileName) == -1)
{
free(pDeviceObjectEx->DeviceName);
free(pDeviceObjectEx->DeviceFileName);
free(pDeviceObjectEx);
return STATUS_ACCESS_DENIED;
}
}
status = mkfifo(pDeviceObjectEx->DeviceFileName, 0666);
if (status != 0)
{
free(pDeviceObjectEx->DeviceName);
free(pDeviceObjectEx->DeviceFileName);
free(pDeviceObjectEx);
switch (errno)
{
case EACCES:
return STATUS_ACCESS_DENIED;
case EEXIST:
return STATUS_OBJECT_NAME_EXISTS;
case ENAMETOOLONG:
return STATUS_NAME_TOO_LONG;
case ENOENT:
case ENOTDIR:
return STATUS_NOT_A_DIRECTORY;
case ENOSPC:
return STATUS_DISK_FULL;
default:
return STATUS_INTERNAL_ERROR;
}
}
*((ULONG_PTR*)(DeviceObject)) = (ULONG_PTR)pDeviceObjectEx;
return STATUS_SUCCESS;
}
/**
* IoDeleteDevice:
* http://msdn.microsoft.com/en-us/library/windows/hardware/ff549083/
*/
VOID _IoDeleteDeviceEx(PDEVICE_OBJECT_EX DeviceObject)
{
DEVICE_OBJECT_EX* pDeviceObjectEx = nullptr;
pDeviceObjectEx = (DEVICE_OBJECT_EX*)DeviceObject;
if (!pDeviceObjectEx)
return;
unlink(pDeviceObjectEx->DeviceFileName);
free(pDeviceObjectEx->DeviceName);
free(pDeviceObjectEx->DeviceFileName);
free(pDeviceObjectEx);
}
#endif

View File

@@ -0,0 +1,220 @@
/**
* WinPR: Windows Portable Runtime
* Asynchronous I/O Functions
*
* Copyright 2012 Marc-Andre Moreau <marcandre.moreau@gmail.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <winpr/config.h>
#include <winpr/io.h>
#ifndef _WIN32
#ifdef WINPR_HAVE_UNISTD_H
#include <unistd.h>
#endif
#include <time.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <dirent.h>
#include <fcntl.h>
#include <sys/un.h>
#include <sys/stat.h>
#include <sys/socket.h>
#include <winpr/crt.h>
#include <winpr/wlog.h>
#include "../handle/handle.h"
#include "../pipe/pipe.h"
#include "../log.h"
#define TAG WINPR_TAG("io")
BOOL GetOverlappedResult(WINPR_ATTR_UNUSED HANDLE hFile,
WINPR_ATTR_UNUSED LPOVERLAPPED lpOverlapped,
WINPR_ATTR_UNUSED LPDWORD lpNumberOfBytesTransferred,
WINPR_ATTR_UNUSED BOOL bWait)
{
WLog_ERR(TAG, "Not implemented");
SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
return FALSE;
}
BOOL GetOverlappedResultEx(WINPR_ATTR_UNUSED HANDLE hFile,
WINPR_ATTR_UNUSED LPOVERLAPPED lpOverlapped,
WINPR_ATTR_UNUSED LPDWORD lpNumberOfBytesTransferred,
WINPR_ATTR_UNUSED DWORD dwMilliseconds,
WINPR_ATTR_UNUSED BOOL bAlertable)
{
WLog_ERR(TAG, "Not implemented");
SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
return FALSE;
}
BOOL DeviceIoControl(WINPR_ATTR_UNUSED HANDLE hDevice, WINPR_ATTR_UNUSED DWORD dwIoControlCode,
WINPR_ATTR_UNUSED LPVOID lpInBuffer, WINPR_ATTR_UNUSED DWORD nInBufferSize,
WINPR_ATTR_UNUSED LPVOID lpOutBuffer, WINPR_ATTR_UNUSED DWORD nOutBufferSize,
WINPR_ATTR_UNUSED LPDWORD lpBytesReturned,
WINPR_ATTR_UNUSED LPOVERLAPPED lpOverlapped)
{
WLog_ERR(TAG, "Not implemented");
SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
return FALSE;
}
HANDLE CreateIoCompletionPort(WINPR_ATTR_UNUSED HANDLE FileHandle,
WINPR_ATTR_UNUSED HANDLE ExistingCompletionPort,
WINPR_ATTR_UNUSED ULONG_PTR CompletionKey,
WINPR_ATTR_UNUSED DWORD NumberOfConcurrentThreads)
{
WLog_ERR(TAG, "Not implemented");
SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
return nullptr;
}
BOOL GetQueuedCompletionStatus(WINPR_ATTR_UNUSED HANDLE CompletionPort,
WINPR_ATTR_UNUSED LPDWORD lpNumberOfBytesTransferred,
WINPR_ATTR_UNUSED PULONG_PTR lpCompletionKey,
WINPR_ATTR_UNUSED LPOVERLAPPED* lpOverlapped,
WINPR_ATTR_UNUSED DWORD dwMilliseconds)
{
WLog_ERR(TAG, "Not implemented");
SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
return FALSE;
}
BOOL GetQueuedCompletionStatusEx(WINPR_ATTR_UNUSED HANDLE CompletionPort,
WINPR_ATTR_UNUSED LPOVERLAPPED_ENTRY lpCompletionPortEntries,
WINPR_ATTR_UNUSED ULONG ulCount,
WINPR_ATTR_UNUSED PULONG ulNumEntriesRemoved,
WINPR_ATTR_UNUSED DWORD dwMilliseconds,
WINPR_ATTR_UNUSED BOOL fAlertable)
{
WLog_ERR(TAG, "Not implemented");
SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
return FALSE;
}
BOOL PostQueuedCompletionStatus(WINPR_ATTR_UNUSED HANDLE CompletionPort,
WINPR_ATTR_UNUSED DWORD dwNumberOfBytesTransferred,
WINPR_ATTR_UNUSED ULONG_PTR dwCompletionKey,
WINPR_ATTR_UNUSED LPOVERLAPPED lpOverlapped)
{
WLog_ERR(TAG, "Not implemented");
SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
return FALSE;
}
BOOL CancelIo(WINPR_ATTR_UNUSED HANDLE hFile)
{
WLog_ERR(TAG, "Not implemented");
SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
return FALSE;
}
BOOL CancelIoEx(WINPR_ATTR_UNUSED HANDLE hFile, WINPR_ATTR_UNUSED LPOVERLAPPED lpOverlapped)
{
WLog_ERR(TAG, "Not implemented");
SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
return FALSE;
}
BOOL CancelSynchronousIo(WINPR_ATTR_UNUSED HANDLE hThread)
{
WLog_ERR(TAG, "Not implemented");
SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
return FALSE;
}
#endif
#ifdef _UWP
#include <winpr/crt.h>
#include <winpr/wlog.h>
#include "../log.h"
#define TAG WINPR_TAG("io")
BOOL GetOverlappedResult(HANDLE hFile, LPOVERLAPPED lpOverlapped,
LPDWORD lpNumberOfBytesTransferred, BOOL bWait)
{
return GetOverlappedResultEx(hFile, lpOverlapped, lpNumberOfBytesTransferred,
bWait ? INFINITE : 0, TRUE);
}
BOOL DeviceIoControl(HANDLE hDevice, DWORD dwIoControlCode, LPVOID lpInBuffer, DWORD nInBufferSize,
LPVOID lpOutBuffer, DWORD nOutBufferSize, LPDWORD lpBytesReturned,
LPOVERLAPPED lpOverlapped)
{
WLog_ERR(TAG, "Not implemented");
SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
return FALSE;
}
HANDLE CreateIoCompletionPort(HANDLE FileHandle, HANDLE ExistingCompletionPort,
ULONG_PTR CompletionKey, DWORD NumberOfConcurrentThreads)
{
WLog_ERR(TAG, "Not implemented");
SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
return nullptr;
}
BOOL GetQueuedCompletionStatus(HANDLE CompletionPort, LPDWORD lpNumberOfBytesTransferred,
PULONG_PTR lpCompletionKey, LPOVERLAPPED* lpOverlapped,
DWORD dwMilliseconds)
{
WLog_ERR(TAG, "Not implemented");
SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
return FALSE;
}
BOOL GetQueuedCompletionStatusEx(HANDLE CompletionPort, LPOVERLAPPED_ENTRY lpCompletionPortEntries,
ULONG ulCount, PULONG ulNumEntriesRemoved, DWORD dwMilliseconds,
BOOL fAlertable)
{
WLog_ERR(TAG, "Not implemented");
SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
return FALSE;
}
BOOL PostQueuedCompletionStatus(HANDLE CompletionPort, DWORD dwNumberOfBytesTransferred,
ULONG_PTR dwCompletionKey, LPOVERLAPPED lpOverlapped)
{
WLog_ERR(TAG, "Not implemented");
SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
return FALSE;
}
BOOL CancelIo(HANDLE hFile)
{
return CancelIoEx(hFile, nullptr);
}
BOOL CancelSynchronousIo(HANDLE hThread)
{
WLog_ERR(TAG, "Not implemented");
SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
return FALSE;
}
#endif

View File

@@ -0,0 +1,37 @@
/**
* WinPR: Windows Portable Runtime
* Asynchronous I/O Functions
*
* Copyright 2012 Marc-Andre Moreau <marcandre.moreau@gmail.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef WINPR_IO_PRIVATE_H
#define WINPR_IO_PRIVATE_H
#ifndef _WIN32
#include <winpr/io.h>
#include "../handle/handle.h"
typedef struct
{
char* DeviceName;
char* DeviceFileName;
} DEVICE_OBJECT_EX;
#endif
#endif /* WINPR_IO_PRIVATE_H */

View File

@@ -0,0 +1,23 @@
set(MODULE_NAME "TestIo")
set(MODULE_PREFIX "TEST_IO")
disable_warnings_for_directory(${CMAKE_CURRENT_BINARY_DIR})
set(${MODULE_PREFIX}_DRIVER ${MODULE_NAME}.c)
set(${MODULE_PREFIX}_TESTS TestIoGetOverlappedResult.c)
create_test_sourcelist(${MODULE_PREFIX}_SRCS ${${MODULE_PREFIX}_DRIVER} ${${MODULE_PREFIX}_TESTS})
add_executable(${MODULE_NAME} ${${MODULE_PREFIX}_SRCS})
target_link_libraries(${MODULE_NAME} winpr)
set_target_properties(${MODULE_NAME} PROPERTIES RUNTIME_OUTPUT_DIRECTORY "${TESTING_OUTPUT_DIRECTORY}")
foreach(test ${${MODULE_PREFIX}_TESTS})
get_filename_component(TestName ${test} NAME_WE)
add_test(${TestName} ${TESTING_OUTPUT_DIRECTORY}/${MODULE_NAME} ${TestName})
endforeach()
set_property(TARGET ${MODULE_NAME} PROPERTY FOLDER "WinPR/Test")

View File

@@ -0,0 +1,10 @@
#include <stdio.h>
#include <winpr/io.h>
#include <winpr/crt.h>
#include <winpr/windows.h>
int TestIoGetOverlappedResult(int argc, char* argv[])
{
return 0;
}