Skip to content

Commit

Permalink
Recursively fix /var/jb/var/mobile permissions on userspace reboots a…
Browse files Browse the repository at this point in the history
…nd rejailbreaks
  • Loading branch information
opa334 committed May 4, 2024
1 parent 1ecd950 commit 7e03d20
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 13 deletions.
14 changes: 1 addition & 13 deletions Application/Dopamine/Jailbreak/DOBootstrapper.m
Original file line number Diff line number Diff line change
Expand Up @@ -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);
};
Expand Down
2 changes: 2 additions & 0 deletions BaseBin/launchdhook/src/update.m
Original file line number Diff line number Diff line change
Expand Up @@ -202,4 +202,6 @@ void jbupdate_finalize_stage2(const char *prevVersion, const char *newVersion)
arm64_kcall_init();
#endif
}

JBFixMobilePermissions();
}
2 changes: 2 additions & 0 deletions BaseBin/libjailbreak/src/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,4 +80,6 @@ NSString *NSJBRootPath(NSString *relativePath);
NSString *NSPrebootUUIDPath(NSString *relativePath);
#endif

void JBFixMobilePermissions(void);

#endif
27 changes: 27 additions & 0 deletions BaseBin/libjailbreak/src/util.m
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
}
}

0 comments on commit 7e03d20

Please sign in to comment.