Skip to content

Commit 2a7adf4

Browse files
neildmdempsky
authored andcommitted
[release-branch.go1.19] 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 erroneously 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. Updates #56284 Fixes #56328 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]> (cherry picked from commit 845accdebb2772c5344ed0c96df9910f3b02d741) Reviewed-on: https://team-review.git.corp.google.com/c/golang/go-private/+/1617553 Run-TryBot: Tatiana Bradley <[email protected]> Reviewed-by: Damien Neil <[email protected]> Reviewed-on: https://go-review.googlesource.com/c/go/+/446879 Reviewed-by: Tatiana Bradley <[email protected]> Reviewed-by: Heschi Kreinick <[email protected]> Run-TryBot: Matthew Dempsky <[email protected]> TryBot-Result: Gopher Robot <[email protected]>
1 parent 0618956 commit 2a7adf4

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
@@ -11,9 +11,10 @@ import (
1111

1212
func TestDedupEnv(t *testing.T) {
1313
tests := []struct {
14-
noCase bool
15-
in []string
16-
want []string
14+
noCase bool
15+
in []string
16+
want []string
17+
wantErr bool
1718
}{
1819
{
1920
noCase: true,
@@ -41,11 +42,17 @@ func TestDedupEnv(t *testing.T) {
4142
in: []string{"dodgy", "entries"},
4243
want: []string{"dodgy", "entries"},
4344
},
45+
{
46+
// Filter out entries containing NULs.
47+
in: []string{"A=a\x00b", "B=b", "C\x00C=c"},
48+
want: []string{"B=b"},
49+
wantErr: true,
50+
},
4451
}
4552
for _, tt := range tests {
46-
got := dedupEnvCase(tt.noCase, tt.in)
47-
if !reflect.DeepEqual(got, tt.want) {
48-
t.Errorf("Dedup(%v, %q) = %q; want %q", tt.noCase, tt.in, got, tt.want)
53+
got, err := dedupEnvCase(tt.noCase, tt.in)
54+
if !reflect.DeepEqual(got, tt.want) || (err != nil) != tt.wantErr {
55+
t.Errorf("Dedup(%v, %q) = %q, %v; want %q, error:%v", tt.noCase, tt.in, got, err, tt.want, tt.wantErr)
4956
}
5057
}
5158
}

src/os/exec/exec.go

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

915-
return addCriticalEnv(dedupEnv(env)), err
915+
env, dedupErr := dedupEnv(env)
916+
if err == nil {
917+
err = dedupErr
918+
}
919+
return addCriticalEnv(env), err
916920
}
917921

918922
// Environ returns a copy of the environment in which the command would be run
@@ -926,20 +930,27 @@ func (c *Cmd) Environ() []string {
926930
// dedupEnv returns a copy of env with any duplicates removed, in favor of
927931
// later values.
928932
// Items not of the normal environment "key=value" form are preserved unchanged.
929-
func dedupEnv(env []string) []string {
933+
// Items containing NUL characters are removed, and an error is returned along with
934+
// the remaining values.
935+
func dedupEnv(env []string) ([]string, error) {
930936
return dedupEnvCase(runtime.GOOS == "windows", env)
931937
}
932938

933939
// dedupEnvCase is dedupEnv with a case option for testing.
934940
// If caseInsensitive is true, the case of keys is ignored.
935-
func dedupEnvCase(caseInsensitive bool, env []string) []string {
941+
func dedupEnvCase(caseInsensitive bool, env []string) ([]string, error) {
936942
// Construct the output in reverse order, to preserve the
937943
// last occurrence of each key.
944+
var err error
938945
out := make([]string, 0, len(env))
939946
saw := make(map[string]bool, len(env))
940947
for n := len(env); n > 0; n-- {
941948
kv := env[n-1]
942949

950+
if strings.IndexByte(kv, 0) != -1 {
951+
err = errors.New("exec: environment variable contains NUL")
952+
continue
953+
}
943954
i := strings.Index(kv, "=")
944955
if i == 0 {
945956
// We observe in practice keys with a single leading "=" on Windows.
@@ -974,7 +985,7 @@ func dedupEnvCase(caseInsensitive bool, env []string) []string {
974985
out[i], out[j] = out[j], out[i]
975986
}
976987

977-
return out
988+
return out, err
978989
}
979990

980991
// 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
@@ -1053,6 +1053,15 @@ func TestDedupEnvEcho(t *testing.T) {
10531053
}
10541054
}
10551055

1056+
func TestEnvNULCharacter(t *testing.T) {
1057+
cmd := helperCommand(t, "echoenv", "FOO", "BAR")
1058+
cmd.Env = append(cmd.Environ(), "FOO=foo\x00BAR=bar")
1059+
out, err := cmd.CombinedOutput()
1060+
if err == nil {
1061+
t.Errorf("output = %q; want error", string(out))
1062+
}
1063+
}
1064+
10561065
func TestString(t *testing.T) {
10571066
echoPath, err := exec.LookPath("echo")
10581067
if err != nil {

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)