From 23390bf9118c72a1789b3d6a6185105c0d554b70 Mon Sep 17 00:00:00 2001 From: Pete Batard Date: Tue, 2 May 2017 00:56:07 +0100 Subject: [PATCH] [core] relax the FILE_SHARE_WRITE rules during enumeration * Should allow enumeration of drives, that other process have open with RW access, back again (looking at you, Paragon!!!) --- src/dev.c | 4 ++-- src/drive.c | 17 ++++++++--------- src/drive.h | 2 +- src/format.c | 4 ++-- src/rufus.rc | 10 +++++----- 5 files changed, 18 insertions(+), 19 deletions(-) diff --git a/src/dev.c b/src/dev.c index 44479437..3297c8c6 100644 --- a/src/dev.c +++ b/src/dev.c @@ -654,8 +654,8 @@ BOOL GetDevices(DWORD devnum) continue; } - hDrive = CreateFileA(devint_detail_data->DevicePath, GENERIC_READ|GENERIC_WRITE, FILE_SHARE_READ, - NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); + hDrive = CreateFileA(devint_detail_data->DevicePath, GENERIC_READ|GENERIC_WRITE, + FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); if(hDrive == INVALID_HANDLE_VALUE) { uprintf("Could not open '%s': %s\n", devint_detail_data->DevicePath, WindowsErrorString()); continue; diff --git a/src/drive.c b/src/drive.c index 6105c3d9..18c46154 100644 --- a/src/drive.c +++ b/src/drive.c @@ -138,7 +138,7 @@ static HANDLE GetHandle(char* Path, BOOL bLockDrive, BOOL bWriteAccess, BOOL bWr // We keep FILE_SHARE_READ though, as this shouldn't hurt us any, and is // required for enumeration. hDrive = CreateFileA(Path, GENERIC_READ|(bWriteAccess?GENERIC_WRITE:0), - FILE_SHARE_READ|((bWriteAccess && bWriteShare)?FILE_SHARE_WRITE:0), + FILE_SHARE_READ|(bWriteShare?FILE_SHARE_WRITE:0), NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); if (hDrive != INVALID_HANDLE_VALUE) break; @@ -146,11 +146,10 @@ static HANDLE GetHandle(char* Path, BOOL bLockDrive, BOOL bWriteAccess, BOOL bWr break; if (i == 0) { uprintf("Waiting for access..."); - } else if (bWriteAccess && !bWriteShare && (i > DRIVE_ACCESS_RETRIES/3)) { + } else if (!bWriteShare && (i > DRIVE_ACCESS_RETRIES/3)) { // If we can't seem to get a hold of the drive for some time, // try to enable FILE_SHARE_WRITE... uprintf("Warning: Could not obtain exclusive rights. Retrying with write sharing enabled..."); - SearchProcess(DevPath, TRUE, TRUE); bWriteShare = TRUE; } Sleep(DRIVE_ACCESS_TIMEOUT / DRIVE_ACCESS_RETRIES); @@ -207,11 +206,11 @@ out: /* * Return a handle to the physical drive identified by DriveIndex */ -HANDLE GetPhysicalHandle(DWORD DriveIndex, BOOL bLockDrive, BOOL bWriteAccess) +HANDLE GetPhysicalHandle(DWORD DriveIndex, BOOL bLockDrive, BOOL bWriteAccess, BOOL bWriteShare) { HANDLE hPhysical = INVALID_HANDLE_VALUE; char* PhysicalPath = GetPhysicalName(DriveIndex); - hPhysical = GetHandle(PhysicalPath, bLockDrive, bWriteAccess, FALSE); + hPhysical = GetHandle(PhysicalPath, bLockDrive, bWriteAccess, bWriteShare); safe_free(PhysicalPath); return hPhysical; } @@ -537,7 +536,7 @@ BOOL GetDriveLabel(DWORD DriveIndex, char* letters, char** label) // Try to read an extended label from autorun first. Fallback to regular label if not found. // In the case of card readers with no card, users can get an annoying popup asking them // to insert media. Use IOCTL_STORAGE_CHECK_VERIFY to prevent this - hPhysical = GetPhysicalHandle(DriveIndex, FALSE, FALSE); + hPhysical = GetPhysicalHandle(DriveIndex, FALSE, FALSE, TRUE); if (DeviceIoControl(hPhysical, IOCTL_STORAGE_CHECK_VERIFY, NULL, 0, NULL, 0, &size, NULL)) AutorunLabel = get_token_data_file("label", AutorunPath); else if (GetLastError() == ERROR_NOT_READY) @@ -570,7 +569,7 @@ uint64_t GetDriveSize(DWORD DriveIndex) BYTE geometry[256]; PDISK_GEOMETRY_EX DiskGeometry = (PDISK_GEOMETRY_EX)(void*)geometry; - hPhysical = GetPhysicalHandle(DriveIndex, FALSE, FALSE); + hPhysical = GetPhysicalHandle(DriveIndex, FALSE, FALSE, TRUE); if (hPhysical == INVALID_HANDLE_VALUE) return FALSE; @@ -592,7 +591,7 @@ BOOL IsMediaPresent(DWORD DriveIndex) DWORD size; BYTE geometry[128]; - hPhysical = GetPhysicalHandle(DriveIndex, FALSE, FALSE); + hPhysical = GetPhysicalHandle(DriveIndex, FALSE, FALSE, TRUE); r = DeviceIoControl(hPhysical, IOCTL_DISK_GET_DRIVE_GEOMETRY_EX, NULL, 0, geometry, sizeof(geometry), &size, NULL) && (size > 0); safe_closehandle(hPhysical); @@ -711,7 +710,7 @@ BOOL GetDrivePartitionData(DWORD DriveIndex, char* FileSystemName, DWORD FileSys } safe_free(volume_name); - hPhysical = GetPhysicalHandle(DriveIndex, FALSE, FALSE); + hPhysical = GetPhysicalHandle(DriveIndex, FALSE, FALSE, TRUE); if (hPhysical == INVALID_HANDLE_VALUE) return 0; diff --git a/src/drive.h b/src/drive.h index 5b222853..88110601 100644 --- a/src/drive.h +++ b/src/drive.h @@ -79,7 +79,7 @@ extern RUFUS_DRIVE_INFO SelectedDrive; BOOL SetAutoMount(BOOL enable); BOOL GetAutoMount(BOOL* enabled); char* GetPhysicalName(DWORD DriveIndex); -HANDLE GetPhysicalHandle(DWORD DriveIndex, BOOL bLockDrive, BOOL bWriteAccess); +HANDLE GetPhysicalHandle(DWORD DriveIndex, BOOL bLockDrive, BOOL bWriteAccess, BOOL bWriteShare); char* GetLogicalName(DWORD DriveIndex, BOOL bKeepTrailingBackslash, BOOL bSilent); BOOL WaitForLogical(DWORD DriveIndex); HANDLE GetLogicalHandle(DWORD DriveIndex, BOOL bLockDrive, BOOL bWriteAccess, BOOL bWriteShare); diff --git a/src/format.c b/src/format.c index e43156b2..1d01293f 100644 --- a/src/format.c +++ b/src/format.c @@ -1663,7 +1663,7 @@ DWORD WINAPI FormatThread(void* param) extra_partitions = XP_COMPAT; PrintInfoDebug(0, MSG_225); - hPhysicalDrive = GetPhysicalHandle(DriveIndex, lock_drive, TRUE); + hPhysicalDrive = GetPhysicalHandle(DriveIndex, lock_drive, TRUE, FALSE); if (hPhysicalDrive == INVALID_HANDLE_VALUE) { FormatStatus = ERROR_SEVERITY_ERROR|FAC(FACILITY_STORAGE)|ERROR_OPEN_FAILED; goto out; @@ -2096,7 +2096,7 @@ DWORD WINAPI SaveImageThread(void* param) LastRefresh = 0; switch (img_save->Type) { case IMG_SAVE_TYPE_VHD: - hPhysicalDrive = GetPhysicalHandle(img_save->DeviceNum, TRUE, FALSE); + hPhysicalDrive = GetPhysicalHandle(img_save->DeviceNum, TRUE, FALSE, FALSE); break; case IMG_SAVE_TYPE_ISO: hPhysicalDrive = CreateFileA(img_save->DevicePath, GENERIC_READ, FILE_SHARE_READ, diff --git a/src/rufus.rc b/src/rufus.rc index ec84f6d1..2d164c22 100644 --- a/src/rufus.rc +++ b/src/rufus.rc @@ -33,7 +33,7 @@ LANGUAGE LANG_NEUTRAL, SUBLANG_NEUTRAL IDD_DIALOG DIALOGEX 12, 12, 242, 376 STYLE DS_SETFONT | DS_MODALFRAME | DS_CENTER | WS_MINIMIZEBOX | WS_POPUP | WS_CAPTION | WS_SYSMENU EXSTYLE WS_EX_ACCEPTFILES -CAPTION "Rufus 2.15.1103" +CAPTION "Rufus 2.15.1104" FONT 8, "Segoe UI Symbol", 400, 0, 0x0 BEGIN LTEXT "Device",IDS_DEVICE_TXT,9,6,200,8 @@ -334,8 +334,8 @@ END // VS_VERSION_INFO VERSIONINFO - FILEVERSION 2,15,1103,0 - PRODUCTVERSION 2,15,1103,0 + FILEVERSION 2,15,1104,0 + PRODUCTVERSION 2,15,1104,0 FILEFLAGSMASK 0x3fL #ifdef _DEBUG FILEFLAGS 0x1L @@ -352,13 +352,13 @@ BEGIN BEGIN VALUE "CompanyName", "Akeo Consulting (http://akeo.ie)" VALUE "FileDescription", "Rufus" - VALUE "FileVersion", "2.15.1103" + VALUE "FileVersion", "2.15.1104" VALUE "InternalName", "Rufus" VALUE "LegalCopyright", "© 2011-2017 Pete Batard (GPL v3)" VALUE "LegalTrademarks", "http://www.gnu.org/copyleft/gpl.html" VALUE "OriginalFilename", "rufus.exe" VALUE "ProductName", "Rufus" - VALUE "ProductVersion", "2.15.1103" + VALUE "ProductVersion", "2.15.1104" END END BLOCK "VarFileInfo"