Skip to content

Commit

Permalink
[release-branch.go1.20] cmd/compile: use absolute file name in isCgo …
Browse files Browse the repository at this point in the history
…check

For #23672
Updates #63211
Fixes #63213
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 <[email protected]>
Reviewed-by: Roland Shoemaker <[email protected]>
(cherry picked from commit 9b19e751918dd218035811b1ef83a8c2693b864a)
Reviewed-on: https://team-review.git.corp.google.com/c/golang/go-private/+/2037629
Reviewed-by: Tatiana Bradley <[email protected]>
Run-TryBot: Roland Shoemaker <[email protected]>
Reviewed-by: Damien Neil <[email protected]>
Reviewed-on: https://go-review.googlesource.com/c/go/+/533195
Auto-Submit: Michael Pratt <[email protected]>
Reviewed-by: Ian Lance Taylor <[email protected]>
TryBot-Bypass: Michael Pratt <[email protected]>
Reviewed-by: Than McIntosh <[email protected]>
  • Loading branch information
ianlancetaylor authored and prattmic committed Oct 5, 2023
1 parent 83dce45 commit 31d5b60
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 9 deletions.
24 changes: 16 additions & 8 deletions misc/cgo/errors/errors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,16 +36,23 @@ func check(t *testing.T, file string) {
continue
}

_, frag, ok := bytes.Cut(line, []byte("ERROR HERE: "))
if !ok {
continue
if _, frag, ok := bytes.Cut(line, []byte("ERROR HERE: ")); ok {
re, err := regexp.Compile(fmt.Sprintf(":%d:.*%s", i+1, frag))
if err != nil {
t.Errorf("Invalid regexp after `ERROR HERE: `: %#q", frag)
continue
}
errors = append(errors, re)
}
re, err := regexp.Compile(fmt.Sprintf(":%d:.*%s", i+1, frag))
if err != nil {
t.Errorf("Invalid regexp after `ERROR HERE: `: %#q", frag)
continue

if _, frag, ok := bytes.Cut(line, []byte("ERROR MESSAGE: ")); ok {
re, err := regexp.Compile(string(frag))
if err != nil {
t.Errorf("Invalid regexp after `ERROR MESSAGE: `: %#q", frag)
continue
}
errors = append(errors, re)
}
errors = append(errors, re)
}
if len(errors) == 0 {
t.Fatalf("cannot find ERROR HERE")
Expand Down Expand Up @@ -106,6 +113,7 @@ func TestReportsTypeErrors(t *testing.T) {
for _, file := range []string{
"err1.go",
"err2.go",
"err5.go",
"issue11097a.go",
"issue11097b.go",
"issue18452.go",
Expand Down
11 changes: 11 additions & 0 deletions misc/cgo/errors/testdata/err5.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// 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 MESSAGE: only allowed in cgo-generated code

func main() {}
8 changes: 7 additions & 1 deletion src/cmd/compile/internal/noder/noder.go
Original file line number Diff line number Diff line change
Expand Up @@ -359,8 +359,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,
Expand Down

0 comments on commit 31d5b60

Please sign in to comment.