Skip to content

Commit 97e4992

Browse files
committed
refactor: slightly improve getting process command lines
It still fails more often than not due to crossing into unreadable memory. I'll look into it later.
1 parent 79397ec commit 97e4992

File tree

1 file changed

+8
-10
lines changed

1 file changed

+8
-10
lines changed

deadlock-dotnet-sdk/Domain/SafeHandleEx.cs

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,9 @@ internal SafeHandleEx(NativeMethods.SYSTEM_HANDLE_TABLE_ENTRY_INFO_EX sysHandleE
5050
);
5151

5252
if (rawHandle.IsNull)
53-
throw new Win32Exception("Failed to open process handle with access rights 'PROCESS_QUERY_LIMITED_INFORMATION' and 'PROCESS_VM_READ'. The following information will be unavailable: main module full name, process name, ");
53+
throw new Win32Exception("Failed to open process handle with access rights 'PROCESS_QUERY_LIMITED_INFORMATION' and 'PROCESS_VM_READ'. The following information will be unavailable: main module full name, process name, process' startup command line");
5454

55-
SafeProcessHandle hProcess = new(rawHandle, true);
55+
using SafeProcessHandle hProcess = new(rawHandle, true);
5656

5757
/** Get main module's full path */
5858
ProcessMainModulePath = GetFullProcessImageName(hProcess);
@@ -64,7 +64,7 @@ internal SafeHandleEx(NativeMethods.SYSTEM_HANDLE_TABLE_ENTRY_INFO_EX sysHandleE
6464
}
6565

6666
/** Get process's possibly-overwritten command line from the PEB struct in its memory space */
67-
GetProcessCommandLine(hProcess);
67+
ProcessCommandLine = GetProcessCommandLine(hProcess);
6868
}
6969
catch (Exception e)
7070
{
@@ -89,8 +89,7 @@ internal SafeHandleEx(NativeMethods.SYSTEM_HANDLE_TABLE_ENTRY_INFO_EX sysHandleE
8989
/// </summary>
9090
/// <value></value>
9191
public string? HandleObjectType { get; }
92-
93-
public string? ProcessCommandLine { get; private set; }
92+
public string? ProcessCommandLine { get; init; }
9493
public string? ProcessMainModulePath { get; }
9594
public string? ProcessName { get; }
9695

@@ -154,15 +153,14 @@ public void UnlockSystemHandle()
154153
/// <exception cref="NotImplementedException">Reading a 64-bit process's PEB from a 32-bit process (under WOW64) is not yet implemented.</exception>
155154
/// <exception cref="Win32Exception">Failed to read the process's PEB in memory. While trying to read the PEB, the operation crossed into an area of the process that is inaccessible.</exception>
156155
/// <exception cref="Exception">NtQueryInformationProcess failed to query the process's 'PROCESS_BASIC_INFORMATION'</exception>
157-
private unsafe void GetProcessCommandLine(SafeProcessHandle hProcess)
156+
private unsafe static string GetProcessCommandLine(SafeProcessHandle hProcess)
158157
{
159158
/* Get PROCESS_BASIC_INFORMATION */
160159
uint sysInfoLength = (uint)Marshal.SizeOf<PROCESS_BASIC_INFORMATION>();
161160
PROCESS_BASIC_INFORMATION processBasicInfo;
162161
IntPtr sysInfo = Marshal.AllocHGlobal((int)sysInfoLength);
163162
NTSTATUS status = (NTSTATUS)0;
164163
uint retLength = 0;
165-
166164
if ((status = NtQueryInformationProcess(
167165
hProcess,
168166
PROCESSINFOCLASS.ProcessBasicInformation,
@@ -171,7 +169,7 @@ private unsafe void GetProcessCommandLine(SafeProcessHandle hProcess)
171169
ref retLength))
172170
.IsSuccessful)
173171
{
174-
processBasicInfo = Marshal.PtrToStructure<PROCESS_BASIC_INFORMATION>(sysInfo);
172+
processBasicInfo = Marshal.PtrToStructure<PROCESS_BASIC_INFORMATION>(sysInfo); //DevSkim: ignore DS104456
175173

176174
// if our process is WOW64, we need to account for different pointer sizes if
177175
// the target process is 64-bit
@@ -190,8 +188,8 @@ private unsafe void GetProcessCommandLine(SafeProcessHandle hProcess)
190188
IntPtr buf = Marshal.AllocHGlobal(sizeof(PEB));
191189
if (ReadProcessMemory(hProcess, processBasicInfo.PebBaseAddress, (void*)buf, (nuint)sizeof(PEB), null))
192190
{
193-
PEB peb = Marshal.PtrToStructure<PEB>(buf);
194-
ProcessCommandLine = (*peb.ProcessParameters).CommandLine.ToStringLength();
191+
PEB peb = Marshal.PtrToStructure<PEB>(buf); //DevSkim: ignore DS104456
192+
return (*peb.ProcessParameters).CommandLine.ToStringLength();
195193
}
196194
else
197195
{

0 commit comments

Comments
 (0)