Ejemplo n.º 1
0
        private static unsafe void DoQueuedCompletionWork(
            ISwatcherConfig config, IWindowsFacade facade, SafeLocalMemHandle completionPortHandle,
            ISubject <SwatcherCreatedEventArgs> createdSubject, ISubject <SwatcherEventArgs> deletedSubject,
            ISubject <SwatcherEventArgs> changedSubject, ISubject <RenamedInfo> renamedSubject)
        {
            //this is a blocking call...
            var completionStatus = WaitForCompletionStatus(facade, completionPortHandle);

            if (completionStatus == null)
            {
                return;
            }

            var overlapped    = Overlapped.Unpack(completionStatus.Value.OverlappedPointer);
            var asyncResult   = (SwatcherAsyncResult)overlapped.AsyncResult;
            var currentOffset = 0;
            // ReSharper disable once TooWideLocalVariableScope
            var nextOffset = 0;

            try
            {
                do
                {
                    nextOffset = DeserializeMessage(
                        config, asyncResult, ref currentOffset, createdSubject,
                        deletedSubject, changedSubject, renamedSubject);
                } while (nextOffset != 0);
            }
            finally
            {
                //*always* free up the overlapped. otherwise, we will have a ~65kb memory leak after each callback.
                Overlapped.Free(completionStatus.Value.OverlappedPointer);
            }
        }
Ejemplo n.º 2
0
        private static void CreateTempDirectoryWithAce(string directory, string identity)
        {
            // Dacl Sddl string:
            // D: Dacl type
            // D; Deny access
            // OI; Object inherit ace
            // SD; Standard delete function
            // wIdentity.User Sid of the given user.
            // A; Allow access
            // OICI; Object inherit, container inherit
            // FA File access
            // BA Built-in administrators
            // S: Sacl type
            // ML;; Mandatory Label
            // NW;;; No write policy
            // HI High integrity processes only
            string sddl = "D:(D;OI;SD;;;" + identity + ")(A;OICI;FA;;;BA)S:(ML;OI;NW;;;HI)";

            SafeLocalMemHandle acl = null;

            SafeLocalMemHandle.ConvertStringSecurityDescriptorToSecurityDescriptor(sddl, NativeMethods.SDDL_REVISION_1, out acl, IntPtr.Zero);

            // Create the directory with the acl
            NativeMethods.CreateDirectory(directory, acl);
        }
Ejemplo n.º 3
0
        private static void RemoveAceOnTempDirectory(string directory, string identity)
        {
            string sddl = "D:(A;OICI;FA;;;" + identity + ")(A;OICI;FA;;;BA)";

            SafeLocalMemHandle acl = null;

            SafeLocalMemHandle.ConvertStringSecurityDescriptorToSecurityDescriptor(sddl, NativeMethods.SDDL_REVISION_1, out acl, IntPtr.Zero);

            NativeMethods.SetNamedSecurityInfo(directory, acl);
        }
Ejemplo n.º 4
0
        private async Task SetCompletionPortHandle(int completionKey)
        {
            await Task.Run(() =>
            {
                //if the completion port doesn't exist yet, this call will create it.
                //if it already exists, this call will bind the directory handle to the completion port,
                //whilst retaining any other directory handles that were previously bound.
                //passing 0 to the last parameter uses the default number of concurrent threads, which
                //is the number of CPUs on the machine.
                var pointer = WindowsFacade.CreateIoCompletionPort(
                    DirectoryHandle, SafeLocalMemHandle.Empty,
                    (uint)completionKey, (uint)Environment.ProcessorCount);

                CompletionPortHandle = new SafeLocalMemHandle(pointer);
            })
            .ConfigureAwait(false);
        }
