Skip to content

Commit 61ae0a3

Browse files
neildmdempsky
authored andcommitted
syscall, os/exec: reject environment variables containing NULs
Check for and reject environment variables containing NULs. The conventions for passing environment variables to subprocesses cause most or all systems to interpret a NUL as a separator. The syscall package rejects environment variables containing a NUL on most systems, but erroniously did not do so on Windows. This causes an environment variable such as "FOO=a\x00BAR=b" to be interpreted as "FOO=a", "BAR=b". Check for and reject NULs in environment variables passed to syscall.StartProcess on Windows. Add a redundant check to os/exec as extra insurance. Fixes #56284 Fixes CVE-2022-41716 Change-Id: I2950e2b0cb14ebd26e5629be1521858f66a7d4ae Reviewed-on: https://team-review.git.corp.google.com/c/golang/go-private/+/1609434 Run-TryBot: Damien Neil <[email protected]> Reviewed-by: Tatiana Bradley <[email protected]> Reviewed-by: Roland Shoemaker <[email protected]> TryBot-Result: Security TryBots <[email protected]> Reviewed-on: https://go-review.googlesource.com/c/go/+/446916 Reviewed-by: Tatiana Bradley <[email protected]> TryBot-Result: Gopher Robot <[email protected]> Run-TryBot: Matthew Dempsky <[email protected]> Reviewed-by: Heschi Kreinick <[email protected]>
1 parent ad5d2f6 commit 61ae0a3

File tree

4 files changed

+52
-15
lines changed

4 files changed

+52
-15
lines changed

src/os/exec/env_test.go

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,10 @@ func TestDedupEnv(t *testing.T) {
1313
t.Parallel()
1414

1515
tests := []struct {
16-
noCase bool
17-
in []string
18-
want []string
16+
noCase bool
17+
in []string
18+
want []string
19+
wantErr bool
1920
}{
2021
{
2122
noCase: true,
@@ -43,11 +44,17 @@ func TestDedupEnv(t *testing.T) {
4344
in: []string{"dodgy", "entries"},
4445
want: []string{"dodgy", "entries"},
4546
},
47+
{
48+
// Filter out entries containing NULs.
49+
in: []string{"A=a\x00b", "B=b", "C\x00C=c"},
50+
want: []string{"B=b"},
51+
wantErr: true,
52+
},
4653
}
4754
for _, tt := range tests {
48-
got := dedupEnvCase(tt.noCase, tt.in)
49-
if !reflect.DeepEqual(got, tt.want) {
50-
t.Errorf("Dedup(%v, %q) = %q; want %q", tt.noCase, tt.in, got, tt.want)
55+
got, err := dedupEnvCase(tt.noCase, tt.in)
56+
if !reflect.DeepEqual(got, tt.want) || (err != nil) != tt.wantErr {
57+
t.Errorf("Dedup(%v, %q) = %q, %v; want %q, error:%v", tt.noCase, tt.in, got, err, tt.want, tt.wantErr)
5158
}
5259
}
5360
}

src/os/exec/exec.go

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1190,7 +1190,11 @@ func (c *Cmd) environ() ([]string, error) {
11901190
}
11911191
}
11921192

1193-
return addCriticalEnv(dedupEnv(env)), err
1193+
env, dedupErr := dedupEnv(env)
1194+
if err == nil {
1195+
err = dedupErr
1196+
}
1197+
return addCriticalEnv(env), err
11941198
}
11951199

