]> Cypherpunks.ru repositories - gostls13.git/commitdiff
[release-branch.go1.21] cmd/compile: use absolute file name in isCgo check
authorIan Lance Taylor <iant@golang.org>
Wed, 20 Sep 2023 23:16:29 +0000 (16:16 -0700)
committerGopher Robot <gobot@golang.org>
Thu, 5 Oct 2023 18:07:27 +0000 (18:07 +0000)
For #23672
Updates #63211
Fixes #63214
Fixes CVE-2023-39323

Change-Id: I4586a69e1b2560036afec29d53e53cf25e6c7352
Reviewed-on: https://team-review.git.corp.google.com/c/golang/go-private/+/2032884
Reviewed-by: Matthew Dempsky <mdempsky@google.com>
Reviewed-by: Roland Shoemaker <bracewell@google.com>
(cherry picked from commit 9b19e751918dd218035811b1ef83a8c2693b864a)
Reviewed-on: https://team-review.git.corp.google.com/c/golang/go-private/+/2037958
Reviewed-by: Damien Neil <dneil@google.com>
Reviewed-by: Tatiana Bradley <tatianabradley@google.com>
Run-TryBot: Roland Shoemaker <bracewell@google.com>
Reviewed-on: https://go-review.googlesource.com/c/go/+/533215
Reviewed-by: Than McIntosh <thanm@google.com>
Auto-Submit: Michael Pratt <mpratt@google.com>
Reviewed-by: Ian Lance Taylor <iant@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>

src/cmd/cgo/internal/testerrors/errors_test.go
src/cmd/cgo/internal/testerrors/testdata/err5.go [new file with mode: 0644]
src/cmd/compile/internal/noder/noder.go

index 486530e186c610fc96d26cfc058e6ac4305593d5..870d05b4914e164cbb3dbe61229ae151215f0ed9 100644 (file)
@@ -111,6 +111,7 @@ func TestReportsTypeErrors(t *testing.T) {
        for _, file := range []string{
                "err1.go",
                "err2.go",
+               "err5.go",
                "issue11097a.go",
                "issue11097b.go",
                "issue18452.go",
diff --git a/src/cmd/cgo/internal/testerrors/testdata/err5.go b/src/cmd/cgo/internal/testerrors/testdata/err5.go
new file mode 100644 (file)
index 0000000..779d745
--- /dev/null
@@ -0,0 +1,10 @@
+// Copyright 2023 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 main
+
+//line /tmp/_cgo_.go:1
+//go:cgo_dynamic_linker "/elf/interp" // ERROR HERE: only allowed in cgo-generated code
+
+func main() {}
index 25a6ba7c8893839c82712e3ac79045256531a6cf..94071581fedbeb35d62c79bada7e47c5cf9ecfaa 100644 (file)
@@ -333,8 +333,14 @@ func (p *noder) pragma(pos syntax.Pos, blankLine bool, text string, old syntax.P
 // contain cgo directives, and for security reasons
 // (primarily misuse of linker flags), other files are not.
 // See golang.org/issue/23672.
+// Note that cmd/go ignores files whose names start with underscore,
+// so the only _cgo_ files we will see from cmd/go are generated by cgo.
+// It's easy to bypass this check by calling the compiler directly;
+// we only protect against uses by cmd/go.
 func isCgoGeneratedFile(pos syntax.Pos) bool {
-       return strings.HasPrefix(filepath.Base(trimFilename(pos.Base())), "_cgo_")
+       // We need the absolute file, independent of //line directives,
+       // so we call pos.Base().Pos().
+       return strings.HasPrefix(filepath.Base(trimFilename(pos.Base().Pos().Base())), "_cgo_")
 }
 
 // safeArg reports whether arg is a "safe" command-line argument,