blun-king-cli 9.1.631 → 9.1.633
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/bin/launcher-runtime.js +25 -15
- package/bin/managed-plugin-selection.cjs +26 -1
- package/bin/plugin-bootstrap.js +280 -38
- package/bin/private-paths.js +442 -76
- package/bin/rename-private-staging.cjs +1 -1
- package/bin/standard-tools-bootstrap.js +97 -0
- package/bin/startup-security.cjs +306 -0
- package/bin/update-notice.js +192 -8
- package/bin/windows-private-temp.cjs +44 -10
- package/package.json +1 -1
package/bin/private-paths.js
CHANGED
|
@@ -39,6 +39,25 @@ public static class BlunPrivateAclNative
|
|
|
39
39
|
private const uint SYNCHRONIZE = 0x00100000;
|
|
40
40
|
private const uint CREATE_NEW = 1;
|
|
41
41
|
private const uint OPEN_EXISTING = 3;
|
|
42
|
+
private const uint FILE_SHARE_ALL = 0x00000007;
|
|
43
|
+
public static int DirectoryDaclWrites = 0;
|
|
44
|
+
public static readonly List<string> Warnings = new List<string>();
|
|
45
|
+
|
|
46
|
+
private static SafeFileHandle OpenShared(string target, uint access, uint flags)
|
|
47
|
+
{
|
|
48
|
+
for (int attempt = 0; ; attempt++)
|
|
49
|
+
{
|
|
50
|
+
SafeFileHandle handle = CreateFileExisting(target, access, FILE_SHARE_ALL,
|
|
51
|
+
IntPtr.Zero, OPEN_EXISTING, flags, IntPtr.Zero);
|
|
52
|
+
if (!handle.IsInvalid) return handle;
|
|
53
|
+
int error = Marshal.GetLastWin32Error();
|
|
54
|
+
handle.Dispose();
|
|
55
|
+
if ((error != 32 && error != 33) || attempt == 2)
|
|
56
|
+
throw new Win32Exception(error);
|
|
57
|
+
System.Threading.Thread.Sleep(40);
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
|
|
42
61
|
private const uint FILE_ATTRIBUTE_NORMAL = 0x00000080;
|
|
43
62
|
private const uint FILE_ATTRIBUTE_DIRECTORY = 0x00000010;
|
|
44
63
|
private const uint FILE_ATTRIBUTE_DEVICE = 0x00000040;
|
|
@@ -217,6 +236,11 @@ public static class BlunPrivateAclNative
|
|
|
217
236
|
out IntPtr dacl,
|
|
218
237
|
[MarshalAs(UnmanagedType.Bool)] out bool daclDefaulted);
|
|
219
238
|
|
|
239
|
+
[DllImport("advapi32.dll", SetLastError = true)]
|
|
240
|
+
[return: MarshalAs(UnmanagedType.Bool)]
|
|
241
|
+
private static extern bool SetKernelObjectSecurity(
|
|
242
|
+
SafeFileHandle handle, uint securityInformation, IntPtr securityDescriptor);
|
|
243
|
+
|
|
220
244
|
[DllImport("advapi32.dll", SetLastError = true)]
|
|
221
245
|
private static extern uint SetSecurityInfo(
|
|
222
246
|
SafeFileHandle handle,
|
|
@@ -314,6 +338,27 @@ public static class BlunPrivateAclNative
|
|
|
314
338
|
return descriptor;
|
|
315
339
|
}
|
|
316
340
|
|
|
341
|
+
private static void SetExactDirectoryDacl(SafeFileHandle handle, string userSid, bool directory)
|
|
342
|
+
{
|
|
343
|
+
CurrentPhase = "dacl-write";
|
|
344
|
+
if (directory) DirectoryDaclWrites++;
|
|
345
|
+
IntPtr descriptor = CreateExactDescriptor(userSid, directory);
|
|
346
|
+
try
|
|
347
|
+
{
|
|
348
|
+
// SetSecurityInfo(SE_FILE_OBJECT) propagates inheritable ACEs into
|
|
349
|
+
// existing descendants. A startup root check must touch only this
|
|
350
|
+
// handle; explicit HardenTree remains the opt-in recursive API.
|
|
351
|
+
if (!SetKernelObjectSecurity(handle,
|
|
352
|
+
DACL_SECURITY_INFORMATION | PROTECTED_DACL_SECURITY_INFORMATION,
|
|
353
|
+
descriptor))
|
|
354
|
+
throw new Win32Exception(Marshal.GetLastWin32Error());
|
|
355
|
+
}
|
|
356
|
+
finally
|
|
357
|
+
{
|
|
358
|
+
LocalFree(descriptor);
|
|
359
|
+
}
|
|
360
|
+
}
|
|
361
|
+
|
|
317
362
|
private static void SetExactDacl(SafeFileHandle handle, string userSid, bool directory)
|
|
318
363
|
{
|
|
319
364
|
CurrentPhase = "dacl-write";
|
|
@@ -579,32 +624,24 @@ public static class BlunPrivateAclNative
|
|
|
579
624
|
}
|
|
580
625
|
|
|
581
626
|
private static ByHandleFileInformation TakeOwnerBeforeReopen(
|
|
582
|
-
string target, bool directory, string userSid)
|
|
627
|
+
string target, bool directory, string userSid, ByHandleFileInformation? expected)
|
|
583
628
|
{
|
|
584
629
|
CurrentTarget = target;
|
|
585
630
|
CurrentPhase = "owner-takeover";
|
|
586
631
|
uint flags = FILE_FLAG_OPEN_REPARSE_POINT;
|
|
587
632
|
if (directory) flags |= FILE_FLAG_BACKUP_SEMANTICS;
|
|
588
|
-
SafeFileHandle handle
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
{
|
|
592
|
-
handle.Dispose();
|
|
593
|
-
// Read-only owner inspection still yields the precise OWNER_TAKEOVER
|
|
594
|
-
// failure when an administrator-owned object lacks WRITE_OWNER.
|
|
595
|
-
handle = CreateFileExisting(target,
|
|
596
|
-
READ_CONTROL | 0x80, 0, IntPtr.Zero, OPEN_EXISTING, flags, IntPtr.Zero);
|
|
597
|
-
}
|
|
598
|
-
if (handle.IsInvalid)
|
|
633
|
+
SafeFileHandle handle;
|
|
634
|
+
try { handle = OpenShared(target, READ_CONTROL | WRITE_OWNER | 0x80, flags); }
|
|
635
|
+
catch (Win32Exception error)
|
|
599
636
|
{
|
|
600
|
-
|
|
601
|
-
handle
|
|
602
|
-
throw new Win32Exception(error);
|
|
637
|
+
if (error.NativeErrorCode != 5) throw;
|
|
638
|
+
handle = OpenShared(target, READ_CONTROL | 0x80, flags);
|
|
603
639
|
}
|
|
604
640
|
using (handle)
|
|
605
641
|
{
|
|
606
642
|
ByHandleFileInformation identity = ValidateHandle(handle, directory);
|
|
607
643
|
VerifyHandlePath(handle, target);
|
|
644
|
+
if (expected.HasValue) VerifyReopenedIdentity(handle, directory, expected.Value);
|
|
608
645
|
CurrentPhase = "owner-verification";
|
|
609
646
|
RawSecurityDescriptor before = ReadAccessDescriptor(handle);
|
|
610
647
|
if (before.Owner == null || before.Owner.Value != "S-1-5-32-544")
|
|
@@ -625,56 +662,106 @@ public static class BlunPrivateAclNative
|
|
|
625
662
|
throw new UnauthorizedAccessException();
|
|
626
663
|
}
|
|
627
664
|
|
|
628
|
-
|
|
665
|
+
// Existing objects are inspected without requesting WRITE_DAC, WRITE_OWNER,
|
|
666
|
+
// DELETE or data-write access. Directory listing is needed for tree snapshots.
|
|
667
|
+
private static SafeFileHandle OpenExisting(string target, bool directory, bool enumerate, string userSid)
|
|
629
668
|
{
|
|
630
669
|
CurrentTarget = target;
|
|
631
670
|
CurrentPhase = "entry-open";
|
|
632
|
-
uint access = GENERIC_READ | READ_CONTROL | WRITE_DAC | DELETE;
|
|
633
|
-
if (write) access |= GENERIC_WRITE;
|
|
634
671
|
uint flags = FILE_FLAG_OPEN_REPARSE_POINT;
|
|
635
672
|
if (directory) flags |= FILE_FLAG_BACKUP_SEMANTICS;
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
673
|
+
uint access = READ_CONTROL | 0x80;
|
|
674
|
+
if (directory && enumerate) access |= 1;
|
|
675
|
+
SafeFileHandle handle;
|
|
676
|
+
try { handle = OpenShared(target, access, flags); }
|
|
677
|
+
catch (Win32Exception error)
|
|
639
678
|
{
|
|
640
|
-
|
|
641
|
-
|
|
679
|
+
if (error.NativeErrorCode != 5) throw;
|
|
680
|
+
ByHandleFileInformation identity = TakeOwnerBeforeReopen(target, directory, userSid, null);
|
|
681
|
+
handle = OpenShared(target, access, flags);
|
|
682
|
+
try { VerifyReopenedIdentity(handle, directory, identity); VerifyHandlePath(handle, target); }
|
|
683
|
+
catch { handle.Dispose(); throw; }
|
|
642
684
|
}
|
|
643
|
-
|
|
685
|
+
return handle;
|
|
686
|
+
}
|
|
687
|
+
|
|
688
|
+
private static bool IsExact(SafeFileHandle handle, string userSid, bool directory, out string owner)
|
|
689
|
+
{
|
|
690
|
+
CurrentPhase = "owner-verification";
|
|
691
|
+
RawSecurityDescriptor descriptor = ReadAccessDescriptor(handle);
|
|
692
|
+
if (descriptor.Owner == null) throw new UnauthorizedAccessException();
|
|
693
|
+
owner = descriptor.Owner.Value;
|
|
694
|
+
if (!ExpectedSids(userSid).Contains(owner))
|
|
644
695
|
{
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
696
|
+
if (owner == "S-1-5-32-544") return false;
|
|
697
|
+
throw new UnauthorizedAccessException();
|
|
698
|
+
}
|
|
699
|
+
try { VerifyExactDacl(handle, userSid, directory, owner); return true; }
|
|
700
|
+
catch (UnauthorizedAccessException) { return false; }
|
|
701
|
+
}
|
|
702
|
+
|
|
703
|
+
private static string ApplyExactSecurity(SafeFileHandle inspected, string target, string userSid, bool directory)
|
|
704
|
+
{
|
|
705
|
+
CurrentTarget = target;
|
|
706
|
+
ByHandleFileInformation identity = ValidateHandle(inspected, directory);
|
|
707
|
+
VerifyHandlePath(inspected, target);
|
|
708
|
+
string owner;
|
|
709
|
+
if (IsExact(inspected, userSid, directory, out owner))
|
|
710
|
+
{
|
|
711
|
+
ValidateHandle(inspected, directory);
|
|
712
|
+
VerifyHandlePath(inspected, target);
|
|
713
|
+
return owner;
|
|
714
|
+
}
|
|
715
|
+
uint flags = FILE_FLAG_OPEN_REPARSE_POINT;
|
|
716
|
+
if (directory) flags |= FILE_FLAG_BACKUP_SEMANTICS;
|
|
717
|
+
uint access = READ_CONTROL | WRITE_DAC | 0x80;
|
|
718
|
+
if (owner == "S-1-5-32-544") access |= WRITE_OWNER;
|
|
719
|
+
SafeFileHandle mutation;
|
|
720
|
+
try { mutation = OpenShared(target, access, flags); }
|
|
721
|
+
catch (Win32Exception error)
|
|
722
|
+
{
|
|
723
|
+
if (error.NativeErrorCode == 5 && owner == "S-1-5-32-544")
|
|
652
724
|
{
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
725
|
+
// Administrator-owned objects may grant WRITE_OWNER but not yet
|
|
726
|
+
// WRITE_DAC. Bind the takeover to the original read identity.
|
|
727
|
+
TakeOwnerBeforeReopen(target, directory, userSid, identity);
|
|
728
|
+
mutation = OpenShared(target, READ_CONTROL | WRITE_DAC | 0x80, flags);
|
|
729
|
+
}
|
|
730
|
+
else
|
|
731
|
+
{
|
|
732
|
+
// A lock is never proof of safety. Recheck this very object AND its
|
|
733
|
+
// current pathname; warn only if the full current descriptor is exact.
|
|
734
|
+
if (error.NativeErrorCode != 32 && error.NativeErrorCode != 33) throw;
|
|
735
|
+
VerifyReopenedIdentity(inspected, directory, identity);
|
|
736
|
+
VerifyHandlePath(inspected, target);
|
|
737
|
+
if (!IsExact(inspected, userSid, directory, out owner)) throw;
|
|
738
|
+
VerifyReopenedIdentity(inspected, directory, identity);
|
|
739
|
+
VerifyHandlePath(inspected, target);
|
|
740
|
+
Warnings.Add(target);
|
|
741
|
+
return owner;
|
|
668
742
|
}
|
|
669
|
-
if (handle.IsInvalid) CurrentPhase = "owner-takeover";
|
|
670
743
|
}
|
|
671
|
-
|
|
744
|
+
using (mutation)
|
|
672
745
|
{
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
746
|
+
// Keep the read handle alive across escalation. Reject replacement,
|
|
747
|
+
// reparse or hardlink attacks before ANY security descriptor mutation.
|
|
748
|
+
VerifyReopenedIdentity(mutation, directory, identity);
|
|
749
|
+
VerifyHandlePath(mutation, target);
|
|
750
|
+
VerifyReopenedIdentity(inspected, directory, identity);
|
|
751
|
+
VerifyHandlePath(inspected, target);
|
|
752
|
+
if (IsExact(mutation, userSid, directory, out owner)) return owner;
|
|
753
|
+
ValidateHandle(mutation, directory);
|
|
754
|
+
VerifyHandlePath(mutation, target);
|
|
755
|
+
owner = ValidateOwner(mutation, ExpectedSids(userSid));
|
|
756
|
+
VerifyReopenedIdentity(mutation, directory, identity);
|
|
757
|
+
VerifyHandlePath(mutation, target);
|
|
758
|
+
if (directory) SetExactDirectoryDacl(mutation, userSid, true);
|
|
759
|
+
else SetExactDacl(mutation, userSid, false);
|
|
760
|
+
VerifyExactDacl(mutation, userSid, directory, owner);
|
|
761
|
+
VerifyReopenedIdentity(mutation, directory, identity);
|
|
762
|
+
VerifyHandlePath(mutation, target);
|
|
763
|
+
return owner;
|
|
676
764
|
}
|
|
677
|
-
return handle;
|
|
678
765
|
}
|
|
679
766
|
|
|
680
767
|
private static string NativePath(string target)
|
|
@@ -691,6 +778,7 @@ public static class BlunPrivateAclNative
|
|
|
691
778
|
{
|
|
692
779
|
CurrentTarget = target;
|
|
693
780
|
CurrentPhase = "directory-open";
|
|
781
|
+
if (Directory.Exists(target)) return OpenExisting(target, true, false, userSid);
|
|
694
782
|
string nativePath = NativePath(target);
|
|
695
783
|
if (Encoding.Unicode.GetByteCount(nativePath) > UInt16.MaxValue - 2)
|
|
696
784
|
throw new PathTooLongException();
|
|
@@ -724,7 +812,7 @@ public static class BlunPrivateAclNative
|
|
|
724
812
|
out ioStatus,
|
|
725
813
|
IntPtr.Zero,
|
|
726
814
|
0,
|
|
727
|
-
|
|
815
|
+
FILE_SHARE_ALL,
|
|
728
816
|
FILE_OPEN_IF,
|
|
729
817
|
FILE_DIRECTORY_FILE | FILE_SYNCHRONOUS_IO_NONALERT | FILE_OPEN_REPARSE_POINT,
|
|
730
818
|
IntPtr.Zero,
|
|
@@ -733,7 +821,7 @@ public static class BlunPrivateAclNative
|
|
|
733
821
|
{
|
|
734
822
|
status = NtCreateFile(out rawHandle,
|
|
735
823
|
GENERIC_READ | READ_CONTROL | WRITE_DAC | DELETE | SYNCHRONIZE,
|
|
736
|
-
ref attributes, out ioStatus, IntPtr.Zero, 0,
|
|
824
|
+
ref attributes, out ioStatus, IntPtr.Zero, 0, FILE_SHARE_ALL, FILE_OPEN_IF,
|
|
737
825
|
FILE_DIRECTORY_FILE | FILE_SYNCHRONOUS_IO_NONALERT | FILE_OPEN_REPARSE_POINT,
|
|
738
826
|
IntPtr.Zero, 0);
|
|
739
827
|
}
|
|
@@ -757,6 +845,79 @@ public static class BlunPrivateAclNative
|
|
|
757
845
|
}
|
|
758
846
|
}
|
|
759
847
|
|
|
848
|
+
private static SafeFileHandle OpenOrCreateStartupDirectory(
|
|
849
|
+
string target,
|
|
850
|
+
string userSid)
|
|
851
|
+
{
|
|
852
|
+
CurrentTarget = target;
|
|
853
|
+
CurrentPhase = "directory-open";
|
|
854
|
+
if (Directory.Exists(target)) return OpenShared(target, READ_CONTROL | 0x80, FILE_FLAG_BACKUP_SEMANTICS | FILE_FLAG_OPEN_REPARSE_POINT);
|
|
855
|
+
string nativePath = NativePath(target);
|
|
856
|
+
if (Encoding.Unicode.GetByteCount(nativePath) > UInt16.MaxValue - 2)
|
|
857
|
+
throw new PathTooLongException();
|
|
858
|
+
|
|
859
|
+
IntPtr nameBuffer = IntPtr.Zero;
|
|
860
|
+
IntPtr namePointer = IntPtr.Zero;
|
|
861
|
+
IntPtr descriptor = IntPtr.Zero;
|
|
862
|
+
IntPtr rawHandle = IntPtr.Zero;
|
|
863
|
+
try
|
|
864
|
+
{
|
|
865
|
+
nameBuffer = Marshal.StringToHGlobalUni(nativePath);
|
|
866
|
+
UnicodeString name = new UnicodeString();
|
|
867
|
+
name.Length = (ushort)Encoding.Unicode.GetByteCount(nativePath);
|
|
868
|
+
name.MaximumLength = (ushort)(name.Length + 2);
|
|
869
|
+
name.Buffer = nameBuffer;
|
|
870
|
+
namePointer = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(UnicodeString)));
|
|
871
|
+
Marshal.StructureToPtr(name, namePointer, false);
|
|
872
|
+
descriptor = CreateExactDescriptor(userSid, true);
|
|
873
|
+
|
|
874
|
+
ObjectAttributes attributes = new ObjectAttributes();
|
|
875
|
+
attributes.Length = Marshal.SizeOf(typeof(ObjectAttributes));
|
|
876
|
+
attributes.ObjectName = namePointer;
|
|
877
|
+
attributes.Attributes = OBJ_CASE_INSENSITIVE;
|
|
878
|
+
attributes.SecurityDescriptor = descriptor;
|
|
879
|
+
|
|
880
|
+
IoStatusBlock ioStatus;
|
|
881
|
+
int status = NtCreateFile(
|
|
882
|
+
out rawHandle,
|
|
883
|
+
GENERIC_READ | READ_CONTROL | WRITE_DAC | WRITE_OWNER | DELETE | SYNCHRONIZE,
|
|
884
|
+
ref attributes,
|
|
885
|
+
out ioStatus,
|
|
886
|
+
IntPtr.Zero,
|
|
887
|
+
0,
|
|
888
|
+
FILE_SHARE_ALL,
|
|
889
|
+
FILE_OPEN_IF,
|
|
890
|
+
FILE_DIRECTORY_FILE | FILE_SYNCHRONOUS_IO_NONALERT | FILE_OPEN_REPARSE_POINT,
|
|
891
|
+
IntPtr.Zero,
|
|
892
|
+
0);
|
|
893
|
+
if (status < 0 && RtlNtStatusToDosError(status) == 5)
|
|
894
|
+
{
|
|
895
|
+
status = NtCreateFile(out rawHandle,
|
|
896
|
+
GENERIC_READ | READ_CONTROL | WRITE_DAC | DELETE | SYNCHRONIZE,
|
|
897
|
+
ref attributes, out ioStatus, IntPtr.Zero, 0, FILE_SHARE_ALL, FILE_OPEN_IF,
|
|
898
|
+
FILE_DIRECTORY_FILE | FILE_SYNCHRONOUS_IO_NONALERT | FILE_OPEN_REPARSE_POINT,
|
|
899
|
+
IntPtr.Zero, 0);
|
|
900
|
+
}
|
|
901
|
+
if (status < 0 && RtlNtStatusToDosError(status) == 5)
|
|
902
|
+
return OpenShared(target, READ_CONTROL | 0x80, FILE_FLAG_BACKUP_SEMANTICS | FILE_FLAG_OPEN_REPARSE_POINT);
|
|
903
|
+
if (status < 0)
|
|
904
|
+
throw new Win32Exception((int)RtlNtStatusToDosError(status));
|
|
905
|
+
return new SafeFileHandle(rawHandle, true);
|
|
906
|
+
}
|
|
907
|
+
catch
|
|
908
|
+
{
|
|
909
|
+
if (rawHandle != IntPtr.Zero && rawHandle != new IntPtr(-1))
|
|
910
|
+
new SafeFileHandle(rawHandle, true).Dispose();
|
|
911
|
+
throw;
|
|
912
|
+
}
|
|
913
|
+
finally
|
|
914
|
+
{
|
|
915
|
+
if (descriptor != IntPtr.Zero) LocalFree(descriptor);
|
|
916
|
+
if (namePointer != IntPtr.Zero) Marshal.FreeHGlobal(namePointer);
|
|
917
|
+
if (nameBuffer != IntPtr.Zero) Marshal.FreeHGlobal(nameBuffer);
|
|
918
|
+
}
|
|
919
|
+
}
|
|
920
|
+
|
|
760
921
|
private static void EnsureParentDirectories(string target, string userSid)
|
|
761
922
|
{
|
|
762
923
|
string parent = Path.GetDirectoryName(Path.GetFullPath(target));
|
|
@@ -776,24 +937,184 @@ public static class BlunPrivateAclNative
|
|
|
776
937
|
{
|
|
777
938
|
ValidateHandle(handle, true);
|
|
778
939
|
VerifyHandlePath(handle, fullTarget);
|
|
779
|
-
|
|
780
|
-
string owner = ValidateOwner(handle, expectedSids);
|
|
781
|
-
SetExactDacl(handle, userSid, true);
|
|
782
|
-
VerifyExactDacl(handle, userSid, true, owner);
|
|
940
|
+
ApplyExactSecurity(handle, fullTarget, userSid, true);
|
|
783
941
|
VerifyHandlePath(handle, fullTarget);
|
|
784
942
|
}
|
|
785
943
|
}
|
|
786
944
|
|
|
945
|
+
// Startup writes only a nonconforming, user-owned directory object. The
|
|
946
|
+
// kernel handle setter deliberately does NOT propagate into descendants.
|
|
947
|
+
private static bool StartupReadOnlyExtras(RawSecurityDescriptor descriptor, string userSid)
|
|
948
|
+
{
|
|
949
|
+
if ((descriptor.ControlFlags & ControlFlags.DiscretionaryAclProtected) == 0
|
|
950
|
+
|| descriptor.DiscretionaryAcl == null) return false;
|
|
951
|
+
HashSet<string> expected = ExpectedSids(userSid);
|
|
952
|
+
HashSet<string> seen = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
|
|
953
|
+
int readMask = (int)(FileSystemRights.ReadAndExecute | FileSystemRights.Synchronize);
|
|
954
|
+
foreach (GenericAce generic in descriptor.DiscretionaryAcl)
|
|
955
|
+
{
|
|
956
|
+
CommonAce ace = generic as CommonAce;
|
|
957
|
+
if (ace == null || ace.AceQualifier != AceQualifier.AccessAllowed
|
|
958
|
+
|| ace.SecurityIdentifier == null) return false;
|
|
959
|
+
string sid = ace.SecurityIdentifier.Value;
|
|
960
|
+
AceFlags inheritance = AceFlags.ObjectInherit | AceFlags.ContainerInherit
|
|
961
|
+
| AceFlags.Inherited | AceFlags.InheritOnly | AceFlags.NoPropagateInherit;
|
|
962
|
+
if ((ace.AceFlags & ~inheritance) != 0) return false;
|
|
963
|
+
if (expected.Contains(sid) || sid == "S-1-5-32-544")
|
|
964
|
+
{
|
|
965
|
+
// Extra allow rights for owner/SYSTEM/Administrators are safe.
|
|
966
|
+
// Still require the explicit, self-effective base grants; an
|
|
967
|
+
// inherited-only/standard DACL belongs to the repair category.
|
|
968
|
+
if (expected.Contains(sid) && ace.AccessMask == (int)FileSystemRights.FullControl
|
|
969
|
+
&& ace.AceFlags == (AceFlags.ObjectInherit | AceFlags.ContainerInherit))
|
|
970
|
+
seen.Add(sid);
|
|
971
|
+
}
|
|
972
|
+
else if ((ace.AccessMask & ~readMask) != 0) return false;
|
|
973
|
+
}
|
|
974
|
+
return seen.SetEquals(expected);
|
|
975
|
+
}
|
|
976
|
+
|
|
977
|
+
// USERPROFILE is supplied by the launching Windows process, never StateDir.
|
|
978
|
+
// Pin every ancestor without delete sharing: junctions and rename races must
|
|
979
|
+
// not move a repair across the profile boundary while the setter runs.
|
|
980
|
+
private sealed class StartupProfileBoundary : IDisposable
|
|
981
|
+
{
|
|
982
|
+
private readonly List<SafeFileHandle> handles = new List<SafeFileHandle>();
|
|
983
|
+
public readonly bool CanRepair;
|
|
984
|
+
private string PinAncestors(string target)
|
|
985
|
+
{
|
|
986
|
+
string full = ValidateTreeRootPath(target);
|
|
987
|
+
string current = full;
|
|
988
|
+
string resolved = null;
|
|
989
|
+
while (!String.IsNullOrEmpty(current))
|
|
990
|
+
{
|
|
991
|
+
SafeFileHandle held = CreateFileExisting(current, 0x80, 3,
|
|
992
|
+
IntPtr.Zero, OPEN_EXISTING,
|
|
993
|
+
FILE_FLAG_BACKUP_SEMANTICS | FILE_FLAG_OPEN_REPARSE_POINT, IntPtr.Zero);
|
|
994
|
+
if (held.IsInvalid) { int error = Marshal.GetLastWin32Error(); held.Dispose(); throw new Win32Exception(error); }
|
|
995
|
+
handles.Add(held);
|
|
996
|
+
ValidateHandle(held, true);
|
|
997
|
+
VerifyHandlePath(held, current);
|
|
998
|
+
if (resolved == null)
|
|
999
|
+
{
|
|
1000
|
+
StringBuilder address = new StringBuilder(32768);
|
|
1001
|
+
uint length = GetFinalPathNameByHandleW(held, address, (uint)address.Capacity, 0);
|
|
1002
|
+
if (length == 0 || length >= address.Capacity) throw new Win32Exception(Marshal.GetLastWin32Error());
|
|
1003
|
+
resolved = Path.GetFullPath(NormalizeHandlePath(address.ToString())).TrimEnd('\\');
|
|
1004
|
+
}
|
|
1005
|
+
current = Path.GetDirectoryName(current);
|
|
1006
|
+
}
|
|
1007
|
+
return resolved;
|
|
1008
|
+
}
|
|
1009
|
+
public StartupProfileBoundary(string target)
|
|
1010
|
+
{
|
|
1011
|
+
try
|
|
1012
|
+
{
|
|
1013
|
+
string actual = PinAncestors(target);
|
|
1014
|
+
string profile = Environment.GetEnvironmentVariable("USERPROFILE");
|
|
1015
|
+
if (String.IsNullOrEmpty(profile)) { CanRepair = false; return; }
|
|
1016
|
+
string resolvedProfile = PinAncestors(profile);
|
|
1017
|
+
string boundary = Path.Combine(resolvedProfile, ".blun");
|
|
1018
|
+
CanRepair = String.Equals(actual, boundary, StringComparison.OrdinalIgnoreCase)
|
|
1019
|
+
|| actual.StartsWith(boundary + "\\", StringComparison.OrdinalIgnoreCase);
|
|
1020
|
+
}
|
|
1021
|
+
catch { Dispose(); throw; }
|
|
1022
|
+
}
|
|
1023
|
+
public void Dispose()
|
|
1024
|
+
{
|
|
1025
|
+
for (int i = handles.Count - 1; i >= 0; i--) handles[i].Dispose();
|
|
1026
|
+
handles.Clear();
|
|
1027
|
+
}
|
|
1028
|
+
}
|
|
1029
|
+
public static readonly List<string> OutsideProfile = new List<string>();
|
|
1030
|
+
public static string StartupOwnerSid = null;
|
|
1031
|
+
|
|
1032
|
+
public static readonly List<string> Repaired = new List<string>();
|
|
1033
|
+
// Windows PowerShell 5.1 does not reliably bind omitted .NET optional
|
|
1034
|
+
// arguments. Keep an explicit two-argument overload for the host protocol.
|
|
1035
|
+
public static void InspectStartupDirectory(string target, string userSid)
|
|
1036
|
+
{
|
|
1037
|
+
InspectStartupDirectory(target, userSid, true);
|
|
1038
|
+
}
|
|
1039
|
+
|
|
1040
|
+
public static void InspectStartupDirectory(string target, string userSid, bool allowRepair)
|
|
1041
|
+
{
|
|
1042
|
+
string full = ValidateTreeRootPath(target);
|
|
1043
|
+
if (!Directory.Exists(full))
|
|
1044
|
+
{
|
|
1045
|
+
string parent = Path.GetDirectoryName(full);
|
|
1046
|
+
if (!Directory.Exists(parent)) InspectStartupDirectory(parent, userSid, allowRepair);
|
|
1047
|
+
using (SafeFileHandle created = OpenOrCreateStartupDirectory(full, userSid))
|
|
1048
|
+
{
|
|
1049
|
+
ValidateHandle(created, true);
|
|
1050
|
+
VerifyHandlePath(created, full);
|
|
1051
|
+
}
|
|
1052
|
+
}
|
|
1053
|
+
CurrentTarget = full;
|
|
1054
|
+
using (SafeFileHandle handle = OpenShared(full, READ_CONTROL | 0x80,
|
|
1055
|
+
FILE_FLAG_BACKUP_SEMANTICS | FILE_FLAG_OPEN_REPARSE_POINT))
|
|
1056
|
+
{
|
|
1057
|
+
ByHandleFileInformation identity = ValidateHandle(handle, true);
|
|
1058
|
+
VerifyHandlePath(handle, full);
|
|
1059
|
+
CurrentPhase = "owner-verification";
|
|
1060
|
+
RawSecurityDescriptor descriptor = ReadAccessDescriptor(handle);
|
|
1061
|
+
StartupOwnerSid = descriptor.Owner == null ? null : descriptor.Owner.Value;
|
|
1062
|
+
if (descriptor.Owner == null || descriptor.Owner.Value != new SecurityIdentifier(userSid).Value)
|
|
1063
|
+
throw new UnauthorizedAccessException();
|
|
1064
|
+
try { VerifyExactDacl(handle, userSid, true, descriptor.Owner.Value); }
|
|
1065
|
+
catch (UnauthorizedAccessException)
|
|
1066
|
+
{
|
|
1067
|
+
if (StartupReadOnlyExtras(descriptor, userSid)) Warnings.Add(full);
|
|
1068
|
+
else
|
|
1069
|
+
{
|
|
1070
|
+
if (!allowRepair) throw new UnauthorizedAccessException();
|
|
1071
|
+
using (StartupProfileBoundary boundary = new StartupProfileBoundary(full))
|
|
1072
|
+
{
|
|
1073
|
+
if (!boundary.CanRepair) { OutsideProfile.Add(full); }
|
|
1074
|
+
else
|
|
1075
|
+
{
|
|
1076
|
+
using (SafeFileHandle mutation = OpenShared(full, READ_CONTROL | WRITE_DAC | 0x80,
|
|
1077
|
+
FILE_FLAG_BACKUP_SEMANTICS | FILE_FLAG_OPEN_REPARSE_POINT))
|
|
1078
|
+
{
|
|
1079
|
+
VerifyReopenedIdentity(mutation, true, identity);
|
|
1080
|
+
VerifyHandlePath(mutation, full);
|
|
1081
|
+
VerifyReopenedIdentity(handle, true, identity);
|
|
1082
|
+
RawSecurityDescriptor current = ReadAccessDescriptor(mutation);
|
|
1083
|
+
if (current.Owner == null || current.Owner.Value != new SecurityIdentifier(userSid).Value)
|
|
1084
|
+
throw new UnauthorizedAccessException();
|
|
1085
|
+
// A concurrent safe repair needs no second write.
|
|
1086
|
+
try { VerifyExactDacl(mutation, userSid, true, current.Owner.Value); }
|
|
1087
|
+
catch (UnauthorizedAccessException)
|
|
1088
|
+
{
|
|
1089
|
+
if (StartupReadOnlyExtras(current, userSid)) Warnings.Add(full);
|
|
1090
|
+
else
|
|
1091
|
+
{
|
|
1092
|
+
SetExactDirectoryDacl(mutation, userSid, true);
|
|
1093
|
+
VerifyExactDacl(mutation, userSid, true, current.Owner.Value);
|
|
1094
|
+
if (ReadAccessDescriptor(mutation).Owner.Value != current.Owner.Value)
|
|
1095
|
+
throw new UnauthorizedAccessException();
|
|
1096
|
+
Repaired.Add(full);
|
|
1097
|
+
}
|
|
1098
|
+
}
|
|
1099
|
+
VerifyReopenedIdentity(mutation, true, identity);
|
|
1100
|
+
VerifyHandlePath(mutation, full);
|
|
1101
|
+
}
|
|
1102
|
+
}
|
|
1103
|
+
}
|
|
1104
|
+
}
|
|
1105
|
+
}
|
|
1106
|
+
VerifyReopenedIdentity(handle, true, identity);
|
|
1107
|
+
VerifyHandlePath(handle, full);
|
|
1108
|
+
}
|
|
1109
|
+
}
|
|
1110
|
+
|
|
787
1111
|
public static void HardenExisting(string target, string userSid, bool directory)
|
|
788
1112
|
{
|
|
789
1113
|
using (SafeFileHandle handle = OpenExisting(target, directory, false, userSid))
|
|
790
1114
|
{
|
|
791
1115
|
ValidateHandle(handle, directory);
|
|
792
1116
|
VerifyHandlePath(handle, target);
|
|
793
|
-
|
|
794
|
-
string owner = ValidateOwner(handle, expectedSids);
|
|
795
|
-
SetExactDacl(handle, userSid, directory);
|
|
796
|
-
VerifyExactDacl(handle, userSid, directory, owner);
|
|
1117
|
+
ApplyExactSecurity(handle, target, userSid, directory);
|
|
797
1118
|
ValidateHandle(handle, directory);
|
|
798
1119
|
VerifyHandlePath(handle, target);
|
|
799
1120
|
}
|
|
@@ -969,9 +1290,7 @@ public static class BlunPrivateAclNative
|
|
|
969
1290
|
{
|
|
970
1291
|
ValidateHandle(handle, directory);
|
|
971
1292
|
VerifyHandlePath(handle, target);
|
|
972
|
-
string owner =
|
|
973
|
-
SetExactDacl(handle, userSid, directory);
|
|
974
|
-
VerifyExactDacl(handle, userSid, directory, owner);
|
|
1293
|
+
string owner = ApplyExactSecurity(handle, target, userSid, directory);
|
|
975
1294
|
ValidateHandle(handle, directory);
|
|
976
1295
|
VerifyHandlePath(handle, target);
|
|
977
1296
|
return owner;
|
|
@@ -997,7 +1316,7 @@ public static class BlunPrivateAclNative
|
|
|
997
1316
|
throw new UnauthorizedAccessException();
|
|
998
1317
|
bool directory = (entry.Attributes & FILE_ATTRIBUTE_DIRECTORY) != 0;
|
|
999
1318
|
string childPath = TreeChildPath(root, directoryPath, entry.Name);
|
|
1000
|
-
SafeFileHandle childHandle = OpenExisting(childPath, directory,
|
|
1319
|
+
SafeFileHandle childHandle = OpenExisting(childPath, directory, true, userSid);
|
|
1001
1320
|
try
|
|
1002
1321
|
{
|
|
1003
1322
|
HeldTreeHandle heldHandle = new HeldTreeHandle();
|
|
@@ -1043,7 +1362,7 @@ public static class BlunPrivateAclNative
|
|
|
1043
1362
|
List<HeldTreeHandle> heldHandles = new List<HeldTreeHandle>();
|
|
1044
1363
|
try
|
|
1045
1364
|
{
|
|
1046
|
-
SafeFileHandle rootHandle = OpenExisting(root, true,
|
|
1365
|
+
SafeFileHandle rootHandle = OpenExisting(root, true, true, userSid);
|
|
1047
1366
|
HeldTreeHandle heldRoot = new HeldTreeHandle();
|
|
1048
1367
|
heldRoot.Handle = rootHandle;
|
|
1049
1368
|
heldRoot.Path = root;
|
|
@@ -1237,6 +1556,9 @@ const WINDOWS_ACL_SCRIPT = [
|
|
|
1237
1556
|
'}',
|
|
1238
1557
|
'}',
|
|
1239
1558
|
"[Console]::Out.Write('BLUN_PRIVATE_ACL_OK_V1')",
|
|
1559
|
+
'foreach ($warning in [BlunPrivateAclNative]::Warnings) {',
|
|
1560
|
+
"[Console]::Out.Write(('`nBLUN_PRIVATE_ACL_LOCK_EXACT:' + [Convert]::ToBase64String([Text.Encoding]::UTF8.GetBytes($warning))).Replace('`n',[Environment]::NewLine))",
|
|
1561
|
+
'}',
|
|
1240
1562
|
'exit 0',
|
|
1241
1563
|
'} catch {',
|
|
1242
1564
|
"$safePhase=$phase",
|
|
@@ -1251,12 +1573,12 @@ const WINDOWS_ACL_SCRIPT = [
|
|
|
1251
1573
|
"if ($failure -is [ArgumentException]) { $reason='argument-error'; break }",
|
|
1252
1574
|
'$failure=$failure.InnerException',
|
|
1253
1575
|
'}',
|
|
1254
|
-
"if ($
|
|
1576
|
+
"if ($phase -eq 'native-dispatch') {",
|
|
1255
1577
|
"$errorTarget=[Convert]::ToBase64String([Text.Encoding]::UTF8.GetBytes([BlunPrivateAclNative]::CurrentTarget))",
|
|
1256
1578
|
"[Console]::Error.Write(('BLUN_PRIVATE_ACL_ERROR_V4:{0}:{1}:{2}:{3}' -f $safePhase,$nativeCode,$reason,$errorTarget))",
|
|
1257
|
-
|
|
1579
|
+
'} else {',
|
|
1258
1580
|
"[Console]::Error.Write(('BLUN_PRIVATE_ACL_ERROR_V3:{0}:{1}:{2}' -f $safePhase,$nativeCode,$reason))",
|
|
1259
|
-
|
|
1581
|
+
'}',
|
|
1260
1582
|
'exit 1',
|
|
1261
1583
|
'}',
|
|
1262
1584
|
].join('\n');
|
|
@@ -1332,13 +1654,14 @@ function windowsPrivateAclError(phase, privatePath, operation, nativeErrorCode,
|
|
|
1332
1654
|
|
|
1333
1655
|
function windowsPrivateAclHostError(failure, privatePath, operation, userSid) {
|
|
1334
1656
|
const stderr = typeof failure?.stderr === 'string' ? failure.stderr : '';
|
|
1335
|
-
const takeover = /^BLUN_PRIVATE_ACL_ERROR_V4:
|
|
1336
|
-
if (takeover &&
|
|
1337
|
-
const target = Buffer.from(takeover[
|
|
1657
|
+
const takeover = /^BLUN_PRIVATE_ACL_ERROR_V4:([a-z-]+):([0-9]{1,5}):(native-error|access-denied|io-error|argument-error|unknown):([A-Za-z0-9+/=]{1,24000})$/u.exec(stderr);
|
|
1658
|
+
if (takeover && WINDOWS_PRIVATE_ACL_PHASES.has(takeover[1]) && Number(takeover[2]) <= 65535) {
|
|
1659
|
+
const target = Buffer.from(takeover[4], 'base64').toString('utf8');
|
|
1338
1660
|
const prefix = privatePath.replace(/[\\/]+$/u, '') + path.sep;
|
|
1339
1661
|
if ((target === privatePath || (operation === 'tree' && target.startsWith(prefix)))
|
|
1340
1662
|
&& !/[\x00-\x1f"]/u.test(target)) {
|
|
1341
|
-
const error = windowsPrivateAclError(
|
|
1663
|
+
const error = windowsPrivateAclError(takeover[1], target, operation, Number(takeover[2]), takeover[3]);
|
|
1664
|
+
if (takeover[1] !== 'owner-takeover') return error;
|
|
1342
1665
|
error.nextStep = `icacls "${target}" /setowner *${userSid}`;
|
|
1343
1666
|
error.requiredAccess = 'WRITE_OWNER';
|
|
1344
1667
|
error.message = `[${error.code}] ${target}; WRITE_OWNER; ${error.nextStep}${error.nativeErrorCode ? ` (${error.nativeErrorCode})` : ''}`;
|
|
@@ -1386,6 +1709,21 @@ function resolveWindowsPrivateTarget(targetPath) {
|
|
|
1386
1709
|
}
|
|
1387
1710
|
}
|
|
1388
1711
|
|
|
1712
|
+
function existingTimeoutDirectory(target) {
|
|
1713
|
+
try { return checkedTimeoutDirectory(target); }
|
|
1714
|
+
catch (error) { if (error.code === 'ENOENT') return undefined; throw error; }
|
|
1715
|
+
}
|
|
1716
|
+
|
|
1717
|
+
function checkedTimeoutDirectory(target, expected) {
|
|
1718
|
+
const stat = fs.lstatSync(target);
|
|
1719
|
+
if (!stat.isDirectory() || stat.isSymbolicLink()
|
|
1720
|
+
|| path.resolve(fs.realpathSync(target)).toLowerCase() !== path.resolve(target).toLowerCase()
|
|
1721
|
+
|| (expected && !sameFileIdentity(expected, stat))) {
|
|
1722
|
+
throw windowsPrivateAclError('target-validation', target, 'directory');
|
|
1723
|
+
}
|
|
1724
|
+
return stat;
|
|
1725
|
+
}
|
|
1726
|
+
|
|
1389
1727
|
function windowsAclController(options) {
|
|
1390
1728
|
const systemDirectory = windowsSystemDirectory(options);
|
|
1391
1729
|
const hostEnvironment = windowsHostEnvironment(systemDirectory);
|
|
@@ -1445,6 +1783,8 @@ function windowsAclController(options) {
|
|
|
1445
1783
|
BLUN_PRIVATE_ACL_USER_SID: sid,
|
|
1446
1784
|
BLUN_PRIVATE_ACL_NATIVE_CSHARP: WINDOWS_ACL_CSHARP_GZIP,
|
|
1447
1785
|
};
|
|
1786
|
+
const beforeTimeout = kind === 'directory' && process.platform === 'win32'
|
|
1787
|
+
? existingTimeoutDirectory(absoluteTarget) : undefined;
|
|
1448
1788
|
let output;
|
|
1449
1789
|
try {
|
|
1450
1790
|
output = runWindowsCommand(
|
|
@@ -1456,11 +1796,24 @@ function windowsAclController(options) {
|
|
|
1456
1796
|
input,
|
|
1457
1797
|
);
|
|
1458
1798
|
} catch (failure) {
|
|
1459
|
-
|
|
1799
|
+
const error = windowsPrivateAclHostError(failure, absoluteTarget, kind, sid);
|
|
1800
|
+
throw error;
|
|
1460
1801
|
}
|
|
1461
|
-
|
|
1802
|
+
const lines = String(output).split(/\r?\n/u);
|
|
1803
|
+
if (lines.shift() !== WINDOWS_ACL_OK_SENTINEL) {
|
|
1462
1804
|
throw windowsPrivateAclError('result-protocol', absoluteTarget, kind);
|
|
1463
1805
|
}
|
|
1806
|
+
for (const line of lines) {
|
|
1807
|
+
const match = /^BLUN_PRIVATE_ACL_LOCK_EXACT:([A-Za-z0-9+/=]+)$/u.exec(line);
|
|
1808
|
+
if (!match) throw windowsPrivateAclError('result-protocol', absoluteTarget, kind);
|
|
1809
|
+
const lockedPath = Buffer.from(match[1], 'base64').toString('utf8');
|
|
1810
|
+
if (lockedPath !== absoluteTarget && !(kind === 'tree' && lockedPath.startsWith(absoluteTarget + path.sep))) {
|
|
1811
|
+
throw windowsPrivateAclError('result-protocol', absoluteTarget, kind);
|
|
1812
|
+
}
|
|
1813
|
+
const warning = windowsPrivateAclError('lock-exact', lockedPath, kind);
|
|
1814
|
+
if (options.onWarning) options.onWarning(warning);
|
|
1815
|
+
else process.emitWarning(warning.message, { code: warning.code });
|
|
1816
|
+
}
|
|
1464
1817
|
} catch (error) {
|
|
1465
1818
|
primaryFailure = error;
|
|
1466
1819
|
throw error;
|
|
@@ -1811,7 +2164,20 @@ function securePrivateTree(targetPath, options = {}) {
|
|
|
1811
2164
|
securePosixPrivateTree(targetPath, fsImpl, options);
|
|
1812
2165
|
}
|
|
1813
2166
|
|
|
2167
|
+
function createStartupSecurity(options = {}) {
|
|
2168
|
+
return require('./startup-security.cjs').createStartupSecurity({
|
|
2169
|
+
...options, nativeCode: WINDOWS_ACL_CSHARP_GZIP,
|
|
2170
|
+
resolveTarget: resolveWindowsPrivateTarget,
|
|
2171
|
+
nativeError: windowsPrivateAclError,
|
|
2172
|
+
allowedPhases: WINDOWS_PRIVATE_ACL_PHASES,
|
|
2173
|
+
systemDirectory: (options.platform || process.platform) === 'win32'
|
|
2174
|
+
? windowsSystemDirectory(options) : undefined,
|
|
2175
|
+
posix: { ensurePrivateDirectory, securePrivateFile, writePrivateFile },
|
|
2176
|
+
});
|
|
2177
|
+
}
|
|
2178
|
+
|
|
1814
2179
|
module.exports = {
|
|
2180
|
+
createStartupSecurity,
|
|
1815
2181
|
PRIVATE_DIRECTORY_MODE,
|
|
1816
2182
|
PRIVATE_FILE_MODE,
|
|
1817
2183
|
ensurePrivateDirectory,
|