11961200
// Environ returns a copy of the environment in which the command would be run
@@ -1204,20 +1208,27 @@ func (c *Cmd) Environ() []string {
12041208
// dedupEnv returns a copy of env with any duplicates removed, in favor of
12051209
// later values.
12061210
// Items not of the normal environment "key=value" form are preserved unchanged.
1207-
func dedupEnv(env []string) []string {
1211+
// Items containing NUL characters are removed, and an error is returned along with
1212+
// the remaining values.
1213+
func dedupEnv(env []string) ([]string, error) {
12081214
return dedupEnvCase(runtime.GOOS == "windows", env)
12091215
}
12101216

12111217
// dedupEnvCase is dedupEnv with a case option for testing.
12121218
// If caseInsensitive is true, the case of keys is ignored.
1213-
func dedupEnvCase(caseInsensitive bool, env []string) []string {
1219+
func dedupEnvCase(caseInsensitive bool, env []string) ([]string, error) {
12141220
// Construct the output in reverse order, to preserve the
12151221
// last occurrence of each key.
1222+
var err error
12161223
out := make([]string, 0, len(env))
12171224
saw := make(map[string]bool, len(env))
12181225
for n := len(env); n > 0; n-- {
12191226
kv := env[n-1]
12201227

1228+
if strings.IndexByte(kv, 0) != -1 {
1229+
err = errors.New("exec: environment variable contains NUL")
1230+
continue
1231+
}
12211232
i := strings.Index(kv, "=")
12221233
if i == 0 {
12231234
// We observe in practice keys with a single leading "=" on Windows.
@@ -1252,7 +1263,7 @@ func dedupEnvCase(caseInsensitive bool, env []string) []string {
12521263
out[i], out[j] = out[j], out[i]
12531264
}
12541265

1255-
return out
1266+
return out, err
12561267
}
12571268

12581269
// addCriticalEnv adds any critical environment variables that are required

src/os/exec/exec_test.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1027,6 +1027,15 @@ func TestDedupEnvEcho(t *testing.T) {
10271027
}
10281028
}
10291029

1030+
func TestEnvNULCharacter(t *testing.T) {
1031+
cmd := helperCommand(t, "echoenv", "FOO", "BAR")
1032+
cmd.Env = append(cmd.Environ(), "FOO=foo\x00BAR=bar")
1033+
out, err := cmd.CombinedOutput()
1034+
if err == nil {
1035+
t.Errorf("output = %q; want error", string(out))
1036+
}
1037+
}
1038+
10301039
func TestString(t *testing.T) {
10311040
t.Parallel()
10321041

src/syscall/exec_windows.go

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
package syscall
88

99
import (
10+
"internal/bytealg"
1011
"runtime"
1112
"sync"
1213
"unicode/utf16"
@@ -115,12 +116,16 @@ func makeCmdLine(args []string) string {
115116
// the representation required by CreateProcess: a sequence of NUL
116117
// terminated strings followed by a nil.
117118
// Last bytes are two UCS-2 NULs, or four NUL bytes.
118-
func createEnvBlock(envv []string) *uint16 {
119+
// If any string contains a NUL, it returns (nil, EINVAL).
120+
func createEnvBlock(envv []string) (*uint16, error) {
119121
if len(envv) == 0 {
120-
return &utf16.Encode([]rune("\x00\x00"))[0]
122+
return &utf16.Encode([]rune("\x00\x00"))[0], nil
121123
}
122124
length := 0
123125
for _, s := range envv {
126+
if bytealg.IndexByteString(s, 0) != -1 {
127+
return nil, EINVAL
128+
}
124129
length += len(s) + 1
125130
}
126131
length += 1
@@ -135,7 +140,7 @@ func createEnvBlock(envv []string) *uint16 {
135140
}
136141
copy(b[i:i+1], []byte{0})
137142

138-
return &utf16.Encode([]rune(string(b)))[0]
143+
return &utf16.Encode([]rune(string(b)))[0], nil
139144
}
140145

141146
func CloseOnExec(fd Handle) {
@@ -400,12 +405,17 @@ func StartProcess(argv0 string, argv []string, attr *ProcAttr) (pid int, handle
400405
}
401406
}
402407

408+
envBlock, err := createEnvBlock(attr.Env)
409+
if err != nil {
410+
return 0, 0, err
411+
}
412+
403413
pi := new(ProcessInformation)
404414
flags := sys.CreationFlags | CREATE_UNICODE_ENVIRONMENT | _EXTENDED_STARTUPINFO_PRESENT
405415
if sys.Token != 0 {
406-
err = CreateProcessAsUser(sys.Token, argv0p, argvp, sys.ProcessAttributes, sys.ThreadAttributes, willInheritHandles, flags, createEnvBlock(attr.Env), dirp, &si.StartupInfo, pi)
416+
err = CreateProcessAsUser(sys.Token, argv0p, argvp, sys.ProcessAttributes, sys.ThreadAttributes, willInheritHandles, flags, envBlock, dirp, &si.StartupInfo, pi)
407417
} else {
408-
err = CreateProcess(argv0p, argvp, sys.ProcessAttributes, sys.ThreadAttributes, willInheritHandles, flags, createEnvBlock(attr.Env), dirp, &si.StartupInfo, pi)
418+
err = CreateProcess(argv0p, argvp, sys.ProcessAttributes, sys.ThreadAttributes, willInheritHandles, flags, envBlock, dirp, &si.StartupInfo, pi)
409419
}
410420
if err != nil {
411421
return 0, 0, err

0 commit comments

Comments
 (0)