| 1 | using System; |
| 2 | using System.ComponentModel; |
| 3 | using System.IO; |
| 4 | using System.Runtime.InteropServices; |
| 5 | using Microsoft.Win32.SafeHandles; |
| 6 | |
| 7 | public static class CageVerifiedFile |
| 8 | { |
| 9 | [StructLayout(LayoutKind.Sequential)] private struct UnicodeString { public ushort Length, MaximumLength; public IntPtr Buffer; } |
| 10 | [StructLayout(LayoutKind.Sequential)] private struct ObjectAttributes |
| 11 | { |
| 12 | public int Length; |
| 13 | public IntPtr RootDirectory, ObjectName; |
| 14 | public uint Attributes; |
| 15 | public IntPtr SecurityDescriptor, SecurityQualityOfService; |
| 16 | } |
| 17 | [StructLayout(LayoutKind.Sequential)] private struct IOStatus { public IntPtr Status, Information; } |
| 18 | [StructLayout(LayoutKind.Sequential)] private struct AttributeTag { public uint Attributes, Tag; } |
| 19 | |
| 20 | [DllImport("kernel32.dll", CharSet = CharSet.Unicode, SetLastError = true)] |
| 21 | private static extern SafeFileHandle CreateFile(string path, uint access, uint share, IntPtr security, uint disposition, uint flags, IntPtr template); |
| 22 | [DllImport("kernel32.dll", SetLastError = true)] |
| 23 | private static extern bool GetFileInformationByHandleEx(SafeFileHandle file, int kind, out AttributeTag info, uint size); |
| 24 | [DllImport("kernel32.dll", SetLastError = true)] |
| 25 | private static extern bool SetFileInformationByHandle(SafeFileHandle file, int kind, ref byte delete, uint size); |
| 26 | [DllImport("ntdll.dll")] |
| 27 | private static extern int NtCreateFile(out SafeFileHandle file, uint access, ref ObjectAttributes attributes, |
| 28 | out IOStatus status, IntPtr allocationSize, uint fileAttributes, uint share, uint disposition, uint options, IntPtr ea, uint eaLength); |
| 29 | [DllImport("ntdll.dll")] private static extern uint RtlNtStatusToDosError(int status); |
| 30 | |
| 31 | public static FileStream Open(string root, string relative) |
| 32 | { |
| 33 | if (String.IsNullOrEmpty(relative) || Path.IsPathRooted(relative) || relative.IndexOf(':') >= 0) |
| 34 | throw new IOException("Invalid relative installation path"); |
| 35 | foreach (string part in relative.Replace('/', '\\').Split('\\')) |
| 36 | if (part == ".." || part == "." || part.Length == 0) throw new IOException("Invalid relative installation path"); |
| 37 | |
| 38 | |
| 39 | |
| 40 | using (SafeFileHandle directory = CreateFile(root, 0x80000000, 7, IntPtr.Zero, 3, 0x02200000, IntPtr.Zero)) |
| 41 | { |
| 42 | if (directory.IsInvalid) throw new Win32Exception(Marshal.GetLastWin32Error()); |
| 43 | AttributeTag tag; |
| 44 | if (!GetFileInformationByHandleEx(directory, 9, out tag, 8)) throw new Win32Exception(Marshal.GetLastWin32Error()); |
| 45 | if ((tag.Attributes & 0x400) != 0) throw new IOException("Installation directory is a reparse point"); |
| 46 | IntPtr text = Marshal.StringToHGlobalUni(relative.Replace('/', '\\')); |
| 47 | IntPtr name = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(UnicodeString))); |
| 48 | try |
| 49 | { |
| 50 | UnicodeString value = new UnicodeString { Length = checked((ushort)(relative.Length * 2)), MaximumLength = checked((ushort)(relative.Length * 2)), Buffer = text }; |
| 51 | Marshal.StructureToPtr(value, name, false); |
| 52 | ObjectAttributes attributes = new ObjectAttributes { |
| 53 | Length = Marshal.SizeOf(typeof(ObjectAttributes)), RootDirectory = directory.DangerousGetHandle(), ObjectName = name, Attributes = 0x1040, |
| 54 | }; |
| 55 | IOStatus status; |
| 56 | SafeFileHandle file; |
| 57 | |
| 58 | |
| 59 | int result = NtCreateFile(out file, 0x80110000, ref attributes, out status, IntPtr.Zero, 0, 5, 1, 0x00200060, IntPtr.Zero, 0); |
| 60 | if (result < 0) { if (file != null) file.Dispose(); throw new Win32Exception((int)RtlNtStatusToDosError(result)); } |
| 61 | try |
| 62 | { |
| 63 | if (!GetFileInformationByHandleEx(file, 9, out tag, 8)) throw new Win32Exception(Marshal.GetLastWin32Error()); |
| 64 | if ((tag.Attributes & 0x410) != 0) throw new IOException("Installation file is a directory or reparse point"); |
| 65 | return new FileStream(file, FileAccess.Read); |
| 66 | } |
| 67 | catch { file.Dispose(); throw; } |
| 68 | } |
| 69 | finally { Marshal.FreeHGlobal(name); Marshal.FreeHGlobal(text); } |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | public static void Delete(FileStream file) |
| 74 | { |
| 75 | byte delete = 1; |
| 76 | if (!SetFileInformationByHandle(file.SafeFileHandle, 4, ref delete, 1)) throw new Win32Exception(Marshal.GetLastWin32Error()); |
| 77 | } |
| 78 | } |