Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add panic handler for COFF execution #2

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion src/coff/coff_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ package coff
import (
_ "embed"
"fmt"
"github.com/praetorian-inc/goffloader/src/lighthouse"
"runtime/debug"
"strings"
"syscall"
"unsafe"

"github.com/praetorian-inc/goffloader/src/lighthouse"

"github.com/RIscRIpt/pecoff"
"github.com/RIscRIpt/pecoff/binutil"
"github.com/RIscRIpt/pecoff/windef"
Expand Down Expand Up @@ -327,6 +329,15 @@ func LoadWithMethod(coffBytes []byte, argBytes []byte, method string) (string, e
func invokeMethod(methodName string, argBytes []byte, parsedCoff *pecoff.File, sectionMap map[string]CoffSection, outChannel chan<- interface{}) {
defer close(outChannel)

// Catch unexpected panics and propagate them to the output channel
// This prevents the host program from terminating unexpectedly
defer func() {
if r := recover(); r != nil {
errorMsg := fmt.Sprintf("Panic occurred when executing COFF: %v\n%s", r, debug.Stack())
outChannel <- errorMsg
}
}()

// Call the entry point
for _, symbol := range parsedCoff.Symbols {
if symbol.NameString() == methodName {
Expand Down