From ef89e4208da03ce7181d7af20dfc13014c1b216a Mon Sep 17 00:00:00 2001 From: andym Date: Wed, 5 Mar 2008 14:07:43 +0000 Subject: [PATCH] Fixed a potential buffer overrun, made sure to assign to self in -init methods, and made some types more specific; patch courtesy Sean McBride. --- NSFileManager+Authentication.m | 24 +++++++++++++----------- SUAutomaticUpdateAlert.m | 11 +++++++---- SUStandardVersionComparator.m | 3 ++- SUStatusController.m | 9 ++++++--- SUUpdateAlert.m | 11 +++++++---- SUUpdatePermissionPrompt.m | 15 +++++++++------ 6 files changed, 44 insertions(+), 29 deletions(-) diff --git a/NSFileManager+Authentication.m b/NSFileManager+Authentication.m index 5592fbd2..eb9d2474 100644 --- a/NSFileManager+Authentication.m +++ b/NSFileManager+Authentication.m @@ -21,8 +21,8 @@ - (BOOL)currentUserOwnsPath:(NSString *)oPath { - char *path = (char *)[oPath fileSystemRepresentation]; - unsigned int uid = getuid(); + const char *path = [oPath fileSystemRepresentation]; + uid_t uid = getuid(); bool res = false; struct stat sb; if(stat(path, &sb) == 0) @@ -39,11 +39,13 @@ if(strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0) continue; - char descend[strlen(path) + 1 + entry->d_namlen + 1]; - strcpy(descend, path); - strcat(descend, "/"); - strcat(descend, entry->d_name); - res = [self currentUserOwnsPath:[NSString stringWithUTF8String:descend]]; + size_t len = strlen(path) + 1 + entry->d_namlen + 1; + char descend[len]; + strlcpy(descend, path, len); + strlcat(descend, "/", len); + strlcat(descend, entry->d_name, len); + NSString* newPath = [[NSFileManager defaultManager] stringWithFileSystemRepresentation:descend length:strlen(descend)]; + res = [self currentUserOwnsPath:newPath]; } closedir(dir); } @@ -70,19 +72,19 @@ sb.st_gid, dst]; - AuthorizationRef auth; + AuthorizationRef auth = NULL; if(AuthorizationCreate(NULL, kAuthorizationEmptyEnvironment, kAuthorizationFlagDefaults, &auth) == errAuthorizationSuccess) { char const* arguments[] = { "-c", [command fileSystemRepresentation], NULL }; - if(AuthorizationExecuteWithPrivileges(auth, "/bin/sh", kAuthorizationFlagDefaults, (char**)arguments, NULL) == errAuthorizationSuccess) + if(AuthorizationExecuteWithPrivileges(auth, "/bin/sh", kAuthorizationFlagDefaults, (char *const *)arguments, NULL) == errAuthorizationSuccess) { int status; - int pid = wait(&status); + pid_t pid = wait(&status); if(pid != -1 && WIFEXITED(status) && WEXITSTATUS(status) == 0) res = YES; } + AuthorizationFree(auth, 0); } - AuthorizationFree(auth, 0); return res; } diff --git a/SUAutomaticUpdateAlert.m b/SUAutomaticUpdateAlert.m index 982195bb..ac47f6ec 100644 --- a/SUAutomaticUpdateAlert.m +++ b/SUAutomaticUpdateAlert.m @@ -13,10 +13,13 @@ - (id)initWithAppcastItem:(SUAppcastItem *)item hostBundle:(NSBundle *)hb; { - updateItem = [item retain]; - hostBundle = [hb retain]; - [super initWithHostBundle:hb windowNibName:@"SUAutomaticUpdateAlert"]; - [self setShouldCascadeWindows:NO]; + self = [super initWithHostBundle:hb windowNibName:@"SUAutomaticUpdateAlert"]; + if (self) + { + updateItem = [item retain]; + hostBundle = [hb retain]; + [self setShouldCascadeWindows:NO]; + } return self; } diff --git a/SUStandardVersionComparator.m b/SUStandardVersionComparator.m index 57cc02f4..87505481 100644 --- a/SUStandardVersionComparator.m +++ b/SUStandardVersionComparator.m @@ -127,7 +127,8 @@ typedef enum { // Yep. Lets get the next part of the larger // n holds the index of the part we want. NSString *missingPart; - int missingType, shorterResult, largerResult; + SUCharacterType missingType; + NSComparisonResult shorterResult, largerResult; if ([partsA count] > [partsB count]) { missingPart = [partsA objectAtIndex:n]; diff --git a/SUStatusController.m b/SUStatusController.m index 7cff8ec3..80633c85 100644 --- a/SUStatusController.m +++ b/SUStatusController.m @@ -13,9 +13,12 @@ - (id)initWithHostBundle:(NSBundle *)hb { - hostBundle = [hb retain]; - [super initWithHostBundle:hb windowNibName:@"SUStatus"]; - [self setShouldCascadeWindows:NO]; + self = [super initWithHostBundle:hb windowNibName:@"SUStatus"]; + if (self) + { + hostBundle = [hb retain]; + [self setShouldCascadeWindows:NO]; + } return self; } diff --git a/SUUpdateAlert.m b/SUUpdateAlert.m index f57cd135..fc926537 100644 --- a/SUUpdateAlert.m +++ b/SUUpdateAlert.m @@ -15,10 +15,13 @@ - (id)initWithAppcastItem:(SUAppcastItem *)item hostBundle:(NSBundle *)hb { - hostBundle = [hb retain]; - updateItem = [item retain]; - [super initWithHostBundle:hb windowNibName:@"SUUpdateAlert"]; - [self setShouldCascadeWindows:NO]; + self = [super initWithHostBundle:hb windowNibName:@"SUUpdateAlert"]; + if (self) + { + hostBundle = [hb retain]; + updateItem = [item retain]; + [self setShouldCascadeWindows:NO]; + } return self; } diff --git a/SUUpdatePermissionPrompt.m b/SUUpdatePermissionPrompt.m index 3824a519..9c2fba39 100644 --- a/SUUpdatePermissionPrompt.m +++ b/SUUpdatePermissionPrompt.m @@ -18,12 +18,15 @@ - (id)initWithHostBundle:(NSBundle *)hb delegate:(id)d { - hostBundle = [hb retain]; - delegate = [d retain]; - isShowingMoreInfo = NO; - shouldSendProfile = [self shouldAskAboutProfile]; - [super initWithHostBundle:hb windowNibName:@"SUUpdatePermissionPrompt"]; - [self setShouldCascadeWindows:NO]; + self = [super initWithHostBundle:hb windowNibName:@"SUUpdatePermissionPrompt"]; + if (self) + { + hostBundle = [hb retain]; + delegate = [d retain]; + isShowingMoreInfo = NO; + shouldSendProfile = [self shouldAskAboutProfile]; + [self setShouldCascadeWindows:NO]; + } return self; }