Esempio n. 1
0
        private static GenericSafeHandle GetReadHandle(int id, bool idIsProcessID)
        {
            IntPtr tempHandle = IntPtr.Zero;
            IntPtr readHandle = IntPtr.Zero;

            if (idIsProcessID)
            {
                GenericSafeHandle procHandle = new GenericSafeHandle(NativeMethods.OpenProcess((int)ProcessRights.QueryInformation, false, id));
                if (procHandle.IsInvalid)
                {
                    int err = Marshal.GetLastWin32Error();
                    switch (err)
                    {
                        case NativeConstants.ERROR_ACCESS_DENIED:
                            throw new UnauthorizedAccessException();
                        default:
                            throw new System.ComponentModel.Win32Exception(err);
                    }
                }
                using (procHandle)
                {
                    if (!NativeMethods.OpenProcessToken(procHandle, (int)TokenRights.ReadPermissions, out readHandle))
                    {
                        int err = Marshal.GetLastWin32Error();
                        switch (err)
                        {
                            case NativeConstants.ERROR_ACCESS_DENIED:
                                throw new UnauthorizedAccessException();
                            default:
                                throw new System.ComponentModel.Win32Exception(err);
                        }
                    }
                }
            }
            else
            {
                GenericSafeHandle threadHandle = new GenericSafeHandle(NativeMethods.OpenThread((int)ThreadRights.QueryInformation, false, id));
                if (threadHandle.IsInvalid)
                {
                    int err = Marshal.GetLastWin32Error();
                    switch (err)
                    {
                        case NativeConstants.ERROR_ACCESS_DENIED:
                            throw new UnauthorizedAccessException();
                        default:
                            throw new System.ComponentModel.Win32Exception(err);
                    }
                }
                using (threadHandle)
                {
                    if (!NativeMethods.OpenThreadToken(threadHandle, (int)TokenRights.ReadPermissions, false, out readHandle))
                    {
                        int err = Marshal.GetLastWin32Error();
                        switch (err)
                        {
                            case NativeConstants.ERROR_ACCESS_DENIED:
                                throw new UnauthorizedAccessException();
                            default:
                                throw new System.ComponentModel.Win32Exception(err);
                        }
                    }
                }
            }

            return new GenericSafeHandle(readHandle);
        }
 public GenericSafeHandle(IntPtr newHandle, GenericSafeHandle oldHandle)
     : this(newHandle, oldHandle.m_ReleaseCallback)
 {
     oldHandle.Close();
 }
Esempio n. 3
0
 internal BaseSecurity(GenericSafeHandle objectHandle, ResourceType resType, AccessControlSections sectionsRequested, bool isContainer)
     : base(isContainer, resType, objectHandle, sectionsRequested)
 {
     m_Handle = objectHandle;
 }
Esempio n. 4
0
 public GenericSafeHandle(IntPtr newHandle, GenericSafeHandle oldHandle)
     : this(newHandle, oldHandle.m_ReleaseCallback)
 {
     oldHandle.Close();
 }
Esempio n. 5
0
        private void MakeWriteableHandle(AccessControlSections sectionsToWrite)
        {
            StandardRights rightsRequired = m_HandleRights;
            bool newHandleRequired = false;

            if ((sectionsToWrite & AccessControlSections.Access) != 0 || (sectionsToWrite & AccessControlSections.Group) != 0)
            {
                if ((m_HandleRights & StandardRights.WritePermissions) == 0)
                {
                    rightsRequired |= StandardRights.WritePermissions;
                    newHandleRequired = true;
                }
            }
            if ((sectionsToWrite & AccessControlSections.Owner) != 0)
            {
                if ((m_HandleRights & StandardRights.TakeOwnership) == 0)
                {
                    rightsRequired |= StandardRights.TakeOwnership;
                    newHandleRequired = true;
                }
            }
            if ((sectionsToWrite & AccessControlSections.Audit) != 0)
            {
                if ((m_HandleRights & (StandardRights)NativeConstants.ACCESS_SYSTEM_SECURITY) == 0)
                {
                    rightsRequired |= (StandardRights)NativeConstants.ACCESS_SYSTEM_SECURITY;
                    newHandleRequired = true;
                }
            }

            if (newHandleRequired)
            {
                IntPtr writeHandle = NativeMethods.DuplicateHandle(m_Handle.DangerousGetHandle(), (int)rightsRequired);
                if (writeHandle == IntPtr.Zero)
                {
                    int err = Marshal.GetLastWin32Error();
                    switch (err)
                    {
                        case NativeConstants.ERROR_ACCESS_DENIED:
                            throw new UnauthorizedAccessException();
                        default:
                            throw new System.ComponentModel.Win32Exception(err);
                    }
                }

                try
                {
                    m_Handle = new GenericSafeHandle(writeHandle, m_Handle);
                    m_HandleRights = rightsRequired;
                }
                catch
                {
                    //Should only happen if out of memory. Release new handle and rethrow.
                    NativeMethods.CloseHandle(writeHandle);
                    throw;
                }
            }
        }