From 4c782a67ed99ae25bdcbfdde388a6f9946267a30 Mon Sep 17 00:00:00 2001 From: Zorg Date: Tue, 5 May 2015 20:22:02 -0400 Subject: [PATCH 1/6] Abort if code signed xattr exists anywhere Code signatures on non-mach-o files is not currently supported, but I'd like to implement this eventually. --- Sparkle/SUBinaryDeltaCreate.m | 43 +++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/Sparkle/SUBinaryDeltaCreate.m b/Sparkle/SUBinaryDeltaCreate.m index a477d674..bc802bdc 100644 --- a/Sparkle/SUBinaryDeltaCreate.m +++ b/Sparkle/SUBinaryDeltaCreate.m @@ -18,6 +18,7 @@ #include #include #include +#include #include extern int bsdiff(int argc, const char **argv); @@ -101,6 +102,38 @@ static bool aclExists(const FTSENT *ent) return false; } +static bool codeSignatureExtendedAttributeExists(const FTSENT *ent) +{ + const int options = XATTR_NOFOLLOW; + ssize_t listSize = listxattr(ent->fts_path, NULL, 0, options); + if (listSize == -1) { + return false; + } + + char *buffer = malloc((size_t)listSize); + assert(buffer != NULL); + + ssize_t sizeBack = listxattr(ent->fts_path, buffer, (size_t)listSize, options); + assert(sizeBack == listSize); + + size_t startCharacterIndex = 0; + for (size_t characterIndex = 0; characterIndex < (size_t)listSize; characterIndex++) { + if (buffer[characterIndex] == '\0') { + char *attribute = &buffer[startCharacterIndex]; + size_t length = characterIndex - startCharacterIndex; + if (strncmp("com.apple.cs.CodeDirectory", attribute, length) == 0 || + strncmp("com.apple.cs.CodeRequirements", attribute, length) == 0 || + strncmp("com.apple.cs.CodeSignature", attribute, length) == 0) { + return true; + } + startCharacterIndex = characterIndex + 1; + } + } + + free(buffer); + return false; +} + static NSString *absolutePath(NSString *path) { NSURL *url = [[NSURL alloc] initFileURLWithPath:path]; @@ -237,6 +270,11 @@ int createBinaryDelta(NSString *source, NSString *destination, NSString *patchFi fprintf(stderr, "\nDiffing ACLs are not supported. Detected ACL in before-tree on file %s\n", ent->fts_path); return 1; } + + if (codeSignatureExtendedAttributeExists(ent)) { + fprintf(stderr, "\nDiffing code signed extended attributes is not supported yet. Detected extended attribute in before-tree on file %s\n", ent->fts_path); + return 1; + } } fts_close(fts); @@ -295,6 +333,11 @@ int createBinaryDelta(NSString *source, NSString *destination, NSString *patchFi return 1; } + if (codeSignatureExtendedAttributeExists(ent)) { + fprintf(stderr, "\nDiffing code signed extended attributes is not supported yet. Detected extended attribute in after-tree on file %s\n", ent->fts_path); + return 1; + } + NSDictionary *oldInfo = originalTreeState[key]; if ([info isEqual:oldInfo]) { From 330c733958791ee348fe1a0e0c4dcdece53c783a Mon Sep 17 00:00:00 2001 From: Zorg Date: Tue, 5 May 2015 20:25:42 -0400 Subject: [PATCH 2/6] Don't forget to free the buffer --- Sparkle/SUBinaryDeltaCreate.m | 1 + 1 file changed, 1 insertion(+) diff --git a/Sparkle/SUBinaryDeltaCreate.m b/Sparkle/SUBinaryDeltaCreate.m index bc802bdc..da2c5888 100644 --- a/Sparkle/SUBinaryDeltaCreate.m +++ b/Sparkle/SUBinaryDeltaCreate.m @@ -124,6 +124,7 @@ static bool codeSignatureExtendedAttributeExists(const FTSENT *ent) if (strncmp("com.apple.cs.CodeDirectory", attribute, length) == 0 || strncmp("com.apple.cs.CodeRequirements", attribute, length) == 0 || strncmp("com.apple.cs.CodeSignature", attribute, length) == 0) { + free(buffer); return true; } startCharacterIndex = characterIndex + 1; From 585194908ea7a2a11f66290ccee5ad433c4a3eb7 Mon Sep 17 00:00:00 2001 From: Zorg Date: Tue, 5 May 2015 20:32:16 -0400 Subject: [PATCH 3/6] Bumping minor diff version --- Sparkle/SUBinaryDeltaCommon.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sparkle/SUBinaryDeltaCommon.h b/Sparkle/SUBinaryDeltaCommon.h index 44a5d04e..4a0b7b55 100644 --- a/Sparkle/SUBinaryDeltaCommon.h +++ b/Sparkle/SUBinaryDeltaCommon.h @@ -55,7 +55,7 @@ typedef NS_ENUM(uint16_t, SUBinaryDeltaMajorVersion) typedef NS_ENUM(uint16_t, SUBinaryDeltaMinorVersion) { SUAzureMinorVersion = 1, - SUBeigeMinorVersion = 0, + SUBeigeMinorVersion = 1, }; #define FIRST_DELTA_DIFF_MAJOR_VERSION SUAzureMajorVersion From 9c3ef30f4c643de720f3f23f9c2c06810f9f3f37 Mon Sep 17 00:00:00 2001 From: Zorg Date: Tue, 5 May 2015 21:13:01 -0400 Subject: [PATCH 4/6] Diff'ing code-signed xattr's should actually not be supported According to Apple's code signing document: https://developer.apple.com/library/mac/technotes/tn2206/_index.html They don't recommend storing signed non-Mach-O files inside an app. They'd prefer script files for example to be in Resources directory and be unsigned. --- Sparkle/SUBinaryDeltaCreate.m | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Sparkle/SUBinaryDeltaCreate.m b/Sparkle/SUBinaryDeltaCreate.m index da2c5888..29677467 100644 --- a/Sparkle/SUBinaryDeltaCreate.m +++ b/Sparkle/SUBinaryDeltaCreate.m @@ -273,7 +273,7 @@ int createBinaryDelta(NSString *source, NSString *destination, NSString *patchFi } if (codeSignatureExtendedAttributeExists(ent)) { - fprintf(stderr, "\nDiffing code signed extended attributes is not supported yet. Detected extended attribute in before-tree on file %s\n", ent->fts_path); + fprintf(stderr, "\nDiffing code signed extended attributes are not supported. Detected extended attribute in before-tree on file %s\n", ent->fts_path); return 1; } } @@ -335,7 +335,7 @@ int createBinaryDelta(NSString *source, NSString *destination, NSString *patchFi } if (codeSignatureExtendedAttributeExists(ent)) { - fprintf(stderr, "\nDiffing code signed extended attributes is not supported yet. Detected extended attribute in after-tree on file %s\n", ent->fts_path); + fprintf(stderr, "\nDiffing code signed extended attributes are not supported. Detected extended attribute in after-tree on file %s\n", ent->fts_path); return 1; } From 4dd4548be4f71701674b9c9e0dccacea32e3a129 Mon Sep 17 00:00:00 2001 From: Zorg Date: Tue, 5 May 2015 21:47:23 -0400 Subject: [PATCH 5/6] Added comment on why code-signed xattr is not diff'ed --- Sparkle/SUBinaryDeltaCreate.m | 3 +++ 1 file changed, 3 insertions(+) diff --git a/Sparkle/SUBinaryDeltaCreate.m b/Sparkle/SUBinaryDeltaCreate.m index 29677467..0e30629d 100644 --- a/Sparkle/SUBinaryDeltaCreate.m +++ b/Sparkle/SUBinaryDeltaCreate.m @@ -323,6 +323,9 @@ int createBinaryDelta(NSString *source, NSString *destination, NSString *patchFi // We should validate permissions and ACLs even if we don't store the info in the diff in the case of ACLs, // or in the case of permissions if the patch version is 1 + // 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 + mode_t permissions = [info[INFO_PERMISSIONS_KEY] unsignedShortValue]; if (!IS_VALID_PERMISSIONS(permissions)) { fprintf(stderr, "\nInvalid file permissions after-tree on file %s\nOnly permissions with modes 0755 and 0644 are supported\n", ent->fts_path); From 61d43f860a073b1b2c91ac3e542fdcd01475b85e Mon Sep 17 00:00:00 2001 From: Zorg Date: Tue, 5 May 2015 22:04:18 -0400 Subject: [PATCH 6/6] Add tests for aborting on code signed xattr's --- Sparkle/SUBinaryDeltaCommon.h | 4 ++++ Sparkle/SUBinaryDeltaCreate.m | 6 +++--- Tests/SUBinaryDeltaTest.m | 39 +++++++++++++++++++++++++++++++++++ 3 files changed, 46 insertions(+), 3 deletions(-) diff --git a/Sparkle/SUBinaryDeltaCommon.h b/Sparkle/SUBinaryDeltaCommon.h index 4a0b7b55..50158c57 100644 --- a/Sparkle/SUBinaryDeltaCommon.h +++ b/Sparkle/SUBinaryDeltaCommon.h @@ -16,6 +16,10 @@ #define IS_VALID_PERMISSIONS(mode) \ (((mode & PERMISSION_FLAGS) == 0755) || ((mode & PERMISSION_FLAGS) == 0644)) +#define APPLE_CODE_SIGN_XATTR_CODE_DIRECTORY_KEY "com.apple.cs.CodeDirectory" +#define APPLE_CODE_SIGN_XATTR_CODE_REQUIREMENTS_KEY "com.apple.cs.CodeRequirements" +#define APPLE_CODE_SIGN_XATTR_CODE_SIGNATURE_KEY "com.apple.cs.CodeSignature" + #define BINARY_DELTA_ATTRIBUTES_KEY "binary-delta-attributes" #define MAJOR_DIFF_VERSION_KEY "major-version" #define MINOR_DIFF_VERSION_KEY "minor-version" diff --git a/Sparkle/SUBinaryDeltaCreate.m b/Sparkle/SUBinaryDeltaCreate.m index 0e30629d..6af55016 100644 --- a/Sparkle/SUBinaryDeltaCreate.m +++ b/Sparkle/SUBinaryDeltaCreate.m @@ -121,9 +121,9 @@ static bool codeSignatureExtendedAttributeExists(const FTSENT *ent) if (buffer[characterIndex] == '\0') { char *attribute = &buffer[startCharacterIndex]; size_t length = characterIndex - startCharacterIndex; - if (strncmp("com.apple.cs.CodeDirectory", attribute, length) == 0 || - strncmp("com.apple.cs.CodeRequirements", attribute, length) == 0 || - strncmp("com.apple.cs.CodeSignature", attribute, length) == 0) { + if (strncmp(APPLE_CODE_SIGN_XATTR_CODE_DIRECTORY_KEY, attribute, length) == 0 || + strncmp(APPLE_CODE_SIGN_XATTR_CODE_REQUIREMENTS_KEY, attribute, length) == 0 || + strncmp(APPLE_CODE_SIGN_XATTR_CODE_SIGNATURE_KEY, attribute, length) == 0) { free(buffer); return true; } diff --git a/Tests/SUBinaryDeltaTest.m b/Tests/SUBinaryDeltaTest.m index ad5c8e9e..2a73c87e 100644 --- a/Tests/SUBinaryDeltaTest.m +++ b/Tests/SUBinaryDeltaTest.m @@ -12,6 +12,7 @@ #import "SUBinaryDeltaCreate.h" #import "SUBinaryDeltaApply.h" #import +#include @interface SUBinaryDeltaTest : XCTestCase @@ -930,4 +931,42 @@ typedef void (^SUDeltaHandler)(NSFileManager *fileManager, NSString *sourceDirec }]; } +- (void)testInvalidCodeSignatureExtendedAttributeInBeforeTree +{ + BOOL success = [self createAndApplyPatchWithBeforeDiffHandler:^(NSFileManager *__unused fileManager, NSString *sourceDirectory, NSString *destinationDirectory) { + NSString *sourceFile = [sourceDirectory stringByAppendingPathComponent:@"A"]; + NSString *destinationFile = [destinationDirectory stringByAppendingPathComponent:@"A"]; + + XCTAssertTrue([[NSData data] writeToFile:sourceFile atomically:YES]); + XCTAssertTrue([[NSData dataWithBytes:"loltest" length:7] writeToFile:destinationFile atomically:YES]); + + // the actual data doesn't matter for testing purposes + const char xattrValue[] = "hello"; + + XCTAssertEqual(0, setxattr([sourceFile fileSystemRepresentation], APPLE_CODE_SIGN_XATTR_CODE_DIRECTORY_KEY, xattrValue, sizeof(xattrValue), 0, XATTR_CREATE)); + XCTAssertEqual(0, setxattr([sourceFile fileSystemRepresentation], APPLE_CODE_SIGN_XATTR_CODE_REQUIREMENTS_KEY, xattrValue, sizeof(xattrValue), 0, XATTR_CREATE)); + XCTAssertEqual(0, setxattr([sourceFile fileSystemRepresentation], APPLE_CODE_SIGN_XATTR_CODE_SIGNATURE_KEY, xattrValue, sizeof(xattrValue), 0, XATTR_CREATE)); + } afterDiffHandler:nil]; + XCTAssertFalse(success); +} + +- (void)testInvalidCodeSignatureExtendedAttributeInAfterTree +{ + BOOL success = [self createAndApplyPatchWithBeforeDiffHandler:^(NSFileManager *__unused fileManager, NSString *sourceDirectory, NSString *destinationDirectory) { + NSString *sourceFile = [sourceDirectory stringByAppendingPathComponent:@"A"]; + NSString *destinationFile = [destinationDirectory stringByAppendingPathComponent:@"A"]; + + XCTAssertTrue([[NSData data] writeToFile:sourceFile atomically:YES]); + XCTAssertTrue([[NSData dataWithBytes:"loltest" length:7] writeToFile:destinationFile atomically:YES]); + + // the actual data doesn't matter for testing purposes + const char xattrValue[] = "hello"; + + XCTAssertEqual(0, setxattr([destinationFile fileSystemRepresentation], APPLE_CODE_SIGN_XATTR_CODE_DIRECTORY_KEY, xattrValue, sizeof(xattrValue), 0, XATTR_CREATE)); + XCTAssertEqual(0, setxattr([destinationFile fileSystemRepresentation], APPLE_CODE_SIGN_XATTR_CODE_REQUIREMENTS_KEY, xattrValue, sizeof(xattrValue), 0, XATTR_CREATE)); + XCTAssertEqual(0, setxattr([destinationFile fileSystemRepresentation], APPLE_CODE_SIGN_XATTR_CODE_SIGNATURE_KEY, xattrValue, sizeof(xattrValue), 0, XATTR_CREATE)); + } afterDiffHandler:nil]; + XCTAssertFalse(success); +} + @end