#include "machorep.h"
#include "StaticCode.h"
#include "reqmaker.h"
#include <security_utilities/logging.h>
#include <security_utilities/cfmunge.h>
namespace Security {
namespace CodeSigning {
using namespace UnixPlusPlus;
MachORep::MachORep(const char *path, const Context *ctx)
: SingleDiskRep(path), mSigningData(NULL)
{
if (ctx)
if (ctx->offset)
mExecutable = new Universal(fd(), (size_t)ctx->offset, ctx->size);
else if (ctx->arch) {
auto_ptr<Universal> full(new Universal(fd()));
mExecutable = new Universal(fd(), full->archOffset(ctx->arch), full->archLength(ctx->arch));
} else
mExecutable = new Universal(fd());
else
mExecutable = new Universal(fd());
assert(mExecutable);
CODESIGN_DISKREP_CREATE_MACHO(this, (char*)path, (void*)ctx);
}
MachORep::~MachORep()
{
delete mExecutable;
::free(mSigningData);
}
bool MachORep::candidate(FileDesc &fd)
{
switch (Universal::typeOf(fd)) {
case MH_EXECUTE:
case MH_DYLIB:
case MH_DYLINKER:
case MH_BUNDLE:
case MH_KEXT_BUNDLE:
case MH_PRELOAD:
return true; case MH_OBJECT:
return false; default:
return false; }
}
Universal *MachORep::mainExecutableImage()
{
return mExecutable;
}
void MachORep::prepareForSigning(SigningContext &context)
{
if (context.digestAlgorithms().empty()) {
auto_ptr<MachO> macho(mainExecutableImage()->architecture());
if (const version_min_command *version = macho->findMinVersion()) {
uint32_t limit = 0;
switch (macho->flip(version->cmd)) {
case LC_VERSION_MIN_MACOSX:
limit = (10 << 16 | 11 << 8 | 4 << 0);
break;
#if 0
case LC_VERSION_MIN_IPHONEOS:
limit = (9 << 16 | 3 << 8);
break;
case LC_VERSION_MIN_WATCHOS:
limit = (2 << 16 | 2 << 8);
break;
case LC_VERSION_MIN_TVOS:
limit = (9 << 16 | 2 << 8);
break;
default:
break;
#else
case LC_VERSION_MIN_IPHONEOS:
case LC_VERSION_MIN_WATCHOS:
case LC_VERSION_MIN_TVOS:
return;
default:
break;
#endif
}
if (macho->flip(version->version) >= limit) {
context.setDigestAlgorithm(kSecCodeSignatureHashSHA256);
}
}
}
}
size_t MachORep::signingBase()
{
return mainExecutableImage()->archOffset();
}
size_t MachORep::signingLimit()
{
auto_ptr<MachO> macho(mExecutable->architecture());
return macho->signingExtent();
}
CFDataRef MachORep::identification()
{
std::auto_ptr<MachO> macho(mainExecutableImage()->architecture());
return identificationFor(macho.get());
}
CFDataRef MachORep::identificationFor(MachO *macho)
{
if (const load_command *cmd = macho->findCommand(LC_UUID)) {
const uuid_command *uuidc = reinterpret_cast<const uuid_command *>(cmd);
if (macho->flip(uuidc->cmdsize) != sizeof(uuid_command))
MacOSError::throwMe(errSecCSSignatureInvalid);
char result[4 + sizeof(uuidc->uuid)];
memcpy(result, "UUID", 4);
memcpy(result+4, uuidc->uuid, sizeof(uuidc->uuid));
return makeCFData(result, sizeof(result));
}
SHA1 hash;
hash(&macho->header(), sizeof(mach_header));
hash(macho->loadCommands(), macho->commandLength());
SHA1::Digest digest;
hash.finish(digest);
return makeCFData(digest, sizeof(digest));
}
CFDataRef MachORep::component(CodeDirectory::SpecialSlot slot)
{
switch (slot) {
case cdInfoSlot:
return infoPlist();
default:
return embeddedComponent(slot);
}
}
CFDataRef MachORep::embeddedComponent(CodeDirectory::SpecialSlot slot)
{
if (!mSigningData) { auto_ptr<MachO> macho(mainExecutableImage()->architecture());
if (macho.get())
if (const linkedit_data_command *cs = macho->findCodeSignature()) {
size_t offset = macho->flip(cs->dataoff);
size_t length = macho->flip(cs->datasize);
if ((mSigningData = EmbeddedSignatureBlob::readBlob(macho->fd(), macho->offset() + offset, length))) {
secinfo("machorep", "%zd signing bytes in %d blob(s) from %s(%s)",
mSigningData->length(), mSigningData->count(),
mainExecutablePath().c_str(), macho->architecture().name());
} else {
secinfo("machorep", "failed to read signing bytes from %s(%s)",
mainExecutablePath().c_str(), macho->architecture().name());
MacOSError::throwMe(errSecCSSignatureInvalid);
}
}
}
if (mSigningData)
return mSigningData->component(slot);
return NULL;
}
CFDataRef MachORep::infoPlist()
{
CFRef<CFDataRef> info;
try {
auto_ptr<MachO> macho(mainExecutableImage()->architecture());
if (const section *sect = macho->findSection("__TEXT", "__info_plist")) {
if (macho->is64()) {
const section_64 *sect64 = reinterpret_cast<const section_64 *>(sect);
info.take(macho->dataAt(macho->flip(sect64->offset), (size_t)macho->flip(sect64->size)));
} else {
info.take(macho->dataAt(macho->flip(sect->offset), macho->flip(sect->size)));
}
}
} catch (...) {
secinfo("machorep", "exception reading embedded Info.plist");
}
return info.yield();
}
string MachORep::format()
{
if (Universal *fat = mainExecutableImage()) {
Universal::Architectures archs;
fat->architectures(archs);
if (fat->isUniversal()) {
string s = "Mach-O universal (";
for (Universal::Architectures::const_iterator it = archs.begin();
it != archs.end(); ++it) {
if (it != archs.begin())
s += " ";
s += it->displayName();
}
return s + ")";
} else {
assert(archs.size() == 1);
return string("Mach-O thin (") + archs.begin()->displayName() + ")";
}
} else
return "Mach-O (unrecognized format)";
}
void MachORep::flush()
{
size_t offset = mExecutable->offset();
size_t length = mExecutable->length();
delete mExecutable;
mExecutable = NULL;
::free(mSigningData);
mSigningData = NULL;
SingleDiskRep::flush();
mExecutable = new Universal(fd(), offset, length);
}
CFDictionaryRef MachORep::diskRepInformation()
{
auto_ptr<MachO> macho (mainExecutableImage()->architecture());
CFRef<CFDictionaryRef> info;
if (const version_min_command *version = macho->findMinVersion()) {
info.take(cfmake<CFMutableDictionaryRef>("{%O = %d,%O = %d,%O = %d}",
kSecCodeInfoDiskRepOSPlatform, macho->flip(version->cmd),
kSecCodeInfoDiskRepOSVersionMin, macho->flip(version->version),
kSecCodeInfoDiskRepOSSDKVersion, macho->flip(version->sdk)));
if (macho->flip(version->cmd) == LC_VERSION_MIN_MACOSX &&
macho->flip(version->sdk) < (10 << 16 | 9 << 8))
{
info.take(cfmake<CFMutableDictionaryRef>("{+%O, %O = 'OS X SDK version before 10.9 does not support Library Validation'}",
info.get(),
kSecCodeInfoDiskRepNoLibraryValidation));
}
}
return info.yield();
}
string MachORep::recommendedIdentifier(const SigningContext &ctx)
{
if (CFDataRef info = infoPlist()) {
if (CFRef<CFDictionaryRef> dict = makeCFDictionaryFrom(info)) {
CFStringRef code = CFStringRef(CFDictionaryGetValue(dict, kCFBundleIdentifierKey));
if (code && CFGetTypeID(code) != CFStringGetTypeID())
MacOSError::throwMe(errSecCSBadDictionaryFormat);
if (code)
return cfString(code);
} else
MacOSError::throwMe(errSecCSBadDictionaryFormat);
}
return SingleDiskRep::recommendedIdentifier(ctx);
}
const Requirements *MachORep::defaultRequirements(const Architecture *arch, const SigningContext &ctx)
{
assert(arch); Requirements::Maker maker;
if (Requirement *libreq = libraryRequirements(arch, ctx))
maker.add(kSecLibraryRequirementType, libreq);
return maker.make();
}
Requirement *MachORep::libraryRequirements(const Architecture *arch, const SigningContext &ctx)
{
auto_ptr<MachO> macho(mainExecutableImage()->architecture(*arch));
Requirement::Maker maker;
Requirement::Maker::Chain chain(maker, opOr);
if (macho.get())
if (const linkedit_data_command *ldep = macho->findLibraryDependencies()) {
size_t offset = macho->flip(ldep->dataoff);
size_t length = macho->flip(ldep->datasize);
if (LibraryDependencyBlob *deplist = LibraryDependencyBlob::readBlob(macho->fd(), macho->offset() + offset, length)) {
try {
secinfo("machorep", "%zd library dependency bytes in %d blob(s) from %s(%s)",
deplist->length(), deplist->count(),
mainExecutablePath().c_str(), macho->architecture().name());
unsigned count = deplist->count();
for (unsigned n = 0; n < count; n++) {
const Requirement *req = NULL;
if (const BlobCore *dep = deplist->blob(n)) {
if ((req = Requirement::specific(dep))) {
} else if (const BlobWrapper *wrap = BlobWrapper::specific(dep)) {
std::string reqString = std::string((const char *)wrap->data(), wrap->length());
CFRef<SecRequirementRef> areq;
MacOSError::check(SecRequirementCreateWithString(CFTempString(reqString), kSecCSDefaultFlags, &areq.aref()));
CFRef<CFDataRef> reqData;
MacOSError::check(SecRequirementCopyData(areq, kSecCSDefaultFlags, &reqData.aref()));
req = Requirement::specific((const BlobCore *)CFDataGetBytePtr(reqData));
} else {
secinfo("machorep", "unexpected blob type 0x%x in slot %d of binary dependencies", dep->magic(), n);
continue;
}
chain.add();
maker.copy(req);
} else
secinfo("machorep", "missing DR info for library index %d", n);
}
::free(deplist);
} catch (...) {
::free(deplist);
throw;
}
}
}
if (chain.empty())
return NULL;
else
return maker.make();
}
size_t MachORep::pageSize(const SigningContext &)
{
return segmentedPageSize;
}
void MachORep::strictValidate(const CodeDirectory* cd, const ToleratedErrors& tolerated, SecCSFlags flags)
{
SingleDiskRep::strictValidate(cd, tolerated, flags);
if (mExecutable->isSuspicious() && tolerated.find(errSecCSBadMainExecutable) == tolerated.end())
MacOSError::throwMe(errSecCSBadMainExecutable);
}
DiskRep::Writer *MachORep::writer()
{
return new Writer(this);
}
void MachORep::Writer::component(CodeDirectory::SpecialSlot slot, CFDataRef data)
{
assert(false);
Syslog::notice("code signing internal error: trying to write Mach-O component directly");
MacOSError::throwMe(errSecCSInternalError);
}
} }