@@ -6,6 +6,7 @@ package process
66import (
77 "bufio"
88 "context"
9+ "encoding/binary"
910 "errors"
1011 "fmt"
1112 "io"
@@ -1204,14 +1205,82 @@ func (p *processReader) Read(buf []byte) (int, error) {
12041205
12051206func getProcessCommandLine (pid int32 ) (string , error ) {
12061207 h , err := windows .OpenProcess (processQueryInformation | windows .PROCESS_VM_READ , false , uint32 (pid ))
1207- if errors .Is (err , windows .ERROR_ACCESS_DENIED ) || errors .Is (err , windows .ERROR_INVALID_PARAMETER ) {
1208+ if err == nil {
1209+ defer syscall .CloseHandle (syscall .Handle (h ))
1210+
1211+ cmdLine , pebErr := getProcessCommandLinePEB (h )
1212+ if pebErr == nil {
1213+ return cmdLine , nil
1214+ }
1215+
1216+ // PEB read failed even though the handle opened. VM_READ may
1217+ // have been granted but ineffective against this process's
1218+ // protection driver. Fall back to the native query on the same handle.
1219+ if cmdLine , err := getProcessCommandLineNative (h , pid ); err == nil {
1220+ return cmdLine , nil
1221+ }
1222+ return "" , pebErr
1223+ }
1224+
1225+ if errors .Is (err , windows .ERROR_INVALID_PARAMETER ) {
12081226 return "" , nil
12091227 }
1210- if err != nil {
1228+ if ! errors . Is ( err , windows . ERROR_ACCESS_DENIED ) {
12111229 return "" , err
12121230 }
1213- defer syscall .CloseHandle (syscall .Handle (h ))
12141231
1232+ // fallback in case it's a genuine PPL process, where
1233+ // PROCESS_VM_READ itself is denied at OpenProcess time. Retry with
1234+ // just PROCESS_QUERY_LIMITED_INFORMATION, which PPL still grants.
1235+ lh , lerr := windows .OpenProcess (processQueryInformation , false , uint32 (pid ))
1236+ if lerr != nil {
1237+ return "" , nil
1238+ }
1239+ defer syscall .CloseHandle (syscall .Handle (lh ))
1240+ cmdLine , _ := getProcessCommandLineNative (lh , pid )
1241+ return cmdLine , nil
1242+ }
1243+
1244+ func getProcessCommandLineNative (handle windows.Handle , pid int32 ) (string , error ) {
1245+ var returnLength uint32
1246+ // first call is to get the actual returnLength
1247+ err := windows .NtQueryInformationProcess (
1248+ handle ,
1249+ windows .ProcessCommandLineInformation ,
1250+ nil ,
1251+ 0 ,
1252+ & returnLength ,
1253+ )
1254+ if err != nil &&
1255+ ! errors .Is (err , windows .STATUS_INFO_LENGTH_MISMATCH ) &&
1256+ ! errors .Is (err , windows .STATUS_BUFFER_OVERFLOW ) &&
1257+ ! errors .Is (err , windows .STATUS_BUFFER_TOO_SMALL ) {
1258+ return "" , fmt .Errorf ("querying command line size for pid %d: %w" , pid , err )
1259+ }
1260+ if returnLength == 0 {
1261+ return "" , fmt .Errorf ("process %d returned zero-length command line info" , pid )
1262+ }
1263+
1264+ // second call: actually fetch it into a correctly-sized buffer
1265+ buf := make ([]byte , returnLength )
1266+ err = windows .NtQueryInformationProcess (
1267+ handle ,
1268+ windows .ProcessCommandLineInformation ,
1269+ unsafe .Pointer (& buf [0 ]),
1270+ returnLength ,
1271+ & returnLength ,
1272+ )
1273+ if err != nil {
1274+ return "" , fmt .Errorf ("reading command line for pid %d: %w" , pid , err )
1275+ }
1276+ cmdLine , err := parseCommandLineInformation (buf )
1277+ if err != nil {
1278+ return "" , fmt .Errorf ("parsing command line for pid %d: %w" , pid , err )
1279+ }
1280+ return cmdLine , nil
1281+ }
1282+
1283+ func getProcessCommandLinePEB (h windows.Handle ) (string , error ) {
12151284 procIs32Bits := is32BitProcess (h )
12161285
12171286 if procIs32Bits {
@@ -1258,3 +1327,19 @@ func convertUTF16ToString(src []byte) string {
12581327 }
12591328 return syscall .UTF16ToString (codePoints )
12601329}
1330+
1331+ func parseCommandLineInformation (buf []byte ) (string , error ) {
1332+ if len (buf ) < 2 {
1333+ return "" , errors .New ("command line buffer too small to read length" )
1334+ }
1335+ length := binary .LittleEndian .Uint16 (buf [0 :2 ])
1336+ if length == 0 {
1337+ return "" , nil
1338+ }
1339+ strOffset := int (unsafe .Sizeof (windows.NTUnicodeString {}))
1340+ strEnd := strOffset + int (length )
1341+ if strEnd > len (buf ) {
1342+ return "" , errors .New ("command line length exceeds buffer size" )
1343+ }
1344+ return convertUTF16ToString (buf [strOffset :strEnd ]), nil
1345+ }
0 commit comments