Disable SafeHandles

This commit is contained in:
Haselnussbomber 2025-12-05 19:10:31 +01:00
parent 7cf20fe102
commit d94cacaac3
No known key found for this signature in database
GPG key ID: BB905BB49E7295D1
6 changed files with 28 additions and 22 deletions

View file

@ -263,7 +263,7 @@ public sealed class EntryPoint
var symbolPath = Path.Combine(info.AssetDirectory, "UIRes", "pdb"); var symbolPath = Path.Combine(info.AssetDirectory, "UIRes", "pdb");
var searchPath = $".;{symbolPath}"; var searchPath = $".;{symbolPath}";
var currentProcess = Windows.Win32.PInvoke.GetCurrentProcess_SafeHandle(); var currentProcess = Windows.Win32.PInvoke.GetCurrentProcess();
// Remove any existing Symbol Handler and Init a new one with our search path added // Remove any existing Symbol Handler and Init a new one with our search path added
Windows.Win32.PInvoke.SymCleanup(currentProcess); Windows.Win32.PInvoke.SymCleanup(currentProcess);

View file

@ -201,19 +201,19 @@ public abstract class Hook<T> : IDalamudHook where T : Delegate
if (EnvironmentConfiguration.DalamudForceMinHook) if (EnvironmentConfiguration.DalamudForceMinHook)
useMinHook = true; useMinHook = true;
using var moduleHandle = Windows.Win32.PInvoke.GetModuleHandle(moduleName); var moduleHandle = Windows.Win32.PInvoke.GetModuleHandle(moduleName);
if (moduleHandle.IsInvalid) if (moduleHandle.IsNull)
throw new Exception($"Could not get a handle to module {moduleName}"); throw new Exception($"Could not get a handle to module {moduleName}");
var procAddress = (nint)Windows.Win32.PInvoke.GetProcAddress(moduleHandle, exportName); var procAddress = Windows.Win32.PInvoke.GetProcAddress(moduleHandle, exportName);
if (procAddress == IntPtr.Zero) if (procAddress.IsNull)
throw new Exception($"Could not get the address of {moduleName}::{exportName}"); throw new Exception($"Could not get the address of {moduleName}::{exportName}");
procAddress = HookManager.FollowJmp(procAddress); var address = HookManager.FollowJmp(procAddress.Value);
if (useMinHook) if (useMinHook)
return new MinHookHook<T>(procAddress, detour, Assembly.GetCallingAssembly()); return new MinHookHook<T>(address, detour, Assembly.GetCallingAssembly());
else else
return new ReloadedHook<T>(procAddress, detour, Assembly.GetCallingAssembly()); return new ReloadedHook<T>(address, detour, Assembly.GetCallingAssembly());
} }
/// <summary> /// <summary>

View file

@ -1,4 +1,5 @@
{ {
"$schema": "https://aka.ms/CsWin32.schema.json", "$schema": "https://aka.ms/CsWin32.schema.json",
"useSafeHandles": false,
"allowMarshaling": false "allowMarshaling": false
} }

View file

