From adf68259a940418fa1dfd24f587dcef9456f2bc8 Mon Sep 17 00:00:00 2001 From: vit9696 Date: Tue, 6 Jun 2017 17:23:46 +0300 Subject: [PATCH] 1. Slightly improved userspace patcher speed for 10.12 2. Added missing dyld_shared_cache detection with a fallback 3. Defined High Sierra kernel version --- Changelog.md | 5 +++ Lilu.xcodeproj/project.pbxproj | 4 +- Lilu/Headers/kern_util.hpp | 3 +- Lilu/Sources/kern_user.cpp | 70 +++++++++++++++++++--------------- 4 files changed, 48 insertions(+), 34 deletions(-) diff --git a/Changelog.md b/Changelog.md index 82c930ea..6bee896a 100644 --- a/Changelog.md +++ b/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 diff --git a/Lilu.xcodeproj/project.pbxproj b/Lilu.xcodeproj/project.pbxproj index fd5768e5..33342dbf 100644 --- a/Lilu.xcodeproj/project.pbxproj +++ b/Lilu.xcodeproj/project.pbxproj @@ -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", @@ -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", diff --git a/Lilu/Headers/kern_util.hpp b/Lilu/Headers/kern_util.hpp index f10ffd43..0b5aaa1c 100644 --- a/Lilu/Headers/kern_util.hpp +++ b/Lilu/Headers/kern_util.hpp @@ -83,7 +83,8 @@ enum KernelVersion { Mavericks = 13, Yosemite = 14, ElCapitan = 15, - Sierra = 16 + Sierra = 16, + HighSierra = 17 }; /** diff --git a/Lilu/Sources/kern_user.cpp b/Lilu/Sources/kern_user.cpp index 2558cad9..738c478d 100644 --- a/Lilu/Sources/kern_user.cpp +++ b/Lilu/Sources/kern_user.cpp @@ -460,36 +460,44 @@ bool UserPatcher::loadDyldSharedCacheMapping() { } bool res {false}; - auto entries = Buffer::create(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(buffer), entries, binaryModSize); - - if (nEntries > 0) { - DBGLOG("user @ mapped %zu entries out of %zu", nEntries, binaryModSize); - + + if (buffer && bufferSize > 0) { + auto entries = Buffer::create(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(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; } @@ -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( - patcher->routeFunction(kern, reinterpret_cast(codeSignValidatePageWrapper), true, true) + orgCodeSignValidateRangeWrapper = reinterpret_cast( + patcher->routeFunction(kern, reinterpret_cast(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( - patcher->routeFunction(kern, reinterpret_cast(codeSignValidateRangeWrapper), true, true) + orgCodeSignValidatePageWrapper = reinterpret_cast( + patcher->routeFunction(kern, reinterpret_cast(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; }