Ejemplo n.º 5
0
        /// <summary>
        /// CreateNativePipeSecurity creates and initializes a new SECURITY_ATTRIBUTES object to allow Authenticated Users read and
        /// write access to a pipe, and to allow the Administrators group full access to the pipe. Windows only.
        /// </summary>
        /// <returns>
        /// A SECURITY_ATTRIBUTES object that allows Authenticated Users read and write access to a pipe, and allows the Administrators group full access to the pipe.
        /// </returns>
        static SECURITY_ATTRIBUTES CreateNativePipeSecurity()
        {
            // Define the SDDL for the security descriptor.
            string sddl = "D:" +                 // Discretionary ACL
                          "(A;OICI;GRGW;;;AU)" + // Allow read/write to authenticated users
                          "(A;OICI;GA;;;BA)";    // Allow full control to administrators

            SafeLocalMemHandle pSecurityDescriptor = null;

            if (!NativeMethod.ConvertStringSecurityDescriptorToSecurityDescriptor(sddl, 1, out pSecurityDescriptor, IntPtr.Zero))
            {
                throw new Win32Exception();
            }

            SECURITY_ATTRIBUTES sa = new SECURITY_ATTRIBUTES();

            sa.nLength = Marshal.SizeOf(sa);
            sa.lpSecurityDescriptor = pSecurityDescriptor;
            sa.bInheritHandle       = false;
            return(sa);
        }
Ejemplo n.º 6
0
        private static unsafe QueuedCompletionStatus?WaitForCompletionStatus(
            IWindowsFacade facade, SafeLocalMemHandle completionPortHandle)
        {
            uint bytesRead;
            uint completionKey;
            NativeOverlapped *overlappedPointer;

            //when change events have occurred, the OS will callback on this blocking method.
            //the key is that we provide the completion port handle, which has been bound to the
            //directory handle being watched (see InitializeCompletionPort() method). we get our change data
            //by passing a pointer by reference to our nativeoverlapped -> IAsyncResult -> buffer.
            var result = facade.GetQueuedCompletionStatus(
                completionPortHandle, out bytesRead, out completionKey,
                &overlappedPointer, InfiniteTimeout);

            //I don't think that this has ever returned false during testing.
            //if (!result)
            //    Debugger.Break();

            if (completionKey == StopIocpThreads)
            {
                Logger.Trace($"Stopping {Thread.CurrentThread.Name}");
                Thread.CurrentThread.Abort();
                return(null);
            }
            if (bytesRead == 0)
            {
                return(null);
            }

            return(new QueuedCompletionStatus()
            {
                BytesRead = bytesRead,
                CompletionKey = completionKey,
                OverlappedPointer = overlappedPointer
            });
        }
Ejemplo n.º 7
0
 public SECURITY_ATTRIBUTES()
 {
     this.nLength              = 12;
     this.bInheritHandle       = true;
     this.lpSecurityDescriptor = new SafeLocalMemHandle(IntPtr.Zero, true);
 }
Ejemplo n.º 8
0
 internal SecurityAttributes()
 {
     this.nLength              = 12;
     this.bInheritHandle       = true;
     this.lpSecurityDescriptor = new SafeLocalMemHandle(IntPtr.Zero, true);
 }
Ejemplo n.º 9
0
 public static extern bool ConvertStringSecurityDescriptorToSecurityDescriptor(
     string sddlSecurityDescriptor, int sddlRevision,
     out SafeLocalMemHandle pSecurityDescriptor,
     IntPtr securityDescriptorSize);
Ejemplo n.º 10
0
 internal static extern bool ConvertStringSidToSid(string stringSid, out SafeLocalMemHandle ByteArray);
Ejemplo n.º 11
0
 internal static extern int GetLengthSid(SafeLocalMemHandle sid);
Ejemplo n.º 12
0
 internal static extern bool ConvertStringSecurityDescriptorToSecurityDescriptor(string stringSecurityDescriptor, int stringSDRevision, out SafeLocalMemHandle securityDescriptor, IntPtr securityDescriptorSize);
 internal static extern unsafe bool ConvertStringSecurityDescriptorToSecurityDescriptor(
     string StringSecurityDescriptor,
     int StringSDRevision,
     out SafeLocalMemHandle pSecurityDescriptor,
     IntPtr SecurityDescriptorSize);