x/sys/windows: NTUnicodeString of PROCESS_BASIC_INFORMATION.PebBaseAddress.ProcessParameters.CommandLine is incorrectly converted to slice #73460
Labels
BugReport
Issues describing a possible bug in the Go implementation.
compiler/runtime
Issues related to the Go compiler and/or runtime.
NeedsInvestigation
Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Milestone
Go version
go version go1.24.2 windows/amd64
Output of
go env
in your module/workspace:What did you do?
I wanted to write a function that wipes the command line in the Windows Process Environment Block so that sensible information like password or key parameters do not show up when processes are listed.
The package
golang.org/x/sys/windows
has all that is needed to accomplish this.Getting at the command line in the PEB is easily done with this code:
So far, so good.
CommandLine
is aNTUnicodeString
structure. It has the fieldsBuffer
,Length
andMaximumLength
and the convenience functionsSlice
andString
to convert theBuffer
into something useable by Go.However, there is a caveat: As documented in Microsoft Learn the fields
Length
andMaximumLength
count the lengths in units ofbytes
, notwchar_t
(uint16
)!To get the character count one needs to halve these lengths.
The function
*NTUnicodeString.Slice()
ingolang.org\x\[email protected]\windows
has this code:I.e., it treats the lengths in units of
uint16
!What did you see happen?
When I convert the command line buffer to a slice with
*NTUnicodeString.Slice()
it has a length that is double the size of theBuffer
and it contains the content of the*NTUnicodeString.Buffer
twice.Changing the elements of this slice in the second half does not have any influence on the real PEB command line.
Changing the elements of the first half of this slice changes the real PEB command line.
What did you expect to see?
The slice returned by
*NTUnicodeString.Slice()
(and correspondingly the string returned by*NTUnicodeString.String()
) should have a length that is half the value of*NTUnicodeString.Length
and a capacity that is half the value of*NTUnicodeString.MaximumLength
. It should also contain only one copy of the command line and changes to it should immediately be visible in the PEB of the process.The text was updated successfully, but these errors were encountered: