Skip to content

Commit

Permalink
Installer: Abort installation if the driver fails to start
Browse files Browse the repository at this point in the history
Currently, in some cases, attempting to start the driver can fail
and cause Usb devices to become non functional. An example of such
case is attemteting to install an unsigned version of UsbDk on Windows
where the driver signature enforcement is enabled. The reason why this
happens is not making sure the driver can start on the system prior to
attaching it to all of the devices.

This patch solves this issue by checking that the driver can start on the
system prior to step 2 in the list below.

UsbDk's installation process:
1) Create and register UsbDk's service
2) Add UsbDk to the registry
3) Reset all Usb host conrollers on the system
4) PNP manager attaches and starts the driver for each Usb device's stack

Signed-off-by: Sameeh Jubran <[email protected]>
Signed-off-by: Dmitry Fleytman <[email protected]>
  • Loading branch information
Sameeh Jubran authored and Dmitry Fleytman committed May 29, 2017
1 parent e7b4fd2 commit 5dda755
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 1 deletion.
3 changes: 3 additions & 0 deletions UsbDkController/UsbDkController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@ static int Controller_AnalyzeInstallResult(InstallResult Res, const tstring &OpN
case InstallSuccessNeedReboot:
tcout << OpName << TEXT(" succeeded but reboot is required in order to make it functional") << endl;
return 2;
case InstallAborted:
tcout << OpName << TEXT(" aborted, couln't start the driver on the system. Please make sure you are installing a signed version of UsbDk or else try to disable \"driver signature enforcement\" on the system") << endl;
return 4;
default:
tcout << OpName << TEXT(" returned unknown error code") << endl;
assert(0);
Expand Down
31 changes: 31 additions & 0 deletions UsbDkHelper/Installer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ bool UsbDkInstaller::Install(bool &NeedRollBack)
auto rebootRequired = !m_wdfCoinstaller.PreDeviceInstallEx(infFilePath);

m_scManager.CreateServiceObject(USBDK_DRIVER_NAME, driverLocation.c_str());

verifyDriverCanStart();

m_wdfCoinstaller.PostDeviceInstall(infFilePath);
addUsbDkToRegistry();

Expand Down Expand Up @@ -301,3 +304,31 @@ bool UsbDkInstaller::isWow64B()
}
return bIsWow64 ? true : false;
}

void UsbDkInstaller::verifyDriverCanStart()
{
try
{
m_scManager.StartServiceObject(USBDK_DRIVER_NAME);
m_scManager.StopServiceObject(USBDK_DRIVER_NAME);
}
catch (const UsbDkServiceManagerFailedException &e)
{
auto err = e.GetErrorCode();
/* ERROR_SERVICE_DISABLED occurs in case we are attempting to start the
* driver without associating it with a device.
*/
if (err != ERROR_SERVICE_DISABLED)
{
try
{
m_scManager.DeleteServiceObject(USBDK_DRIVER_NAME);
}
catch (const exception &e)
{
UNREFERENCED_PARAMETER(e);
}
throw UsbDkInstallerAbortedException();
}
}
}
7 changes: 7 additions & 0 deletions UsbDkHelper/Installer.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@ class UsbDkInstallerFailedException : public UsbDkW32ErrorException
UsbDkInstallerFailedException(tstring errMsg, DWORD dwErrorCode) : UsbDkW32ErrorException(tstring(INSTALLER_EXCEPTION_STRING) + errMsg, dwErrorCode){}
};

class UsbDkInstallerAbortedException : public UsbDkInstallerFailedException
{
public:
UsbDkInstallerAbortedException() : UsbDkInstallerFailedException() {}
};

class UsbDkInstaller
{
public:
Expand Down Expand Up @@ -68,5 +74,6 @@ class UsbDkInstaller
void buildMultiStringVectorFromList(vector<TCHAR> &valVector, tstringlist &newfiltersList);
void validatePlatform();
bool isWow64B();
void verifyDriverCanStart();

};
5 changes: 5 additions & 0 deletions UsbDkHelper/UsbDkHelper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,11 @@ InstallResult UsbDk_InstallDriver(void)
UsbDkInstaller installer;
return installer.Install(NeedRollBack) ? InstallSuccess : InstallSuccessNeedReboot;
}
catch (const UsbDkInstallerAbortedException &e)
{
printExceptionString(e.what());
return InstallAborted;
}
catch (const exception &e)
{
printExceptionString(e.what());
Expand Down
3 changes: 2 additions & 1 deletion UsbDkHelper/UsbDkHelper.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ typedef enum
{
InstallFailure,
InstallSuccessNeedReboot,
InstallSuccess
InstallSuccess,
InstallAborted
} InstallResult;

#ifdef __cplusplus
Expand Down
5 changes: 5 additions & 0 deletions UsbDkInstHelper/UsbDkInstHelper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ static int Controller_InstallDriver()
return 0;
case InstallFailure:
return 1;
case InstallAborted:
MessageBox(NULL,
TEXT("Failed to start the driver on the system, installation aborted!\nPlease make sure you are installing a signed version of UsbDk or else try to disable \"driver signature enforcement\" on the system"),
TEXT("UsbDk Runtime Libraries Installer"), MB_OK | MB_ICONEXCLAMATION | MB_SETFOREGROUND | MB_SYSTEMMODAL);
return 4;
case InstallSuccessNeedReboot:
MessageBox(NULL,
TEXT("Please restart your computer to complete the installation"),
Expand Down

0 comments on commit 5dda755

Please sign in to comment.