Files
ThreadPilot/Services/AdminOnlyDirectory.cs
T
PrimeBuild-pc ac84382841 security: follow-ups from the validation pass
- The launch task is re-registered on every elevated start again (now in the
  background), so a changed definition or another admin never keeps a stale one.
- Admin-only folders are owned by Administrators when elevated, so the user
  cannot rewrite their DACL with UAC off or a non-default owner policy.
- The update provider trusts only folders it created itself.
- Game Boost does not remember a foreground process as not-a-game while its
  path is still unreadable.
2026-10-22 10:54:37 +02:00

60 lines
2.6 KiB
C#

/*
* ThreadPilot - directories only administrators and SYSTEM can write, for files the elevated app later runs or hands to Windows.
*/
namespace ThreadPilot.Services
{
using System;
using System.IO;
using System.Security.AccessControl;
using System.Security.Principal;
public static class AdminOnlyDirectory
{
/// <summary>
/// Gets the default parent: in %SystemRoot%\Temp users may create folders but cannot rename or delete
/// another account's, unlike their own %TEMP%, where they could swap a file between check and use.
/// </summary>
public static string DefaultRoot { get; } = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Windows), "Temp");
/// <summary>Creates <paramref name="path"/> (which must not exist) with an Administrators + SYSTEM only DACL.</summary>
public static string Create(string path)
{
var security = new DirectorySecurity();
security.SetAccessRuleProtection(isProtected: true, preserveInheritance: false);
Allow(security, new SecurityIdentifier(WellKnownSidType.LocalSystemSid, null));
Allow(security, new SecurityIdentifier(WellKnownSidType.BuiltinAdministratorsSid, null));
// Not elevated (tests, dev runs): there is no privilege gap to protect, and without this the
// process could not write its own files.
using var identity = WindowsIdentity.GetCurrent();
if (new WindowsPrincipal(identity).IsInRole(WindowsBuiltInRole.Administrator))
{
// Owned by the group, not the user: with UAC off or another owner policy the user could otherwise rewrite the DACL.
security.SetOwner(new SecurityIdentifier(WellKnownSidType.BuiltinAdministratorsSid, null));
}
else if (identity.User != null)
{
Allow(security, identity.User);
}
if (Directory.Exists(path) || File.Exists(path))
{
throw new IOException($"Refusing to reuse existing path '{path}'.");
}
security.CreateDirectory(path);
return path;
}
private static void Allow(DirectorySecurity security, IdentityReference identity)
{
security.AddAccessRule(new FileSystemAccessRule(
identity,
FileSystemRights.FullControl,
InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit,
PropagationFlags.None,
AccessControlType.Allow));
}
}
}