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 if(!eventResultError) {
315 [self logSuccessForEventNamed:eventName];
317 // Make an Attributes dictionary
318 NSMutableDictionary* eventAttributes = [NSMutableDictionary dictionary];
320 /* if we have underlying errors, capture the chain below the top-most error */
321 NSError *underlyingError = eventResultError.userInfo[NSUnderlyingErrorKey];
322 if ([underlyingError isKindOfClass:[NSError class]]) {
323 NSMutableString *chain = [NSMutableString string];
326 [chain appendFormat:@"%@-%ld:", underlyingError.domain, (long)underlyingError.code];
327 underlyingError = underlyingError.userInfo[NSUnderlyingErrorKey];
328 } while (count++ < 5 && [underlyingError isKindOfClass:[NSError class]]);
330 eventAttributes[SFAnalyticsAttributeErrorUnderlyingChain] = chain;
333 eventAttributes[SFAnalyticsAttributeErrorDomain] = eventResultError.domain;
334 eventAttributes[SFAnalyticsAttributeErrorCode] = @(eventResultError.code);
337 [self logHardFailureForEventNamed:eventName withAttributes:eventAttributes];
339 [self logSoftFailureForEventNamed:eventName withAttributes:eventAttributes];
344 - (void)noteEventNamed:(NSString*)eventName
346 [self logEventNamed:eventName class:SFAnalyticsEventClassNote attributes:nil];
349 - (void)logEventNamed:(NSString*)eventName class:(SFAnalyticsEventClass)class attributes:(NSDictionary*)attributes
352 secerror("SFAnalytics: attempt to log an event with no name");
356 __weak __typeof(self) weakSelf = self;
357 dispatch_sync(_queue, ^{
358 __strong __typeof(self) strongSelf = weakSelf;
359 if (!strongSelf || strongSelf->_disableLogging) {
363 NSDictionary* eventDict = [self eventDictForEventName:eventName withAttributes:attributes eventClass:class];
364 [strongSelf.database addEventDict:eventDict toTable:SFAnalyticsTableAllEvents];
366 if (class == SFAnalyticsEventClassHardFailure) {
367 [strongSelf.database addEventDict:eventDict toTable:SFAnalyticsTableHardFailures];
368 [strongSelf.database incrementHardFailureCountForEventType:eventName];
370 else if (class == SFAnalyticsEventClassSoftFailure) {
371 [strongSelf.database addEventDict:eventDict toTable:SFAnalyticsTableSoftFailures];
372 [strongSelf.database incrementSoftFailureCountForEventType:eventName];
374 else if (class == SFAnalyticsEventClassSuccess || class == SFAnalyticsEventClassNote) {
375 [strongSelf.database incrementSuccessCountForEventType:eventName];
380 - (NSDictionary*)eventDictForEventName:(NSString*)eventName withAttributes:(NSDictionary*)attributes eventClass:(SFAnalyticsEventClass)eventClass
382 NSMutableDictionary* eventDict = attributes ? attributes.mutableCopy : [NSMutableDictionary dictionary];
383 eventDict[SFAnalyticsEventType] = eventName;
384 // our backend wants timestamps in milliseconds
385 eventDict[SFAnalyticsEventTime] = @([[NSDate date] timeIntervalSince1970] * 1000);
386 eventDict[SFAnalyticsEventClassKey] = @(eventClass);
387 [SFAnalytics addOSVersionToEvent:eventDict];
394 - (SFAnalyticsSampler*)addMetricSamplerForName:(NSString *)samplerName withTimeInterval:(NSTimeInterval)timeInterval block:(NSNumber *(^)(void))block
397 secerror("SFAnalytics: cannot add sampler without name");
400 if (timeInterval < 1.0f && timeInterval != SFAnalyticsSamplerIntervalOncePerReport) {
401 secerror("SFAnalytics: cannot add sampler with interval %f", timeInterval);
405 secerror("SFAnalytics: cannot add sampler without block");
409 __block SFAnalyticsSampler* sampler = nil;
411 __weak __typeof(self) weakSelf = self;
412 dispatch_sync(_queue, ^{
413 __strong __typeof(self) strongSelf = weakSelf;
414 if (strongSelf->_samplers[samplerName]) {
415 secerror("SFAnalytics: sampler \"%@\" already exists", samplerName);
417 sampler = [[SFAnalyticsSampler alloc] initWithName:samplerName interval:timeInterval block:block clientClass:[self class]];
418 strongSelf->_samplers[samplerName] = sampler; // If sampler did not init because of bad data this 'removes' it from the dict, so a noop
425 - (SFAnalyticsMultiSampler*)AddMultiSamplerForName:(NSString *)samplerName withTimeInterval:(NSTimeInterval)timeInterval block:(NSDictionary<NSString *,NSNumber *> *(^)(void))block
428 secerror("SFAnalytics: cannot add sampler without name");
431 if (timeInterval < 1.0f && timeInterval != SFAnalyticsSamplerIntervalOncePerReport) {
432 secerror("SFAnalytics: cannot add sampler with interval %f", timeInterval);
436 secerror("SFAnalytics: cannot add sampler without block");
440 __block SFAnalyticsMultiSampler* sampler = nil;
441 __weak __typeof(self) weakSelf = self;
442 dispatch_sync(_queue, ^{
443 __strong __typeof(self) strongSelf = weakSelf;
444 if (strongSelf->_multisamplers[samplerName]) {
445 secerror("SFAnalytics: multisampler \"%@\" already exists", samplerName);
447 sampler = [[SFAnalyticsMultiSampler alloc] initWithName:samplerName interval:timeInterval block:block clientClass:[self class]];
448 strongSelf->_multisamplers[samplerName] = sampler;
456 - (SFAnalyticsSampler*)existingMetricSamplerForName:(NSString *)samplerName
458 __block SFAnalyticsSampler* sampler = nil;
460 __weak __typeof(self) weakSelf = self;
461 dispatch_sync(_queue, ^{
462 __strong __typeof(self) strongSelf = weakSelf;
464 sampler = strongSelf->_samplers[samplerName];
470 - (SFAnalyticsMultiSampler*)existingMultiSamplerForName:(NSString *)samplerName
472 __block SFAnalyticsMultiSampler* sampler = nil;
474 __weak __typeof(self) weakSelf = self;
475 dispatch_sync(_queue, ^{
476 __strong __typeof(self) strongSelf = weakSelf;
478 sampler = strongSelf->_multisamplers[samplerName];
484 - (void)removeMetricSamplerForName:(NSString *)samplerName
487 secerror("Attempt to remove sampler without specifying samplerName");
491 __weak __typeof(self) weakSelf = self;
492 dispatch_async(_queue, ^{
493 __strong __typeof(self) strongSelf = weakSelf;
495 [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
496 [strongSelf->_samplers removeObjectForKey:samplerName];
501 - (void)removeMultiSamplerForName:(NSString *)samplerName
504 secerror("Attempt to remove multisampler without specifying samplerName");
508 __weak __typeof(self) weakSelf = self;
509 dispatch_async(_queue, ^{
510 __strong __typeof(self) strongSelf = weakSelf;
512 [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
513 [strongSelf->_multisamplers removeObjectForKey:samplerName];
518 - (SFAnalyticsActivityTracker*)logSystemMetricsForActivityNamed:(NSString *)eventName withAction:(void (^)(void))action
520 if (![eventName isKindOfClass:[NSString class]]) {
521 secerror("Cannot log system metrics without name");
524 SFAnalyticsActivityTracker* tracker = [[SFAnalyticsActivityTracker alloc] initWithName:eventName clientClass:[self class]];
526 [tracker performAction:action];
531 - (void)logMetric:(NSNumber *)metric withName:(NSString *)metricName
533 [self logMetric:metric withName:metricName oncePerReport:NO];
536 - (void)logMetric:(NSNumber*)metric withName:(NSString*)metricName oncePerReport:(BOOL)once
538 if (![metric isKindOfClass:[NSNumber class]] || ![metricName isKindOfClass:[NSString class]]) {
539 secerror("SFAnalytics: Need a valid result and name to log result");
543 __weak __typeof(self) weakSelf = self;
544 dispatch_async(_queue, ^{
545 __strong __typeof(self) strongSelf = weakSelf;
546 if (strongSelf && !strongSelf->_disableLogging) {
548 [strongSelf.database removeAllSamplesForName:metricName];
550 [strongSelf.database addSample:metric forName:metricName];