From acf758e48511c14c5ea0f68abfe1eb35b8f0afd8 Mon Sep 17 00:00:00 2001 From: Dmytro Tretiakov Date: Wed, 7 May 2014 12:43:28 +0300 Subject: [PATCH] Separate check of using XPC installer and XPC downloader. --- SUAppcast.m | 2 +- SUAutomaticUpdateDriver.m | 2 +- SUBasicUpdateDriver.m | 8 +-- SUCodeSigningVerifier.h | 4 ++ SUCodeSigningVerifier.m | 102 ++++++++++++++++++++++++++++++-------- SUUpdateAlert.m | 2 +- SUUpdater.m | 12 ++++- SUUpdater_Private.h | 3 +- 8 files changed, 106 insertions(+), 29 deletions(-) diff --git a/SUAppcast.m b/SUAppcast.m index 26d7c10b..795ae4d7 100644 --- a/SUAppcast.m +++ b/SUAppcast.m @@ -64,7 +64,7 @@ if ([delegate respondsToSelector:@selector(appcast:willFetchURLRequest:)]) [delegate appcast:self willFetchURLRequest:request]; - if ([SUUpdater shouldUseXPC]) + if ([SUUpdater shouldUseXPCDownloader]) { download = (NSURLDownload *)[[SUXPCURLDownload alloc] initWithRequest:request delegate:self]; } diff --git a/SUAutomaticUpdateDriver.m b/SUAutomaticUpdateDriver.m index 5d6bb3c0..ee264b87 100644 --- a/SUAutomaticUpdateDriver.m +++ b/SUAutomaticUpdateDriver.m @@ -35,7 +35,7 @@ static NSString * const kSUAutomaticUpdateParamName = @"autoupdate"; [request setCachePolicy:NSURLRequestReloadIgnoringCacheData]; [self updateURLRequestIfNeeds:request]; - if ([SUUpdater shouldUseXPC]) + if ([SUUpdater shouldUseXPCDownloader]) { [SUXPCURLDownload sendSynchronousRequest:request delegate:nil]; } diff --git a/SUBasicUpdateDriver.m b/SUBasicUpdateDriver.m index b93faf4e..3b2a79d2 100644 --- a/SUBasicUpdateDriver.m +++ b/SUBasicUpdateDriver.m @@ -202,7 +202,7 @@ NSArray *SUGetAllDevMateURLHosts(void) [request setValue:[updater userAgentString] forHTTPHeaderField:@"User-Agent"]; [self updateURLRequestIfNeeds:request]; - if ([SUUpdater shouldUseXPC]) + if ([SUUpdater shouldUseXPCDownloader]) download = (NSURLDownload *)[[SUXPCURLDownload alloc] initWithRequest:request delegate:self]; else download = [[NSURLDownload alloc] initWithRequest:request delegate:self]; @@ -210,7 +210,7 @@ NSArray *SUGetAllDevMateURLHosts(void) - (void)download:(NSURLDownload *)d decideDestinationWithSuggestedFilename:(NSString *)name { - if ([SUUpdater shouldUseXPC]) { + if ([SUUpdater shouldUseXPCDownloader]) { // The downloader will determine a file name, somewhere within our sandbox. downloadPath = nil; [d setDestination:name allowOverwrite:YES]; @@ -387,7 +387,7 @@ NSArray *SUGetAllDevMateURLHosts(void) #endif // Only the paranoid survive: if there's already a stray copy of relaunch there, we would have problems. - if( [SUUpdater shouldUseXPC] ) + if( [SUUpdater shouldUseXPCInstaller] ) { [SUXPCInstaller copyPathWithAuthentication:relaunchPathToCopy overPath:targetPath temporaryName:nil completionHandler:^(NSError *xpcError) { if (xpcError != nil) @@ -428,7 +428,7 @@ NSArray *SUGetAllDevMateURLHosts(void) pathToRelaunch = [[updater delegate] pathToRelaunchForUpdater:updater]; NSString *relaunchToolPath = [relaunchPath stringByAppendingPathComponent: @"/Contents/MacOS/finish_installation"]; NSArray *arguments = [NSArray arrayWithObjects:[host bundlePath], pathToRelaunch, [NSString stringWithFormat:@"%d", [[NSProcessInfo processInfo] processIdentifier]], tempDir, relaunch ? @"1" : @"0", nil]; - if( [SUUpdater shouldUseXPC] ) + if( [SUUpdater shouldUseXPCInstaller] ) [SUXPCInstaller launchTaskWithLaunchPath: relaunchToolPath arguments:arguments completionHandler:^{ [NSApp terminate:self]; }]; diff --git a/SUCodeSigningVerifier.h b/SUCodeSigningVerifier.h index 637355bc..e8e885e3 100644 --- a/SUCodeSigningVerifier.h +++ b/SUCodeSigningVerifier.h @@ -12,9 +12,13 @@ #import @interface SUCodeSigningVerifier : NSObject + + (BOOL)codeSignatureIsValidAtPath:(NSString *)destinationPath error:(NSError **)error; + + (BOOL)hostApplicationIsCodeSigned; + (BOOL)hostApplicationIsSandboxed; ++ (BOOL)hostAppAllowsNetworkOutgoingConnections; + @end #endif diff --git a/SUCodeSigningVerifier.m b/SUCodeSigningVerifier.m index e2a6374f..78be9ded 100644 --- a/SUCodeSigningVerifier.m +++ b/SUCodeSigningVerifier.m @@ -66,29 +66,47 @@ finally: + (BOOL)hostApplicationIsCodeSigned { - // This API didn't exist prior to 10.6. - if (SecCodeCopySelf == NULL) return NO; + static BOOL sIsCodesigned = NO; - OSStatus result; - SecCodeRef hostCode = NULL; - result = SecCodeCopySelf(kSecCSDefaultFlags, &hostCode); - if (result != 0) return NO; + static BOOL onceToken = NO; + while (!onceToken) + { + onceToken = YES; + + // This API didn't exist prior to 10.6. + if (SecCodeCopySelf == NULL) + break; - SecRequirementRef requirement = NULL; - result = SecCodeCopyDesignatedRequirement(hostCode, kSecCSDefaultFlags, &requirement); - if (hostCode) CFRelease(hostCode); - if (requirement) CFRelease(requirement); - return (result == 0); + SecCodeRef hostCode = NULL; + OSStatus result = SecCodeCopySelf(kSecCSDefaultFlags, &hostCode); + if (result != noErr) + break; + + SecRequirementRef requirement = NULL; + result = SecCodeCopyDesignatedRequirement(hostCode, kSecCSDefaultFlags, &requirement); + + if (hostCode) CFRelease(hostCode); + if (requirement) CFRelease(requirement); + + sIsCodesigned = (noErr == result); + } + + return sIsCodesigned; } + (BOOL)hostApplicationIsSandboxed { - // This API didn't exist prior to 10.6 - if (SecCodeCopySelf == NULL) return NO; + static BOOL sIsAppSandboxed = NO; - BOOL isSandboxed = NO; - do + static BOOL onceToken = NO; + while (!onceToken) { + onceToken = YES; + + // This API didn't exist prior to 10.6 + if (SecCodeCopySelf == NULL) + break; + SecCodeRef hostCode = NULL; SecRequirementRef hostRequirement = NULL; @@ -116,14 +134,58 @@ finally: } status = SecCodeCheckValidity(hostCode, kSecCSDefaultFlags, hostRequirement); - if (noErr == status) - isSandboxed = YES; - + sIsAppSandboxed = (noErr == status); Cleanup(); } - while (NO); - return isSandboxed; + return sIsAppSandboxed; +} + ++ (BOOL)hostAppAllowsNetworkOutgoingConnections +{ + static BOOL sIsAppAllowsOutgoingConnections = NO; + + static BOOL onceToken = NO; + while (!onceToken) + { + onceToken = YES; + + if (![self hostApplicationIsSandboxed]) + { + sIsAppAllowsOutgoingConnections = YES; + break; + } + + SecCodeRef hostCode = NULL; + SecRequirementRef hostRequirement = NULL; + + #define Cleanup() \ + { \ + if (hostCode) CFRelease(hostCode); \ + if (hostRequirement) CFRelease(hostRequirement); \ + } + + OSStatus status = SecCodeCopySelf(kSecCSDefaultFlags, &hostCode); + if (status != noErr || hostCode == NULL) + { + Cleanup(); + break; + } + + CFStringRef requirementString = CFSTR("entitlement[\"com.apple.security.network.client\"] exists"); + status = SecRequirementCreateWithString(requirementString, kSecCSDefaultFlags, &hostRequirement); + if (status != noErr || hostRequirement == NULL) + { + Cleanup(); + break; + } + + status = SecCodeCheckValidity(hostCode, kSecCSDefaultFlags, hostRequirement); + sIsAppAllowsOutgoingConnections = (noErr == status); + Cleanup(); + } + + return sIsAppAllowsOutgoingConnections; } @end diff --git a/SUUpdateAlert.m b/SUUpdateAlert.m index 514b1b39..59b4c9f3 100644 --- a/SUUpdateAlert.m +++ b/SUUpdateAlert.m @@ -127,7 +127,7 @@ [delegate updateAlert:self willLoadReleaseNotesWithRequest:request]; } - if ([SUUpdater shouldUseXPC]) + if ([SUUpdater shouldUseXPCDownloader]) { [releaseNotesDownloader release]; releaseNotesDownloader = (NSURLDownload *)[[SUXPCURLDownload alloc] initWithRequest:request delegate:self]; diff --git a/SUUpdater.m b/SUUpdater.m index c397270e..cee1c25b 100644 --- a/SUUpdater.m +++ b/SUUpdater.m @@ -554,7 +554,17 @@ static NSString * const SUUpdaterDefaultsObservationContext = @"SUUpdaterDefault - (id)delegate { return delegate; } - (NSBundle *)hostBundle { return [host bundle]; } -+ (BOOL)shouldUseXPC ++ (BOOL)shouldUseXPCDownloader +{ + if ([SUCodeSigningVerifier hostAppAllowsNetworkOutgoingConnections]) + return NO; + + NSString *xpcServicePrefixPath = [[[SUUpdater sharedUpdater] hostBundle] bundlePath]; + NSString *xpcServiceSuffixPath = @"Contents/XPCServices/com.andymatuschak.Sparkle.download-service.xpc"; + return [[NSFileManager defaultManager] fileExistsAtPath:[xpcServicePrefixPath stringByAppendingPathComponent:xpcServiceSuffixPath]]; +} + ++ (BOOL)shouldUseXPCInstaller { if (![SUCodeSigningVerifier hostApplicationIsSandboxed]) return NO; diff --git a/SUUpdater_Private.h b/SUUpdater_Private.h index 331563c3..84a431ca 100644 --- a/SUUpdater_Private.h +++ b/SUUpdater_Private.h @@ -12,6 +12,7 @@ - (BOOL)mayUpdateAndRestart; -+ (BOOL)shouldUseXPC; ++ (BOOL)shouldUseXPCDownloader; ++ (BOOL)shouldUseXPCInstaller; @end