From f1dbfe2b7449b74c8c8dcaf573b385b3d822e095 Mon Sep 17 00:00:00 2001 From: Bill Zissimopoulos Date: Mon, 30 Nov 2015 17:30:00 -0800 Subject: [PATCH] dll: fsctl: testing --- src/dll/fsctl.c | 78 ++++++++++++++++++++++++----------- tst/winfsp-tests/mount-test.c | 20 ++++++++- 2 files changed, 73 insertions(+), 25 deletions(-) diff --git a/src/dll/fsctl.c b/src/dll/fsctl.c index af0857b4..8584c259 100644 --- a/src/dll/fsctl.c +++ b/src/dll/fsctl.c @@ -7,13 +7,16 @@ #include #include #include +#if !defined(NDEBUG) +#include +#endif #define GLOBALROOT L"\\\\?\\GLOBALROOT" static inline VOID GlobalDevicePath(PWCHAR DevicePathBuf, SIZE_T DevicePathSize, PWSTR DevicePath) { - StringCbPrintf(DevicePathBuf, DevicePathSize, - L'\\' == DevicePath[0] ? GLOBALROOT "%S" : GLOBALROOT "\\Device\\%S", DevicePath); + StringCbPrintfW(DevicePathBuf, DevicePathSize, + L'\\' == DevicePath[0] ? GLOBALROOT "%s" : GLOBALROOT "\\Device\\%s", DevicePath); } FSP_API NTSTATUS FspFsctlCreateVolume(PWSTR DevicePath, @@ -21,13 +24,13 @@ FSP_API NTSTATUS FspFsctlCreateVolume(PWSTR DevicePath, PWCHAR VolumePathBuf, SIZE_T VolumePathSize) { NTSTATUS Result = STATUS_SUCCESS; - WCHAR DevicePathBuf[MAX_PATH]; - FSP_FSCTL_VOLUME_PARAMS *ParamsBuf; - HANDLE Token; - PVOID DaclBuf = 0; + FSP_FSCTL_VOLUME_PARAMS *ParamsBuf = 0; + HANDLE Token = 0; + PTOKEN_DEFAULT_DACL DefaultDacl = 0; SECURITY_DESCRIPTOR SecurityDescriptorStruct; - PSECURITY_DESCRIPTOR SecurityDescriptorBuf = 0; + PSECURITY_DESCRIPTOR SecurityDescriptorBuf; DWORD SecurityDescriptorSize, Bytes; + WCHAR DevicePathBuf[MAX_PATH]; HANDLE DeviceHandle = INVALID_HANDLE_VALUE; VolumePathBuf[0] = L'\0'; @@ -39,30 +42,50 @@ FSP_API NTSTATUS FspFsctlCreateVolume(PWSTR DevicePath, Result = FspNtStatusFromWin32(GetLastError()); goto exit; } - GetTokenInformation(Token, TokenDefaultDacl, 0, 0, &Bytes); - DaclBuf = malloc(Bytes); - if (0 == DaclBuf) + Bytes = 0; + if (!GetTokenInformation(Token, TokenDefaultDacl, 0, 0, &Bytes) && + ERROR_INSUFFICIENT_BUFFER != GetLastError()) + { + Result = FspNtStatusFromWin32(GetLastError()); + goto exit; + } + DefaultDacl = malloc(Bytes); + if (0 == DefaultDacl) { - CloseHandle(Token); Result = STATUS_INSUFFICIENT_RESOURCES; goto exit; } - if (GetTokenInformation(Token, TokenDefaultDacl, DaclBuf, Bytes, &Bytes) && - InitializeSecurityDescriptor(&SecurityDescriptorStruct, SECURITY_DESCRIPTOR_REVISION) && - SetSecurityDescriptorDacl(&SecurityDescriptorStruct, TRUE, DaclBuf, FALSE)) - { - SecurityDescriptor = &SecurityDescriptorStruct; - CloseHandle(Token); - } - else + if (!GetTokenInformation(Token, TokenDefaultDacl, DefaultDacl, Bytes, &Bytes) || + !InitializeSecurityDescriptor(&SecurityDescriptorStruct, SECURITY_DESCRIPTOR_REVISION) || + !SetSecurityDescriptorDacl(&SecurityDescriptorStruct, TRUE, DefaultDacl->DefaultDacl, FALSE)) { Result = FspNtStatusFromWin32(GetLastError()); - CloseHandle(Token); goto exit; } + SecurityDescriptor = &SecurityDescriptorStruct; + CloseHandle(Token); + Token = 0; } - SecurityDescriptorSize = GetSecurityDescriptorLength(SecurityDescriptor); +#if !defined(NDEBUG) + { + PWSTR Sddl; + if (ConvertSecurityDescriptorToStringSecurityDescriptorW(SecurityDescriptor, + SDDL_REVISION_1, DACL_SECURITY_INFORMATION, &Sddl, 0)) + { + OutputDebugStringW(Sddl); + LocalFree(Sddl); + } + } +#endif + + SecurityDescriptorSize = 0; + if (!MakeSelfRelativeSD(SecurityDescriptor, 0, &SecurityDescriptorSize) && + ERROR_INSUFFICIENT_BUFFER != GetLastError()) + { + Result = FspNtStatusFromWin32(GetLastError()); + goto exit; + } ParamsBuf = malloc(sizeof *ParamsBuf + SecurityDescriptorSize); if (0 == ParamsBuf) { @@ -83,6 +106,8 @@ FSP_API NTSTATUS FspFsctlCreateVolume(PWSTR DevicePath, if (INVALID_HANDLE_VALUE == DeviceHandle) { Result = FspNtStatusFromWin32(GetLastError()); + if (STATUS_OBJECT_NAME_NOT_FOUND == Result) + Result = STATUS_NO_SUCH_DEVICE; goto exit; } @@ -95,10 +120,15 @@ FSP_API NTSTATUS FspFsctlCreateVolume(PWSTR DevicePath, } exit: + if (INVALID_HANDLE_VALUE != DeviceHandle) CloseHandle(DeviceHandle); - free(SecurityDescriptorBuf); - free(DaclBuf); + if (0 != Token) + CloseHandle(Token); + + free(ParamsBuf); + free(DefaultDacl); + return Result; } @@ -118,6 +148,8 @@ FSP_API NTSTATUS FspFsctlOpenVolume(PWSTR VolumePath, if (INVALID_HANDLE_VALUE == VolumeHandle) { Result = FspNtStatusFromWin32(GetLastError()); + if (STATUS_OBJECT_NAME_NOT_FOUND == Result) + Result = STATUS_NO_SUCH_DEVICE; goto exit; } diff --git a/tst/winfsp-tests/mount-test.c b/tst/winfsp-tests/mount-test.c index 95411e84..80f06f67 100644 --- a/tst/winfsp-tests/mount-test.c +++ b/tst/winfsp-tests/mount-test.c @@ -2,15 +2,30 @@ #include #include +void mount_invalid_test(void) +{ + NTSTATUS Result; + FSP_FSCTL_VOLUME_PARAMS Params = { 0 }; + WCHAR VolumePath[MAX_PATH]; + HANDLE VolumeHandle; + + Params.SectorSize = 16384; + Result = FspFsctlCreateVolume(L"WinFsp.DoesNotExist", &Params, 0, VolumePath, sizeof VolumePath); + ASSERT(STATUS_NO_SUCH_DEVICE == Result); + + Result = FspFsctlOpenVolume(L"\\Device\\Volume{31EF947D-B0F3-4A19-A4E7-BE0C1BE94886}.DoesNotExist", &VolumeHandle); + ASSERT(STATUS_NO_SUCH_DEVICE == Result); +} + void mount_dotest(PWSTR DeviceName) { NTSTATUS Result; BOOLEAN Success; - FSP_FSCTL_VOLUME_PARAMS Params; + FSP_FSCTL_VOLUME_PARAMS Params = { 0 }; WCHAR VolumePath[MAX_PATH]; HANDLE VolumeHandle; - Params.SectorSize = (UINT16)65536; + Params.SectorSize = 16384; Result = FspFsctlCreateVolume(DeviceName, &Params, 0, VolumePath, sizeof VolumePath); ASSERT(STATUS_SUCCESS == Result); @@ -32,5 +47,6 @@ void mount_test(void) void mount_tests(void) { + TEST(mount_invalid_test); TEST(mount_test); }