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"
43 // Architecturally inverted files
44 // These are in SecureObjectSync but utilities depends on them
45 // <rdar://problem/20802079> Fix layer violation (SOSDigestVector, SOSManifest, SecDB.c)
47 #include <Security/SecureObjectSync/SOSDigestVector.h>
48 #include <Security/SecureObjectSync/SOSManifest.h>
50 #define HAVE_UNLOCK_NOTIFY 0
52 struct __OpaqueSecDbStatement
{
55 SecDbConnectionRef dbconn
;
59 struct __OpaqueSecDbConnection
{
62 //CFMutableDictionaryRef statements;
64 SecDbRef db
; // NONRETAINED, since db or block retains us
67 SecDbTransactionSource source
;
69 int maybeCorruptedCode
;
71 CFErrorRef corruptionError
;
73 // Pending deletions and additions for the current transaction
74 // Entires are either:
75 // 1) a CFArrayRef of 1 element representing a deletion,
76 // 2) a CFArrayRef of 2 elements representing the element 0 having been replaced with element 1
77 // 3) a CFTypeRef that is not a CFArrayRef, representing an add of the element in question.
78 CFMutableArrayRef changes
;
81 struct __OpaqueSecDb
{
85 dispatch_queue_t queue
;
86 dispatch_queue_t commitQueue
;
87 CFMutableArrayRef connections
;
88 dispatch_semaphore_t write_semaphore
;
89 dispatch_semaphore_t read_semaphore
;
91 bool (^opened
)(SecDbRef db
, SecDbConnectionRef dbconn
, bool didCreate
, bool *callMeAgainForNextConnection
, CFErrorRef
*error
);
92 bool callOpenedHandlerForNextConnection
;
93 CFMutableArrayRef notifyPhase
; /* array of SecDBNotifyBlock */
94 mode_t mode
; /* database file permissions, default 0600 */
95 bool readWrite
; /* open database read-write, default true */
96 bool allowRepair
; /* allow database repair, default true */
97 bool useWAL
; /* use WAL mode, default true */
100 // MARK: Error domains and error helper functions
102 CFStringRef kSecDbErrorDomain
= CFSTR("com.apple.utilities.sqlite3");
104 bool SecDbError(int sql_code
, CFErrorRef
*error
, CFStringRef format
, ...) {
105 if (sql_code
== SQLITE_OK
) return true;
109 CFIndex code
= sql_code
;
110 CFErrorRef previousError
= *error
;
113 va_start(args
, format
);
114 SecCFCreateErrorWithFormatAndArguments(code
, kSecDbErrorDomain
, previousError
, error
, NULL
, format
, args
);
120 bool SecDbErrorWithDb(int sql_code
, sqlite3
*db
, CFErrorRef
*error
, CFStringRef format
, ...) {
121 if (sql_code
== SQLITE_OK
) return true;
124 va_start(args
, format
);
125 CFStringRef message
= CFStringCreateWithFormatAndArguments(kCFAllocatorDefault
, NULL
, format
, args
);
127 CFStringRef errno_code
= NULL
;
129 if (sql_code
== SQLITE_CANTOPEN
) {
130 int errno_number
= sqlite3_system_errno(db
);
131 errno_code
= CFStringCreateWithFormat(NULL
, NULL
, CFSTR("%d"), errno_number
);
133 errno_code
= CFRetain(CFSTR(""));
136 int extended_code
= sqlite3_extended_errcode(db
);
137 if (sql_code
== extended_code
)
138 SecDbError(sql_code
, error
, CFSTR("%@: [%d]%@ %s"), message
, sql_code
, errno_code
, sqlite3_errmsg(db
));
140 SecDbError(sql_code
, error
, CFSTR("%@: [%d->%d]%@ %s"), message
, sql_code
, extended_code
, errno_code
, sqlite3_errmsg(db
));
141 CFReleaseSafe(message
);
142 CFReleaseSafe(errno_code
);
147 bool SecDbErrorWithStmt(int sql_code
, sqlite3_stmt
*stmt
, CFErrorRef
*error
, CFStringRef format
, ...) {
148 if (sql_code
== SQLITE_OK
) return true;
151 va_start(args
, format
);
152 CFStringRef message
= CFStringCreateWithFormatAndArguments(kCFAllocatorDefault
, NULL
, format
, args
);
155 sqlite3
*db
= sqlite3_db_handle(stmt
);
156 const char *sql
= sqlite3_sql(stmt
);
157 int extended_code
= sqlite3_extended_errcode(db
);
158 if (sql_code
== extended_code
)
159 SecDbError(sql_code
, error
, CFSTR("%@: [%d] %s sql: %s"), message
, sql_code
, sqlite3_errmsg(db
), sql
);
161 SecDbError(sql_code
, error
, CFSTR("%@: [%d->%d] %s sql: %s"), message
, sql_code
, extended_code
, sqlite3_errmsg(db
), sql
);
162 CFReleaseSafe(message
);
167 // A callback for the sqlite3_log() interface.
168 static void sqlite3Log(void *pArg
, int iErrCode
, const char *zMsg
){
169 secdebug("sqlite3", "(%d) %s", iErrCode
, zMsg
);
172 void _SecDbServerSetup(void)
174 static dispatch_once_t onceToken
;
175 dispatch_once(&onceToken
, ^{
176 int rx
= sqlite3_config(SQLITE_CONFIG_LOG
, sqlite3Log
, NULL
);
177 if (SQLITE_OK
!= rx
) {
178 secwarning("Could not set up sqlite global error logging to syslog: %d", rx
);
185 // MARK: Static helper functions
187 static bool SecDbOpenHandle(SecDbConnectionRef dbconn
, bool *created
, CFErrorRef
*error
);
188 static bool SecDbHandleCorrupt(SecDbConnectionRef dbconn
, int rc
, CFErrorRef
*error
);
191 #pragma mark SecDbRef
194 SecDbCopyFormatDescription(CFTypeRef value
, CFDictionaryRef formatOptions
)
196 SecDbRef db
= (SecDbRef
)value
;
197 return CFStringCreateWithFormat(kCFAllocatorDefault
, NULL
, CFSTR("<SecDb path:%@ connections: %@>"), db
->db_path
, db
->connections
);
202 SecDbDestroy(CFTypeRef value
)
204 SecDbRef db
= (SecDbRef
)value
;
205 CFReleaseNull(db
->connections
);
206 CFReleaseNull(db
->db_path
);
208 dispatch_release(db
->queue
);
211 if (db
->commitQueue
) {
212 dispatch_release(db
->commitQueue
);
213 db
->commitQueue
= NULL
;
215 if (db
->read_semaphore
) {
216 dispatch_release(db
->read_semaphore
);
217 db
->read_semaphore
= NULL
;
219 if (db
->write_semaphore
) {
220 dispatch_release(db
->write_semaphore
);
221 db
->write_semaphore
= NULL
;
224 Block_release(db
->opened
);
227 CFReleaseNull(db
->notifyPhase
);
233 SecDbCreateWithOptions(CFStringRef dbName
, mode_t mode
, bool readWrite
, bool allowRepair
, bool useWAL
,
234 bool (^opened
)(SecDbRef db
, SecDbConnectionRef dbconn
, bool didCreate
, bool *callMeAgainForNextConnection
, CFErrorRef
*error
))
238 db
= CFTypeAllocate(SecDb
, struct __OpaqueSecDb
, kCFAllocatorDefault
);
239 require(db
!= NULL
, done
);
241 CFStringPerformWithCString(dbName
, ^(const char *dbNameStr
) {
242 db
->queue
= dispatch_queue_create(dbNameStr
, DISPATCH_QUEUE_SERIAL
);
244 CFStringRef commitQueueStr
= CFStringCreateWithFormat(kCFAllocatorDefault
, NULL
, CFSTR("%@-commit"), dbName
);
245 CFStringPerformWithCString(commitQueueStr
, ^(const char *cqNameStr
) {
246 db
->commitQueue
= dispatch_queue_create(cqNameStr
, DISPATCH_QUEUE_CONCURRENT
);
248 CFReleaseNull(commitQueueStr
);
249 db
->read_semaphore
= dispatch_semaphore_create(kSecDbMaxReaders
);
250 db
->write_semaphore
= dispatch_semaphore_create(kSecDbMaxWriters
);
251 db
->connections
= CFArrayCreateMutableForCFTypes(kCFAllocatorDefault
);
252 db
->opened
= opened
? Block_copy(opened
) : NULL
;
253 if (getenv("__OSINSTALL_ENVIRONMENT") != NULL
) {
254 // TODO: Move this code out of this layer
255 secinfo("#SecDB", "SecDB: running from installer");
257 db
->db_path
= CFSTR("file::memory:?cache=shared");
259 db
->db_path
= CFStringCreateCopy(kCFAllocatorDefault
, dbName
);
262 db
->readWrite
= readWrite
;
263 db
->allowRepair
= allowRepair
;
271 SecDbCreate(CFStringRef dbName
,
272 bool (^opened
)(SecDbRef db
, SecDbConnectionRef dbconn
, bool didCreate
, bool *callMeAgainForNextConnection
, CFErrorRef
*error
))
274 return SecDbCreateWithOptions(dbName
, 0600, true, true, true, opened
);
278 SecDbIdleConnectionCount(SecDbRef db
) {
279 __block CFIndex count
= 0;
280 dispatch_sync(db
->queue
, ^{
281 count
= CFArrayGetCount(db
->connections
);
286 void SecDbAddNotifyPhaseBlock(SecDbRef db
, SecDBNotifyBlock notifyPhase
)
288 // SecDbNotifyPhase seems to mostly be called on the db's commitQueue, and not the db's queue. Therefore, protect the array with that queue.
289 dispatch_sync(db
->commitQueue
, ^{
290 SecDBNotifyBlock block
= Block_copy(notifyPhase
); /* Force the block off the stack */
291 if (db
->notifyPhase
== NULL
) {
292 db
->notifyPhase
= CFArrayCreateMutableForCFTypes(NULL
);
294 CFArrayAppendValue(db
->notifyPhase
, block
);
295 Block_release(block
);
299 static void SecDbNotifyPhase(SecDbConnectionRef dbconn
, SecDbTransactionPhase phase
) {
300 if (CFArrayGetCount(dbconn
->changes
)) {
301 CFArrayRef changes
= dbconn
->changes
;
302 dbconn
->changes
= CFArrayCreateMutableForCFTypes(kCFAllocatorDefault
);
303 if (dbconn
->db
->notifyPhase
) {
304 CFArrayForEach(dbconn
->db
->notifyPhase
, ^(const void *value
) {
305 SecDBNotifyBlock notifyBlock
= (SecDBNotifyBlock
)value
;
306 notifyBlock(dbconn
, phase
, dbconn
->source
, changes
);
309 CFReleaseSafe(changes
);
313 static void SecDbOnNotify(SecDbConnectionRef dbconn
, void (^perform
)()) {
317 CFStringRef
SecDbGetPath(SecDbRef db
) {
326 #pragma mark SecDbConnectionRef
328 static bool SecDbCheckCorrupted(SecDbConnectionRef dbconn
)
330 __block
bool checkDidRun
= false;
331 __block
bool isCorrupted
= false;
332 __block CFErrorRef error
= NULL
;
333 SecDbPrepare(dbconn
, CFSTR("PRAGMA integrity_check"), &error
, ^(sqlite3_stmt
*stmt
) {
334 SecDbStep(dbconn
, stmt
, &error
, ^(bool *stop
) {
335 const char * result
= (const char*)sqlite3_column_text(stmt
, 0);
336 if (!result
|| strncasecmp(result
, "ok", 3) != 0) {
338 secerror("SecDBCheckCorrupted integrity_check returned %s", (result
) ? result
: "NULL");
344 // An error occurred in SecDbPrepare before we could run the block.
346 CFIndex code
= CFErrorGetCode(error
);
347 if (SQLITE_CORRUPT
== code
|| SQLITE_NOTADB
== code
) {
350 secinfo("#SecDB", "#SecDB warning error %{public}@ when running integrity check", error
);
352 // We don't have an error ref if SecDbPrepare has called SecDbConnectionCheckCode,
353 // which then called SecDbHandleCorrupt. That code path is only entered when the
354 // original error was SQLITE_CORRUPT or SQLITE_NOTADB. On other errors, the
355 // CFErrorRef is not cleared and we can just check the code above.
357 secinfo("#SecDB", "#SecDB warning: failed to run integrity check due to corruption");
362 secerror("SecDBCheckCorrupted ran integrity_check, and that didn't return ok");
364 secerror("SecDBCheckCorrupted failed to run integrity check");
367 CFReleaseNull(error
);
372 static bool SecDbDidCreateFirstConnection(SecDbConnectionRef dbconn
, bool didCreate
, CFErrorRef
*error
)
374 secinfo("#SecDB", "#SecDB starting maintenance");
377 // Historical note: this used to check for integrity but that became too slow and caused panics at boot.
378 // Now, just react to SQLite errors when doing an operation. If file on disk is borked it'll tell us right away.
380 if (!dbconn
->isCorrupted
&& dbconn
->db
->opened
) {
381 CFErrorRef localError
= NULL
;
383 dbconn
->db
->callOpenedHandlerForNextConnection
= false;
384 ok
= dbconn
->db
->opened(dbconn
->db
, dbconn
, didCreate
, &dbconn
->db
->callOpenedHandlerForNextConnection
, &localError
);
387 secerror("opened block failed: %@", localError
);
389 if (!dbconn
->isCorrupted
&& error
&& *error
== NULL
) {
394 secerror("opened block failed: error (%@) is being released and lost", localError
);
395 CFReleaseNull(localError
);
399 if (dbconn
->isCorrupted
) {
400 /* SecDbHandleCorrupt must be executed on the db queue so nobody
401 can acquire a new connection while it is running. */
402 if (dispatch_get_current_queue() == dbconn
->db
->queue
) {
403 ok
= SecDbHandleCorrupt(dbconn
, 0, error
);
405 /* We aren't on the db queue already when we get here. It may be
406 unsafe to dispatch this work from the current queue, so log
407 and return false without attempting recovery. */
408 secerror("WARNING: unable to call SecDbHandleCorrupt (not on db queue)");
413 secinfo("#SecDB", "#SecDB starting maintenance");
417 void SecDbCorrupt(SecDbConnectionRef dbconn
, CFErrorRef error
)
419 CFStringRef str
= CFStringCreateWithFormat(NULL
, NULL
, CFSTR("SecDBCorrupt: %@"), error
);
421 char buffer
[1000] = "?";
422 uint32_t errorCode
= 0;
423 CFStringGetCString(str
, buffer
, sizeof(buffer
), kCFStringEncodingUTF8
);
424 os_log_fault(secLogObjForScope("SecEmergency"), "%s", buffer
);
426 errorCode
= (uint32_t)CFErrorGetCode(error
);
427 __security_simulatecrash(str
, __sec_exception_code_CorruptDb(errorCode
));
430 dbconn
->isCorrupted
= true;
431 CFRetainAssign(dbconn
->corruptionError
, error
);
435 static uint8_t knownDbPathIndex(SecDbConnectionRef dbconn
)
438 if(CFEqual(dbconn
->db
->db_path
, CFSTR("/Library/Keychains/keychain-2.db")))
440 if(CFEqual(dbconn
->db
->db_path
, CFSTR("/Library/Keychains/ocspcache.sqlite3")))
442 if(CFEqual(dbconn
->db
->db_path
, CFSTR("/Library/Keychains/TrustStore.sqlite3")))
444 if(CFEqual(dbconn
->db
->db_path
, CFSTR("/Library/Keychains/caissuercache.sqlite3")))
447 /* Unknown DB path */
451 static bool SecDbConnectionCheckCode(SecDbConnectionRef dbconn
, int code
, CFErrorRef
*error
, CFStringRef desc
, ...)
452 CF_FORMAT_FUNCTION(4, 5);
456 // Return true if there was no error, returns false otherwise and set *error to an appropriate CFErrorRef.
457 static bool SecDbConnectionCheckCode(SecDbConnectionRef dbconn
, int code
, CFErrorRef
*error
, CFStringRef desc
, ...) {
458 if (code
== SQLITE_OK
|| code
== SQLITE_DONE
)
463 va_start(args
, desc
);
464 CFStringRef msg
= CFStringCreateWithFormatAndArguments(kCFAllocatorDefault
, NULL
, desc
, args
);
466 SecDbErrorWithDb(code
, dbconn
->handle
, error
, CFSTR("%@"), msg
);
470 dbconn
->hasIOFailure
|= (SQLITE_IOERR
== code
);
472 /* If it's already corrupted, don't try to recover */
473 if (dbconn
->isCorrupted
) {
474 CFStringRef reason
= CFStringCreateWithFormat(kCFAllocatorDefault
, NULL
,
475 CFSTR("SQL DB %@ is corrupted already. Not trying to recover, corruption error was: %d (previously %d)"),
476 dbconn
->db
->db_path
, code
, dbconn
->maybeCorruptedCode
);
477 secerror("%@",reason
);
478 __security_simulatecrash(reason
, __sec_exception_code_TwiceCorruptDb(knownDbPathIndex(dbconn
)));
479 CFReleaseSafe(reason
);
483 // NOTADB means file is garbage, so it's functionally equivalent to corruption
484 dbconn
->isCorrupted
= (SQLITE_CORRUPT
== code
) || (SQLITE_NOTADB
== code
);
485 if (dbconn
->isCorrupted
) {
486 /* Run integrity check and only make dbconn->isCorrupted true and
487 run the corruption handler if the integrity check conclusively fails. */
488 dbconn
->maybeCorruptedCode
= code
;
489 dbconn
->isCorrupted
= SecDbCheckCorrupted(dbconn
);
490 if (dbconn
->isCorrupted
) {
491 secerror("operation returned code: %d integrity check=fail", code
);
492 /* SecDbHandleCorrupt must be executed on the db queue so nobody
493 can acquire a new connection while it is running. */
494 if (dispatch_get_current_queue() == dbconn
->db
->queue
) {
495 (void)SecDbHandleCorrupt(dbconn
, code
, error
);
497 /* We aren't on the db queue already when we get here. It may be
498 unsafe to dispatch this work from the current queue, so log
499 and return false without attempting recovery. */
500 secerror("WARNING: unable to call SecDbHandleCorrupt (not on db queue)");
503 secerror("operation returned code: %d: integrity check=pass", code
);
510 #if HAVE_UNLOCK_NOTIFY
512 static void SecDbUnlockNotify(void **apArg
, int nArg
) {
514 for(i
=0; i
<nArg
; i
++) {
515 dispatch_semaphore_t dsema
= (dispatch_semaphore_t
)apArg
[i
];
516 dispatch_semaphore_signal(dsema
);
520 static bool SecDbWaitForUnlockNotify(SecDbConnectionRef dbconn
, sqlite3_stmt
*stmt
, CFErrorRef
*error
) {
522 dispatch_semaphore_t dsema
= dispatch_semaphore_create(0);
523 rc
= sqlite3_unlock_notify(dbconn
->handle
, SecDbUnlockNotify
, dsema
);
524 assert(rc
== SQLITE_LOCKED
|| rc
== SQLITE_OK
);
525 if (rc
== SQLITE_OK
) {
526 dispatch_semaphore_wait(dsema
, DISPATCH_TIME_FOREVER
);
528 dispatch_release(dsema
);
529 return (rc
== SQLITE_OK
532 ? SecDbErrorWithStmt(rc
, stmt
, error
, CFSTR("sqlite3_unlock_notify"))
533 : SecDbErrorWithDb(rc
, dbconn
->handle
, error
, CFSTR("sqlite3_unlock_notify"))));
538 #define BUSY_TIMEOUT_MS (5 * 60 * 1000) /* 5 minutes */
540 static bool SecDbBusyHandler(SecDbConnectionRef dbconn
, CFErrorRef
*error
) {
541 return SecDbErrorWithDb(sqlite3_busy_timeout(dbconn
->handle
, BUSY_TIMEOUT_MS
), dbconn
->handle
, error
, CFSTR("busy_handler"));
544 static int sleepBackoff
[] = { 10, 20, 50, 100, 250 };
545 static int sumBackoff
[] = { 10, 30, 80, 180, 430 };
546 static int NumberOfSleepBackoff
= sizeof(sleepBackoff
)/sizeof(sleepBackoff
[0]);
548 // Return true causes the operation to be tried again.
549 static bool SecDbWaitIfNeeded(SecDbConnectionRef dbconn
, int s3e
, sqlite3_stmt
*stmt
, CFStringRef desc
, int nTries
, CFErrorRef
*error
) {
550 #if HAVE_UNLOCK_NOTIFY
551 if (s3e
== SQLITE_LOCKED
) { // Optionally check for extended code being SQLITE_LOCKED_SHAREDCACHE
552 return SecDbWaitForUnlockNotify(dbconn
, stmt
, error
))
555 if (((0xFF & s3e
) == SQLITE_BUSY
) || ((0xFF & s3e
) == SQLITE_LOCKED
)) {
556 int totaltimeout
, timeout
;
558 _Static_assert(sizeof(sumBackoff
) == sizeof(sleepBackoff
), "matching arrays not matching");
559 _Static_assert(sizeof(sumBackoff
[0]) == sizeof(sleepBackoff
[0]), "matching arrays not matching");
561 if (nTries
< NumberOfSleepBackoff
) {
562 timeout
= sleepBackoff
[nTries
];
563 totaltimeout
= sumBackoff
[nTries
];
565 timeout
= sleepBackoff
[NumberOfSleepBackoff
- 1];
566 totaltimeout
= sumBackoff
[NumberOfSleepBackoff
- 1] + (timeout
* (nTries
- NumberOfSleepBackoff
));
568 if (totaltimeout
< BUSY_TIMEOUT_MS
) {
569 secinfo("#SecDB", "sqlite busy/locked: %d ntries: %d totaltimeout: %d", s3e
, nTries
, totaltimeout
);
570 sqlite3_sleep(timeout
);
573 secinfo("#SecDB", "sqlite busy/locked: too long: %d ms, giving up", totaltimeout
);
577 return SecDbConnectionCheckCode(dbconn
, s3e
, error
, CFSTR("%@"), desc
);
580 enum SecDbStepResult
{
585 typedef enum SecDbStepResult SecDbStepResult
;
587 static SecDbStepResult
_SecDbStep(SecDbConnectionRef dbconn
, sqlite3_stmt
*stmt
, CFErrorRef
*error
) {
588 assert(stmt
!= NULL
);
592 s3e
= sqlite3_step(stmt
);
593 if (s3e
== SQLITE_ROW
) {
594 return kSecDbRowStep
;
595 } else if (s3e
== SQLITE_DONE
) {
597 ** ^[SQLITE_DONE] means that the statement has finished executing
598 ** successfully. sqlite3_step() should not be called again on this virtual
599 ** machine without first calling [] to reset the virtual
600 ** machine back to its initial state.
603 return kSecDbDoneStep
;
604 } else if (!SecDbWaitIfNeeded(dbconn
, s3e
, stmt
, CFSTR("step"), ntries
, error
)) {
605 return kSecDbErrorStep
;
612 SecDbExec(SecDbConnectionRef dbconn
, CFStringRef sql
, CFErrorRef
*error
)
617 CFStringRef tail
= NULL
;
619 sqlite3_stmt
*stmt
= SecDbCopyStmt(dbconn
, sql
, &tail
, error
);
623 while ((sr
= _SecDbStep(dbconn
, stmt
, error
)) == kSecDbRowStep
);
624 if (sr
== kSecDbErrorStep
)
626 ok
&= SecDbReleaseCachedStmt(dbconn
, sql
, stmt
, error
);
629 // TODO We already have an error here we really just want the left over sql in it's userData
630 ok
= SecDbError(SQLITE_ERROR
, error
, CFSTR("Error with unexecuted sql remaining %@"), sql
);
638 static bool SecDbBeginTransaction(SecDbConnectionRef dbconn
, SecDbTransactionType type
, CFErrorRef
*error
)
643 case kSecDbImmediateTransactionType
:
644 secdebug("db", "SecDbBeginTransaction SecDbBeginTransaction %p", dbconn
);
645 query
= CFSTR("BEGIN IMMEDIATE");
647 case kSecDbExclusiveRemoteSOSTransactionType
:
648 secdebug("db", "SecDbBeginTransaction kSecDbExclusiveRemoteSOSTransactionType %p", dbconn
);
649 dbconn
->source
= kSecDbSOSTransaction
;
650 query
= CFSTR("BEGIN EXCLUSIVE");
652 case kSecDbExclusiveRemoteCKKSTransactionType
:
653 secdebug("db", "SecDbBeginTransaction kSecDbExclusiveRemoteCKKSTransactionType %p", dbconn
);
654 dbconn
->source
= kSecDbCKKSTransaction
;
655 query
= CFSTR("BEGIN EXCLUSIVE");
657 case kSecDbExclusiveTransactionType
:
658 if (type
==kSecDbExclusiveTransactionType
)
659 secdebug("db", "SecDbBeginTransaction kSecDbExclusiveTransactionType %p", dbconn
);
660 query
= CFSTR("BEGIN EXCLUSIVE");
662 case kSecDbNormalTransactionType
:
663 secdebug("db", "SecDbBeginTransaction kSecDbNormalTransactionType %p", dbconn
);
664 query
= CFSTR("BEGIN");
667 secdebug("db", "SecDbBeginTransaction invalid transaction type %lu", type
);
668 ok
= SecDbError(SQLITE_ERROR
, error
, CFSTR("invalid transaction type %d"), (int)type
);
673 if (query
!= NULL
&& sqlite3_get_autocommit(dbconn
->handle
) != 0) {
674 ok
= SecDbExec(dbconn
, query
, error
);
677 dbconn
->inTransaction
= true;
682 static bool SecDbEndTransaction(SecDbConnectionRef dbconn
, bool commit
, CFErrorRef
*error
)
684 __block
bool ok
= true;
685 __block
bool commited
= false;
687 dispatch_block_t notifyAndExec
= ^{
689 //secdebug("db", "SecDbEndTransaction kSecDbTransactionWillCommit %p", dbconn);
690 SecDbNotifyPhase(dbconn
, kSecDbTransactionWillCommit
);
691 commited
= ok
= SecDbExec(dbconn
, CFSTR("END"), error
);
692 //secdebug("db", "SecDbEndTransaction kSecDbTransactionWillCommit %p (after notify)", dbconn);
694 ok
= SecDbExec(dbconn
, CFSTR("ROLLBACK"), error
);
697 dbconn
->inTransaction
= false;
698 SecDbNotifyPhase(dbconn
, commited
? kSecDbTransactionDidCommit
: kSecDbTransactionDidRollback
);
699 secdebug("db", "SecDbEndTransaction %s %p", commited
? "kSecDbTransactionDidCommit" : "kSecDbTransactionDidRollback", dbconn
);
700 dbconn
->source
= kSecDbAPITransaction
;
703 SecDbPerformOnCommitQueue(dbconn
, true, notifyAndExec
);
708 bool SecDbTransaction(SecDbConnectionRef dbconn
, SecDbTransactionType type
,
709 CFErrorRef
*error
, void (^transaction
)(bool *commit
))
714 if (dbconn
->inTransaction
) {
715 transaction(&commit
);
717 secinfo("#SecDB", "#SecDB nested transaction asked to not be committed");
720 ok
= SecDbBeginTransaction(dbconn
, type
, error
);
722 transaction(&commit
);
723 ok
= SecDbEndTransaction(dbconn
, commit
, error
);
730 sqlite3
*SecDbHandle(SecDbConnectionRef dbconn
) {
731 return dbconn
->handle
;
734 bool SecDbStep(SecDbConnectionRef dbconn
, sqlite3_stmt
*stmt
, CFErrorRef
*error
, void (^row
)(bool *stop
)) {
736 switch (_SecDbStep(dbconn
, stmt
, error
)) {
737 case kSecDbErrorStep
:
738 secdebug("db", "kSecDbErrorStep %@", error
?*error
:NULL
);
741 secdebug("db", "kSecDbRowStep %@", error
?*error
:NULL
);
749 SecDbError(SQLITE_ERROR
, error
, CFSTR("SecDbStep SQLITE_ROW returned without a row handler"));
752 secdebug("db", "kSecDbDoneStep %@", error
?*error
:NULL
);
758 bool SecDbCheckpoint(SecDbConnectionRef dbconn
, CFErrorRef
*error
)
760 return SecDbConnectionCheckCode(dbconn
, sqlite3_wal_checkpoint(dbconn
->handle
, NULL
), error
, CFSTR("wal_checkpoint"));
763 static bool SecDbFileControl(SecDbConnectionRef dbconn
, int op
, void *arg
, CFErrorRef
*error
) {
764 return SecDbConnectionCheckCode(dbconn
, sqlite3_file_control(dbconn
->handle
, NULL
, op
, arg
), error
, CFSTR("file_control"));
767 static sqlite3
*_SecDbOpenV2(const char *path
, int flags
, CFErrorRef
*error
) {
768 #if HAVE_UNLOCK_NOTIFY
769 flags
|= SQLITE_OPEN_SHAREDCACHE
;
771 sqlite3
*handle
= NULL
;
772 int s3e
= sqlite3_open_v2(path
, &handle
, flags
, NULL
);
775 SecDbErrorWithDb(s3e
, handle
, error
, CFSTR("open_v2 \"%s\" 0x%X"), path
, flags
);
776 sqlite3_close(handle
);
779 SecDbError(s3e
, error
, CFSTR("open_v2 \"%s\" 0x%X"), path
, flags
);
785 static bool SecDbOpenV2(SecDbConnectionRef dbconn
, const char *path
, int flags
, CFErrorRef
*error
) {
786 return (dbconn
->handle
= _SecDbOpenV2(path
, flags
, error
)) != NULL
;
789 static bool SecDbTruncate(SecDbConnectionRef dbconn
, CFErrorRef
*error
)
791 int flags
= SQLITE_TRUNCATE_AUTOVACUUM_FULL
;
792 if (dbconn
->db
->useWAL
) {
793 flags
|= SQLITE_TRUNCATE_JOURNALMODE_WAL
;
795 __block
bool ok
= SecDbFileControl(dbconn
, SQLITE_TRUNCATE_DATABASE
, &flags
, error
);
797 sqlite3_close(dbconn
->handle
);
798 dbconn
->handle
= NULL
;
799 CFStringPerformWithCString(dbconn
->db
->db_path
, ^(const char *path
) {
801 CFReleaseNull(*error
);
802 if (SecCheckErrno(unlink(path
), error
, CFSTR("unlink %s"), path
)) {
803 ok
= SecDbOpenHandle(dbconn
, NULL
, error
);
807 secinfo("#SecDB", "#SecDB Failed to delete db handle: %{public}@", error
? *error
: NULL
);
815 static bool SecDbHandleCorrupt(SecDbConnectionRef dbconn
, int rc
, CFErrorRef
*error
)
817 if (!dbconn
->db
->allowRepair
) {
818 SecCFCreateErrorWithFormat(rc
, kSecErrnoDomain
, NULL
, error
, NULL
,
819 CFSTR("SecDbHandleCorrupt handled error: [%d] %s"), rc
, strerror(rc
));
820 dbconn
->isCorrupted
= false;
824 // Backup current db.
825 __block
bool didRename
= false;
826 CFStringPerformWithCString(dbconn
->db
->db_path
, ^(const char *db_path
) {
827 sqlite3
*corrupt_db
= NULL
;
828 char buf
[PATH_MAX
+1];
829 snprintf(buf
, sizeof(buf
), "%s-corrupt", db_path
);
830 if (dbconn
->handle
&& (corrupt_db
= _SecDbOpenV2(buf
, SQLITE_OPEN_READWRITE
, error
))) {
832 didRename
= SecDbErrorWithDb(sqlite3_file_control(corrupt_db
, NULL
, SQLITE_FCNTL_PERSIST_WAL
, &on
), corrupt_db
, error
, CFSTR("persist wal"));
833 didRename
&= SecDbErrorWithDb(sqlite3_file_control(corrupt_db
, NULL
, SQLITE_REPLACE_DATABASE
, (void *)dbconn
->handle
), corrupt_db
, error
, CFSTR("replace database"));
834 sqlite3_close(corrupt_db
);
838 secerror("Tried to rename corrupt database at path %@, but we failed: %@, trying explicit rename", dbconn
->db
->db_path
, error
? *error
: NULL
);
840 CFReleaseNull(*error
);
842 // Explicitly close our connection, plus all other open connections to this db.
844 if (dbconn
->handle
) {
845 closed
&= SecDbError(sqlite3_close(dbconn
->handle
), error
, CFSTR("close"));
846 dbconn
->handle
= NULL
;
848 SecDbRef db
= dbconn
->db
;
849 CFIndex idx
, count
= (db
->connections
) ? CFArrayGetCount(db
->connections
) : 0;
850 for (idx
= 0; idx
< count
; idx
++) {
851 SecDbConnectionRef dbconn
= (SecDbConnectionRef
) CFArrayGetValueAtIndex(db
->connections
, idx
);
852 if (dbconn
&& dbconn
->handle
) {
853 closed
&= SecDbError(sqlite3_close(dbconn
->handle
), error
, CFSTR("close"));
854 dbconn
->handle
= NULL
;
857 CFArrayRemoveAllValues(db
->connections
);
859 // Attempt rename only if all connections closed successfully.
861 if (SecCheckErrno(rename(db_path
, buf
), error
, CFSTR("rename %s %s"), db_path
, buf
)) {
862 if (SecDbOpenHandle(dbconn
, NULL
, error
)) {
869 secerror("Database at path %@ is corrupt. Copied it to %s for further investigation.", dbconn
->db
->db_path
, buf
);
871 seccritical("Tried to copy corrupt database at path %@, but we failed: %@", dbconn
->db
->db_path
, error
? *error
: NULL
);
875 bool ok
= (didRename
&&
876 (dbconn
->handle
|| SecDbOpenHandle(dbconn
, NULL
, error
)) &&
877 SecDbTruncate(dbconn
, error
));
879 // Mark the db as not corrupted, even if something failed.
880 // Always note we are no longer in the corruption handler
881 dbconn
->isCorrupted
= false;
883 // Invoke our callers opened callback, since we just created a new database
884 if (ok
&& dbconn
->db
->opened
) {
885 dbconn
->db
->callOpenedHandlerForNextConnection
= false;
886 ok
= dbconn
->db
->opened(dbconn
->db
, dbconn
, true, &dbconn
->db
->callOpenedHandlerForNextConnection
, error
);
892 static bool SecDbLoggingEnabled(CFStringRef type
)
894 CFTypeRef profile
= NULL
;
895 bool enabled
= false;
897 if (csr_check(CSR_ALLOW_APPLE_INTERNAL
) != 0)
900 profile
= (CFNumberRef
)CFPreferencesCopyValue(CFSTR("SQLProfile"), CFSTR("com.apple.security"), kCFPreferencesAnyUser
, kCFPreferencesAnyHost
);
905 if (CFGetTypeID(profile
) == CFBooleanGetTypeID()) {
906 enabled
= CFBooleanGetValue((CFBooleanRef
)profile
);
907 } else if (CFGetTypeID(profile
) == CFNumberGetTypeID()) {
909 CFNumberGetValue(profile
, kCFNumberSInt32Type
, &num
);
913 CFReleaseSafe(profile
);
919 SecDbProfileMask(void)
921 static dispatch_once_t onceToken
;
922 static unsigned profile_mask
= 0;
924 // sudo defaults write /Library/Preferences/com.apple.security SQLProfile -bool true
925 dispatch_once(&onceToken
, ^{
926 if (SecDbLoggingEnabled(CFSTR("SQLProfile")))
927 profile_mask
= SQLITE_TRACE_PROFILE
;
929 profile_mask
|= SQLITE_TRACE_STMT
;
931 if (SecDbLoggingEnabled(CFSTR("SQLTrace")))
932 profile_mask
= SQLITE_TRACE_STMT
;
934 if (SecDbLoggingEnabled(CFSTR("SQLRow")))
935 profile_mask
= SQLITE_TRACE_ROW
;
936 secinfo("#SecDB", "sqlDb: sql trace mask: 0x%08x", profile_mask
);
942 SecDbTraceV2(unsigned mask
, void *ctx
, void *p
, void *x
) {
943 SecDbConnectionRef dbconn __unused
= ctx
;
944 const char *trace
= "unknown";
946 if (mask
== SQLITE_TRACE_PROFILE
)
947 trace
= sqlite3_sql(p
);
948 else if (mask
== SQLITE_TRACE_STMT
) {
949 trace
= sqlite3_sql(p
);
950 } else if (mask
== SQLITE_TRACE_ROW
) {
951 trace
= sqlite3_expanded_sql(p
);
954 secinfo("#SecDB", "#SecDB %{public}s", trace
);
959 static bool SecDbOpenHandle(SecDbConnectionRef dbconn
, bool *created
, CFErrorRef
*error
)
961 __block
bool ok
= true;
963 CFStringPerformWithCString(dbconn
->db
->db_path
, ^(const char *db_path
) {
964 int flags
= (dbconn
->db
->readWrite
) ? SQLITE_OPEN_READWRITE
: SQLITE_OPEN_READONLY
;
965 ok
= created
&& SecDbOpenV2(dbconn
, db_path
, flags
, NULL
);
969 char *tmp
= dirname((char *)db_path
);
971 mode_t omode
= dbconn
->db
->mode
;
972 if (omode
& S_IRUSR
) { omode
|= S_IXUSR
; } // owner can read
973 if (omode
& S_IRGRP
) { omode
|= S_IXGRP
; } // group can read
974 if (omode
& S_IROTH
) { omode
|= S_IXOTH
; } // other can read
975 int errnum
= mkpath_np(tmp
, omode
);
976 if (errnum
!= 0 && errnum
!= EEXIST
) {
977 SecCFCreateErrorWithFormat(errnum
, kSecErrnoDomain
, NULL
, error
, NULL
,
978 CFSTR("mkpath_np %s: [%d] %s"), tmp
, errnum
, strerror(errnum
));
983 // if the enclosing directory is ok, try to create the database.
984 // this forces us to open it read-write, so we'll need to be the owner here.
985 ok
= ok
&& SecDbOpenV2(dbconn
, db_path
, SQLITE_OPEN_READWRITE
| SQLITE_OPEN_CREATE
, error
);
987 chmod(db_path
, dbconn
->db
->mode
); // default: 0600 (S_IRUSR | S_IWUSR)
994 unsigned mask
= SecDbProfileMask();
996 (void)sqlite3_trace_v2(dbconn
->handle
,
1003 ok
= ok
&& SecDbBusyHandler(dbconn
, error
);
1009 static SecDbConnectionRef
1010 SecDbConnectionCreate(SecDbRef db
, bool readOnly
, CFErrorRef
*error
)
1012 SecDbConnectionRef dbconn
= NULL
;
1014 dbconn
= CFTypeAllocate(SecDbConnection
, struct __OpaqueSecDbConnection
, kCFAllocatorDefault
);
1015 require(dbconn
!= NULL
, done
);
1018 dbconn
->readOnly
= readOnly
;
1019 dbconn
->inTransaction
= false;
1020 dbconn
->source
= kSecDbInvalidTransaction
;
1021 dbconn
->isCorrupted
= false;
1022 dbconn
->maybeCorruptedCode
= 0;
1023 dbconn
->hasIOFailure
= false;
1024 dbconn
->corruptionError
= NULL
;
1025 dbconn
->handle
= NULL
;
1026 dbconn
->changes
= CFArrayCreateMutableForCFTypes(kCFAllocatorDefault
);
1032 static bool SecDbConnectionIsReadOnly(SecDbConnectionRef dbconn
) {
1033 return dbconn
->readOnly
;
1036 static void SecDbConectionSetReadOnly(SecDbConnectionRef dbconn
, bool readOnly
) {
1037 dbconn
->readOnly
= readOnly
;
1040 /* Read only connections go to the end of the queue, writeable connections
1041 go to the start of the queue. */
1042 SecDbConnectionRef
SecDbConnectionAcquire(SecDbRef db
, bool readOnly
, CFErrorRef
*error
) {
1044 secinfo("dbconn", "acquire %s connection", readOnly
? "ro" : "rw");
1045 dispatch_semaphore_wait(readOnly
? db
->read_semaphore
: db
->write_semaphore
, DISPATCH_TIME_FOREVER
);
1046 __block SecDbConnectionRef dbconn
= NULL
;
1047 __block
bool ok
= true;
1048 __block
bool ranOpenedHandler
= false;
1049 dispatch_sync(db
->queue
, ^{
1050 if (!db
->didFirstOpen
) {
1051 bool didCreate
= false;
1052 ok
= dbconn
= SecDbConnectionCreate(db
, false, error
);
1053 CFErrorRef localError
= NULL
;
1054 if (ok
&& !SecDbOpenHandle(dbconn
, &didCreate
, &localError
)) {
1055 secerror("Unable to create database: %@", localError
);
1056 if (localError
&& CFEqual(CFErrorGetDomain(localError
), kSecDbErrorDomain
)) {
1057 int code
= (int)CFErrorGetCode(localError
);
1058 dbconn
->isCorrupted
= (SQLITE_CORRUPT
== code
) || (SQLITE_NOTADB
== code
);
1060 // If the open failure isn't due to corruption, propagate the error.
1061 ok
= dbconn
->isCorrupted
;
1062 if (!ok
&& error
&& *error
== NULL
) {
1063 *error
= localError
;
1067 CFReleaseNull(localError
);
1070 db
->didFirstOpen
= ok
= SecDbDidCreateFirstConnection(dbconn
, didCreate
, error
);
1071 ranOpenedHandler
= true;
1074 CFReleaseNull(dbconn
);
1076 /* Try to get one from the cache */
1077 CFIndex count
= CFArrayGetCount(db
->connections
);
1078 while (count
&& !dbconn
) {
1079 CFIndex ix
= readOnly
? count
- 1 : 0;
1080 dbconn
= (SecDbConnectionRef
)CFArrayGetValueAtIndex(db
->connections
, ix
);
1084 secerror("got NULL dbconn at index: %" PRIdCFIndex
" skipping", ix
);
1085 CFArrayRemoveValueAtIndex(db
->connections
, ix
);
1091 /* Make sure the connection we found has the right access */
1092 if (SecDbConnectionIsReadOnly(dbconn
) != readOnly
) {
1093 SecDbConectionSetReadOnly(dbconn
, readOnly
);
1096 /* Nothing found in cache, create a new connection */
1097 bool created
= false;
1098 dbconn
= SecDbConnectionCreate(db
, readOnly
, error
);
1099 if (dbconn
&& !SecDbOpenHandle(dbconn
, &created
, error
)) {
1100 CFReleaseNull(dbconn
);
1104 if (dbconn
&& !ranOpenedHandler
&& dbconn
->db
->opened
) {
1105 dispatch_sync(db
->queue
, ^{
1106 if (dbconn
->db
->callOpenedHandlerForNextConnection
) {
1107 dbconn
->db
->callOpenedHandlerForNextConnection
= false;
1108 if (!dbconn
->db
->opened(db
, dbconn
, false, &dbconn
->db
->callOpenedHandlerForNextConnection
, error
)) {
1109 if (!dbconn
->isCorrupted
|| !SecDbHandleCorrupt(dbconn
, 0, error
)) {
1110 CFReleaseNull(dbconn
);
1118 // If acquire fails we need to signal the semaphore again.
1119 dispatch_semaphore_signal(readOnly
? db
->read_semaphore
: db
->write_semaphore
);
1126 void SecDbConnectionRelease(SecDbConnectionRef dbconn
) {
1128 secerror("called with NULL dbconn");
1131 SecDbRef db
= dbconn
->db
;
1132 secinfo("dbconn", "release %@", dbconn
);
1133 dispatch_sync(db
->queue
, ^{
1134 bool readOnly
= SecDbConnectionIsReadOnly(dbconn
);
1135 if (dbconn
->hasIOFailure
) {
1136 // Something wrong on the file layer (e.g. revoked file descriptor for networked home)
1137 // so we don't trust our existing connections anymore.
1138 CFArrayRemoveAllValues(db
->connections
);
1140 CFIndex count
= CFArrayGetCount(db
->connections
);
1141 // Add back possible writable dbconn to the pool.
1142 CFArrayInsertValueAtIndex(db
->connections
, readOnly
? count
: 0, dbconn
);
1143 // Remove the last (probably read-only) dbconn from the pool.
1144 if (count
>= kSecDbMaxIdleHandles
) {
1145 CFArrayRemoveValueAtIndex(db
->connections
, count
);
1148 // Signal after we have put the connection back in the pool of connections
1149 dispatch_semaphore_signal(readOnly
? db
->read_semaphore
: db
->write_semaphore
);
1155 void SecDbReleaseAllConnections(SecDbRef db
) {
1156 // Force all connections to be removed (e.g. file descriptor no longer valid)
1158 secerror("called with NULL db");
1161 dispatch_sync(db
->queue
, ^{
1162 CFArrayRemoveAllValues(db
->connections
);
1163 dispatch_semaphore_signal(db
->write_semaphore
);
1164 dispatch_semaphore_signal(db
->read_semaphore
);
1168 bool SecDbReplace(SecDbRef db
, CFStringRef newDbPath
, CFErrorRef
*error
) {
1169 // Replace the given database with the contents of the database
1170 // at newDbPath, as an atomic operation on db->queue.
1172 __block
bool result
= false;
1173 if (!db
|| !newDbPath
) {
1174 secerror("called with NULL argument");
1177 dispatch_sync(db
->queue
, ^{
1178 // Explicitly close all database connections
1179 CFIndex idx
, count
= (db
->connections
) ? CFArrayGetCount(db
->connections
) : 0;
1180 for (idx
= 0; idx
< count
; idx
++) {
1181 SecDbConnectionRef dbconn
= (SecDbConnectionRef
) CFArrayGetValueAtIndex(db
->connections
, idx
);
1182 if (dbconn
->handle
) {
1183 sqlite3_close(dbconn
->handle
);
1184 dbconn
->handle
= NULL
;
1187 CFArrayRemoveAllValues(db
->connections
);
1189 __block sqlite3
*old_db
= NULL
;
1190 __block sqlite3
*new_db
= NULL
;
1191 CFStringPerformWithCString(db
->db_path
, ^(const char *db_path
) {
1192 int s3e
= sqlite3_open_v2(db_path
, &old_db
, SQLITE_OPEN_READWRITE
, NULL
);
1193 if (SQLITE_OK
!= s3e
) {
1194 secerror("Unable to open db for writing: %s (error %d)", db_path
, s3e
);
1197 CFStringPerformWithCString(newDbPath
, ^(const char *new_db_path
) {
1198 int s3e
= sqlite3_open_v2(new_db_path
, &new_db
, SQLITE_OPEN_READONLY
, NULL
);
1199 if (SQLITE_OK
!= s3e
) {
1200 secerror("Unable to open db for reading: %s (error %d)", new_db_path
, s3e
);
1203 if (old_db
&& new_db
) {
1204 // Replace old_db with contents of new_db
1205 int s3e
= sqlite3_file_control(old_db
, NULL
, SQLITE_FCNTL_REPLACE_DATABASE
, new_db
);
1206 if (SQLITE_OK
!= s3e
) {
1207 secerror("Unable to replace db: %@ (error %d)", db
->db_path
, s3e
);
1213 sqlite3_close(old_db
);
1216 sqlite3_close(new_db
);
1219 // Signal that connections are available again.
1220 dispatch_semaphore_signal(db
->write_semaphore
);
1221 dispatch_semaphore_signal(db
->read_semaphore
);
1227 bool SecDbPerformRead(SecDbRef db
, CFErrorRef
*error
, void (^perform
)(SecDbConnectionRef dbconn
)) {
1228 SecDbConnectionRef dbconn
= SecDbConnectionAcquire(db
, true, error
);
1229 bool success
= false;
1233 SecDbConnectionRelease(dbconn
);
1238 bool SecDbPerformWrite(SecDbRef db
, CFErrorRef
*error
, void (^perform
)(SecDbConnectionRef dbconn
)) {
1240 SecError(errSecNotAvailable
, error
, CFSTR("failed to get a db handle"));
1243 SecDbConnectionRef dbconn
= SecDbConnectionAcquire(db
, false, error
);
1244 bool success
= false;
1248 SecDbConnectionRelease(dbconn
);
1254 SecDbConnectionCopyFormatDescription(CFTypeRef value
, CFDictionaryRef formatOptions
)
1256 SecDbConnectionRef dbconn
= (SecDbConnectionRef
)value
;
1257 return CFStringCreateWithFormat(kCFAllocatorDefault
, NULL
, CFSTR("<SecDbConnection %s %s>"),
1258 dbconn
->readOnly
? "ro" : "rw", dbconn
->handle
? "open" : "closed");
1262 SecDbConnectionDestroy(CFTypeRef value
)
1264 SecDbConnectionRef dbconn
= (SecDbConnectionRef
)value
;
1265 if (dbconn
->handle
) {
1266 sqlite3_close(dbconn
->handle
);
1269 CFReleaseNull(dbconn
->changes
);
1270 CFReleaseNull(dbconn
->corruptionError
);
1274 void SecDbPerformOnCommitQueue(SecDbConnectionRef dbconn
, bool barrier
, dispatch_block_t perform
) {
1276 dispatch_barrier_sync(dbconn
->db
->commitQueue
, ^{
1280 dispatch_sync(dbconn
->db
->commitQueue
, ^{
1287 // MARK: Bind helpers
1290 bool SecDbBindNull(sqlite3_stmt
*stmt
, int param
, CFErrorRef
*error
) {
1291 bool ok
= SecDbErrorWithStmt(sqlite3_bind_null(stmt
, param
),
1292 stmt
, error
, CFSTR("bind_null[%d]"), param
);
1293 secinfo("bind", "bind_null[%d] error: %@", param
, error
? *error
: NULL
);
1298 bool SecDbBindBlob(sqlite3_stmt
*stmt
, int param
, const void *zData
, size_t n
, void(*xDel
)(void*), CFErrorRef
*error
) {
1300 return SecDbErrorWithStmt(SQLITE_TOOBIG
, stmt
, error
,
1301 CFSTR("bind_blob[%d]: blob bigger than INT_MAX"), param
);
1303 bool ok
= SecDbErrorWithStmt(sqlite3_bind_blob(stmt
, param
, zData
, (int)n
, xDel
),
1304 stmt
, error
, CFSTR("bind_blob[%d]"), param
);
1305 secinfo("bind", "bind_blob[%d]: %.*s: %@", param
, (int)n
, zData
, error
? *error
: NULL
);
1309 bool SecDbBindText(sqlite3_stmt
*stmt
, int param
, const char *zData
, size_t n
, void(*xDel
)(void*), CFErrorRef
*error
) {
1311 return SecDbErrorWithStmt(SQLITE_TOOBIG
, stmt
, error
,
1312 CFSTR("bind_text[%d]: text bigger than INT_MAX"), param
);
1314 bool ok
= SecDbErrorWithStmt(sqlite3_bind_text(stmt
, param
, zData
, (int)n
, xDel
), stmt
, error
,
1315 CFSTR("bind_text[%d]"), param
);
1316 secinfo("bind", "bind_text[%d]: \"%s\" error: %@", param
, zData
, error
? *error
: NULL
);
1320 bool SecDbBindDouble(sqlite3_stmt
*stmt
, int param
, double value
, CFErrorRef
*error
) {
1321 bool ok
= SecDbErrorWithStmt(sqlite3_bind_double(stmt
, param
, value
), stmt
, error
,
1322 CFSTR("bind_double[%d]"), param
);
1323 secinfo("bind", "bind_double[%d]: %f error: %@", param
, value
, error
? *error
: NULL
);
1327 bool SecDbBindInt(sqlite3_stmt
*stmt
, int param
, int value
, CFErrorRef
*error
) {
1328 bool ok
= SecDbErrorWithStmt(sqlite3_bind_int(stmt
, param
, value
), stmt
, error
,
1329 CFSTR("bind_int[%d]"), param
);
1330 secinfo("bind", "bind_int[%d]: %d error: %@", param
, value
, error
? *error
: NULL
);
1334 bool SecDbBindInt64(sqlite3_stmt
*stmt
, int param
, sqlite3_int64 value
, CFErrorRef
*error
) {
1335 bool ok
= SecDbErrorWithStmt(sqlite3_bind_int64(stmt
, param
, value
), stmt
, error
,
1336 CFSTR("bind_int64[%d]"), param
);
1337 secinfo("bind", "bind_int64[%d]: %lld error: %@", param
, value
, error
? *error
: NULL
);
1342 /* AUDIT[securityd](done):
1343 value (ok) is a caller provided, non NULL CFTypeRef.
1345 bool SecDbBindObject(sqlite3_stmt
*stmt
, int param
, CFTypeRef value
, CFErrorRef
*error
) {
1347 __block
bool result
= false;
1349 /* TODO: Can we use SQLITE_STATIC below everwhere we currently use
1350 SQLITE_TRANSIENT since we finalize the statement before the value
1351 goes out of scope? */
1352 if (!value
|| (valueId
= CFGetTypeID(value
)) == CFNullGetTypeID()) {
1353 /* Skip bindings for NULL values. sqlite3 will interpret unbound
1354 params as NULL which is exactly what we want. */
1358 result
= SecDbBindNull(stmt
, param
, error
);
1360 } else if (valueId
== CFStringGetTypeID()) {
1361 CFStringPerformWithCStringAndLength(value
, ^(const char *cstr
, size_t clen
) {
1362 result
= SecDbBindText(stmt
, param
, cstr
, clen
, SQLITE_TRANSIENT
, error
);
1364 } else if (valueId
== CFDataGetTypeID()) {
1365 CFIndex len
= CFDataGetLength(value
);
1367 result
= SecDbBindBlob(stmt
, param
, CFDataGetBytePtr(value
),
1368 len
, SQLITE_TRANSIENT
, error
);
1370 result
= SecDbBindText(stmt
, param
, "", 0, SQLITE_TRANSIENT
, error
);
1372 } else if (valueId
== CFDateGetTypeID()) {
1373 CFAbsoluteTime abs_time
= CFDateGetAbsoluteTime(value
);
1374 result
= SecDbBindDouble(stmt
, param
, abs_time
, error
);
1375 } else if (valueId
== CFBooleanGetTypeID()) {
1376 int bval
= CFBooleanGetValue(value
);
1377 result
= SecDbBindInt(stmt
, param
, bval
, error
);
1378 } else if (valueId
== CFNumberGetTypeID()) {
1380 if (CFNumberIsFloatType(value
)) {
1382 convertOk
= CFNumberGetValue(value
, kCFNumberDoubleType
, &nval
);
1383 result
= SecDbBindDouble(stmt
, param
, nval
, error
);
1386 convertOk
= CFNumberGetValue(value
, kCFNumberSInt32Type
, &nval
);
1388 result
= SecDbBindInt(stmt
, param
, nval
, error
);
1390 sqlite_int64 nval64
;
1391 convertOk
= CFNumberGetValue(value
, kCFNumberSInt64Type
, &nval64
);
1393 result
= SecDbBindInt64(stmt
, param
, nval64
, error
);
1397 result
= SecDbError(SQLITE_INTERNAL
, error
, CFSTR("bind CFNumberGetValue failed for %@"), value
);
1401 CFStringRef valueDesc
= CFCopyTypeIDDescription(valueId
);
1402 SecDbError(SQLITE_MISMATCH
, error
, CFSTR("bind unsupported type %@"), valueDesc
);
1403 CFReleaseSafe(valueDesc
);
1411 // MARK: SecDbStatementRef
1413 bool SecDbReset(sqlite3_stmt
*stmt
, CFErrorRef
*error
) {
1414 return SecDbErrorWithStmt(sqlite3_reset(stmt
), stmt
, error
, CFSTR("reset"));
1417 bool SecDbClearBindings(sqlite3_stmt
*stmt
, CFErrorRef
*error
) {
1418 return SecDbErrorWithStmt(sqlite3_clear_bindings(stmt
), stmt
, error
, CFSTR("clear bindings"));
1421 bool SecDbFinalize(sqlite3_stmt
*stmt
, CFErrorRef
*error
) {
1422 sqlite3
*handle
= sqlite3_db_handle(stmt
);
1423 int s3e
= sqlite3_finalize(stmt
);
1424 return s3e
== SQLITE_OK
? true : SecDbErrorWithDb(s3e
, handle
, error
, CFSTR("finalize: %p"), stmt
);
1427 sqlite3_stmt
*SecDbPrepareV2(SecDbConnectionRef dbconn
, const char *sql
, size_t sqlLen
, const char **sqlTail
, CFErrorRef
*error
) {
1428 sqlite3
*db
= SecDbHandle(dbconn
);
1429 if (sqlLen
> INT_MAX
) {
1430 SecDbErrorWithDb(SQLITE_TOOBIG
, db
, error
, CFSTR("prepare_v2: sql bigger than INT_MAX"));
1435 sqlite3_stmt
*stmt
= NULL
;
1436 int s3e
= sqlite3_prepare_v2(db
, sql
, (int)sqlLen
, &stmt
, sqlTail
);
1437 if (s3e
== SQLITE_OK
)
1439 else if (!SecDbWaitIfNeeded(dbconn
, s3e
, NULL
, CFSTR("preparev2"), ntries
, error
))
1445 static sqlite3_stmt
*SecDbCopyStatementWithTailRange(SecDbConnectionRef dbconn
, CFStringRef sql
, CFRange
*sqlTail
, CFErrorRef
*error
) {
1446 __block sqlite3_stmt
*stmt
= NULL
;
1447 if (sql
) CFStringPerformWithCStringAndLength(sql
, ^(const char *sqlStr
, size_t sqlLen
) {
1448 const char *tail
= NULL
;
1449 stmt
= SecDbPrepareV2(dbconn
, sqlStr
, sqlLen
, &tail
, error
);
1450 if (sqlTail
&& sqlStr
< tail
&& tail
< sqlStr
+ sqlLen
) {
1451 sqlTail
->location
= tail
- sqlStr
;
1452 sqlTail
->length
= sqlLen
- sqlTail
->location
;
1459 sqlite3_stmt
*SecDbCopyStmt(SecDbConnectionRef dbconn
, CFStringRef sql
, CFStringRef
*tail
, CFErrorRef
*error
) {
1460 // TODO: Add caching and cache lookup of statements
1461 CFRange sqlTail
= {};
1462 sqlite3_stmt
*stmt
= SecDbCopyStatementWithTailRange(dbconn
, sql
, &sqlTail
, error
);
1463 if (sqlTail
.length
> 0) {
1464 CFStringRef excess
= CFStringCreateWithSubstring(CFGetAllocator(sql
), sql
, sqlTail
);
1468 SecDbError(SQLITE_INTERNAL
, error
,
1469 CFSTR("prepare_v2: %@ unused sql: %@"),
1471 CFReleaseSafe(excess
);
1472 SecDbFinalize(stmt
, error
);
1480 TODO: Could do a hack here with a custom kCFAllocatorNULL allocator for a second CFRuntimeBase inside a SecDbStatement,
1481 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. */
1482 bool SecDbReleaseCachedStmt(SecDbConnectionRef dbconn
, CFStringRef sql
, sqlite3_stmt
*stmt
, CFErrorRef
*error
) {
1484 return SecDbFinalize(stmt
, error
);
1489 bool SecDbPrepare(SecDbConnectionRef dbconn
, CFStringRef sql
, CFErrorRef
*error
, void(^exec
)(sqlite3_stmt
*stmt
)) {
1490 assert(sql
!= NULL
);
1491 sqlite3_stmt
*stmt
= SecDbCopyStmt(dbconn
, sql
, NULL
, error
);
1496 return SecDbReleaseCachedStmt(dbconn
, sql
, stmt
, error
);
1499 bool SecDbWithSQL(SecDbConnectionRef dbconn
, CFStringRef sql
, CFErrorRef
*error
, bool(^perform
)(sqlite3_stmt
*stmt
)) {
1503 CFStringRef tail
= NULL
;
1505 sqlite3_stmt
*stmt
= SecDbCopyStmt(dbconn
, sql
, &tail
, error
);
1511 // TODO: Use a different error scope here.
1512 ok
= SecError(-50 /* errSecParam */, error
, CFSTR("SecDbWithSQL perform block missing"));
1514 ok
&= SecDbReleaseCachedStmt(dbconn
, sql
, stmt
, error
);
1517 // TODO We already have an error here we really just want the left over sql in it's userData
1518 ok
= SecDbError(SQLITE_ERROR
, error
, CFSTR("Error with unexecuted sql remaining %@"), sql
);
1527 /* SecDbForEach returns true if all SQLITE_ROW returns of sqlite3_step() return true from the row block.
1528 If the row block returns false and doesn't set an error (to indicate it has reached a limit),
1529 this entire function returns false. In that case no error will be set. */
1530 bool SecDbForEach(SecDbConnectionRef dbconn
, sqlite3_stmt
*stmt
, CFErrorRef
*error
, bool(^row
)(int row_index
)) {
1531 bool result
= false;
1532 for (int row_ix
= 0;;++row_ix
) {
1533 int s3e
= sqlite3_step(stmt
);
1534 if (s3e
== SQLITE_ROW
) {
1540 // If we have no row block then getting SQLITE_ROW is an error
1541 SecDbError(s3e
, error
,
1542 CFSTR("step[%d]: %s returned SQLITE_ROW with NULL row block"),
1543 row_ix
, sqlite3_sql(stmt
));
1546 if (s3e
== SQLITE_DONE
) {
1549 dbconn
->hasIOFailure
|= (s3e
== SQLITE_IOERR
);
1550 SecDbErrorWithStmt(s3e
, stmt
, error
, CFSTR("step[%d]"), row_ix
);
1558 bool SecDbForEach(sqlite3_stmt
*stmt
, CFErrorRef
*error
, bool(^row
)(int row_index
)) {
1561 switch (_SecDbStep(dbconn
, stmt
, error
)) {
1562 case kSecDbErrorStep
:
1569 SecDbError(SQLITE_ERROR
, error
, CFSTR("SecDbStep SQLITE_ROW returned without a row handler"));
1572 case kSecDbDoneStep
:
1579 void SecDbRecordChange(SecDbConnectionRef dbconn
, CFTypeRef deleted
, CFTypeRef inserted
) {
1580 if (!dbconn
->db
->notifyPhase
) return;
1581 CFTypeRef entry
= SecDbEventCreateWithComponents(deleted
, inserted
);
1583 CFArrayAppendValue(dbconn
->changes
, entry
);
1586 if (!dbconn
->inTransaction
) {
1587 secerror("db %@ changed outside txn", dbconn
);
1588 // Only notify of DidCommit, since WillCommit code assumes
1590 SecDbOnNotify(dbconn
, ^{
1591 SecDbNotifyPhase(dbconn
, kSecDbTransactionDidCommit
);
1598 CFGiblisFor(SecDbConnection
)
1601 // SecDbEvent Creation and consumption
1604 static SecDbEventRef
SecDbEventCreateInsert(CFTypeRef inserted
) {
1605 return CFRetainSafe(inserted
);
1608 static SecDbEventRef
SecDbEventCreateDelete(CFTypeRef deleted
) {
1609 return CFArrayCreate(kCFAllocatorDefault
, &deleted
, 1, &kCFTypeArrayCallBacks
);
1612 static SecDbEventRef
SecDbEventCreateUpdate(CFTypeRef deleted
, CFTypeRef inserted
) {
1613 const void *values
[2] = { deleted
, inserted
};
1614 return CFArrayCreate(kCFAllocatorDefault
, values
, 2, &kCFTypeArrayCallBacks
);
1617 SecDbEventRef
SecDbEventCreateWithComponents(CFTypeRef deleted
, CFTypeRef inserted
) {
1618 if (deleted
&& inserted
)
1619 return SecDbEventCreateUpdate(deleted
, inserted
);
1621 return SecDbEventCreateDelete(deleted
);
1623 return SecDbEventCreateInsert(inserted
);
1628 void SecDbEventTranslateComponents(SecDbEventRef item
, CFTypeRef
* deleted
, CFTypeRef
* inserted
) {
1629 if(CFGetTypeID(item
) == CFArrayGetTypeID()) {
1630 // One item: deletion. Two: update.
1631 CFIndex arraySize
= CFArrayGetCount(item
);
1632 if(arraySize
== 1) {
1633 if(deleted
) { *deleted
= CFArrayGetValueAtIndex(item
, 0); }
1634 if(inserted
) { *inserted
= NULL
; }
1635 } else if(arraySize
== 2) {
1636 if(deleted
) { *deleted
= CFArrayGetValueAtIndex(item
, 0); }
1637 if(inserted
) { *inserted
= CFArrayGetValueAtIndex(item
, 1); }
1639 if(deleted
) { *deleted
= NULL
; }
1640 if(inserted
) { *inserted
= NULL
; }
1643 if(deleted
) { *deleted
= NULL
; }
1644 if(inserted
) { *inserted
= item
; }
1649 bool SecDbEventGetComponents(SecDbEventRef event
, CFTypeRef
*deleted
, CFTypeRef
*inserted
, CFErrorRef
*error
) {
1650 if (isArray(event
)) {
1651 CFArrayRef array
= event
;
1652 switch (CFArrayGetCount(array
)) {
1654 *deleted
= CFArrayGetValueAtIndex(array
, 0);
1655 *inserted
= CFArrayGetValueAtIndex(array
, 1);
1658 *deleted
= CFArrayGetValueAtIndex(array
, 0);
1662 SecError(errSecParam
, error
, NULL
, CFSTR("invalid entry in changes array: %@"), array
);