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.
This commit is contained in:
Zorg committed 2016-07-30 17:36:22 -04:00
1 parent 2fc57ce288
commit 1ca233eadf
7 files changed
+115 -41

No files matched your search

+44 -6
View File
@@ -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 <SUUnarchiverProtocol> unarchiver = [SUUnarchiver unarchiverForPath:downloadPath updatingHostBundlePath:self.host.bundlePath decryptionPassword:self.decryptionPassword delegate:self];
NSString *archivePath = [self.updateDirectoryPath stringByAppendingPathComponent:self.downloadName];
id<SUUnarchiverProtocol> 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");
+5
View File
@@ -35,6 +35,11 @@
return [[path pathExtension] isEqualToString:@"delta"];
}
+ (BOOL)requiresValidationBeforeUnarchiving
{
return YES;
}
- (instancetype)initWithArchivePath:(NSString *)archivePath updateHostBundlePath:(NSString *)updateHostBundlePath delegate:(id <SUUnarchiverDelegate>)delegate
{
self = [super init];
+5
View File
@@ -32,6 +32,11 @@
return [[path pathExtension] isEqualToString:@"dmg"];
}
+ (BOOL)requiresValidationBeforeUnarchiving
{
return NO;
}
- (instancetype)initWithArchivePath:(NSString *)archivePath decryptionPassword:(nullable NSString *)decryptionPassword delegate:(nullable id <SUUnarchiverDelegate>)delegate
{
self = [super init];
+13 -4
View File
@@ -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
+41 -31
View File
@@ -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
+5
View File
@@ -63,6 +63,11 @@
return ([self commandAndArgumentsConformingToTypeOfPath:path] != nil);
}
+ (BOOL)requiresValidationBeforeUnarchiving
{
return NO;
}
- (instancetype)initWithArchivePath:(NSString *)archivePath delegate:(nullable id <SUUnarchiverDelegate>)delegate
{
self = [super init];
+2
View File
@@ -14,6 +14,8 @@ NS_ASSUME_NONNULL_BEGIN
+ (BOOL)canUnarchivePath:(NSString *)path;
+ (BOOL)requiresValidationBeforeUnarchiving;
- (void)start;
- (NSString *)description;