From 4a84bf21086398d05a39d02cbe87a54cf66dbaba Mon Sep 17 00:00:00 2001 From: Zorg Date: Tue, 28 Jul 2026 21:41:46 -0700 Subject: [PATCH] Harden patching delta file against symbolic link at destination path (#2891) --- Autoupdate/SPUSparkleDeltaArchive.h | 1 + Autoupdate/SPUSparkleDeltaArchive.m | 7 ++++++- Autoupdate/SUBinaryDeltaApply.m | 16 +++++++++++++--- Autoupdate/SUBinaryDeltaCommon.h | 1 + Autoupdate/SUBinaryDeltaCommon.m | 14 ++++++++++---- Vendor/bsdiff/bspatch.c | 18 +++++++++++++----- 6 files changed, 44 insertions(+), 13 deletions(-) diff --git a/Autoupdate/SPUSparkleDeltaArchive.h b/Autoupdate/SPUSparkleDeltaArchive.h index 48c6a0e4..8306ff0e 100644 --- a/Autoupdate/SPUSparkleDeltaArchive.h +++ b/Autoupdate/SPUSparkleDeltaArchive.h @@ -20,6 +20,7 @@ NS_ASSUME_NONNULL_BEGIN #define SPARKLE_DELTA_ARCHIVE_ERROR_CODE_BAD_CLONE_LOOKUP 4 #define SPARKLE_DELTA_ARCHIVE_ERROR_CODE_TOO_MANY_FILES 5 #define SPARKLE_DELTA_ARCHIVE_ERROR_CODE_LINK_TOO_LONG 6 +#define SPARKLE_DELTA_ARCHIVE_ERROR_CODE_BAD_ITEM_INDEX 7 /* Modern container format for binary delta archives. diff --git a/Autoupdate/SPUSparkleDeltaArchive.m b/Autoupdate/SPUSparkleDeltaArchive.m index a02954cb..218058b0 100644 --- a/Autoupdate/SPUSparkleDeltaArchive.m +++ b/Autoupdate/SPUSparkleDeltaArchive.m @@ -458,7 +458,12 @@ static compression_algorithm compressionAlgorithmForMode(SPUDeltaCompressionMode if (commands == SPUDeltaItemCommandEndMarker) { break; } - + + if (currentItemIndex >= relativeFilePaths.count) { + _error = [NSError errorWithDomain:SPARKLE_DELTA_ARCHIVE_ERROR_DOMAIN code:SPARKLE_DELTA_ARCHIVE_ERROR_CODE_BAD_ITEM_INDEX userInfo:@{ NSLocalizedDescriptionKey: [NSString stringWithFormat:@"Item index %llu is past relative path table bounds of length %lu", currentItemIndex, (unsigned long)relativeFilePaths.count] }]; + break; + } + // Check if we need to decode additional data uint16_t decodedMode = 0; uint64_t decodedDataLength = 0; diff --git a/Autoupdate/SUBinaryDeltaApply.m b/Autoupdate/SUBinaryDeltaApply.m index 162e918c..2f439b09 100644 --- a/Autoupdate/SUBinaryDeltaApply.m +++ b/Autoupdate/SUBinaryDeltaApply.m @@ -16,6 +16,7 @@ #include #include #import +#include #include "AppKitPrevention.h" @@ -229,8 +230,7 @@ BOOL applyBinaryDelta(NSString *source, NSString *finalDestination, NSString *pa } } - // Don't use -[NSFileManager fileExistsAtPath:] because it will follow symbolic links - BOOL fileExisted = verbose && [fileManager attributesOfItemAtPath:destinationFilePath error:nil]; + BOOL fileExisted = verbose && fileExists(destinationFilePath); BOOL removedFile = NO; // Files that have no property set that we check for will get ignored @@ -320,8 +320,18 @@ BOOL applyBinaryDelta(NSString *source, NSString *finalDestination, NSString *pa // applyBinaryDeltaToFile() normally preserves file permissions on the file it's replacing. // However this is not possible if the destination file we're patching is not writable. // We also need to preserve permissions for clones except when we'll be changing permissions later anyway. + BOOL destinationFileIsWritable; + { + const char *destinationFileSystemRepresentation = destinationFilePath.fileSystemRepresentation; + struct stat destinationFileInfo; + destinationFileIsWritable = + (lstat(destinationFileSystemRepresentation, &destinationFileInfo) == 0 && + !S_ISLNK(destinationFileInfo.st_mode) && + access(destinationFileSystemRepresentation, W_OK) == 0); + + } BOOL needsToCopyFilePermissions; - if (![fileManager isWritableFileAtPath:destinationFilePath]) { + if (!destinationFileIsWritable) { // Remove the file non-writable we're patching that may cause issues [fileManager removeItemAtPath:destinationFilePath error:NULL]; diff --git a/Autoupdate/SUBinaryDeltaCommon.h b/Autoupdate/SUBinaryDeltaCommon.h index 7b1bfc91..feb7e716 100644 --- a/Autoupdate/SUBinaryDeltaCommon.h +++ b/Autoupdate/SUBinaryDeltaCommon.h @@ -70,6 +70,7 @@ NSString *displayHashFromRawHash(const unsigned char *hash); void getRawHashFromDisplayHash(unsigned char *hash, NSString *hexHash); extern NSString *hashOfTreeWithVersion(NSString *path, uint16_t majorVersion); extern NSString *hashOfTree(NSString *path); +BOOL fileExists(NSString *path); extern BOOL removeTree(NSString *path); extern BOOL copyTree(NSFileManager *fileManager, NSString *source, NSString *dest); extern BOOL modifyPermissions(NSString *path, mode_t desiredPermissions); diff --git a/Autoupdate/SUBinaryDeltaCommon.m b/Autoupdate/SUBinaryDeltaCommon.m index 0202b26c..4193ffa9 100644 --- a/Autoupdate/SUBinaryDeltaCommon.m +++ b/Autoupdate/SUBinaryDeltaCommon.m @@ -453,14 +453,20 @@ extern NSString *hashOfTree(NSString *path) return hashOfTreeWithVersion(path, SUBinaryDeltaMajorVersionLatest); } +BOOL fileExists(NSString *path) +{ + // Don't use fileExistsForPath: because it will try to follow symbolic links + struct stat fileInfo; + return lstat(path.fileSystemRepresentation, &fileInfo) == 0; +} + BOOL removeTree(NSString *path) { - NSFileManager *fileManager = [NSFileManager defaultManager]; - // Don't use fileExistsForPath: because it will try to follow symbolic links - if (![fileManager attributesOfItemAtPath:path error:nil]) { + if (!fileExists(path)) { return YES; } - return [fileManager removeItemAtPath:path error:nil]; + + return [NSFileManager.defaultManager removeItemAtPath:path error:nil]; } BOOL copyTree(NSFileManager *fileManager, NSString *source, NSString *dest) diff --git a/Vendor/bsdiff/bspatch.c b/Vendor/bsdiff/bspatch.c index 0c234996..566b3ecf 100644 --- a/Vendor/bsdiff/bspatch.c +++ b/Vendor/bsdiff/bspatch.c @@ -343,11 +343,19 @@ int bspatch(int argc,const char * const argv[]) } epf = NULL; - /* Write the new file */ - f = fopen(argv[2], "w"); - if (f == NULL) { - warn("failed to write new file: %s", argv[2]); - goto cleanup; + /* Write the new file without following symlinks at the destination path */ + { + int newfd = open(argv[2], O_WRONLY | O_CREAT | O_TRUNC | O_NOFOLLOW, 0644); + if (newfd < 0) { + warn("failed to open new file: %s", argv[2]); + goto cleanup; + } + f = fdopen(newfd, "w"); + if (f == NULL) { + warn("failed to write new file: %s", argv[2]); + close(newfd); + goto cleanup; + } } if (fwrite(newp, 1, (size_t)newsize, f) < (size_t)newsize) {