Leaked source code of windows server 2003
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.

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