|
|||||||
![]() |
|
|
Thread Tools | Search this Thread | Display Modes |
|
|
#1 |
|
Debugging a malloc crash within AuthorizationExecuteWithPrivileges
I've got this chunk of code that calls AuthorizationExecuteWithPrivileges(). So far I've seen 5 crashes all with the same stack trace, all running as 64-bit on 10.6.x, but unreproducible on my end:
Code:
Exception Type: EXC_BAD_ACCESS (SIGSEGV) Exception Codes: KERN_INVALID_ADDRESS at 0x00000001029e77fe Crashed Thread: 0 Dispatch queue: com.apple.main-thread Thread 0 Crashed: Dispatch queue: com.apple.main-thread 0 libSystem.B.dylib 0x00007fff8725cf6b small_malloc_from_free_list + 912 1 libSystem.B.dylib 0x00007fff87259741 szone_malloc_should_clear + 2070 2 libSystem.B.dylib 0x00007fff87258eea malloc_zone_malloc + 82 3 libSystem.B.dylib 0x00007fff872571e8 malloc + 44 4 libSystem.B.dylib 0x00007fff8726e7f2 _pthread_work_internal_init + 180 5 libSystem.B.dylib 0x00007fff87330231 pthread_workqueue_atfork_child + 39 6 libSystem.B.dylib 0x00007fff87318ebc _cthread_fork_child + 180 7 libSystem.B.dylib 0x00007fff872b8923 fork + 83 8 com.mycompany.myapp 0x000000010004d648 AuthorizationExecuteWithPrivileges ... Code:
+ (OSStatus)launchAuthorizedTaskForPath:(NSString *)taskPath arguments:(NSArray *)arguments output:(NSString **)output
{
if (!taskPath || ![[NSFileManager defaultManager] fileExistsAtPath:taskPath])
return errAuthorizationToolExecuteFailure;
AuthorizationRef authorizationRef = NULL;
OSStatus status = AuthorizationCreate(NULL, kAuthorizationEmptyEnvironment, kAuthorizationFlagDefaults, &authorizationRef);
if (status != errAuthorizationSuccess)
return status;
const char *path = [taskPath fileSystemRepresentation];
AuthorizationItem right = {kAuthorizationRightExecute, strlen(path), (void *)path, kAuthorizationFlagDefaults};
AuthorizationRights rightSet = {1, &right};
AuthorizationFlags flags = kAuthorizationFlagDefaults | kAuthorizationFlagPreAuthorize | kAuthorizationFlagInteractionAllowed | kAuthorizationFlagExtendRights;
status = AuthorizationCopyRights(authorizationRef, &rightSet, kAuthorizationEmptyEnvironment, flags, NULL);
if (status != errAuthorizationSuccess)
{
AuthorizationFree(authorizationRef, kAuthorizationFlagDestroyRights);
return status;
}
char **args = NULL;
if ((arguments != nil) && ([arguments count] > 0))
{
args = calloc(1, sizeof(char*) * ([arguments count] + 1));
NSUInteger i=0;
for (i=0; i<[arguments count]; i++)
{
NSString *arg = [arguments objectAtIndex:i];
if ([arg isKindOfClass:[NSString class]])
args[i] = (char *)[arg UTF8String];
}
}
FILE *file = NULL;
status = AuthorizationExecuteWithPrivileges(authorizationRef, path, kAuthorizationFlagDefaults, args, (output != NULL ? &file : NULL));
...
I've been staring at this code all morning and am going a little crazy. The one idea I have is the call to calloc which *should* properly separate the count and size arguments instead of combining them like a call to malloc. However, would this really make a difference? The only thing I can think of is separating them might help prevent overflows but in this specific situation there is only 1 argument so that shouldn't apply. Any other ideas? Last edited by kainjow; Feb 8, 2011 at 12:28 PM. |
|
|
|
0
|
|
|
#2 |
|
I'm probably not going to be able to be much help, but a few things:
Where do you free the pointer returned from calloc, assigned to args? Have you written a small program that calls this method in a loop with quasi-random arguments and let it run for a few hours/days to see if you ever get a crash? Have you run this for edge cases like 0 arguments? Best i can think of for now, being wholly unfamiliar with the APIs you're using. -Lee |
|
|
|
0
|
|
|
#3 |
|
First, is it running under GC or retain/release?
Second, calloc() zeros the memory it returns; malloc() doesn't. This is critical to the posted code, since you don't actually put a NULL in the last slot of args. So I would expect malloc() to fail in weird ways. Third, I don't think this code does quite what you want: Code:
for (i=0; i<[arguments count]; i++)
{
NSString *arg = [arguments objectAtIndex:i];
if ([arg isKindOfClass:[NSString class]])
args[i] = (char *)[arg UTF8String];
}
Finally, I might be suspicious of the lifetime of [arg UTF8String]'s returned pointer. It may be worthwhile to copy it to a calloc()'ed buffer whose lifetime you have more control over. And are you sure AEWP will work with a NULL ptr passed in args? Again, I don't think it's relevant to the crash, but if arguments is nil or empty, that's what's passed in. |
|
|
|
0
|
|
|
#4 |
|
According to the docs, the block returned by -UTF8String will belong to the autorelease pool. I would not pass such a thing to an asynchronous process, and it not being a NSObject, there is no way to prevent its deallocation.
__________________
Mr. Paul, sir, I thought you should be advised, there seems to be a zombie tribble clinging to your head, for it is scarfing your brain
|
|
|
|
0
|
|
|
#5 | ||||||||
|
Quote:
Quote:
Quote:
Quote:
retain/release Quote:
Quote:
Quote:
Quote:
|
|||||||||
|
|
0
|
|
|
#6 | ||
|
Quote:
Quote:
|
|||
|
|
0
|
|
|
#7 | |
|
Quote:
It seems like playing with arguments is going to be the key, because that seems to be what affects the behavior of the code at runtime. The idea of chown33 to get some memory for the arguments array and copy might be a good idea, too. If the char * returned from UTF8String has a lifetime of "sometime", that could certainly result in erroneous behavior "sometime". -Lee |
||
|
|
0
|
![]() |
|
«
Previous Thread
|
Next Thread
»
| Thread Tools | Search this Thread |
| Display Modes | |
|
|
Similar Threads
|
||||
| thread | Thread Starter | Forum | Replies | Last Post |
| App crashes when Archived using Release but works fine with Debug... | dbramhall | iPhone/iPad Programming | 5 | Apr 17, 2012 07:49 PM |
| Crash Logs with iPad 2 | RogersDA | iPad | 6 | May 13, 2011 10:18 AM |
| Eclipse Java EE Helio (sr 2) debug perspective not scrolling with breakpoint? | robm99x | Mac Programming | 0 | Mar 24, 2011 06:27 AM |
| Really Need some Debugging Tips!!! App crashes Device but not Simulator! | belsokar | iPhone/iPad Programming | 2 | Jul 29, 2008 09:37 PM |
| NEW MBA crashes within minute, Word 2003 ? | writermiguel78 | MacBook Air | 3 | May 16, 2008 09:33 PM |
All times are GMT -5. The time now is 03:38 AM.







Linear Mode

