2017-08-26 01:34:03 +02:00
|
|
|
//
|
|
|
|
// OCCAM
|
|
|
|
//
|
|
|
|
// Copyright (c) 2017, SRI International
|
|
|
|
//
|
|
|
|
// All rights reserved.
|
|
|
|
//
|
|
|
|
// Redistribution and use in source and binary forms, with or without
|
|
|
|
// modification, are permitted provided that the following conditions are met:
|
|
|
|
//
|
|
|
|
// * Redistributions of source code must retain the above copyright notice, this
|
|
|
|
// list of conditions and the following disclaimer.
|
|
|
|
//
|
|
|
|
// * Redistributions in binary form must reproduce the above copyright notice,
|
|
|
|
// this list of conditions and the following disclaimer in the documentation
|
|
|
|
// and/or other materials provided with the distribution.
|
|
|
|
//
|
|
|
|
// * Neither the name of SRI International nor the names of its contributors may
|
|
|
|
// be used to endorse or promote products derived from this software without
|
|
|
|
// specific prior written permission.
|
|
|
|
//
|
|
|
|
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
|
|
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
|
|
|
// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
|
|
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
|
|
|
// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
|
|
// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
//
|
|
|
|
|
2017-07-05 17:14:11 +02:00
|
|
|
package shared
|
2017-06-30 20:54:59 +02:00
|
|
|
|
|
|
|
import (
|
2020-10-30 23:04:23 +01:00
|
|
|
"debug/elf"
|
|
|
|
"debug/macho"
|
|
|
|
"os"
|
2017-06-30 20:54:59 +02:00
|
|
|
"os/exec"
|
2020-10-30 23:04:23 +01:00
|
|
|
"runtime"
|
2017-06-30 20:54:59 +02:00
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
2020-10-31 16:05:29 +01:00
|
|
|
//BinaryType is the 'intersection' of elf.Type and macho.Type and partitions
|
|
|
|
// the binary world into categories we are most interested in. Missing is
|
|
|
|
// ARCHIVE but that is because it is not an elf format, so we cannot entirely
|
|
|
|
// eliminate the use of the 'file' utility (cf getFileType below).
|
|
|
|
type BinaryType uint32
|
|
|
|
|
|
|
|
const (
|
|
|
|
//BinaryUnknown signals that the file does not fit into our three simple minded categories
|
|
|
|
BinaryUnknown BinaryType = 0
|
|
|
|
//BinaryObject is the type of an object file, the output unit of compilation
|
|
|
|
BinaryObject BinaryType = 1
|
|
|
|
//BinaryExecutable is the type of an executable file
|
|
|
|
BinaryExecutable BinaryType = 2
|
|
|
|
//BinaryShared is the type of a shared or dynamic library
|
|
|
|
BinaryShared BinaryType = 3
|
|
|
|
)
|
|
|
|
|
|
|
|
func elfType2BinaryType(et elf.Type) (bt BinaryType) {
|
|
|
|
bt = BinaryUnknown
|
|
|
|
switch et {
|
|
|
|
case elf.ET_NONE:
|
|
|
|
bt = BinaryUnknown
|
|
|
|
case elf.ET_REL:
|
|
|
|
bt = BinaryObject
|
|
|
|
case elf.ET_EXEC:
|
|
|
|
bt = BinaryExecutable
|
|
|
|
case elf.ET_DYN:
|
|
|
|
bt = BinaryShared
|
|
|
|
case elf.ET_CORE, elf.ET_LOOS, elf.ET_HIOS, elf.ET_LOPROC, elf.ET_HIPROC:
|
|
|
|
bt = BinaryUnknown
|
|
|
|
default:
|
|
|
|
bt = BinaryUnknown
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func machoType2BinaryType(mt macho.Type) (bt BinaryType) {
|
|
|
|
bt = BinaryUnknown
|
|
|
|
switch mt {
|
|
|
|
case macho.TypeObj:
|
|
|
|
bt = BinaryObject
|
|
|
|
case macho.TypeExec:
|
|
|
|
bt = BinaryExecutable
|
|
|
|
case macho.TypeDylib:
|
|
|
|
bt = BinaryShared
|
|
|
|
case macho.TypeBundle:
|
|
|
|
bt = BinaryUnknown
|
|
|
|
default:
|
|
|
|
bt = BinaryUnknown
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// isPlainFile returns true if the file is stat-able (i.e. exists etc), and is not a directory, else it returns false.
|
|
|
|
func isPlainFile(objectFile string) (ok bool) {
|
|
|
|
info, err := os.Stat(objectFile)
|
|
|
|
if os.IsNotExist(err) || info.IsDir() {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
ok = true
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func injectableViaFileType(objectFile string) (ok bool, err error) {
|
|
|
|
plain := isPlainFile(objectFile)
|
|
|
|
if !plain {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
fileType, err := getFileType(objectFile)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
ok = (fileType == fileTypeELFOBJECT) || (fileType == fileTypeELFOBJECT)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func injectableViaDebug(objectFile string) (ok bool, err error) {
|
|
|
|
// I guess we are not doing cross compiling. Otherwise we are fucking up here.
|
|
|
|
ok, err = IsObjectFileForOS(objectFile, runtime.GOOS)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// ElfFileType returns the elf.Type of the given file name
|
|
|
|
func ElfFileType(objectFile string) (code BinaryType, err error) {
|
|
|
|
var lbinFile *elf.File
|
|
|
|
lbinFile, err = elf.Open(objectFile)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
code = elfType2BinaryType(lbinFile.FileHeader.Type)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// MachoFileType returns the macho.Type of the given file name
|
|
|
|
func MachoFileType(objectFile string) (code BinaryType, err error) {
|
|
|
|
var dbinFile *macho.File
|
|
|
|
dbinFile, err = macho.Open(objectFile)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
code = machoType2BinaryType(dbinFile.FileHeader.Type)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
//IsObjectFileForOS returns true if the given file is an object file for the given OS, using the debug/elf and debug/macho packages.
|
|
|
|
func IsObjectFileForOS(objectFile string, operatingSys string) (ok bool, err error) {
|
|
|
|
plain := isPlainFile(objectFile)
|
|
|
|
if !plain {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
var binaryType BinaryType
|
|
|
|
switch operatingSys {
|
|
|
|
case "linux", "freebsd":
|
|
|
|
binaryType, err = ElfFileType(objectFile)
|
|
|
|
case "darwin":
|
|
|
|
binaryType, err = MachoFileType(objectFile)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
ok = (binaryType == BinaryObject)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// file types via the unix 'file' utility
|
2017-06-30 20:54:59 +02:00
|
|
|
const (
|
|
|
|
// File types
|
|
|
|
fileTypeUNDEFINED = iota
|
|
|
|
fileTypeELFEXECUTABLE
|
|
|
|
fileTypeELFOBJECT
|
|
|
|
fileTypeELFSHARED
|
|
|
|
fileTypeMACHEXECUTABLE
|
|
|
|
fileTypeMACHOBJECT
|
|
|
|
fileTypeMACHSHARED
|
|
|
|
fileTypeARCHIVE
|
2018-04-28 03:31:38 +02:00
|
|
|
fileTypeTHINARCHIVE
|
2019-10-26 18:47:47 +02:00
|
|
|
|
|
|
|
fileTypeERROR
|
2017-06-30 20:54:59 +02:00
|
|
|
)
|
|
|
|
|
2020-10-31 16:05:29 +01:00
|
|
|
//iam: this is not that robust, because it depends on the file utility "file" which is
|
|
|
|
// often missing on docker images (the klee docker file had this problem)
|
|
|
|
// this is only used in extraction, not in compilation.
|
2020-10-30 23:04:23 +01:00
|
|
|
func getFileType(realPath string) (fileType int, err error) {
|
2017-06-30 20:54:59 +02:00
|
|
|
// We need the file command to guess the file type
|
2020-10-30 23:04:23 +01:00
|
|
|
fileType = fileTypeERROR
|
2017-06-30 20:54:59 +02:00
|
|
|
cmd := exec.Command("file", realPath)
|
|
|
|
out, err := cmd.Output()
|
|
|
|
if err != nil {
|
2019-10-26 18:47:47 +02:00
|
|
|
LogError("There was an error getting the type of %s. Make sure that the 'file' command is installed.", realPath)
|
|
|
|
return
|
2017-06-30 20:54:59 +02:00
|
|
|
}
|
2018-04-28 04:29:45 +02:00
|
|
|
fo := string(out)
|
|
|
|
if strings.Contains(fo, "ELF") {
|
|
|
|
|
|
|
|
if strings.Contains(fo, "executable") {
|
|
|
|
fileType = fileTypeELFEXECUTABLE
|
|
|
|
} else if strings.Contains(fo, "shared") {
|
|
|
|
fileType = fileTypeELFSHARED
|
|
|
|
} else if strings.Contains(fo, "relocatable") {
|
|
|
|
fileType = fileTypeELFOBJECT
|
|
|
|
} else {
|
|
|
|
fileType = fileTypeUNDEFINED
|
|
|
|
}
|
|
|
|
|
|
|
|
} else if strings.Contains(fo, "Mach-O") {
|
|
|
|
|
|
|
|
if strings.Contains(fo, "executable") {
|
|
|
|
fileType = fileTypeMACHEXECUTABLE
|
|
|
|
} else if strings.Contains(fo, "dynamically linked shared") {
|
|
|
|
fileType = fileTypeMACHSHARED
|
|
|
|
} else if strings.Contains(fo, "object") {
|
|
|
|
fileType = fileTypeMACHOBJECT
|
|
|
|
} else {
|
|
|
|
fileType = fileTypeUNDEFINED
|
|
|
|
}
|
|
|
|
|
2017-06-30 20:54:59 +02:00
|
|
|
} else if strings.Contains(fo, "current ar archive") {
|
|
|
|
fileType = fileTypeARCHIVE
|
2018-04-28 03:31:38 +02:00
|
|
|
} else if strings.Contains(fo, "thin archive") {
|
|
|
|
fileType = fileTypeTHINARCHIVE
|
2018-04-28 04:30:55 +02:00
|
|
|
} else {
|
2017-06-30 20:54:59 +02:00
|
|
|
fileType = fileTypeUNDEFINED
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|