using System.ComponentModel; using System.Reflection; using System.Runtime.InteropServices; using Microsoft.Win32.SafeHandles; using XFEToolBox.Indexing.Service.Installation; internal static class InstallerSourceSafetyTests { internal static int Run() { var root = Path.Combine(Path.GetTempPath(), "xfetoolbox-service-source-" + Guid.NewGuid().ToString("N")); var source = Path.Combine(root, "ordinary.dll"); var copied = Path.Combine(root, "copied.dll"); var hardLink = Path.Combine(root, "linked.dll"); var rejected = Path.Combine(root, "rejected.dll"); var junction = Path.Combine(root, "junction"); var copy = typeof(IndexingServiceInstaller).GetMethod("CopyBundleFile", BindingFlags.NonPublic | BindingFlags.Static)!; var pin = typeof(IndexingServiceInstaller).GetMethod("PinSourceDirectory", BindingFlags.NonPublic | BindingFlags.Static)!; var pins = new Dictionary(StringComparer.OrdinalIgnoreCase); var passed = 0; Directory.CreateDirectory(root); try { const string body = "ordinary user-owned portable bundle"; File.WriteAllText(source, body); var length = (long)copy.Invoke(null, [source, copied, 4096L])!; Check(length == new FileInfo(source).Length && File.ReadAllText(copied) == body, "Installer copies a normal user-owned source through its validated file handle"); if (!CreateHardLinkW(hardLink, source, IntPtr.Zero)) throw new Win32Exception(Marshal.GetLastWin32Error()); Check(Throws(() => copy.Invoke(null, [hardLink, rejected, 4096L])) && !File.Exists(rejected), "Installer rejects a real NTFS hard link before creating its public target"); File.Delete(hardLink); using (var writer = File.Open(source, FileMode.Open, FileAccess.Write, FileShare.ReadWrite | FileShare.Delete)) Check(Throws(() => copy.Invoke(null, [source, rejected, 4096L])) && !File.Exists(rejected), "Installer refuses to copy a source that another handle can still modify"); Check(Throws(() => copy.Invoke(null, [source, rejected, 1L])) && !File.Exists(rejected), "Installer checks the size of the opened file before copying"); Check(Throws(() => copy.Invoke(null, [source, copied, 4096L])) && File.ReadAllText(copied) == body, "Installer creates a new destination and never overwrites an existing file"); Junction.Create(junction, root); Check(Throws(() => copy.Invoke(null, [Path.Combine(junction, "ordinary.dll"), rejected, 4096L])) && !File.Exists(rejected), "Installer rejects a source file reached through an ancestor junction using final handle identity"); Check(Throws(() => pin.Invoke(null, [junction, pins])), "Installer refuses to pin a directory reparse point for enumeration"); Directory.Delete(junction); pin.Invoke(null, [root, pins]); Check(pins.Count == 1 && !pins[root].IsClosed && !pins[root].IsInvalid, "Installer pins a validated source directory without granting delete sharing"); var renamed = root + "-renamed"; var renameDenied = false; try { Directory.Move(root, renamed); } catch (IOException) { renameDenied = true; } if (!renameDenied) { foreach (var handle in pins.Values) handle.Dispose(); pins.Clear(); Directory.Move(renamed, root); } Check(renameDenied && Directory.Exists(root), "Pinned source directory cannot be renamed or replaced during enumeration"); } finally { foreach (var handle in pins.Values) handle.Dispose(); // Exact owned fixtures only. Never recursively follow a junction or delete user data. if (Directory.Exists(junction)) Directory.Delete(junction); foreach (var file in new[] { hardLink, rejected, copied, source }) if (File.Exists(file)) File.Delete(file); if (Directory.Exists(root)) Directory.Delete(root); } return passed; void Check(bool condition, string name) { if (!condition) throw new InvalidOperationException(name); passed++; Console.WriteLine("PASS " + name); } } private static bool Throws(Action action) where T : Exception { try { action(); return false; } catch (TargetInvocationException exception) when (exception.InnerException is T) { return true; } } [DllImport("kernel32.dll", CharSet = CharSet.Unicode, SetLastError = true)] [return: MarshalAs(UnmanagedType.Bool)] private static extern bool CreateHardLinkW(string newPath, string existingPath, IntPtr security); }