Skip to content

Commit 7a8ac6a

Browse files
committed
core/overreach: reset secure mode on log level changes
1 parent e9d4b04 commit 7a8ac6a

File tree

4 files changed

+37
-10
lines changed

4 files changed

+37
-10
lines changed

intra/backend/netstat.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,7 @@ type GoStat struct {
229229
NumCgo int64 // number of cgo calls
230230
NumCPU int64 // number of CPUs
231231

232+
Trac string // gotraceback
232233
Pers string // personality
233234
Args string // command line arguments
234235
Env string // environment variables

intra/core/overreach.go

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
package core
88

99
import (
10+
"runtime/debug"
1011
"syscall"
1112
_ "unsafe" // for go:linkname
1213
)
@@ -17,7 +18,6 @@ func RuntimeEnviron() []string {
1718
}
1819

1920
// github.com/golang/go/issues/69868
20-
// RuntimeSecureMode reports whether the Go runtime is in secure mode.
2121
// Unfortunately, Android apps have AT_SECURE set
2222
// (read bytes in /proc/self/auxv on non-rooted Androids).
2323
// This means, on Go runtime fatal / throws and a few kinds of panics,
@@ -28,16 +28,37 @@ func RuntimeEnviron() []string {
2828
// Perhaps, there's security benefits to the Go runtime being this rigid
2929
// about GOTRACEBACK, but for goos.IsAndroid (and for apps with uid > 10000),
3030
// using AT_SECURE to determine "setuid-like" protections appears pointless.
31-
func RuntimeSecureMode() bool {
32-
return runtime_iss()
31+
func init() {
32+
// override runtime.secureMode
33+
// to make GOTRACEBACK work as expected on Android
34+
debug.SetTraceback("all")
35+
secureMode = false
36+
}
37+
38+
func SecureMode(new bool) (prev bool) {
39+
prev = secureMode
40+
secureMode = new
41+
return prev
3342
}
3443

35-
//go:linkname runtime_iss runtime.isSecureMode
36-
func runtime_iss() bool
44+
// RuntimeSecureMode reports whether the Go runtime is in secure mode.
45+
func RuntimeSecureMode() (them, us bool) {
46+
return runtime_isSecureMode(), secureMode
47+
}
48+
49+
func RuntimeGotraceback() (l int32, all, crash bool) {
50+
return runtime_gotraceback()
51+
}
52+
53+
//go:linkname runtime_isSecureMode runtime.isSecureMode
54+
func runtime_isSecureMode() bool
55+
56+
//go:linkname runtime_gotraceback runtime.gotraceback
57+
func runtime_gotraceback() (int32, bool, bool)
3758

3859
// pushing func symbols does not work on go1.24+
3960
// but pushing vars apparently still works provided
4061
// -ldflags="checklinkname=0"
4162

42-
//go:linkname iss runtime.secureMode
43-
var iss bool
63+
//go:linkname secureMode runtime.secureMode
64+
var secureMode bool

intra/tun2socks.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ func LogLevel(gologLevel, consolelogLevel int32) {
114114
log.SetConsoleLevel(clvl)
115115
dbg := dlvl <= log.DEBUG || clvl <= log.DEBUG
116116
settings.Debug = dbg
117+
prevsm := core.SecureMode(false /*off*/)
117118
// traceback is always set to "crash" for c-shared / c-archive buildmodes
118119
// github.com/golang/go/blob/fed3b0a298/src/runtime/runtime1.go#L586
119120
// gomobile builds a c-shared gojnilib:
@@ -125,8 +126,8 @@ func LogLevel(gologLevel, consolelogLevel int32) {
125126
debug.SetTraceback(usr.s())
126127
}
127128

128-
log.I("tun: new levels; golog: %d, consolelog: %d; debug? %t; traceback: %s",
129-
dlvl, clvl, dbg, envtraceback)
129+
log.I("tun: new levels; golog: %d, consolelog: %d; debug? %t; traceback: %s; sm? %t",
130+
dlvl, clvl, dbg, envtraceback, prevsm)
130131
}
131132

132133
// FlightRecorder starts Go runtime's flight recorder if y is true,

intra/tunnel.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -519,9 +519,13 @@ func (t *rtunnel) stat() (*x.NetStat, error) {
519519
out.GOSt.NumCgo = int64(runtime.NumCgoCall())
520520
out.GOSt.NumCPU = int64(runtime.NumCPU())
521521

522+
l, all, crash := core.RuntimeGotraceback()
523+
out.GOSt.Trac = fmt.Sprintf("%d; all? %t; crash? %t", l, all, crash)
524+
525+
sm1, sm2 := core.RuntimeSecureMode()
522526
uid := fmt.Sprintf("uid=%d", syscall.Getuid())
523527
pid := fmt.Sprintf("pid=%d", syscall.Getpid())
524-
sec := fmt.Sprintf("sec=%t", core.RuntimeSecureMode())
528+
sec := fmt.Sprintf("sec=%t/%t", sm1, sm2)
525529
out.GOSt.Args = strings.Join(append(os.Args, uid, pid, sec), ";")
526530
out.GOSt.Env = strings.Join(core.RuntimeEnviron(), ";")
527531
out.GOSt.Pers, _ = os.Executable()

0 commit comments

Comments
 (0)