Skip to content

Commit 98dc366

Browse files
committed
fix(process): Solidify env var parsing
If the env variable contains multiple = symbols, we make sure the split will always produce two substrings. When reading the env block, use the buffer size as the size of the read memory area.
1 parent f1aca37 commit 98dc366

File tree

1 file changed

+5
-4
lines changed

1 file changed

+5
-4
lines changed

pkg/ps/peb.go

+5-4
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,8 @@ func (p PEB) GetEnvs() map[string]string {
110110
}
111111
start, end := 0, 0
112112
envs := make(map[string]string)
113-
s := readUTF16(p.proc, uintptr(p.procParams.Environment), uint32(p.procParams.EnvironmentSize))
113+
l := uint32(p.procParams.EnvironmentSize)
114+
s := readUTF16(p.proc, uintptr(p.procParams.Environment), l)
114115
for i, r := range s {
115116
// each env variable key/value pair terminates with the NUL character
116117
if r == 0 {
@@ -123,7 +124,7 @@ func (p PEB) GetEnvs() map[string]string {
123124
break
124125
}
125126
env := string(utf16.Decode(s[start:end]))
126-
if kv := strings.Split(env, "="); len(kv) == 2 {
127+
if kv := strings.SplitN(env, "=", 2); len(kv) == 2 {
127128
envs[kv[0]] = kv[1]
128129
}
129130
start = end + 1
@@ -133,8 +134,8 @@ func (p PEB) GetEnvs() map[string]string {
133134
}
134135

135136
func readUTF16(proc windows.Handle, addr uintptr, size uint32) []uint16 {
136-
b := make([]byte, size*2)
137-
err := windows.ReadProcessMemory(proc, addr, &b[0], uintptr(size), nil)
137+
b := make([]byte, size*2+1)
138+
err := windows.ReadProcessMemory(proc, addr, &b[0], uintptr(len(b)), nil)
138139
if err != nil {
139140
return nil
140141
}

0 commit comments

Comments
 (0)