Explorar o código

CreateSafeDirectory: Give current user SID read access at minimum.

Jordan Russell hai 1 ano
pai
achega
56d98e4efc
Modificáronse 1 ficheiros con 21 adicións e 17 borrados
  1. 21 17
      Projects/Src/InstFunc.pas

+ 21 - 17
Projects/Src/InstFunc.pas

@@ -49,7 +49,7 @@ type
 
 
 function CheckForMutexes(const Mutexes: String): Boolean;
 function CheckForMutexes(const Mutexes: String): Boolean;
 procedure CreateMutexes(const Mutexes: String);
 procedure CreateMutexes(const Mutexes: String);
-function CreateTempDir(const AllowOnlyPrivilegedAccess: Boolean): String;
+function CreateTempDir(const LimitCurrentUserSidAccess: Boolean): String;
 function DecrementSharedCount(const RegView: TRegView; const Filename: String): Boolean;
 function DecrementSharedCount(const RegView: TRegView; const Filename: String): Boolean;
 procedure DelayDeleteFile(const DisableFsRedir: Boolean; const Filename: String;
 procedure DelayDeleteFile(const DisableFsRedir: Boolean; const Filename: String;
   const MaxTries, FirstRetryDelayMS, SubsequentRetryDelayMS: Integer);
   const MaxTries, FirstRetryDelayMS, SubsequentRetryDelayMS: Integer);
@@ -62,7 +62,7 @@ function DetermineDefaultLanguage(const GetLanguageEntryProc: TGetLanguageEntryP
   var ResultIndex: Integer): TDetermineDefaultLanguageResult;
   var ResultIndex: Integer): TDetermineDefaultLanguageResult;
 procedure EnumFileReplaceOperationsFilenames(const EnumFunc: TEnumFROFilenamesProc;
 procedure EnumFileReplaceOperationsFilenames(const EnumFunc: TEnumFROFilenamesProc;
   Param: Pointer);
   Param: Pointer);
-function GenerateNonRandomUniqueTempDir(const AllowOnlyPrivilegedAccess: Boolean;
+function GenerateNonRandomUniqueTempDir(const LimitCurrentUserSidAccess: Boolean;
   Path: String; var TempDir: String): Boolean;
   Path: String; var TempDir: String): Boolean;
 function GenerateUniqueName(const DisableFsRedir: Boolean; Path: String;
 function GenerateUniqueName(const DisableFsRedir: Boolean; Path: String;
   const Extension: String): String;
   const Extension: String): String;
@@ -174,11 +174,11 @@ function ConvertStringSecurityDescriptorToSecurityDescriptorW(
   StringSDRevision: DWORD; var ppSecurityDescriptor: Pointer;
   StringSDRevision: DWORD; var ppSecurityDescriptor: Pointer;
   dummy: Pointer): BOOL; stdcall; external advapi32;
   dummy: Pointer): BOOL; stdcall; external advapi32;
 
 
-function CreateSafeDirectory(const AllowOnlyPrivilegedAccess: Boolean; Path: String;
+function CreateSafeDirectory(const LimitCurrentUserSidAccess: Boolean; Path: String;
   var ErrorCode: DWORD): Boolean;
   var ErrorCode: DWORD): Boolean;
 { Creates a protected directory if
 { Creates a protected directory if
   -it's a subdirectory of c:\WINDOWS\TEMP, or
   -it's a subdirectory of c:\WINDOWS\TEMP, or
-  -it's on a local drive and only priviliged acces is allowed (latter is true atm if elevated and not debugging)
+  -it's on a local drive and LimitCurrentUserSidAccess is True (latter is true atm if elevated and not debugging)
   otherwise creates a normal directory. }
   otherwise creates a normal directory. }
 const
 const
   SDDL_REVISION_1 = 1;
   SDDL_REVISION_1 = 1;