@ -14,11 +14,11 @@ namespace Dalamud;
/// </remarks> /// </remarks>
public static class SafeMemory public static class SafeMemory
{ {
private static readonly SafeHandle Handle; private static readonly HANDLE Handle;
static SafeMemory() static SafeMemory()
{ {
Handle = Windows.Win32.PInvoke.GetCurrentProcess_SafeHandle(); Handle = Windows.Win32.PInvoke.GetCurrentProcess();
} }
/// <summary> /// <summary>
@ -30,7 +30,7 @@ public static class SafeMemory
/// <returns>Whether the read succeeded.</returns> /// <returns>Whether the read succeeded.</returns>
public static unsafe bool ReadBytes(IntPtr address, int count, out byte[] buffer) public static unsafe bool ReadBytes(IntPtr address, int count, out byte[] buffer)
{ {
if (Handle.IsClosed || Handle.IsInvalid) if (Handle.IsNull)
{ {
buffer = []; buffer = [];
return false; return false;
@ -41,7 +41,7 @@ public static class SafeMemory
{ {
UIntPtr bytesRead; UIntPtr bytesRead;
if (!Windows.Win32.PInvoke.ReadProcessMemory( if (!Windows.Win32.PInvoke.ReadProcessMemory(
(HANDLE)Handle.DangerousGetHandle(), Handle,
address.ToPointer(), address.ToPointer(),
p, p,
new UIntPtr((uint)count), new UIntPtr((uint)count),
@ -62,7 +62,7 @@ public static class SafeMemory
/// <returns>Whether the write succeeded.</returns> /// <returns>Whether the write succeeded.</returns>
public static unsafe bool WriteBytes(IntPtr address, byte[] buffer) public static unsafe bool WriteBytes(IntPtr address, byte[] buffer)
{ {
if (Handle.IsClosed || Handle.IsInvalid) if (Handle.IsNull)
return false; return false;
if (buffer.Length == 0) if (buffer.Length == 0)
@ -72,7 +72,7 @@ public static class SafeMemory
fixed (byte* p = buffer) fixed (byte* p = buffer)
{ {
if (!Windows.Win32.PInvoke.WriteProcessMemory( if (!Windows.Win32.PInvoke.WriteProcessMemory(
(HANDLE)Handle.DangerousGetHandle(), Handle,
address.ToPointer(), address.ToPointer(),
p, p,
new UIntPtr((uint)buffer.Length), new UIntPtr((uint)buffer.Length),

View file

@ -48,33 +48,39 @@ public static class FilesystemUtil
// Open the temp file // Open the temp file
var tempPath = path + ".tmp"; var tempPath = path + ".tmp";
using var tempFile = Windows.Win32.PInvoke.CreateFile( var tempFile = Windows.Win32.PInvoke.CreateFile(
tempPath, tempPath,
(uint)(FILE_ACCESS_RIGHTS.FILE_GENERIC_READ | FILE_ACCESS_RIGHTS.FILE_GENERIC_WRITE), (uint)(FILE_ACCESS_RIGHTS.FILE_GENERIC_READ | FILE_ACCESS_RIGHTS.FILE_GENERIC_WRITE),
FILE_SHARE_MODE.FILE_SHARE_NONE, FILE_SHARE_MODE.FILE_SHARE_NONE,
null, null,
FILE_CREATION_DISPOSITION.CREATE_ALWAYS, FILE_CREATION_DISPOSITION.CREATE_ALWAYS,
FILE_FLAGS_AND_ATTRIBUTES.FILE_ATTRIBUTE_NORMAL, FILE_FLAGS_AND_ATTRIBUTES.FILE_ATTRIBUTE_NORMAL,
null); HANDLE.Null);
if (tempFile.IsInvalid) if (tempFile.IsNull)
throw new Win32Exception(); throw new Win32Exception();
// Write the data // Write the data
uint bytesWritten = 0; uint bytesWritten = 0;
fixed (byte* ptr = bytes) fixed (byte* ptr = bytes)
{ {
if (!Windows.Win32.PInvoke.WriteFile((HANDLE)tempFile.DangerousGetHandle(), ptr, (uint)bytes.Length, &bytesWritten, null)) if (!Windows.Win32.PInvoke.WriteFile(tempFile, ptr, (uint)bytes.Length, &bytesWritten, null))
throw new Win32Exception(); throw new Win32Exception();
} }
if (bytesWritten != bytes.Length) if (bytesWritten != bytes.Length)
{
Windows.Win32.PInvoke.CloseHandle(tempFile);
throw new Exception($"Could not write all bytes to temp file ({bytesWritten} of {bytes.Length})"); throw new Exception($"Could not write all bytes to temp file ({bytesWritten} of {bytes.Length})");
}
if (!Windows.Win32.PInvoke.FlushFileBuffers(tempFile)) if (!Windows.Win32.PInvoke.FlushFileBuffers(tempFile))
{
Windows.Win32.PInvoke.CloseHandle(tempFile);
throw new Win32Exception(); throw new Win32Exception();
}
tempFile.Close(); Windows.Win32.PInvoke.CloseHandle(tempFile);
if (!Windows.Win32.PInvoke.MoveFileEx(tempPath, path, MOVE_FILE_FLAGS.MOVEFILE_REPLACE_EXISTING | MOVE_FILE_FLAGS.MOVEFILE_WRITE_THROUGH)) if (!Windows.Win32.PInvoke.MoveFileEx(tempPath, path, MOVE_FILE_FLAGS.MOVEFILE_REPLACE_EXISTING | MOVE_FILE_FLAGS.MOVEFILE_WRITE_THROUGH))
throw new Win32Exception(); throw new Win32Exception();

View file

@ -858,7 +858,7 @@ public static partial class Util
var sizeWithTerminators = pathBytesSize + (pathBytes.Length * 2); var sizeWithTerminators = pathBytesSize + (pathBytes.Length * 2);
var dropFilesSize = sizeof(DROPFILES); var dropFilesSize = sizeof(DROPFILES);
var hGlobal = Win32_PInvoke.GlobalAlloc_SafeHandle( var hGlobal = Win32_PInvoke.GlobalAlloc(
GLOBAL_ALLOC_FLAGS.GHND, GLOBAL_ALLOC_FLAGS.GHND,
// struct size + size of encoded strings + null terminator for each // struct size + size of encoded strings + null terminator for each
// string + two null terminators for end of list // string + two null terminators for end of list
@ -896,12 +896,11 @@ public static partial class Util
{ {
Win32_PInvoke.SetClipboardData( Win32_PInvoke.SetClipboardData(
(uint)CLIPBOARD_FORMAT.CF_HDROP, (uint)CLIPBOARD_FORMAT.CF_HDROP,
hGlobal); (Windows.Win32.Foundation.HANDLE)hGlobal.Value);
Win32_PInvoke.CloseClipboard(); Win32_PInvoke.CloseClipboard();
return true; return true;
} }
hGlobal.Dispose();
return false; return false;
} }