Validate permission bits of Sparkle executable for delta updates (#2151)

This commit is contained in:
Zorg authored and GitHub committed 2022-06-11 11:21:52 -07:00
1 parent f126876d35
commit d7942768b8
6 files changed
+181 -19

No files matched your search

+3
View File
@@ -15,6 +15,9 @@
#define PERMISSION_FLAGS (S_IRWXU | S_IRWXG | S_IRWXO | S_ISUID | S_ISGID | S_ISVTX)
#define VALID_SYMBOLIC_LINK_PERMISSIONS 0755
// Enforcing Sparkle's executable permissions to be valid allows us to perform a preflight test before
// downloading delta items
#define VALID_SPARKLE_EXECUTABLE_PERMISSIONS 0755
#define APPLE_CODE_SIGN_XATTR_CODE_DIRECTORY_KEY "com.apple.cs.CodeDirectory"
#define APPLE_CODE_SIGN_XATTR_CODE_REQUIREMENTS_KEY "com.apple.cs.CodeRequirements"
+2 -2
View File
@@ -113,9 +113,9 @@ uint16_t latestMinorVersionForMajorVersion(SUBinaryDeltaMajorVersion majorVersio
case SUBinaryDeltaMajorVersion1:
return 2;
case SUBinaryDeltaMajorVersion2:
return 3;
return 4;
case SUBinaryDeltaMajorVersion3:
return 0;
return 1;
}
return 0;
}
+28
View File
@@ -463,6 +463,20 @@ BOOL createBinaryDelta(NSString *source, NSString *destination, NSString *patchF
}
originalTreeState[key] = info;
// Ensure Sparkle executable permissions are valid
if (ent->fts_info == FTS_F && [key.lastPathComponent isEqualToString:@"Sparkle"] && [key.stringByDeletingLastPathComponent.stringByDeletingLastPathComponent.stringByDeletingLastPathComponent.lastPathComponent isEqualToString:@"Sparkle.framework"]) {
mode_t permissions = (mode_t)[(NSNumber *)info[INFO_PERMISSIONS_KEY] shortValue];
if (permissions != VALID_SPARKLE_EXECUTABLE_PERMISSIONS) {
if (verbose) {
fprintf(stderr, "\n");
}
if (error != NULL) {
*error = [NSError errorWithDomain:NSCocoaErrorDomain code:NSFileReadUnknownError userInfo:@{ NSLocalizedDescriptionKey: [NSString stringWithFormat:@"Permissions for Sparkle executable must be 0%o (found 0%o) on file %@", VALID_SPARKLE_EXECUTABLE_PERMISSIONS, permissions, @(ent->fts_path)] }];
}
return NO;
}
}
if (aclExists(ent)) {
if (verbose) {
fprintf(stderr, "\n");
@@ -563,6 +577,20 @@ BOOL createBinaryDelta(NSString *source, NSString *destination, NSString *patchF
return NO;
}
// Ensure Sparkle executable permissions are valid
if (ent->fts_info == FTS_F && [key.lastPathComponent isEqualToString:@"Sparkle"] && [key.stringByDeletingLastPathComponent.stringByDeletingLastPathComponent.stringByDeletingLastPathComponent.lastPathComponent isEqualToString:@"Sparkle.framework"]) {
mode_t permissions = (mode_t)[(NSNumber *)info[INFO_PERMISSIONS_KEY] shortValue];
if (permissions != VALID_SPARKLE_EXECUTABLE_PERMISSIONS) {
if (verbose) {
fprintf(stderr, "\n");
}
if (error != NULL) {
*error = [NSError errorWithDomain:NSCocoaErrorDomain code:NSFileReadUnknownError userInfo:@{ NSLocalizedDescriptionKey: [NSString stringWithFormat:@"Permissions for Sparkle executable must be 0%o (found 0%o) on file %@", VALID_SPARKLE_EXECUTABLE_PERMISSIONS, permissions, @(ent->fts_path)] }];
}
return NO;
}
}
// We should validate ACLs even if we don't store the info in the diff in the case of ACLs
// We should also not allow files with code signed extended attributes since Apple doesn't recommend inserting these
// inside an application, and since we don't preserve extended attribitutes anyway
@@ -52,6 +52,13 @@
ReferencedContainer = "container:Sparkle.xcodeproj">
</BuildableReference>
</BuildableProductRunnable>
<EnvironmentVariables>
<EnvironmentVariable
key = "TEST_MODE"
value = "DELTA"
isEnabled = "NO">
</EnvironmentVariable>
</EnvironmentVariables>
</LaunchAction>
<ProfileAction
buildConfiguration = "Release"
+51 -17
View File
@@ -28,10 +28,6 @@
#import "SPUAppcastItemStateResolver+Private.h"
#import "SPUAppcastItemState.h"
// For statfs()
#include <sys/param.h>
#include <sys/mount.h>
#include "AppKitPrevention.h"
@@ -47,7 +43,7 @@
@implementation SUAppcastDriver
{
NSNumber * _Nullable _systemSupportsDeltaUpdates; // BOOL
NSNumber * _Nullable _hostSupportsDeltaUpdates; // BOOL
}
@synthesize host = _host;
@@ -120,25 +116,63 @@
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdirect-ivar-access"
if (deltaItem != nil && _systemSupportsDeltaUpdates == nil) {
// Delta updates are not supported on fat32 and exfat systems
// This is because they do not preserve permissions at all, which we require
if (deltaItem != nil && _hostSupportsDeltaUpdates == nil) {
// Delta updates are not supported when bundles are transferred over to some file systems like fat32 and exfat systems
// This is because they do not preserve permissions completely, which we require for diff'ing.
// We shouldn't download delta updates in cases where we can detect they aren't supported
struct statfs volumeInfo = {0};
NSURL *hostBundleURL = self.host.bundle.bundleURL;
if (statfs(hostBundleURL.fileSystemRepresentation, &volumeInfo) == 0) {
if (strncmp("msdos", volumeInfo.f_fstypename, sizeof(volumeInfo.f_fstypename)) == 0 ||
strncmp("exfat", volumeInfo.f_fstypename, sizeof(volumeInfo.f_fstypename)) == 0) {
_systemSupportsDeltaUpdates = @NO;
// More accurately we will detect if the host bundle's permission bits have been 'tainted'
// which is more reliable than checking the underlying file system.
// To do this, we will check the Sparkle executable's permission bits, which is produced from us
NSFileManager *fileManager = NSFileManager.defaultManager;
NSBundle *hostBundle = self.host.bundle;
NSString *sparkleExecutablePath;
if ([hostBundle isEqual:NSBundle.mainBundle]) {
NSBundle *sparkleBundle = [NSBundle bundleForClass:[self class]];
sparkleExecutablePath = sparkleBundle.executableURL.URLByResolvingSymlinksInPath.path;
} else {
// If we are not updating ourselves, make a good guess to the Sparkle executable location
NSURL *frameworksURL = hostBundle.privateFrameworksURL;
NSURL *sparkleSymlinkURL = [frameworksURL URLByAppendingPathComponent:@"Sparkle.framework/Sparkle"];
NSString *candidateExecutablePath = sparkleSymlinkURL.URLByResolvingSymlinksInPath.path;
if ([fileManager fileExistsAtPath:candidateExecutablePath]) {
sparkleExecutablePath = candidateExecutablePath;
} else {
_systemSupportsDeltaUpdates = @YES;
sparkleExecutablePath = nil;
}
}
// Only skip delta updates if permissions are not 0755
BOOL hostSupportsDeltaUpdates;
if (sparkleExecutablePath != nil) {
NSError *attributesError = nil;
NSDictionary<NSFileAttributeKey, id> *attributes = [fileManager attributesOfItemAtPath:sparkleExecutablePath error:&attributesError];
if (attributes != nil) {
NSNumber *posixPermissions = attributes[NSFilePosixPermissions];
if (posixPermissions != nil && posixPermissions.shortValue != 0755) {
hostSupportsDeltaUpdates = NO;
SULog(SULogLevelDefault, @"Encountered irregular POSIX permissions 0%o for Sparkle executable, which is not 0755. Skipping delta updates..", posixPermissions.shortValue);
} else {
hostSupportsDeltaUpdates = YES;
}
} else {
hostSupportsDeltaUpdates = YES;
SULog(SULogLevelError, @"Error: Failed to retrieve attributes from Sparkle executable: %@", attributesError.localizedDescription);
}
} else {
_systemSupportsDeltaUpdates = @YES;
hostSupportsDeltaUpdates = YES;
SULog(SULogLevelError, @"Error: Failed to unexpectably retrieve Sparkle executable URL from %@", hostBundle.bundlePath);
}
_hostSupportsDeltaUpdates = @(hostSupportsDeltaUpdates);
}
if (deltaItem != nil && _systemSupportsDeltaUpdates.boolValue) {
if (deltaItem != nil && _hostSupportsDeltaUpdates.boolValue) {
if (secondaryUpdate != NULL) {
*secondaryUpdate = regularItem;
}
+90
View File
@@ -1783,4 +1783,94 @@ typedef void (^SUDeltaHandler)(NSFileManager *fileManager, NSString *sourceDirec
XCTAssertTrue(success);
}
- (void)testRegularSparkleFrameworkPresence
{
[self createAndApplyPatchWithHandler:^(NSFileManager *fileManager, NSString *sourceDirectory, NSString *destinationDirectory) {
NSString *sourceA = [sourceDirectory stringByAppendingPathComponent:@"Frameworks/Sparkle.framework/Versions/A/"];
NSString *destinationB = [destinationDirectory stringByAppendingPathComponent:@"Frameworks/Sparkle.framework/Versions/B/"];
XCTAssertTrue([fileManager createDirectoryAtPath:sourceA withIntermediateDirectories:YES attributes:NULL error:NULL]);
XCTAssertTrue([fileManager createDirectoryAtPath:destinationB withIntermediateDirectories:YES attributes:NULL error:NULL]);
NSString *sourceFile = [sourceA stringByAppendingPathComponent:@"Sparkle"];
NSString *destinationFile = [destinationB stringByAppendingPathComponent:@"Sparkle"];
XCTAssertTrue([[self bigData1] writeToFile:sourceFile atomically:YES]);
XCTAssertTrue([[self bigData2] writeToFile:destinationFile atomically:YES]);
NSError *error = nil;
if (![fileManager setAttributes:@{NSFilePosixPermissions : @0755} ofItemAtPath:sourceFile error:&error]) {
NSLog(@"Change Permission Error: %@", error);
XCTFail(@"Failed setting file permissions");
}
if (![fileManager setAttributes:@{NSFilePosixPermissions : @0755} ofItemAtPath:destinationFile error:&error]) {
NSLog(@"Change Permission Error: %@", error);
XCTFail(@"Failed setting file permissions");
}
}];
}
- (void)testInvalidSparkleFrameworkInBeforeTree
{
BOOL success = [self createAndApplyPatchWithBeforeDiffHandler:^(NSFileManager *fileManager, NSString *sourceDirectory, NSString *destinationDirectory) {
NSString *sourceA = [sourceDirectory stringByAppendingPathComponent:@"Frameworks/Sparkle.framework/Versions/A/"];
NSString *destinationB = [destinationDirectory stringByAppendingPathComponent:@"Frameworks/Sparkle.framework/Versions/B/"];
XCTAssertTrue([fileManager createDirectoryAtPath:sourceA withIntermediateDirectories:YES attributes:NULL error:NULL]);
XCTAssertTrue([fileManager createDirectoryAtPath:destinationB withIntermediateDirectories:YES attributes:NULL error:NULL]);
NSString *sourceFile = [sourceA stringByAppendingPathComponent:@"Sparkle"];
NSString *destinationFile = [destinationB stringByAppendingPathComponent:@"Sparkle"];
XCTAssertTrue([[self bigData1] writeToFile:sourceFile atomically:YES]);
XCTAssertTrue([[self bigData2] writeToFile:destinationFile atomically:YES]);
NSError *error = nil;
// Use invalid permission mode 0777
if (![fileManager setAttributes:@{NSFilePosixPermissions : @0777} ofItemAtPath:sourceFile error:&error]) {
NSLog(@"Change Permission Error: %@", error);
XCTFail(@"Failed setting file permissions");
}
if (![fileManager setAttributes:@{NSFilePosixPermissions : @0755} ofItemAtPath:destinationFile error:&error]) {
NSLog(@"Change Permission Error: %@", error);
XCTFail(@"Failed setting file permissions");
}
} afterDiffHandler:nil afterPatchHandler:nil];
XCTAssertFalse(success);
}
- (void)testInvalidSparkleFrameworkInAfterTree
{
BOOL success = [self createAndApplyPatchWithBeforeDiffHandler:^(NSFileManager *fileManager, NSString *sourceDirectory, NSString *destinationDirectory) {
NSString *sourceA = [sourceDirectory stringByAppendingPathComponent:@"Frameworks/Sparkle.framework/Versions/A/"];
NSString *destinationB = [destinationDirectory stringByAppendingPathComponent:@"Frameworks/Sparkle.framework/Versions/B/"];
XCTAssertTrue([fileManager createDirectoryAtPath:sourceA withIntermediateDirectories:YES attributes:NULL error:NULL]);
XCTAssertTrue([fileManager createDirectoryAtPath:destinationB withIntermediateDirectories:YES attributes:NULL error:NULL]);
NSString *sourceFile = [sourceA stringByAppendingPathComponent:@"Sparkle"];
NSString *destinationFile = [destinationB stringByAppendingPathComponent:@"Sparkle"];
XCTAssertTrue([[self bigData1] writeToFile:sourceFile atomically:YES]);
XCTAssertTrue([[self bigData2] writeToFile:destinationFile atomically:YES]);
NSError *error = nil;
if (![fileManager setAttributes:@{NSFilePosixPermissions : @0755} ofItemAtPath:sourceFile error:&error]) {
NSLog(@"Change Permission Error: %@", error);
XCTFail(@"Failed setting file permissions");
}
// Use invalid permission mode 0700
if (![fileManager setAttributes:@{NSFilePosixPermissions : @0700} ofItemAtPath:destinationFile error:&error]) {
NSLog(@"Change Permission Error: %@", error);
XCTFail(@"Failed setting file permissions");
}
} afterDiffHandler:nil afterPatchHandler:nil];
XCTAssertFalse(success);
}
@end