From 1ca233eadf1139eea159585f90f26a116150d727 Mon Sep 17 00:00:00 2001 From: Zorg Date: Sat, 30 Jul 2016 17:36:22 -0400 Subject: [PATCH] Validate the update before extraction whenever possible If we download a package type installation or a delta update, we should validate the update before trying to unarchive it. We can't do this with ordinary updates because we handle changing of DSA keys for that particular case. --- Autoupdate/AppInstaller.m | 50 ++++++++++++++++--- Autoupdate/SUBinaryDeltaUnarchiver.m | 5 ++ Autoupdate/SUDiskImageUnarchiver.m | 5 ++ Autoupdate/SUInstallerValidation.h | 17 +++++-- Autoupdate/SUInstallerValidation.m | 72 ++++++++++++++++------------ Autoupdate/SUPipedUnarchiver.m | 5 ++ Autoupdate/SUUnarchiverProtocol.h | 2 + 7 files changed, 115 insertions(+), 41 deletions(-) diff --git a/Autoupdate/AppInstaller.m b/Autoupdate/AppInstaller.m index 21245298..3ded2aa9 100644 --- a/Autoupdate/AppInstaller.m +++ b/Autoupdate/AppInstaller.m @@ -68,6 +68,7 @@ static const NSTimeInterval SUDisplayProgressTimeDelay = 0.7; @property (nonatomic) BOOL receivedInstallationData; @property (nonatomic) BOOL finishedValidation; @property (nonatomic) BOOL agentInitiatedConnection; +@property (nonatomic) BOOL validatedArchiveBeforeExtraction; @property (nonatomic) dispatch_queue_t installerQueue; @property (nonatomic) BOOL performedStage1Installation; @@ -95,6 +96,7 @@ static const NSTimeInterval SUDisplayProgressTimeDelay = 0.7; @synthesize shouldRelaunch = _shouldRelaunch; @synthesize shouldShowUI = _shouldShowUI; @synthesize installer = _installer; +@synthesize validatedArchiveBeforeExtraction = _validatedArchiveBeforeExtraction; @synthesize willCompleteInstallation = _willCompleteInstallation; @synthesize receivedInstallationData = _receivedInstallationData; @synthesize installerQueue = _installerQueue; @@ -183,13 +185,25 @@ static const NSTimeInterval SUDisplayProgressTimeDelay = 0.7; { [self.communicator handleMessageWithIdentifier:SUExtractionStarted data:[NSData data]]; - NSString *downloadPath = [self.updateDirectoryPath stringByAppendingPathComponent:self.downloadName]; - id unarchiver = [SUUnarchiver unarchiverForPath:downloadPath updatingHostBundlePath:self.host.bundlePath decryptionPassword:self.decryptionPassword delegate:self]; + NSString *archivePath = [self.updateDirectoryPath stringByAppendingPathComponent:self.downloadName]; + id unarchiver = [SUUnarchiver unarchiverForPath:archivePath updatingHostBundlePath:self.host.bundlePath decryptionPassword:self.decryptionPassword delegate:self]; if (!unarchiver) { - SULog(@"Error: No valid unarchiver for %@!", downloadPath); + SULog(@"Error: No valid unarchiver for %@", archivePath); [self unarchiverDidFail]; } else { - [unarchiver start]; + // Delta & package updates will require validation before extraction + // Normal application updates are a bit more lenient allowing developers to change one of apple dev ID or DSA keys + if ([[unarchiver class] requiresValidationBeforeUnarchiving] || ![self.installationType isEqualToString:SUInstallationTypeApplication]) { + if ([SUInstallerValidation validateUpdateForHost:self.host archivePath:archivePath DSASignature:self.dsaSignature]) { + self.validatedArchiveBeforeExtraction = YES; + [unarchiver start]; + } else { + SULog(@"Error: Archive could not be validated for %@", archivePath); + [self unarchiverDidFail]; + } + } else { + [unarchiver start]; + } } } @@ -205,6 +219,9 @@ static const NSTimeInterval SUDisplayProgressTimeDelay = 0.7; - (void)unarchiverDidFail { + // Clear this in case we try another extraction attempt (eg: if it failed once from a delta update) + self.validatedArchiveBeforeExtraction = NO; + // Client could try update again with different inputs // Eg: one common case is if a delta update fails, client may want to fall back to regular update // We really only need to set updateDirectoryPath to nil since that's the field we check if we've received installation data, @@ -223,8 +240,29 @@ static const NSTimeInterval SUDisplayProgressTimeDelay = 0.7; { [self.communicator handleMessageWithIdentifier:SUValidationStarted data:[NSData data]]; - NSString *downloadPath = [self.updateDirectoryPath stringByAppendingPathComponent:self.downloadName]; - BOOL validationSuccess = [SUInstallerValidation validateUpdateForHost:self.host downloadedToPath:downloadPath extractedToPath:self.updateDirectoryPath DSASignature:self.dsaSignature]; + NSString *archivePath = [self.updateDirectoryPath stringByAppendingPathComponent:self.downloadName]; + + BOOL isPackage = NO; + // The path to the new bundle or package + NSString *installSourcePath = [SUInstaller installSourcePathInUpdateFolder:self.updateDirectoryPath forHost:self.host isPackage:&isPackage isGuided:NULL]; + if (installSourcePath == nil) { + SULog(@"No suitable install is found in the update. The update will be rejected."); + [self cleanupAndExitWithStatus:EXIT_FAILURE]; + } + + BOOL validationSuccess = YES; + if (!self.validatedArchiveBeforeExtraction) { + if (isPackage) { + // If we get here, then the appcast installation type was lying to us.. This error will be caught later when starting the installer. + validationSuccess = [SUInstallerValidation validateUpdateForHost:self.host archivePath:archivePath DSASignature:self.dsaSignature]; + } else { + // Validate the bundle code signatures and DSA signatures of archive together + validationSuccess = [SUInstallerValidation validateBundleUpdateForHost:self.host newBundlePath:installSourcePath archivePath:archivePath DSASignature:self.dsaSignature]; + } + } else if (!isPackage) { + // Just make sure the bundle is valid to check that the developer didn't make a careless mistake + validationSuccess = [SUInstallerValidation validateCodeSignatureIfAvailableForBundlePath:installSourcePath]; + } if (!validationSuccess) { SULog(@"Error: update validation was a failure"); diff --git a/Autoupdate/SUBinaryDeltaUnarchiver.m b/Autoupdate/SUBinaryDeltaUnarchiver.m index 9bda5581..bb9f1f93 100644 --- a/Autoupdate/SUBinaryDeltaUnarchiver.m +++ b/Autoupdate/SUBinaryDeltaUnarchiver.m @@ -35,6 +35,11 @@ return [[path pathExtension] isEqualToString:@"delta"]; } ++ (BOOL)requiresValidationBeforeUnarchiving +{ + return YES; +} + - (instancetype)initWithArchivePath:(NSString *)archivePath updateHostBundlePath:(NSString *)updateHostBundlePath delegate:(id )delegate { self = [super init]; diff --git a/Autoupdate/SUDiskImageUnarchiver.m b/Autoupdate/SUDiskImageUnarchiver.m index e0cfdb44..d02e90d4 100644 --- a/Autoupdate/SUDiskImageUnarchiver.m +++ b/Autoupdate/SUDiskImageUnarchiver.m @@ -32,6 +32,11 @@ return [[path pathExtension] isEqualToString:@"dmg"]; } ++ (BOOL)requiresValidationBeforeUnarchiving +{ + return NO; +} + - (instancetype)initWithArchivePath:(NSString *)archivePath decryptionPassword:(nullable NSString *)decryptionPassword delegate:(nullable id )delegate { self = [super init]; diff --git a/Autoupdate/SUInstallerValidation.h b/Autoupdate/SUInstallerValidation.h index 33e4d242..18e3e0fd 100644 --- a/Autoupdate/SUInstallerValidation.h +++ b/Autoupdate/SUInstallerValidation.h @@ -13,13 +13,22 @@ @interface SUInstallerValidation : NSObject /** - * If the update is a package, then the download must be signed using DSA. No other verification is done. - * * If the update is a bundle, then the download must also be signed using DSA. * However, a change of DSA public keys is allowed if the Apple Code Signing identities match and are valid. * Likewise, a change of Apple Code Signing identities is allowed if the DSA public keys match and the update is valid. - * */ -+ (BOOL)validateUpdateForHost:(SUHost *)host downloadedToPath:(NSString *)downloadedPath extractedToPath:(NSString *)extractedPath DSASignature:(NSString *)DSASignature; ++ (BOOL)validateBundleUpdateForHost:(SUHost *)host newBundlePath:(NSString *)newBundlePath archivePath:(NSString *)archivePath DSASignature:(NSString *)DSASignature; + +/* + * Use this for validating updates by validating the DSA signature using the host's public DSA key + */ ++ (BOOL)validateUpdateForHost:(SUHost *)host archivePath:(NSString *)archivePath DSASignature:(NSString *)DSASignature; + +/* + * Use this for validating a code signature for a bundle. + * This is simply a consistency check rather than a security one. + * This will return YES if the code signature is valid on the bundle, or if the bundle is not code signed at all, otherwise returns NO. + */ ++ (BOOL)validateCodeSignatureIfAvailableForBundlePath:(NSString *)bundlePath; @end diff --git a/Autoupdate/SUInstallerValidation.m b/Autoupdate/SUInstallerValidation.m index 81b1c471..1446cc59 100644 --- a/Autoupdate/SUInstallerValidation.m +++ b/Autoupdate/SUInstallerValidation.m @@ -8,40 +8,15 @@ #import "SUInstallerValidation.h" #import "SUHost.h" -#import "SUInstaller.h" #import "SULog.h" #import "SUDSAVerifier.h" #import "SUCodeSigningVerifier.h" @implementation SUInstallerValidation -+ (BOOL)validateUpdateForHost:(SUHost *)host downloadedToPath:(NSString *)downloadedPath extractedToPath:(NSString *)extractedPath DSASignature:(NSString *)DSASignature ++ (BOOL)validateBundleUpdateForHost:(SUHost *)host newBundlePath:(NSString *)newBundlePath archivePath:(NSString *)archivePath DSASignature:(NSString *)DSASignature { - BOOL isPackage = NO; - NSString *installSourcePath = [SUInstaller installSourcePathInUpdateFolder:extractedPath forHost:host isPackage:&isPackage isGuided:NULL]; - if (installSourcePath == nil) { - SULog(@"No suitable install is found in the update. The update will be rejected."); - return NO; - } - - NSString *publicDSAKey = host.publicDSAKey; - - // Modern packages are not distributed as bundles and are code signed differently than regular applications - if (isPackage) { - if (nil == publicDSAKey) { - SULog(@"The existing app bundle does not have a DSA key, so it can't verify installer packages."); - } - - BOOL packageValidated = [SUDSAVerifier validatePath:downloadedPath withEncodedDSASignature:DSASignature withPublicDSAKey:publicDSAKey]; - - if (!packageValidated) { - SULog(@"DSA signature validation of the package failed. The update contains an installer package, and valid DSA signatures are mandatory for all installer packages. The update will be rejected. Sign the installer with a valid DSA key or use an .app bundle update instead."); - } - - return packageValidated; - } - - NSBundle *newBundle = [NSBundle bundleWithPath:installSourcePath]; + NSBundle *newBundle = [NSBundle bundleWithPath:newBundlePath]; if (newBundle == nil) { SULog(@"No suitable bundle is found in the update. The update will be rejected."); return NO; @@ -55,22 +30,24 @@ return NO; } + NSString *publicDSAKey = host.publicDSAKey; + BOOL dsaKeysMatch = (publicDSAKey == nil) ? NO : [publicDSAKey isEqualToString:newPublicDSAKey]; // If the new DSA key differs from the old, then this check is not a security measure, because the new key is not trusted. // In that case, the check ensures that the app author has correctly used DSA keys, so that the app will be updateable in the next version. // However if the new and old DSA keys are the same, then this is a security measure. - if (![SUDSAVerifier validatePath:downloadedPath withEncodedDSASignature:DSASignature withPublicDSAKey:newPublicDSAKey]) { + if (![SUDSAVerifier validatePath:archivePath withEncodedDSASignature:DSASignature withPublicDSAKey:newPublicDSAKey]) { SULog(@"DSA signature validation failed. The update has a public DSA key and is signed with a DSA key, but the %@ doesn't match the signature. The update will be rejected.", dsaKeysMatch ? @"public key" : @"new public key shipped with the update"); return NO; } - BOOL updateIsCodeSigned = [SUCodeSigningVerifier bundleAtPathIsCodeSigned:installSourcePath]; + BOOL updateIsCodeSigned = [SUCodeSigningVerifier bundleAtPathIsCodeSigned:newBundlePath]; if (dsaKeysMatch) { NSError *error = nil; - if (updateIsCodeSigned && ![SUCodeSigningVerifier codeSignatureIsValidAtPath:installSourcePath error:&error]) { + if (updateIsCodeSigned && ![SUCodeSigningVerifier codeSignatureIsValidAtPath:newBundlePath error:&error]) { SULog(@"The update archive has a valid DSA signature, but the app is also signed with Code Signing, which is corrupted: %@. The update will be rejected.", error); return NO; } @@ -86,7 +63,7 @@ } NSError *error = nil; - if (![SUCodeSigningVerifier codeSignatureAtPath:hostBundlePath matchesSignatureAtPath:installSourcePath error:&error]) { + if (![SUCodeSigningVerifier codeSignatureAtPath:hostBundlePath matchesSignatureAtPath:newBundlePath error:&error]) { SULog(@"The update archive %@, and the app is signed with a new Code Signing identity that doesn't match code signing of the original app: %@. At least one method of signature verification must be valid. The update will be rejected.", dsaStatus, error); return NO; } @@ -95,4 +72,37 @@ return YES; } ++ (BOOL)validateUpdateForHost:(SUHost *)host archivePath:(NSString *)archivePath DSASignature:(NSString *)DSASignature +{ + NSString *publicDSAKey = host.publicDSAKey; + if (publicDSAKey ==nil){ + SULog(@"Failed to validate delta update because no DSA key was found"); + return NO; + } + + if (DSASignature == nil) { + SULog(@"Failed to validate delta update because no DSA signature was found"); + return NO; + } + + if (![SUDSAVerifier validatePath:archivePath withEncodedDSASignature:DSASignature withPublicDSAKey:publicDSAKey]) { + SULog(@"DSA signature validation failed for delta update. This update will be skipped."); + return NO; + } + + return YES; +} + ++ (BOOL)validateCodeSignatureIfAvailableForBundlePath:(NSString *)bundlePath +{ + BOOL updateIsCodeSigned = [SUCodeSigningVerifier bundleAtPathIsCodeSigned:bundlePath]; + + NSError *error = nil; + if (updateIsCodeSigned && ![SUCodeSigningVerifier codeSignatureIsValidAtPath:bundlePath error:&error]) { + SULog(@"Failed to validate code sign signature on bundle at %@", bundlePath); + return NO; + } + return YES; +} + @end diff --git a/Autoupdate/SUPipedUnarchiver.m b/Autoupdate/SUPipedUnarchiver.m index da6f1d5d..d3bac75e 100644 --- a/Autoupdate/SUPipedUnarchiver.m +++ b/Autoupdate/SUPipedUnarchiver.m @@ -63,6 +63,11 @@ return ([self commandAndArgumentsConformingToTypeOfPath:path] != nil); } ++ (BOOL)requiresValidationBeforeUnarchiving +{ + return NO; +} + - (instancetype)initWithArchivePath:(NSString *)archivePath delegate:(nullable id )delegate { self = [super init]; diff --git a/Autoupdate/SUUnarchiverProtocol.h b/Autoupdate/SUUnarchiverProtocol.h index 8bcfd520..27bd42ad 100644 --- a/Autoupdate/SUUnarchiverProtocol.h +++ b/Autoupdate/SUUnarchiverProtocol.h @@ -14,6 +14,8 @@ NS_ASSUME_NONNULL_BEGIN + (BOOL)canUnarchivePath:(NSString *)path; ++ (BOOL)requiresValidationBeforeUnarchiving; + - (void)start; - (NSString *)description;