Skip to content

Commit

Permalink
[release-branch.go1.15-security] all: introduce and use internal/execabs
Browse files Browse the repository at this point in the history
Introduces a wrapper around os/exec, internal/execabs, for use in
all commands. This wrapper prevents exec.LookPath and exec.Command from
running executables in the current directory.

All imports of os/exec in non-test files in cmd/ are replaced with
imports of internal/execabs.

This issue was reported by RyotaK.

Fixes CVE-2021-3115

Change-Id: I0423451a6e27ec1e1d6f3fe929ab1ef69145c08f
Reviewed-on: https://team-review.git.corp.google.com/c/golang/go-private/+/955304
Reviewed-by: Russ Cox <rsc@google.com>
Reviewed-by: Katie Hockman <katiehockman@google.com>
(cherry picked from commit 44f09a6990ccf4db601cbf8208c89ac4e888f884)
Reviewed-on: https://team-review.git.corp.google.com/c/golang/go-private/+/955308
  • Loading branch information
rolandshoemaker committed Jan 16, 2021
1 parent b210522 commit 07e3195
Show file tree
Hide file tree
Showing 38 changed files with 221 additions and 36 deletions.
2 changes: 1 addition & 1 deletion src/cmd/api/goapi.go
Expand Up @@ -16,11 +16,11 @@ import (
"go/parser"
"go/token"
"go/types"
exec "internal/execabs"
"io"
"io/ioutil"
"log"
"os"
"os/exec"
"path/filepath"
"regexp"
"runtime"
Expand Down
2 changes: 1 addition & 1 deletion src/cmd/api/run.go
Expand Up @@ -10,9 +10,9 @@ package main

import (
"fmt"
exec "internal/execabs"
"log"
"os"
"os/exec"
"path/filepath"
"runtime"
"strings"
Expand Down
2 changes: 1 addition & 1 deletion src/cmd/cgo/out.go
Expand Up @@ -13,11 +13,11 @@ import (
"go/ast"
"go/printer"
"go/token"
exec "internal/execabs"
"internal/xcoff"
"io"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"regexp"
"sort"
Expand Down
2 changes: 1 addition & 1 deletion src/cmd/cgo/util.go
Expand Up @@ -8,9 +8,9 @@ import (
"bytes"
"fmt"
"go/token"
exec "internal/execabs"
"io/ioutil"
"os"
"os/exec"
)

// run runs the command argv, feeding in stdin on standard input.
Expand Down
2 changes: 1 addition & 1 deletion src/cmd/compile/internal/ssa/html.go
Expand Up @@ -9,9 +9,9 @@ import (
"cmd/internal/src"
"fmt"
"html"
exec "internal/execabs"
"io"
"os"
"os/exec"
"path/filepath"
"strconv"
"strings"
Expand Down
2 changes: 1 addition & 1 deletion src/cmd/cover/func.go
Expand Up @@ -15,9 +15,9 @@ import (
"go/ast"
"go/parser"
"go/token"
exec "internal/execabs"
"io"
"os"
"os/exec"
"path"
"path/filepath"
"runtime"
Expand Down
2 changes: 1 addition & 1 deletion src/cmd/cover/testdata/toolexec.go
Expand Up @@ -16,7 +16,7 @@ package main

import (
"os"
"os/exec"
exec "internal/execabs"
"strings"
)

Expand Down
4 changes: 3 additions & 1 deletion src/cmd/dist/buildtool.go
Expand Up @@ -302,8 +302,10 @@ func bootstrapFixImports(srcFile string) string {
continue
}
if strings.HasPrefix(line, `import "`) || strings.HasPrefix(line, `import . "`) ||
inBlock && (strings.HasPrefix(line, "\t\"") || strings.HasPrefix(line, "\t. \"")) {
inBlock && (strings.HasPrefix(line, "\t\"") || strings.HasPrefix(line, "\t. \"") || strings.HasPrefix(line, "\texec \"")) {
line = strings.Replace(line, `"cmd/`, `"bootstrap/cmd/`, -1)
// During bootstrap, must use plain os/exec.
line = strings.Replace(line, `exec "internal/execabs"`, `"os/exec"`, -1)
for _, dir := range bootstrapDirs {
if strings.HasPrefix(dir, "cmd/") {
continue
Expand Down
2 changes: 1 addition & 1 deletion src/cmd/doc/dirs.go
Expand Up @@ -7,9 +7,9 @@ package main
import (
"bytes"
"fmt"
exec "internal/execabs"
"log"
"os"
"os/exec"
"path/filepath"
"regexp"
"strings"
Expand Down
2 changes: 1 addition & 1 deletion src/cmd/fix/typecheck.go
Expand Up @@ -9,9 +9,9 @@ import (
"go/ast"
"go/parser"
"go/token"
exec "internal/execabs"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"reflect"
"runtime"
Expand Down
2 changes: 1 addition & 1 deletion src/cmd/go/internal/base/base.go
Expand Up @@ -9,9 +9,9 @@ package base
import (
"flag"
"fmt"
exec "internal/execabs"
"log"
"os"
"os/exec"
"strings"
"sync"

Expand Down
2 changes: 1 addition & 1 deletion src/cmd/go/internal/bug/bug.go
Expand Up @@ -8,11 +8,11 @@ package bug
import (
"bytes"
"fmt"
exec "internal/execabs"
"io"
"io/ioutil"
urlpkg "net/url"
"os"
"os/exec"
"path/filepath"
"regexp"
"runtime"
Expand Down
2 changes: 1 addition & 1 deletion src/cmd/go/internal/generate/generate.go
Expand Up @@ -11,11 +11,11 @@ import (
"fmt"
"go/parser"
"go/token"
exec "internal/execabs"
"io"
"io/ioutil"
"log"
"os"
"os/exec"
"path/filepath"
"regexp"
"strconv"
Expand Down
2 changes: 1 addition & 1 deletion src/cmd/go/internal/modfetch/codehost/codehost.go
Expand Up @@ -10,10 +10,10 @@ import (
"bytes"
"crypto/sha256"
"fmt"
exec "internal/execabs"
"io"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"strings"
"sync"
Expand Down
2 changes: 1 addition & 1 deletion src/cmd/go/internal/modfetch/codehost/git.go
Expand Up @@ -8,11 +8,11 @@ import (
"bytes"
"errors"
"fmt"
exec "internal/execabs"
"io"
"io/ioutil"
"net/url"
"os"
"os/exec"
"path/filepath"
"sort"
"strconv"
Expand Down
2 changes: 1 addition & 1 deletion src/cmd/go/internal/test/genflags.go
Expand Up @@ -9,9 +9,9 @@ package main
import (
"bytes"
"flag"
exec "internal/execabs"
"log"
"os"
"os/exec"
"strings"
"testing"
"text/template"
Expand Down
2 changes: 1 addition & 1 deletion src/cmd/go/internal/test/test.go
Expand Up @@ -10,10 +10,10 @@ import (
"errors"
"fmt"
"go/build"
exec "internal/execabs"
"io"
"io/ioutil"
"os"
"os/exec"
"path"
"path/filepath"
"regexp"
Expand Down
2 changes: 1 addition & 1 deletion src/cmd/go/internal/tool/tool.go
Expand Up @@ -7,8 +7,8 @@ package tool

import (
"fmt"
exec "internal/execabs"
"os"
"os/exec"
"sort"
"strings"

Expand Down
2 changes: 1 addition & 1 deletion src/cmd/go/internal/vet/vetflag.go
Expand Up @@ -10,9 +10,9 @@ import (
"errors"
"flag"
"fmt"
exec "internal/execabs"
"log"
"os"
"os/exec"
"path/filepath"
"strings"

Expand Down
2 changes: 1 addition & 1 deletion src/cmd/go/internal/work/build.go
Expand Up @@ -8,8 +8,8 @@ import (
"errors"
"fmt"
"go/build"
exec "internal/execabs"
"os"
"os/exec"
"path/filepath"
"runtime"
"strings"
Expand Down
2 changes: 1 addition & 1 deletion src/cmd/go/internal/work/buildid.go
Expand Up @@ -7,9 +7,9 @@ package work
import (
"bytes"
"fmt"
exec "internal/execabs"
"io/ioutil"
"os"
"os/exec"
"strings"

"cmd/go/internal/base"
Expand Down
2 changes: 1 addition & 1 deletion src/cmd/go/internal/work/exec.go
Expand Up @@ -11,13 +11,13 @@ import (
"encoding/json"
"errors"
"fmt"
exec "internal/execabs"
"internal/lazyregexp"
"io"
"io/ioutil"
"log"
"math/rand"
"os"
"os/exec"
"path/filepath"
"regexp"
"runtime"
Expand Down
2 changes: 1 addition & 1 deletion src/cmd/go/internal/work/gccgo.go
Expand Up @@ -6,9 +6,9 @@ package work

import (
"fmt"
exec "internal/execabs"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"strings"

Expand Down
2 changes: 1 addition & 1 deletion src/cmd/go/testdata/addmod.go
Expand Up @@ -25,7 +25,7 @@ import (
"io/ioutil"
"log"
"os"
"os/exec"
exec "internal/execabs"
"path/filepath"
"strings"

Expand Down
2 changes: 1 addition & 1 deletion src/cmd/internal/browser/browser.go
Expand Up @@ -6,8 +6,8 @@
package browser

import (
exec "internal/execabs"
"os"
"os/exec"
"runtime"
"time"
)
Expand Down
2 changes: 1 addition & 1 deletion src/cmd/internal/diff/diff.go
Expand Up @@ -7,9 +7,9 @@
package diff

import (
exec "internal/execabs"
"io/ioutil"
"os"
"os/exec"
"runtime"
)

Expand Down
2 changes: 1 addition & 1 deletion src/cmd/internal/dwarf/dwarf.go
Expand Up @@ -12,7 +12,7 @@ import (
"cmd/internal/objabi"
"errors"
"fmt"
"os/exec"
exec "internal/execabs"
"sort"
"strconv"
"strings"
Expand Down
2 changes: 1 addition & 1 deletion src/cmd/link/internal/ld/execarchive.go
Expand Up @@ -7,8 +7,8 @@
package ld

import (
exec "internal/execabs"
"os"
"os/exec"
"path/filepath"
"syscall"
)
Expand Down
2 changes: 1 addition & 1 deletion src/cmd/link/internal/ld/lib.go
Expand Up @@ -50,11 +50,11 @@ import (
"encoding/binary"
"encoding/hex"
"fmt"
exec "internal/execabs"
"io"
"io/ioutil"
"log"
"os"
"os/exec"
"path/filepath"
"runtime"
"sort"
Expand Down
2 changes: 1 addition & 1 deletion src/cmd/test2json/main.go
Expand Up @@ -82,9 +82,9 @@ package main
import (
"flag"
"fmt"
exec "internal/execabs"
"io"
"os"
"os/exec"

"cmd/internal/test2json"
)
Expand Down
2 changes: 1 addition & 1 deletion src/cmd/trace/pprof.go
Expand Up @@ -9,12 +9,12 @@ package main
import (
"bufio"
"fmt"
exec "internal/execabs"
"internal/trace"
"io"
"io/ioutil"
"net/http"
"os"
"os/exec"
"path/filepath"
"runtime"
"sort"
Expand Down
2 changes: 1 addition & 1 deletion src/go/build/build.go
Expand Up @@ -12,12 +12,12 @@ import (
"go/doc"
"go/parser"
"go/token"
exec "internal/execabs"
"internal/goroot"
"internal/goversion"
"io"
"io/ioutil"
"os"
"os/exec"
pathpkg "path"
"path/filepath"
"runtime"
Expand Down
8 changes: 7 additions & 1 deletion src/go/build/deps_test.go
Expand Up @@ -161,7 +161,7 @@ var depsRules = `
reflect !< OS;
OS
< golang.org/x/sys/cpu, internal/goroot;
< golang.org/x/sys/cpu;
# FMT is OS (which includes string routines) plus reflect and fmt.
# It does not include package log, which should be avoided in core packages.
Expand All @@ -177,6 +177,12 @@ var depsRules = `
log !< FMT;
OS, FMT
< internal/execabs;
OS, internal/execabs
< internal/goroot;
# Misc packages needing only FMT.
FMT
< flag,
Expand Down
2 changes: 1 addition & 1 deletion src/go/internal/gccgoimporter/gccgoinstallation.go
Expand Up @@ -7,8 +7,8 @@ package gccgoimporter
import (
"bufio"
"go/types"
exec "internal/execabs"
"os"
"os/exec"
"path/filepath"
"strings"
)
Expand Down

0 comments on commit 07e3195

Please sign in to comment.