Skip to content

Commit bfc86b7

Browse files
cmd/go: reject cgo -sectcreate linker flags by default
The Darwin linker reads the file named by -sectcreate and embeds its contents in the output. Allowing dependency-supplied #cgo LDFLAGS to use it lets a module read arbitrary files available to the build. Reject the flag by default. Users who explicitly trust it can retain the existing CGO_LDFLAGS_ALLOW override. Fixes #78750.
1 parent fb60d8c commit bfc86b7

3 files changed

Lines changed: 29 additions & 3 deletions

File tree

src/cmd/go/internal/work/security.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,8 @@ var invalidLinkerFlags = []*lazyregexp.Regexp{
158158
// On macOS this means the linker loads and executes the next argument.
159159
// Have to exclude separately because -lfoo is allowed in general.
160160
re(`-lto_library`),
161+
// On macOS this means the linker reads the file named by the last argument.
162+
re(`-Wl,-sectcreate,.*`),
161163
}
162164

163165
var validLinkerFlags = []*lazyregexp.Regexp{
@@ -223,7 +225,6 @@ var validLinkerFlags = []*lazyregexp.Regexp{
223225
re(`-Wl,-rpath(-link)?[=,]([^,@\-][^,]*)`),
224226
re(`-Wl,-s`),
225227
re(`-Wl,-search_paths_first`),
226-
re(`-Wl,-sectcreate,([^,@\-][^,]*),([^,@\-][^,]*),([^,@\-][^,]*)`),
227228
re(`-Wl,--start-group`),
228229
re(`-Wl,-?-static`),
229230
re(`-Wl,-?-subsystem,(native|windows|console|posix|xbox)`),

src/cmd/go/internal/work/security_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,6 @@ var goodLinkerFlags = [][]string{
202202
{"-L", "framework"},
203203
{"-framework", "Chocolate"},
204204
{"-v"},
205-
{"-Wl,-sectcreate,__TEXT,__info_plist,${SRCDIR}/Info.plist"},
206205
{"-Wl,-framework", "-Wl,Chocolate"},
207206
{"-Wl,-framework,Chocolate"},
208207
{"-Wl,-unresolved-symbols=ignore-all"},
@@ -220,7 +219,6 @@ var goodLinkerFlags = [][]string{
220219
{"-Wl,-framework,."},
221220
{"-Wl,-rpath,."},
222221
{"-Wl,-rpath-link,."},
223-
{"-Wl,-sectcreate,.,.,."},
224222
{"-Wl,-syslibroot,."},
225223
{"-Wl,-undefined,."},
226224
}
@@ -289,6 +287,9 @@ var badLinkerFlags = [][]string{
289287
{"-Wl,-e="},
290288
{"-Wl,-e,"},
291289
{"-Wl,-R,-flag"},
290+
{"-Wl,-sectcreate,.,.,."},
291+
{"-Wl,-sectcreate,__TEXT,__info_plist,${SRCDIR}/Info.plist"},
292+
{"-Wl,-sectcreate,__TEXT,__info,/etc/hosts"},
292293
{"-Wl,--push-state,"},
293294
{"-Wl,--push-state,@foo"},
294295
{"-fplugin=./-Wl,--push-state,-R.so"},
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
[!GOOS:darwin] skip
2+
[!cgo] skip
3+
4+
! go build
5+
stderr '^ldflag: invalid flag in #cgo LDFLAGS: -Wl,-sectcreate,__TEXT,__info,.*info.txt'
6+
7+
env CGO_LDFLAGS_ALLOW=-Wl,-sectcreate,.*
8+
go build
9+
10+
-- go.mod --
11+
module ldflag
12+
13+
go 1.26
14+
15+
-- main.go --
16+
package main
17+
18+
// #cgo LDFLAGS: -Wl,-sectcreate,__TEXT,__info,${SRCDIR}/info.txt
19+
import "C"
20+
21+
func main() {}
22+
23+
-- info.txt --
24+
trusted local input

0 commit comments

Comments
 (0)