-
Notifications
You must be signed in to change notification settings - Fork 51
Expand file tree
/
Copy pathSNTRule.m
More file actions
506 lines (450 loc) · 18.5 KB
/
Copy pathSNTRule.m
File metadata and controls
506 lines (450 loc) · 18.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
/// Copyright 2015 Google Inc. All rights reserved.
/// Copyright 2024 North Pole Security, Inc.
///
/// Licensed under the Apache License, Version 2.0 (the "License");
/// you may not use this file except in compliance with the License.
/// You may obtain a copy of the License at
///
/// http://www.apache.org/licenses/LICENSE-2.0
///
/// Unless required by applicable law or agreed to in writing, software
/// distributed under the License is distributed on an "AS IS" BASIS,
/// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
/// See the License for the specific language governing permissions and
/// limitations under the License.
#import "Source/common/SNTRule.h"
#include <CommonCrypto/CommonCrypto.h>
#include <Kernel/kern/cs_blobs.h>
#include <os/base.h>
#import "Source/common/CoderMacros.h"
#import "Source/common/SNTError.h"
#import "Source/common/SNTSyncConstants.h"
// https://developer.apple.com/help/account/manage-your-team/locate-your-team-id/
static const NSUInteger kExpectedTeamIDLength = 10;
@interface SNTRule ()
@property(readwrite) NSUInteger timestamp;
@end
@implementation SNTRule
- (instancetype)initWithIdentifier:(NSString *)identifier
state:(SNTRuleState)state
type:(SNTRuleType)type
customMsg:(NSString *)customMsg
customURL:(NSString *)customURL
timestamp:(NSUInteger)timestamp
comment:(NSString *)comment
celExpr:(NSString *)celExpr
error:(NSError **)error {
self = [super init];
if (self) {
if (identifier.length == 0) {
return nil;
}
NSCharacterSet *nonHex =
[[NSCharacterSet characterSetWithCharactersInString:@"0123456789abcdef"] invertedSet];
NSCharacterSet *nonUppercaseAlphaNumeric = [[NSCharacterSet
characterSetWithCharactersInString:@"ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"] invertedSet];
switch (type) {
case SNTRuleTypeBinary: OS_FALLTHROUGH;
case SNTRuleTypeCertificate: {
// For binary and certificate rules, force the hash identifier to be lowercase hex.
identifier = [identifier lowercaseString];
identifier = [identifier stringByTrimmingCharactersInSet:nonHex];
if (identifier.length != (CC_SHA256_DIGEST_LENGTH * 2)) {
[SNTError populateError:error
withCode:SNTErrorCodeRuleInvalidIdentifier
format:@"Rule received with invalid identifier for its type"];
return nil;
}
break;
}
case SNTRuleTypeTeamID: {
// TeamIDs are always [0-9A-Z], so enforce that the identifier is uppercase
identifier =
[[identifier uppercaseString] stringByTrimmingCharactersInSet:nonUppercaseAlphaNumeric];
if (identifier.length != kExpectedTeamIDLength) {
[SNTError populateError:error
withCode:SNTErrorCodeRuleInvalidIdentifier
format:@"Rule received with invalid identifier for its type"];
return nil;
}
break;
}
case SNTRuleTypeSigningID: {
// SigningID rules are a combination of `TeamID:SigningID`. The TeamID should
// be forced to be uppercase, but because very loose rules exist for SigningIDs,
// their case will be kept as-is. However, platform binaries are expected to
// have the hardcoded string "platform" as the team ID and the case will be left
// as is.
NSArray *sidComponents = [identifier componentsSeparatedByString:@":"];
if (!sidComponents || sidComponents.count < 2) {
[SNTError populateError:error
withCode:SNTErrorCodeRuleInvalidIdentifier
format:@"Rule received with invalid identifier for its type"];
return nil;
}
// The first component is the TeamID
NSString *teamID = sidComponents[0];
if (![teamID isEqualToString:@"platform"]) {
teamID =
[[teamID uppercaseString] stringByTrimmingCharactersInSet:nonUppercaseAlphaNumeric];
if (teamID.length != kExpectedTeamIDLength) {
[SNTError populateError:error
withCode:SNTErrorCodeRuleInvalidIdentifier
format:@"Rule received with invalid identifier for its type"];
return nil;
}
}
// The rest of the components are the Signing ID since ":" a legal character.
// Join all but the last element of the components to rebuild the SigningID.
NSString *signingID =
[[sidComponents subarrayWithRange:NSMakeRange(1, sidComponents.count - 1)]
componentsJoinedByString:@":"];
if (signingID.length == 0) {
[SNTError populateError:error
withCode:SNTErrorCodeRuleInvalidIdentifier
format:@"Rule received with invalid identifier for its type"];
return nil;
}
identifier = [NSString stringWithFormat:@"%@:%@", teamID, signingID];
break;
}
case SNTRuleTypeCDHash: {
identifier = [[identifier lowercaseString] stringByTrimmingCharactersInSet:nonHex];
if (identifier.length != CS_CDHASH_LEN * 2) {
[SNTError populateError:error
withCode:SNTErrorCodeRuleInvalidIdentifier
format:@"Rule received with invalid identifier for its type"];
return nil;
}
break;
}
default: {
break;
}
}
if (state == SNTRuleStateCEL && celExpr.length == 0) {
[SNTError populateError:error
withCode:SNTErrorCodeRuleInvalidCELExpression
format:@"Rule received missing CEL expression"];
return nil;
}
_identifier = identifier;
_state = state;
_type = type;
_customMsg = customMsg;
_customURL = customURL;
_timestamp = timestamp;
_comment = comment;
_celExpr = celExpr;
}
return self;
}
- (instancetype)initWithIdentifier:(NSString *)identifier
state:(SNTRuleState)state
type:(SNTRuleType)type
customMsg:(NSString *)customMsg
customURL:(NSString *)customURL
celExpr:(NSString *)celExpr {
self = [self initWithIdentifier:identifier
state:state
type:type
customMsg:customMsg
customURL:customURL
timestamp:0
comment:nil
celExpr:celExpr
error:nil];
// Initialize timestamp to current time if rule is transitive.
if (self && state == SNTRuleStateAllowTransitive) {
[self resetTimestamp];
}
return self;
}
- (instancetype)initWithIdentifier:(NSString *)identifier
state:(SNTRuleState)state
type:(SNTRuleType)type {
return [self initWithIdentifier:identifier
state:state
type:type
customMsg:nil
customURL:nil
timestamp:0
comment:nil
celExpr:nil
error:nil];
}
// lowercase policy keys and upper case the policy decision.
- (NSDictionary *)normalizeRuleDictionary:(NSDictionary *)dict {
NSMutableDictionary *newDict = [NSMutableDictionary dictionaryWithCapacity:dict.count];
for (id rawKey in dict) {
if (![rawKey isKindOfClass:[NSString class]]) continue;
NSString *key = (NSString *)rawKey;
NSString *newKey = [key lowercaseString];
if (([newKey isEqualToString:kRulePolicy] || [newKey isEqualToString:kRuleType]) &&
[dict[key] isKindOfClass:[NSString class]]) {
newDict[newKey] = [dict[key] uppercaseString];
} else {
newDict[newKey] = dict[key];
}
}
return newDict;
}
// Converts rule information from santactl or static rules into a SNTRule.
- (instancetype)initWithDictionary:(NSDictionary *)rawDict error:(NSError **)error {
if (![rawDict isKindOfClass:[NSDictionary class]]) {
[SNTError populateError:error
withCode:SNTErrorCodeInvalidType
format:@"Rule received with invalid type %@", [rawDict class]];
return nil;
}
NSDictionary *dict = [self normalizeRuleDictionary:rawDict];
NSString *identifier = dict[kRuleIdentifier];
if (![identifier isKindOfClass:[NSString class]] || !identifier.length) {
identifier = dict[kRuleSHA256];
}
if (![identifier isKindOfClass:[NSString class]] || !identifier.length) {
[SNTError populateError:error
withCode:SNTErrorCodeRuleMissingIdentifier
format:@"Rule received with missing/invalid identifier '%@'", identifier];
return nil;
}
NSString *policyString = dict[kRulePolicy];
SNTRuleState state;
if (![policyString isKindOfClass:[NSString class]]) {
[SNTError populateError:error
withCode:SNTErrorCodeRuleMissingPolicy
format:@"Rule received with missing/invalid policy '%@'", policyString];
return nil;
}
if ([policyString isEqual:kRulePolicyAllowlist] ||
[policyString isEqual:kRulePolicyAllowlistDeprecated]) {
state = SNTRuleStateAllow;
} else if ([policyString isEqual:kRulePolicyAllowlistCompiler] ||
[policyString isEqual:kRulePolicyAllowlistCompilerDeprecated]) {
state = SNTRuleStateAllowCompiler;
} else if ([policyString isEqual:kRulePolicyAllowlistLocalBinary]) {
state = SNTRuleStateAllowLocalBinary;
} else if ([policyString isEqual:kRulePolicyAllowlistLocalSigningID]) {
state = SNTRuleStateAllowLocalSigningID;
} else if ([policyString isEqual:kRulePolicyBlocklist] ||
[policyString isEqual:kRulePolicyBlocklistDeprecated]) {
state = SNTRuleStateBlock;
} else if ([policyString isEqual:kRulePolicySilentBlocklist] ||
[policyString isEqual:kRulePolicySilentBlocklistDeprecated]) {
state = SNTRuleStateSilentBlock;
} else if ([policyString isEqual:kRulePolicyRemove]) {
state = SNTRuleStateRemove;
} else if ([policyString isEqual:kRulePolicyCEL]) {
state = SNTRuleStateCEL;
} else {
[SNTError populateError:error
withCode:SNTErrorCodeRuleInvalidPolicy
format:@"Rule received with invalid policy '%@'", policyString];
return nil;
}
NSString *ruleTypeString = dict[kRuleType];
SNTRuleType type;
if (![ruleTypeString isKindOfClass:[NSString class]]) {
[SNTError populateError:error
withCode:SNTErrorCodeRuleMissingRuleType
format:@"Rule received with missing/invalid rule type '%@'", ruleTypeString];
return nil;
}
if ([ruleTypeString isEqual:kRuleTypeBinary]) {
type = SNTRuleTypeBinary;
} else if ([ruleTypeString isEqual:kRuleTypeCertificate]) {
type = SNTRuleTypeCertificate;
} else if ([ruleTypeString isEqual:kRuleTypeTeamID]) {
type = SNTRuleTypeTeamID;
} else if ([ruleTypeString isEqual:kRuleTypeSigningID]) {
type = SNTRuleTypeSigningID;
} else if ([ruleTypeString isEqual:kRuleTypeCDHash]) {
type = SNTRuleTypeCDHash;
} else {
[SNTError populateError:error
withCode:SNTErrorCodeRuleInvalidRuleType
format:@"Rule received with invalid rule type '%@'", ruleTypeString];
return nil;
}
NSString *customMsg = dict[kRuleCustomMsg];
if (![customMsg isKindOfClass:[NSString class]] || customMsg.length == 0) {
customMsg = nil;
}
NSString *customURL = dict[kRuleCustomURL];
if (![customURL isKindOfClass:[NSString class]] || customURL.length == 0) {
customURL = nil;
}
NSString *comment = dict[kRuleComment];
if (![comment isKindOfClass:[NSString class]] || comment.length == 0) {
comment = nil;
}
NSString *celExpr = dict[kRuleCELExpr];
if (![celExpr isKindOfClass:[NSString class]] || celExpr.length == 0) {
celExpr = nil;
}
return [self initWithIdentifier:identifier
state:state
type:type
customMsg:customMsg
customURL:customURL
timestamp:0
comment:comment
celExpr:celExpr
error:error];
}
#pragma mark NSSecureCoding
+ (BOOL)supportsSecureCoding {
return YES;
}
- (void)encodeWithCoder:(NSCoder *)coder {
ENCODE(coder, identifier);
ENCODE_BOXABLE(coder, state);
ENCODE_BOXABLE(coder, type);
ENCODE(coder, customMsg);
ENCODE(coder, customURL);
ENCODE_BOXABLE(coder, timestamp);
ENCODE(coder, comment);
ENCODE(coder, celExpr);
}
- (instancetype)initWithCoder:(NSCoder *)decoder {
self = [super init];
if (self) {
DECODE(decoder, identifier, NSString);
DECODE_SELECTOR(decoder, state, NSNumber, intValue);
DECODE_SELECTOR(decoder, type, NSNumber, intValue);
DECODE(decoder, customMsg, NSString);
DECODE(decoder, customURL, NSString);
DECODE_SELECTOR(decoder, timestamp, NSNumber, unsignedIntegerValue);
DECODE(decoder, comment, NSString);
DECODE(decoder, celExpr, NSString);
}
return self;
}
- (NSString *)ruleStateToPolicyString:(SNTRuleState)state {
switch (state) {
case SNTRuleStateAllow: return kRulePolicyAllowlist;
case SNTRuleStateAllowCompiler: return kRulePolicyAllowlistCompiler;
case SNTRuleStateBlock: return kRulePolicyBlocklist;
case SNTRuleStateSilentBlock: return kRulePolicySilentBlocklist;
case SNTRuleStateRemove: return kRulePolicyRemove;
case SNTRuleStateAllowTransitive: return @"AllowTransitive";
case SNTRuleStateAllowLocalBinary: return kRulePolicyAllowlistLocalBinary;
case SNTRuleStateAllowLocalSigningID: return kRulePolicyAllowlistLocalSigningID;
case SNTRuleStateCEL: return kRulePolicyCEL;
// This should never be hit. But is here for completion.
default: return @"Unknown";
}
}
- (NSString *)ruleTypeToString:(SNTRuleType)ruleType {
switch (ruleType) {
case SNTRuleTypeBinary: return kRuleTypeBinary;
case SNTRuleTypeCertificate: return kRuleTypeCertificate;
case SNTRuleTypeTeamID: return kRuleTypeTeamID;
case SNTRuleTypeSigningID: return kRuleTypeSigningID;
// This should never be hit. If we have rule types of Unknown then there's a
// coding error somewhere.
default: return @"Unknown";
}
}
// Returns an NSDictionary representation of the rule. Primarily use for
// exporting rules.
- (NSDictionary *)dictionaryRepresentation {
return @{
kRuleIdentifier : self.identifier,
kRulePolicy : [self ruleStateToPolicyString:self.state],
kRuleType : [self ruleTypeToString:self.type],
kRuleCustomMsg : self.customMsg ?: @"",
kRuleCustomURL : self.customURL ?: @"",
kRuleComment : self.comment ?: @"",
kRuleCELExpr : self.celExpr ?: @"",
};
}
- (BOOL)isEqual:(id)other {
if (other == self) return YES;
if (![other isKindOfClass:[SNTRule class]]) return NO;
SNTRule *o = other;
return ([self.identifier isEqual:o.identifier] && self.state == o.state && self.type == o.type &&
(self.celExpr == nil || [self.celExpr isEqual:o.celExpr]));
}
- (NSUInteger)hash {
NSUInteger prime = 31;
NSUInteger result = 1;
result = prime * result + [self.identifier hash];
result = prime * result + self.state;
result = prime * result + self.type;
result = prime * result + [self.celExpr hash];
result = prime * result + [self.celExpr hash];
return result;
}
- (NSString *)description {
return [NSString
stringWithFormat:@"SNTRule: Identifier: %@, State: %ld, Type: %ld, Timestamp: %lu",
self.identifier, self.state, self.type, (unsigned long)self.timestamp];
}
- (NSString *)stringifyWithColor:(BOOL)colorize {
NSMutableString *output;
// Rule state is saved as eventState for output colorization down below
SNTEventState eventState = SNTEventStateUnknown;
switch (self.state) {
case SNTRuleStateUnknown: return @"None"; break;
case SNTRuleStateAllow: OS_FALLTHROUGH;
case SNTRuleStateAllowCompiler: OS_FALLTHROUGH;
case SNTRuleStateAllowTransitive:
output = [@"Allowed" mutableCopy];
eventState = SNTEventStateAllow;
break;
case SNTRuleStateBlock: OS_FALLTHROUGH;
case SNTRuleStateSilentBlock:
output = [@"Blocked" mutableCopy];
eventState = SNTEventStateBlock;
break;
case SNTRuleStateCEL: output = [@"CEL" mutableCopy]; break;
case SNTRuleStateRemove: OS_FALLTHROUGH;
default:
output = [NSMutableString stringWithFormat:@"Unexpected rule state: %ld", self.state];
break;
}
[output appendString:@" ("];
switch (self.type) {
case SNTRuleTypeUnknown: [output appendString:@"Unknown"]; break;
case SNTRuleTypeCDHash: [output appendString:@"CDHash"]; break;
case SNTRuleTypeBinary: [output appendString:@"Binary"]; break;
case SNTRuleTypeSigningID: [output appendString:@"SigningID"]; break;
case SNTRuleTypeCertificate: [output appendString:@"Certificate"]; break;
case SNTRuleTypeTeamID: [output appendString:@"TeamID"]; break;
default:
output = [NSMutableString stringWithFormat:@"Unexpected rule type: %ld", self.type];
break;
}
// Add additional attributes
switch (self.state) {
case SNTRuleStateAllowCompiler: [output appendString:@", Compiler"]; break;
case SNTRuleStateAllowTransitive: [output appendString:@", Transitive"]; break;
case SNTRuleStateSilentBlock: [output appendString:@", Silent"]; break;
default: break;
}
[output appendString:@")"];
// Colorize
if (colorize) {
if ((SNTEventStateAllow & eventState)) {
[output insertString:@"\033[32m" atIndex:0];
[output appendString:@"\033[0m"];
} else if ((SNTEventStateBlock & eventState)) {
[output insertString:@"\033[31m" atIndex:0];
[output appendString:@"\033[0m"];
} else {
[output insertString:@"\033[33m" atIndex:0];
[output appendString:@"\033[0m"];
}
}
if (self.state == SNTRuleStateAllowTransitive) {
NSDate *date = [NSDate dateWithTimeIntervalSinceReferenceDate:self.timestamp];
[output appendString:[NSString stringWithFormat:@"\nlast access date: %@", [date description]]];
}
return output;
}
#pragma mark Last-access Timestamp
- (void)resetTimestamp {
self.timestamp = (NSUInteger)[[NSDate date] timeIntervalSinceReferenceDate];
}
@end