runtime: properly frame panic values in tracebacks

This CL causes the printing of panic values to ensure that all
newlines in the output are immediately followed by a tab, so
that there is no way for a maliciously crafted panic value to
fool a program attempting to parse the traceback into thinking
that the panic value is in fact a goroutine stack.

See https://github.com/golang/go/issues/64590#issuecomment-1932675696

+ release note

Updates #64590
Updates #63455

Change-Id: I5142acb777383c0c122779d984e73879567dc627
Reviewed-on: https://go-review.googlesource.com/c/go/+/581215
Auto-Submit: Alan Donovan <adonovan@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Michael Pratt <mpratt@google.com>
This commit is contained in:
Alan Donovan 2024-04-23 12:44:54 -04:00 committed by Gopher Robot
parent 4513f1a1c1
commit 69e75c8581
8 changed files with 56 additions and 22 deletions

View file

@ -656,7 +656,7 @@ func printpanics(p *_panic) {
return
}
print("panic: ")
printany(p.arg)
printpanicval(p.arg)
if p.recovered {
print(" [recovered]")
}
@ -718,20 +718,20 @@ func gopanic(e any) {
gp := getg()
if gp.m.curg != gp {
print("panic: ")
printany(e)
printpanicval(e)
print("\n")
throw("panic on system stack")
}
if gp.m.mallocing != 0 {
print("panic: ")
printany(e)
printpanicval(e)
print("\n")
throw("panic during malloc")
}
if gp.m.preemptoff != "" {
print("panic: ")
printany(e)
printpanicval(e)
print("\n")
print("preempt off reason: ")
print(gp.m.preemptoff)
@ -740,7 +740,7 @@ func gopanic(e any) {
}
if gp.m.locks != 0 {
print("panic: ")
printany(e)
printpanicval(e)
print("\n")
throw("panic holding locks")
}
@ -1015,7 +1015,9 @@ func throw(s string) {
// Everything throw does should be recursively nosplit so it
// can be called even when it's unsafe to grow the stack.
systemstack(func() {
print("fatal error: ", s, "\n")
print("fatal error: ")
printpanicval(s)
print("\n")
})
fatalthrow(throwTypeRuntime)
@ -1034,7 +1036,9 @@ func fatal(s string) {
// Everything fatal does should be recursively nosplit so it
// can be called even when it's unsafe to grow the stack.
systemstack(func() {
print("fatal error: ", s, "\n")
print("fatal error: ")
printpanicval(s)
print("\n")
})
fatalthrow(throwTypeUser)