Harden patching delta file against symbolic link at destination path (#2891)

This commit is contained in:
Zorg authored and GitHub committed 2026-07-28 21:41:46 -07:00
1 parent 63e2e40934
commit 4a84bf2108
6 files changed
+44 -13

No files matched your search

+1
View File
@@ -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.
+6 -1
View File
@@ -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;
+13 -3
View File
@@ -16,6 +16,7 @@
#include <stdio.h>
#include <stdlib.h>
#import <sys/stat.h>
#include <unistd.h>
#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];
+1
View File
@@ -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);
+10 -4
View File
@@ -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)
+13 -5
View File
@@ -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) {