@@ -930,15 +930,16 @@ func (c *Cmd) Environ() []string {
930
930
// dedupEnv returns a copy of env with any duplicates removed, in favor of
931
931
// later values.
932
932
// Items not of the normal environment "key=value" form are preserved unchanged.
933
- // Items containing NUL characters are removed, and an error is returned along with
934
- // the remaining values.
933
+ // Except on Plan 9, items containing NUL characters are removed, and
934
+ // an error is returned along with the remaining values.
935
935
func dedupEnv (env []string ) ([]string , error ) {
936
- return dedupEnvCase (runtime .GOOS == "windows" , env )
936
+ return dedupEnvCase (runtime .GOOS == "windows" , runtime . GOOS == "plan9" , env )
937
937
}
938
938
939
939
// dedupEnvCase is dedupEnv with a case option for testing.
940
940
// If caseInsensitive is true, the case of keys is ignored.
941
- func dedupEnvCase (caseInsensitive bool , env []string ) ([]string , error ) {
941
+ // If nulOK is false, items containing NUL characters are allowed.
942
+ func dedupEnvCase (caseInsensitive , nulOK bool , env []string ) ([]string , error ) {
942
943
// Construct the output in reverse order, to preserve the
943
944
// last occurrence of each key.
944
945
var err error
@@ -947,10 +948,13 @@ func dedupEnvCase(caseInsensitive bool, env []string) ([]string, error) {
947
948
for n := len (env ); n > 0 ; n -- {
948
949
kv := env [n - 1 ]
949
950
950
- if strings .IndexByte (kv , 0 ) != - 1 {
951
+ // Reject NUL in environment variables to prevent security issues (#56284);
952
+ // except on Plan 9, which uses NUL as os.PathListSeparator (#56544).
953
+ if ! nulOK && strings .IndexByte (kv , 0 ) != - 1 {
951
954
err = errors .New ("exec: environment variable contains NUL" )
952
955
continue
953
956
}
957
+
954
958
i := strings .Index (kv , "=" )
955
959
if i == 0 {
956
960
// We observe in practice keys with a single leading "=" on Windows.
0 commit comments