56
56
// Timeout used for setDialogReturn:.
57
57
static NSTimeInterval MMSetDialogReturnTimeout = 1.0 ;
58
58
59
- static unsigned identifierCounter = 1 ;
60
-
61
59
static BOOL isUnsafeMessage (int msgid);
62
60
63
61
@@ -168,8 +166,15 @@ - (id)initWithBackend:(id)backend pid:(int)processIdentifier
168
166
if (!(self = [super init ]))
169
167
return nil ;
170
168
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
+ }
173
178
174
179
windowController =
175
180
[[MMWindowController alloc ] initWithVimController: self ];
@@ -257,7 +262,7 @@ - (void)uninitialize
257
262
isInitialized = NO ;
258
263
}
259
264
260
- - (unsigned )vimControllerId
265
+ - (unsigned long )vimControllerId
261
266
{
262
267
return identifier;
263
268
}
@@ -436,7 +441,7 @@ - (void)sendMessage:(int)msgid data:(NSData *)data
436
441
[backendProxy processInput: msgid data: data];
437
442
}
438
443
@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=%@ " ,
440
445
pid, identifier, MMVimMsgIDStrings[msgid], ex);
441
446
}
442
447
}
@@ -468,7 +473,7 @@ - (BOOL)sendMessageNow:(int)msgid data:(NSData *)data
468
473
}
469
474
@catch (NSException *ex) {
470
475
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=%@ " ,
472
477
pid, identifier, MMVimMsgIDStrings[msgid], ex);
473
478
}
474
479
@finally {
@@ -500,7 +505,7 @@ - (NSString *)evaluateVimExpression:(NSString *)expr
500
505
ASLogDebug (@" eval(%@ )=%@ " , expr, eval);
501
506
}
502
507
@catch (NSException *ex) {
503
- ASLogDebug (@" evaluateExpression: failed: pid=%d id=%d reason=%@ " ,
508
+ ASLogDebug (@" evaluateExpression: failed: pid=%d id=%lu reason=%@ " ,
504
509
pid, identifier, ex);
505
510
}
506
511
@@ -517,7 +522,7 @@ - (id)evaluateVimExpressionCocoa:(NSString *)expr
517
522
errorString: errstr];
518
523
ASLogDebug (@" eval(%@ )=%@ " , expr, eval);
519
524
} @catch (NSException *ex) {
520
- ASLogDebug (@" evaluateExpressionCocoa: failed: pid=%d id=%d reason=%@ " ,
525
+ ASLogDebug (@" evaluateExpressionCocoa: failed: pid=%d id=%lu reason=%@ " ,
521
526
pid, identifier, ex);
522
527
*errstr = [ex reason ];
523
528
}
@@ -556,7 +561,7 @@ - (void)processInputQueue:(NSArray *)queue
556
561
[windowController processInputQueueDidFinish ];
557
562
}
558
563
@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);
560
565
}
561
566
}
562
567
@@ -1275,7 +1280,7 @@ - (void)savePanelDidEnd:(NSSavePanel *)panel code:(int)code
1275
1280
noteNewRecentFilePath: path];
1276
1281
}
1277
1282
@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);
1279
1284
}
1280
1285
@finally {
1281
1286
[conn setRequestTimeout: oldTimeout];
@@ -1308,7 +1313,7 @@ - (void)alertDidEnd:(MMAlert *)alert code:(int)code context:(void *)context
1308
1313
[backendProxy setDialogReturn: ret];
1309
1314
}
1310
1315
@catch (NSException *ex) {
1311
- ASLogDebug (@" setDialogReturn: failed: pid=%d id=%d reason=%@ " ,
1316
+ ASLogDebug (@" setDialogReturn: failed: pid=%d id=%lu reason=%@ " ,
1312
1317
pid, identifier, ex);
1313
1318
}
1314
1319
}
@@ -2089,7 +2094,7 @@ - (void)connectionDidDie:(NSNotification *)notification
2089
2094
2090
2095
- (void )scheduleClose
2091
2096
{
2092
- ASLogDebug (@" pid=%d id=%d " , pid, identifier);
2097
+ ASLogDebug (@" pid=%d id=%lu " , pid, identifier);
2093
2098
2094
2099
// NOTE! This message can arrive at pretty much anytime, e.g. while
2095
2100
// the run loop is the 'event tracking' mode. This means that Cocoa may
0 commit comments