@@ -2640,10 +2640,27 @@ namespace VeraCrypt
2640
2640
}
2641
2641
2642
2642
void EfiBoot::DeleteStartExec (uint16 statrtOrderNum, wchar_t * type) {
2643
- SetPrivilege (SE_SYSTEM_ENVIRONMENT_NAME, TRUE );
2643
+ DWORD dwLastError;
2644
+ BOOL bPrivilegesSet = IsPrivilegeEnabled (SE_SYSTEM_ENVIRONMENT_NAME);
2645
+ if (!bPrivilegesSet && !SetPrivilege (SE_SYSTEM_ENVIRONMENT_NAME, TRUE ))
2646
+ {
2647
+ dwLastError = GetLastError ();
2648
+ wchar_t szMsg[128 ];
2649
+ StringCbPrintfW (szMsg, ARRAYSIZE (szMsg), L" Failed to set SE_SYSTEM_ENVIRONMENT_NAME privilege (error code 0x.8X)" , dwLastError);
2650
+ throw ErrorException (szMsg, SRC_POS);
2651
+ }
2644
2652
// Check EFI
2645
2653
if (!IsEfiBoot ()) {
2646
- throw ErrorException (L" can not detect EFI environment" , SRC_POS);
2654
+ dwLastError = GetLastError ();
2655
+ if (dwLastError != ERROR_SUCCESS)
2656
+ {
2657
+ if (!bPrivilegesSet)
2658
+ SetPrivilege (SE_SYSTEM_ENVIRONMENT_NAME, FALSE );
2659
+ // format message to append the error code to the exception message
2660
+ wchar_t szMsg[128 ];
2661
+ StringCbPrintfW (szMsg, ARRAYSIZE (szMsg), L" Failed to detect EFI environment (error code 0x.8X)" , dwLastError);
2662
+ throw ErrorException (szMsg, SRC_POS);
2663
+ }
2647
2664
}
2648
2665
wchar_t varName[256 ];
2649
2666
StringCchPrintfW (varName, ARRAYSIZE (varName), L" %s%04X" , type == NULL ? L" Boot" : type, statrtOrderNum);
@@ -2686,13 +2703,33 @@ namespace VeraCrypt
2686
2703
SetFirmwareEnvironmentVariable (next.c_str (), EfiVarGuid, startOrder, 0 );
2687
2704
}
2688
2705
}
2706
+
2707
+ if (!bPrivilegesSet)
2708
+ SetPrivilege (SE_SYSTEM_ENVIRONMENT_NAME, FALSE );
2689
2709
}
2690
2710
2691
2711
void EfiBoot::SetStartExec (wstring description, wstring execPath, bool setBootEntry, bool forceFirstBootEntry, bool setBootNext, uint16 statrtOrderNum , wchar_t * type, uint32 attr) {
2692
- SetPrivilege (SE_SYSTEM_ENVIRONMENT_NAME, TRUE );
2712
+ DWORD dwLastError;
2713
+ BOOL bPrivilegesSet = IsPrivilegeEnabled (SE_SYSTEM_ENVIRONMENT_NAME);
2714
+ if (!bPrivilegesSet && !SetPrivilege (SE_SYSTEM_ENVIRONMENT_NAME, TRUE ))
2715
+ {
2716
+ dwLastError = GetLastError ();
2717
+ wchar_t szMsg[128 ];
2718
+ StringCbPrintfW (szMsg, ARRAYSIZE (szMsg), L" Failed to set SE_SYSTEM_ENVIRONMENT_NAME privilege (error code 0x.8X)" , dwLastError);
2719
+ throw ErrorException (szMsg, SRC_POS);
2720
+ }
2693
2721
// Check EFI
2694
2722
if (!IsEfiBoot ()) {
2695
- throw ErrorException (L" can not detect EFI environment" , SRC_POS);
2723
+ dwLastError = GetLastError ();
2724
+ if (dwLastError != ERROR_SUCCESS)
2725
+ {
2726
+ if (!bPrivilegesSet)
2727
+ SetPrivilege (SE_SYSTEM_ENVIRONMENT_NAME, FALSE );
2728
+ // format message to append the error code to the exception message
2729
+ wchar_t szMsg[1024 ];
2730
+ StringCbPrintfW (szMsg, ARRAYSIZE (szMsg), L" Failed to detect EFI environment (error code 0x.8X)" , dwLastError);
2731
+ throw ErrorException (szMsg, SRC_POS);
2732
+ }
2696
2733
}
2697
2734
2698
2735
if (bDeviceInfoValid)
@@ -2866,6 +2903,9 @@ namespace VeraCrypt
2866
2903
SetFirmwareEnvironmentVariable (next.c_str (), EfiVarGuid, &statrtOrderNum, 2 );
2867
2904
2868
2905
}
2906
+
2907
+ if (!bPrivilegesSet)
2908
+ SetPrivilege (SE_SYSTEM_ENVIRONMENT_NAME, FALSE );
2869
2909
}
2870
2910
2871
2911
bool EfiBoot::CompareFiles (const wchar_t * fileName1, const wchar_t * fileName2)
0 commit comments