Skip to content

Commit

Permalink
1. Slightly improved userspace patcher speed for 10.12
Browse files Browse the repository at this point in the history
2. Added missing dyld_shared_cache detection with a fallback
3. Defined High Sierra kernel version
  • Loading branch information
vit9696 committed Jun 6, 2017
1 parent 26ba381 commit adf6825
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 34 deletions.
5 changes: 5 additions & 0 deletions Changelog.md
@@ -1,6 +1,11 @@
Lilu Changelog
==============

#### v1.1.4
- Slightly improved userspace patcher speed for 10.12
- Added missing dyld_shared_cache detection with a fallback
- Defined High Sierra kernel version

#### v1.1.3
- Reduced binary size by modding capstone
- Fixed LiluAPI::onProcLoad return code
Expand Down
4 changes: 2 additions & 2 deletions Lilu.xcodeproj/project.pbxproj
Expand Up @@ -623,7 +623,7 @@
MODULE_NAME = as.vit9696.Lilu;
MODULE_START = kern_start;
MODULE_STOP = kern_stop;
MODULE_VERSION = 1.1.3;
MODULE_VERSION = 1.1.4;
OTHER_CFLAGS = (
"-mmmx",
"-msse",
Expand Down Expand Up @@ -677,7 +677,7 @@
MODULE_NAME = as.vit9696.Lilu;
MODULE_START = kern_start;
MODULE_STOP = kern_stop;
MODULE_VERSION = 1.1.3;
MODULE_VERSION = 1.1.4;
OTHER_CFLAGS = (
"-mmmx",
"-msse",
Expand Down
3 changes: 2 additions & 1 deletion Lilu/Headers/kern_util.hpp
Expand Up @@ -83,7 +83,8 @@ enum KernelVersion {
Mavericks = 13,
Yosemite = 14,
ElCapitan = 15,
Sierra = 16
Sierra = 16,
HighSierra = 17
};

/**
Expand Down
70 changes: 39 additions & 31 deletions Lilu/Sources/kern_user.cpp
Expand Up @@ -460,36 +460,44 @@ bool UserPatcher::loadDyldSharedCacheMapping() {
}

bool res {false};
auto entries = Buffer::create<MapEntry>(binaryModSize);
if (entries && buffer && bufferSize > 0) {
for (size_t i = 0; i < binaryModSize; i++) {
entries[i].filename = binaryMod[i]->path;
entries[i].length = strlen(binaryMod[i]->path);
entries[i].startTEXT = entries[i].endTEXT = entries[i].startDATA = entries[i].endDATA = 0;
}

size_t nEntries = mapAddresses(reinterpret_cast<char *>(buffer), entries, binaryModSize);

if (nEntries > 0) {
DBGLOG("user @ mapped %zu entries out of %zu", nEntries, binaryModSize);


if (buffer && bufferSize > 0) {
auto entries = Buffer::create<MapEntry>(binaryModSize);
if (entries) {
for (size_t i = 0; i < binaryModSize; i++) {
binaryMod[i]->startTEXT = entries[i].startTEXT;
binaryMod[i]->endTEXT = entries[i].endTEXT;
binaryMod[i]->startDATA = entries[i].startDATA;
binaryMod[i]->endDATA = entries[i].endDATA;
entries[i].filename = binaryMod[i]->path;
entries[i].length = strlen(binaryMod[i]->path);
entries[i].startTEXT = entries[i].endTEXT = entries[i].startDATA = entries[i].endDATA = 0;
}

res = true;
size_t nEntries = mapAddresses(reinterpret_cast<char *>(buffer), entries, binaryModSize);

if (nEntries > 0) {
DBGLOG("user @ mapped %zu entries out of %zu", nEntries, binaryModSize);

for (size_t i = 0; i < binaryModSize; i++) {
binaryMod[i]->startTEXT = entries[i].startTEXT;
binaryMod[i]->endTEXT = entries[i].endTEXT;
binaryMod[i]->startDATA = entries[i].startDATA;
binaryMod[i]->endDATA = entries[i].endDATA;
}

res = true;
} else {
SYSLOG("user @ failed to map any entry out of %zu", binaryModSize);
}
} else {
SYSLOG("user @ failed to map any entry out of %zu", binaryModSize);
SYSLOG("user @ failed to allocate memory for MapEntry %zu", binaryModSize);
}

if (entries) Buffer::deleter(entries);
} else {
SYSLOG("user @ failed to allocate memory for MapEntry %zu", binaryModSize);
SYSLOG("user @ no dyld_shared_cache discovered, fallback to slow!");
patchDyldSharedCache = false;
res = true;
}

if (buffer) Buffer::deleter(buffer);
if (entries) Buffer::deleter(entries);

return res;
}
Expand Down Expand Up @@ -714,28 +722,28 @@ vm_prot_t UserPatcher::getPageProtection(vm_map_t map, vm_map_address_t addr) {
}

bool UserPatcher::hookMemoryAccess() {
mach_vm_address_t kern = patcher->solveSymbol(KernelPatcher::KernelID, "_cs_validate_page");
// 10.12 and newer
mach_vm_address_t kern = patcher->solveSymbol(KernelPatcher::KernelID, "_cs_validate_range");

if (patcher->getError() == KernelPatcher::Error::NoError) {
orgCodeSignValidatePageWrapper = reinterpret_cast<t_codeSignValidatePageWrapper>(
patcher->routeFunction(kern, reinterpret_cast<mach_vm_address_t>(codeSignValidatePageWrapper), true, true)
orgCodeSignValidateRangeWrapper = reinterpret_cast<t_codeSignValidateRangeWrapper>(
patcher->routeFunction(kern, reinterpret_cast<mach_vm_address_t>(codeSignValidateRangeWrapper), true, true)
);

if (patcher->getError() != KernelPatcher::Error::NoError) {
SYSLOG("user @ failed to hook _cs_validate_page");
SYSLOG("user @ failed to hook _cs_validate_range");
patcher->clearError();
return false;
}
// 10.12 and newer
} else if (patcher->clearError(),
kern = patcher->solveSymbol(KernelPatcher::KernelID, "_cs_validate_range"),
kern = patcher->solveSymbol(KernelPatcher::KernelID, "_cs_validate_page"),
patcher->getError() == KernelPatcher::Error::NoError) {
orgCodeSignValidateRangeWrapper = reinterpret_cast<t_codeSignValidateRangeWrapper>(
patcher->routeFunction(kern, reinterpret_cast<mach_vm_address_t>(codeSignValidateRangeWrapper), true, true)
orgCodeSignValidatePageWrapper = reinterpret_cast<t_codeSignValidatePageWrapper>(
patcher->routeFunction(kern, reinterpret_cast<mach_vm_address_t>(codeSignValidatePageWrapper), true, true)
);

if (patcher->getError() != KernelPatcher::Error::NoError) {
SYSLOG("user @ failed to hook _cs_validate_range");
SYSLOG("user @ failed to hook _cs_validate_page");
patcher->clearError();
return false;
}
Expand Down

0 comments on commit adf6825

Please sign in to comment.