Skip to content

Commit

Permalink
ensure a valid ELF file provided (nanovms#698) (nanovms#708)
Browse files Browse the repository at this point in the history
  • Loading branch information
emikhalev authored Oct 14, 2020
1 parent 3d9174c commit a1576e7
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions lepton/ldd_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,16 @@ func getSharedLibs(targetRoot string, path string) ([]string, error) {
return nil, err
}

// Check file is a valid ELF
isELF, err := isELF(path)
if err != nil {
return nil, errors.WrapPrefix(err, path, 0)
}
if !isELF {
fmt.Printf(ErrorColor, "Only ELF binaries are supported. Is thia a Linux binary? run 'file "+path+"' on it\n")
os.Exit(1)
}

if _, err := os.Stat(path); err != nil {
return nil, errors.Wrap(err, 1)
}
Expand Down Expand Up @@ -99,3 +109,16 @@ func getSharedLibs(targetRoot string, path string) ([]string, error) {
}
return deps, nil
}

// isELF returns true if file is valid ELF
func isELF(path string) (bool, error) {
fd, err := elf.Open(path)
if err != nil {
if strings.Contains(err.Error(), "bad magic number") {
return false, nil
}
return false, err
}
fd.Close()
return true, nil
}

0 comments on commit a1576e7

Please sign in to comment.