Skip to content

Commit

Permalink
o Fix issue with exception handling
Browse files Browse the repository at this point in the history
o Fix alignment issues with entitlment parsing
  • Loading branch information
Mike Miller committed Jan 2, 2024
1 parent 673f710 commit acf9afa
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 14 deletions.
25 changes: 15 additions & 10 deletions app/AppGroup.m
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,22 @@
#define CSMAGIC_EMBEDDED_SIGNATURE 0xfade0cc0
#define CSMAGIC_EMBEDDED_ENTITLEMENTS 0xfade7171

struct cs_blob_index {
struct __attribute__((packed)) cs_blob_index {
uint32_t type;
uint32_t offset;
};

struct cs_superblob {
struct __attribute__((packed)) cs_superblob {
uint32_t magic;
uint32_t length;
uint32_t count;
struct cs_blob_index index[];
struct cs_blob_index index[]; // This must be handled carefully since it's a flexible array member.
};

struct cs_entitlements {
struct __attribute__((packed)) cs_entitlements {
uint32_t magic;
uint32_t length;
char entitlements[];
char entitlements[]; // This must be handled carefully since it's a flexible array member.
};

static NSDictionary *AppEntitlements(void) {
Expand Down Expand Up @@ -78,17 +78,22 @@

NSData *entitlementsData = nil;
for (uint32_t i = 0; i < ntohl(cs->count); i++) {
struct cs_entitlements *ents = (void *) ((char *) cs + ntohl(cs->index[i].offset));
uint32_t offset = ntohl(cs->index[i].offset);
const struct cs_entitlements *ents = (const struct cs_entitlements *)((const char *)cs + offset);

// Read the magic number in a way that does not assume alignment
uint32_t magic;
memcpy(&magic, &ents->magic, sizeof(uint32_t));
if (ntohl(ents->magic) == CSMAGIC_EMBEDDED_ENTITLEMENTS) {
entitlementsData = [NSData dataWithBytes:ents->entitlements length:ntohl(ents->length) - offsetof(struct cs_entitlements, entitlements)];
magic = ntohl(magic);

if (magic == CSMAGIC_EMBEDDED_ENTITLEMENTS) {
uint32_t length;
memcpy(&length, &ents->length, sizeof(uint32_t));
length = ntohl(length);

entitlementsData = [NSData dataWithBytes:ents->entitlements length:length - offsetof(struct cs_entitlements, entitlements)];
break; // Entitlements found
}
}

if (entitlementsData == nil)
return;

Expand Down
1 change: 0 additions & 1 deletion app/main.m
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ int main(int argc, char * argv[]) {

int retVal = 0;
@try {
// Your existing setup code here
retVal = UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
}
@catch (NSException *exception) {
Expand Down
23 changes: 21 additions & 2 deletions iSH-AOK.xcodeproj/xcshareddata/xcschemes/iSH.xcscheme
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,7 @@
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
enableASanStackUseAfterReturn = "YES"
disableMainThreadChecker = "YES"
disablePerformanceAntipatternChecker = "YES"
enableUBSanitizer = "YES"
launchStyle = "0"
useCustomWorkingDirectory = "NO"
ignoresPersistentStateOnLaunch = "NO"
Expand All @@ -114,6 +113,26 @@
</BuildableReference>
</BuildableProductRunnable>
<AdditionalOptions>
<AdditionalOption
key = "MallocStackLogging"
value = ""
isEnabled = "YES">
</AdditionalOption>
<AdditionalOption
key = "DYLD_INSERT_LIBRARIES"
value = "/usr/lib/libgmalloc.dylib"
isEnabled = "YES">
</AdditionalOption>
<AdditionalOption
key = "PrefersMallocStackLoggingLite"
value = ""
isEnabled = "YES">
</AdditionalOption>
<AdditionalOption
key = "NSZombieEnabled"
value = "YES"
isEnabled = "YES">
</AdditionalOption>
<AdditionalOption
key = "MallocGuardEdges"
value = ""
Expand Down
2 changes: 1 addition & 1 deletion main.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ static void handler(int signo, siginfo_t *sigaction, void *context) {

static void gen_exception(void) {
printk("WARNING: gen_exception in.\n");
*(int *)0 = 0;
*(volatile int *)0 = 0;
printk("WARNING: gen_exception out.\n");
}

Expand Down

0 comments on commit acf9afa

Please sign in to comment.