replace uses of os/exec with golang.org/x/sys/execabs
Go 1.15.7 contained a security fix for CVE-2021-3115, which allowed arbitrary code to be executed at build time when using cgo on Windows. This issue also affects Unix users who have “.” listed explicitly in their PATH and are running “go get” outside of a module or with module mode disabled. This issue is not limited to the go command itself, and can also affect binaries that use `os.Command`, `os.LookPath`, etc. From the related blogpost (ttps://blog.golang.org/path-security): > Are your own programs affected? > > If you use exec.LookPath or exec.Command in your own programs, you only need to > be concerned if you (or your users) run your program in a directory with untrusted > contents. If so, then a subprocess could be started using an executable from dot > instead of from a system directory. (Again, using an executable from dot happens > always on Windows and only with uncommon PATH settings on Unix.) > > If you are concerned, then we’ve published the more restricted variant of os/exec > as golang.org/x/sys/execabs. You can use it in your program by simply replacing This patch replaces all uses of `os/exec` with `golang.org/x/sys/execabs`. While some uses of `os/exec` should not be problematic (e.g. part of tests), it is probably good to be consistent, in case code gets moved around. Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
@@ -23,7 +23,6 @@ import (
|
||||
"io/ioutil"
|
||||
"net"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"sync"
|
||||
@@ -33,6 +32,7 @@ import (
|
||||
"github.com/gogo/protobuf/proto"
|
||||
"github.com/gogo/protobuf/types"
|
||||
"github.com/pkg/errors"
|
||||
exec "golang.org/x/sys/execabs"
|
||||
)
|
||||
|
||||
var runtimePaths sync.Map
|
||||
@@ -73,12 +73,9 @@ func Command(ctx context.Context, runtime, containerdAddress, containerdTTRPCAdd
|
||||
if cmdPath, lerr = exec.LookPath(name); lerr != nil {
|
||||
if eerr, ok := lerr.(*exec.Error); ok {
|
||||
if eerr.Err == exec.ErrNotFound {
|
||||
// LookPath only finds current directory matches based on
|
||||
// the callers current directory but the caller is not
|
||||
// likely in the same directory as the containerd
|
||||
// executables. Instead match the calling binaries path
|
||||
// (containerd) and see if they are side by side. If so
|
||||
// execute the shim found there.
|
||||
// Match the calling binaries (containerd) path and see
|
||||
// if they are side by side. If so, execute the shim
|
||||
// found there.
|
||||
testPath := filepath.Join(filepath.Dir(self), name)
|
||||
if _, serr := os.Stat(testPath); serr == nil {
|
||||
cmdPath = testPath
|
||||
|
||||
Reference in New Issue
Block a user