From 7f5f22b2260c238edc1efa186cad7c656998d92a Mon Sep 17 00:00:00 2001 From: Yabin Cui Date: Wed, 31 Aug 2022 16:23:43 -0700 Subject: [PATCH] symbol_inject: add step to codesign Mach-O binaries. The adhoc codesign of Macho-O binaries is broken after symbol injection. MacOS refuses to run the binaries unless we sign them again. Bug: 241493489 Test: build and run simpleperf_ndk64 on MacOS Change-Id: I25ef5c6413bd97e1bfa0a4ec5d04eaefb6fea54c --- symbol_inject/cmd/symbol_inject.go | 9 +++++++++ symbol_inject/macho.go | 8 +++++++- symbol_inject/symbol_inject.go | 7 ++++--- 3 files changed, 20 insertions(+), 4 deletions(-) diff --git a/symbol_inject/cmd/symbol_inject.go b/symbol_inject/cmd/symbol_inject.go index 1397b37b1..89b361907 100644 --- a/symbol_inject/cmd/symbol_inject.go +++ b/symbol_inject/cmd/symbol_inject.go @@ -94,4 +94,13 @@ func main() { os.Remove(*output) os.Exit(5) } + + if file.IsMachoFile { + err = symbol_inject.CodeSignMachoFile(*output) + if err != nil { + fmt.Fprintln(os.Stderr, err.Error()) + os.Remove(*output) + os.Exit(6) + } + } } diff --git a/symbol_inject/macho.go b/symbol_inject/macho.go index 6ee3f4fbd..9946d34af 100644 --- a/symbol_inject/macho.go +++ b/symbol_inject/macho.go @@ -18,6 +18,7 @@ import ( "debug/macho" "fmt" "io" + "os/exec" "sort" "strings" ) @@ -40,7 +41,7 @@ func extractMachoSymbols(machoFile *macho.File) (*File, error) { return symbols[i].Value < symbols[j].Value }) - file := &File{} + file := &File{IsMachoFile: true} for _, section := range machoFile.Sections { file.Sections = append(file.Sections, &Section{ @@ -95,3 +96,8 @@ func dumpMachoSymbols(r io.ReaderAt) error { return nil } + +func CodeSignMachoFile(path string) error { + cmd := exec.Command("/usr/bin/codesign", "--force", "-s", "-", path) + return cmd.Run() +} diff --git a/symbol_inject/symbol_inject.go b/symbol_inject/symbol_inject.go index 2a3d67e6d..77aff6fe3 100644 --- a/symbol_inject/symbol_inject.go +++ b/symbol_inject/symbol_inject.go @@ -161,9 +161,10 @@ func findSymbol(file *File, symbolName string) (uint64, uint64, error) { } type File struct { - r io.ReaderAt - Symbols []*Symbol - Sections []*Section + r io.ReaderAt + Symbols []*Symbol + Sections []*Section + IsMachoFile bool } type Symbol struct {