2 * Copyright (c) 2017 Apple Inc. All Rights Reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
21 * @APPLE_LICENSE_HEADER_END@
26 #import "SFAnalytics+Internal.h"
27 #import "SFAnalyticsDefines.h"
28 #import "SFAnalyticsActivityTracker+Internal.h"
29 #import "SFAnalyticsSampler+Internal.h"
30 #import "SFAnalyticsMultiSampler+Internal.h"
31 #import "SFAnalyticsSQLiteStore.h"
32 #import "utilities/debugging.h"
33 #import <utilities/SecFileLocations.h>
34 #import <objc/runtime.h>
36 #import <CoreFoundation/CFPriv.h>
38 // SFAnalyticsDefines constants
39 NSString* const SFAnalyticsTableSuccessCount = @"success_count";
40 NSString* const SFAnalyticsTableHardFailures = @"hard_failures";
41 NSString* const SFAnalyticsTableSoftFailures = @"soft_failures";
42 NSString* const SFAnalyticsTableSamples = @"samples";
43 NSString* const SFAnalyticsTableAllEvents = @"all_events";
45 NSString* const SFAnalyticsColumnSuccessCount = @"success_count";
46 NSString* const SFAnalyticsColumnHardFailureCount = @"hard_failure_count";
47 NSString* const SFAnalyticsColumnSoftFailureCount = @"soft_failure_count";
48 NSString* const SFAnalyticsColumnSampleValue = @"value";
49 NSString* const SFAnalyticsColumnSampleName = @"name";
51 NSString* const SFAnalyticsEventTime = @"eventTime";
52 NSString* const SFAnalyticsEventType = @"eventType";
53 NSString* const SFAnalyticsEventClassKey = @"eventClass";
55 NSString* const SFAnalyticsAttributeErrorUnderlyingChain = @"errorChain";
56 NSString* const SFAnalyticsAttributeErrorDomain = @"errorDomain";
57 NSString* const SFAnalyticsAttributeErrorCode = @"errorCode";
59 NSString* const SFAnalyticsUserDefaultsSuite = @"com.apple.security.analytics";
61 char* const SFAnalyticsFireSamplersNotification = "com.apple.security.sfanalytics.samplers";
63 NSString* const SFAnalyticsTopicKeySync = @"KeySyncTopic";
64 NSString* const SFAnaltyicsTopicTrust = @"TrustTopic";
66 NSString* const SFAnalyticsTableSchema = @"CREATE TABLE IF NOT EXISTS hard_failures (\n"
67 @"id INTEGER PRIMARY KEY AUTOINCREMENT,\n"
71 @"CREATE TRIGGER IF NOT EXISTS maintain_ring_buffer_hard_failures AFTER INSERT ON hard_failures\n"
73 @"DELETE FROM hard_failures WHERE id != NEW.id AND id % 1000 = NEW.id % 1000;\n"
75 @"CREATE TABLE IF NOT EXISTS soft_failures (\n"
76 @"id INTEGER PRIMARY KEY AUTOINCREMENT,\n"
80 @"CREATE TRIGGER IF NOT EXISTS maintain_ring_buffer_soft_failures AFTER INSERT ON soft_failures\n"
82 @"DELETE FROM soft_failures WHERE id != NEW.id AND id % 1000 = NEW.id % 1000;\n"
84 @"CREATE TABLE IF NOT EXISTS all_events (\n"
85 @"id INTEGER PRIMARY KEY AUTOINCREMENT,\n"
89 @"CREATE TRIGGER IF NOT EXISTS maintain_ring_buffer_all_events AFTER INSERT ON all_events\n"
91 @"DELETE FROM all_events WHERE id != NEW.id AND id % 10000 = NEW.id % 10000;\n"
93 @"CREATE TABLE IF NOT EXISTS samples (\n"
94 @"id INTEGER PRIMARY KEY AUTOINCREMENT,\n"
99 @"CREATE TRIGGER IF NOT EXISTS maintain_ring_buffer_samples AFTER INSERT ON samples\n"
101 @"DELETE FROM samples WHERE id != NEW.id AND id % 1000 = NEW.id % 1000;\n"
103 @"CREATE TABLE IF NOT EXISTS success_count (\n"
104 @"event_type STRING PRIMARY KEY,\n"
105 @"success_count INTEGER,\n"
106 @"hard_failure_count INTEGER,\n"
107 @"soft_failure_count INTEGER\n"
110 NSUInteger const SFAnalyticsMaxEventsToReport = 1000;
112 NSString* const SFAnalyticsErrorDomain = @"com.apple.security.sfanalytics";
115 NSString* const SFAnalyticsEventBuild = @"build";
116 NSString* const SFAnalyticsEventProduct = @"product";
117 const NSTimeInterval SFAnalyticsSamplerIntervalOncePerReport = -1.0;
119 @interface SFAnalytics ()
120 @property (nonatomic) SFAnalyticsSQLiteStore* database;
121 @property (nonatomic) dispatch_queue_t queue;
124 @implementation SFAnalytics {
125 SFAnalyticsSQLiteStore* _database;
126 dispatch_queue_t _queue;
127 NSMutableDictionary<NSString*, SFAnalyticsSampler*>* _samplers;
128 NSMutableDictionary<NSString*, SFAnalyticsMultiSampler*>* _multisamplers;
129 unsigned int _disableLogging:1;
132 + (instancetype)logger
134 #if TARGET_OS_SIMULATOR
138 if (self == [SFAnalytics class]) {
139 secerror("attempt to instatiate abstract class SFAnalytics");
143 SFAnalytics* logger = nil;
144 @synchronized(self) {
145 logger = objc_getAssociatedObject(self, "SFAnalyticsInstance");
147 logger = [[self alloc] init];
148 objc_setAssociatedObject(self, "SFAnalyticsInstance", logger, OBJC_ASSOCIATION_RETAIN);
152 [logger database]; // For unit testing so there's always a database. DB shouldn't be nilled in production though
157 + (NSString*)databasePath
162 + (NSString *)defaultAnalyticsDatabasePath:(NSString *)basename
164 WithPathInKeychainDirectory(CFSTR("Analytics"), ^(const char *path) {
166 mode_t permissions = 0775;
168 mode_t permissions = 0700;
169 #endif // TARGET_OS_IPHONE
170 int ret = mkpath_np(path, permissions);
171 if (!(ret == 0 || ret == EEXIST)) {
172 secerror("could not create path: %s (%s)", path, strerror(ret));
174 chmod(path, permissions);
176 NSString *path = [NSString stringWithFormat:@"Analytics/%@.db", basename];
177 return [(__bridge_transfer NSURL*)SecCopyURLForFileInKeychainDirectory((__bridge CFStringRef)path) path];
180 + (NSInteger)fuzzyDaysSinceDate:(NSDate*)date
182 // Sentinel: it didn't happen at all
187 // Sentinel: it happened but we don't know when because the date doesn't make sense
188 // Magic number represents January 1, 2017.
189 if ([date compare:[NSDate dateWithTimeIntervalSince1970:1483228800]] == NSOrderedAscending) {
193 NSInteger secondsPerDay = 60 * 60 * 24;
195 NSTimeInterval timeIntervalSinceDate = [[NSDate date] timeIntervalSinceDate:date];
196 if (timeIntervalSinceDate < secondsPerDay) {
199 else if (timeIntervalSinceDate < (secondsPerDay * 7)) {
202 else if (timeIntervalSinceDate < (secondsPerDay * 30)) {
205 else if (timeIntervalSinceDate < (secondsPerDay * 365)) {
213 // Instantiate lazily so unit tests can have clean databases each
214 - (SFAnalyticsSQLiteStore*)database
217 _database = [SFAnalyticsSQLiteStore storeWithPath:self.class.databasePath schema:SFAnalyticsTableSchema];
219 seccritical("Did not get a database! (Client %@)", NSStringFromClass([self class]));
227 [_samplers removeAllObjects];
228 [_multisamplers removeAllObjects];
230 __weak __typeof(self) weakSelf = self;
231 dispatch_sync(_queue, ^{
232 __strong __typeof(self) strongSelf = weakSelf;
234 [strongSelf.database close];
235 strongSelf->_database = nil;
240 - (void)setDateProperty:(NSDate*)date forKey:(NSString*)key
242 __weak __typeof(self) weakSelf = self;
243 dispatch_sync(_queue, ^{
244 __strong __typeof(self) strongSelf = weakSelf;
246 [strongSelf.database setDateProperty:date forKey:key];
251 - (NSDate*)datePropertyForKey:(NSString*)key
253 __block NSDate* result = nil;
254 __weak __typeof(self) weakSelf = self;
255 dispatch_sync(_queue, ^{
256 __strong __typeof(self) strongSelf = weakSelf;
258 result = [strongSelf.database datePropertyForKey:key];
264 + (void)addOSVersionToEvent:(NSMutableDictionary*)eventDict {
265 static dispatch_once_t onceToken;
266 static NSString *build = NULL;
267 static NSString *product = NULL;
268 dispatch_once(&onceToken, ^{
269 NSDictionary *version = CFBridgingRelease(_CFCopySystemVersionDictionary());
272 build = version[(__bridge NSString *)_kCFSystemVersionBuildVersionKey];
273 product = version[(__bridge NSString *)_kCFSystemVersionProductNameKey];
276 eventDict[SFAnalyticsEventBuild] = build;
279 eventDict[SFAnalyticsEventProduct] = product;
285 if (self = [super init]) {
286 _queue = dispatch_queue_create("SFAnalytics data access queue", DISPATCH_QUEUE_SERIAL_WITH_AUTORELEASE_POOL);
287 _samplers = [NSMutableDictionary<NSString*, SFAnalyticsSampler*> new];
288 _multisamplers = [NSMutableDictionary<NSString*, SFAnalyticsMultiSampler*> new];
289 [self database]; // for side effect of instantiating DB object. Used for testing.
295 // MARK: Event logging
297 - (void)logSuccessForEventNamed:(NSString*)eventName
299 [self logEventNamed:eventName class:SFAnalyticsEventClassSuccess attributes:nil];
302 - (void)logHardFailureForEventNamed:(NSString*)eventName withAttributes:(NSDictionary*)attributes
304 [self logEventNamed:eventName class:SFAnalyticsEventClassHardFailure attributes:attributes];
307 - (void)logSoftFailureForEventNamed:(NSString*)eventName withAttributes:(NSDictionary*)attributes
309 [self logEventNamed:eventName class:SFAnalyticsEventClassSoftFailure attributes:attributes];
312 - (void)logResultForEvent:(NSString*)eventName hardFailure:(bool)hardFailure result:(NSError*)eventResultError
314 [self logResultForEvent:eventName hardFailure:hardFailure result:eventResultError withAttributes:nil];
317 - (void)logResultForEvent:(NSString*)eventName hardFailure:(bool)hardFailure result:(NSError*)eventResultError withAttributes:(NSDictionary*)attributes
319 if(!eventResultError) {
320 [self logSuccessForEventNamed:eventName];
322 // Make an Attributes dictionary
323 NSMutableDictionary* eventAttributes = nil;
325 eventAttributes = [attributes mutableCopy];
327 eventAttributes = [NSMutableDictionary dictionary];
330 /* if we have underlying errors, capture the chain below the top-most error */
331 NSError *underlyingError = eventResultError.userInfo[NSUnderlyingErrorKey];
332 if ([underlyingError isKindOfClass:[NSError class]]) {
333 NSMutableString *chain = [NSMutableString string];
336 [chain appendFormat:@"%@-%ld:", underlyingError.domain, (long)underlyingError.code];
337 underlyingError = underlyingError.userInfo[NSUnderlyingErrorKey];
338 } while (count++ < 5 && [underlyingError isKindOfClass:[NSError class]]);
340 eventAttributes[SFAnalyticsAttributeErrorUnderlyingChain] = chain;
343 eventAttributes[SFAnalyticsAttributeErrorDomain] = eventResultError.domain;
344 eventAttributes[SFAnalyticsAttributeErrorCode] = @(eventResultError.code);
347 [self logHardFailureForEventNamed:eventName withAttributes:eventAttributes];
349 [self logSoftFailureForEventNamed:eventName withAttributes:eventAttributes];
354 - (void)noteEventNamed:(NSString*)eventName
356 [self logEventNamed:eventName class:SFAnalyticsEventClassNote attributes:nil];
359 - (void)logEventNamed:(NSString*)eventName class:(SFAnalyticsEventClass)class attributes:(NSDictionary*)attributes
362 secerror("SFAnalytics: attempt to log an event with no name");
366 __weak __typeof(self) weakSelf = self;
367 dispatch_sync(_queue, ^{
368 __strong __typeof(self) strongSelf = weakSelf;
369 if (!strongSelf || strongSelf->_disableLogging) {
373 NSDictionary* eventDict = [self eventDictForEventName:eventName withAttributes:attributes eventClass:class];
374 [strongSelf.database addEventDict:eventDict toTable:SFAnalyticsTableAllEvents];
376 if (class == SFAnalyticsEventClassHardFailure) {
377 [strongSelf.database addEventDict:eventDict toTable:SFAnalyticsTableHardFailures];
378 [strongSelf.database incrementHardFailureCountForEventType:eventName];
380 else if (class == SFAnalyticsEventClassSoftFailure) {
381 [strongSelf.database addEventDict:eventDict toTable:SFAnalyticsTableSoftFailures];
382 [strongSelf.database incrementSoftFailureCountForEventType:eventName];
384 else if (class == SFAnalyticsEventClassSuccess || class == SFAnalyticsEventClassNote) {
385 [strongSelf.database incrementSuccessCountForEventType:eventName];
390 - (NSDictionary*)eventDictForEventName:(NSString*)eventName withAttributes:(NSDictionary*)attributes eventClass:(SFAnalyticsEventClass)eventClass
392 NSMutableDictionary* eventDict = attributes ? attributes.mutableCopy : [NSMutableDictionary dictionary];
393 eventDict[SFAnalyticsEventType] = eventName;
394 // our backend wants timestamps in milliseconds
395 eventDict[SFAnalyticsEventTime] = @([[NSDate date] timeIntervalSince1970] * 1000);
396 eventDict[SFAnalyticsEventClassKey] = @(eventClass);
397 [SFAnalytics addOSVersionToEvent:eventDict];
404 - (SFAnalyticsSampler*)addMetricSamplerForName:(NSString *)samplerName withTimeInterval:(NSTimeInterval)timeInterval block:(NSNumber *(^)(void))block
407 secerror("SFAnalytics: cannot add sampler without name");
410 if (timeInterval < 1.0f && timeInterval != SFAnalyticsSamplerIntervalOncePerReport) {
411 secerror("SFAnalytics: cannot add sampler with interval %f", timeInterval);
415 secerror("SFAnalytics: cannot add sampler without block");
419 __block SFAnalyticsSampler* sampler = nil;
421 __weak __typeof(self) weakSelf = self;
422 dispatch_sync(_queue, ^{
423 __strong __typeof(self) strongSelf = weakSelf;
424 if (strongSelf->_samplers[samplerName]) {
425 secerror("SFAnalytics: sampler \"%@\" already exists", samplerName);
427 sampler = [[SFAnalyticsSampler alloc] initWithName:samplerName interval:timeInterval block:block clientClass:[self class]];
428 strongSelf->_samplers[samplerName] = sampler; // If sampler did not init because of bad data this 'removes' it from the dict, so a noop
435 - (SFAnalyticsMultiSampler*)AddMultiSamplerForName:(NSString *)samplerName withTimeInterval:(NSTimeInterval)timeInterval block:(NSDictionary<NSString *,NSNumber *> *(^)(void))block
438 secerror("SFAnalytics: cannot add sampler without name");
441 if (timeInterval < 1.0f && timeInterval != SFAnalyticsSamplerIntervalOncePerReport) {
442 secerror("SFAnalytics: cannot add sampler with interval %f", timeInterval);
446 secerror("SFAnalytics: cannot add sampler without block");
450 __block SFAnalyticsMultiSampler* sampler = nil;
451 __weak __typeof(self) weakSelf = self;
452 dispatch_sync(_queue, ^{
453 __strong __typeof(self) strongSelf = weakSelf;
454 if (strongSelf->_multisamplers[samplerName]) {
455 secerror("SFAnalytics: multisampler \"%@\" already exists", samplerName);
457 sampler = [[SFAnalyticsMultiSampler alloc] initWithName:samplerName interval:timeInterval block:block clientClass:[self class]];
458 strongSelf->_multisamplers[samplerName] = sampler;
466 - (SFAnalyticsSampler*)existingMetricSamplerForName:(NSString *)samplerName
468 __block SFAnalyticsSampler* sampler = nil;
470 __weak __typeof(self) weakSelf = self;
471 dispatch_sync(_queue, ^{
472 __strong __typeof(self) strongSelf = weakSelf;
474 sampler = strongSelf->_samplers[samplerName];
480 - (SFAnalyticsMultiSampler*)existingMultiSamplerForName:(NSString *)samplerName
482 __block SFAnalyticsMultiSampler* sampler = nil;
484 __weak __typeof(self) weakSelf = self;
485 dispatch_sync(_queue, ^{
486 __strong __typeof(self) strongSelf = weakSelf;
488 sampler = strongSelf->_multisamplers[samplerName];
494 - (void)removeMetricSamplerForName:(NSString *)samplerName
497 secerror("Attempt to remove sampler without specifying samplerName");
501 __weak __typeof(self) weakSelf = self;
502 dispatch_async(_queue, ^{
503 __strong __typeof(self) strongSelf = weakSelf;
505 [strongSelf->_samplers[samplerName] pauseSampling]; // when dealloced it would also stop, but we're not sure when that is so let's stop it right away
506 [strongSelf->_samplers removeObjectForKey:samplerName];
511 - (void)removeMultiSamplerForName:(NSString *)samplerName
514 secerror("Attempt to remove multisampler without specifying samplerName");
518 __weak __typeof(self) weakSelf = self;
519 dispatch_async(_queue, ^{
520 __strong __typeof(self) strongSelf = weakSelf;
522 [strongSelf->_multisamplers[samplerName] pauseSampling]; // when dealloced it would also stop, but we're not sure when that is so let's stop it right away
523 [strongSelf->_multisamplers removeObjectForKey:samplerName];
528 - (SFAnalyticsActivityTracker*)logSystemMetricsForActivityNamed:(NSString *)eventName withAction:(void (^)(void))action
530 if (![eventName isKindOfClass:[NSString class]]) {
531 secerror("Cannot log system metrics without name");
534 SFAnalyticsActivityTracker* tracker = [[SFAnalyticsActivityTracker alloc] initWithName:eventName clientClass:[self class]];
536 [tracker performAction:action];
541 - (void)logMetric:(NSNumber *)metric withName:(NSString *)metricName
543 [self logMetric:metric withName:metricName oncePerReport:NO];
546 - (void)logMetric:(NSNumber*)metric withName:(NSString*)metricName oncePerReport:(BOOL)once
548 if (![metric isKindOfClass:[NSNumber class]] || ![metricName isKindOfClass:[NSString class]]) {
549 secerror("SFAnalytics: Need a valid result and name to log result");
553 __weak __typeof(self) weakSelf = self;
554 dispatch_async(_queue, ^{
555 __strong __typeof(self) strongSelf = weakSelf;
556 if (strongSelf && !strongSelf->_disableLogging) {
558 [strongSelf.database removeAllSamplesForName:metricName];
560 [strongSelf.database addSample:metric forName:metricName];