]> git.saurik.com Git - apple/security.git/blob - keychain/ckks/CKKSZoneChangeFetcher.m
Security-59306.41.2.tar.gz
[apple/security.git] / keychain / ckks / CKKSZoneChangeFetcher.m
1 /*
2 * Copyright (c) 2017 Apple Inc. All Rights Reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
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
11 * file.
12 *
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.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23
24 #import <Foundation/Foundation.h>
25
26 #if OCTAGON
27
28 #import <dispatch/dispatch.h>
29
30 #import "keychain/ckks/CKKSZoneChangeFetcher.h"
31 #import "keychain/ckks/CKKSFetchAllRecordZoneChangesOperation.h"
32 #import "keychain/ckks/CKKSKeychainView.h"
33 #import "keychain/ckks/CKKSNearFutureScheduler.h"
34 #import "keychain/ckks/CloudKitCategories.h"
35 #import "keychain/ckks/CKKSReachabilityTracker.h"
36 #import "keychain/categories/NSError+UsefulConstructors.h"
37 #import "keychain/analytics/SecEventMetric.h"
38 #import "keychain/analytics/SecMetrics.h"
39 #import "keychain/ot/ObjCImprovements.h"
40
41 CKKSFetchBecause* const CKKSFetchBecauseAPNS = (CKKSFetchBecause*) @"apns";
42 CKKSFetchBecause* const CKKSFetchBecauseAPIFetchRequest = (CKKSFetchBecause*) @"api";
43 CKKSFetchBecause* const CKKSFetchBecauseCurrentItemFetchRequest = (CKKSFetchBecause*) @"currentitemcheck";
44 CKKSFetchBecause* const CKKSFetchBecauseInitialStart = (CKKSFetchBecause*) @"initialfetch";
45 CKKSFetchBecause* const CKKSFetchBecauseSecuritydRestart = (CKKSFetchBecause*) @"restart";
46 CKKSFetchBecause* const CKKSFetchBecausePreviousFetchFailed = (CKKSFetchBecause*) @"fetchfailed";
47 CKKSFetchBecause* const CKKSFetchBecauseNetwork = (CKKSFetchBecause*) @"network";
48 CKKSFetchBecause* const CKKSFetchBecauseKeyHierarchy = (CKKSFetchBecause*) @"keyhierarchy";
49 CKKSFetchBecause* const CKKSFetchBecauseTesting = (CKKSFetchBecause*) @"testing";
50 CKKSFetchBecause* const CKKSFetchBecauseResync = (CKKSFetchBecause*) @"resync";
51 CKKSFetchBecause* const CKKSFetchBecauseMoreComing = (CKKSFetchBecause*) @"more-coming";
52
53 #pragma mark - CKKSZoneChangeFetchDependencyOperation
54 @interface CKKSZoneChangeFetchDependencyOperation : CKKSResultOperation
55 @property (weak) CKKSZoneChangeFetcher* owner;
56 @property NSMutableArray<CKKSZoneChangeFetchDependencyOperation*>* chainDependents;
57 - (void)chainDependency:(CKKSZoneChangeFetchDependencyOperation*)newDependency;
58 @end
59
60 @implementation CKKSZoneChangeFetchDependencyOperation
61 - (instancetype)init {
62 if((self = [super init])) {
63 _chainDependents = [NSMutableArray array];
64 }
65 return self;
66 }
67
68 - (NSError* _Nullable)descriptionError {
69 return [NSError errorWithDomain:CKKSResultDescriptionErrorDomain
70 code:CKKSResultDescriptionPendingSuccessfulFetch
71 description:@"Fetch failed"
72 underlying:self.owner.lastCKFetchError];
73 }
74
75 - (void)chainDependency:(CKKSZoneChangeFetchDependencyOperation*)newDependency {
76 [self addSuccessDependency:newDependency];
77
78 // There's no need to build a chain more than two links long. Move all our children up to depend on the new dependency.
79 for(CKKSZoneChangeFetchDependencyOperation* op in self.chainDependents) {
80 [newDependency.chainDependents addObject:op];
81 [op addSuccessDependency:newDependency];
82 [op removeDependency:self];
83 }
84 [self.chainDependents removeAllObjects];
85 }
86 @end
87
88 #pragma mark - CKKSZoneChangeFetcher
89
90 @interface CKKSZoneChangeFetcher ()
91 @property NSString* name;
92 @property NSOperationQueue* operationQueue;
93 @property dispatch_queue_t queue;
94
95 @property NSError* lastCKFetchError;
96
97 @property NSMapTable<CKRecordZoneID*, id<CKKSChangeFetcherClient>>* clientMap;
98
99 @property CKKSFetchAllRecordZoneChangesOperation* currentFetch;
100 @property CKKSResultOperation* currentProcessResult;
101
102 @property NSMutableSet<CKKSFetchBecause*>* currentFetchReasons;
103 @property NSMutableSet<CKRecordZoneNotification*>* apnsPushes;
104 @property bool newRequests; // true if there's someone pending on successfulFetchDependency
105 @property CKKSZoneChangeFetchDependencyOperation* successfulFetchDependency;
106
107 @property CKKSResultOperation* holdOperation;
108 @end
109
110 @implementation CKKSZoneChangeFetcher
111
112 - (instancetype)initWithContainer:(CKContainer*)container
113 fetchClass:(Class<CKKSFetchRecordZoneChangesOperation>)fetchRecordZoneChangesOperationClass
114 reachabilityTracker:(CKKSReachabilityTracker *)reachabilityTracker
115 {
116 if((self = [super init])) {
117 _container = container;
118 _fetchRecordZoneChangesOperationClass = fetchRecordZoneChangesOperationClass;
119 _reachabilityTracker = reachabilityTracker;
120
121 _currentFetchReasons = [[NSMutableSet alloc] init];
122 _apnsPushes = [[NSMutableSet alloc] init];
123
124 _clientMap = [NSMapTable strongToWeakObjectsMapTable];
125
126 _name = @"zone-change-fetcher";
127 _queue = dispatch_queue_create([_name UTF8String], DISPATCH_QUEUE_SERIAL_WITH_AUTORELEASE_POOL);
128 _operationQueue = [[NSOperationQueue alloc] init];
129 _successfulFetchDependency = [self createSuccesfulFetchDependency];
130
131 _newRequests = false;
132
133 // If we're testing, for the initial delay, use 0.25 second. Otherwise, 2s.
134 dispatch_time_t initialDelay = (SecCKKSReduceRateLimiting() ? 250 * NSEC_PER_MSEC : 2 * NSEC_PER_SEC);
135
136 // If we're testing, for the maximum delay, use 6 second. Otherwise, 2m.
137 dispatch_time_t maximumDelay = (SecCKKSReduceRateLimiting() ? 6 * NSEC_PER_SEC : 120 * NSEC_PER_SEC);
138
139 WEAKIFY(self);
140 _fetchScheduler = [[CKKSNearFutureScheduler alloc] initWithName:@"zone-change-fetch-scheduler"
141 initialDelay:initialDelay
142 expontialBackoff:2
143 maximumDelay:maximumDelay
144 keepProcessAlive:false
145 dependencyDescriptionCode:CKKSResultDescriptionPendingZoneChangeFetchScheduling
146 block:^{
147 STRONGIFY(self);
148 [self maybeCreateNewFetch];
149 }];
150 }
151 return self;
152 }
153
154 - (NSString*)description {
155 NSDate* nextFetchAt = self.fetchScheduler.nextFireTime;
156 if(nextFetchAt) {
157 NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
158 [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
159 return [NSString stringWithFormat: @"<CKKSZoneChangeFetcher(%@): next fetch at %@", self.name, [dateFormatter stringFromDate: nextFetchAt]];
160 } else {
161 return [NSString stringWithFormat: @"<CKKSZoneChangeFetcher(%@): no pending fetches", self.name];
162 }
163 }
164
165 - (void)registerClient:(id<CKKSChangeFetcherClient>)client
166 {
167 @synchronized(self.clientMap) {
168 [self.clientMap setObject:client forKey:client.zoneID];
169 }
170 }
171
172
173 - (CKKSResultOperation*)requestSuccessfulFetch:(CKKSFetchBecause*)why {
174 return [self requestSuccessfulFetchForManyReasons:[NSSet setWithObject:why]];
175 }
176
177 - (CKKSResultOperation*)requestSuccessfulFetchForManyReasons:(NSSet<CKKSFetchBecause*>*)why
178 {
179 return [self requestSuccessfulFetchForManyReasons:why apns:nil];
180 }
181
182 - (CKKSResultOperation*)requestSuccessfulFetchDueToAPNS:(CKRecordZoneNotification*)notification
183 {
184 return [self requestSuccessfulFetchForManyReasons:[NSSet setWithObject:CKKSFetchBecauseAPNS] apns:notification];
185 }
186
187 - (CKKSResultOperation*)requestSuccessfulFetchForManyReasons:(NSSet<CKKSFetchBecause*>*)why apns:(CKRecordZoneNotification*)notification
188 {
189 __block CKKSResultOperation* dependency = nil;
190 dispatch_sync(self.queue, ^{
191 dependency = self.successfulFetchDependency;
192 self.newRequests = true;
193 [self.currentFetchReasons unionSet:why];
194 if(notification) {
195 [self.apnsPushes addObject:notification];
196
197 if(notification.ckksPushTracingEnabled) {
198 // Report that we saw this notification before doing anything else
199 secnotice("ckksfetch", "Submitting initial CKEventMetric due to notification %@", notification);
200
201 CKEventMetric *metric = [[CKEventMetric alloc] initWithEventName:@"APNSPushMetrics"];
202 metric.isPushTriggerFired = true;
203 metric[@"push_token_uuid"] = notification.ckksPushTracingUUID;
204 metric[@"push_received_date"] = notification.ckksPushReceivedDate;
205 metric[@"push_event_name"] = @"CKKS APNS Push Received";
206
207 [self.container submitEventMetric:metric];
208
209 SecEventMetric *metric2 = [[SecEventMetric alloc] initWithEventName:@"APNSPushMetrics"];
210 metric2[@"push_token_uuid"] = notification.ckksPushTracingUUID;
211 metric2[@"push_received_date"] = notification.ckksPushReceivedDate;
212 metric2[@"push_event_name"] = @"CKKS APNS Push Received-webtunnel";
213
214 [[SecMetrics managerObject] submitEvent:metric2];
215
216 }
217
218 }
219
220 [self.fetchScheduler trigger];
221 });
222
223 return dependency;
224 }
225
226 -(void)maybeCreateNewFetchOnQueue {
227 dispatch_assert_queue(self.queue);
228 if(self.newRequests &&
229 (self.currentFetch == nil || [self.currentFetch isFinished]) &&
230 (self.currentProcessResult == nil || [self.currentProcessResult isFinished])) {
231 [self _onqueueCreateNewFetch];
232 }
233 }
234
235 -(void)maybeCreateNewFetch {
236 dispatch_sync(self.queue, ^{
237 [self maybeCreateNewFetchOnQueue];
238 });
239 }
240
241 -(void)_onqueueCreateNewFetch {
242 dispatch_assert_queue(self.queue);
243
244 WEAKIFY(self);
245
246 CKKSZoneChangeFetchDependencyOperation* dependency = self.successfulFetchDependency;
247 NSMutableSet<CKKSFetchBecause*>* lastFetchReasons = self.currentFetchReasons;
248 self.currentFetchReasons = [[NSMutableSet alloc] init];
249
250 NSString *reasonsString = [[lastFetchReasons sortedArrayUsingDescriptors:@[[NSSortDescriptor sortDescriptorWithKey:@"description" ascending:YES]]] componentsJoinedByString:@","];
251
252 secnotice("ckksfetcher", "Starting a new fetch, reasons: %@", reasonsString);
253
254 NSMutableSet<CKRecordZoneNotification*>* lastAPNSPushes = self.apnsPushes;
255 self.apnsPushes = [[NSMutableSet alloc] init];
256
257 CKOperationGroup* operationGroup = [CKOperationGroup CKKSGroupWithName: reasonsString];
258
259 NSMutableArray<id<CKKSChangeFetcherClient>>* clients = [NSMutableArray array];
260 @synchronized(self.clientMap) {
261 for(id<CKKSChangeFetcherClient> client in [self.clientMap objectEnumerator]) {
262 if(client != nil) {
263 [clients addObject:client];
264 }
265 }
266 }
267
268 if(clients.count == 0u) {
269 secnotice("ckksfetcher", "No clients");
270 // Nothing to do, really.
271 }
272
273 CKKSFetchAllRecordZoneChangesOperation* fetchAllChanges = [[CKKSFetchAllRecordZoneChangesOperation alloc] initWithContainer:self.container
274 fetchClass:self.fetchRecordZoneChangesOperationClass
275 clients:clients
276 fetchReasons:lastFetchReasons
277 apnsPushes:lastAPNSPushes
278 forceResync:false
279 ckoperationGroup:operationGroup];
280
281 if ([lastFetchReasons containsObject:CKKSFetchBecauseNetwork]) {
282 secnotice("ckksfetcher", "blocking fetch on network reachability");
283 [fetchAllChanges addNullableDependency: self.reachabilityTracker.reachabilityDependency]; // wait on network, if its unavailable
284 }
285 [fetchAllChanges addNullableDependency: self.holdOperation];
286
287 self.currentProcessResult = [CKKSResultOperation operationWithBlock: ^{
288 STRONGIFY(self);
289 if(!self) {
290 secerror("ckksfetcher: Received a null self pointer; strange.");
291 return;
292 }
293
294 bool attemptAnotherFetch = false;
295 if(fetchAllChanges.error != nil) {
296 secerror("ckksfetcher: Interrogating clients about fetch error: %@", fetchAllChanges.error);
297
298 // Check in with clients: should we keep fetching for them?
299 @synchronized(self.clientMap) {
300 for(CKRecordZoneID* zoneID in fetchAllChanges.fetchedZoneIDs) {
301 id<CKKSChangeFetcherClient> client = [self.clientMap objectForKey:zoneID];
302 if(client) {
303 attemptAnotherFetch |= [client shouldRetryAfterFetchError:fetchAllChanges.error];
304 }
305 }
306 }
307 }
308
309 dispatch_sync(self.queue, ^{
310 self.lastCKFetchError = fetchAllChanges.error;
311
312 if(fetchAllChanges.error == nil) {
313 // success! notify the listeners.
314 [self.operationQueue addOperation: dependency];
315 self.currentFetch = nil;
316
317 // Did new people show up and want another fetch?
318 if(self.newRequests) {
319 [self.fetchScheduler trigger];
320 }
321 } else {
322 // The operation errored. Chain the dependency on the current one...
323 [dependency chainDependency:self.successfulFetchDependency];
324 [self.operationQueue addOperation: dependency];
325
326 if(!attemptAnotherFetch) {
327 secerror("ckksfetcher: All clients thought %@ is a fatal error. Not restarting fetch.", fetchAllChanges.error);
328 return;
329 }
330
331 // And in a bit, try the fetch again.
332 NSTimeInterval delay = CKRetryAfterSecondsForError(fetchAllChanges.error);
333 if (delay) {
334 secnotice("ckksfetcher", "Fetch failed with rate-limiting error, restarting in %.1f seconds: %@", delay, fetchAllChanges.error);
335 [self.fetchScheduler waitUntil:NSEC_PER_SEC * delay];
336 } else {
337 secnotice("ckksfetcher", "Fetch failed with error, restarting soon: %@", fetchAllChanges.error);
338 }
339
340 // Add the failed fetch reasons to the new fetch reasons
341 [self.currentFetchReasons unionSet:lastFetchReasons];
342 [self.apnsPushes unionSet:lastAPNSPushes];
343
344 // If its a network error, make next try depend on network availability
345 if ([self.reachabilityTracker isNetworkError:fetchAllChanges.error]) {
346 [self.currentFetchReasons addObject:CKKSFetchBecauseNetwork];
347 } else {
348 [self.currentFetchReasons addObject:CKKSFetchBecausePreviousFetchFailed];
349 }
350 self.newRequests = true;
351 [self.fetchScheduler trigger];
352 }
353 });
354 }];
355
356 // creata a new fetch dependency, for all those who come in while this operation is executing
357 self.newRequests = false;
358 self.successfulFetchDependency = [self createSuccesfulFetchDependency];
359
360 // now let new new fetch go and process it's results
361 self.currentProcessResult.name = @"zone-change-fetcher-worker";
362 [self.currentProcessResult addDependency: fetchAllChanges];
363
364 [self.operationQueue addOperation:self.currentProcessResult];
365
366 self.currentFetch = fetchAllChanges;
367 [self.operationQueue addOperation:self.currentFetch];
368 }
369
370 -(CKKSZoneChangeFetchDependencyOperation*)createSuccesfulFetchDependency {
371 CKKSZoneChangeFetchDependencyOperation* dep = [[CKKSZoneChangeFetchDependencyOperation alloc] init];
372
373 dep.name = @"successful-fetch-dependency";
374 dep.descriptionErrorCode = CKKSResultDescriptionPendingSuccessfulFetch;
375 dep.owner = self;
376
377 return dep;
378 }
379
380 - (void)holdFetchesUntil:(CKKSResultOperation*)holdOperation {
381 self.holdOperation = holdOperation;
382 }
383
384 -(void)cancel {
385 [self.fetchScheduler cancel];
386 }
387
388 @end
389
390 #endif
391
392