Skip to content

Commit d733d29

Browse files
committed
core/overreach: override runtime.secureMode for tracebacks on panics & fatals
1 parent 31a9ca6 commit d733d29

File tree

3 files changed

+31
-2
lines changed

3 files changed

+31
-2
lines changed

Makefile

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@ DATESTR=$(shell date -u +'%Y%m%d%H%M%S')
99
XGO_LDFLAGS='-s -w -X main.version=$(COMMIT_ID)'
1010
# github.com/xjasonlyu/tun2socks/blob/bf745d0e0/Makefile#L14
1111
LDFLAGS_DEBUG='-buildid= -X $(IMPORT_PATH)/intra/core.Date=$(DATESTR) -X $(IMPORT_PATH)/intra/core.Commit=$(COMMIT_ID)'
12-
LDFLAGS='-w -s -buildid= -X $(IMPORT_PATH)/intra/core.Date=$(DATESTR) -X $(IMPORT_PATH)/intra/core.Commit=$(COMMIT_ID)'
12+
# checklinkname to override runtime.secureMode; see: core/overreach.go
13+
# github.com/golang/go/issues/69868
14+
LDFLAGS='-checklinkname=0 -w -s -buildid= -X $(IMPORT_PATH)/intra/core.Date=$(DATESTR) -X $(IMPORT_PATH)/intra/core.Commit=$(COMMIT_ID)'
1315
CGO_LDFLAGS="$(CGO_LDFLAGS) -s -w -Wl,-z,max-page-size=16384"
1416

1517
# github.com/golang/mobile/blob/a1d90793fc/cmd/gomobile/bind.go#L36

intra/core/overreach.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,29 @@ import (
1414
func RuntimeEnviron() []string {
1515
return syscall.Environ()
1616
}
17+
18+
// github.com/golang/go/issues/69868
19+
// RuntimeSecureMode reports whether the Go runtime is in secure mode.
20+
// Unfortunately, Android apps have AT_SECURE set
21+
// (read bytes in /proc/self/auxv on non-rooted Androids).
22+
// This means, on Go runtime fatal / throws and a few kinds of panics,
23+
// only one line is output to logcat (Android's stderr) which makes it
24+
// hard to tell just what went wrong. Android, does use unwinder for
25+
// native apps, and the Android RunTime has its own unwinder;
26+
// both of which traceback seemingly oblivious to AT_SECURE.
27+
// Perhaps, there's security benefits to the Go runtime being this rigid
28+
// about GOTRACEBACK, but for goos.IsAndroid (and for apps with uid > 10000),
29+
// using AT_SECURE to determine "setuid-like" protections appears pointless.
30+
func RuntimeSecureMode() bool {
31+
return runtime_iss()
32+
}
33+
34+
//go:linkname runtime_iss runtime.isSecureMode
35+
func runtime_iss() bool
36+
37+
// pushing func symbols does not work on go1.24+
38+
// but pushing vars apparently still works provided
39+
// -ldflags="checklinkname=0"
40+
41+
//go:linkname iss runtime.secureMode
42+
var iss bool

intra/tunnel.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -521,7 +521,8 @@ func (t *rtunnel) stat() (*x.NetStat, error) {
521521

522522
uid := fmt.Sprintf("uid=%d", syscall.Getuid())
523523
pid := fmt.Sprintf("pid=%d", syscall.Getpid())
524-
out.GOSt.Args = strings.Join(append(os.Args, uid, pid), ";")
524+
sec := fmt.Sprintf("sec=%t", core.RuntimeSecureMode())
525+
out.GOSt.Args = strings.Join(append(os.Args, uid, pid, sec), ";")
525526
out.GOSt.Env = strings.Join(core.RuntimeEnviron(), ";")
526527
out.GOSt.Pers, _ = os.Executable()
527528

0 commit comments

Comments
 (0)