[release-branch.go1.27] cmd/go: disallow --sysroot and -target, validate -isysroot/-syslibroot, and restrict -x Remove --sysroot and -target from the compiler and linker flag allowlists, as GCC and Clang use --sysroot to relocate subprograms, spec files, and CRT startup objects, and Clang's -target flag falls back to executing target helper tools (such as orbis-ld, prospero-lld, xcc, or llvm-spirv) from the current working directory when not found in PATH. Keep -isysroot and -Wl,-syslibroot for SDK selection, but verify in checkLinkerFlags that their target is not a regular file on disk (which GNU ld.bfd treats as a positional linker script). Also restrict -x to the C/C++/Objective-C/assembler languages supported by cgo. Fixes CVE-2026-97033 For golang/go#82577 Fixes golang/go#82600 Change-Id: I6e85fe46f26bb6366e03e17f8761e7c884694874 Reviewed-on: https://go-internal-review.googlesource.com/c/go/+/6080 Reviewed-by: Cherry Mui <cherryyz@google.com> Reviewed-by: Damien Neil <dneil@google.com> Reviewed-on: https://go-internal-review.googlesource.com/c/go/+/6860 Reviewed-by: Nicholas Husin <husin@google.com> Reviewed-on: https://go-review.googlesource.com/c/go/+/861484 TryBot-Bypass: Gopher Robot <gobot@golang.org> Reviewed-by: Neal Patel <nealpatel@google.com>
diff --git a/src/cmd/go/internal/work/security.go b/src/cmd/go/internal/work/security.go index fd54b5c..4999ee9 100644 --- a/src/cmd/go/internal/work/security.go +++ b/src/cmd/go/internal/work/security.go
@@ -32,6 +32,7 @@ import ( "fmt" "internal/lazyregexp" + "os" "regexp" "strings" @@ -132,12 +133,13 @@ re(`--static`), re(`-?-std=([^@\-].*)`), re(`-?-stdlib=([^@\-].*)`), - re(`--sysroot=([^@\-].*)`), re(`-w`), - re(`-x([^@\-].*)`), + re(`-x(c|c-header|cpp-output|c\+\+|c\+\+-header|c\+\+-cpp-output|objective-c|objective-c-header|objective-c-cpp-output|objective-c\+\+|objective-c\+\+-header|objective-c\+\+-cpp-output|assembler|assembler-with-cpp)`), re(`-v`), } +var validCompilerXLang = re(`^(c|c-header|cpp-output|c\+\+|c\+\+-header|c\+\+-cpp-output|objective-c|objective-c-header|objective-c-cpp-output|objective-c\+\+|objective-c\+\+-header|objective-c\+\+-cpp-output|assembler|assembler-with-cpp)$`) + var validCompilerFlagsWithNextArg = []string{ "-arch", "-D", @@ -148,8 +150,6 @@ "-include", "-isysroot", "-isystem", - "--sysroot", - "-target", "-x", } @@ -244,8 +244,6 @@ "-L", "-framework", "-isysroot", - "--sysroot", - "-target", "-Wl,-framework", "-Wl,-rpath", "-Wl,-R", @@ -312,7 +310,56 @@ func checkLinkerFlags(name, source string, list []string) error { checkOverrides := true - return checkFlags(name, source, list, invalidLinkerFlags, validLinkerFlags, validLinkerFlagsWithNextArg, checkOverrides) + if err := checkFlags(name, source, list, invalidLinkerFlags, validLinkerFlags, validLinkerFlagsWithNextArg, checkOverrides); err != nil { + return err + } + for i := 0; i < len(list); i++ { + arg := list[i] + // Although -isysroot is a preprocessor/compiler option in GCC, clang on + // Darwin also accepts it as a linker flag (forwarding it as -syslibroot) + // and we include it in validLinkerFlagsWithNextArg on all platforms. + if arg == "-isysroot" && i+1 < len(list) { + i++ + if err := checkLinkerSysroot(source, arg+" "+list[i], list[i]); err != nil { + return err + } + continue + } + if strings.HasPrefix(arg, "-Wl,") { + if err := checkLinkerWlFlag(source, arg, arg); err != nil { + return err + } + } + } + return nil +} + +// checkLinkerSysroot verifies that the target of -isysroot or -Wl,-syslibroot +// does not name a regular file on disk, which GNU ld.bfd would treat as a +// positional linker script. +func checkLinkerSysroot(source, flag, dir string) error { + if fi, err := os.Stat(dir); err == nil && !fi.IsDir() { + return fmt.Errorf("invalid flag in %s: %s (not a directory)", source, flag) + } + return nil +} + +func checkLinkerWlFlag(source, raw, arg string) error { + wargs := []string{arg} + if strings.HasPrefix(arg, "-Wl,--push-state,") { + for _, sub := range strings.Split(arg, ",")[2:] { + wargs = append(wargs, "-Wl,"+sub) + } + } + for _, warg := range wargs { + if strings.HasPrefix(warg, "-Wl,-syslibroot,") || strings.HasPrefix(warg, "-Wl,-syslibroot=") { + dir := warg[len("-Wl,-syslibroot,"):] + if err := checkLinkerSysroot(source, raw, dir); err != nil { + return err + } + } + } + return nil } func checkPkgConfigFlags(name, source string, list []string) error { @@ -431,6 +478,16 @@ } for _, x := range validNext { if arg == x { + if arg == "-x" { + if i+1 < len(list) && validCompilerXLang.MatchString(list[i+1]) { + i++ + continue Args + } + if i+1 < len(list) { + return fmt.Errorf("invalid flag in %s: %s %s (see https://go.dev/s/invalidflag)", source, arg, list[i+1]) + } + return fmt.Errorf("invalid flag in %s: %s without argument (see https://go.dev/s/invalidflag)", source, arg) + } if i+1 < len(list) && load.SafeArg(list[i+1]) { i++ continue Args
diff --git a/src/cmd/go/internal/work/security_test.go b/src/cmd/go/internal/work/security_test.go index 48f9810..d8c8d8d 100644 --- a/src/cmd/go/internal/work/security_test.go +++ b/src/cmd/go/internal/work/security_test.go
@@ -6,6 +6,7 @@ import ( "os" + "path/filepath" "strings" "testing" ) @@ -99,6 +100,7 @@ {"-isystem", "/usr/include/mozjs-68"}, {"-include", "/usr/include/mozjs-68/RequiredDefines.h"}, {"-framework", "Chocolate"}, + {"-isysroot", "/tmp"}, {"-x", "c"}, {"-v"}, } @@ -142,6 +144,13 @@ {"-framework", "@Home"}, {"-x", "--c"}, {"-x", "@obj"}, + {"-xada"}, + {"-x", "ada"}, + {"-x", "none"}, + {"--sysroot=/tmp"}, + {"--sysroot", "/tmp"}, + {"-target", "x86_64-scei-ps4"}, + {"-target", "x86_64-linux-gnu"}, } func TestCheckCompilerFlags(t *testing.T) { @@ -290,6 +299,10 @@ {"-Wl,--push-state,@foo"}, {"-fplugin=./-Wl,--push-state,-R.so"}, {"./-Wl,--push-state,-R.c"}, + {"--sysroot=/tmp"}, + {"--sysroot", "/tmp"}, + {"-target", "x86_64-scei-ps4"}, + {"-target", "x86_64-linux-gnu"}, } func TestCheckLinkerFlags(t *testing.T) { @@ -305,6 +318,33 @@ } } +func TestCheckLinkerSysrootFlags(t *testing.T) { + dir := t.TempDir() + script := filepath.Join(dir, "script.ld") + if err := os.WriteFile(script, []byte("PLUGIN(\"/tmp/pwn.so\")\n"), 0644); err != nil { + t.Fatal(err) + } + for _, f := range [][]string{ + {"-isysroot", dir}, + {"-Wl,-syslibroot," + dir}, + {"-Wl,-syslibroot=" + dir}, + } { + if err := checkLinkerFlags("test", "test", f); err != nil { + t.Errorf("unexpected error for %q: %v", f, err) + } + } + for _, f := range [][]string{ + {"-isysroot", script}, + {"-Wl,-syslibroot," + script}, + {"-Wl,-syslibroot=" + script}, + {"-Wl,--push-state,-syslibroot=" + script}, + } { + if err := checkLinkerFlags("test", "test", f); err == nil { + t.Errorf("missing error for %q", f) + } + } +} + func TestCheckFlagAllowDisallow(t *testing.T) { if err := checkCompilerFlags("TEST", "test", []string{"-disallow"}); err == nil { t.Fatalf("missing error for -disallow")