Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

3000 lines
73 KiB

  1. //
  2. // Driver Verifier UI
  3. // Copyright (c) Microsoft Corporation, 1999
  4. //
  5. //
  6. //
  7. // module: VrfUtil.cpp
  8. // author: DMihai
  9. // created: 11/1/00
  10. //
  11. // Description
  12. //
  13. #include "stdafx.h"
  14. #include "verifier.h"
  15. #include "vrfutil.h"
  16. #include "vglobal.h"
  17. #include "VSetting.h"
  18. #ifdef _DEBUG
  19. #define new DEBUG_NEW
  20. #undef THIS_FILE
  21. static char THIS_FILE[] = __FILE__;
  22. #endif
  23. //
  24. // Global data
  25. //
  26. const TCHAR RegMemoryManagementKeyName[] =
  27. _T( "System\\CurrentControlSet\\Control\\Session Manager\\Memory Management" );
  28. const TCHAR RegVerifyDriverLevelValueName[] =
  29. _T( "VerifyDriverLevel" );
  30. const TCHAR RegVerifyDriversValueName[] =
  31. _T( "VerifyDrivers" );
  32. ///////////////////////////////////////////////////////////////////////////
  33. //
  34. // Report an error using a dialog box or a console message.
  35. // The message format string is loaded from the resources.
  36. //
  37. void __cdecl VrfErrorResourceFormat( UINT uIdResourceFormat,
  38. ... )
  39. {
  40. TCHAR szMessage[ 256 ];
  41. TCHAR strFormat[ 256 ];
  42. BOOL bResult;
  43. va_list prms;
  44. //
  45. // Load the format string from the resources
  46. //
  47. bResult = VrfLoadString( uIdResourceFormat,
  48. strFormat,
  49. ARRAY_LENGTH( strFormat ) );
  50. ASSERT( bResult );
  51. if( bResult )
  52. {
  53. va_start (prms, uIdResourceFormat);
  54. //
  55. // Format the message in our local buffer
  56. //
  57. _vsntprintf ( szMessage,
  58. ARRAY_LENGTH( szMessage ),
  59. strFormat,
  60. prms);
  61. if( g_bCommandLineMode )
  62. {
  63. //
  64. // Command console mode
  65. //
  66. _putts( szMessage );
  67. TRACE( _T( "%s\n" ), szMessage );
  68. }
  69. else
  70. {
  71. //
  72. // GUI mode
  73. //
  74. AfxMessageBox( szMessage,
  75. MB_OK | MB_ICONSTOP );
  76. }
  77. va_end (prms);
  78. }
  79. }
  80. ///////////////////////////////////////////////////////////////////////////
  81. //
  82. // Print out a message to the console
  83. // The message string is loaded from the resources.
  84. //
  85. void __cdecl VrfTPrintfResourceFormat( UINT uIdResourceFormat,
  86. ... )
  87. {
  88. TCHAR szMessage[ 256 ];
  89. TCHAR strFormat[ 256 ];
  90. BOOL bResult;
  91. va_list prms;
  92. ASSERT( g_bCommandLineMode );
  93. //
  94. // Load the format string from the resources
  95. //
  96. bResult = VrfLoadString( uIdResourceFormat,
  97. strFormat,
  98. ARRAY_LENGTH( strFormat ) );
  99. ASSERT( bResult );
  100. if( bResult )
  101. {
  102. va_start (prms, uIdResourceFormat);
  103. //
  104. // Format the message in our local buffer
  105. //
  106. _vsntprintf ( szMessage,
  107. ARRAY_LENGTH( szMessage ),
  108. strFormat,
  109. prms);
  110. _putts( szMessage );
  111. va_end (prms);
  112. }
  113. }
  114. ///////////////////////////////////////////////////////////////////////////
  115. //
  116. // Print out a simple (non-formatted) message to the console
  117. // The message string is loaded from the resources.
  118. //
  119. void __cdecl VrfPrintStringFromResources( UINT uIdString )
  120. {
  121. TCHAR szMessage[ 256 ];
  122. ASSERT( g_bCommandLineMode );
  123. VERIFY( VrfLoadString( uIdString,
  124. szMessage,
  125. ARRAY_LENGTH( szMessage ) ) );
  126. _putts( szMessage );
  127. }
  128. ///////////////////////////////////////////////////////////////////////////
  129. //
  130. // Report an error using a dialog box or a console message.
  131. // The message string is loaded from the resources.
  132. //
  133. void __cdecl VrfMesssageFromResource( UINT uIdString )
  134. {
  135. TCHAR szMessage[ 256 ];
  136. VERIFY( VrfLoadString( uIdString,
  137. szMessage,
  138. ARRAY_LENGTH( szMessage ) ) );
  139. if( g_bCommandLineMode )
  140. {
  141. //
  142. // Command console mode
  143. //
  144. _putts( szMessage );
  145. }
  146. else
  147. {
  148. //
  149. // GUI mode
  150. //
  151. AfxMessageBox( szMessage,
  152. MB_OK | MB_ICONINFORMATION );
  153. }
  154. }
  155. ///////////////////////////////////////////////////////////////////////////
  156. //
  157. // Load a string from resources.
  158. // Return TRUE if we successfully loaded and FALSE if not.
  159. //
  160. BOOL VrfLoadString( ULONG uIdResource,
  161. TCHAR *szBuffer,
  162. ULONG uBufferLength )
  163. {
  164. ULONG uLoadStringResult;
  165. if( uBufferLength < 1 )
  166. {
  167. ASSERT( FALSE );
  168. return FALSE;
  169. }
  170. uLoadStringResult = LoadString (
  171. g_hProgramModule,
  172. uIdResource,
  173. szBuffer,
  174. uBufferLength );
  175. //
  176. // We should never try to load non-existent strings.
  177. //
  178. ASSERT (uLoadStringResult > 0);
  179. return (uLoadStringResult > 0);
  180. }
  181. ///////////////////////////////////////////////////////////////////////////
  182. //
  183. // Load a string from resources.
  184. // Return TRUE if we successfully loaded and FALSE if not.
  185. //
  186. BOOL VrfLoadString( ULONG uIdResource,
  187. CString &strText )
  188. {
  189. TCHAR szText[ 256 ];
  190. BOOL bSuccess;
  191. bSuccess = VrfLoadString( uIdResource,
  192. szText,
  193. ARRAY_LENGTH( szText ) );
  194. if( TRUE == bSuccess )
  195. {
  196. strText = szText;
  197. }
  198. else
  199. {
  200. strText = "";
  201. }
  202. return bSuccess;
  203. }
  204. ///////////////////////////////////////////////////////////////////////////
  205. VOID
  206. CopyStringArray( const CStringArray &strArraySource,
  207. CStringArray &strArrayDest )
  208. {
  209. INT_PTR nNewSize;
  210. INT_PTR nCrtElem;
  211. strArrayDest.RemoveAll();
  212. nNewSize = strArraySource.GetSize();
  213. for( nCrtElem = 0; nCrtElem < nNewSize; nCrtElem += 1 )
  214. {
  215. strArrayDest.Add( strArraySource.GetAt( nCrtElem ) );
  216. }
  217. }
  218. /////////////////////////////////////////////////////////////////////////////
  219. //
  220. // Copied from sdktools\bvtsigvf
  221. //
  222. BOOL VerifyIsFileSigned( LPCTSTR pcszMatchFile,
  223. PDRIVER_VER_INFO lpVerInfo)
  224. {
  225. HRESULT hRes;
  226. WINTRUST_DATA WinTrustData;
  227. WINTRUST_FILE_INFO WinTrustFile;
  228. GUID gOSVerCheck = DRIVER_ACTION_VERIFY;
  229. GUID gPublishedSoftware = WINTRUST_ACTION_GENERIC_VERIFY_V2;
  230. #ifndef UNICODE
  231. INT iRet;
  232. WCHAR wszFileName[MAX_PATH];
  233. #endif
  234. ZeroMemory(&WinTrustData, sizeof(WINTRUST_DATA));
  235. WinTrustData.cbStruct = sizeof(WINTRUST_DATA);
  236. WinTrustData.dwUIChoice = WTD_UI_NONE;
  237. WinTrustData.fdwRevocationChecks = WTD_REVOKE_NONE;
  238. WinTrustData.dwUnionChoice = WTD_CHOICE_FILE;
  239. WinTrustData.dwStateAction = WTD_STATEACTION_AUTO_CACHE;
  240. WinTrustData.pFile = &WinTrustFile;
  241. WinTrustData.pPolicyCallbackData = (LPVOID)lpVerInfo;
  242. ZeroMemory(lpVerInfo, sizeof(DRIVER_VER_INFO));
  243. lpVerInfo->cbStruct = sizeof(DRIVER_VER_INFO);
  244. ZeroMemory(&WinTrustFile, sizeof(WINTRUST_FILE_INFO));
  245. WinTrustFile.cbStruct = sizeof(WINTRUST_FILE_INFO);
  246. #ifndef UNICODE
  247. iRet = MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, pcszMatchFile, -1, wszFileName, ARRAY_LENGTH(wszFileName));
  248. WinTrustFile.pcwszFilePath = wszFileName;
  249. #else
  250. WinTrustFile.pcwszFilePath = pcszMatchFile;
  251. #endif
  252. hRes = WinVerifyTrust( AfxGetMainWnd()->m_hWnd, &gOSVerCheck, &WinTrustData);
  253. if (hRes != ERROR_SUCCESS)
  254. hRes = WinVerifyTrust( AfxGetMainWnd()->m_hWnd, &gPublishedSoftware, &WinTrustData);
  255. //
  256. // Free the pcSignerCertContext member of the DRIVER_VER_INFO struct
  257. // that was allocated in our call to WinVerifyTrust.
  258. //
  259. if (lpVerInfo && lpVerInfo->pcSignerCertContext) {
  260. CertFreeCertificateContext(lpVerInfo->pcSignerCertContext);
  261. lpVerInfo->pcSignerCertContext = NULL;
  262. }
  263. return (hRes == ERROR_SUCCESS);
  264. }
  265. #define HASH_SIZE 100
  266. BOOL IsDriverSigned( LPCTSTR szDriverFileName )
  267. {
  268. HANDLE hFile;
  269. BOOL bSigned;
  270. BOOL bSuccess;
  271. HRESULT hTrustResult;
  272. DWORD dwHashSize;
  273. GUID guidSubSystemDriver = DRIVER_ACTION_VERIFY;
  274. HCATINFO hCatInfo;
  275. HCATINFO hPrevCatInfo;
  276. BYTE Hash[ HASH_SIZE ];
  277. WINTRUST_DATA WinTrustData;
  278. DRIVER_VER_INFO VerInfo;
  279. WINTRUST_CATALOG_INFO WinTrustCatalogInfo;
  280. CATALOG_INFO CatInfo;
  281. #ifndef UNICODE
  282. WCHAR szUnicodeFileName[MAX_PATH];
  283. #endif
  284. ASSERT( NULL != szDriverFileName );
  285. bSigned = FALSE;
  286. //
  287. // Open the file
  288. //
  289. hFile = CreateFile( szDriverFileName,
  290. GENERIC_READ,
  291. FILE_SHARE_READ | FILE_SHARE_WRITE,
  292. NULL,
  293. OPEN_EXISTING,
  294. FILE_ATTRIBUTE_NORMAL,
  295. NULL);
  296. if( hFile == INVALID_HANDLE_VALUE )
  297. {
  298. //
  299. // ISSUE:
  300. //
  301. // If we cannot find the file we assume it's signed
  302. //
  303. bSigned = TRUE;
  304. goto Done;
  305. }
  306. //
  307. // Generate the hash from the file handle and store it in Hash
  308. //
  309. dwHashSize = ARRAY_LENGTH( Hash );
  310. ZeroMemory( Hash,
  311. sizeof( Hash ) );
  312. bSuccess = CryptCATAdminCalcHashFromFileHandle( hFile,
  313. &dwHashSize,
  314. Hash,
  315. 0);
  316. CloseHandle( hFile );
  317. if( TRUE != bSuccess )
  318. {
  319. //
  320. // If we couldn't generate a hash assume the file is not signed
  321. //
  322. goto Done;
  323. }
  324. //
  325. // Now we have the file's hash. Initialize the structures that
  326. // will be used later on in calls to WinVerifyTrust.
  327. //
  328. //
  329. // Initialize the VerInfo structure
  330. //
  331. ZeroMemory( &VerInfo, sizeof( VerInfo ) );
  332. VerInfo.cbStruct = sizeof( VerInfo );
  333. //
  334. // Initialize the WinTrustCatalogInfo structure
  335. //
  336. ZeroMemory( &WinTrustCatalogInfo, sizeof( WinTrustCatalogInfo ) );
  337. WinTrustCatalogInfo.cbStruct = sizeof(WinTrustCatalogInfo);
  338. WinTrustCatalogInfo.pbCalculatedFileHash = Hash;
  339. WinTrustCatalogInfo.cbCalculatedFileHash = dwHashSize;
  340. #ifdef UNICODE
  341. WinTrustCatalogInfo.pcwszMemberTag = szDriverFileName;
  342. #else
  343. MultiByteToWideChar( CP_ACP,
  344. 0,
  345. szDriverFileName,
  346. -1,
  347. szUnicodeFileName,
  348. ARRAY_LENGTH( szUnicodeFileName ) );
  349. WinTrustCatalogInfo.pcwszMemberTag = szUnicodeFileName;
  350. #endif
  351. //
  352. // Initialize the WinTrustData structure
  353. //
  354. ZeroMemory( &WinTrustData, sizeof( WinTrustData ) );
  355. WinTrustData.cbStruct = sizeof( WinTrustData );
  356. WinTrustData.dwUIChoice = WTD_UI_NONE;
  357. WinTrustData.fdwRevocationChecks = WTD_REVOKE_NONE;
  358. WinTrustData.dwUnionChoice = WTD_CHOICE_CATALOG;
  359. WinTrustData.dwStateAction = WTD_STATEACTION_AUTO_CACHE;
  360. WinTrustData.pPolicyCallbackData = (LPVOID)&VerInfo;
  361. WinTrustData.pCatalog = &WinTrustCatalogInfo;
  362. //
  363. // If we don't have a g_hCatAdmin yet, acquire one
  364. //
  365. if( NULL == g_hCatAdmin )
  366. {
  367. CryptCATAdminAcquireContext( &g_hCatAdmin,
  368. NULL,
  369. 0);
  370. if( NULL == g_hCatAdmin )
  371. {
  372. //
  373. // Bad luck - consider that the file is not signed and bail out
  374. //
  375. goto Done;
  376. }
  377. }
  378. //
  379. // Now we try to find the file hash in the catalog list, via CryptCATAdminEnumCatalogFromHash
  380. //
  381. hPrevCatInfo = NULL;
  382. hCatInfo = CryptCATAdminEnumCatalogFromHash(
  383. g_hCatAdmin,
  384. Hash,
  385. dwHashSize,
  386. 0,
  387. &hPrevCatInfo );
  388. while( TRUE != bSigned && NULL != hCatInfo )
  389. {
  390. ZeroMemory( &CatInfo, sizeof( CatInfo ) );
  391. CatInfo.cbStruct = sizeof( CatInfo );
  392. bSuccess = CryptCATCatalogInfoFromContext( hCatInfo,
  393. &CatInfo,
  394. 0);
  395. if( FALSE != bSuccess )
  396. {
  397. WinTrustCatalogInfo.pcwszCatalogFilePath = CatInfo.wszCatalogFile;
  398. //
  399. // Now verify that the file is an actual member of the catalog.
  400. //
  401. hTrustResult = WinVerifyTrust( AfxGetMainWnd()->m_hWnd,
  402. &guidSubSystemDriver,
  403. &WinTrustData );
  404. bSigned = SUCCEEDED( hTrustResult );
  405. //
  406. // Free the pcSignerCertContext member of the DRIVER_VER_INFO struct
  407. // that was allocated in our call to WinVerifyTrust.
  408. //
  409. if( VerInfo.pcSignerCertContext != NULL )
  410. {
  411. CertFreeCertificateContext( VerInfo.pcSignerCertContext );
  412. VerInfo.pcSignerCertContext = NULL;
  413. }
  414. }
  415. if( TRUE != bSigned )
  416. {
  417. //
  418. // The hash was in this catalog, but the file wasn't a member... so off to the next catalog
  419. //
  420. hPrevCatInfo = hCatInfo;
  421. hCatInfo = CryptCATAdminEnumCatalogFromHash( g_hCatAdmin,
  422. Hash,
  423. dwHashSize,
  424. 0,
  425. &hPrevCatInfo );
  426. }
  427. }
  428. if( NULL == hCatInfo )
  429. {
  430. //
  431. // If it wasn't found in the catalogs, check if the file is individually signed.
  432. //
  433. bSigned = VerifyIsFileSigned( szDriverFileName,
  434. &VerInfo );
  435. }
  436. Done:
  437. return bSigned;
  438. }
  439. /////////////////////////////////////////////////////////////////////////////
  440. BOOL VrfSetWindowText( CWnd &Wnd,
  441. ULONG uIdResourceString )
  442. {
  443. BOOL bLoaded;
  444. CString strText;
  445. //
  446. // It's safe to use CString::LoadString here because we are
  447. // in GUI mode
  448. //
  449. ASSERT( FALSE == g_bCommandLineMode );
  450. bLoaded = strText.LoadString( uIdResourceString );
  451. ASSERT( TRUE == bLoaded );
  452. Wnd.SetWindowText( strText );
  453. return ( TRUE == bLoaded );
  454. }
  455. /////////////////////////////////////////////////////////////////////////////
  456. BOOL VrfWriteVerifierSettings( BOOL bHaveNewDrivers,
  457. const CString &strDriversToVerify,
  458. BOOL bHaveNewFlags,
  459. DWORD dwVerifyFlags )
  460. {
  461. HKEY hMmKey = NULL;
  462. LONG lResult;
  463. BOOL bSuccess;
  464. ASSERT( bHaveNewDrivers || bHaveNewFlags );
  465. bSuccess = FALSE;
  466. if( bHaveNewDrivers && strDriversToVerify.GetLength() == 0 )
  467. {
  468. //
  469. // No drivers to verify
  470. //
  471. ASSERT( FALSE && "VrfDeleteAllVerifierSettings should have been called for this" );
  472. return VrfDeleteAllVerifierSettings();
  473. }
  474. if( bHaveNewFlags )
  475. {
  476. TRACE( _T( "VrfWriteVerifierSettings: New verifier flags = %#x\n" ),
  477. dwVerifyFlags );
  478. }
  479. if( bHaveNewDrivers )
  480. {
  481. TRACE( _T( "VrfWriteVerifierSettings: New drivers = %s\n" ),
  482. (LPCTSTR) strDriversToVerify );
  483. }
  484. //
  485. // Open the Mm key
  486. //
  487. lResult = RegOpenKeyEx( HKEY_LOCAL_MACHINE,
  488. RegMemoryManagementKeyName,
  489. 0,
  490. KEY_SET_VALUE,
  491. &hMmKey );
  492. if( lResult != ERROR_SUCCESS )
  493. {
  494. if( lResult == ERROR_ACCESS_DENIED )
  495. {
  496. VrfErrorResourceFormat(
  497. IDS_ACCESS_IS_DENIED );
  498. }
  499. else
  500. {
  501. VrfErrorResourceFormat(
  502. IDS_REGOPENKEYEX_FAILED,
  503. RegMemoryManagementKeyName,
  504. (DWORD)lResult);
  505. }
  506. goto Done;
  507. }
  508. if( bHaveNewFlags )
  509. {
  510. //
  511. // Write VerifyDriverLevel value
  512. //
  513. if( VrfWriteRegistryDwordValue( hMmKey,
  514. RegVerifyDriverLevelValueName,
  515. dwVerifyFlags ) == FALSE )
  516. {
  517. RegCloseKey (hMmKey);
  518. goto Done;
  519. }
  520. }
  521. if( bHaveNewDrivers )
  522. {
  523. //
  524. // Write VerifyDrivers value
  525. //
  526. if( VrfWriteRegistryStringValue( hMmKey,
  527. RegVerifyDriversValueName,
  528. strDriversToVerify ) == FALSE )
  529. {
  530. RegCloseKey (hMmKey);
  531. goto Done;
  532. }
  533. }
  534. //
  535. // Close the Mm key and return success
  536. //
  537. RegCloseKey( hMmKey );
  538. bSuccess = TRUE;
  539. if( bSuccess )
  540. {
  541. if( g_bSettingsSaved )
  542. {
  543. VrfMesssageFromResource( IDS_REBOOT );
  544. }
  545. else
  546. {
  547. VrfMesssageFromResource( IDS_NO_SETTINGS_WERE_CHANGED );
  548. }
  549. }
  550. Done:
  551. return TRUE;
  552. }
  553. /////////////////////////////////////////////////////////////////////////////
  554. BOOL VrfWriteRegistryDwordValue( HKEY hKey,
  555. LPCTSTR szValueName,
  556. DWORD dwValue )
  557. {
  558. LONG lResult;
  559. BOOL bSuccess;
  560. lResult = RegSetValueEx( hKey,
  561. szValueName,
  562. 0,
  563. REG_DWORD,
  564. ( LPBYTE ) &dwValue,
  565. sizeof( dwValue ) );
  566. bSuccess = ( lResult == ERROR_SUCCESS );
  567. g_bSettingsSaved = g_bSettingsSaved | bSuccess;
  568. if( TRUE != bSuccess )
  569. {
  570. VrfErrorResourceFormat(
  571. IDS_REGSETVALUEEX_FAILED,
  572. szValueName,
  573. (DWORD) lResult );
  574. }
  575. return bSuccess;
  576. }
  577. /////////////////////////////////////////////////////////////////////////////
  578. BOOL VrfWriteRegistryStringValue( HKEY hKey,
  579. LPCTSTR szValueName,
  580. LPCTSTR szValue )
  581. {
  582. BOOL bSuccess;
  583. LONG lResult;
  584. lResult = RegSetValueEx ( hKey,
  585. szValueName,
  586. 0,
  587. REG_SZ,
  588. (LPBYTE) szValue,
  589. ( _tcslen( szValue ) + 1 ) * sizeof (TCHAR) );
  590. bSuccess = ( lResult == ERROR_SUCCESS );
  591. g_bSettingsSaved = g_bSettingsSaved | bSuccess;
  592. if( TRUE != bSuccess )
  593. {
  594. VrfErrorResourceFormat(
  595. IDS_REGSETVALUEEX_FAILED,
  596. szValueName,
  597. (DWORD) lResult);
  598. }
  599. return bSuccess;
  600. }
  601. /////////////////////////////////////////////////////////////////////////////
  602. BOOL VrfReadVerifierSettings( CString &strDriversToVerify,
  603. DWORD &dwVerifyFlags )
  604. {
  605. HKEY hMmKey = NULL;
  606. LONG lResult;
  607. BOOL bSuccess;
  608. bSuccess = FALSE;
  609. //
  610. // Open the Mm key
  611. //
  612. lResult = RegOpenKeyEx( HKEY_LOCAL_MACHINE,
  613. RegMemoryManagementKeyName,
  614. 0,
  615. KEY_QUERY_VALUE,
  616. &hMmKey );
  617. if( lResult != ERROR_SUCCESS )
  618. {
  619. if( lResult == ERROR_ACCESS_DENIED )
  620. {
  621. VrfErrorResourceFormat(
  622. IDS_ACCESS_IS_DENIED );
  623. }
  624. else
  625. {
  626. VrfErrorResourceFormat(
  627. IDS_REGOPENKEYEX_FAILED,
  628. RegMemoryManagementKeyName,
  629. (DWORD)lResult);
  630. }
  631. goto Done;
  632. }
  633. //
  634. // Read VerifyDriverLevel value
  635. //
  636. if( VrfReadRegistryDwordValue( hMmKey,
  637. RegVerifyDriverLevelValueName,
  638. dwVerifyFlags ) == FALSE )
  639. {
  640. RegCloseKey (hMmKey);
  641. goto Done;
  642. }
  643. //
  644. // Read VerifyDrivers value
  645. //
  646. if( VrfReadRegistryStringValue( hMmKey,
  647. RegVerifyDriversValueName,
  648. strDriversToVerify ) == FALSE )
  649. {
  650. RegCloseKey (hMmKey);
  651. goto Done;
  652. }
  653. //
  654. // Close the Mm key and return success
  655. //
  656. RegCloseKey( hMmKey );
  657. bSuccess = TRUE;
  658. Done:
  659. return TRUE;
  660. }
  661. /////////////////////////////////////////////////////////////////////////////
  662. BOOL VrtLoadCurrentRegistrySettings( BOOL &bAllDriversVerified,
  663. CStringArray &astrDriversToVerify,
  664. DWORD &dwVerifyFlags )
  665. {
  666. BOOL bResult;
  667. CString strDriversToVerify;
  668. astrDriversToVerify.RemoveAll();
  669. dwVerifyFlags = 0;
  670. bAllDriversVerified = FALSE;
  671. bResult = VrfReadVerifierSettings( strDriversToVerify,
  672. dwVerifyFlags );
  673. strDriversToVerify.TrimLeft();
  674. strDriversToVerify.TrimRight();
  675. if( strDriversToVerify.CompareNoCase( _T( "*" ) ) == 0 )
  676. {
  677. bAllDriversVerified = TRUE;
  678. }
  679. else
  680. {
  681. VrfSplitDriverNamesSpaceSeparated( strDriversToVerify,
  682. astrDriversToVerify );
  683. }
  684. return bResult;
  685. }
  686. /////////////////////////////////////////////////////////////////////////////
  687. VOID VrfSplitDriverNamesSpaceSeparated( CString strAllDrivers,
  688. CStringArray &astrVerifyDriverNames )
  689. {
  690. INT nCharIndex;
  691. CString strCrtDriverName;
  692. astrVerifyDriverNames.RemoveAll();
  693. //
  694. // Split the space separated driver names in astrDriversToVerify
  695. //
  696. strAllDrivers.TrimRight();
  697. while( TRUE )
  698. {
  699. strAllDrivers.TrimLeft();
  700. if( strAllDrivers.GetLength() == 0 )
  701. {
  702. //
  703. // We are done parsing the whole string
  704. //
  705. break;
  706. }
  707. //
  708. // Look for a space or a tab
  709. //
  710. nCharIndex = strAllDrivers.Find( _T( ' ' ) );
  711. if( nCharIndex < 0 )
  712. {
  713. nCharIndex = strAllDrivers.Find( _T( '\t' ) );
  714. }
  715. if( nCharIndex >= 0 )
  716. {
  717. //
  718. // Found a separator character
  719. //
  720. strCrtDriverName = strAllDrivers.Left( nCharIndex );
  721. if( strCrtDriverName.GetLength() > 0 &&
  722. FALSE == VrfIsStringInArray( strCrtDriverName,
  723. astrVerifyDriverNames ) )
  724. {
  725. astrVerifyDriverNames.Add( strCrtDriverName );
  726. }
  727. strAllDrivers = strAllDrivers.Right( strAllDrivers.GetLength() - nCharIndex - 1 );
  728. }
  729. else
  730. {
  731. //
  732. // This is the last driver name
  733. //
  734. if( FALSE == VrfIsStringInArray( strAllDrivers,
  735. astrVerifyDriverNames ) )
  736. {
  737. astrVerifyDriverNames.Add( strAllDrivers );
  738. }
  739. break;
  740. }
  741. }
  742. }
  743. /////////////////////////////////////////////////////////////////////////////
  744. BOOL VrfIsDriversSetDifferent( CString strAllDrivers1,
  745. const CStringArray &astrVerifyDriverNames2 )
  746. {
  747. BOOL bDifferent;
  748. INT_PTR nDriverNames1;
  749. INT_PTR nDriverNames2;
  750. INT_PTR nCrtDriver1;
  751. INT_PTR nCrtDriver2;
  752. CString strDriver1;
  753. CString strDriver2;
  754. CStringArray astrVerifyDriverNames1;
  755. bDifferent = TRUE;
  756. VrfSplitDriverNamesSpaceSeparated( strAllDrivers1,
  757. astrVerifyDriverNames1 );
  758. nDriverNames1 = astrVerifyDriverNames1.GetSize();
  759. nDriverNames2 = astrVerifyDriverNames2.GetSize();
  760. if( nDriverNames1 == nDriverNames2 )
  761. {
  762. //
  763. // Same number of drivers
  764. //
  765. bDifferent = FALSE;
  766. for( nCrtDriver1 = 0; nCrtDriver1 < nDriverNames1; nCrtDriver1 += 1 )
  767. {
  768. strDriver1 = astrVerifyDriverNames1.GetAt( nCrtDriver1 );
  769. bDifferent = TRUE;
  770. //
  771. // Look for strDriver1 in astrVerifyDriverNames2
  772. //
  773. for( nCrtDriver2 = 0; nCrtDriver2 < nDriverNames2; nCrtDriver2 += 1 )
  774. {
  775. strDriver2 = astrVerifyDriverNames2.GetAt( nCrtDriver2 );
  776. if( strDriver1.CompareNoCase( strDriver2 ) == 0 )
  777. {
  778. bDifferent = FALSE;
  779. break;
  780. }
  781. }
  782. if( TRUE == bDifferent )
  783. {
  784. //
  785. // Did not find strDriver1 in astrVerifyDriverNames2
  786. //
  787. break;
  788. }
  789. }
  790. }
  791. return bDifferent;
  792. }
  793. /////////////////////////////////////////////////////////////////////////////
  794. BOOL VrfReadRegistryDwordValue( HKEY hKey,
  795. LPCTSTR szValueName,
  796. DWORD &dwValue )
  797. {
  798. LONG lResult;
  799. BOOL bSuccess;
  800. DWORD dwType;
  801. DWORD dwDataSize;
  802. dwDataSize = sizeof( dwValue );
  803. lResult = RegQueryValueEx( hKey,
  804. szValueName,
  805. 0,
  806. &dwType,
  807. ( LPBYTE ) &dwValue,
  808. &dwDataSize );
  809. if( lResult == ERROR_FILE_NOT_FOUND )
  810. {
  811. //
  812. // The value doesn't currently exist
  813. //
  814. dwValue = 0;
  815. bSuccess = TRUE;
  816. }
  817. else
  818. {
  819. bSuccess = ( ERROR_SUCCESS == lResult &&
  820. REG_DWORD == dwType &&
  821. dwDataSize == sizeof( dwValue ) );
  822. }
  823. if( TRUE != bSuccess )
  824. {
  825. VrfErrorResourceFormat(
  826. IDS_REGQUERYVALUEEX_FAILED,
  827. szValueName,
  828. (DWORD) lResult );
  829. }
  830. return bSuccess;
  831. }
  832. /////////////////////////////////////////////////////////////////////////////
  833. BOOL VrfReadRegistryStringValue( HKEY hKey,
  834. LPCTSTR szValueName,
  835. CString &strDriversToVerify )
  836. {
  837. BOOL bSuccess;
  838. LONG lResult;
  839. LPTSTR szDriversToVerify;
  840. ULONG uRegKeyLength;
  841. DWORD dwType;
  842. DWORD dwDataSize;
  843. bSuccess = FALSE;
  844. lResult = ERROR_NOT_ENOUGH_MEMORY;
  845. szDriversToVerify = NULL;
  846. for( uRegKeyLength = 128; uRegKeyLength < 4096; uRegKeyLength += 128 )
  847. {
  848. //
  849. // Try allocate a local buffer and use it to query
  850. //
  851. szDriversToVerify = new TCHAR[ uRegKeyLength ];
  852. if( NULL != szDriversToVerify )
  853. {
  854. dwDataSize = uRegKeyLength * sizeof (TCHAR);
  855. lResult = RegQueryValueEx( hKey,
  856. szValueName,
  857. 0,
  858. &dwType,
  859. (LPBYTE) szDriversToVerify,
  860. &dwDataSize );
  861. switch( lResult )
  862. {
  863. case ERROR_FILE_NOT_FOUND:
  864. //
  865. // Return an empty string
  866. //
  867. szDriversToVerify[ 0 ] = (TCHAR)0;
  868. bSuccess = TRUE;
  869. break;
  870. case ERROR_SUCCESS:
  871. //
  872. // Got the driver names from the registry
  873. //
  874. bSuccess = ( REG_SZ == dwType );
  875. break;
  876. default:
  877. //
  878. // Try with a bigger buffer
  879. //
  880. break;
  881. }
  882. }
  883. if( FALSE != bSuccess )
  884. {
  885. //
  886. // Got what we needed
  887. //
  888. break;
  889. }
  890. else
  891. {
  892. //
  893. // Delete the current buffer and try with a bigger one
  894. //
  895. ASSERT( NULL != szDriversToVerify );
  896. strDriversToVerify = szDriversToVerify;
  897. delete [] szDriversToVerify;
  898. szDriversToVerify = NULL;
  899. }
  900. }
  901. if( TRUE != bSuccess )
  902. {
  903. VrfErrorResourceFormat(
  904. IDS_REGSETVALUEEX_FAILED,
  905. szValueName,
  906. (DWORD) lResult);
  907. }
  908. else
  909. {
  910. ASSERT( NULL != szDriversToVerify );
  911. strDriversToVerify = szDriversToVerify;
  912. delete [] szDriversToVerify;
  913. szDriversToVerify = NULL;
  914. }
  915. return bSuccess;
  916. }
  917. /////////////////////////////////////////////////////////////////////////////
  918. BOOL VrfDeleteAllVerifierSettings()
  919. {
  920. HKEY hMmKey = NULL;
  921. LONG lResult;
  922. BOOL bSuccess;
  923. bSuccess = FALSE;
  924. //
  925. // Open the Mm key
  926. //
  927. lResult = RegOpenKeyEx( HKEY_LOCAL_MACHINE,
  928. RegMemoryManagementKeyName,
  929. 0,
  930. KEY_SET_VALUE,
  931. &hMmKey );
  932. if( lResult != ERROR_SUCCESS )
  933. {
  934. if( lResult == ERROR_ACCESS_DENIED )
  935. {
  936. VrfErrorResourceFormat(
  937. IDS_ACCESS_IS_DENIED );
  938. }
  939. else
  940. {
  941. VrfErrorResourceFormat(
  942. IDS_REGOPENKEYEX_FAILED,
  943. RegMemoryManagementKeyName,
  944. (DWORD)lResult);
  945. }
  946. goto Done;
  947. }
  948. //
  949. // Delete VerifyDriverLevel value
  950. //
  951. lResult = RegDeleteValue( hMmKey,
  952. RegVerifyDriverLevelValueName );
  953. if( lResult != ERROR_SUCCESS && lResult != ERROR_FILE_NOT_FOUND )
  954. {
  955. VrfErrorResourceFormat(
  956. IDS_REGDELETEVALUE_FAILED,
  957. RegVerifyDriverLevelValueName,
  958. (DWORD) lResult );
  959. goto Done;
  960. }
  961. g_bSettingsSaved = g_bSettingsSaved | ( lResult != ERROR_FILE_NOT_FOUND );
  962. //
  963. // Delete VerifyDrivers value
  964. //
  965. lResult = RegDeleteValue( hMmKey,
  966. RegVerifyDriversValueName );
  967. if( lResult != ERROR_SUCCESS && lResult != ERROR_FILE_NOT_FOUND )
  968. {
  969. VrfErrorResourceFormat(
  970. IDS_REGDELETEVALUE_FAILED,
  971. RegVerifyDriversValueName,
  972. (DWORD) lResult );
  973. RegCloseKey (hMmKey);
  974. goto Done;
  975. }
  976. g_bSettingsSaved = g_bSettingsSaved | ( lResult != ERROR_FILE_NOT_FOUND );
  977. //
  978. // Close the Mm key and return success
  979. //
  980. RegCloseKey( hMmKey );
  981. bSuccess = TRUE;
  982. if( bSuccess && g_bSettingsSaved )
  983. {
  984. VrfMesssageFromResource( IDS_REBOOT );
  985. }
  986. else
  987. {
  988. VrfMesssageFromResource(
  989. IDS_NO_SETTINGS_WERE_CHANGED );
  990. }
  991. Done:
  992. return bSuccess;
  993. }
  994. /////////////////////////////////////////////////////////////////////////////
  995. BOOL VrfGetRuntimeVerifierData( CRuntimeVerifierData *pRuntimeVerifierData )
  996. {
  997. NTSTATUS Status;
  998. ULONG Length = 0;
  999. ULONG buffersize;
  1000. BOOL bSuccess;
  1001. PSYSTEM_VERIFIER_INFORMATION VerifierInfo;
  1002. PSYSTEM_VERIFIER_INFORMATION VerifierInfoBase;
  1003. CRuntimeDriverData *pCrtDriverData;
  1004. LPTSTR szName;
  1005. ASSERT_VALID( pRuntimeVerifierData );
  1006. pRuntimeVerifierData->FillWithDefaults();
  1007. pRuntimeVerifierData->m_RuntimeDriverDataArray.DeleteAll();
  1008. bSuccess = FALSE;
  1009. //
  1010. // Try to get the right size for the NtQuery buffer
  1011. //
  1012. buffersize = 1024;
  1013. do
  1014. {
  1015. VerifierInfo = (PSYSTEM_VERIFIER_INFORMATION)malloc (buffersize);
  1016. if (VerifierInfo == NULL)
  1017. {
  1018. Status = STATUS_INSUFFICIENT_RESOURCES;
  1019. break;
  1020. }
  1021. Status = NtQuerySystemInformation (SystemVerifierInformation,
  1022. VerifierInfo,
  1023. buffersize,
  1024. &Length);
  1025. if (Status != STATUS_INFO_LENGTH_MISMATCH)
  1026. {
  1027. break;
  1028. }
  1029. free (VerifierInfo);
  1030. buffersize += 1024;
  1031. } while (1);
  1032. if (! NT_SUCCESS(Status))
  1033. {
  1034. VrfErrorResourceFormat(
  1035. IDS_QUERY_SYSINFO_FAILED,
  1036. Status);
  1037. goto Done;
  1038. }
  1039. //
  1040. // If no info fill out return success but no info.
  1041. //
  1042. if (Length == 0)
  1043. {
  1044. free (VerifierInfo);
  1045. bSuccess = TRUE;
  1046. goto Done;
  1047. }
  1048. //
  1049. // Fill out the cumulative-driver stuff.
  1050. //
  1051. VerifierInfoBase = VerifierInfo;
  1052. pRuntimeVerifierData->m_bSpecialPool = (VerifierInfo->Level & DRIVER_VERIFIER_SPECIAL_POOLING) ? TRUE : FALSE;
  1053. pRuntimeVerifierData->m_bForceIrql = (VerifierInfo->Level & DRIVER_VERIFIER_FORCE_IRQL_CHECKING) ? TRUE : FALSE;
  1054. pRuntimeVerifierData->m_bLowRes = (VerifierInfo->Level & DRIVER_VERIFIER_INJECT_ALLOCATION_FAILURES) ? TRUE : FALSE;
  1055. pRuntimeVerifierData->m_bPoolTracking = (VerifierInfo->Level & DRIVER_VERIFIER_TRACK_POOL_ALLOCATIONS) ? TRUE : FALSE;
  1056. pRuntimeVerifierData->m_bIo = (VerifierInfo->Level & DRIVER_VERIFIER_IO_CHECKING) ? TRUE : FALSE;
  1057. pRuntimeVerifierData->m_bDeadlockDetect = (VerifierInfo->Level & DRIVER_VERIFIER_DEADLOCK_DETECTION) ? TRUE : FALSE;
  1058. pRuntimeVerifierData->m_bDMAVerif = (VerifierInfo->Level & DRIVER_VERIFIER_DMA_VERIFIER) ? TRUE : FALSE;
  1059. pRuntimeVerifierData->m_bEnhIo = (VerifierInfo->Level & DRIVER_VERIFIER_ENHANCED_IO_CHECKING) ? TRUE : FALSE;
  1060. pRuntimeVerifierData->RaiseIrqls = VerifierInfo->RaiseIrqls;
  1061. pRuntimeVerifierData->AcquireSpinLocks = VerifierInfo->AcquireSpinLocks;
  1062. pRuntimeVerifierData->SynchronizeExecutions = VerifierInfo->SynchronizeExecutions;
  1063. pRuntimeVerifierData->AllocationsAttempted = VerifierInfo->AllocationsAttempted;
  1064. pRuntimeVerifierData->AllocationsSucceeded = VerifierInfo->AllocationsSucceeded;
  1065. pRuntimeVerifierData->AllocationsSucceededSpecialPool = VerifierInfo->AllocationsSucceededSpecialPool;
  1066. pRuntimeVerifierData->AllocationsWithNoTag = VerifierInfo->AllocationsWithNoTag;
  1067. pRuntimeVerifierData->Trims = VerifierInfo->Trims;
  1068. pRuntimeVerifierData->AllocationsFailed = VerifierInfo->AllocationsFailed;
  1069. pRuntimeVerifierData->AllocationsFailedDeliberately = VerifierInfo->AllocationsFailedDeliberately;
  1070. pRuntimeVerifierData->UnTrackedPool = VerifierInfo->UnTrackedPool;
  1071. pRuntimeVerifierData->Level = VerifierInfo->Level;
  1072. //
  1073. // Fill out the per-driver stuff.
  1074. //
  1075. VerifierInfo = VerifierInfoBase;
  1076. do
  1077. {
  1078. //
  1079. // Allocate a new driver data structure
  1080. //
  1081. pCrtDriverData = new CRuntimeDriverData;
  1082. if( NULL == pCrtDriverData )
  1083. {
  1084. VrfErrorResourceFormat( IDS_NOT_ENOUGH_MEMORY );
  1085. break;
  1086. }
  1087. #ifndef UNICODE
  1088. ANSI_STRING Name;
  1089. NTSTATUS Status;
  1090. Status = RtlUnicodeStringToAnsiString (
  1091. & Name,
  1092. & VerifierInfo->DriverName,
  1093. TRUE);
  1094. if (! (NT_SUCCESS(Status) ) )
  1095. {
  1096. VrfErrorResourceFormat( IDS_NOT_ENOUGH_MEMORY );
  1097. break;
  1098. }
  1099. szName = pCrtDriverData->m_strName.GetBuffer( Name.Length + 1 );
  1100. if( NULL != szName )
  1101. {
  1102. CopyMemory( szName,
  1103. Name.Buffer,
  1104. ( Name.Length + 1 ) * sizeof( Name.Buffer[ 0 ] ) );
  1105. szName[ Name.Length ] = 0;
  1106. pCrtDriverData->m_strName.ReleaseBuffer();
  1107. }
  1108. RtlFreeAnsiString (& Name);
  1109. #else
  1110. szName = pCrtDriverData->m_strName.GetBuffer( VerifierInfo->DriverName.Length + 1 );
  1111. if( NULL != szName )
  1112. {
  1113. CopyMemory( szName,
  1114. VerifierInfo->DriverName.Buffer,
  1115. ( VerifierInfo->DriverName.Length + 1 ) * sizeof( VerifierInfo->DriverName.Buffer[ 0 ] ) );
  1116. szName[ VerifierInfo->DriverName.Length ] = 0;
  1117. pCrtDriverData->m_strName.ReleaseBuffer();
  1118. }
  1119. #endif //#ifndef UNICODE
  1120. if( FALSE != pRuntimeVerifierData->IsDriverVerified( pCrtDriverData->m_strName ) )
  1121. {
  1122. //
  1123. // This is a duplicate entry - ignore it.
  1124. //
  1125. delete pCrtDriverData;
  1126. }
  1127. else
  1128. {
  1129. pCrtDriverData->Loads = VerifierInfo->Loads;
  1130. pCrtDriverData->Unloads = VerifierInfo->Unloads;
  1131. pCrtDriverData->CurrentPagedPoolAllocations = VerifierInfo->CurrentPagedPoolAllocations;
  1132. pCrtDriverData->CurrentNonPagedPoolAllocations = VerifierInfo->CurrentNonPagedPoolAllocations;
  1133. pCrtDriverData->PeakPagedPoolAllocations = VerifierInfo->PeakPagedPoolAllocations;
  1134. pCrtDriverData->PeakNonPagedPoolAllocations = VerifierInfo->PeakNonPagedPoolAllocations;
  1135. pCrtDriverData->PagedPoolUsageInBytes = VerifierInfo->PagedPoolUsageInBytes;
  1136. pCrtDriverData->NonPagedPoolUsageInBytes = VerifierInfo->NonPagedPoolUsageInBytes;
  1137. pCrtDriverData->PeakPagedPoolUsageInBytes = VerifierInfo->PeakPagedPoolUsageInBytes;
  1138. pCrtDriverData->PeakNonPagedPoolUsageInBytes = VerifierInfo->PeakNonPagedPoolUsageInBytes;
  1139. pRuntimeVerifierData->m_RuntimeDriverDataArray.Add( pCrtDriverData );
  1140. }
  1141. if (VerifierInfo->NextEntryOffset == 0) {
  1142. break;
  1143. }
  1144. VerifierInfo = (PSYSTEM_VERIFIER_INFORMATION)((PCHAR)VerifierInfo + VerifierInfo->NextEntryOffset);
  1145. }
  1146. while (1);
  1147. free (VerifierInfoBase);
  1148. Done:
  1149. return TRUE;
  1150. }
  1151. /////////////////////////////////////////////////////////////////////
  1152. PLOADED_IMAGE VrfImageLoad( LPTSTR szBinaryName,
  1153. LPTSTR szDirectory )
  1154. {
  1155. #ifdef UNICODE
  1156. char *szOemImageName;
  1157. char *szOemDirectory;
  1158. int nStringLength;
  1159. PLOADED_IMAGE pLoadedImage;
  1160. pLoadedImage = NULL;
  1161. nStringLength = wcslen( szBinaryName );
  1162. szOemImageName = new char [ nStringLength + 1 ];
  1163. if( NULL != szOemImageName )
  1164. {
  1165. CharToOem( szBinaryName,
  1166. szOemImageName );
  1167. if( NULL == szDirectory )
  1168. {
  1169. szOemDirectory = NULL;
  1170. }
  1171. else
  1172. {
  1173. nStringLength = wcslen( szDirectory );
  1174. szOemDirectory = new char [ nStringLength + 1 ];
  1175. CharToOem( szDirectory,
  1176. szOemDirectory );
  1177. }
  1178. pLoadedImage = ImageLoad( szOemImageName,
  1179. szOemDirectory );
  1180. if( NULL != szOemDirectory )
  1181. {
  1182. delete [] szOemDirectory;
  1183. }
  1184. delete [] szOemImageName;
  1185. }
  1186. return pLoadedImage;
  1187. #else
  1188. //
  1189. // Already have ANSI strings
  1190. //
  1191. return ImageLoad( szBinaryName,
  1192. szDirectory );
  1193. #endif //#ifdef UNICODE
  1194. }
  1195. /////////////////////////////////////////////////////////////////////////////
  1196. BOOL VrfDumpStateToFile( FILE *file )
  1197. {
  1198. BOOL bSuccess;
  1199. INT_PTR nDriversNo;
  1200. INT_PTR nCrtDriver;
  1201. SYSTEMTIME SystemTime;
  1202. TCHAR strLocalTime[ 64 ];
  1203. TCHAR strLocalDate[ 64 ];
  1204. CRuntimeDriverData *pRunDriverData;
  1205. CRuntimeVerifierData RunTimeVerifierData;
  1206. //
  1207. // Output the date&time in the current user format
  1208. //
  1209. GetLocalTime( &SystemTime );
  1210. if( GetDateFormat(
  1211. LOCALE_USER_DEFAULT,
  1212. 0,
  1213. &SystemTime,
  1214. NULL,
  1215. strLocalDate,
  1216. ARRAY_LENGTH( strLocalDate ) ) )
  1217. {
  1218. VrfFTPrintf(
  1219. file,
  1220. _T( "%s, " ),
  1221. strLocalDate );
  1222. }
  1223. else
  1224. {
  1225. ASSERT( FALSE );
  1226. }
  1227. if( GetTimeFormat(
  1228. LOCALE_USER_DEFAULT,
  1229. 0,
  1230. &SystemTime,
  1231. NULL,
  1232. strLocalTime,
  1233. ARRAY_LENGTH( strLocalTime ) ) )
  1234. {
  1235. VrfFTPrintf(
  1236. file,
  1237. _T( "%s\n" ),
  1238. strLocalTime);
  1239. }
  1240. else
  1241. {
  1242. ASSERT( FALSE );
  1243. VrfFTPrintf(
  1244. file,
  1245. _T( "\n" ) );
  1246. }
  1247. //
  1248. // Get the current verifier statistics
  1249. //
  1250. if( VrfGetRuntimeVerifierData( &RunTimeVerifierData ) == FALSE) {
  1251. VrfOuputStringFromResources(
  1252. IDS_CANTGET_VERIF_STATE,
  1253. file );
  1254. bSuccess = FALSE;
  1255. goto Done;
  1256. }
  1257. nDriversNo = RunTimeVerifierData.m_RuntimeDriverDataArray.GetSize();
  1258. if( 0 == nDriversNo )
  1259. {
  1260. //
  1261. // no statistics to dump
  1262. //
  1263. bSuccess = VrfOuputStringFromResources(
  1264. IDS_NO_DRIVER_VERIFIED,
  1265. file );
  1266. }
  1267. else
  1268. {
  1269. //
  1270. // dump the counters
  1271. //
  1272. //
  1273. // global counters
  1274. //
  1275. if( ( ! VrfFTPrintfResourceFormat( file, IDS_LEVEL, RunTimeVerifierData.Level ) ) ||
  1276. ( ! VrfFTPrintfResourceFormat( file, IDS_RAISEIRQLS, RunTimeVerifierData.RaiseIrqls ) ) ||
  1277. ( ! VrfFTPrintfResourceFormat( file, IDS_ACQUIRESPINLOCKS, RunTimeVerifierData.AcquireSpinLocks ) ) ||
  1278. ( ! VrfFTPrintfResourceFormat( file, IDS_SYNCHRONIZEEXECUTIONS, RunTimeVerifierData.SynchronizeExecutions) ) ||
  1279. ( ! VrfFTPrintfResourceFormat( file, IDS_ALLOCATIONSATTEMPTED, RunTimeVerifierData.AllocationsAttempted) ) ||
  1280. ( ! VrfFTPrintfResourceFormat( file, IDS_ALLOCATIONSSUCCEEDED, RunTimeVerifierData.AllocationsSucceeded) ) ||
  1281. ( ! VrfFTPrintfResourceFormat( file, IDS_ALLOCATIONSSUCCEEDEDSPECIALPOOL, RunTimeVerifierData.AllocationsSucceededSpecialPool) ) ||
  1282. ( ! VrfFTPrintfResourceFormat( file, IDS_ALLOCATIONSWITHNOTAG, RunTimeVerifierData.AllocationsWithNoTag) ) ||
  1283. ( ! VrfFTPrintfResourceFormat( file, IDS_ALLOCATIONSFAILED, RunTimeVerifierData.AllocationsFailed) ) ||
  1284. ( ! VrfFTPrintfResourceFormat( file, IDS_ALLOCATIONSFAILEDDELIBERATELY, RunTimeVerifierData.AllocationsFailedDeliberately) ) ||
  1285. ( ! VrfFTPrintfResourceFormat( file, IDS_TRIMS, RunTimeVerifierData.Trims) ) ||
  1286. ( ! VrfFTPrintfResourceFormat( file, IDS_UNTRACKEDPOOL, RunTimeVerifierData.UnTrackedPool) ) )
  1287. {
  1288. bSuccess = FALSE;
  1289. goto Done;
  1290. }
  1291. //
  1292. // per driver counters
  1293. //
  1294. if( ! VrfOuputStringFromResources(
  1295. IDS_THE_VERIFIED_DRIVERS,
  1296. file ) )
  1297. {
  1298. bSuccess = FALSE;
  1299. goto Done;
  1300. }
  1301. for( nCrtDriver = 0; nCrtDriver < nDriversNo; nCrtDriver += 1 )
  1302. {
  1303. VrfFTPrintf(
  1304. file,
  1305. _T( "\n" ) );
  1306. pRunDriverData = RunTimeVerifierData.m_RuntimeDriverDataArray.GetAt( nCrtDriver ) ;
  1307. ASSERT_VALID( pRunDriverData );
  1308. if( VrfFTPrintfResourceFormat(
  1309. file,
  1310. IDS_NAME_LOADS_UNLOADS,
  1311. (LPCTSTR)pRunDriverData->m_strName,
  1312. pRunDriverData->Loads,
  1313. pRunDriverData->Unloads) == FALSE )
  1314. {
  1315. bSuccess = FALSE;
  1316. goto Done;
  1317. }
  1318. //
  1319. // pool statistics
  1320. //
  1321. if( ( ! VrfFTPrintfResourceFormat( file, IDS_CURRENTPAGEDPOOLALLOCATIONS, pRunDriverData->CurrentPagedPoolAllocations) ) ||
  1322. ( ! VrfFTPrintfResourceFormat( file, IDS_CURRENTNONPAGEDPOOLALLOCATIONS, pRunDriverData->CurrentNonPagedPoolAllocations) ) ||
  1323. ( ! VrfFTPrintfResourceFormat( file, IDS_PEAKPAGEDPOOLALLOCATIONS, pRunDriverData->PeakPagedPoolAllocations) ) ||
  1324. ( ! VrfFTPrintfResourceFormat( file, IDS_PEAKNONPAGEDPOOLALLOCATIONS, pRunDriverData->PeakNonPagedPoolAllocations) ) ||
  1325. ( ! VrfFTPrintfResourceFormat( file, IDS_PAGEDPOOLUSAGEINBYTES, (ULONG) pRunDriverData->PagedPoolUsageInBytes) ) ||
  1326. ( ! VrfFTPrintfResourceFormat( file, IDS_NONPAGEDPOOLUSAGEINBYTES, (ULONG) pRunDriverData->NonPagedPoolUsageInBytes) ) ||
  1327. ( ! VrfFTPrintfResourceFormat( file, IDS_PEAKPAGEDPOOLUSAGEINBYTES, (ULONG) pRunDriverData->PeakPagedPoolUsageInBytes) ) ||
  1328. ( ! VrfFTPrintfResourceFormat( file, IDS_PEAKNONPAGEDPOOLUSAGEINBYTES, (ULONG) pRunDriverData->PeakNonPagedPoolUsageInBytes) ) )
  1329. {
  1330. bSuccess = FALSE;
  1331. goto Done;
  1332. }
  1333. }
  1334. }
  1335. Done:
  1336. return bSuccess;
  1337. }
  1338. /////////////////////////////////////////////////////////////////////////////
  1339. BOOL __cdecl VrfFTPrintf( FILE *file,
  1340. LPCTSTR szFormat,
  1341. ... )
  1342. {
  1343. TCHAR szMessage[ 256 ];
  1344. BOOL bResult;
  1345. va_list prms;
  1346. ASSERT( NULL != file );
  1347. ASSERT( g_bCommandLineMode );
  1348. va_start (prms, szFormat);
  1349. //
  1350. // Format the message in our local buffer
  1351. //
  1352. _vsntprintf ( szMessage,
  1353. ARRAY_LENGTH( szMessage ),
  1354. szFormat,
  1355. prms );
  1356. bResult = ( _fputts( szMessage, file ) >= 0 );
  1357. va_end (prms);
  1358. return bResult;
  1359. }
  1360. /////////////////////////////////////////////////////////////////////////////
  1361. BOOL __cdecl VrfFTPrintfResourceFormat( FILE *file,
  1362. UINT uIdResourceFormat,
  1363. ... )
  1364. {
  1365. TCHAR szMessage[ 256 ];
  1366. TCHAR strFormat[ 256 ];
  1367. BOOL bResult;
  1368. va_list prms;
  1369. ASSERT( NULL != file );
  1370. //
  1371. // Load the format string from the resources
  1372. //
  1373. bResult = VrfLoadString( uIdResourceFormat,
  1374. strFormat,
  1375. ARRAY_LENGTH( strFormat ) );
  1376. ASSERT( bResult );
  1377. if( bResult )
  1378. {
  1379. va_start (prms, uIdResourceFormat);
  1380. //
  1381. // Format the message in our local buffer
  1382. //
  1383. _vsntprintf ( szMessage,
  1384. ARRAY_LENGTH( szMessage ),
  1385. strFormat,
  1386. prms);
  1387. bResult = ( _fputts( szMessage, file ) >= 0 );
  1388. va_end (prms);
  1389. }
  1390. return bResult;
  1391. }
  1392. /////////////////////////////////////////////////////////////////////////////
  1393. BOOL VrfOuputStringFromResources( UINT uIdString,
  1394. FILE *file )
  1395. {
  1396. TCHAR szText[ 256 ];
  1397. BOOL bResult;
  1398. ASSERT( NULL != file );
  1399. bResult = VrfLoadString( uIdString,
  1400. szText,
  1401. ARRAY_LENGTH( szText ) );
  1402. if( FALSE == bResult )
  1403. {
  1404. goto Done;
  1405. }
  1406. bResult = ( _fputts( szText, file ) >= 0 );
  1407. Done:
  1408. return bResult;
  1409. }
  1410. /////////////////////////////////////////////////////////////////////////////
  1411. BOOL VrfSetNewFlagsVolatile( DWORD dwNewFlags )
  1412. {
  1413. BOOL bResult;
  1414. NTSTATUS Status;
  1415. INT_PTR nCurrentlyVerifiedDrivers;
  1416. CRuntimeVerifierData RunTimeVerifierData;
  1417. bResult = TRUE;
  1418. nCurrentlyVerifiedDrivers = 0;
  1419. if( VrfGetRuntimeVerifierData( &RunTimeVerifierData ) == FALSE )
  1420. {
  1421. //
  1422. // Cannot get current verifier settings
  1423. //
  1424. VrfErrorResourceFormat( IDS_CANTGET_VERIF_STATE );
  1425. bResult = FALSE;
  1426. goto Done;
  1427. }
  1428. nCurrentlyVerifiedDrivers = RunTimeVerifierData.m_RuntimeDriverDataArray.GetSize();
  1429. if( nCurrentlyVerifiedDrivers > 0 )
  1430. {
  1431. //
  1432. // There are some drivers currently verified
  1433. //
  1434. if( RunTimeVerifierData.Level != dwNewFlags )
  1435. {
  1436. //
  1437. // Just use NtSetSystemInformation to set the flags
  1438. // that can be modified on the fly. Don't write anything to the registry.
  1439. //
  1440. //
  1441. // Enable debug privilege
  1442. //
  1443. if( g_bPrivegeEnabled != TRUE )
  1444. {
  1445. g_bPrivegeEnabled = VrfEnableDebugPrivilege();
  1446. if( g_bPrivegeEnabled != TRUE )
  1447. {
  1448. bResult = FALSE;
  1449. goto Done;
  1450. }
  1451. }
  1452. //
  1453. // Set the new flags
  1454. //
  1455. Status = NtSetSystemInformation(
  1456. SystemVerifierInformation,
  1457. &dwNewFlags,
  1458. sizeof( dwNewFlags ) );
  1459. if( ! NT_SUCCESS( Status ) )
  1460. {
  1461. if( Status == STATUS_ACCESS_DENIED )
  1462. {
  1463. //
  1464. // Access denied
  1465. //
  1466. VrfErrorResourceFormat(
  1467. IDS_ACCESS_IS_DENIED );
  1468. }
  1469. else
  1470. {
  1471. //
  1472. // Some other error
  1473. //
  1474. VrfErrorResourceFormat(
  1475. IDS_CANNOT_CHANGE_SETTING_ON_FLY );
  1476. }
  1477. bResult = FALSE;
  1478. goto Done;
  1479. }
  1480. }
  1481. }
  1482. Done:
  1483. if( g_bCommandLineMode )
  1484. {
  1485. VrfDumpChangedSettings( RunTimeVerifierData.Level,
  1486. dwNewFlags,
  1487. nCurrentlyVerifiedDrivers );
  1488. }
  1489. return bResult;
  1490. }
  1491. /////////////////////////////////////////////////////////////////////////////
  1492. BOOL VrfAddDriversVolatile( const CStringArray &astrNewDrivers )
  1493. {
  1494. BOOL bSuccess;
  1495. INT_PTR nDrivers;
  1496. INT_PTR nCrtDriver;
  1497. CString strCrtDriver;
  1498. bSuccess = TRUE;
  1499. nDrivers = astrNewDrivers.GetSize();
  1500. for( nCrtDriver = 0; nCrtDriver < nDrivers; nCrtDriver += 1 )
  1501. {
  1502. strCrtDriver = astrNewDrivers.GetAt( nCrtDriver );
  1503. if( TRUE != VrfAddDriverVolatile( strCrtDriver ) )
  1504. {
  1505. bSuccess = FALSE;
  1506. }
  1507. }
  1508. return bSuccess;
  1509. }
  1510. /////////////////////////////////////////////////////////////////////////////
  1511. BOOL VrfAddDriverVolatile( const CString &strCrtDriver )
  1512. {
  1513. NTSTATUS Status;
  1514. UINT uIdErrorString;
  1515. BOOL bSuccess;
  1516. UNICODE_STRING usDriverName;
  1517. #ifndef UNICODE
  1518. WCHAR *szUnicodeName = NULL;
  1519. INT_PTR nNameLength;
  1520. #endif //#ifndef UNICODE
  1521. bSuccess = TRUE;
  1522. //
  1523. // Enable debug privilege
  1524. //
  1525. if( g_bPrivegeEnabled != TRUE )
  1526. {
  1527. g_bPrivegeEnabled = VrfEnableDebugPrivilege();
  1528. if( g_bPrivegeEnabled != TRUE )
  1529. {
  1530. bSuccess = FALSE;
  1531. goto Done;
  1532. }
  1533. }
  1534. //
  1535. // Must have driver name as a UNICODE_STRING
  1536. //
  1537. #ifdef UNICODE
  1538. //
  1539. // UNICODE
  1540. //
  1541. RtlInitUnicodeString(
  1542. &usDriverName,
  1543. (LPCTSTR) strCrtDriver );
  1544. #else
  1545. //
  1546. // ANSI
  1547. //
  1548. nNameLength = strCrtDriver.GetLength();
  1549. szUnicodeName = new WCHAR[ nNameLength + 1 ];
  1550. if( NULL == szUnicodeName )
  1551. {
  1552. VrfErrorResourceFormat( IDS_NOT_ENOUGH_MEMORY );
  1553. bSuccess = FALSE;
  1554. goto Done;
  1555. }
  1556. MultiByteToWideChar( CP_ACP,
  1557. 0,
  1558. (LPCSTR) strCrtDriver,
  1559. -1,
  1560. szUnicodeName,
  1561. nNameLength + 1 );
  1562. RtlInitUnicodeString(
  1563. &usDriverName,
  1564. szUnicodeName );
  1565. #endif //#ifdef UNICODE
  1566. Status = NtSetSystemInformation(
  1567. SystemVerifierAddDriverInformation,
  1568. &usDriverName,
  1569. sizeof( UNICODE_STRING ) );
  1570. if( ! NT_SUCCESS( Status ) )
  1571. {
  1572. switch( Status )
  1573. {
  1574. case STATUS_INVALID_INFO_CLASS:
  1575. uIdErrorString = IDS_VERIFIER_ADD_NOT_SUPPORTED;
  1576. break;
  1577. case STATUS_NOT_SUPPORTED:
  1578. uIdErrorString = IDS_DYN_ADD_NOT_SUPPORTED;
  1579. break;
  1580. case STATUS_IMAGE_ALREADY_LOADED:
  1581. uIdErrorString = IDS_DYN_ADD_ALREADY_LOADED;
  1582. break;
  1583. case STATUS_INSUFFICIENT_RESOURCES:
  1584. case STATUS_NO_MEMORY:
  1585. uIdErrorString = IDS_DYN_ADD_INSUF_RESOURCES;
  1586. break;
  1587. case STATUS_PRIVILEGE_NOT_HELD:
  1588. uIdErrorString = IDS_DYN_ADD_ACCESS_DENIED;
  1589. break;
  1590. default:
  1591. VrfErrorResourceFormat(
  1592. IDS_DYN_ADD_MISC_ERROR,
  1593. (LPCTSTR) strCrtDriver,
  1594. Status );
  1595. bSuccess = FALSE;
  1596. }
  1597. VrfErrorResourceFormat(
  1598. uIdErrorString,
  1599. (LPCTSTR) strCrtDriver );
  1600. bSuccess = FALSE;
  1601. }
  1602. #ifndef UNICODE
  1603. if( NULL != szUnicodeName )
  1604. {
  1605. delete [] szUnicodeName;
  1606. }
  1607. #endif //#ifndef UNICODE
  1608. Done:
  1609. return bSuccess;
  1610. }
  1611. /////////////////////////////////////////////////////////////////////////////
  1612. BOOL VrfRemoveDriversVolatile( const CStringArray &astrNewDrivers )
  1613. {
  1614. INT_PTR nDrivers;
  1615. INT_PTR nCrtDriver;
  1616. BOOL bSuccess;
  1617. CString strCrtDriver;
  1618. bSuccess = TRUE;
  1619. nDrivers = astrNewDrivers.GetSize();
  1620. for( nCrtDriver = 0; nCrtDriver < nDrivers; nCrtDriver += 1 )
  1621. {
  1622. strCrtDriver = astrNewDrivers.GetAt( nCrtDriver );
  1623. if( TRUE != VrfRemoveDriverVolatile( strCrtDriver ) )
  1624. {
  1625. bSuccess = FALSE;
  1626. }
  1627. }
  1628. return bSuccess;
  1629. }
  1630. /////////////////////////////////////////////////////////////////////////////
  1631. BOOL VrfRemoveDriverVolatile( const CString &strDriverName )
  1632. {
  1633. NTSTATUS Status;
  1634. UINT uIdErrorString;
  1635. BOOL bSuccess;
  1636. UNICODE_STRING usDriverName;
  1637. #ifndef UNICODE
  1638. WCHAR *szUnicodeName = NULL;
  1639. INT_PTR nNameLength;
  1640. #endif //#ifndef UNICODE
  1641. bSuccess = TRUE;
  1642. //
  1643. // Enable debug privilege
  1644. //
  1645. if( g_bPrivegeEnabled != TRUE )
  1646. {
  1647. g_bPrivegeEnabled = VrfEnableDebugPrivilege();
  1648. if( g_bPrivegeEnabled != TRUE )
  1649. {
  1650. bSuccess = FALSE;
  1651. goto Done;
  1652. }
  1653. }
  1654. //
  1655. // Must have driver name as a UNICODE_STRING
  1656. //
  1657. #ifdef UNICODE
  1658. //
  1659. // UNICODE
  1660. //
  1661. RtlInitUnicodeString(
  1662. &usDriverName,
  1663. (LPCTSTR) strDriverName );
  1664. #else
  1665. //
  1666. // ANSI
  1667. //
  1668. nNameLength = strDriverName.GetLength();
  1669. szUnicodeName = new WCHAR[ nNameLength + 1 ];
  1670. if( NULL == szUnicodeName )
  1671. {
  1672. VrfErrorResourceFormat( IDS_NOT_ENOUGH_MEMORY );
  1673. bSuccess = FALSE;
  1674. goto Done;
  1675. }
  1676. MultiByteToWideChar( CP_ACP,
  1677. 0,
  1678. (LPCSTR) strDriverName,
  1679. -1,
  1680. szUnicodeName,
  1681. nNameLength + 1 );
  1682. RtlInitUnicodeString(
  1683. &usDriverName,
  1684. szUnicodeName );
  1685. #endif //#ifdef UNICODE
  1686. Status = NtSetSystemInformation(
  1687. SystemVerifierRemoveDriverInformation,
  1688. &usDriverName,
  1689. sizeof( UNICODE_STRING ) );
  1690. if( ! NT_SUCCESS( Status ) )
  1691. {
  1692. switch( Status )
  1693. {
  1694. case STATUS_INVALID_INFO_CLASS:
  1695. uIdErrorString = IDS_VERIFIER_REMOVE_NOT_SUPPORTED;
  1696. break;
  1697. case STATUS_NOT_SUPPORTED:
  1698. //
  1699. // the driver verifier is not currently active at all -> success
  1700. //
  1701. case STATUS_NOT_FOUND:
  1702. //
  1703. // the driver is not currently verified -> success
  1704. //
  1705. return TRUE;
  1706. case STATUS_IMAGE_ALREADY_LOADED:
  1707. uIdErrorString = IDS_DYN_REMOVE_ALREADY_LOADED;
  1708. break;
  1709. case STATUS_INSUFFICIENT_RESOURCES:
  1710. case STATUS_NO_MEMORY:
  1711. uIdErrorString = IDS_DYN_REMOVE_INSUF_RESOURCES;
  1712. break;
  1713. case STATUS_PRIVILEGE_NOT_HELD:
  1714. uIdErrorString = IDS_DYN_REMOVE_ACCESS_DENIED;
  1715. break;
  1716. default:
  1717. VrfErrorResourceFormat(
  1718. IDS_DYN_REMOVE_MISC_ERROR,
  1719. (LPCTSTR) strDriverName,
  1720. Status );
  1721. bSuccess = FALSE;
  1722. }
  1723. VrfErrorResourceFormat(
  1724. uIdErrorString,
  1725. (LPCTSTR) strDriverName );
  1726. bSuccess = FALSE;
  1727. }
  1728. Done:
  1729. #ifndef UNICODE
  1730. if( NULL != szUnicodeName )
  1731. {
  1732. delete [] szUnicodeName;
  1733. }
  1734. #endif //#ifndef UNICODE
  1735. return bSuccess;
  1736. }
  1737. //////////////////////////////////////////////////////////////////////
  1738. BOOL VrfEnableDebugPrivilege( )
  1739. {
  1740. struct
  1741. {
  1742. DWORD Count;
  1743. LUID_AND_ATTRIBUTES Privilege [1];
  1744. } Info;
  1745. HANDLE Token;
  1746. BOOL Result;
  1747. //
  1748. // Open the process token
  1749. //
  1750. Result = OpenProcessToken (
  1751. GetCurrentProcess (),
  1752. TOKEN_ADJUST_PRIVILEGES,
  1753. & Token);
  1754. if( Result != TRUE )
  1755. {
  1756. VrfErrorResourceFormat(
  1757. IDS_ACCESS_IS_DENIED );
  1758. return FALSE;
  1759. }
  1760. //
  1761. // Prepare the info structure
  1762. //
  1763. Info.Count = 1;
  1764. Info.Privilege[0].Attributes = SE_PRIVILEGE_ENABLED;
  1765. Result = LookupPrivilegeValue (
  1766. NULL,
  1767. SE_DEBUG_NAME,
  1768. &(Info.Privilege[0].Luid));
  1769. if( Result != TRUE )
  1770. {
  1771. VrfErrorResourceFormat(
  1772. IDS_ACCESS_IS_DENIED );
  1773. CloseHandle( Token );
  1774. return FALSE;
  1775. }
  1776. //
  1777. // Adjust the privileges
  1778. //
  1779. Result = AdjustTokenPrivileges (
  1780. Token,
  1781. FALSE,
  1782. (PTOKEN_PRIVILEGES) &Info,
  1783. NULL,
  1784. NULL,
  1785. NULL);
  1786. if( Result != TRUE || GetLastError() != ERROR_SUCCESS )
  1787. {
  1788. VrfErrorResourceFormat(
  1789. IDS_ACCESS_IS_DENIED );
  1790. CloseHandle( Token );
  1791. return FALSE;
  1792. }
  1793. CloseHandle( Token );
  1794. return TRUE;
  1795. }
  1796. //////////////////////////////////////////////////////////////////////
  1797. VOID VrfDumpChangedSettings( UINT OldFlags,
  1798. UINT NewFlags,
  1799. INT_PTR nDriversVerified )
  1800. {
  1801. UINT uDifferentFlags;
  1802. if( nDriversVerified == 0 )
  1803. {
  1804. VrfPrintStringFromResources(
  1805. IDS_NO_DRIVER_VERIFIED );
  1806. goto Done;
  1807. }
  1808. if( OldFlags == NewFlags )
  1809. {
  1810. //
  1811. // no settings were changed
  1812. //
  1813. VrfPrintStringFromResources(
  1814. IDS_NO_SETTINGS_WERE_CHANGED );
  1815. }
  1816. else
  1817. {
  1818. VrfPrintStringFromResources(
  1819. IDS_CHANGED_SETTINGS_ARE );
  1820. uDifferentFlags = OldFlags ^ NewFlags;
  1821. //
  1822. // changed DRIVER_VERIFIER_SPECIAL_POOLING ?
  1823. //
  1824. if( uDifferentFlags & DRIVER_VERIFIER_SPECIAL_POOLING )
  1825. {
  1826. if( NewFlags & DRIVER_VERIFIER_SPECIAL_POOLING )
  1827. {
  1828. VrfPrintStringFromResources(
  1829. IDS_SPECIAL_POOL_ENABLED_NOW );
  1830. }
  1831. else
  1832. {
  1833. VrfPrintStringFromResources(
  1834. IDS_SPECIAL_POOL_DISABLED_NOW );
  1835. }
  1836. }
  1837. //
  1838. // changed DRIVER_VERIFIER_FORCE_IRQL_CHECKING ?
  1839. //
  1840. if( uDifferentFlags & DRIVER_VERIFIER_FORCE_IRQL_CHECKING )
  1841. {
  1842. if( NewFlags & DRIVER_VERIFIER_FORCE_IRQL_CHECKING )
  1843. {
  1844. VrfPrintStringFromResources(
  1845. IDS_FORCE_IRQLCHECK_ENABLED_NOW );
  1846. }
  1847. else
  1848. {
  1849. VrfPrintStringFromResources(
  1850. IDS_FORCE_IRQLCHECK_DISABLED_NOW );
  1851. }
  1852. }
  1853. //
  1854. // changed DRIVER_VERIFIER_INJECT_ALLOCATION_FAILURES ?
  1855. //
  1856. if( uDifferentFlags & DRIVER_VERIFIER_INJECT_ALLOCATION_FAILURES )
  1857. {
  1858. if( NewFlags & DRIVER_VERIFIER_INJECT_ALLOCATION_FAILURES )
  1859. {
  1860. VrfPrintStringFromResources(
  1861. IDS_FAULT_INJECTION_ENABLED_NOW );
  1862. }
  1863. else
  1864. {
  1865. VrfPrintStringFromResources(
  1866. IDS_FAULT_INJECTION_DISABLED_NOW );
  1867. }
  1868. }
  1869. //
  1870. // changed DRIVER_VERIFIER_TRACK_POOL_ALLOCATIONS ?
  1871. //
  1872. if( uDifferentFlags & DRIVER_VERIFIER_TRACK_POOL_ALLOCATIONS )
  1873. {
  1874. if( NewFlags & DRIVER_VERIFIER_TRACK_POOL_ALLOCATIONS )
  1875. {
  1876. VrfPrintStringFromResources(
  1877. IDS_POOL_TRACK_ENABLED_NOW );
  1878. }
  1879. else
  1880. {
  1881. VrfPrintStringFromResources(
  1882. IDS_POOL_TRACK_DISABLED_NOW );
  1883. }
  1884. }
  1885. //
  1886. // changed DRIVER_VERIFIER_IO_CHECKING ?
  1887. //
  1888. if( uDifferentFlags & DRIVER_VERIFIER_IO_CHECKING )
  1889. {
  1890. if( NewFlags & DRIVER_VERIFIER_IO_CHECKING )
  1891. {
  1892. VrfPrintStringFromResources(
  1893. IDS_IO_CHECKING_ENABLED_NOW );
  1894. }
  1895. else
  1896. {
  1897. VrfPrintStringFromResources(
  1898. IDS_IO_CHECKING_DISABLED_NOW );
  1899. }
  1900. }
  1901. //
  1902. // the changes are not saved to the registry
  1903. //
  1904. VrfPrintStringFromResources(
  1905. IDS_CHANGES_ACTIVE_ONLY_BEFORE_REBOOT );
  1906. }
  1907. Done:
  1908. NOTHING;
  1909. }
  1910. /////////////////////////////////////////////////////////////////////////////
  1911. DWORD VrfGetStandardFlags()
  1912. {
  1913. DWORD dwStandardFlags;
  1914. dwStandardFlags = DRIVER_VERIFIER_SPECIAL_POOLING |
  1915. DRIVER_VERIFIER_FORCE_IRQL_CHECKING |
  1916. DRIVER_VERIFIER_TRACK_POOL_ALLOCATIONS |
  1917. DRIVER_VERIFIER_IO_CHECKING |
  1918. DRIVER_VERIFIER_DEADLOCK_DETECTION |
  1919. DRIVER_VERIFIER_DMA_VERIFIER;
  1920. return dwStandardFlags;
  1921. }
  1922. /////////////////////////////////////////////////////////////////////////////
  1923. VOID VrfAddMiniports( CStringArray &astrVerifiedDrivers )
  1924. {
  1925. CStringArray astrToAdd;
  1926. CString strCrtDriver;
  1927. CString strCrtDriverToAdd;
  1928. CString strLinkedDriver;
  1929. INT_PTR nVerifiedDrivers;
  1930. INT_PTR nCrtDriver;
  1931. INT_PTR nDriversToAdd;
  1932. INT_PTR nCrtDriverToAdd;
  1933. nVerifiedDrivers = astrVerifiedDrivers.GetSize();
  1934. for( nCrtDriver = 0; nCrtDriver < nVerifiedDrivers; nCrtDriver += 1 )
  1935. {
  1936. //
  1937. // This will be a verified driver
  1938. //
  1939. strCrtDriver = astrVerifiedDrivers.GetAt( nCrtDriver );
  1940. //
  1941. // Check if it is a miniport driver
  1942. //
  1943. if( VrfIsDriverMiniport( strCrtDriver,
  1944. strLinkedDriver ) )
  1945. {
  1946. //
  1947. // Check if we didn't add already strLinkedDriver
  1948. //
  1949. nDriversToAdd = astrToAdd.GetSize();
  1950. for( nCrtDriverToAdd = 0; nCrtDriverToAdd < nDriversToAdd; nCrtDriverToAdd += 1 )
  1951. {
  1952. strCrtDriverToAdd = astrToAdd.GetAt( nCrtDriverToAdd );
  1953. if( strCrtDriverToAdd.CompareNoCase( strLinkedDriver ) == 0 )
  1954. {
  1955. //
  1956. // We already wanted to add this driver
  1957. //
  1958. break;
  1959. }
  1960. }
  1961. if( nCrtDriverToAdd >= nDriversToAdd )
  1962. {
  1963. //
  1964. // Add this new driver (strLinkedDriver)
  1965. //
  1966. astrToAdd.Add( strLinkedDriver );
  1967. }
  1968. }
  1969. }
  1970. //
  1971. // Flush astrToAdd into astrVerifiedDrivers
  1972. //
  1973. nDriversToAdd = astrToAdd.GetSize();
  1974. for( nCrtDriverToAdd = 0; nCrtDriverToAdd < nDriversToAdd; nCrtDriverToAdd += 1 )
  1975. {
  1976. strCrtDriverToAdd = astrToAdd.GetAt( nCrtDriverToAdd );
  1977. astrVerifiedDrivers.Add( strCrtDriverToAdd );
  1978. }
  1979. }
  1980. /////////////////////////////////////////////////////////////////////////////
  1981. BOOL VrfIsDriverMiniport( CString &strCrtDriver,
  1982. CString &strLinkedDriver )
  1983. {
  1984. //
  1985. // N.B.
  1986. //
  1987. // The imagehlp functions are not multithreading safe
  1988. // (see Whistler bug #88373) so if we want to use them from more than
  1989. // one thread we will have to aquire some critical section before.
  1990. //
  1991. // Currently only one thread is using the imagehlp APIs in this app
  1992. // (CSlowProgressDlg::LoadDriverDataWorkerThread) so we don't need
  1993. // our synchronization.
  1994. //
  1995. LPTSTR szDriverName;
  1996. LPTSTR szDriversDir;
  1997. PLOADED_IMAGE pLoadedImage;
  1998. BOOL bIsMiniport;
  1999. BOOL bUnloaded;
  2000. bIsMiniport = FALSE;
  2001. ASSERT( strCrtDriver.GetLength() > 0 );
  2002. //
  2003. // ImageLoad doesn't know about const pointers so
  2004. // we have to GetBuffer here :-(
  2005. //
  2006. szDriverName = strCrtDriver.GetBuffer( strCrtDriver.GetLength() + 1 );
  2007. if( NULL == szDriverName )
  2008. {
  2009. goto Done;
  2010. }
  2011. szDriversDir = g_strDriversDir.GetBuffer( g_strDriversDir.GetLength() + 1 );
  2012. if( NULL == szDriversDir )
  2013. {
  2014. strCrtDriver.ReleaseBuffer();
  2015. goto Done;
  2016. }
  2017. //
  2018. // Load the image
  2019. //
  2020. pLoadedImage = VrfImageLoad( szDriverName,
  2021. szDriversDir );
  2022. if( NULL == pLoadedImage )
  2023. {
  2024. //
  2025. // Could not load the image from %windir%\system32\drivers
  2026. // Try again from the PATH
  2027. //
  2028. pLoadedImage = VrfImageLoad( szDriverName,
  2029. NULL );
  2030. }
  2031. //
  2032. // Give our string buffers back to MFC
  2033. //
  2034. strCrtDriver.ReleaseBuffer();
  2035. g_strDriversDir.ReleaseBuffer();
  2036. if( NULL == pLoadedImage )
  2037. {
  2038. //
  2039. // We couldn't load this image - bad luck
  2040. //
  2041. TRACE( _T( "ImageLoad failed for %s, error %u\n" ),
  2042. (LPCTSTR) strCrtDriver,
  2043. GetLastError() );
  2044. goto Done;
  2045. }
  2046. //
  2047. // Check if the current driver is a miniport
  2048. //
  2049. bIsMiniport = VrfIsDriverMiniport( pLoadedImage,
  2050. strLinkedDriver );
  2051. //
  2052. // Clean-up
  2053. //
  2054. bUnloaded = ImageUnload( pLoadedImage );
  2055. //
  2056. // If ImageUnload fails we cannot do much about it...
  2057. //
  2058. ASSERT( bUnloaded );
  2059. Done:
  2060. return bIsMiniport;
  2061. }
  2062. /////////////////////////////////////////////////////////////////////////////
  2063. LPSTR g_szSpecialDrivers[] =
  2064. {
  2065. "videoprt.sys",
  2066. "scsiport.sys"
  2067. };
  2068. BOOL VrfpLookForAllImportDescriptors( PIMAGE_IMPORT_DESCRIPTOR pImportDescriptor,
  2069. ULONG_PTR uVACorrection,
  2070. CString &strLinkedAgainst )
  2071. {
  2072. PIMAGE_IMPORT_DESCRIPTOR pCurrentDescriptor;
  2073. PCHAR pCrtName;
  2074. ULONG uCrtSpecialDriver;
  2075. BOOL bIsMiniport;
  2076. #ifdef UNICODE
  2077. //
  2078. // UNICODE
  2079. //
  2080. INT nCrtStringLength;
  2081. PWSTR szMiniportName;
  2082. #endif //#ifdef UNICODE
  2083. bIsMiniport = FALSE;
  2084. for( uCrtSpecialDriver = 0; ! bIsMiniport && uCrtSpecialDriver < ARRAY_LENGTH( g_szSpecialDrivers ); uCrtSpecialDriver += 1 )
  2085. {
  2086. pCurrentDescriptor = pImportDescriptor;
  2087. while( pCurrentDescriptor->Characteristics != NULL )
  2088. {
  2089. pCrtName = (PCHAR)UlongToPtr( pCurrentDescriptor->Name ) + uVACorrection;
  2090. if( lstrcmpiA( g_szSpecialDrivers[ uCrtSpecialDriver ] , pCrtName ) == 0 )
  2091. {
  2092. //
  2093. // This is a miniport
  2094. //
  2095. #ifndef UNICODE
  2096. //
  2097. // ANSI
  2098. //
  2099. strLinkedAgainst = g_szSpecialDrivers[ uCrtSpecialDriver ];
  2100. #else
  2101. //
  2102. // UNICODE
  2103. //
  2104. nCrtStringLength = strlen( g_szSpecialDrivers[ uCrtSpecialDriver ] );
  2105. szMiniportName = strLinkedAgainst.GetBuffer( nCrtStringLength + 1 );
  2106. if( NULL != szMiniportName )
  2107. {
  2108. MultiByteToWideChar( CP_ACP,
  2109. 0,
  2110. g_szSpecialDrivers[ uCrtSpecialDriver ],
  2111. -1,
  2112. szMiniportName,
  2113. ( nCrtStringLength + 1 ) * sizeof( TCHAR ) );
  2114. strLinkedAgainst.ReleaseBuffer();
  2115. }
  2116. #endif
  2117. bIsMiniport = TRUE;
  2118. break;
  2119. }
  2120. pCurrentDescriptor += 1;
  2121. }
  2122. }
  2123. return bIsMiniport;
  2124. }
  2125. /////////////////////////////////////////////////////////////////////////////
  2126. BOOL VrfIsDriverMiniport( PLOADED_IMAGE pLoadedImage,
  2127. CString &strLinkedDriver )
  2128. {
  2129. PIMAGE_IMPORT_DESCRIPTOR pImportDescriptor;
  2130. PIMAGE_SECTION_HEADER pSectionHeader;
  2131. ULONG_PTR uVACorrection;
  2132. ULONG uDataSize;
  2133. BOOL bIsMiniport;
  2134. bIsMiniport = FALSE;
  2135. //
  2136. // We are protecting ourselves against corrupted binaries
  2137. // with this exception handler
  2138. //
  2139. try
  2140. {
  2141. pSectionHeader = NULL;
  2142. pImportDescriptor = (PIMAGE_IMPORT_DESCRIPTOR)ImageDirectoryEntryToDataEx(
  2143. pLoadedImage->MappedAddress,
  2144. FALSE,
  2145. IMAGE_DIRECTORY_ENTRY_IMPORT,
  2146. &uDataSize,
  2147. &pSectionHeader );
  2148. if( NULL == pSectionHeader )
  2149. {
  2150. goto Done;
  2151. }
  2152. uVACorrection = (ULONG_PTR) pLoadedImage->MappedAddress +
  2153. pSectionHeader->PointerToRawData -
  2154. pSectionHeader->VirtualAddress;
  2155. bIsMiniport = VrfpLookForAllImportDescriptors( pImportDescriptor,
  2156. uVACorrection,
  2157. strLinkedDriver );
  2158. #ifdef _DEBUG
  2159. if( bIsMiniport )
  2160. {
  2161. TRACE( _T( "%s will be auto-enabled\n" ),
  2162. (LPCTSTR) strLinkedDriver );
  2163. }
  2164. #endif //#ifdef DEBUG
  2165. }
  2166. catch( ... )
  2167. {
  2168. TRACE( _T( "VrfIsDriverMiniport: Caught exception\n" ) );
  2169. }
  2170. Done:
  2171. return bIsMiniport;
  2172. }
  2173. /////////////////////////////////////////////////////////////////////////////
  2174. VOID VrfpDumpSettingToConsole( ULONG uIdResourceString,
  2175. BOOL bEnabled )
  2176. {
  2177. CString strTitle;
  2178. CString strEnabled;
  2179. ULONG uIdEnabledString;
  2180. TCHAR szBigBuffer[ 128 ];
  2181. VERIFY( VrfLoadString( uIdResourceString, strTitle ) );
  2182. if( FALSE == bEnabled )
  2183. {
  2184. uIdEnabledString = IDS_DISABLED;
  2185. }
  2186. else
  2187. {
  2188. uIdEnabledString = IDS_ENABLED;
  2189. }
  2190. VERIFY( VrfLoadString( uIdEnabledString, strEnabled ) );
  2191. _sntprintf( szBigBuffer,
  2192. ARRAY_LENGTH( szBigBuffer ),
  2193. _T( "%s: %s" ),
  2194. (LPCTSTR) strTitle,
  2195. (LPCTSTR) strEnabled );
  2196. szBigBuffer[ ARRAY_LENGTH( szBigBuffer ) - 1 ] = 0;
  2197. _putts( szBigBuffer );
  2198. }
  2199. /////////////////////////////////////////////////////////////////////////////
  2200. VOID VrfDumpRegistrySettingsToConsole()
  2201. {
  2202. BOOL bLoaded;
  2203. BOOL bAllDriversVerified;
  2204. DWORD dwVerifyFlags;
  2205. INT_PTR nDrivers;
  2206. INT_PTR nCrtDriver;
  2207. CString strCrtDriver;
  2208. CStringArray astrDriversToVerify;
  2209. bLoaded = VrtLoadCurrentRegistrySettings( bAllDriversVerified,
  2210. astrDriversToVerify,
  2211. dwVerifyFlags );
  2212. if( FALSE != bLoaded )
  2213. {
  2214. VrfpDumpSettingToConsole( IDS_SPECIAL_POOL, ( dwVerifyFlags & DRIVER_VERIFIER_SPECIAL_POOLING ) != 0 );
  2215. VrfpDumpSettingToConsole( IDS_FORCE_IRQL_CHECKING, ( dwVerifyFlags & DRIVER_VERIFIER_FORCE_IRQL_CHECKING ) != 0 );
  2216. VrfpDumpSettingToConsole( IDS_LOW_RESOURCE_SIMULATION, ( dwVerifyFlags & DRIVER_VERIFIER_INJECT_ALLOCATION_FAILURES ) != 0 );
  2217. VrfpDumpSettingToConsole( IDS_POOL_TRACKING, ( dwVerifyFlags & DRIVER_VERIFIER_TRACK_POOL_ALLOCATIONS ) != 0 );
  2218. VrfpDumpSettingToConsole( IDS_IO_VERIFICATION, ( dwVerifyFlags & DRIVER_VERIFIER_IO_CHECKING ) != 0 );
  2219. VrfpDumpSettingToConsole( IDS_DEADLOCK_DETECTION, ( dwVerifyFlags & DRIVER_VERIFIER_DEADLOCK_DETECTION ) != 0 );
  2220. VrfpDumpSettingToConsole( IDS_ENH_IO_VERIFICATION, ( dwVerifyFlags & DRIVER_VERIFIER_ENHANCED_IO_CHECKING ) != 0 );
  2221. VrfpDumpSettingToConsole( IDS_DMA_CHECHKING, ( dwVerifyFlags & DRIVER_VERIFIER_DMA_VERIFIER ) != 0 );
  2222. VrfPrintStringFromResources( IDS_VERIFIED_DRIVERS );
  2223. if( bAllDriversVerified )
  2224. {
  2225. VrfPrintStringFromResources( IDS_ALL );
  2226. }
  2227. else
  2228. {
  2229. nDrivers = astrDriversToVerify.GetSize();
  2230. if( nDrivers > 0 )
  2231. {
  2232. for( nCrtDriver = 0; nCrtDriver < nDrivers; nCrtDriver += 1 )
  2233. {
  2234. strCrtDriver = astrDriversToVerify.GetAt( nCrtDriver );
  2235. _putts( (LPCTSTR) strCrtDriver );
  2236. }
  2237. }
  2238. else
  2239. {
  2240. VrfPrintStringFromResources( IDS_NONE );
  2241. }
  2242. }
  2243. }
  2244. }
  2245. /////////////////////////////////////////////////////////////////////////////
  2246. BOOL VrfIsNameAlreadyInList( LPCTSTR szDriver,
  2247. LPCTSTR szAllDrivers )
  2248. {
  2249. INT nNameLength;
  2250. INT nLastIndex;
  2251. INT nIndex;
  2252. BOOL bFoundIt;
  2253. CString strDriver( szDriver );
  2254. CString strAllDrivers( szAllDrivers );
  2255. bFoundIt = FALSE;
  2256. strDriver.MakeLower();
  2257. strAllDrivers.MakeLower();
  2258. nNameLength = strDriver.GetLength();
  2259. nLastIndex = 0;
  2260. do
  2261. {
  2262. nIndex = strAllDrivers.Find( (LPCTSTR)strDriver, nLastIndex );
  2263. if( nIndex >= 0 )
  2264. {
  2265. //
  2266. // Found the substring. Verify it is separated of spaces, etc.
  2267. //
  2268. if( (nIndex == 0 || _T( ' ' ) == strAllDrivers[ nIndex - 1 ]) &&
  2269. ( (TCHAR)0 == strAllDrivers[ nNameLength + nIndex ] || _T( ' ' ) == strAllDrivers[ nNameLength + nIndex ]) )
  2270. {
  2271. //
  2272. // This is our driver.
  2273. //
  2274. bFoundIt = TRUE;
  2275. break;
  2276. }
  2277. else
  2278. {
  2279. //
  2280. // Continue searching.
  2281. //
  2282. nLastIndex = nIndex + 1;
  2283. }
  2284. }
  2285. }
  2286. while( nIndex >= 0 );
  2287. return bFoundIt;
  2288. }
  2289. /////////////////////////////////////////////////////////////////////////////
  2290. VOID VrfAddDriverNameNoDuplicates( LPCTSTR szDriver,
  2291. CString &strAllDrivers )
  2292. {
  2293. if( FALSE == VrfIsNameAlreadyInList( szDriver,
  2294. strAllDrivers ) )
  2295. {
  2296. if( strAllDrivers.GetLength() > 0 )
  2297. {
  2298. strAllDrivers += _T( ' ' );
  2299. }
  2300. strAllDrivers += szDriver;
  2301. }
  2302. }
  2303. /////////////////////////////////////////////////////////////////////////////
  2304. BOOL VrfIsStringInArray( LPCTSTR szText,
  2305. const CStringArray &astrAllTexts )
  2306. {
  2307. INT_PTR nTexts;
  2308. BOOL bFound;
  2309. bFound = FALSE;
  2310. nTexts = astrAllTexts.GetSize();
  2311. while( nTexts > 0 )
  2312. {
  2313. nTexts -= 1;
  2314. if( 0 == astrAllTexts.GetAt( nTexts ).CompareNoCase( szText ) )
  2315. {
  2316. bFound = TRUE;
  2317. break;
  2318. }
  2319. }
  2320. return bFound;
  2321. }