5656// Timeout used for setDialogReturn:.
5757static NSTimeInterval MMSetDialogReturnTimeout = 1.0 ;
5858
59- static unsigned identifierCounter = 1 ;
60-
6159static BOOL isUnsafeMessage (int msgid);
6260
6361
@@ -168,8 +166,15 @@ - (id)initWithBackend:(id)backend pid:(int)processIdentifier
168166 if (!(self = [super init ]))
169167 return nil ;
170168
171- // TODO: Come up with a better way of creating an identifier.
172- identifier = identifierCounter++;
169+ // Use a random identifier. Currently, MMBackend connects using a public
170+ // NSConnection, which has security implications. Using random identifiers
171+ // make it much harder for third-party attacker to spoof.
172+ int secSuccess = SecRandomCopyBytes (kSecRandomDefault , sizeof (identifier), &identifier);
173+ if (secSuccess != errSecSuccess) {
174+ // Don't know what concrete reasons secure random would fail, but just
175+ // as a failsafe, use a less secure option.
176+ identifier = ((unsigned long )arc4random ()) << 32 | (unsigned long )arc4random ();
177+ }
173178
174179 windowController =
175180 [[MMWindowController alloc ] initWithVimController: self ];
@@ -257,7 +262,7 @@ - (void)uninitialize
257262 isInitialized = NO ;
258263}
259264
260- - (unsigned )vimControllerId
265+ - (unsigned long )vimControllerId
261266{
262267 return identifier;
263268}
@@ -436,7 +441,7 @@ - (void)sendMessage:(int)msgid data:(NSData *)data
436441 [backendProxy processInput: msgid data: data];
437442 }
438443 @catch (NSException *ex) {
439- ASLogDebug (@" processInput:data: failed: pid=%d id=%d msg=%s reason=%@ " ,
444+ ASLogDebug (@" processInput:data: failed: pid=%d id=%lu msg=%s reason=%@ " ,
440445 pid, identifier, MMVimMsgIDStrings[msgid], ex);
441446 }
442447}
@@ -468,7 +473,7 @@ - (BOOL)sendMessageNow:(int)msgid data:(NSData *)data
468473 }
469474 @catch (NSException *ex) {
470475 sendOk = NO ;
471- ASLogDebug (@" processInput:data: failed: pid=%d id=%d msg=%s reason=%@ " ,
476+ ASLogDebug (@" processInput:data: failed: pid=%d id=%lu msg=%s reason=%@ " ,
472477 pid, identifier, MMVimMsgIDStrings[msgid], ex);
473478 }
474479 @finally {
@@ -500,7 +505,7 @@ - (NSString *)evaluateVimExpression:(NSString *)expr
500505 ASLogDebug (@" eval(%@ )=%@ " , expr, eval);
501506 }
502507 @catch (NSException *ex) {
503- ASLogDebug (@" evaluateExpression: failed: pid=%d id=%d reason=%@ " ,
508+ ASLogDebug (@" evaluateExpression: failed: pid=%d id=%lu reason=%@ " ,
504509 pid, identifier, ex);
505510 }
506511
@@ -517,7 +522,7 @@ - (id)evaluateVimExpressionCocoa:(NSString *)expr
517522 errorString: errstr];
518523 ASLogDebug (@" eval(%@ )=%@ " , expr, eval);
519524 } @catch (NSException *ex) {
520- ASLogDebug (@" evaluateExpressionCocoa: failed: pid=%d id=%d reason=%@ " ,
525+ ASLogDebug (@" evaluateExpressionCocoa: failed: pid=%d id=%lu reason=%@ " ,
521526 pid, identifier, ex);
522527 *errstr = [ex reason ];
523528 }
@@ -556,7 +561,7 @@ - (void)processInputQueue:(NSArray *)queue
556561 [windowController processInputQueueDidFinish ];
557562 }
558563 @catch (NSException *ex) {
559- ASLogDebug (@" Exception: pid=%d id=%d reason=%@ " , pid, identifier, ex);
564+ ASLogDebug (@" Exception: pid=%d id=%lu reason=%@ " , pid, identifier, ex);
560565 }
561566}
562567
@@ -1275,7 +1280,7 @@ - (void)savePanelDidEnd:(NSSavePanel *)panel code:(int)code
12751280 noteNewRecentFilePath: path];
12761281 }
12771282 @catch (NSException *ex) {
1278- ASLogDebug (@" Exception: pid=%d id=%d reason=%@ " , pid, identifier, ex);
1283+ ASLogDebug (@" Exception: pid=%d id=%lu reason=%@ " , pid, identifier, ex);
12791284 }
12801285 @finally {
12811286 [conn setRequestTimeout: oldTimeout];
@@ -1308,7 +1313,7 @@ - (void)alertDidEnd:(MMAlert *)alert code:(int)code context:(void *)context
13081313 [backendProxy setDialogReturn: ret];
13091314 }
13101315 @catch (NSException *ex) {
1311- ASLogDebug (@" setDialogReturn: failed: pid=%d id=%d reason=%@ " ,
1316+ ASLogDebug (@" setDialogReturn: failed: pid=%d id=%lu reason=%@ " ,
13121317 pid, identifier, ex);
13131318 }
13141319}
@@ -2089,7 +2094,7 @@ - (void)connectionDidDie:(NSNotification *)notification
20892094
20902095- (void )scheduleClose
20912096{
2092- ASLogDebug (@" pid=%d id=%d " , pid, identifier);
2097+ ASLogDebug (@" pid=%d id=%lu " , pid, identifier);
20932098
20942099 // NOTE! This message can arrive at pretty much anytime, e.g. while
20952100 // the run loop is the 'event tracking' mode. This means that Cocoa may
0 commit comments