tst: passthrough-dotnet: fix security

This commit is contained in:
Bill Zissimopoulos 2017-04-10 21:33:31 -07:00
parent 67711082b0
commit 259f2bf1c1

View File

@ -153,17 +153,26 @@ namespace passthrough
} }
public void SetSecurityDescriptor(AccessControlSections Sections, Byte[] SecurityDescriptor) public void SetSecurityDescriptor(AccessControlSections Sections, Byte[] SecurityDescriptor)
{ {
Int32 SecurityInformation = 0;
if (0 != (Sections & AccessControlSections.Owner))
SecurityInformation |= 1/*OWNER_SECURITY_INFORMATION*/;
if (0 != (Sections & AccessControlSections.Group))
SecurityInformation |= 2/*GROUP_SECURITY_INFORMATION*/;
if (0 != (Sections & AccessControlSections.Access))
SecurityInformation |= 4/*DACL_SECURITY_INFORMATION*/;
if (0 != (Sections & AccessControlSections.Audit))
SecurityInformation |= 8/*SACL_SECURITY_INFORMATION*/;
if (null != Stream) if (null != Stream)
{ {
FileSecurity Security = Stream.GetAccessControl(); if (!SetKernelObjectSecurity(Stream.SafeFileHandle.DangerousGetHandle(),
Security.SetSecurityDescriptorBinaryForm(SecurityDescriptor, Sections); SecurityInformation, SecurityDescriptor))
Stream.SetAccessControl(Security); ThrowIoExceptionWithWin32(Marshal.GetLastWin32Error());
} }
else else
{ {
DirectorySecurity Security = DirInfo.GetAccessControl(); if (!SetFileSecurityW(DirInfo.FullName,
Security.SetSecurityDescriptorBinaryForm(SecurityDescriptor, Sections); SecurityInformation, SecurityDescriptor))
DirInfo.SetAccessControl(Security); ThrowIoExceptionWithWin32(Marshal.GetLastWin32Error());
} }
} }
public void SetDisposition(Boolean Safe) public void SetDisposition(Boolean Safe)
@ -234,6 +243,16 @@ namespace passthrough
Int32 FileInformationClass, Int32 FileInformationClass,
ref FILE_DISPOSITION_INFO lpFileInformation, ref FILE_DISPOSITION_INFO lpFileInformation,
UInt32 dwBufferSize); UInt32 dwBufferSize);
[DllImport("advapi32.dll", SetLastError = true)]
private static extern Boolean SetFileSecurityW(
[MarshalAs(UnmanagedType.LPWStr)] String FileName,
Int32 SecurityInformation,
Byte[] SecurityDescriptor);
[DllImport("advapi32.dll", SetLastError = true)]
private static extern Boolean SetKernelObjectSecurity(
IntPtr Handle,
Int32 SecurityInformation,
Byte[] SecurityDescriptor);
} }
private class DirectoryEntryComparer : IComparer private class DirectoryEntryComparer : IComparer