aboutsummaryrefslogtreecommitdiff
path: root/src/os
diff options
context:
space:
mode:
authorLiam 'Auzzie' Haworth <liam@haworth.id.au>2020-02-25 00:11:28 +0000
committerIan Lance Taylor <iant@golang.org>2020-02-25 02:05:29 +0000
commite0c3ded337e95ded40eb401e7d9e74716e3a445f (patch)
tree45488c670a67f754bc175cdcf4c32a0ce7588ec1 /src/os
parent17f7c12eb9ee0df304eb7a48b3f29ec165a384be (diff)
downloadgo-e0c3ded337e95ded40eb401e7d9e74716e3a445f.tar.xz
os/exec: use environment variables for user token when present
Builds upon the changes from #32000 which supported sourcing environment variables for a new process from the environment of a Windows user token when supplied. But due to the logic of os/exec, the Env field of a process was always non-nil when it reached that change. This change moves the logic up to os/exec, specifically when os.ProcAttr is being built for the os.StartProcess call, this ensures that if a user token has been supplied and no Env slice has been provided on the command it will be sourced from the user's environment. If no token is provided, or the program is compiled for any other platform than Windows, the default environment will be sourced from syscall.Environ(). Fixes #35314 Change-Id: I4c1722e90b91945eb6980d5c5928183269b50487 GitHub-Last-Rev: 32216b7291418f9285147a93ed6d0ba028f94ef2 GitHub-Pull-Request: golang/go#37402 Reviewed-on: https://go-review.googlesource.com/c/go/+/220587 Run-TryBot: Ian Lance Taylor <iant@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Ian Lance Taylor <iant@golang.org>
Diffstat (limited to 'src/os')
-rw-r--r--src/os/env_default.go13
-rw-r--r--src/os/env_windows.go44
-rw-r--r--src/os/exec/exec.go15
-rw-r--r--src/os/exec_posix.go3
4 files changed, 12 insertions, 63 deletions
diff --git a/src/os/env_default.go b/src/os/env_default.go
deleted file mode 100644
index c11ccce7e3..0000000000
--- a/src/os/env_default.go
+++ /dev/null
@@ -1,13 +0,0 @@
-// Copyright 2019 The Go Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style
-// license that can be found in the LICENSE file.
-
-// +build !windows
-
-package os
-
-import "syscall"
-
-func environForSysProcAttr(sys *syscall.SysProcAttr) ([]string, error) {
- return Environ(), nil
-}
diff --git a/src/os/env_windows.go b/src/os/env_windows.go
deleted file mode 100644
index b1b1ee4b3e..0000000000
--- a/src/os/env_windows.go
+++ /dev/null
@@ -1,44 +0,0 @@
-// Copyright 2019 The Go Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style
-// license that can be found in the LICENSE file.
-
-package os
-
-import (
- "internal/syscall/windows"
- "syscall"
- "unicode/utf16"
- "unsafe"
-)
-
-func environForSysProcAttr(sys *syscall.SysProcAttr) (env []string, err error) {
- if sys == nil || sys.Token == 0 {
- return Environ(), nil
- }
- var block *uint16
- err = windows.CreateEnvironmentBlock(&block, sys.Token, false)
- if err != nil {
- return nil, err
- }
- defer windows.DestroyEnvironmentBlock(block)
- blockp := uintptr(unsafe.Pointer(block))
- for {
-
- // find NUL terminator
- end := unsafe.Pointer(blockp)
- for *(*uint16)(end) != 0 {
- end = unsafe.Pointer(uintptr(end) + 2)
- }
-
- n := (uintptr(end) - uintptr(unsafe.Pointer(blockp))) / 2
- if n == 0 {
- // environment block ends with empty string
- break
- }
-
- entry := (*[(1 << 30) - 1]uint16)(unsafe.Pointer(blockp))[:n:n]
- env = append(env, string(utf16.Decode(entry)))
- blockp += 2 * (uintptr(len(entry)) + 1)
- }
- return
-}
diff --git a/src/os/exec/exec.go b/src/os/exec/exec.go
index 3474ae0ca4..0c49575511 100644
--- a/src/os/exec/exec.go
+++ b/src/os/exec/exec.go
@@ -24,6 +24,7 @@ import (
"bytes"
"context"
"errors"
+ "internal/syscall/execenv"
"io"
"os"
"path/filepath"
@@ -222,11 +223,11 @@ func interfaceEqual(a, b interface{}) bool {
return a == b
}
-func (c *Cmd) envv() []string {
+func (c *Cmd) envv() ([]string, error) {
if c.Env != nil {
- return c.Env
+ return c.Env, nil
}
- return os.Environ()
+ return execenv.Default(c.SysProcAttr)
}
func (c *Cmd) argv() []string {
@@ -413,11 +414,15 @@ func (c *Cmd) Start() error {
}
c.childFiles = append(c.childFiles, c.ExtraFiles...)
- var err error
+ envv, err := c.envv()
+ if err != nil {
+ return err
+ }
+
c.Process, err = os.StartProcess(c.Path, c.argv(), &os.ProcAttr{
Dir: c.Dir,
Files: c.childFiles,
- Env: addCriticalEnv(dedupEnv(c.envv())),
+ Env: addCriticalEnv(dedupEnv(envv)),
Sys: c.SysProcAttr,
})
if err != nil {
diff --git a/src/os/exec_posix.go b/src/os/exec_posix.go
index 95ccc246a8..45b47a542d 100644
--- a/src/os/exec_posix.go
+++ b/src/os/exec_posix.go
@@ -7,6 +7,7 @@
package os
import (
+ "internal/syscall/execenv"
"runtime"
"syscall"
)
@@ -39,7 +40,7 @@ func startProcess(name string, argv []string, attr *ProcAttr) (p *Process, err e
Sys: attr.Sys,
}
if sysattr.Env == nil {
- sysattr.Env, err = environForSysProcAttr(sysattr.Sys)
+ sysattr.Env, err = execenv.Default(sysattr.Sys)
if err != nil {
return nil, err
}