2 * Copyright (c) 2012-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 #include "debugging.h"
29 #include <sqlite3_private.h>
30 #include <CoreFoundation/CoreFoundation.h>
34 #include <AssertMacros.h>
35 #include "SecCFWrappers.h"
36 #include "SecCFError.h"
37 #include "SecIOFormat.h"
39 #include "Security/SecBase.h"
40 #include "SecAutorelease.h"
44 // Architecturally inverted files
45 // These are in SecureObjectSync but utilities depends on them
46 // <rdar://problem/20802079> Fix layer violation (SOSDigestVector, SOSManifest, SecDB.c)
48 #include <Security/SecureObjectSync/SOSDigestVector.h>
49 #include <Security/SecureObjectSync/SOSManifest.h>
51 struct __OpaqueSecDbStatement
{
54 SecDbConnectionRef dbconn
;
58 struct __OpaqueSecDbConnection
{
61 //CFMutableDictionaryRef statements;
63 SecDbRef db
; // NONRETAINED, since db or block retains us
66 SecDbTransactionSource source
;
68 int maybeCorruptedCode
;
70 CFErrorRef corruptionError
;
72 // Pending deletions and additions for the current transaction
73 // Entires are either:
74 // 1) a CFArrayRef of 1 element representing a deletion,
75 // 2) a CFArrayRef of 2 elements representing the element 0 having been replaced with element 1
76 // 3) a CFTypeRef that is not a CFArrayRef, representing an add of the element in question.
77 CFMutableArrayRef changes
;
80 struct __OpaqueSecDb
{
84 dispatch_queue_t queue
;
85 dispatch_queue_t commitQueue
;
86 CFMutableArrayRef connections
;
87 dispatch_semaphore_t write_semaphore
;
88 dispatch_semaphore_t read_semaphore
;
90 bool (^opened
)(SecDbRef db
, SecDbConnectionRef dbconn
, bool didCreate
, bool *callMeAgainForNextConnection
, CFErrorRef
*error
);
91 bool callOpenedHandlerForNextConnection
;
92 CFMutableArrayRef notifyPhase
; /* array of SecDBNotifyBlock */
93 mode_t mode
; /* database file permissions */
94 bool readWrite
; /* open database read-write */
95 bool allowRepair
; /* allow database repair */
96 bool useWAL
; /* use WAL mode */
97 bool useRobotVacuum
; /* use if SecDB should manage vacuum behind your back */
98 uint8_t maxIdleHandles
;
99 void (^corruptionReset
)(void);
102 // MARK: Error domains and error helper functions
104 CFStringRef kSecDbErrorDomain
= CFSTR("com.apple.utilities.sqlite3");
106 bool SecDbError(int sql_code
, CFErrorRef
*error
, CFStringRef format
, ...) {
107 if (sql_code
== SQLITE_OK
) return true;
111 CFIndex code
= sql_code
;
112 CFErrorRef previousError
= *error
;
115 va_start(args
, format
);
116 SecCFCreateErrorWithFormatAndArguments(code
, kSecDbErrorDomain
, previousError
, error
, NULL
, format
, args
);
122 bool SecDbErrorWithDb(int sql_code
, sqlite3
*db
, CFErrorRef
*error
, CFStringRef format
, ...) {
123 if (sql_code
== SQLITE_OK
) return true;
126 va_start(args
, format
);
127 CFStringRef message
= CFStringCreateWithFormatAndArguments(kCFAllocatorDefault
, NULL
, format
, args
);
129 CFStringRef errno_code
= NULL
;
131 if (sql_code
== SQLITE_CANTOPEN
) {
132 int errno_number
= sqlite3_system_errno(db
);
133 errno_code
= CFStringCreateWithFormat(NULL
, NULL
, CFSTR("%d"), errno_number
);
135 errno_code
= CFRetain(CFSTR(""));
138 int extended_code
= sqlite3_extended_errcode(db
);
139 if (sql_code
== extended_code
)
140 SecDbError(sql_code
, error
, CFSTR("%@: [%d]%@ %s"), message
, sql_code
, errno_code
, sqlite3_errmsg(db
));
142 SecDbError(sql_code
, error
, CFSTR("%@: [%d->%d]%@ %s"), message
, sql_code
, extended_code
, errno_code
, sqlite3_errmsg(db
));
143 CFReleaseSafe(message
);
144 CFReleaseSafe(errno_code
);
149 bool SecDbErrorWithStmt(int sql_code
, sqlite3_stmt
*stmt
, CFErrorRef
*error
, CFStringRef format
, ...) {
150 if (sql_code
== SQLITE_OK
) return true;
153 va_start(args
, format
);
154 CFStringRef message
= CFStringCreateWithFormatAndArguments(kCFAllocatorDefault
, NULL
, format
, args
);
157 sqlite3
*db
= sqlite3_db_handle(stmt
);
158 const char *sql
= sqlite3_sql(stmt
);
159 int extended_code
= sqlite3_extended_errcode(db
);
160 if (sql_code
== extended_code
)
161 SecDbError(sql_code
, error
, CFSTR("%@: [%d] %s sql: %s"), message
, sql_code
, sqlite3_errmsg(db
), sql
);
163 SecDbError(sql_code
, error
, CFSTR("%@: [%d->%d] %s sql: %s"), message
, sql_code
, extended_code
, sqlite3_errmsg(db
), sql
);
164 CFReleaseSafe(message
);
169 // A callback for the sqlite3_log() interface.
170 static void sqlite3Log(void *pArg
, int iErrCode
, const char *zMsg
){
171 secdebug("sqlite3", "(%d) %s", iErrCode
, zMsg
);
174 void _SecDbServerSetup(void)
176 static dispatch_once_t onceToken
;
177 dispatch_once(&onceToken
, ^{
178 int rx
= sqlite3_config(SQLITE_CONFIG_LOG
, sqlite3Log
, NULL
);
179 if (SQLITE_OK
!= rx
) {
180 secwarning("Could not set up sqlite global error logging to syslog: %d", rx
);
187 // MARK: Static helper functions
189 static bool SecDbOpenHandle(SecDbConnectionRef dbconn
, bool *created
, CFErrorRef
*error
);
190 static bool SecDbHandleCorrupt(SecDbConnectionRef dbconn
, int rc
, CFErrorRef
*error
);
193 #pragma mark SecDbRef
196 SecDbCopyFormatDescription(CFTypeRef value
, CFDictionaryRef formatOptions
)
198 SecDbRef db
= (SecDbRef
)value
;
199 return CFStringCreateWithFormat(kCFAllocatorDefault
, NULL
, CFSTR("<SecDb path:%@ connections: %@>"), db
->db_path
, db
->connections
);
204 SecDbDestroy(CFTypeRef value
)
206 SecDbRef db
= (SecDbRef
)value
;
207 CFReleaseNull(db
->connections
);
208 CFReleaseNull(db
->db_path
);
210 dispatch_release(db
->queue
);
213 if (db
->commitQueue
) {
214 dispatch_release(db
->commitQueue
);
215 db
->commitQueue
= NULL
;
217 if (db
->read_semaphore
) {
218 dispatch_release(db
->read_semaphore
);
219 db
->read_semaphore
= NULL
;
221 if (db
->write_semaphore
) {
222 dispatch_release(db
->write_semaphore
);
223 db
->write_semaphore
= NULL
;
226 Block_release(db
->opened
);
229 CFReleaseNull(db
->notifyPhase
);
235 SecDbCreate(CFStringRef dbName
, mode_t mode
, bool readWrite
, bool allowRepair
, bool useWAL
, bool useRobotVacuum
, uint8_t maxIdleHandles
,
236 bool (^opened
)(SecDbRef db
, SecDbConnectionRef dbconn
, bool didCreate
, bool *callMeAgainForNextConnection
, CFErrorRef
*error
))
240 db
= CFTypeAllocate(SecDb
, struct __OpaqueSecDb
, kCFAllocatorDefault
);
241 require(db
!= NULL
, done
);
243 CFStringPerformWithCString(dbName
, ^(const char *dbNameStr
) {
244 db
->queue
= dispatch_queue_create(dbNameStr
, DISPATCH_QUEUE_SERIAL
);
246 CFStringRef commitQueueStr
= CFStringCreateWithFormat(kCFAllocatorDefault
, NULL
, CFSTR("%@-commit"), dbName
);
247 CFStringPerformWithCString(commitQueueStr
, ^(const char *cqNameStr
) {
248 db
->commitQueue
= dispatch_queue_create(cqNameStr
, DISPATCH_QUEUE_CONCURRENT
);
250 CFReleaseNull(commitQueueStr
);
251 db
->read_semaphore
= dispatch_semaphore_create(kSecDbMaxReaders
);
252 db
->write_semaphore
= dispatch_semaphore_create(kSecDbMaxWriters
);
253 db
->connections
= CFArrayCreateMutableForCFTypes(kCFAllocatorDefault
);
254 db
->opened
= opened
? Block_copy(opened
) : NULL
;
255 if (getenv("__OSINSTALL_ENVIRONMENT") != NULL
) {
256 // TODO: Move this code out of this layer
257 secinfo("#SecDB", "SecDB: running from installer");
259 db
->db_path
= CFSTR("file::memory:?cache=shared");
261 db
->db_path
= CFStringCreateCopy(kCFAllocatorDefault
, dbName
);
264 db
->readWrite
= readWrite
;
265 db
->allowRepair
= allowRepair
;
267 db
->useRobotVacuum
= useRobotVacuum
;
268 db
->maxIdleHandles
= maxIdleHandles
;
269 db
->corruptionReset
= NULL
;
276 SecDbIdleConnectionCount(SecDbRef db
) {
277 __block CFIndex count
= 0;
278 dispatch_sync(db
->queue
, ^{
279 count
= CFArrayGetCount(db
->connections
);
284 void SecDbAddNotifyPhaseBlock(SecDbRef db
, SecDBNotifyBlock notifyPhase
)
286 // SecDbNotifyPhase seems to mostly be called on the db's commitQueue, and not the db's queue. Therefore, protect the array with that queue.
287 dispatch_sync(db
->commitQueue
, ^{
288 SecDBNotifyBlock block
= Block_copy(notifyPhase
); /* Force the block off the stack */
289 if (db
->notifyPhase
== NULL
) {
290 db
->notifyPhase
= CFArrayCreateMutableForCFTypes(NULL
);
292 CFArrayAppendValue(db
->notifyPhase
, block
);
293 Block_release(block
);
297 static void SecDbNotifyPhase(SecDbConnectionRef dbconn
, SecDbTransactionPhase phase
) {
298 if (CFArrayGetCount(dbconn
->changes
)) {
299 CFArrayRef changes
= dbconn
->changes
;
300 dbconn
->changes
= CFArrayCreateMutableForCFTypes(kCFAllocatorDefault
);
301 if (dbconn
->db
->notifyPhase
) {
302 CFArrayForEach(dbconn
->db
->notifyPhase
, ^(const void *value
) {
303 SecDBNotifyBlock notifyBlock
= (SecDBNotifyBlock
)value
;
304 notifyBlock(dbconn
, phase
, dbconn
->source
, changes
);
307 CFReleaseSafe(changes
);
311 static void SecDbOnNotify(SecDbConnectionRef dbconn
, void (^perform
)(void)) {
315 CFStringRef
SecDbGetPath(SecDbRef db
) {
324 #pragma mark SecDbConnectionRef
326 static bool SecDbCheckCorrupted(SecDbConnectionRef dbconn
)
328 __block
bool checkDidRun
= false;
329 __block
bool isCorrupted
= false;
330 __block CFErrorRef error
= NULL
;
331 SecDbPrepare(dbconn
, CFSTR("PRAGMA integrity_check"), &error
, ^(sqlite3_stmt
*stmt
) {
332 SecDbStep(dbconn
, stmt
, &error
, ^(bool *stop
) {
333 const char * result
= (const char*)sqlite3_column_text(stmt
, 0);
334 if (!result
|| strncasecmp(result
, "ok", 3) != 0) {
336 secerror("SecDBCheckCorrupted integrity_check returned %s", (result
) ? result
: "NULL");
342 // An error occurred in SecDbPrepare before we could run the block.
344 CFIndex code
= CFErrorGetCode(error
);
345 if (SQLITE_CORRUPT
== code
|| SQLITE_NOTADB
== code
) {
348 secinfo("#SecDB", "#SecDB warning error %{public}@ when running integrity check", error
);
350 // We don't have an error ref if SecDbPrepare has called SecDbConnectionCheckCode,
351 // which then called SecDbHandleCorrupt. That code path is only entered when the
352 // original error was SQLITE_CORRUPT or SQLITE_NOTADB. On other errors, the
353 // CFErrorRef is not cleared and we can just check the code above.
355 secinfo("#SecDB", "#SecDB warning: failed to run integrity check due to corruption");
360 secerror("SecDBCheckCorrupted ran integrity_check, and that didn't return ok");
362 secerror("SecDBCheckCorrupted failed to run integrity check");
365 CFReleaseNull(error
);
370 static bool SecDbDidCreateFirstConnection(SecDbConnectionRef dbconn
, bool didCreate
, CFErrorRef
*error
)
372 secinfo("#SecDB", "#SecDB starting maintenance");
375 // Historical note: this used to check for integrity but that became too slow and caused panics at boot.
376 // Now, just react to SQLite errors when doing an operation. If file on disk is borked it'll tell us right away.
378 if (!dbconn
->isCorrupted
&& dbconn
->db
->opened
) {
379 CFErrorRef localError
= NULL
;
381 dbconn
->db
->callOpenedHandlerForNextConnection
= false;
382 ok
= dbconn
->db
->opened(dbconn
->db
, dbconn
, didCreate
, &dbconn
->db
->callOpenedHandlerForNextConnection
, &localError
);
385 secerror("opened block failed: %@", localError
);
387 if (!dbconn
->isCorrupted
&& error
&& *error
== NULL
) {
392 secerror("opened block failed: error (%@) is being released and lost", localError
);
393 CFReleaseNull(localError
);
397 if (dbconn
->isCorrupted
) {
398 ok
= SecDbHandleCorrupt(dbconn
, 0, error
);
401 secinfo("#SecDB", "#SecDB starting maintenance");
405 void SecDbCorrupt(SecDbConnectionRef dbconn
, CFErrorRef error
)
407 os_log_fault(secLogObjForScope("SecEmergency"), "SecDBCorrupt: %@", error
);
408 dbconn
->isCorrupted
= true;
409 CFRetainAssign(dbconn
->corruptionError
, error
);
413 static uint8_t knownDbPathIndex(SecDbConnectionRef dbconn
)
416 if(CFEqual(dbconn
->db
->db_path
, CFSTR("/Library/Keychains/keychain-2.db")))
418 if(CFEqual(dbconn
->db
->db_path
, CFSTR("/Library/Keychains/ocspcache.sqlite3")))
420 if(CFEqual(dbconn
->db
->db_path
, CFSTR("/Library/Keychains/TrustStore.sqlite3")))
422 if(CFEqual(dbconn
->db
->db_path
, CFSTR("/Library/Keychains/caissuercache.sqlite3")))
425 /* Unknown DB path */
429 static bool SecDbConnectionCheckCode(SecDbConnectionRef dbconn
, int code
, CFErrorRef
*error
, CFStringRef desc
, ...)
430 CF_FORMAT_FUNCTION(4, 5);
434 // Return true if there was no error, returns false otherwise and set *error to an appropriate CFErrorRef.
435 static bool SecDbConnectionCheckCode(SecDbConnectionRef dbconn
, int code
, CFErrorRef
*error
, CFStringRef desc
, ...) {
436 if (code
== SQLITE_OK
|| code
== SQLITE_DONE
)
441 va_start(args
, desc
);
442 CFStringRef msg
= CFStringCreateWithFormatAndArguments(kCFAllocatorDefault
, NULL
, desc
, args
);
444 SecDbErrorWithDb(code
, dbconn
->handle
, error
, CFSTR("%@"), msg
);
448 dbconn
->hasIOFailure
|= (SQLITE_IOERR
== code
);
450 /* If it's already corrupted, don't try to recover */
451 if (dbconn
->isCorrupted
) {
452 CFStringRef reason
= CFStringCreateWithFormat(kCFAllocatorDefault
, NULL
,
453 CFSTR("SQL DB %@ is corrupted already. Corruption error was: %d (previously %d)"),
454 dbconn
->db
->db_path
, code
, dbconn
->maybeCorruptedCode
);
455 secerror("%@",reason
);
456 __security_simulatecrash(reason
, __sec_exception_code_TwiceCorruptDb(knownDbPathIndex(dbconn
)));
457 CFReleaseSafe(reason
);
458 // We can't fall through to the checking case because it eventually calls SecDbConnectionCheckCode again.
459 // However, this is the second time we're seeing corruption so let's take the ultimate measure.
460 if ((SQLITE_CORRUPT
== code
) || (SQLITE_NOTADB
== code
)) {
461 secerror("SecDbConnectionCheckCode detected corruption twice: going to handle corrupt DB");
462 (void)SecDbHandleCorrupt(dbconn
, code
, error
);
467 // NOTADB means file is garbage, so it's functionally equivalent to corruption
468 dbconn
->isCorrupted
= (SQLITE_CORRUPT
== code
) || (SQLITE_NOTADB
== code
);
469 if (dbconn
->isCorrupted
) {
470 /* Run integrity check and only make dbconn->isCorrupted true and
471 run the corruption handler if the integrity check conclusively fails. */
472 dbconn
->maybeCorruptedCode
= code
;
473 dbconn
->isCorrupted
= SecDbCheckCorrupted(dbconn
);
474 if (dbconn
->isCorrupted
) {
475 secerror("operation returned code: %d integrity check=fail", code
);
476 (void)SecDbHandleCorrupt(dbconn
, code
, error
);
478 secerror("operation returned code: %d: integrity check=pass", code
);
485 #define BUSY_TIMEOUT_MS (5 * 60 * 1000) /* 5 minutes */
487 static int sleepBackoff
[] = { 10, 20, 50, 100, 250 };
488 static int sumBackoff
[] = { 10, 30, 80, 180, 430 };
489 static int NumberOfSleepBackoff
= sizeof(sleepBackoff
)/sizeof(sleepBackoff
[0]);
491 // Use these as silly hacks to encode the SQLite return code in the backtrace, for hang debugging purposes
492 static void __attribute__((noinline
)) SecDbLockSleep(int ms
) {
496 static void __attribute__((noinline
)) SecDbBusySleep(int ms
) {
500 // Return true causes the operation to be tried again.
501 // Note that we set sqlite3_busy_timeout on the connection, so anytime you're in here, it's likely due to SQLITE_LOCKED.
502 static bool SecDbWaitIfNeeded(SecDbConnectionRef dbconn
, int s3e
, sqlite3_stmt
*stmt
, CFStringRef desc
, int nTries
, CFErrorRef
*error
) {
503 if (((0xFF & s3e
) == SQLITE_BUSY
) || ((0xFF & s3e
) == SQLITE_LOCKED
)) {
504 int totaltimeout
, timeout
;
506 _Static_assert(sizeof(sumBackoff
) == sizeof(sleepBackoff
), "matching arrays not matching");
507 _Static_assert(sizeof(sumBackoff
[0]) == sizeof(sleepBackoff
[0]), "matching arrays not matching");
509 if (nTries
< NumberOfSleepBackoff
) {
510 timeout
= sleepBackoff
[nTries
];
511 totaltimeout
= sumBackoff
[nTries
];
513 timeout
= sleepBackoff
[NumberOfSleepBackoff
- 1];
514 totaltimeout
= sumBackoff
[NumberOfSleepBackoff
- 1] + (timeout
* (nTries
- NumberOfSleepBackoff
));
516 if (totaltimeout
< BUSY_TIMEOUT_MS
) {
517 secinfo("#SecDB", "sqlite busy/locked: %d ntries: %d totaltimeout: %d", s3e
, nTries
, totaltimeout
);
518 if(((0xFF & s3e
) == SQLITE_LOCKED
)) {
519 SecDbLockSleep(timeout
);
521 SecDbBusySleep(timeout
);
525 secinfo("#SecDB", "sqlite busy/locked: too long: %d ms, giving up", totaltimeout
);
529 return SecDbConnectionCheckCode(dbconn
, s3e
, error
, CFSTR("%@"), desc
);
532 enum SecDbStepResult
{
537 typedef enum SecDbStepResult SecDbStepResult
;
539 static SecDbStepResult
_SecDbStep(SecDbConnectionRef dbconn
, sqlite3_stmt
*stmt
, CFErrorRef
*error
) {
540 assert(stmt
!= NULL
);
544 s3e
= sqlite3_step(stmt
);
545 if (s3e
== SQLITE_ROW
) {
546 return kSecDbRowStep
;
547 } else if (s3e
== SQLITE_DONE
) {
549 ** ^[SQLITE_DONE] means that the statement has finished executing
550 ** successfully. sqlite3_step() should not be called again on this virtual
551 ** machine without first calling [] to reset the virtual
552 ** machine back to its initial state.
555 return kSecDbDoneStep
;
556 } else if (!SecDbWaitIfNeeded(dbconn
, s3e
, stmt
, CFSTR("step"), ntries
, error
)) {
557 return kSecDbErrorStep
;
564 SecDbExec(SecDbConnectionRef dbconn
, CFStringRef sql
, CFErrorRef
*error
)
569 CFStringRef tail
= NULL
;
571 sqlite3_stmt
*stmt
= SecDbCopyStmt(dbconn
, sql
, &tail
, error
);
575 while ((sr
= _SecDbStep(dbconn
, stmt
, error
)) == kSecDbRowStep
);
576 if (sr
== kSecDbErrorStep
)
578 ok
&= SecDbReleaseCachedStmt(dbconn
, sql
, stmt
, error
);
581 // TODO We already have an error here we really just want the left over sql in it's userData
582 ok
= SecDbError(SQLITE_ERROR
, error
, CFSTR("Error with unexecuted sql remaining %@"), sql
);
590 static int SecDBGetInteger(SecDbConnectionRef dbconn
, CFStringRef sql
)
592 __block
int number
= -1;
593 __block CFErrorRef error
= NULL
;
595 (void)SecDbWithSQL(dbconn
, sql
, &error
, ^bool(sqlite3_stmt
*sqlStmt
) {
596 (void)SecDbStep(dbconn
, sqlStmt
, &error
, ^(bool *stop
) {
597 number
= sqlite3_column_int(sqlStmt
, 0);
602 CFReleaseNull(error
);
607 void SecDBManagementTasks(SecDbConnectionRef dbconn
)
609 int64_t page_count
= SecDBGetInteger(dbconn
, CFSTR("pragma page_count"));
610 if (page_count
<= 0) {
613 int64_t free_count
= SecDBGetInteger(dbconn
, CFSTR("pragma freelist_count"));
614 if (free_count
< 0) {
618 int64_t max_free
= 8192;
620 int64_t pages_in_use
= page_count
- free_count
;
621 double loadFactor
= ((double)pages_in_use
/(double)page_count
);
622 if (0.85 < loadFactor
&& free_count
< max_free
) {
625 int64_t pages_to_free
= (int64_t)(0.2 * free_count
);
626 if (0.4 > loadFactor
) {
627 pages_to_free
= free_count
;
630 char *formatString
= NULL
;
631 asprintf(&formatString
, "pragma incremental_vacuum(%d)", (int)pages_to_free
);
633 char *sqlerror
= NULL
;
634 int rc
= sqlite3_exec(dbconn
->handle
, formatString
, NULL
, NULL
, &sqlerror
);
636 secerror("incremental_vacuum failed with: (%d) %{public}s", rc
, sqlerror
);
638 sqlite3_free(sqlerror
);
645 static bool SecDbBeginTransaction(SecDbConnectionRef dbconn
, SecDbTransactionType type
, CFErrorRef
*error
)
650 case kSecDbImmediateTransactionType
:
651 secdebug("db", "SecDbBeginTransaction SecDbBeginTransaction %p", dbconn
);
652 query
= CFSTR("BEGIN IMMEDIATE");
654 case kSecDbExclusiveRemoteSOSTransactionType
:
655 secdebug("db", "SecDbBeginTransaction kSecDbExclusiveRemoteSOSTransactionType %p", dbconn
);
656 dbconn
->source
= kSecDbSOSTransaction
;
657 query
= CFSTR("BEGIN EXCLUSIVE");
659 case kSecDbExclusiveRemoteCKKSTransactionType
:
660 secdebug("db", "SecDbBeginTransaction kSecDbExclusiveRemoteCKKSTransactionType %p", dbconn
);
661 dbconn
->source
= kSecDbCKKSTransaction
;
662 query
= CFSTR("BEGIN EXCLUSIVE");
664 case kSecDbExclusiveTransactionType
:
665 if (type
==kSecDbExclusiveTransactionType
)
666 secdebug("db", "SecDbBeginTransaction kSecDbExclusiveTransactionType %p", dbconn
);
667 query
= CFSTR("BEGIN EXCLUSIVE");
669 case kSecDbNormalTransactionType
:
670 secdebug("db", "SecDbBeginTransaction kSecDbNormalTransactionType %p", dbconn
);
671 query
= CFSTR("BEGIN");
674 secdebug("db", "SecDbBeginTransaction invalid transaction type %lu", type
);
675 ok
= SecDbError(SQLITE_ERROR
, error
, CFSTR("invalid transaction type %d"), (int)type
);
680 if (query
!= NULL
&& sqlite3_get_autocommit(dbconn
->handle
) != 0) {
681 ok
= SecDbExec(dbconn
, query
, error
);
684 dbconn
->inTransaction
= true;
689 static bool SecDbEndTransaction(SecDbConnectionRef dbconn
, bool commit
, CFErrorRef
*error
)
691 __block
bool ok
= true;
692 __block
bool commited
= false;
694 dispatch_block_t notifyAndExec
= ^{
696 //secdebug("db", "SecDbEndTransaction kSecDbTransactionWillCommit %p", dbconn);
697 SecDbNotifyPhase(dbconn
, kSecDbTransactionWillCommit
);
698 commited
= ok
= SecDbExec(dbconn
, CFSTR("END"), error
);
699 //secdebug("db", "SecDbEndTransaction kSecDbTransactionWillCommit %p (after notify)", dbconn);
701 ok
= SecDbExec(dbconn
, CFSTR("ROLLBACK"), error
);
704 dbconn
->inTransaction
= false;
705 SecDbNotifyPhase(dbconn
, commited
? kSecDbTransactionDidCommit
: kSecDbTransactionDidRollback
);
706 secdebug("db", "SecDbEndTransaction %s %p", commited
? "kSecDbTransactionDidCommit" : "kSecDbTransactionDidRollback", dbconn
);
707 dbconn
->source
= kSecDbAPITransaction
;
709 if (commit
&& dbconn
->db
->useRobotVacuum
) {
710 SecDBManagementTasks(dbconn
);
714 SecDbPerformOnCommitQueue(dbconn
, true, notifyAndExec
);
719 bool SecDbTransaction(SecDbConnectionRef dbconn
, SecDbTransactionType type
,
720 CFErrorRef
*error
, void (^transaction
)(bool *commit
))
725 if (dbconn
->inTransaction
) {
726 transaction(&commit
);
728 secinfo("#SecDB", "#SecDB nested transaction asked to not be committed");
731 ok
= SecDbBeginTransaction(dbconn
, type
, error
);
733 transaction(&commit
);
734 ok
= SecDbEndTransaction(dbconn
, commit
, error
);
741 sqlite3
*SecDbHandle(SecDbConnectionRef dbconn
) {
742 return dbconn
->handle
;
745 bool SecDbStep(SecDbConnectionRef dbconn
, sqlite3_stmt
*stmt
, CFErrorRef
*error
, void (^row
)(bool *stop
)) {
747 switch (_SecDbStep(dbconn
, stmt
, error
)) {
748 case kSecDbErrorStep
:
749 secdebug("db", "kSecDbErrorStep %@", error
?*error
:NULL
);
752 secdebug("db", "kSecDbRowStep %@", error
?*error
:NULL
);
754 __block
bool stop
= false;
755 SecAutoreleaseInvokeWithPool(^{
762 SecDbError(SQLITE_ERROR
, error
, CFSTR("SecDbStep SQLITE_ROW returned without a row handler"));
765 secdebug("db", "kSecDbDoneStep %@", error
?*error
:NULL
);
771 bool SecDbCheckpoint(SecDbConnectionRef dbconn
, CFErrorRef
*error
)
773 return SecDbConnectionCheckCode(dbconn
, sqlite3_wal_checkpoint(dbconn
->handle
, NULL
), error
, CFSTR("wal_checkpoint"));
776 static bool SecDbFileControl(SecDbConnectionRef dbconn
, int op
, void *arg
, CFErrorRef
*error
) {
777 return SecDbConnectionCheckCode(dbconn
, sqlite3_file_control(dbconn
->handle
, NULL
, op
, arg
), error
, CFSTR("file_control"));
780 static sqlite3
*_SecDbOpenV2(const char *path
,
785 sqlite3
*handle
= NULL
;
786 int s3e
= sqlite3_open_v2(path
, &handle
, flags
, NULL
);
789 SecDbErrorWithDb(s3e
, handle
, error
, CFSTR("open_v2 \"%s\" 0x%X"), path
, flags
);
790 sqlite3_close(handle
);
793 SecDbError(s3e
, error
, CFSTR("open_v2 \"%s\" 0x%X"), path
, flags
);
795 } else if (SQLITE_OPEN_READWRITE
== (flags
& SQLITE_OPEN_READWRITE
)) {
796 if (useRobotVacuum
) {
797 #define SECDB_SQLITE_AUTO_VACUUM_INCREMENTAL 2
802 * Setting auto_vacuum = incremental on a database that is not empty requires
803 * a VACCUUM, so check if the vacuum mode is not INCREMENTAL, and if its not,
804 * set it to incremental and vacuum.
807 int s3e
= sqlite3_prepare_v2(handle
, "PRAGMA auto_vacuum", -1, &stmt
, NULL
);
809 s3e
= sqlite3_step(stmt
);
810 if (s3e
== SQLITE_ROW
) {
811 vacuumMode
= sqlite3_column_int(stmt
, 0);
816 if (vacuumMode
!= SECDB_SQLITE_AUTO_VACUUM_INCREMENTAL
) {
817 (void)sqlite3_exec(handle
, "PRAGMA auto_vacuum = incremental", NULL
, NULL
, NULL
);
818 (void)sqlite3_exec(handle
, "VACUUM", NULL
, NULL
, NULL
);
822 (void)sqlite3_exec(handle
, "PRAGMA journal_mode = WAL", NULL
, NULL
, NULL
);
825 // Let SQLite handle timeouts.
826 sqlite3_busy_timeout(handle
, 5*1000);
831 static bool SecDbOpenV2(SecDbConnectionRef dbconn
, const char *path
, int flags
, CFErrorRef
*error
) {
832 return (dbconn
->handle
= _SecDbOpenV2(path
, flags
, dbconn
->db
->useWAL
, dbconn
->db
->useRobotVacuum
, error
)) != NULL
;
835 static bool SecDbTruncate(SecDbConnectionRef dbconn
, CFErrorRef
*error
)
837 int flags
= SQLITE_TRUNCATE_AUTOVACUUM_FULL
;
838 if (dbconn
->db
->useWAL
) {
839 flags
|= SQLITE_TRUNCATE_JOURNALMODE_WAL
;
841 __block
bool ok
= SecDbFileControl(dbconn
, SQLITE_TRUNCATE_DATABASE
, &flags
, error
);
843 sqlite3_close(dbconn
->handle
);
844 dbconn
->handle
= NULL
;
845 CFStringPerformWithCString(dbconn
->db
->db_path
, ^(const char *path
) {
847 CFReleaseNull(*error
);
848 if (SecCheckErrno(unlink(path
), error
, CFSTR("unlink %s"), path
)) {
849 ok
= SecDbOpenHandle(dbconn
, NULL
, error
);
853 secinfo("#SecDB", "#SecDB Failed to delete db handle: %{public}@", error
? *error
: NULL
);
861 static bool SecDbHandleCorrupt(SecDbConnectionRef dbconn
, int rc
, CFErrorRef
*error
)
863 if (!dbconn
->db
->allowRepair
) {
864 SecCFCreateErrorWithFormat(rc
, kSecErrnoDomain
, NULL
, error
, NULL
,
865 CFSTR("SecDbHandleCorrupt handled error: [%d] %s"), rc
, strerror(rc
));
866 dbconn
->isCorrupted
= false;
870 // Backup current db.
871 __block
bool didRename
= false;
872 CFStringPerformWithCString(dbconn
->db
->db_path
, ^(const char *db_path
) {
873 sqlite3
*corrupt_db
= NULL
;
874 char buf
[PATH_MAX
+1];
875 snprintf(buf
, sizeof(buf
), "%s-corrupt", db_path
);
876 if (dbconn
->handle
&& (corrupt_db
= _SecDbOpenV2(buf
, SQLITE_OPEN_READWRITE
, dbconn
->db
->useWAL
, dbconn
->db
->useRobotVacuum
, error
))) {
878 didRename
= SecDbErrorWithDb(sqlite3_file_control(corrupt_db
, NULL
, SQLITE_FCNTL_PERSIST_WAL
, &on
), corrupt_db
, error
, CFSTR("persist wal"));
879 didRename
&= SecDbErrorWithDb(sqlite3_file_control(corrupt_db
, NULL
, SQLITE_REPLACE_DATABASE
, (void *)dbconn
->handle
), corrupt_db
, error
, CFSTR("replace database"));
880 sqlite3_close(corrupt_db
);
884 secerror("Tried to rename corrupt database at path %@, but we failed: %@, trying explicit rename", dbconn
->db
->db_path
, error
? *error
: NULL
);
886 CFReleaseNull(*error
);
888 // Explicitly close our connection, plus all other open connections to this db.
890 if (dbconn
->handle
) {
891 closed
&= SecDbError(sqlite3_close(dbconn
->handle
), error
, CFSTR("close"));
892 dbconn
->handle
= NULL
;
894 SecDbRef db
= dbconn
->db
;
895 CFIndex idx
, count
= (db
->connections
) ? CFArrayGetCount(db
->connections
) : 0;
896 for (idx
= 0; idx
< count
; idx
++) {
897 SecDbConnectionRef dbconn
= (SecDbConnectionRef
) CFArrayGetValueAtIndex(db
->connections
, idx
);
898 if (dbconn
&& dbconn
->handle
) {
899 closed
&= SecDbError(sqlite3_close(dbconn
->handle
), error
, CFSTR("close"));
900 dbconn
->handle
= NULL
;
903 CFArrayRemoveAllValues(db
->connections
);
905 // Attempt rename only if all connections closed successfully.
907 if (SecCheckErrno(rename(db_path
, buf
), error
, CFSTR("rename %s %s"), db_path
, buf
)) {
908 if (SecDbOpenHandle(dbconn
, NULL
, error
)) {
915 secerror("Database at path %@ is corrupt. Copied it to %s for further investigation.", dbconn
->db
->db_path
, buf
);
917 seccritical("Tried to copy corrupt database at path %@, but we failed: %@", dbconn
->db
->db_path
, error
? *error
: NULL
);
921 bool ok
= (didRename
&&
922 (dbconn
->handle
|| SecDbOpenHandle(dbconn
, NULL
, error
)) &&
923 SecDbTruncate(dbconn
, error
));
925 // Mark the db as not corrupted, even if something failed.
926 // Always note we are no longer in the corruption handler
927 dbconn
->isCorrupted
= false;
929 // Invoke our callers opened callback, since we just created a new database
930 if (ok
&& dbconn
->db
->opened
) {
931 dbconn
->db
->callOpenedHandlerForNextConnection
= false;
932 ok
= dbconn
->db
->opened(dbconn
->db
, dbconn
, true, &dbconn
->db
->callOpenedHandlerForNextConnection
, error
);
935 if (dbconn
->db
->corruptionReset
) {
936 dbconn
->db
->corruptionReset();
943 SecDbSetCorruptionReset(SecDbRef db
, void (^corruptionReset
)(void))
945 if (db
->corruptionReset
) {
946 Block_release(db
->corruptionReset
);
947 db
->corruptionReset
= NULL
;
949 if (corruptionReset
) {
950 db
->corruptionReset
= Block_copy(corruptionReset
);
954 static bool SecDbLoggingEnabled(CFStringRef type
)
956 CFTypeRef profile
= NULL
;
957 bool enabled
= false;
959 if (csr_check(CSR_ALLOW_APPLE_INTERNAL
) != 0)
962 profile
= (CFNumberRef
)CFPreferencesCopyValue(CFSTR("SQLProfile"), CFSTR("com.apple.security"), kCFPreferencesAnyUser
, kCFPreferencesAnyHost
);
967 if (CFGetTypeID(profile
) == CFBooleanGetTypeID()) {
968 enabled
= CFBooleanGetValue((CFBooleanRef
)profile
);
969 } else if (CFGetTypeID(profile
) == CFNumberGetTypeID()) {
971 CFNumberGetValue(profile
, kCFNumberSInt32Type
, &num
);
975 CFReleaseSafe(profile
);
981 SecDbProfileMask(void)
983 static dispatch_once_t onceToken
;
984 static unsigned profile_mask
= 0;
986 // sudo defaults write /Library/Preferences/com.apple.security SQLProfile -bool true
987 dispatch_once(&onceToken
, ^{
988 if (SecDbLoggingEnabled(CFSTR("SQLProfile")))
989 profile_mask
= SQLITE_TRACE_PROFILE
;
991 profile_mask
|= SQLITE_TRACE_STMT
;
993 if (SecDbLoggingEnabled(CFSTR("SQLTrace")))
994 profile_mask
= SQLITE_TRACE_STMT
;
996 if (SecDbLoggingEnabled(CFSTR("SQLRow")))
997 profile_mask
= SQLITE_TRACE_ROW
;
998 secinfo("#SecDB", "sqlDb: sql trace mask: 0x%08x", profile_mask
);
1000 return profile_mask
;
1004 SecDbTraceV2(unsigned mask
, void *ctx
, void *p
, void *x
) {
1005 SecDbConnectionRef dbconn __unused
= ctx
;
1006 const char *trace
= "unknown";
1008 if (mask
== SQLITE_TRACE_PROFILE
)
1009 trace
= sqlite3_sql(p
);
1010 else if (mask
== SQLITE_TRACE_STMT
) {
1011 trace
= sqlite3_sql(p
);
1012 } else if (mask
== SQLITE_TRACE_ROW
) {
1013 trace
= sqlite3_expanded_sql(p
);
1016 secinfo("#SecDB", "#SecDB %{public}s", trace
);
1021 static bool SecDbOpenHandle(SecDbConnectionRef dbconn
, bool *created
, CFErrorRef
*error
)
1023 __block
bool ok
= true;
1025 CFStringPerformWithCString(dbconn
->db
->db_path
, ^(const char *db_path
) {
1026 int flags
= (dbconn
->db
->readWrite
) ? SQLITE_OPEN_READWRITE
: SQLITE_OPEN_READONLY
;
1027 ok
= created
&& SecDbOpenV2(dbconn
, db_path
, flags
, NULL
);
1031 char *tmp
= dirname((char *)db_path
);
1033 mode_t omode
= dbconn
->db
->mode
;
1034 if (omode
& S_IRUSR
) { omode
|= S_IXUSR
; } // owner can read
1035 if (omode
& S_IRGRP
) { omode
|= S_IXGRP
; } // group can read
1036 if (omode
& S_IROTH
) { omode
|= S_IXOTH
; } // other can read
1037 int errnum
= mkpath_np(tmp
, omode
);
1038 if (errnum
!= 0 && errnum
!= EEXIST
) {
1039 SecCFCreateErrorWithFormat(errnum
, kSecErrnoDomain
, NULL
, error
, NULL
,
1040 CFSTR("mkpath_np %s: [%d] %s"), tmp
, errnum
, strerror(errnum
));
1045 // if the enclosing directory is ok, try to create the database.
1046 // this forces us to open it read-write, so we'll need to be the owner here.
1047 ok
= ok
&& SecDbOpenV2(dbconn
, db_path
, SQLITE_OPEN_READWRITE
| SQLITE_OPEN_CREATE
, error
);
1049 chmod(db_path
, dbconn
->db
->mode
); // default: 0600 (S_IRUSR | S_IWUSR)
1056 unsigned mask
= SecDbProfileMask();
1058 (void)sqlite3_trace_v2(dbconn
->handle
,
1069 static SecDbConnectionRef
1070 SecDbConnectionCreate(SecDbRef db
, bool readOnly
, CFErrorRef
*error
)
1072 SecDbConnectionRef dbconn
= NULL
;
1074 dbconn
= CFTypeAllocate(SecDbConnection
, struct __OpaqueSecDbConnection
, kCFAllocatorDefault
);
1075 require(dbconn
!= NULL
, done
);
1078 dbconn
->readOnly
= readOnly
;
1079 dbconn
->inTransaction
= false;
1080 dbconn
->source
= kSecDbInvalidTransaction
;
1081 dbconn
->isCorrupted
= false;
1082 dbconn
->maybeCorruptedCode
= 0;
1083 dbconn
->hasIOFailure
= false;
1084 dbconn
->corruptionError
= NULL
;
1085 dbconn
->handle
= NULL
;
1086 dbconn
->changes
= CFArrayCreateMutableForCFTypes(kCFAllocatorDefault
);
1092 static bool SecDbConnectionIsReadOnly(SecDbConnectionRef dbconn
) {
1093 return dbconn
->readOnly
;
1096 static void SecDbConectionSetReadOnly(SecDbConnectionRef dbconn
, bool readOnly
) {
1097 dbconn
->readOnly
= readOnly
;
1100 /* Read only connections go to the end of the queue, writeable connections
1101 go to the start of the queue. */
1102 SecDbConnectionRef
SecDbConnectionAcquire(SecDbRef db
, bool readOnly
, CFErrorRef
*error
) {
1103 SecDbConnectionRef dbconn
= NULL
;
1104 SecDbConnectionAcquireRefMigrationSafe(db
, readOnly
, &dbconn
, error
);
1108 bool SecDbConnectionAcquireRefMigrationSafe(SecDbRef db
, bool readOnly
, SecDbConnectionRef
* dbconnRef
, CFErrorRef
*error
)
1111 secinfo("dbconn", "acquire %s connection", readOnly
? "ro" : "rw");
1112 dispatch_semaphore_wait(readOnly
? db
->read_semaphore
: db
->write_semaphore
, DISPATCH_TIME_FOREVER
);
1113 __block SecDbConnectionRef dbconn
= NULL
;
1114 __block
bool ok
= true;
1115 __block
bool ranOpenedHandler
= false;
1117 bool (^assignDbConn
)(SecDbConnectionRef
) = ^bool(SecDbConnectionRef connection
) {
1118 dbconn
= connection
;
1120 *dbconnRef
= connection
;
1123 return dbconn
!= NULL
;
1126 dispatch_sync(db
->queue
, ^{
1127 if (!db
->didFirstOpen
) {
1128 bool didCreate
= false;
1129 ok
= assignDbConn(SecDbConnectionCreate(db
, false, error
));
1130 CFErrorRef localError
= NULL
;
1131 if (ok
&& !SecDbOpenHandle(dbconn
, &didCreate
, &localError
)) {
1132 secerror("Unable to create database: %@", localError
);
1133 if (localError
&& CFEqual(CFErrorGetDomain(localError
), kSecDbErrorDomain
)) {
1134 int code
= (int)CFErrorGetCode(localError
);
1135 dbconn
->isCorrupted
= (SQLITE_CORRUPT
== code
) || (SQLITE_NOTADB
== code
);
1137 // If the open failure isn't due to corruption, propagate the error.
1138 ok
= dbconn
->isCorrupted
;
1139 if (!ok
&& error
&& *error
== NULL
) {
1140 *error
= localError
;
1144 CFReleaseNull(localError
);
1147 db
->didFirstOpen
= ok
= SecDbDidCreateFirstConnection(dbconn
, didCreate
, error
);
1148 ranOpenedHandler
= true;
1151 CFReleaseNull(dbconn
);
1153 /* Try to get one from the cache */
1154 CFIndex count
= CFArrayGetCount(db
->connections
);
1155 while (count
&& !dbconn
) {
1156 CFIndex ix
= readOnly
? count
- 1 : 0;
1157 if (assignDbConn((SecDbConnectionRef
)CFArrayGetValueAtIndex(db
->connections
, ix
)))
1158 CFRetainSafe(dbconn
);
1160 secerror("got NULL dbconn at index: %" PRIdCFIndex
" skipping", ix
);
1161 CFArrayRemoveValueAtIndex(db
->connections
, ix
);
1167 /* Make sure the connection we found has the right access */
1168 if (SecDbConnectionIsReadOnly(dbconn
) != readOnly
) {
1169 SecDbConectionSetReadOnly(dbconn
, readOnly
);
1172 /* Nothing found in cache, create a new connection */
1173 bool created
= false;
1174 if (assignDbConn(SecDbConnectionCreate(db
, readOnly
, error
)) && !SecDbOpenHandle(dbconn
, &created
, error
)) {
1175 CFReleaseNull(dbconn
);
1180 if (dbconn
&& !ranOpenedHandler
&& dbconn
->db
->opened
) {
1181 dispatch_sync(db
->queue
, ^{
1182 if (dbconn
->db
->callOpenedHandlerForNextConnection
) {
1183 dbconn
->db
->callOpenedHandlerForNextConnection
= false;
1184 if (!dbconn
->db
->opened(db
, dbconn
, false, &dbconn
->db
->callOpenedHandlerForNextConnection
, error
)) {
1185 if (!dbconn
->isCorrupted
|| !SecDbHandleCorrupt(dbconn
, 0, error
)) {
1186 CFReleaseNull(dbconn
);
1194 *dbconnRef
= dbconn
;
1198 // If acquire fails we need to signal the semaphore again.
1199 dispatch_semaphore_signal(readOnly
? db
->read_semaphore
: db
->write_semaphore
);
1203 return dbconn
? true : false;
1206 void SecDbConnectionRelease(SecDbConnectionRef dbconn
) {
1208 secerror("called with NULL dbconn");
1211 SecDbRef db
= dbconn
->db
;
1212 secinfo("dbconn", "release %@", dbconn
);
1213 dispatch_sync(db
->queue
, ^{
1214 bool readOnly
= SecDbConnectionIsReadOnly(dbconn
);
1215 if (dbconn
->hasIOFailure
) {
1216 // Something wrong on the file layer (e.g. revoked file descriptor for networked home)
1217 // so we don't trust our existing connections anymore.
1218 CFArrayRemoveAllValues(db
->connections
);
1220 CFIndex count
= CFArrayGetCount(db
->connections
);
1221 // Add back possible writable dbconn to the pool.
1222 CFArrayInsertValueAtIndex(db
->connections
, readOnly
? count
: 0, dbconn
);
1223 // Remove the last (probably read-only) dbconn from the pool.
1224 if (count
>= db
->maxIdleHandles
) {
1225 CFArrayRemoveValueAtIndex(db
->connections
, count
);
1228 // Signal after we have put the connection back in the pool of connections
1229 dispatch_semaphore_signal(readOnly
? db
->read_semaphore
: db
->write_semaphore
);
1235 void SecDbReleaseAllConnections(SecDbRef db
) {
1236 // Force all connections to be removed (e.g. file descriptor no longer valid)
1238 secerror("called with NULL db");
1241 dispatch_sync(db
->queue
, ^{
1242 CFArrayRemoveAllValues(db
->connections
);
1243 dispatch_semaphore_signal(db
->write_semaphore
);
1244 dispatch_semaphore_signal(db
->read_semaphore
);
1248 bool SecDbPerformRead(SecDbRef db
, CFErrorRef
*error
, void (^perform
)(SecDbConnectionRef dbconn
)) {
1249 SecDbConnectionRef dbconn
= SecDbConnectionAcquire(db
, true, error
);
1250 bool success
= false;
1254 SecDbConnectionRelease(dbconn
);
1259 bool SecDbPerformWrite(SecDbRef db
, CFErrorRef
*error
, void (^perform
)(SecDbConnectionRef dbconn
)) {
1261 SecError(errSecNotAvailable
, error
, CFSTR("failed to get a db handle"));
1264 SecDbConnectionRef dbconn
= SecDbConnectionAcquire(db
, false, error
);
1265 bool success
= false;
1269 SecDbConnectionRelease(dbconn
);
1275 SecDbConnectionCopyFormatDescription(CFTypeRef value
, CFDictionaryRef formatOptions
)
1277 SecDbConnectionRef dbconn
= (SecDbConnectionRef
)value
;
1278 return CFStringCreateWithFormat(kCFAllocatorDefault
, NULL
, CFSTR("<SecDbConnection %s %s>"),
1279 dbconn
->readOnly
? "ro" : "rw", dbconn
->handle
? "open" : "closed");
1283 SecDbConnectionDestroy(CFTypeRef value
)
1285 SecDbConnectionRef dbconn
= (SecDbConnectionRef
)value
;
1286 if (dbconn
->handle
) {
1287 sqlite3_close(dbconn
->handle
);
1290 CFReleaseNull(dbconn
->changes
);
1291 CFReleaseNull(dbconn
->corruptionError
);
1295 void SecDbPerformOnCommitQueue(SecDbConnectionRef dbconn
, bool barrier
, dispatch_block_t perform
) {
1297 dispatch_barrier_sync(dbconn
->db
->commitQueue
, ^{
1301 dispatch_sync(dbconn
->db
->commitQueue
, ^{
1308 // MARK: Bind helpers
1311 bool SecDbBindNull(sqlite3_stmt
*stmt
, int param
, CFErrorRef
*error
) {
1312 bool ok
= SecDbErrorWithStmt(sqlite3_bind_null(stmt
, param
),
1313 stmt
, error
, CFSTR("bind_null[%d]"), param
);
1314 secinfo("bind", "bind_null[%d] error: %@", param
, error
? *error
: NULL
);
1319 bool SecDbBindBlob(sqlite3_stmt
*stmt
, int param
, const void *zData
, size_t n
, void(*xDel
)(void*), CFErrorRef
*error
) {
1321 return SecDbErrorWithStmt(SQLITE_TOOBIG
, stmt
, error
,
1322 CFSTR("bind_blob[%d]: blob bigger than INT_MAX"), param
);
1324 bool ok
= SecDbErrorWithStmt(sqlite3_bind_blob(stmt
, param
, zData
, (int)n
, xDel
),
1325 stmt
, error
, CFSTR("bind_blob[%d]"), param
);
1326 secinfo("bind", "bind_blob[%d]: %.*s: %@", param
, (int)n
, zData
, error
? *error
: NULL
);
1330 bool SecDbBindText(sqlite3_stmt
*stmt
, int param
, const char *zData
, size_t n
, void(*xDel
)(void*), CFErrorRef
*error
) {
1332 return SecDbErrorWithStmt(SQLITE_TOOBIG
, stmt
, error
,
1333 CFSTR("bind_text[%d]: text bigger than INT_MAX"), param
);
1335 bool ok
= SecDbErrorWithStmt(sqlite3_bind_text(stmt
, param
, zData
, (int)n
, xDel
), stmt
, error
,
1336 CFSTR("bind_text[%d]"), param
);
1337 secinfo("bind", "bind_text[%d]: \"%s\" error: %@", param
, zData
, error
? *error
: NULL
);
1341 bool SecDbBindDouble(sqlite3_stmt
*stmt
, int param
, double value
, CFErrorRef
*error
) {
1342 bool ok
= SecDbErrorWithStmt(sqlite3_bind_double(stmt
, param
, value
), stmt
, error
,
1343 CFSTR("bind_double[%d]"), param
);
1344 secinfo("bind", "bind_double[%d]: %f error: %@", param
, value
, error
? *error
: NULL
);
1348 bool SecDbBindInt(sqlite3_stmt
*stmt
, int param
, int value
, CFErrorRef
*error
) {
1349 bool ok
= SecDbErrorWithStmt(sqlite3_bind_int(stmt
, param
, value
), stmt
, error
,
1350 CFSTR("bind_int[%d]"), param
);
1351 secinfo("bind", "bind_int[%d]: %d error: %@", param
, value
, error
? *error
: NULL
);
1355 bool SecDbBindInt64(sqlite3_stmt
*stmt
, int param
, sqlite3_int64 value
, CFErrorRef
*error
) {
1356 bool ok
= SecDbErrorWithStmt(sqlite3_bind_int64(stmt
, param
, value
), stmt
, error
,
1357 CFSTR("bind_int64[%d]"), param
);
1358 secinfo("bind", "bind_int64[%d]: %lld error: %@", param
, value
, error
? *error
: NULL
);
1363 /* AUDIT[securityd](done):
1364 value (ok) is a caller provided, non NULL CFTypeRef.
1366 bool SecDbBindObject(sqlite3_stmt
*stmt
, int param
, CFTypeRef value
, CFErrorRef
*error
) {
1368 __block
bool result
= false;
1370 /* TODO: Can we use SQLITE_STATIC below everwhere we currently use
1371 SQLITE_TRANSIENT since we finalize the statement before the value
1372 goes out of scope? */
1373 if (!value
|| (valueId
= CFGetTypeID(value
)) == CFNullGetTypeID()) {
1374 /* Skip bindings for NULL values. sqlite3 will interpret unbound
1375 params as NULL which is exactly what we want. */
1379 result
= SecDbBindNull(stmt
, param
, error
);
1381 } else if (valueId
== CFStringGetTypeID()) {
1382 CFStringPerformWithCStringAndLength(value
, ^(const char *cstr
, size_t clen
) {
1383 result
= SecDbBindText(stmt
, param
, cstr
, clen
, SQLITE_TRANSIENT
, error
);
1385 } else if (valueId
== CFDataGetTypeID()) {
1386 CFIndex len
= CFDataGetLength(value
);
1388 result
= SecDbBindBlob(stmt
, param
, CFDataGetBytePtr(value
),
1389 len
, SQLITE_TRANSIENT
, error
);
1391 result
= SecDbBindText(stmt
, param
, "", 0, SQLITE_TRANSIENT
, error
);
1393 } else if (valueId
== CFDateGetTypeID()) {
1394 CFAbsoluteTime abs_time
= CFDateGetAbsoluteTime(value
);
1395 result
= SecDbBindDouble(stmt
, param
, abs_time
, error
);
1396 } else if (valueId
== CFBooleanGetTypeID()) {
1397 int bval
= CFBooleanGetValue(value
);
1398 result
= SecDbBindInt(stmt
, param
, bval
, error
);
1399 } else if (valueId
== CFNumberGetTypeID()) {
1401 if (CFNumberIsFloatType(value
)) {
1403 convertOk
= CFNumberGetValue(value
, kCFNumberDoubleType
, &nval
);
1404 result
= SecDbBindDouble(stmt
, param
, nval
, error
);
1407 convertOk
= CFNumberGetValue(value
, kCFNumberSInt32Type
, &nval
);
1409 result
= SecDbBindInt(stmt
, param
, nval
, error
);
1411 sqlite_int64 nval64
;
1412 convertOk
= CFNumberGetValue(value
, kCFNumberSInt64Type
, &nval64
);
1414 result
= SecDbBindInt64(stmt
, param
, nval64
, error
);
1418 result
= SecDbError(SQLITE_INTERNAL
, error
, CFSTR("bind CFNumberGetValue failed for %@"), value
);
1422 CFStringRef valueDesc
= CFCopyTypeIDDescription(valueId
);
1423 SecDbError(SQLITE_MISMATCH
, error
, CFSTR("bind unsupported type %@"), valueDesc
);
1424 CFReleaseSafe(valueDesc
);
1432 // MARK: SecDbStatementRef
1434 bool SecDbReset(sqlite3_stmt
*stmt
, CFErrorRef
*error
) {
1435 return SecDbErrorWithStmt(sqlite3_reset(stmt
), stmt
, error
, CFSTR("reset"));
1438 bool SecDbClearBindings(sqlite3_stmt
*stmt
, CFErrorRef
*error
) {
1439 return SecDbErrorWithStmt(sqlite3_clear_bindings(stmt
), stmt
, error
, CFSTR("clear bindings"));
1442 bool SecDbFinalize(sqlite3_stmt
*stmt
, CFErrorRef
*error
) {
1443 sqlite3
*handle
= sqlite3_db_handle(stmt
);
1444 int s3e
= sqlite3_finalize(stmt
);
1445 return s3e
== SQLITE_OK
? true : SecDbErrorWithDb(s3e
, handle
, error
, CFSTR("finalize: %p"), stmt
);
1448 sqlite3_stmt
*SecDbPrepareV2(SecDbConnectionRef dbconn
, const char *sql
, size_t sqlLen
, const char **sqlTail
, CFErrorRef
*error
) {
1449 sqlite3
*db
= SecDbHandle(dbconn
);
1450 if (sqlLen
> INT_MAX
) {
1451 SecDbErrorWithDb(SQLITE_TOOBIG
, db
, error
, CFSTR("prepare_v2: sql bigger than INT_MAX"));
1456 sqlite3_stmt
*stmt
= NULL
;
1457 int s3e
= sqlite3_prepare_v2(db
, sql
, (int)sqlLen
, &stmt
, sqlTail
);
1458 if (s3e
== SQLITE_OK
)
1460 else if (!SecDbWaitIfNeeded(dbconn
, s3e
, NULL
, CFSTR("preparev2"), ntries
, error
))
1466 static sqlite3_stmt
*SecDbCopyStatementWithTailRange(SecDbConnectionRef dbconn
, CFStringRef sql
, CFRange
*sqlTail
, CFErrorRef
*error
) {
1467 __block sqlite3_stmt
*stmt
= NULL
;
1468 if (sql
) CFStringPerformWithCStringAndLength(sql
, ^(const char *sqlStr
, size_t sqlLen
) {
1469 const char *tail
= NULL
;
1470 stmt
= SecDbPrepareV2(dbconn
, sqlStr
, sqlLen
, &tail
, error
);
1471 if (sqlTail
&& sqlStr
< tail
&& tail
< sqlStr
+ sqlLen
) {
1472 sqlTail
->location
= tail
- sqlStr
;
1473 sqlTail
->length
= sqlLen
- sqlTail
->location
;
1480 sqlite3_stmt
*SecDbCopyStmt(SecDbConnectionRef dbconn
, CFStringRef sql
, CFStringRef
*tail
, CFErrorRef
*error
) {
1481 // TODO: Add caching and cache lookup of statements
1482 CFRange sqlTail
= {};
1483 sqlite3_stmt
*stmt
= SecDbCopyStatementWithTailRange(dbconn
, sql
, &sqlTail
, error
);
1484 if (sqlTail
.length
> 0) {
1485 CFStringRef excess
= CFStringCreateWithSubstring(CFGetAllocator(sql
), sql
, sqlTail
);
1489 SecDbError(SQLITE_INTERNAL
, error
,
1490 CFSTR("prepare_v2: %@ unused sql: %@"),
1492 CFReleaseSafe(excess
);
1493 SecDbFinalize(stmt
, error
);
1501 TODO: Could do a hack here with a custom kCFAllocatorNULL allocator for a second CFRuntimeBase inside a SecDbStatement,
1502 TODO: Better yet make a full blow SecDbStatement instance whenever SecDbCopyStmt is called. Then, when the statement is released, in the Dispose method, we Reset and ClearBindings the sqlite3_stmt * and hand it back to the SecDb with the original CFStringRef for the sql (or hash thereof) as an argument. */
1503 bool SecDbReleaseCachedStmt(SecDbConnectionRef dbconn
, CFStringRef sql
, sqlite3_stmt
*stmt
, CFErrorRef
*error
) {
1505 return SecDbFinalize(stmt
, error
);
1510 bool SecDbPrepare(SecDbConnectionRef dbconn
, CFStringRef sql
, CFErrorRef
*error
, void(^exec
)(sqlite3_stmt
*stmt
)) {
1511 assert(sql
!= NULL
);
1512 sqlite3_stmt
*stmt
= SecDbCopyStmt(dbconn
, sql
, NULL
, error
);
1517 return SecDbReleaseCachedStmt(dbconn
, sql
, stmt
, error
);
1520 bool SecDbWithSQL(SecDbConnectionRef dbconn
, CFStringRef sql
, CFErrorRef
*error
, bool(^perform
)(sqlite3_stmt
*stmt
)) {
1524 CFStringRef tail
= NULL
;
1526 sqlite3_stmt
*stmt
= SecDbCopyStmt(dbconn
, sql
, &tail
, error
);
1532 // TODO: Use a different error scope here.
1533 ok
= SecError(-50 /* errSecParam */, error
, CFSTR("SecDbWithSQL perform block missing"));
1535 ok
&= SecDbReleaseCachedStmt(dbconn
, sql
, stmt
, error
);
1538 // TODO We already have an error here we really just want the left over sql in it's userData
1539 ok
= SecDbError(SQLITE_ERROR
, error
, CFSTR("Error with unexecuted sql remaining %@"), sql
);
1548 /* SecDbForEach returns true if all SQLITE_ROW returns of sqlite3_step() return true from the row block.
1549 If the row block returns false and doesn't set an error (to indicate it has reached a limit),
1550 this entire function returns false. In that case no error will be set. */
1551 bool SecDbForEach(SecDbConnectionRef dbconn
, sqlite3_stmt
*stmt
, CFErrorRef
*error
, bool(^row
)(int row_index
)) {
1552 bool result
= false;
1553 for (int row_ix
= 0;;++row_ix
) {
1554 int s3e
= sqlite3_step(stmt
);
1555 if (s3e
== SQLITE_ROW
) {
1561 // If we have no row block then getting SQLITE_ROW is an error
1562 SecDbError(s3e
, error
,
1563 CFSTR("step[%d]: %s returned SQLITE_ROW with NULL row block"),
1564 row_ix
, sqlite3_sql(stmt
));
1567 if (s3e
== SQLITE_DONE
) {
1570 SecDbConnectionCheckCode(dbconn
, s3e
, error
, CFSTR("SecDbForEach step[%d]"), row_ix
);
1578 bool SecDbForEach(sqlite3_stmt
*stmt
, CFErrorRef
*error
, bool(^row
)(int row_index
)) {
1581 switch (_SecDbStep(dbconn
, stmt
, error
)) {
1582 case kSecDbErrorStep
:
1589 SecDbError(SQLITE_ERROR
, error
, CFSTR("SecDbStep SQLITE_ROW returned without a row handler"));
1592 case kSecDbDoneStep
:
1599 void SecDbRecordChange(SecDbConnectionRef dbconn
, CFTypeRef deleted
, CFTypeRef inserted
) {
1600 if (!dbconn
->db
->notifyPhase
) return;
1601 CFTypeRef entry
= SecDbEventCreateWithComponents(deleted
, inserted
);
1603 CFArrayAppendValue(dbconn
->changes
, entry
);
1606 if (!dbconn
->inTransaction
) {
1607 secerror("db %@ changed outside txn", dbconn
);
1608 // Only notify of DidCommit, since WillCommit code assumes
1610 SecDbOnNotify(dbconn
, ^{
1611 SecDbNotifyPhase(dbconn
, kSecDbTransactionDidCommit
);
1618 CFGiblisFor(SecDbConnection
)
1621 // SecDbEvent Creation and consumption
1624 static SecDbEventRef
SecDbEventCreateInsert(CFTypeRef inserted
) {
1625 return CFRetainSafe(inserted
);
1628 static SecDbEventRef
SecDbEventCreateDelete(CFTypeRef deleted
) {
1629 return CFArrayCreate(kCFAllocatorDefault
, &deleted
, 1, &kCFTypeArrayCallBacks
);
1632 static SecDbEventRef
SecDbEventCreateUpdate(CFTypeRef deleted
, CFTypeRef inserted
) {
1633 const void *values
[2] = { deleted
, inserted
};
1634 return CFArrayCreate(kCFAllocatorDefault
, values
, 2, &kCFTypeArrayCallBacks
);
1637 SecDbEventRef
SecDbEventCreateWithComponents(CFTypeRef deleted
, CFTypeRef inserted
) {
1638 if (deleted
&& inserted
)
1639 return SecDbEventCreateUpdate(deleted
, inserted
);
1641 return SecDbEventCreateDelete(deleted
);
1643 return SecDbEventCreateInsert(inserted
);
1648 void SecDbEventTranslateComponents(SecDbEventRef item
, CFTypeRef
* deleted
, CFTypeRef
* inserted
) {
1649 if(CFGetTypeID(item
) == CFArrayGetTypeID()) {
1650 // One item: deletion. Two: update.
1651 CFIndex arraySize
= CFArrayGetCount(item
);
1652 if(arraySize
== 1) {
1653 if(deleted
) { *deleted
= CFArrayGetValueAtIndex(item
, 0); }
1654 if(inserted
) { *inserted
= NULL
; }
1655 } else if(arraySize
== 2) {
1656 if(deleted
) { *deleted
= CFArrayGetValueAtIndex(item
, 0); }
1657 if(inserted
) { *inserted
= CFArrayGetValueAtIndex(item
, 1); }
1659 if(deleted
) { *deleted
= NULL
; }
1660 if(inserted
) { *inserted
= NULL
; }
1663 if(deleted
) { *deleted
= NULL
; }
1664 if(inserted
) { *inserted
= item
; }
1669 bool SecDbEventGetComponents(SecDbEventRef event
, CFTypeRef
*deleted
, CFTypeRef
*inserted
, CFErrorRef
*error
) {
1670 if (isArray(event
)) {
1671 CFArrayRef array
= event
;
1672 switch (CFArrayGetCount(array
)) {
1674 *deleted
= CFArrayGetValueAtIndex(array
, 0);
1675 *inserted
= CFArrayGetValueAtIndex(array
, 1);
1678 *deleted
= CFArrayGetValueAtIndex(array
, 0);
1682 SecError(errSecParam
, error
, NULL
, CFSTR("invalid entry in changes array: %@"), array
);