From 7e03d202951010b1dbb4a2b3d29c1b7cfb59741e Mon Sep 17 00:00:00 2001 From: opa334 Date: Sat, 4 May 2024 16:17:21 +0200 Subject: [PATCH] Recursively fix /var/jb/var/mobile permissions on userspace reboots and rejailbreaks --- .../Dopamine/Jailbreak/DOBootstrapper.m | 14 +--------- BaseBin/launchdhook/src/update.m | 2 ++ BaseBin/libjailbreak/src/util.h | 2 ++ BaseBin/libjailbreak/src/util.m | 27 +++++++++++++++++++ 4 files changed, 32 insertions(+), 13 deletions(-) diff --git a/Application/Dopamine/Jailbreak/DOBootstrapper.m b/Application/Dopamine/Jailbreak/DOBootstrapper.m index 4b484dac9..e4790590d 100644 --- a/Application/Dopamine/Jailbreak/DOBootstrapper.m +++ b/Application/Dopamine/Jailbreak/DOBootstrapper.m @@ -525,19 +525,7 @@ - (void)prepareBootstrapWithCompletion:(void (^)(NSError *))completion [[NSFileManager defaultManager] createDirectoryAtPath:mobilePreferencesPath withIntermediateDirectories:YES attributes:attributes error:nil]; } - // Dopamine 2.0 - 2.0.4 would bootstrap with wrong permissions - // Try to detect and fix it - NSString *mobilePath = NSJBRootPath(@"/var/mobile"); - struct stat s; - stat(mobilePath.fileSystemRepresentation, &s); - if (s.st_uid != 501 || s.st_gid != 501) { - chown(mobilePath.fileSystemRepresentation, 501, 501); - NSURL *mobileURL = [NSURL fileURLWithPath:mobilePath]; - NSDirectoryEnumerator *enumerator = [[NSFileManager defaultManager] enumeratorAtURL:mobileURL includingPropertiesForKeys:nil options:0 errorHandler:nil]; - for (NSURL *fileURL in enumerator) { - chown(fileURL.fileSystemRepresentation, 501, 501); - } - } + JBFixMobilePermissions(); completion(nil); }; diff --git a/BaseBin/launchdhook/src/update.m b/BaseBin/launchdhook/src/update.m index 3f4b9322e..f5d42964b 100644 --- a/BaseBin/launchdhook/src/update.m +++ b/BaseBin/launchdhook/src/update.m @@ -202,4 +202,6 @@ void jbupdate_finalize_stage2(const char *prevVersion, const char *newVersion) arm64_kcall_init(); #endif } + + JBFixMobilePermissions(); } \ No newline at end of file diff --git a/BaseBin/libjailbreak/src/util.h b/BaseBin/libjailbreak/src/util.h index 02f48fc9f..12677d16b 100644 --- a/BaseBin/libjailbreak/src/util.h +++ b/BaseBin/libjailbreak/src/util.h @@ -80,4 +80,6 @@ NSString *NSJBRootPath(NSString *relativePath); NSString *NSPrebootUUIDPath(NSString *relativePath); #endif +void JBFixMobilePermissions(void); + #endif \ No newline at end of file diff --git a/BaseBin/libjailbreak/src/util.m b/BaseBin/libjailbreak/src/util.m index d637bb6bd..f4eeadac4 100644 --- a/BaseBin/libjailbreak/src/util.m +++ b/BaseBin/libjailbreak/src/util.m @@ -14,4 +14,31 @@ @autoreleasepool { return [NSString stringWithUTF8String:prebootUUIDPath(relativePath.UTF8String)]; } +} + +void JBFixMobilePermissions(void) +{ + @autoreleasepool { + struct stat s; + + // Anything in /var/mobile should owned by mobile... + // For some reason some packages seem to fuck this up, so we automatically fix it every userspace reboot and on rejailbreak + NSString *mobilePath = NSJBRootPath(@"/var/mobile"); + NSURL *mobileURL = [NSURL fileURLWithPath:mobilePath]; + + if (stat(mobileURL.fileSystemRepresentation, &s) == 0) { + if (s.st_uid != 501 || s.st_gid != 501) { + chown(mobileURL.fileSystemRepresentation, 501, 501); + } + } + + NSDirectoryEnumerator *enumerator = [[NSFileManager defaultManager] enumeratorAtURL:mobileURL includingPropertiesForKeys:nil options:0 errorHandler:nil]; + for (NSURL *fileURL in enumerator) { + if (stat(fileURL.fileSystemRepresentation, &s) == 0) { + if (s.st_uid != 501 || s.st_gid != 501) { + chown(fileURL.fileSystemRepresentation, 501, 501); + } + } + } + } } \ No newline at end of file