@@ -188,7 +188,7 @@ begin
   var IsUnderWindowsTemp := Pos(PathLowercase(AddBackslash(GetSystemWinDir) + 'TEMP\'),
   var IsUnderWindowsTemp := Pos(PathLowercase(AddBackslash(GetSystemWinDir) + 'TEMP\'),
     PathLowercase(Path)) = 1;
     PathLowercase(Path)) = 1;
   var Drive := PathExtractDrive(Path);
   var Drive := PathExtractDrive(Path);
-  var IsLocalTempToProtect := AllowOnlyPrivilegedAccess and (Drive <> '') and
+  var IsLocalTempToProtect := LimitCurrentUserSidAccess and (Drive <> '') and
     not PathCharIsSlash(Drive[1]) and
     not PathCharIsSlash(Drive[1]) and
     (GetDriveType(PChar(AddBackslash(Drive))) <> DRIVE_REMOTE);
     (GetDriveType(PChar(AddBackslash(Drive))) <> DRIVE_REMOTE);
 
 
@@ -197,21 +197,25 @@ begin
       // D: adds a Discretionary ACL ("DACL", i.e. access control via SIDs)
       // D: adds a Discretionary ACL ("DACL", i.e. access control via SIDs)
       // P: prevents DACL from being modified by inherited ACLs
       // P: prevents DACL from being modified by inherited ACLs
       'D:P';
       'D:P';
-    if not AllowOnlyPrivilegedAccess then begin
-      var CurrentUserSid := GetCurrentUserSid;
-      if CurrentUserSid = '' then
-        CurrentUserSid := 'OW'; // OW: owner rights
+    var CurrentUserSid := GetCurrentUserSid;
+    if CurrentUserSid = '' then
+      CurrentUserSid := 'OW'; // OW: owner rights
+    { Omit the CurrentUserSid ACE if the current user is SYSTEM, because
+      there's already a fixed Full Control ACE for SYSTEM below }
+    if not SameText(CurrentUserSid, 'S-1-5-18') then begin
       // A: "allow"
       // A: "allow"
       // OICI: "object and container inherit",
       // OICI: "object and container inherit",
       //    i.e. files and directories created within the new directory
       //    i.e. files and directories created within the new directory
       //    inherit these permissions
       //    inherit these permissions
-      // 0x001F01FF: corresponds to `FILE_ALL_ACCESS`
+      var AccessRights := 'FA'; // FILE_ALL_ACCESS (Full Control)
+      if LimitCurrentUserSidAccess then
+        AccessRights := 'FRFX'; // FILE_GENERIC_READ | FILE_GENERIC_EXECUTE
       StringSecurityDescriptor := StringSecurityDescriptor +
       StringSecurityDescriptor := StringSecurityDescriptor +
-        '(A;OICI;0x001F01FF;;;' + CurrentUserSid + ')'; // current user
+        '(A;OICI;' + AccessRights + ';;;' + CurrentUserSid + ')'; // current user
     end;
     end;
     StringSecurityDescriptor := StringSecurityDescriptor +
     StringSecurityDescriptor := StringSecurityDescriptor +
-      '(A;OICI;0x001F01FF;;;BA)' + // BA: built-in administrator
-      '(A;OICI;0x001F01FF;;;SY)'; // SY: local SYSTEM account
+      '(A;OICI;FA;;;BA)' + // BA: built-in Administrators group
+      '(A;OICI;FA;;;SY)'; // SY: local SYSTEM account
 
 
     var pSecurityDescriptor: Pointer;
     var pSecurityDescriptor: Pointer;
     if not ConvertStringSecurityDescriptorToSecurityDescriptorW(
     if not ConvertStringSecurityDescriptorToSecurityDescriptorW(
@@ -275,7 +279,7 @@ begin
   Result := Filename;
   Result := Filename;
 end;
 end;
 
 
-function GenerateNonRandomUniqueTempDir(const AllowOnlyPrivilegedAccess: Boolean;
+function GenerateNonRandomUniqueTempDir(const LimitCurrentUserSidAccess: Boolean;
   Path: String; var TempDir: String): Boolean;
   Path: String; var TempDir: String): Boolean;
 { Creates a new temporary directory with a non-random name. Returns True if an
 { Creates a new temporary directory with a non-random name. Returns True if an
   existing directory was re-created. This is called by Uninstall. A non-random
   existing directory was re-created. This is called by Uninstall. A non-random
@@ -305,7 +309,7 @@ begin
     end else if NewFileExists(TempDir) then
     end else if NewFileExists(TempDir) then
       if not DeleteFile(TempDir) then Continue;
       if not DeleteFile(TempDir) then Continue;
 
 
-    if CreateSafeDirectory(AllowOnlyPrivilegedAccess, TempDir, ErrorCode) then Break;
+    if CreateSafeDirectory(LimitCurrentUserSidAccess, TempDir, ErrorCode) then Break;
     if ErrorCode <> ERROR_ALREADY_EXISTS then
     if ErrorCode <> ERROR_ALREADY_EXISTS then
       raise Exception.Create(FmtSetupMessage(msgLastErrorMessage,
       raise Exception.Create(FmtSetupMessage(msgLastErrorMessage,
         [FmtSetupMessage1(msgErrorCreatingDir, TempDir), IntToStr(ErrorCode),
         [FmtSetupMessage1(msgErrorCreatingDir, TempDir), IntToStr(ErrorCode),
@@ -313,7 +317,7 @@ begin
   until False; // continue until a new directory was created
   until False; // continue until a new directory was created
 end;
 end;
 
 
-function CreateTempDir(const AllowOnlyPrivilegedAccess: Boolean): String;
+function CreateTempDir(const LimitCurrentUserSidAccess: Boolean): String;
 { This is called by SetupLdr, Setup, and Uninstall. }
 { This is called by SetupLdr, Setup, and Uninstall. }
 var
 var
   Dir: String;
   Dir: String;
@@ -321,7 +325,7 @@ var
 begin
 begin
   while True do begin
   while True do begin
     Dir := GenerateUniqueName(False, GetTempDir, '.tmp');
     Dir := GenerateUniqueName(False, GetTempDir, '.tmp');
-    if CreateSafeDirectory(AllowOnlyPrivilegedAccess, Dir, ErrorCode) then
+    if CreateSafeDirectory(LimitCurrentUserSidAccess, Dir, ErrorCode) then
       Break;
       Break;
     if ErrorCode <> ERROR_ALREADY_EXISTS then
     if ErrorCode <> ERROR_ALREADY_EXISTS then
       raise Exception.Create(FmtSetupMessage(msgLastErrorMessage,
       raise Exception.Create(FmtSetupMessage(msgLastErrorMessage,