1
1
Fork 0
mirror of https://github.com/pbatard/rufus.git synced 2024-08-14 23:57:05 +00:00

[core] avoid the use of FILE_SHARE_WRITE

* Not having FILE_SHARE_WRITE on a disk or volume handle helps
  prevent spurious accesses from the OS or other apps while we
  are repartitioning/refomatting.
This commit is contained in:
Pete Batard 2017-02-16 14:13:30 +00:00
parent c877284d2a
commit 2f81e4f177
11 changed files with 50 additions and 31 deletions

View file

@ -248,7 +248,7 @@ static BOOL ExtractFAT(int entry, const char* path)
}
/* Create a file, using the same attributes as found in the FAT */
hFile = CreateFileA(filename, GENERIC_READ|GENERIC_WRITE, FILE_SHARE_READ|FILE_SHARE_WRITE,
hFile = CreateFileA(filename, GENERIC_READ|GENERIC_WRITE, FILE_SHARE_READ,
NULL, CREATE_ALWAYS, dir_entry->Attributes, NULL);
if (hFile == INVALID_HANDLE_VALUE) {
uprintf("Unable to create file '%s': %s.\n", filename, WindowsErrorString());
@ -385,7 +385,7 @@ BOOL ExtractFreeDOS(const char* path)
safe_strcpy(filename, sizeof(filename), ((i<2)?path:locale_path));
safe_strcat(filename, sizeof(filename), res_name[i]);
hFile = CreateFileA(filename, GENERIC_READ|GENERIC_WRITE, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL,
hFile = CreateFileA(filename, GENERIC_READ|GENERIC_WRITE, FILE_SHARE_READ, NULL,
CREATE_ALWAYS, (i<2)?(FILE_ATTRIBUTE_HIDDEN|FILE_ATTRIBUTE_SYSTEM):FILE_ATTRIBUTE_NORMAL, NULL);
if ((hFile == NULL) || (hFile == INVALID_HANDLE_VALUE)) {
uprintf("Unable to create file '%s': %s.\n", filename, WindowsErrorString());

View file

@ -117,7 +117,7 @@ BOOL GetAutoMount(BOOL* enabled)
* Open a drive or volume with optional write and lock access
* Return INVALID_HANDLE_VALUE (/!\ which is DIFFERENT from NULL /!\) on failure.
*/
static HANDLE GetHandle(char* Path, BOOL bWriteAccess, BOOL bLockDrive)
static HANDLE GetHandle(char* Path, BOOL bWriteAccess, BOOL bLockDrive, BOOL bWriteShare)
{
int i;
DWORD size;
@ -125,8 +125,22 @@ static HANDLE GetHandle(char* Path, BOOL bWriteAccess, BOOL bLockDrive)
if (Path == NULL)
goto out;
hDrive = CreateFileA(Path, GENERIC_READ|(bWriteAccess?GENERIC_WRITE:0),
FILE_SHARE_READ|(bWriteAccess?FILE_SHARE_WRITE:0), NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
for (i = 0; i < DRIVE_ACCESS_RETRIES; i++) {
// Don't enable FILE_SHARE_WRITE (unless specifically requested) so that
// we won't be bothered by the OS or other apps when we set up our data.
// However this means we might have to wait for an access gap...
// 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|(bWriteShare?FILE_SHARE_WRITE:0), NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if (hDrive != INVALID_HANDLE_VALUE)
break;
if ((GetLastError() != ERROR_SHARING_VIOLATION) && (GetLastError() != ERROR_ACCESS_DENIED))
break;
if (i == 0)
uprintf("Waiting for access...");
Sleep(DRIVE_ACCESS_TIMEOUT / DRIVE_ACCESS_RETRIES);
}
if (hDrive == INVALID_HANDLE_VALUE) {
uprintf("Could not open drive %s: %s\n", Path, WindowsErrorString());
goto out;
@ -180,7 +194,7 @@ HANDLE GetPhysicalHandle(DWORD DriveIndex, BOOL bWriteAccess, BOOL bLockDrive)
{
HANDLE hPhysical = INVALID_HANDLE_VALUE;
char* PhysicalPath = GetPhysicalName(DriveIndex);
hPhysical = GetHandle(PhysicalPath, bWriteAccess, bLockDrive);
hPhysical = GetHandle(PhysicalPath, bWriteAccess, bLockDrive, FALSE);
safe_free(PhysicalPath);
return hPhysical;
}
@ -301,7 +315,7 @@ BOOL WaitForLogical(DWORD DriveIndex)
* Returns INVALID_HANDLE_VALUE on error or NULL if no logical path exists (typical
* of unpartitioned drives)
*/
HANDLE GetLogicalHandle(DWORD DriveIndex, BOOL bWriteAccess, BOOL bLockDrive)
HANDLE GetLogicalHandle(DWORD DriveIndex, BOOL bWriteAccess, BOOL bLockDrive, BOOL bWriteShare)
{
HANDLE hLogical = INVALID_HANDLE_VALUE;
char* LogicalPath = GetLogicalName(DriveIndex, FALSE, FALSE);
@ -311,7 +325,7 @@ HANDLE GetLogicalHandle(DWORD DriveIndex, BOOL bWriteAccess, BOOL bLockDrive)
return NULL;
}
hLogical = GetHandle(LogicalPath, bWriteAccess, bLockDrive);
hLogical = GetHandle(LogicalPath, bWriteAccess, bLockDrive, bWriteShare);
free(LogicalPath);
return hLogical;
}

View file

@ -82,7 +82,7 @@ char* GetPhysicalName(DWORD DriveIndex);
HANDLE GetPhysicalHandle(DWORD DriveIndex, BOOL bWriteAccess, BOOL bLockDrive);
char* GetLogicalName(DWORD DriveIndex, BOOL bKeepTrailingBackslash, BOOL bSilent);
BOOL WaitForLogical(DWORD DriveIndex);
HANDLE GetLogicalHandle(DWORD DriveIndex, BOOL bWriteAccess, BOOL bLockDrive);
HANDLE GetLogicalHandle(DWORD DriveIndex, BOOL bWriteAccess, BOOL bLockDrive, BOOL bWriteShare);
int GetDriveNumber(HANDLE hDrive, char* path);
BOOL GetDriveLetters(DWORD DriveIndex, char* drive_letters);
UINT GetDriveTypeFromIndex(DWORD DriveIndex);

View file

@ -414,7 +414,7 @@ static BOOL FormatFAT32(DWORD DriveIndex)
VolumeId = GetVolumeID();
// Open the drive and lock it
hLogicalVolume = GetLogicalHandle(DriveIndex, TRUE, TRUE);
hLogicalVolume = GetLogicalHandle(DriveIndex, TRUE, TRUE, FALSE);
if (IS_ERROR(FormatStatus)) goto out;
if ((hLogicalVolume == INVALID_HANDLE_VALUE) || (hLogicalVolume == NULL))
die("Invalid logical volume handle\n", ERROR_INVALID_HANDLE);
@ -1198,7 +1198,7 @@ static BOOL SetupWinPE(char drive_letter)
}
// At this stage we only handle \i386
handle = CreateFileA(dst, GENERIC_READ|GENERIC_WRITE, FILE_SHARE_READ|FILE_SHARE_WRITE,
handle = CreateFileA(dst, GENERIC_READ|GENERIC_WRITE, FILE_SHARE_READ,
NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if (handle == INVALID_HANDLE_VALUE) {
uprintf("Could not open %s for patching: %s\n", dst, WindowsErrorString());
@ -1680,7 +1680,7 @@ DWORD WINAPI FormatThread(void* param)
uprintf("Will use '%c:' as volume mountpoint\n", drive_name[0]);
// ...but we need a lock to the logical drive to be able to write anything to it
hLogicalVolume = GetLogicalHandle(DriveIndex, FALSE, TRUE);
hLogicalVolume = GetLogicalHandle(DriveIndex, FALSE, TRUE, FALSE);
if (hLogicalVolume == INVALID_HANDLE_VALUE) {
uprintf("Could not lock volume\n");
FormatStatus = ERROR_SEVERITY_ERROR|FAC(FACILITY_STORAGE)|ERROR_OPEN_FAILED;
@ -1917,11 +1917,12 @@ DWORD WINAPI FormatThread(void* param)
(!HAS_WINDOWS(img_report) || !allow_dual_uefi_bios)) ) {
if (!InstallSyslinux(DriveIndex, drive_name[0], fs)) {
FormatStatus = ERROR_SEVERITY_ERROR|FAC(FACILITY_STORAGE)|ERROR_INSTALL_FAILURE;
goto out;
}
} else {
// We still have a lock, which we need to modify the volume boot record
// => no need to reacquire the lock...
hLogicalVolume = GetLogicalHandle(DriveIndex, TRUE, FALSE);
hLogicalVolume = GetLogicalHandle(DriveIndex, TRUE, FALSE, FALSE);
if ((hLogicalVolume == INVALID_HANDLE_VALUE) || (hLogicalVolume == NULL)) {
uprintf("Could not re-mount volume for partition boot record access\n");
FormatStatus = ERROR_SEVERITY_ERROR|FAC(FACILITY_STORAGE)|ERROR_OPEN_FAILED;

View file

@ -101,7 +101,7 @@ static BOOL SaveIcon(const char* filename)
icondir = (GRPICONDIR*)GetResource(hMainInstance, MAKEINTRESOURCEA(IDI_ICON), _RT_GROUP_ICON, "icon", &res_size, FALSE);
hFile = CreateFileA(filename, GENERIC_READ|GENERIC_WRITE, FILE_SHARE_READ|FILE_SHARE_WRITE,
hFile = CreateFileA(filename, GENERIC_READ|GENERIC_WRITE, FILE_SHARE_READ,
NULL, CREATE_NEW, FILE_ATTRIBUTE_NORMAL, NULL);
if (hFile == INVALID_HANDLE_VALUE) {
uprintf("Unable to create icon '%s': %s.", filename, WindowsErrorString());

View file

@ -350,7 +350,7 @@ static LPFILETIME __inline to_filetime(time_t t)
static void __inline set_directory_timestamp(char* path, LPFILETIME creation, LPFILETIME last_access, LPFILETIME modify)
{
HANDLE dir_handle = CreateFileU(path, GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL);
FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL);
if ((dir_handle == INVALID_HANDLE_VALUE) || (!SetFileTime(dir_handle, creation, last_access, modify)))
uprintf(" Could not set timestamp for directory '%s': %s", path, WindowsErrorString());
safe_closehandle(dir_handle);
@ -427,7 +427,7 @@ static int udf_extract_files(udf_t *p_udf, udf_dirent_t *p_udf_dirent, const cha
if (!is_identical)
uprintf(" File name sanitized to '%s'", psz_sanpath);
file_handle = CreateFileU(psz_sanpath, GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
FILE_SHARE_READ, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
if (file_handle == INVALID_HANDLE_VALUE) {
err = GetLastError();
uprintf(" Unable to create file: %s", WindowsErrorString());
@ -573,7 +573,7 @@ static int iso_extract_files(iso9660_t* p_iso, const char *psz_path)
safe_free(p_statbuf->rr.psz_symlink);
}
file_handle = CreateFileU(psz_sanpath, GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
FILE_SHARE_READ, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
if (file_handle == INVALID_HANDLE_VALUE) {
err = GetLastError();
uprintf(" Unable to create file: %s", WindowsErrorString());
@ -936,7 +936,7 @@ int64_t ExtractISOFile(const char* iso, const char* iso_file, const char* dest_f
HANDLE file_handle = INVALID_HANDLE_VALUE;
file_handle = CreateFileU(dest_file, GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, CREATE_ALWAYS, attributes, NULL);
FILE_SHARE_READ, NULL, CREATE_ALWAYS, attributes, NULL);
if (file_handle == INVALID_HANDLE_VALUE) {
uprintf(" Unable to create file %s: %s\n", dest_file, WindowsErrorString());
goto out;

View file

@ -2854,7 +2854,7 @@ static HANDLE SetHogger(BOOL attached_console, BOOL disable_hogger)
hogmutex = CreateMutexA(NULL, TRUE, "Global/Rufus_CmdLine");
// Extract the hogger resource
hFile = CreateFileA(cmdline_hogger, GENERIC_READ|GENERIC_WRITE, FILE_SHARE_READ|FILE_SHARE_WRITE,
hFile = CreateFileA(cmdline_hogger, GENERIC_READ|GENERIC_WRITE, FILE_SHARE_READ,
NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
if (hFile != INVALID_HANDLE_VALUE) {
// coverity[check_return]
@ -3076,7 +3076,7 @@ int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine
safe_strcpy(loc_file, sizeof(loc_file), rufus_loc);
}
hFile = CreateFileU(loc_file, GENERIC_READ|GENERIC_WRITE, FILE_SHARE_READ|FILE_SHARE_WRITE,
hFile = CreateFileU(loc_file, GENERIC_READ|GENERIC_WRITE, FILE_SHARE_READ,
NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
if ((hFile == INVALID_HANDLE_VALUE) || (!WriteFileWithRetry(hFile, loc_data, loc_size, &size, WRITE_RETRIES))) {
uprintf("localization: unable to extract '%s': %s", loc_file, WindowsErrorString());

View file

@ -50,7 +50,7 @@
#define POP_DIRECTIONAL_FORMATTING ""
#define RIGHT_TO_LEFT_OVERRIDE ""
#define DRIVE_ACCESS_TIMEOUT 15000 // How long we should retry drive access (in ms)
#define DRIVE_ACCESS_RETRIES 60 // How many times we should retry
#define DRIVE_ACCESS_RETRIES 150 // How many times we should retry
#define DRIVE_INDEX_MIN 0x00000080
#define DRIVE_INDEX_MAX 0x000000C0
#define MIN_DRIVE_SIZE 8 // Minimum size a drive must have, to be formattable (in MB)

View file

@ -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.13.1062"
CAPTION "Rufus 2.13.1063"
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,13,1062,0
PRODUCTVERSION 2,13,1062,0
FILEVERSION 2,13,1063,0
PRODUCTVERSION 2,13,1063,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.13.1062"
VALUE "FileVersion", "2.13.1063"
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.13.1062"
VALUE "ProductVersion", "2.13.1063"
END
END
BLOCK "VarFileInfo"

View file

@ -132,8 +132,12 @@ BOOL InstallSyslinux(DWORD drive_index, char drive_letter, int fs_type)
if (sectbuf == NULL)
goto out;
/* First, reopen the volume (we already have a lock) */
d_handle = GetLogicalHandle(drive_index, TRUE, FALSE);
/*
* First, reopen the volume (we already have a lock). Also, for some
* weird reason.the Syslinux install process *MUST* have FILE_SHARE_WRITE
* on the volume, or else creating 'ldlinux.sys' will fail...
*/
d_handle = GetLogicalHandle(drive_index, TRUE, FALSE, TRUE);
if ((d_handle == INVALID_HANDLE_VALUE) || (d_handle == NULL)) {
uprintf("Could open volume for Syslinux installation");
goto out;
@ -198,7 +202,7 @@ BOOL InstallSyslinux(DWORD drive_index, char drive_letter, int fs_type)
FILE_ATTRIBUTE_HIDDEN, NULL);
if (f_handle == INVALID_HANDLE_VALUE) {
uprintf("Unable to create '%s'", &path[3]);
uprintf("Unable to create '%s': %s", &path[3], WindowsErrorString());
goto out;
}
@ -386,7 +390,7 @@ out:
safe_free(syslinux_ldlinux[0]);
safe_free(syslinux_ldlinux[1]);
safe_free(sectors);
safe_closehandle(d_handle);
safe_unlockclose(d_handle);
safe_closehandle(f_handle);
return r;
}

View file

@ -413,7 +413,7 @@ BOOL WimExtractFile_API(const char* image, int index, const char* src, const cha
uprintf(" Could not access WIM info: %s", WindowsErrorString());
goto out;
}
hFile = CreateFileW(wdst, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE,
hFile = CreateFileW(wdst, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ,
NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
if ((hFile == INVALID_HANDLE_VALUE) || (!WriteFile(hFile, wim_info, dw, &dw, NULL))) {
uprintf(" Could not extract file: %s", WindowsErrorString());